Skip to content

Commit a9966d9

Browse files
committed
[llvm] 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. These values should be up to date with Xcode 16.2 beta 3.
1 parent 73bebf9 commit a9966d9

File tree

1 file changed

+58
-4
lines changed

1 file changed

+58
-4
lines changed

llvm/lib/TargetParser/Host.cpp

+58-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,46 @@ 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_BRAVA 0x17d5b93a
1492+
#define CPUFAMILY_ARM_TAHITI 0x75d4acb9
1493+
#define CPUFAMILY_ARM_TUPAI 0x204526d0
14741494

14751495
StringRef sys::getHostCPUName() {
14761496
uint32_t Family;
14771497
size_t Length = sizeof(Family);
14781498
sysctlbyname("hw.cpufamily", &Family, &Length, NULL, 0);
14791499

1500+
// This is found by testing on actual hardware, and by looking at:
1501+
// https://github.com/apple-oss-distributions/xnu/blob/xnu-11215.41.3/osfmk/arm/cpuid.c#L109-L231.
1502+
//
1503+
// Another great resource is
1504+
// https://github.com/AsahiLinux/docs/wiki/Codenames.
1505+
//
1506+
// NOTE: We choose to return `apple-mX` instead of `apple-aX`, since the M1,
1507+
// M2, M3 etc. aliases are more widely known to users than A14, A15, A16 etc.
1508+
// (and this code is basically only used on host macOS anyways).
14801509
switch (Family) {
1510+
case CPUFAMILY_UNKNOWN:
1511+
return "generic";
1512+
case CPUFAMILY_ARM_9:
1513+
return "arm920t"; // or arm926ej-s
1514+
case CPUFAMILY_ARM_11:
1515+
return "arm1136jf-s";
1516+
case CPUFAMILY_ARM_XSCALE:
1517+
return "xscale";
1518+
case CPUFAMILY_ARM_12:
1519+
return "generic"; // Seems unsued by the kernel
1520+
case CPUFAMILY_ARM_13:
1521+
return "cortex-a8";
1522+
case CPUFAMILY_ARM_14:
1523+
return "cortex-a9";
1524+
case CPUFAMILY_ARM_15:
1525+
return "cortex-a7";
14811526
case CPUFAMILY_ARM_SWIFT:
14821527
return "swift";
14831528
case CPUFAMILY_ARM_CYCLONE:
@@ -1494,15 +1539,24 @@ StringRef sys::getHostCPUName() {
14941539
return "apple-a12";
14951540
case CPUFAMILY_ARM_LIGHTNING_THUNDER:
14961541
return "apple-a13";
1497-
case CPUFAMILY_ARM_FIRESTORM_ICESTORM:
1542+
case CPUFAMILY_ARM_FIRESTORM_ICESTORM: // A14 / M1
14981543
return "apple-m1";
1499-
case CPUFAMILY_ARM_BLIZZARD_AVALANCHE:
1544+
case CPUFAMILY_ARM_BLIZZARD_AVALANCHE: // A15 / M2
15001545
return "apple-m2";
1501-
case CPUFAMILY_ARM_EVEREST_SAWTOOTH:
1546+
case CPUFAMILY_ARM_EVEREST_SAWTOOTH: // A16
1547+
case CPUFAMILY_ARM_IBIZA: // M3
1548+
case CPUFAMILY_ARM_PALMA: // M3 Max
1549+
case CPUFAMILY_ARM_COLL: // A17
1550+
case CPUFAMILY_ARM_LOBOS: // M3 Pro
15021551
return "apple-m3";
1552+
case CPUFAMILY_ARM_DONAN: // M4
1553+
case CPUFAMILY_ARM_BRAVA: // M4 Max
1554+
case CPUFAMILY_ARM_TAHITI: // A18 Pro
1555+
case CPUFAMILY_ARM_TUPAI: // A18
1556+
return "apple-m4";
15031557
default:
15041558
// Default to the newest CPU we know about.
1505-
return "apple-m3";
1559+
return "apple-m4";
15061560
}
15071561
}
15081562
#elif defined(_AIX)

0 commit comments

Comments
 (0)