Skip to content

fix inconsistent precedence of -Ctarget-feature wrt -Zllvm-target-feature - #166008

Open
RalfJung wants to merge 4 commits into
rust-lang:mainfrom
RalfJung:llvm-target-feature-precedence
Open

RalfJung wants to merge 4 commits into
rust-lang:mainfrom
RalfJung:llvm-target-feature-precedence

Conversation

@RalfJung

@RalfJung RalfJung commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

View all comments

Currently, -Zllvm-target-feature takes precedence for populating internal_target_features, but -Ctarget-feature takes precedence for what we actually tell LLVM. That's pretty bad, so this PR makes them consistent.

I first wanted -Zllvm-target-feature to take precedence everywhere, as that was the intent in #160457. However, I don't see a good way to implement that: internal_target_features are computed by instantiating an actual LLVM data structure without -Ctarget-feature, querying which Rust target features that supports, and then applying -Ctarget-feature and its implications "manually". This is done to prevent backend implications from affecting cfg(target_feature). But this means we can't really apply -Zllvm-target-feature after having done that as that would take us back down to the LLVM level. So instead, the precedence order is that all LLVM-level stuff comes first, and then -Ctarget-feature comes all the way at the end.

Cc @folkertdev (reviewer of #160457), @nnethercote (recently did some refactoring of the backend init logic that is being further adjusted here)
Tracking issue: #157753

@rustbot

rustbot commented Nov 15, 2026

Copy link
Copy Markdown
Collaborator

rustc_codegen_cranelift is developed in its own repository. If possible, consider making this change to rust-lang/rustc_codegen_cranelift instead.

cc @bjorn3

miri is developed in its own repository. If the Miri part of this change can be broken out, consider making this change to rust-lang/miri instead. However, if Miri needs adjusting for rustc changes, just ignore this message.

cc @rust-lang/miri

rustc_codegen_gcc is developed in its own repository. If possible, consider making this change to rust-lang/rustc_codegen_gcc instead.

cc @antoyo, @GuillaumeGomez

@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 Nov 15, 2026
@rustbot

rustbot commented Nov 15, 2026

Copy link
Copy Markdown
Collaborator

r? @chenyukang

rustbot has assigned @chenyukang.
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 79 candidates
  • Random selection from 20 candidates


fn init(&mut self, _sess: &EarlySession) -> CodegenBackendInit {
Default::default()
fn init(&mut self, _sess: &EarlySession) -> (CodegenBackendInit, TargetConfig) {

@RalfJung RalfJung Nov 15, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 2nd commit refactors backend initialization by merging init and target_config. This is because the LLVM backend can avoid recomputing some data if it can take intermediate results from init and use them to construct target_config. This makes it so that base_llvm_features is actually finally called only once ever, which makes me very happy.

Maybe that means the two types should be merged as well, but I'd prefer to leave that to a future PR as it seems mostly orthogonal, logic-wise.

View changes since the review

@RalfJung
RalfJung force-pushed the llvm-target-feature-precedence branch 2 times, most recently from 4262fd4 to bc2cf69 Compare November 15, 2026 19:43
@RalfJung

Copy link
Copy Markdown
Member Author

r? @folkertdev

@rustbot rustbot assigned folkertdev and unassigned chenyukang Nov 15, 2026
@rustbot

rustbot commented Nov 15, 2026

Copy link
Copy Markdown
Collaborator

folkertdev is currently at their maximum review capacity.
They may take a while to respond.

@rust-log-analyzer

This comment has been minimized.

@RalfJung
RalfJung force-pushed the llvm-target-feature-precedence branch from bc2cf69 to 7721e8d Compare November 15, 2026 19:53
@rust-log-analyzer

This comment has been minimized.

@RalfJung
RalfJung force-pushed the llvm-target-feature-precedence branch from 7721e8d to 01422ce Compare November 15, 2026 21:15
@rust-bors

This comment has been minimized.

@RalfJung
RalfJung force-pushed the llvm-target-feature-precedence branch from 01422ce to e00304e Compare November 17, 2026 10:20
@rustbot

This comment has been minimized.

None,
&sess.target,
);
let _ = native_codegen_backend.init(sess);

@folkertdev folkertdev Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is removing the call to init here correct? That function takes &mut self so it might update the session.

View changes since the review

@RalfJung RalfJung Nov 22, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The call has been moved into MiriCodegenBackend::init, because it felt more natural to have it there.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not clear locally how MiriCodegenBackend::init gets called (I guess in compiler/rustc_interface/src/interface.rs?), but I'll happily believe you wrt Miri code.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MiriCodegenBackend is basically wrapping another codegen backend, so forwarding these calls makes most sense to me. I'm not sure why we previously went for eager initialization...

// Typically we'll want more explicit settings to override the implicit ones, so:
//
// * Features from -Ctarget-cpu=*; are overridden by [^1]
// * Features implied by --target; are overridden by

@folkertdev folkertdev Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this mean "overridden by empty string"? it reads really weird?

View changes since the review

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it means "overriden by the things listed later in this enumeration"

@RalfJung
RalfJung force-pushed the llvm-target-feature-precedence branch from e00304e to c9e3b90 Compare November 22, 2026 19:15
None,
&sess.target,
);
let _ = native_codegen_backend.init(sess);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not clear locally how MiriCodegenBackend::init gets called (I guess in compiler/rustc_interface/src/interface.rs?), but I'll happily believe you wrt Miri code.

@rust-bors

This comment has been minimized.

@RalfJung
RalfJung force-pushed the llvm-target-feature-precedence branch from c9e3b90 to 687e034 Compare November 23, 2026 11:04
@RalfJung
RalfJung force-pushed the llvm-target-feature-precedence branch from 687e034 to ba6921e Compare November 23, 2026 11:07
@rustbot

rustbot commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

Comment thread compiler/rustc_codegen_gcc/src/lib.rs Resolved
@RalfJung
RalfJung force-pushed the llvm-target-feature-precedence branch 4 times, most recently from b5fbf06 to abea8b4 Compare November 23, 2026 12:29
Comment thread compiler/rustc_codegen_gcc/src/lib.rs Resolved
@RalfJung
RalfJung force-pushed the llvm-target-feature-precedence branch from abea8b4 to ddbf0a3 Compare November 23, 2026 12:32
@RalfJung
RalfJung force-pushed the llvm-target-feature-precedence branch 2 times, most recently from 0c84573 to d87e107 Compare November 23, 2026 12:41
@RalfJung
RalfJung force-pushed the llvm-target-feature-precedence branch 4 times, most recently from 669da09 to 895f6ba Compare November 24, 2026 19:40
Comment thread compiler/rustc_codegen_gcc/src/lib.rs Resolved
@RalfJung
RalfJung force-pushed the llvm-target-feature-precedence branch from 895f6ba to c71ded2 Compare November 25, 2026 16:22
@RalfJung

Copy link
Copy Markdown
Member Author

@folkertdev looks like the GCC side is good. Anything else from your side? :)

@folkertdev

Copy link
Copy Markdown
Contributor

@bors r+

@rust-bors

rust-bors Bot commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

📌 Commit c71ded2 has been approved by folkertdev

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Nov 25, 2026
@rust-bors

rust-bors Bot commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

☔ The latest upstream changes (presumably #166567) made this pull request unmergeable. Please resolve the merge conflicts by rebasing.

This pull request was unapproved.

This branch has not been deployed

No deployments
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. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. 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.

7 participants