This is an archive of the discontinued LLVM Phabricator instance.

[AArch64] Add support for -march=native for Apple M1 CPU
ClosedPublic

Authored by keith on Feb 14 2022, 3:14 PM.

Details

Summary

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.

Diff Detail

Event Timeline

keith created this revision.Feb 14 2022, 3:14 PM
keith requested review of this revision.Feb 14 2022, 3:14 PM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptFeb 14 2022, 3:14 PM

Things have moved on since the ARM and (especially) PPC variants of that function were written. That field (despite the name) is now more of an ABI tag and not going to be updated with each CPU.

I think the modern replacement for it is hw.cpufamily obtained from sysctl (command line or man 3 sysctl for programmatically). The potential values (CPUFAMILY_ARM_* starting with Cyclone) are listed in https://opensource.apple.com/source/xnu/xnu-4570.41.2/osfmk/mach/machine.h.auto.html (which should be in the SDK under mach/machine.h). It warns against inferring features from this, which is exactly what we'd be doing, but still probably has a better chance of being right long term.

I'd probably default to the latest known one rather than earliest too, since an unknown family is more likely to be new than old.

Sorry, that was an old xnu version, the newest one is https://opensource.apple.com/source/xnu/xnu-7195.81.3/osfmk/mach/machine.h.auto.html which has

CPUFAMILY_ARM_CYCLONE0x37a09642apple-a7
CPUFAMILY_ARM_TYPHOON0x2c91a47eapple-a8
CPUFAMILY_ARM_TWISTER0x92fb37c8apple-a9
CPUFAMILY_ARM_HURRICANE0x67ceee93apple-a10
CPUFAMILY_ARM_MONSOON_MISTRAL0xe81e7ef6apple-a11
CPUFAMILY_ARM_VORTEX_TEMPEST0x07d34b9fapple-a12
CPUFAMILY_ARM_LIGHTNING_THUNDER0x462504d2apple-a13
CPUFAMILY_ARM_FIRESTORM_ICESTORM0x1b588bb3apple-m1 (and apple-a14)

We'll probably have to hard-code most of those because we need LLVM to be buildable on much older versions of Xcode that don't know about newer CPUs (likely Xcode 9.3 soon, but now older even than that).

keith updated this revision to Diff 409849.Feb 17 2022, 7:57 PM

Update to read CPU from sysctl

keith added a comment.Feb 17 2022, 7:58 PM

Thanks for the tips! I left the default as generic here, but I totally take your point on defaulting to the newest and I'm happy to update if you feel strongly, lmk what you think

jrtc27 added a subscriber: jrtc27.Feb 17 2022, 8:37 PM
jrtc27 added inline comments.
llvm/lib/Support/Host.cpp
1321

ARM only calls it cyclone, AArch64 allows both but apple-a7 is the canonical name (the comment above the cyclone alias states it's for old bitcode compatibility)

1327

Not sure this should be an assert? I think other implementations will fall back on generic on errors (e.g. the things that read /proc/cpuinfo will use "" on error for the file contents and that gets parsed as generic by the various default cases).

Also technically you need to check Length is still sizeof(Family) and not smaller (the other direction is an error), but nobody ever bothers so meh.

1335

Maybe a question for AArch64 maintainers about whether apple-a14 or apple-m1 should be used... they're the same thing from the backend's perspective just with different names. Shame the naming doesn't separate out the "generation" from the specific configuration like you see for other backends (apple-aN is almost that as it omits all the bionic etc marketing name fluff, except apple-m1 comes along and ruins that).

ab added a comment.Feb 18 2022, 3:04 PM

Thanks for the tips! I left the default as generic here, but I totally take your point on defaulting to the newest and I'm happy to update if you feel strongly, lmk what you think

I agree with Tim, this should default to the newest known one.

generic doesn't really make sense for Darwin anyway, the "oldest" sane default would probably be cyclone, apple-a12, or apple-m1, depending on the slice and OS

llvm/lib/Support/Host.cpp
1303

Looks like we have the same includes for computeHostNumPhysicalCores. Maybe move this to the top of the file, gated with just __APPLE__?

1335

Seems fine to stick to the apple-aNN names here. (we already have a similar situation with apple-sN)

keith updated this revision to Diff 410040.Feb 18 2022, 4:05 PM
keith marked 2 inline comments as done.

Code review improvements

llvm/lib/Support/Host.cpp
1321

I saw this comment:

// Support cyclone as an alias for apple-a7 so we can still LTO old bitcode.
def : ProcessorModel<"cyclone", CycloneModel, ProcessorFeatures.AppleA7,
                     [TuneAppleA7]>;

And thought that implied that the a7 name would be preferred to the "old" name for this case of case, wdyt?

1335

I felt like the m1 name would be preferred here even though they're aliases just because I assume since this is for host CPUs you're more likely to hit this on an m1 machine than something that has an a14, which might lead to some user confusion. For example if you run clang -march=native -### with this change, you see apple-m1 in the invocation, I would personally be surprised if I saw a14 there on my mac, even if they're the same.

keith updated this revision to Diff 414866.Mar 12 2022, 11:48 AM

Update default to latest known CPU

Herald added a project: Restricted Project. · View Herald TranscriptMar 12 2022, 11:48 AM
keith updated this revision to Diff 414878.Mar 12 2022, 1:20 PM

Fix typo

keith added a comment.Mar 14 2022, 8:09 PM

Bump, I think I've covered everything here, let me know if not!

ab accepted this revision.Mar 23 2022, 1:11 PM

Reading this again, I have a feeling defaulting to newest will come back to bite us (or, well, apple folks.) But we can reevaluate if we get to that. So, LGTM, thanks!

This revision is now accepted and ready to land.Mar 23 2022, 1:11 PM
This revision was automatically updated to reflect the committed changes.