[compiler-rt] [compiler-rt] Add cpu model init for Windows. (PR #111961)

Martin Storsjö via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 11 00:56:02 PDT 2024


================
@@ -0,0 +1,42 @@
+#ifndef _ARM64_
+#define _ARM64_
+#endif
+#include <processthreadsapi.h>
+#include <stdint.h>
+
+void __init_cpu_features_resolver(unsigned long hwcap,
+                                  const __ifunc_arg_t *arg) {}
+
+void  CONSTRUCTOR_ATTRIBUTE __init_cpu_features(void) {
+  if (__atomic_load_n(&__aarch64_cpu_features.features, __ATOMIC_RELAXED))
+    return;
+
+  #define setCPUFeature(F) features |= 1ULL << F
+
+  uint64_t features = 0;
+
+  setCPUFeature(FEAT_INIT);
+  setCPUFeature(FEAT_FP);
+
+  // https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-isprocessorfeaturepresent
+  if (IsProcessorFeaturePresent(PF_ARM_V8_CRC32_INSTRUCTIONS_AVAILABLE))
+    setCPUFeature(FEAT_CRC);
+  if (IsProcessorFeaturePresent(PF_ARM_V81_ATOMIC_INSTRUCTIONS_AVAILABLE))
+    setCPUFeature(FEAT_LSE);
+  if (IsProcessorFeaturePresent(PF_ARM_V82_DP_INSTRUCTIONS_AVAILABLE))
+    setCPUFeature(FEAT_DOTPROD);
+
+  if (IsProcessorFeaturePresent(PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE)) {
+    setCPUFeature(FEAT_AES);
+    setCPUFeature(FEAT_SHA2);
+    setCPUFeature(FEAT_PMULL);
+  }
+  if (IsProcessorFeaturePresent(PF_ARM_V83_JSCVT_INSTRUCTIONS_AVAILABLE))
+    setCPUFeature(FEAT_JSCVT);
+
+  if (IsProcessorFeaturePresent(PF_ARM_V83_LRCPC_INSTRUCTIONS_AVAILABLE))
+    setCPUFeature(FEAT_RCPC);
----------------
mstorsjo wrote:

If we do this unconditionally like this, we require a relatively new SDK for building. `PF_ARM_V83_LRCPC_INSTRUCTIONS_AVAILABLE` is only visible in WinSDK since 10.0.22621.0, and in mingw-w64 headers since https://github.com/mingw-w64/mingw-w64/commit/4dd72e2cef122cfbc825974c28aa83ae18a01a24 (since a year). If we enclose it, at least the last one, in an `#ifdef PF_ARM_V83_LRCPC_INSTRUCTIONS_AVAILABLE`, we wouldn't break people building with slightly older headers.

Looking forward, there's a bunch of more aarch64 feature constants that you can look for since WinSDK 10.0.26100, and quite recently in mingw-w64. They aren't documented in the public MS documentation yet, but they're mostly quite self-documenting - see https://github.com/mingw-w64/mingw-w64/commit/f97814b9602d62cec180aae9e0a0bb5198e7289d for a list of them. If we look for those features, we definitely should wrap each of them in an `#ifdef`.

https://github.com/llvm/llvm-project/pull/111961


More information about the llvm-commits mailing list