Skip to content

Commit b4efe4b

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 89e919f commit b4efe4b

File tree

1 file changed

+59
-4
lines changed

1 file changed

+59
-4
lines changed

llvm/lib/TargetParser/Host.cpp

+59-4
Original file line numberDiff line numberDiff line change
@@ -1485,6 +1485,18 @@ StringRef sys::getHostCPUName() {
14851485
return getCPUNameFromS390Model(Id, HaveVectorSupport);
14861486
}
14871487
#elif defined(__APPLE__) && (defined(__arm__) || defined(__aarch64__))
1488+
// Copied from <mach/machine.h> in the macOS SDK.
1489+
//
1490+
// Also available here, though usually not as up-to-date:
1491+
// https://github.com/apple-oss-distributions/xnu/blob/xnu-11215.41.3/osfmk/mach/machine.h#L403-L452.
1492+
#define CPUFAMILY_UNKNOWN 0
1493+
#define CPUFAMILY_ARM_9 0xe73283ae
1494+
#define CPUFAMILY_ARM_11 0x8ff620d8
1495+
#define CPUFAMILY_ARM_XSCALE 0x53b005f5
1496+
#define CPUFAMILY_ARM_12 0xbd1b0ae9
1497+
#define CPUFAMILY_ARM_13 0x0cc90e64
1498+
#define CPUFAMILY_ARM_14 0x96077ef1
1499+
#define CPUFAMILY_ARM_15 0xa8511bca
14881500
#define CPUFAMILY_ARM_SWIFT 0x1e2d6381
14891501
#define CPUFAMILY_ARM_CYCLONE 0x37a09642
14901502
#define CPUFAMILY_ARM_TYPHOON 0x2c91a47e
@@ -1496,13 +1508,46 @@ StringRef sys::getHostCPUName() {
14961508
#define CPUFAMILY_ARM_FIRESTORM_ICESTORM 0x1b588bb3
14971509
#define CPUFAMILY_ARM_BLIZZARD_AVALANCHE 0xda33d83d
14981510
#define CPUFAMILY_ARM_EVEREST_SAWTOOTH 0x8765edea
1511+
#define CPUFAMILY_ARM_IBIZA 0xfa33415e
1512+
#define CPUFAMILY_ARM_PALMA 0x72015832
1513+
#define CPUFAMILY_ARM_COLL 0x2876f5b5
1514+
#define CPUFAMILY_ARM_LOBOS 0x5f4dea93
1515+
#define CPUFAMILY_ARM_DONAN 0x6f5129ac
1516+
#define CPUFAMILY_ARM_BRAVA 0x17d5b93a
1517+
#define CPUFAMILY_ARM_TAHITI 0x75d4acb9
1518+
#define CPUFAMILY_ARM_TUPAI 0x204526d0
14991519

15001520
StringRef sys::getHostCPUName() {
15011521
uint32_t Family;
15021522
size_t Length = sizeof(Family);
15031523
sysctlbyname("hw.cpufamily", &Family, &Length, NULL, 0);
15041524

1525+
// This is found by testing on actual hardware, and by looking at:
1526+
// https://github.com/apple-oss-distributions/xnu/blob/xnu-11215.41.3/osfmk/arm/cpuid.c#L109-L231.
1527+
//
1528+
// Another great resource is
1529+
// https://github.com/AsahiLinux/docs/wiki/Codenames.
1530+
//
1531+
// NOTE: We choose to return `apple-mX` instead of `apple-aX`, since the M1,
1532+
// M2, M3 etc. aliases are more widely known to users than A14, A15, A16 etc.
1533+
// (and this code is basically only used on host macOS anyways).
15051534
switch (Family) {
1535+
case CPUFAMILY_UNKNOWN:
1536+
return "generic";
1537+
case CPUFAMILY_ARM_9:
1538+
return "arm920t"; // or arm926ej-s
1539+
case CPUFAMILY_ARM_11:
1540+
return "arm1136jf-s";
1541+
case CPUFAMILY_ARM_XSCALE:
1542+
return "xscale";
1543+
case CPUFAMILY_ARM_12: // Seems unused by the kernel
1544+
return "generic";
1545+
case CPUFAMILY_ARM_13:
1546+
return "cortex-a8";
1547+
case CPUFAMILY_ARM_14:
1548+
return "cortex-a9";
1549+
case CPUFAMILY_ARM_15:
1550+
return "cortex-a7";
15061551
case CPUFAMILY_ARM_SWIFT:
15071552
return "swift";
15081553
case CPUFAMILY_ARM_CYCLONE:
@@ -1519,15 +1564,25 @@ StringRef sys::getHostCPUName() {
15191564
return "apple-a12";
15201565
case CPUFAMILY_ARM_LIGHTNING_THUNDER:
15211566
return "apple-a13";
1522-
case CPUFAMILY_ARM_FIRESTORM_ICESTORM:
1567+
case CPUFAMILY_ARM_FIRESTORM_ICESTORM: // A14 / M1
15231568
return "apple-m1";
1524-
case CPUFAMILY_ARM_BLIZZARD_AVALANCHE:
1569+
case CPUFAMILY_ARM_BLIZZARD_AVALANCHE: // A15 / M2
15251570
return "apple-m2";
1526-
case CPUFAMILY_ARM_EVEREST_SAWTOOTH:
1571+
case CPUFAMILY_ARM_EVEREST_SAWTOOTH: // A16
1572+
case CPUFAMILY_ARM_IBIZA: // M3
1573+
case CPUFAMILY_ARM_PALMA: // M3 Max
1574+
case CPUFAMILY_ARM_LOBOS: // M3 Pro
15271575
return "apple-m3";
1576+
case CPUFAMILY_ARM_COLL: // A17 Pro
1577+
return "apple-a17";
1578+
case CPUFAMILY_ARM_DONAN: // M4
1579+
case CPUFAMILY_ARM_BRAVA: // M4 Max
1580+
case CPUFAMILY_ARM_TAHITI: // A18 Pro
1581+
case CPUFAMILY_ARM_TUPAI: // A18
1582+
return "apple-m4";
15281583
default:
15291584
// Default to the newest CPU we know about.
1530-
return "apple-m3";
1585+
return "apple-m4";
15311586
}
15321587
}
15331588
#elif defined(_AIX)

0 commit comments

Comments
 (0)