Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Apple M4 host detection #117530

Merged
merged 1 commit into from
Jan 16, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 59 additions & 4 deletions llvm/lib/TargetParser/Host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1485,6 +1485,18 @@ StringRef sys::getHostCPUName() {
return getCPUNameFromS390Model(Id, HaveVectorSupport);
}
#elif defined(__APPLE__) && (defined(__arm__) || defined(__aarch64__))
// Copied from <mach/machine.h> in the macOS SDK.
//
// Also available here, though usually not as up-to-date:
// https://github.com/apple-oss-distributions/xnu/blob/xnu-11215.41.3/osfmk/mach/machine.h#L403-L452.
#define CPUFAMILY_UNKNOWN 0
#define CPUFAMILY_ARM_9 0xe73283ae
#define CPUFAMILY_ARM_11 0x8ff620d8
#define CPUFAMILY_ARM_XSCALE 0x53b005f5
#define CPUFAMILY_ARM_12 0xbd1b0ae9
#define CPUFAMILY_ARM_13 0x0cc90e64
#define CPUFAMILY_ARM_14 0x96077ef1
#define CPUFAMILY_ARM_15 0xa8511bca
#define CPUFAMILY_ARM_SWIFT 0x1e2d6381
#define CPUFAMILY_ARM_CYCLONE 0x37a09642
#define CPUFAMILY_ARM_TYPHOON 0x2c91a47e
Expand All @@ -1496,13 +1508,46 @@ StringRef sys::getHostCPUName() {
#define CPUFAMILY_ARM_FIRESTORM_ICESTORM 0x1b588bb3
#define CPUFAMILY_ARM_BLIZZARD_AVALANCHE 0xda33d83d
#define CPUFAMILY_ARM_EVEREST_SAWTOOTH 0x8765edea
#define CPUFAMILY_ARM_IBIZA 0xfa33415e
#define CPUFAMILY_ARM_PALMA 0x72015832
#define CPUFAMILY_ARM_COLL 0x2876f5b5
#define CPUFAMILY_ARM_LOBOS 0x5f4dea93
#define CPUFAMILY_ARM_DONAN 0x6f5129ac
#define CPUFAMILY_ARM_BRAVA 0x17d5b93a
#define CPUFAMILY_ARM_TAHITI 0x75d4acb9
#define CPUFAMILY_ARM_TUPAI 0x204526d0

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

// This is found by testing on actual hardware, and by looking at:
// https://github.com/apple-oss-distributions/xnu/blob/xnu-11215.41.3/osfmk/arm/cpuid.c#L109-L231.
//
// Another great resource is
// https://github.com/AsahiLinux/docs/wiki/Codenames.
//
// NOTE: We choose to return `apple-mX` instead of `apple-aX`, since the M1,
// M2, M3 etc. aliases are more widely known to users than A14, A15, A16 etc.
// (and this code is basically only used on host macOS anyways).
switch (Family) {
case CPUFAMILY_UNKNOWN:
return "generic";
case CPUFAMILY_ARM_9:
return "arm920t"; // or arm926ej-s
case CPUFAMILY_ARM_11:
return "arm1136jf-s";
case CPUFAMILY_ARM_XSCALE:
return "xscale";
case CPUFAMILY_ARM_12: // Seems unused by the kernel
return "generic";
case CPUFAMILY_ARM_13:
return "cortex-a8";
case CPUFAMILY_ARM_14:
return "cortex-a9";
case CPUFAMILY_ARM_15:
return "cortex-a7";
case CPUFAMILY_ARM_SWIFT:
return "swift";
case CPUFAMILY_ARM_CYCLONE:
Expand All @@ -1519,15 +1564,25 @@ StringRef sys::getHostCPUName() {
return "apple-a12";
case CPUFAMILY_ARM_LIGHTNING_THUNDER:
return "apple-a13";
case CPUFAMILY_ARM_FIRESTORM_ICESTORM:
case CPUFAMILY_ARM_FIRESTORM_ICESTORM: // A14 / M1
return "apple-m1";
case CPUFAMILY_ARM_BLIZZARD_AVALANCHE:
case CPUFAMILY_ARM_BLIZZARD_AVALANCHE: // A15 / M2
return "apple-m2";
case CPUFAMILY_ARM_EVEREST_SAWTOOTH:
case CPUFAMILY_ARM_EVEREST_SAWTOOTH: // A16
case CPUFAMILY_ARM_IBIZA: // M3
case CPUFAMILY_ARM_PALMA: // M3 Max
case CPUFAMILY_ARM_LOBOS: // M3 Pro
return "apple-m3";
case CPUFAMILY_ARM_COLL: // A17 Pro
return "apple-a17";
case CPUFAMILY_ARM_DONAN: // M4
case CPUFAMILY_ARM_BRAVA: // M4 Max
case CPUFAMILY_ARM_TAHITI: // A18 Pro
case CPUFAMILY_ARM_TUPAI: // A18
return "apple-m4";
default:
// Default to the newest CPU we know about.
return "apple-m3";
return "apple-m4";
}
}
#elif defined(_AIX)
Expand Down
Loading