Skip to content

Commit fcca10c

Browse files
committed
[AArch64] Add support for -march=native for Apple M1 CPU
This improves the getHostCPUName check for Apple M1 CPUs, which previously would always be considered cyclone instead. This also enables `-march=native` support when building on M1 CPUs which would previously fail. This isn't as sophisticated as the X86 CPU feature checking which consults the CPU via getHostCPUFeatures, but this is still better than before. This CPU selection could also be invalid if this was run on an iOS device instead, ideally we can improve those cases as they come up. Differential Revision: https://reviews.llvm.org/D119788
1 parent 460fc44 commit fcca10c

File tree

2 files changed

+40
-26
lines changed

2 files changed

+40
-26
lines changed

clang/lib/Driver/ToolChains/Arch/AArch64.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ getAArch64ArchFeaturesFromMarch(const Driver &D, StringRef March,
151151
std::pair<StringRef, StringRef> Split = StringRef(MarchLowerCase).split("+");
152152

153153
llvm::AArch64::ArchKind ArchKind = llvm::AArch64::parseArch(Split.first);
154+
if (Split.first == "native")
155+
ArchKind = llvm::AArch64::getCPUArchKind(llvm::sys::getHostCPUName().str());
154156
if (ArchKind == llvm::AArch64::ArchKind::INVALID ||
155157
!llvm::AArch64::getArchFeatures(ArchKind, Features))
156158
return false;

llvm/lib/Support/Host.cpp

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
#include <mach/mach.h>
4242
#include <mach/mach_host.h>
4343
#include <mach/machine.h>
44+
#include <sys/param.h>
45+
#include <sys/sysctl.h>
4446
#endif
4547
#ifdef _AIX
4648
#include <sys/systemcfg.h>
@@ -1297,32 +1299,45 @@ StringRef sys::getHostCPUName() {
12971299
bool HaveVectorSupport = CVT[244] & 0x80;
12981300
return getCPUNameFromS390Model(Id, HaveVectorSupport);
12991301
}
1300-
#elif defined(__APPLE__) && defined(__aarch64__)
1301-
StringRef sys::getHostCPUName() {
1302-
return "cyclone";
1303-
}
1304-
#elif defined(__APPLE__) && defined(__arm__)
1305-
StringRef sys::getHostCPUName() {
1306-
host_basic_info_data_t hostInfo;
1307-
mach_msg_type_number_t infoCount;
1302+
#elif defined(__APPLE__) && (defined(__arm__) || defined(__aarch64__))
1303+
#define CPUFAMILY_ARM_SWIFT 0x1e2d6381
1304+
#define CPUFAMILY_ARM_CYCLONE 0x37a09642
1305+
#define CPUFAMILY_ARM_TYPHOON 0x2c91a47e
1306+
#define CPUFAMILY_ARM_TWISTER 0x92fb37c8
1307+
#define CPUFAMILY_ARM_HURRICANE 0x67ceee93
1308+
#define CPUFAMILY_ARM_MONSOON_MISTRAL 0xe81e7ef6
1309+
#define CPUFAMILY_ARM_VORTEX_TEMPEST 0x07d34b9f
1310+
#define CPUFAMILY_ARM_LIGHTNING_THUNDER 0x462504d2
1311+
#define CPUFAMILY_ARM_FIRESTORM_ICESTORM 0x1b588bb3
13081312

1309-
infoCount = HOST_BASIC_INFO_COUNT;
1310-
mach_port_t hostPort = mach_host_self();
1311-
host_info(hostPort, HOST_BASIC_INFO, (host_info_t)&hostInfo,
1312-
&infoCount);
1313-
mach_port_deallocate(mach_task_self(), hostPort);
1313+
StringRef sys::getHostCPUName() {
1314+
uint32_t Family;
1315+
size_t Length = sizeof(Family);
1316+
sysctlbyname("hw.cpufamily", &Family, &Length, NULL, 0);
13141317

1315-
if (hostInfo.cpu_type != CPU_TYPE_ARM) {
1316-
assert(false && "CPUType not equal to ARM should not be possible on ARM");
1317-
return "generic";
1318+
switch (Family) {
1319+
case CPUFAMILY_ARM_SWIFT:
1320+
return "swift";
1321+
case CPUFAMILY_ARM_CYCLONE:
1322+
return "apple-a7";
1323+
case CPUFAMILY_ARM_TYPHOON:
1324+
return "apple-a8";
1325+
case CPUFAMILY_ARM_TWISTER:
1326+
return "apple-a9";
1327+
case CPUFAMILY_ARM_HURRICANE:
1328+
return "apple-a10";
1329+
case CPUFAMILY_ARM_MONSOON_MISTRAL:
1330+
return "apple-a11";
1331+
case CPUFAMILY_ARM_VORTEX_TEMPEST:
1332+
return "apple-a12";
1333+
case CPUFAMILY_ARM_LIGHTNING_THUNDER:
1334+
return "apple-a13";
1335+
case CPUFAMILY_ARM_FIRESTORM_ICESTORM:
1336+
return "apple-m1";
1337+
default:
1338+
// Default to the newest CPU we know about.
1339+
return "apple-m1";
13181340
}
1319-
switch (hostInfo.cpu_subtype) {
1320-
case CPU_SUBTYPE_ARM_V7S:
1321-
return "swift";
1322-
default:;
1323-
}
1324-
1325-
return "generic";
13261341
}
13271342
#elif defined(_AIX)
13281343
StringRef sys::getHostCPUName() {
@@ -1453,9 +1468,6 @@ int computeHostNumPhysicalCores() {
14531468
#elif defined(__linux__) && defined(__s390x__)
14541469
int computeHostNumPhysicalCores() { return sysconf(_SC_NPROCESSORS_ONLN); }
14551470
#elif defined(__APPLE__)
1456-
#include <sys/param.h>
1457-
#include <sys/sysctl.h>
1458-
14591471
// Gets the number of *physical cores* on the machine.
14601472
int computeHostNumPhysicalCores() {
14611473
uint32_t count;

0 commit comments

Comments
 (0)