[llvm] [win][aarch64] Add support for detecting the Host CPU on Arm64 Windows (PR #151596)

David Green via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 4 22:51:43 PDT 2025


================
@@ -1450,6 +1462,48 @@ StringRef sys::getHostCPUName() {
   return "generic";
 }
 
+#elif defined(_M_ARM64) || defined(_M_ARM64EC)
+
+union MIDR_EL1 {
+  uint64_t Raw;
+  struct _Parts {
+    uint64_t Revision : 4;
+    uint64_t Partnum : 12;
+    uint64_t Architecture : 4;
+    uint64_t Variant : 4;
+    uint64_t Implementer : 8;
+    uint64_t Reserved : 32;
+  } Parts;
+};
+
+StringRef sys::getHostCPUName() {
+  StringRef CPU = "generic";
+
+  // The "CP 4000" registry key contains a cached copy of the MIDR_EL1 register.
+  HKEY Key;
+  if (RegOpenKeyExA(HKEY_LOCAL_MACHINE,
+                    "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", 0,
+                    KEY_READ, &Key) == ERROR_SUCCESS) {
+    MIDR_EL1 RegValue;
+    DWORD ActualType;
+    DWORD RegValueSize = sizeof(RegValue);
+    if ((RegQueryValueExA(Key, "CP 4000", nullptr, &ActualType,
+                          (PBYTE)&RegValue, &RegValueSize) == ERROR_SUCCESS) &&
+        (ActualType == REG_QWORD) && RegValueSize == sizeof(RegValue)) {
+      auto Part = "0x" + utohexstr(RegValue.Parts.Partnum, /*LowerCase*/ true,
+                                   /*Width*/ 3);
+      CPU = detail::getHostCPUNameForARM(
+          "0x" + utohexstr(RegValue.Parts.Implementer, /*LowerCase*/ true,
----------------
davemgreen wrote:

Oh yes I didn't mean to imply you had to do this, just that this has come up elsewhere recently and it is known not to be ideal which node is picked by default.

So long as you make it mostly like linux (preferably in the same order, picking the same cpu), then that sounds OK to me. If you want to be more explicit about which is picked on a specific system then it is probably best to add a MatchBigLittle or equivalent for it, but for any specific device that is a separate issue.

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


More information about the llvm-commits mailing list