Skip to content

apple-m3 detected as the native CPU for nightly rustc on apple-m4 #133414

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

Closed
mmizutani opened this issue Nov 24, 2024 · 7 comments · Fixed by llvm/llvm-project#117530
Closed

apple-m3 detected as the native CPU for nightly rustc on apple-m4 #133414

mmizutani opened this issue Nov 24, 2024 · 7 comments · Fixed by llvm/llvm-project#117530
Labels
A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. C-external-bug Category: issue that is caused by bugs in software beyond our control llvm-fixed-upstream Issue expected to be fixed by the next major LLVM upgrade, or backported fixes O-AArch64 Armv8-A or later processors in AArch64 mode O-apple Operating system: Apple (macOS, iOS, tvOS, visionOS, watchOS) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@mmizutani
Copy link

mmizutani commented Nov 24, 2024

I tried the following code with the latest nightly toolchain on a MacBook Pro M4 Max device:

$ rustup run nightly rustc --print target-cpus | head -n2

I expected to see this happen:

The output is expected to be as follows, indicating that the detected CPU is apple-m4.

Available CPUs for this target:
    native          - Select the CPU of the current host (currently apple-m4).

Instead, this happened:

The output indicates that the detected CPU is apple-m3, not apple-m4.

Available CPUs for this target:
    native          - Select the CPU of the current host (currently apple-m3).

The latest nightly rustc does recognize apple-m4 as a valid target-cpu, though:

$ rustup run nightly rustc --print=cfg -C target-cpu=apple-m4 | grep feature
target_feature="aes"
target_feature="bf16"
target_feature="bti"
...
target_feature="wfxt"

Meta

$ rustup run nightly rustc --version --verbose 

rustc 1.85.0-nightly (a47555110 2024-11-22)
binary: rustc
commit-hash: a47555110cf09b3ed59811d9b02235443e76a595
commit-date: 2024-11-22
host: aarch64-apple-darwin
release: 1.85.0-nightly
LLVM version: 19.1.4
$ /usr/sbin/sysctl -n machdep.cpu.brand_string
Apple M4 Max

As was the case in the past similar issue, #127448 (mis-detection of apple-m3 as apple-m2), this might be addressed by an upcoming update to the upstream LLVM.
This fix (llvm/llvm-project#106599) in LLVM 19.x might be relevant.

@mmizutani mmizutani added the C-bug Category: This is a bug. label Nov 24, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Nov 24, 2024
@Noratrieb Noratrieb added A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. O-AArch64 Armv8-A or later processors in AArch64 mode O-apple Operating system: Apple (macOS, iOS, tvOS, visionOS, watchOS) C-external-bug Category: issue that is caused by bugs in software beyond our control and removed C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Nov 24, 2024
@madsmtm
Copy link
Contributor

madsmtm commented Nov 25, 2024

We use LLVM to do our detection, and that will need a similar fix as llvm/llvm-project#82100.

I don't own an M4 myself, so could I get you to run:

$ sysctl hw.cpufamily

Just to make sure what the value actually is?

@madsmtm
Copy link
Contributor

madsmtm commented Nov 25, 2024

I've submitted llvm/llvm-project#117530, but I'd still like to know the value of sysctl hw.cpufamily, just to make sure that I got it right.

@mmizutani
Copy link
Author

mmizutani commented Nov 25, 2024

@madsmtm
Thanks so much.
Here is the output on a real MacBook Pro M4 Max device:

$ /usr/sbin/sysctl hw.cpufamily
hw.cpufamily: 399882554

(CPUFAMILY_ARM_BRAVA (0x17d5b93a))

@madsmtm
Copy link
Contributor

madsmtm commented Nov 25, 2024

Great that I asked you, because I didn't actually implement that at first, as that constant was only available in the SDK shipped with Xcode 16.2 beta 3 (and not in Xcode 16.1)! But have done it now, thanks for the help!

github-actions bot pushed a commit to arm/arm-toolchain that referenced this issue Jan 16, 2025
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).
@madsmtm
Copy link
Contributor

madsmtm commented Jan 16, 2025

The LLVM PR has been merged.

@rustbot label llvm-fixed-upstream

@rustbot rustbot added the llvm-fixed-upstream Issue expected to be fixed by the next major LLVM upgrade, or backported fixes label Jan 16, 2025
@nikic nikic reopened this Jan 20, 2025
@nikic
Copy link
Contributor

nikic commented Feb 20, 2025

This should be fixed on nightly. Can someone on the relevant hardware confirm?

@mmizutani
Copy link
Author

@nikic

It's just been confirmed that that the target CPU detection is now fixed on the latest nightly. Thank you.

$ /usr/sbin/sysctl -n machdep.cpu.brand_string
Apple M4 Max

$ rustup run nightly rustc --version --verbose 
rustc 1.87.0-nightly (46420c960 2025-02-22)
binary: rustc
commit-hash: 46420c96070b4c4bd8242f16d5806b8f26a57016
commit-date: 2025-02-22
host: aarch64-apple-darwin
release: 1.87.0-nightly
LLVM version: 20.1.0

$ rustup run nightly rustc --print target-cpus | head -n2
Available CPUs for this target:
    native          - Select the CPU of the current host (currently apple-m4).

@dianqk dianqk closed this as completed Feb 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. C-external-bug Category: issue that is caused by bugs in software beyond our control llvm-fixed-upstream Issue expected to be fixed by the next major LLVM upgrade, or backported fixes O-AArch64 Armv8-A or later processors in AArch64 mode O-apple Operating system: Apple (macOS, iOS, tvOS, visionOS, watchOS) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants