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.

/// LLVM. Does NOT include the features in `-Ctarget-feature`!
pub(crate) fn base_llvm_features(sess: &EarlySession) -> Vec<String> {
// Features that come earlier are overridden by conflicting features later in the string.
// Typically we'll want more explicit settings to override the implicit ones, so:

@folkertdev folkertdev Nov 22, 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.

Suggested change
// Typically we'll want more explicit settings to override the implicit ones, so:
// Typically we'll want more explicit settings to override the implicit ones, so the ordering we use is:

Maybe?

Also the GCC backend has what is basically a copy of this function, are the comments there still accurate now? I think they are because the changes are only relevant to LLVM.

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.

Also the GCC backend has what is basically a copy of this function, are the comments there still accurate now? I think they are because the changes are only relevant to LLVM.

Yeah... I have added a commit for that I tried to preserve the existing logic even when it strangely does not match what LLVM does, and left FIXMEs.
Cc @GuillaumeGomez @antoyo I'd appreciate if you could take a look.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Changes look good to me but I didn't work on this part of the code so better wait for @antoyo's confirmation.

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.

What happens on the LLVM side is roughly the following

  • compute "backend" target features, does not include -Ctarget-feature
  • construct a temporary LLVM machine instance for that
  • query which Rust target features that machine has
  • add -Ctarget-features
  • store the result for cfg(target_feature)
  • separately translate -Ctarget-features to LLVM feature and add them to the "backend" target features
  • use that list when actually compiling the code

We are doing this dance because we do not want LLVM feature implications to influence how we interpret -Ctarget-features. So we cannot add -Ctarget-features to the temporary LLVM machine and then back-translate that to Rust features.

With GCC, my best guess is that the BackendInfo constructed in init plays the role of that temporary LLVM machine... but that does not look temporary, it is stored in the backend and later used in compile_codegen_unit. So it seems to me that the GCC backend is not doing the "two separate backend machine instances" dance correctly.

@antoyo antoyo Nov 24, 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.

With GCC, my best guess is that the BackendInfo constructed in init plays the role of that temporary LLVM machine... but that does not look temporary, it is stored in the backend and later used in compile_codegen_unit. So it seems to me that the GCC backend is not doing the "two separate backend machine instances" dance correctly.

Only the TargetInfo is saved (to know if the types like f16/f128 are supported), not the context, so I believe this is doing the same as LLVM by creating 2 libgccjit Contexts here.

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.

That TargetInfo is saved though, unlike LLVM where it exists just temporarily. So something is still different.

@RalfJung RalfJung Nov 24, 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.

to know if the types like f16/f128 are supported

With this PR, that happens inside init, so this does not need saving the TargetInfo any more.

However, target_info is also used here, and that has no equivalent in LLVM as far as I can tell. It looks like it is doing codegen, but it is using the TargetInfo that was created for cfg queries. That does not seem right.

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.

However, target_info is also used here, and that has no equivalent in LLVM as far as I can tell. It looks like it is doing codegen, but it is using the TargetInfo that was created for cfg queries

That's what I meant by checking support for the f16/f128 types: it is only used for that checking in the called function.

@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 2 times, most recently from 1ad9660 to fe4e465 Compare November 24, 2026 07:31
@RalfJung
RalfJung force-pushed the llvm-target-feature-precedence branch from fe4e465 to 669da09 Compare November 24, 2026 07:33

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-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.

7 participants