Normalise LLVM target triple for Apple platforms (aarch64 -> arm64)#156
Conversation
Apple's LLVM toolchain uses `arm64` as the canonical architecture name for AArch64 on Apple platforms, while GNU config.sub normalises to `aarch64`. This mismatch causes `--target=aarch64-apple-darwin` to be passed to clang, which conflicts with toolchain wrappers (e.g. nix cc-wrapper) that expect `arm64-apple-darwin`. The result is thousands of test failures on aarch64-darwin because the cc-wrapper warning pollutes compiler output and the target flag interaction breaks compilation. Fix by adding normaliseLlvmTarget that rewrites `aarch64-apple-*` to `arm64-apple-*` for the LLVM target triple, matching Apple conventions and the existing llvm-targets file which already uses arm64-apple-darwin.
There was a problem hiding this comment.
Pull request overview
Adds LLVM-target triple normalization in ghc-toolchain to avoid Apple toolchain / wrapper mismatches (notably nix cc-wrapper) by rewriting aarch64-apple-* to arm64-apple-*.
Changes:
- Export
normaliseLlvmTargetfromGHC.Toolchain.NormaliseTripleand implementaarch64-apple-*→arm64-apple-*rewriting. - Apply
normaliseLlvmTargetwhen computingtgtLlvmTargetinghc-toolchain’smkTarget.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| utils/ghc-toolchain/src/GHC/Toolchain/NormaliseTriple.hs | Adds and exports normaliseLlvmTarget to rewrite Apple AArch64 LLVM triples. |
| utils/ghc-toolchain/exe/Main.hs | Uses normaliseLlvmTarget when generating the persisted “LLVM target” setting. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| -- See also: the @llvm-targets@ file uses @arm64-apple-darwin@. | ||
| normaliseLlvmTarget :: String -> String | ||
| normaliseLlvmTarget triple | ||
| | "aarch64-apple-" `isPrefixOf` triple |
There was a problem hiding this comment.
normaliseLlvmTarget currently rewrites any aarch64-apple-* triple to arm64-apple-*. This will change aarch64-apple-ios into arm64-apple-ios, but the shipped llvm-targets file contains an aarch64-apple-ios entry and no arm64-apple-ios entry, so GHC's LLVM backend will fail to look up a data layout for iOS targets (panic in the lookup target (llvmTargets ...) path). Consider narrowing this rewrite to the Darwin host triples that actually trigger the cc-wrapper warning (e.g. aarch64-apple-darwin*), or alternatively add matching arm64-apple-* aliases to llvm-targets as part of this change.
| | "aarch64-apple-" `isPrefixOf` triple | |
| | "aarch64-apple-darwin" `isPrefixOf` triple |
| -- Use Llvm target if specified, otherwise use triple as llvm target. | ||
| -- Normalise for platform conventions (e.g. aarch64-apple-* -> arm64-apple-*). | ||
| let tgtLlvmTarget = normaliseLlvmTarget $ fromMaybe normalised_triple (optLlvmTriple opts) |
There was a problem hiding this comment.
tgtLlvmTarget is persisted into the generated settings file as "LLVM target" and is later used by the LLVM backend to look up a data layout in llvm-targets. Applying normaliseLlvmTarget unconditionally here can therefore make the settings contain a triple that isn't present in llvm-targets (e.g. arm64-apple-ios). Please ensure the normalisation only produces triples that exist in llvm-targets (either by narrowing the rewrite to darwin, or by adding the corresponding aliases to llvm-targets).
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| | "aarch64-apple-" `isPrefixOf` triple | ||
| = "arm64-" ++ drop (length "aarch64-") triple | ||
| | otherwise | ||
| = triple |
There was a problem hiding this comment.
The guard checks the prefix "aarch64-apple-" but the rewrite drops only "aarch64-". This works today, but it’s easy to accidentally break if the guard/prefix changes later. Prefer rewriting based on a single shared prefix (e.g., strip the exact matched prefix and prepend the corresponding replacement prefix) to keep the condition and transformation coupled.
| | "aarch64-apple-" `isPrefixOf` triple | |
| = "arm64-" ++ drop (length "aarch64-") triple | |
| | otherwise | |
| = triple | |
| | srcPrefix `isPrefixOf` triple | |
| = dstPrefix ++ drop (length srcPrefix) triple | |
| | otherwise | |
| = triple | |
| where | |
| srcPrefix = "aarch64-apple-" | |
| dstPrefix = "arm64-apple-" |
| -- Use Llvm target if specified, otherwise use triple as llvm target. | ||
| -- Normalise for platform conventions (e.g. aarch64-apple-* -> arm64-apple-*). | ||
| let tgtLlvmTarget = normaliseLlvmTarget $ fromMaybe normalised_triple (optLlvmTriple opts) |
There was a problem hiding this comment.
“Llvm”/“llvm” in the comment is inconsistent with the standard acronym “LLVM”. Consider updating the comment to use “LLVM” consistently (and, if desired, “LLVM target triple” for clarity).
Summary
normaliseLlvmTargettoghc-toolchainthat rewritesaarch64-apple-*toarm64-apple-*in LLVM target triplesconfig.subnormalises toaarch64-apple-darwin, but Apple's LLVM toolchain and nix's cc-wrapper expectarm64-apple-darwin--target=aarch64-apple-darwinto clang, triggering a cc-wrapper warning that pollutes stderr and causes ~5400/10775 testsuite failures on aarch64-darwinSplit out from #149 (the HADRIAN_SETTINGS removal was already merged separately).
Test plan
--targetmismatch warnings