Skip to content

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

Merged
rust-bors[bot] merged 4 commits into
rust-lang:mainfrom
RalfJung:llvm-target-feature-precedence
Nov 26, 2026
Merged

rust-bors[bot] merged 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 2 times, most recently from 687e034 to ba6921e Compare November 23, 2026 11:07
@rustbot

This comment has been minimized.

Comment thread compiler/rustc_codegen_gcc/src/lib.rs Resolved
@RalfJung
RalfJung force-pushed the llvm-target-feature-precedence branch from ba6921e to 47cf3f6 Compare November 23, 2026 12:13
@rust-bors rust-bors Bot removed the S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. label Nov 25, 2026
@rust-bors

This comment has been minimized.

@RalfJung
RalfJung force-pushed the llvm-target-feature-precedence branch from c71ded2 to 72d4c98 Compare November 26, 2026 07:37
@rustbot

rustbot commented Nov 26, 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.

@RalfJung

Copy link
Copy Markdown
Member Author

@bors r=folkertdev

@rust-bors

rust-bors Bot commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 72d4c98 has been tentatively approved by folkertdev

It will be put into the queue for this repository once PR CI succeeds.

@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. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 26, 2026
@rust-log-analyzer

This comment has been minimized.

@RalfJung
RalfJung force-pushed the llvm-target-feature-precedence branch from 72d4c98 to 7649596 Compare November 26, 2026 08:02
@rust-bors rust-bors Bot added 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-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Nov 26, 2026
@rust-bors

rust-bors Bot commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

⚠️ A new commit 76495965dd54e035d4a1e1fccb3b368821cfda56 was pushed.

This pull request was unapproved.

@RalfJung

Copy link
Copy Markdown
Member Author

@bors r=folkertdev

@rust-bors

rust-bors Bot commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 7649596 has been tentatively approved by folkertdev

It will be put into the queue for this repository once PR CI succeeds.

@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. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 26, 2026
rust-bors Bot pushed a commit that referenced this pull request Nov 26, 2026
…uwer

Rollup of 10 pull requests

Successful merges:

 - #164252 (Share retain implementation between Vec and VecDeque)
 - #166008 (fix inconsistent precedence of -Ctarget-feature wrt -Zllvm-target-feature)
 - #166156 (tests: cover proc-macro metadata path remapping)
 - #166171 (borrowck: Fix Reborrow ICE on types with several lifetimes)
 - #166506 (traits: Fix dead match arm for String type suggestions.)
 - #164542 (Add regression test for #151312)
 - #165651 (compiletest: Further clean up directive parsing)
 - #166196 (autodiff: don't ICE on invalid macro arguments)
 - #166556 (remove `Box::into_boxed_slice`)
 - #166562 (docs: add backticks around `!` in primitive docs )
rust-bors Bot pushed a commit that referenced this pull request Nov 26, 2026
…uwer

Rollup of 10 pull requests

Successful merges:

 - #164252 (Share retain implementation between Vec and VecDeque)
 - #166008 (fix inconsistent precedence of -Ctarget-feature wrt -Zllvm-target-feature)
 - #166156 (tests: cover proc-macro metadata path remapping)
 - #166171 (borrowck: Fix Reborrow ICE on types with several lifetimes)
 - #166506 (traits: Fix dead match arm for String type suggestions.)
 - #164542 (Add regression test for #151312)
 - #165651 (compiletest: Further clean up directive parsing)
 - #166196 (autodiff: don't ICE on invalid macro arguments)
 - #166556 (remove `Box::into_boxed_slice`)
 - #166562 (docs: add backticks around `!` in primitive docs )
@rust-bors
rust-bors Bot merged commit 9d84c73 into rust-lang:main Nov 26, 2026
14 checks passed
rust-bors Bot pushed a commit that referenced this pull request Nov 26, 2026
Rollup merge of #166008 - RalfJung:llvm-target-feature-precedence, r=folkertdev

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

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 added this to the 1.102.0 milestone Nov 26, 2026
mehmettoplu-rs pushed a commit to mehmettoplu-rs/miri that referenced this pull request Nov 27, 2026
…uwer

Rollup of 10 pull requests

Successful merges:

 - rust-lang/rust#164252 (Share retain implementation between Vec and VecDeque)
 - rust-lang/rust#166008 (fix inconsistent precedence of -Ctarget-feature wrt -Zllvm-target-feature)
 - rust-lang/rust#166156 (tests: cover proc-macro metadata path remapping)
 - rust-lang/rust#166171 (borrowck: Fix Reborrow ICE on types with several lifetimes)
 - rust-lang/rust#166506 (traits: Fix dead match arm for String type suggestions.)
 - rust-lang/rust#164542 (Add regression test for rust-lang/rust#151312)
 - rust-lang/rust#165651 (compiletest: Further clean up directive parsing)
 - rust-lang/rust#166196 (autodiff: don't ICE on invalid macro arguments)
 - rust-lang/rust#166556 (remove `Box::into_boxed_slice`)
 - rust-lang/rust#166562 (docs: add backticks around `!` in primitive docs )
@RalfJung
RalfJung deleted the llvm-target-feature-precedence branch November 27, 2026 18:54
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-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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