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

Daniel Paoliello via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 1 16:50:38 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,
----------------
dpaoliello wrote:

Done.

On Linux, the "primary" part is select as the last one added *before* sorting:
https://github.com/llvm/llvm-project/blob/154354e60e46debb68716cde867cb4e325ce5eb7/llvm/lib/TargetParser/Host.cpp#L193-L198

For Windows, I'm selecting the "first" (lowest numbered) part as that corresponds with the BIG cores on my local machine.

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


More information about the llvm-commits mailing list