Skip to content

rustc_codegen_llvm: handle LLVM 24 ABI constraints - #162783

Open
durin42 wants to merge 1 commit into
rust-lang:mainfrom
durin42:llvm-24-riscv-hardfloat
Open

durin42 wants to merge 1 commit into
rust-lang:mainfrom
durin42:llvm-24-riscv-hardfloat

Conversation

@durin42

@durin42 durin42 commented Sep 14, 2026

Copy link
Copy Markdown
Contributor

LLVM 24 is pickier about things like hard-float being disabled but the compiled module ABI mentioning hard-float. As an example, if the user specifies target-features=-d to disable the d extension but the declared target-abi is lp64d that's now an error where before I guess it was a warning.

This fix seems somewhat inelegant, but in the name of keeping the behavior changes minimal I did gate the new behavior to only happen on LLVM 24. I'm very open to alternative solutions!

An LLM was used to identify the breaking commit and help me sort out why the commit was breaking.

@rustbot label: +llvm-main

LLVM 24 is pickier about things like hard-float being disabled but
the compiled module ABI mentioning hard-float. As an example, if the
user specifies target-features=-d to disable the d extension but the
declared target-abi is lp64d that's now an error where before I guess it
was a warning.

This fix seems somewhat inelegant, but in the name of keeping the
behavior changes minimal I did gate the new behavior to only happen on
LLVM 24. I'm very open to alternative solutions!

An LLM was used to identify the breaking commit and help me sort out
_why_ the commit was breaking.
@rustbot rustbot added A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 14, 2026
@rustbot

rustbot commented Sep 14, 2026

Copy link
Copy Markdown
Collaborator

r? @mejrs

rustbot has assigned @mejrs.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 76 candidates
  • Random selection from 18 candidates

@rustbot rustbot added the llvm-main Marks PRs that are making Rust work with LLVM main (this label is consumed by CI tooling) label Sep 14, 2026
@arichardson

Copy link
Copy Markdown
Contributor

Apologies for this breakage. My change was intended to buy NFC but apparently there is no test coverage for this. While longer term this should probably be an error it's definitely not supposed to cause churn for downstreams.

@arichardson

Copy link
Copy Markdown
Contributor

It does seem like this change is an improvement regardless of whether my commit is reverted or not so it definitely LGTM from a functional point. Not a rust expert so can't comment on style or anything else.

@beetrees

beetrees commented Sep 15, 2026

Copy link
Copy Markdown
Contributor

rustc has generally been moving towards emitting hard errors when target features required by the ABI aren't enabled (e.g. #161280), so changing the target ABI we pass to LLVM seems like the wrong solution. Instead we should add RISC-V to the hard error ABI feature check.

cc @RalfJung

@RalfJung

RalfJung commented Sep 15, 2026

Copy link
Copy Markdown
Member

Agreed, the proposed behavior in this PR is IMO a very bad idea. It's basically re-introducing the broken LLVM behavior that LLVM finally fixed, of silently changing the ABI. We should never silently change the ABI, that's plain unsound.

Instead we should add RISC-V to the hard error ABI feature check.

Agreed. This matches #161280.

@RalfJung

RalfJung commented Sep 15, 2026

Copy link
Copy Markdown
Member

Apologies for this breakage. My change was intended to buy NFC but apparently there is no test coverage for this. While longer term this should probably be an error it's definitely not supposed to cause churn for downstreams.

No need to apologize, I am very happy that LLVM is finally moving towards actually complaining about nonsensical ABI requests from the frontend, rather than silently doing something arbitrary. :)

llvm/llvm-project#111334 deliberately introduced such errors for the ARM backend. I'd love for RISCV and all the other backends to do the same.

arichardson added a commit to arichardson/upstream-llvm-project that referenced this pull request Sep 15, 2026
…d by streamer

Commit 105ff16 (llvm#213410)
changed RISCVABI::computeTargetABI() to return Expected<ABI> and added
validation to RISCVAsmParser::onBeginOfFile() to report invalid
-target-abi flags with real source location in llvm-mc.

However, this broke LTO builds containing inline assembly (e.g. Android
riscv64 builds and downstream Rust in
rust-lang/rust#162783). During LTO, LLD sets
TargetOptions.MCOptions.ABIName from the module's target-abi metadata
("lp64d"), while the linker's default TargetMachine subtarget lacks
"+d" (individual functions specify "+d" in target-features).
RISCVSubtarget handles this gracefully by emitting a diagnostic note
and falling back to lp64 for code generation. When inline assembly was
subsequently parsed, AsmPrinter::emitInlineAsm instantiated
RISCVAsmParser with a subtarget lacking "+d", causing onBeginOfFile() to
re-validate TargetOptions.ABIName and fail with a fatal error.

To fix this, skip the parser validation whenever the streamer has
already resolved an ABI. This preserves llvm-mc diagnostics on invalid
command-line flags while avoiding conflicting validation when assembling
inline asm during code generation.

This commit was created with the help of AI tools
@durin42

durin42 commented Sep 15, 2026

Copy link
Copy Markdown
Contributor Author

Sounds good. Any thoughts on what the right fix for tests/ui/target-feature/abi-required-target-feature-flag-disable.rs is? Without this patch it fails on LLVM 24: https://buildkite.com/llvm-project/rust-llvm-integrate-prototype/builds/48760#01a0a14a-7bcc-4d31-8c8a-e89dcaf72ee8

@beetrees

Copy link
Copy Markdown
Contributor

Changing the riscv revision to be a check-fail like a similar test in when the ARM check was made a hard error should allow the test to pass. I believe you'll also need to add some minicore-compile-flags re-enabling the d target feature (similar to what ARM test I linked to does with the target-cpu) otherwise minicore itself will fail to build, as opposed to just the test.

@RalfJung

Copy link
Copy Markdown
Member

Given llvm/llvm-project#223606 it seems like the LLVM error here is considered a bug by upstream LLVM? If they plan to revert that change there's probably no point in us making this a hard error.

That said, I'd prefer if LLVM actually made this an error consistently, not just for this specific inline-asm-LTO corner case. ;)

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. llvm-main Marks PRs that are making Rust work with LLVM main (this label is consumed by CI tooling) S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants