Skip to content

Add -Zllvm-target-feature to pass features directly to LLVM#159545

Draft
fo40225 wants to merge 1 commit into
rust-lang:mainfrom
fo40225:Zllvm-target-feature
Draft

Add -Zllvm-target-feature to pass features directly to LLVM#159545
fo40225 wants to merge 1 commit into
rust-lang:mainfrom
fo40225:Zllvm-target-feature

Conversation

@fo40225

@fo40225 fo40225 commented Jul 19, 2026

Copy link
Copy Markdown

Summary

Implement rust-lang/compiler-team#994: add a new unstable flag -Zllvm-target-feature that forwards comma-separated +feat/-feat strings verbatim to the LLVM backend, bypassing rustc's known-feature table.

Tracking issue: #157753

Motivation

-Ctarget-feature currently serves two purposes: toggling Rust-recognized target features, and (with a warning) forwarding arbitrary unknown strings to LLVM.

The warning-based passthrough is unsound to rely on and unacceptable for users who need raw LLVM features (e.g. matching the ABI of foreign libraries built with features Rust does not model, such as MIPS nan2008), forcing them onto unstable JSON target specs instead.

MCP 994 splits the two concerns: a dedicated, explicitly-unsafe-ish escape hatch for LLVM-level features, and a deprecation path for the old passthrough.

What this PR does

  • New flag -Zllvm-target-feature=<+feat,-feat,...> (unstable): each segment is appended verbatim to the LLVM feature list in llvm_features_by_flags, after all other flag-derived features.
    It does not affect cfg(target_feature) or runtime feature detection, and non-LLVM backends ignore it (same stance as -Cllvm-args).
    No validation is performed; invalid names surface as LLVM's own "not a recognized feature" diagnostics.
  • Target modifier: the option is registered with [TRACKED] { TARGET_MODIFIER: LlvmTargetFeature }, so linking crates compiled with different values is an error by default (E-target-modifier machinery), overridable with -Cunsafe-allow-abi-mismatch=llvm-target-feature.
  • Deprecation step: the existing warning for unknown features passed via -Ctarget-feature now carries two extra notes that this will become a hard error in a future release, and to use -Zllvm-target-feature instead.
  • Docs: unstable-book page compiler-flags/llvm-target-feature.md.

Tests

  • tests/ui/target_modifiers/incompatible_llvm_target_feature.rs (+ auxiliary crate): mismatch error, -Cunsafe-allow-abi-mismatch override, and missing-value revisions, mirroring incompatible_regparm.rs.
  • tests/codegen-llvm/llvm-target-feature.rs: verifies a raw LLVM-only feature (+prefer-256-bit) reaches the "target-features" function attribute.
  • Blessed tests/ui/target-feature/similar-feature-suggestion.stderr for the new deprecation notes.

Implement MCP 994 (tracking issue 157753): a new unstable flag
`-Zllvm-target-feature` that forwards comma-separated `+feat`/`-feat`
strings verbatim to the LLVM backend, bypassing Rust's known-feature
table. The flag is registered as a target modifier, so mixing different
values across crates is an error unless overridden with
`-Cunsafe-allow-abi-mismatch=llvm-target-feature`. Non-LLVM backends
ignore it, like `-Cllvm-args`.

As part of the deprecation path, the warning for unknown features passed
via `-Ctarget-feature` now notes that this will become a hard error in
a future release and points to `-Zllvm-target-feature` instead.
@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 Jul 19, 2026
@rustbot

rustbot commented Jul 19, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the pull request, and welcome! The Rust Project is excited to review your changes, and you should hear from @nnethercote (or someone else) some time within the next two weeks.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue
Why was this reviewer chosen?

The reviewer was selected based on:

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

@workingjubilee workingjubilee left a comment

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.

The warning-based passthrough is unsound to rely on

I do not understand what you mean by "unsound" here, @fo40225.

It is undesirable to rely on.

Passing target features at all, however, is simply prone to being unsound, because LLVM can have arbitrary semantics for "target features" that do not accord with the model adopted for Rust target features. It is not relying on the passthrough that is unsound.

Anyways, please rewrite your entire PR description for conciseness. Or, if you had something else "write" it, then actually do so for the first time. Either way, I do not need to be regaled with the minutiae of every test if I can read the code. That entire PR description will become a commit in our repo, so please account for people staring at it in their terminal window after tapping out git status.

Also, please split out the addition of the future compatibility warning. This does not need to, and should not, all happen in one PR. Make this add -Zllvm-target-feature only.

View changes since this review

features in the form `+feat` or `-feat`, for example:

```sh
rustc -Zllvm-target-feature=+prefer-256-bit main.rs

@workingjubilee workingjubilee Jul 19, 2026

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.

Example should exemplify the multi-feature case (either in the same invocation or in another).

```

Each feature string is forwarded verbatim to LLVM. This allows using LLVM target features
that are not (or not yet) part of Rust's target feature system, for example to match the

@workingjubilee workingjubilee Jul 19, 2026

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.

This can be less ambiguous: some things LLVM calls "target features" should never be part of ours, because they e.g. are incoherent with target_feature(enable), which allows adding them on a per-function basis, and only make sense as whole-program modifiers.

@fo40225
fo40225 marked this pull request as draft July 19, 2026 23:35
@rustbot rustbot 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-review Status: Awaiting review from the assignee but also interested parties. labels Jul 19, 2026
@nnethercote

Copy link
Copy Markdown
Contributor

r? @workingjubilee

@rustbot

rustbot commented Jul 20, 2026

Copy link
Copy Markdown
Collaborator

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

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.

4 participants