Skip to content

Commit a082cc1

Browse files
authored
Add Apple M4 host detection (#117530)
Add Apple M4 host detection, which fixes rust-lang/rust#133414. 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. NOTE: These values are taken from `mach/machine.h` the Xcode 16.2 SDK, and has been confirmed on an M4 Max in rust-lang/rust#133414 (comment).
1 parent 66d347b commit a082cc1

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
@@ -1509,6 +1509,18 @@ StringRef sys::getHostCPUName() {
15091509
return getCPUNameFromS390Model(Id, HaveVectorSupport);
15101510
}
15111511
#elif defined(__APPLE__) && (defined(__arm__) || defined(__aarch64__))
1512+
// Copied from <mach/machine.h> in the macOS SDK.
1513+
//
1514+
// Also available here, though usually not as up-to-date:
1515+
// https://github.com/apple-oss-distributions/xnu/blob/xnu-11215.41.3/osfmk/mach/machine.h#L403-L452.
1516+
#define CPUFAMILY_UNKNOWN 0
1517+
#define CPUFAMILY_ARM_9 0xe73283ae
1518+
#define CPUFAMILY_ARM_11 0x8ff620d8
1519+
#define CPUFAMILY_ARM_XSCALE 0x53b005f5
1520+
#define CPUFAMILY_ARM_12 0xbd1b0ae9
1521+
#define CPUFAMILY_ARM_13 0x0cc90e64
1522+
#define CPUFAMILY_ARM_14 0x96077ef1
1523+
#define CPUFAMILY_ARM_15 0xa8511bca
15121524
#define CPUFAMILY_ARM_SWIFT 0x1e2d6381
15131525
#define CPUFAMILY_ARM_CYCLONE 0x37a09642
15141526
#define CPUFAMILY_ARM_TYPHOON 0x2c91a47e
@@ -1520,13 +1532,46 @@ StringRef sys::getHostCPUName() {
15201532
#define CPUFAMILY_ARM_FIRESTORM_ICESTORM 0x1b588bb3
15211533
#define CPUFAMILY_ARM_BLIZZARD_AVALANCHE 0xda33d83d
15221534
#define CPUFAMILY_ARM_EVEREST_SAWTOOTH 0x8765edea
1535+
#define CPUFAMILY_ARM_IBIZA 0xfa33415e
1536+
#define CPUFAMILY_ARM_PALMA 0x72015832
1537+
#define CPUFAMILY_ARM_COLL 0x2876f5b5
1538+
#define CPUFAMILY_ARM_LOBOS 0x5f4dea93
1539+
#define CPUFAMILY_ARM_DONAN 0x6f5129ac
1540+
#define CPUFAMILY_ARM_BRAVA 0x17d5b93a
1541+
#define CPUFAMILY_ARM_TAHITI 0x75d4acb9
1542+
#define CPUFAMILY_ARM_TUPAI 0x204526d0
15231543

15241544
StringRef sys::getHostCPUName() {
15251545
uint32_t Family;
15261546
size_t Length = sizeof(Family);
15271547
sysctlbyname("hw.cpufamily", &Family, &Length, NULL, 0);
15281548

1549+
// This is found by testing on actual hardware, and by looking at:
1550+
// https://github.com/apple-oss-distributions/xnu/blob/xnu-11215.41.3/osfmk/arm/cpuid.c#L109-L231.
1551+
//
1552+
// Another great resource is
1553+
// https://github.com/AsahiLinux/docs/wiki/Codenames.
1554+
//
1555+
// NOTE: We choose to return `apple-mX` instead of `apple-aX`, since the M1,
1556+
// M2, M3 etc. aliases are more widely known to users than A14, A15, A16 etc.
1557+
// (and this code is basically only used on host macOS anyways).
15291558
switch (Family) {
1559+
case CPUFAMILY_UNKNOWN:
1560+
return "generic";
1561+
case CPUFAMILY_ARM_9:
1562+
return "arm920t"; // or arm926ej-s
1563+
case CPUFAMILY_ARM_11:
1564+
return "arm1136jf-s";
1565+
case CPUFAMILY_ARM_XSCALE:
1566+
return "xscale";
1567+
case CPUFAMILY_ARM_12: // Seems unused by the kernel
1568+
return "generic";
1569+
case CPUFAMILY_ARM_13:
1570+
return "cortex-a8";
1571+
case CPUFAMILY_ARM_14:
1572+
return "cortex-a9";
1573+
case CPUFAMILY_ARM_15:
1574+
return "cortex-a7";
15301575
case CPUFAMILY_ARM_SWIFT:
15311576
return "swift";
15321577
case CPUFAMILY_ARM_CYCLONE:
@@ -1543,15 +1588,25 @@ StringRef sys::getHostCPUName() {
15431588
return "apple-a12";
15441589
case CPUFAMILY_ARM_LIGHTNING_THUNDER:
15451590
return "apple-a13";
1546-
case CPUFAMILY_ARM_FIRESTORM_ICESTORM:
1591+
case CPUFAMILY_ARM_FIRESTORM_ICESTORM: // A14 / M1
15471592
return "apple-m1";
1548-
case CPUFAMILY_ARM_BLIZZARD_AVALANCHE:
1593+
case CPUFAMILY_ARM_BLIZZARD_AVALANCHE: // A15 / M2
15491594
return "apple-m2";
1550-
case CPUFAMILY_ARM_EVEREST_SAWTOOTH:
1595+
case CPUFAMILY_ARM_EVEREST_SAWTOOTH: // A16
1596+
case CPUFAMILY_ARM_IBIZA: // M3
1597+
case CPUFAMILY_ARM_PALMA: // M3 Max
1598+
case CPUFAMILY_ARM_LOBOS: // M3 Pro
15511599
return "apple-m3";
1600+
case CPUFAMILY_ARM_COLL: // A17 Pro
1601+
return "apple-a17";
1602+
case CPUFAMILY_ARM_DONAN: // M4
1603+
case CPUFAMILY_ARM_BRAVA: // M4 Max
1604+
case CPUFAMILY_ARM_TAHITI: // A18 Pro
1605+
case CPUFAMILY_ARM_TUPAI: // A18
1606+
return "apple-m4";
15521607
default:
15531608
// Default to the newest CPU we know about.
1554-
return "apple-m3";
1609+
return "apple-m4";
15551610
}
15561611
}
15571612
#elif defined(_AIX)

0 commit comments

Comments
 (0)