Skip to content

Commit 82bd606

Browse files
committed
Add Apple M4 host detection
Also add support for older ARM families (this is likely never going to get used, since only macOS is officially supported as host OS, but nice to have for completeness sake). Error handling (checking CPUFAMILY_UNKNOWN) is also included here. Finally, add links to extra documentation to make it easier for others to update this in the future.
1 parent 73bebf9 commit 82bd606

File tree

1 file changed

+62
-4
lines changed

1 file changed

+62
-4
lines changed

llvm/lib/TargetParser/Host.cpp

+62-4
Original file line numberDiff line numberDiff line change
@@ -1460,6 +1460,18 @@ StringRef sys::getHostCPUName() {
14601460
return getCPUNameFromS390Model(Id, HaveVectorSupport);
14611461
}
14621462
#elif defined(__APPLE__) && (defined(__arm__) || defined(__aarch64__))
1463+
// Copied from <mach/machine.h> in the macOS SDK.
1464+
//
1465+
// Also available here, though usually not as up-to-date:
1466+
// https://github.com/apple-oss-distributions/xnu/blob/xnu-11215.41.3/osfmk/mach/machine.h#L403-L452.
1467+
#define CPUFAMILY_UNKNOWN 0
1468+
#define CPUFAMILY_ARM_9 0xe73283ae
1469+
#define CPUFAMILY_ARM_11 0x8ff620d8
1470+
#define CPUFAMILY_ARM_XSCALE 0x53b005f5
1471+
#define CPUFAMILY_ARM_12 0xbd1b0ae9
1472+
#define CPUFAMILY_ARM_13 0x0cc90e64
1473+
#define CPUFAMILY_ARM_14 0x96077ef1
1474+
#define CPUFAMILY_ARM_15 0xa8511bca
14631475
#define CPUFAMILY_ARM_SWIFT 0x1e2d6381
14641476
#define CPUFAMILY_ARM_CYCLONE 0x37a09642
14651477
#define CPUFAMILY_ARM_TYPHOON 0x2c91a47e
@@ -1471,13 +1483,45 @@ StringRef sys::getHostCPUName() {
14711483
#define CPUFAMILY_ARM_FIRESTORM_ICESTORM 0x1b588bb3
14721484
#define CPUFAMILY_ARM_BLIZZARD_AVALANCHE 0xda33d83d
14731485
#define CPUFAMILY_ARM_EVEREST_SAWTOOTH 0x8765edea
1486+
#define CPUFAMILY_ARM_IBIZA 0xfa33415e
1487+
#define CPUFAMILY_ARM_PALMA 0x72015832
1488+
#define CPUFAMILY_ARM_COLL 0x2876f5b5
1489+
#define CPUFAMILY_ARM_LOBOS 0x5f4dea93
1490+
#define CPUFAMILY_ARM_DONAN 0x6f5129ac
1491+
#define CPUFAMILY_ARM_TAHITI 0x75d4acb9
1492+
#define CPUFAMILY_ARM_TUPAI 0x204526d0
14741493

14751494
StringRef sys::getHostCPUName() {
14761495
uint32_t Family;
14771496
size_t Length = sizeof(Family);
14781497
sysctlbyname("hw.cpufamily", &Family, &Length, NULL, 0);
14791498

1499+
// This is found by testing on actual hardware, and by looking at:
1500+
// https://github.com/apple-oss-distributions/xnu/blob/xnu-11215.41.3/osfmk/arm/cpuid.c#L109-L231.
1501+
//
1502+
// Another great resource is
1503+
// https://github.com/AsahiLinux/docs/wiki/Codenames.
1504+
//
1505+
// NOTE: We choose to return `apple-mX` instead of `apple-aX`, since the M1,
1506+
// M2, M3 etc. aliases are more widely known to users than A14, A15, A16 etc.
1507+
// (and this code is basically only used on host macOS anyways).
14801508
switch (Family) {
1509+
case CPUFAMILY_UNKNOWN:
1510+
return "invalid";
1511+
case CPUFAMILY_ARM_9:
1512+
return "arm920t"; // or arm926ej-s
1513+
case CPUFAMILY_ARM_11:
1514+
return "arm1136jf-s";
1515+
case CPUFAMILY_ARM_XSCALE:
1516+
return "xscale";
1517+
case CPUFAMILY_ARM_12:
1518+
return "invalid"; // Seems unsued by the kernel
1519+
case CPUFAMILY_ARM_13:
1520+
return "cortex-a8";
1521+
case CPUFAMILY_ARM_14:
1522+
return "cortex-a9";
1523+
case CPUFAMILY_ARM_15:
1524+
return "cortex-a7";
14811525
case CPUFAMILY_ARM_SWIFT:
14821526
return "swift";
14831527
case CPUFAMILY_ARM_CYCLONE:
@@ -1495,14 +1539,28 @@ StringRef sys::getHostCPUName() {
14951539
case CPUFAMILY_ARM_LIGHTNING_THUNDER:
14961540
return "apple-a13";
14971541
case CPUFAMILY_ARM_FIRESTORM_ICESTORM:
1498-
return "apple-m1";
1542+
return "apple-m1"; // A14 / M1
14991543
case CPUFAMILY_ARM_BLIZZARD_AVALANCHE:
1500-
return "apple-m2";
1544+
return "apple-m2"; // A15 / M2
15011545
case CPUFAMILY_ARM_EVEREST_SAWTOOTH:
1502-
return "apple-m3";
1546+
return "apple-m3"; // A16
1547+
case CPUFAMILY_ARM_IBIZA:
1548+
return "apple-m3"; // M3
1549+
case CPUFAMILY_ARM_PALMA:
1550+
return "apple-m3"; // M3 Max
1551+
case CPUFAMILY_ARM_COLL:
1552+
return "apple-m3"; // A17
1553+
case CPUFAMILY_ARM_LOBOS:
1554+
return "apple-m3"; // M3 Pro
1555+
case CPUFAMILY_ARM_DONAN:
1556+
return "apple-m4"; // M4
1557+
case CPUFAMILY_ARM_TAHITI:
1558+
return "apple-m4"; // A18 Pro
1559+
case CPUFAMILY_ARM_TUPAI:
1560+
return "apple-m4"; // A18
15031561
default:
15041562
// Default to the newest CPU we know about.
1505-
return "apple-m3";
1563+
return "apple-m4";
15061564
}
15071565
}
15081566
#elif defined(_AIX)

0 commit comments

Comments
 (0)