-
-
Notifications
You must be signed in to change notification settings - Fork 15.4k
Add -Zllvm-target-feature to pass features directly to LLVM #159545
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
fo40225
wants to merge
1
commit into
rust-lang:main
Choose a base branch
from
fo40225:Zllvm-target-feature
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/doc/unstable-book/src/compiler-flags/llvm-target-feature.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| # `llvm-target-feature` | ||
|
|
||
| The tracking issue for this feature is: [#157753](https://github.com/rust-lang/rust/issues/157753). | ||
|
|
||
| ------------------------ | ||
|
|
||
| The `-Zllvm-target-feature` flag passes target feature strings directly to the LLVM | ||
| backend, bypassing Rust's known-feature validation. It accepts a comma-separated list of | ||
| features in the form `+feat` or `-feat`, for example: | ||
|
|
||
| ```sh | ||
| rustc -Zllvm-target-feature=+prefer-256-bit main.rs | ||
| ``` | ||
|
|
||
| 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 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| ABI of foreign libraries built with features Rust does not recognize. | ||
|
|
||
| Because such features can change the ABI, this flag is registered as a *target modifier*: | ||
| linking together crates compiled with different values of this flag is an error by | ||
| default. If you are sure the mismatch is sound, it can be overridden with | ||
| `-Cunsafe-allow-abi-mismatch=llvm-target-feature`. | ||
|
|
||
| ## Scope and interactions | ||
|
|
||
| This flag: | ||
|
|
||
| - does **not** affect `cfg(target_feature)`: conditional compilation will not observe | ||
| features passed this way; | ||
| - does **not** affect runtime feature detection macros such as `is_x86_feature_detected!`; | ||
| - is ignored by non-LLVM codegen backends (like `-Cllvm-args`); | ||
| - performs no validation: invalid or unsupported feature names are reported (or silently | ||
| ignored) by LLVM itself during codegen. | ||
|
|
||
| ## Relationship to `-Ctarget-feature` | ||
|
|
||
| `-Ctarget-feature` is the supported way to toggle target features that Rust recognizes. | ||
| Passing feature names unknown to Rust through `-Ctarget-feature` is deprecated and will | ||
| eventually become a hard error; use `-Zllvm-target-feature` for LLVM-only features | ||
| instead. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| // Test that `-Zllvm-target-feature` forwards raw feature strings directly to the | ||
| // LLVM backend, bypassing Rust's known-feature table. This test uses | ||
| // `prefer-256-bit`, an LLVM-only x86 feature that is not listed in rustc's | ||
| // `target_features.rs`. | ||
|
|
||
| //@ add-minicore | ||
| //@ needs-llvm-components: x86 | ||
| //@ compile-flags: --target x86_64-unknown-linux-gnu -Zllvm-target-feature=+prefer-256-bit | ||
| #![crate_type = "lib"] | ||
| #![feature(no_core)] | ||
| #![no_core] | ||
| extern crate minicore; | ||
|
|
||
| #[no_mangle] | ||
| pub fn foo() { | ||
| // CHECK: @foo() unnamed_addr #0 | ||
|
|
||
| // CHECK: attributes #0 = { {{.*}}"target-features"="{{[^"]*}}+prefer-256-bit{{.*}} } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
tests/ui/target_modifiers/auxiliary/wrong_llvm_target_feature.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| //@ no-prefer-dynamic | ||
| //@ compile-flags: --target x86_64-unknown-linux-gnu -Zllvm-target-feature=+fake-feature | ||
| //@ needs-llvm-components: x86 | ||
|
|
||
| #![feature(no_core)] | ||
| #![crate_type = "rlib"] | ||
| #![no_core] |
1 change: 1 addition & 0 deletions
1
...rget_modifiers/incompatible_llvm_target_feature.allow_llvm_target_feature_mismatch.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| '+another-feature' is not a recognized feature for this target (ignoring feature) |
2 changes: 2 additions & 0 deletions
2
tests/ui/target_modifiers/incompatible_llvm_target_feature.allow_no_value.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| error: codegen option `unsafe-allow-abi-mismatch` requires a comma-separated list of strings (`-C unsafe-allow-abi-mismatch=<value>`) | ||
|
|
14 changes: 14 additions & 0 deletions
14
tests/ui/target_modifiers/incompatible_llvm_target_feature.error_generated.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| '+another-feature' is not a recognized feature for this target (ignoring feature) | ||
| error: mixing `-Zllvm-target-feature` will cause an ABI mismatch in crate `incompatible_llvm_target_feature` | ||
| --> $DIR/incompatible_llvm_target_feature.rs:12:1 | ||
| | | ||
| LL | #![feature(no_core)] | ||
| | ^ | ||
| | | ||
| = help: the `-Zllvm-target-feature` flag modifies the ABI so Rust crates compiled with different values of this flag cannot be used together safely | ||
| = note: `-Zllvm-target-feature=+another-feature` in this crate is incompatible with `-Zllvm-target-feature=+fake-feature` in dependency `wrong_llvm_target_feature` | ||
| = help: set `-Zllvm-target-feature=+fake-feature` in this crate or `-Zllvm-target-feature=+another-feature` in `wrong_llvm_target_feature` | ||
| = help: if you are sure this will not cause problems, you may use `-Cunsafe-allow-abi-mismatch=llvm-target-feature` to silence this error | ||
|
|
||
| error: aborting due to 1 previous error | ||
|
|
19 changes: 19 additions & 0 deletions
19
tests/ui/target_modifiers/incompatible_llvm_target_feature.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| //@ aux-build:wrong_llvm_target_feature.rs | ||
| //@ compile-flags: --target x86_64-unknown-linux-gnu -Zllvm-target-feature=+another-feature | ||
| //@ needs-llvm-components: x86 | ||
|
|
||
| //@ revisions:allow_llvm_target_feature_mismatch allow_no_value error_generated | ||
| //@[allow_llvm_target_feature_mismatch] compile-flags: -Cunsafe-allow-abi-mismatch=llvm-target-feature | ||
| //@[allow_no_value] compile-flags: -Cunsafe-allow-abi-mismatch | ||
| // [error_generated] no extra compile-flags | ||
| //@[allow_llvm_target_feature_mismatch] check-pass | ||
| //@ ignore-backends: gcc | ||
|
|
||
| #![feature(no_core)] | ||
| //[error_generated]~^ ERROR mixing `-Zllvm-target-feature` will cause an ABI mismatch in crate `incompatible_llvm_target_feature` | ||
| #![crate_type = "rlib"] | ||
| #![no_core] | ||
|
|
||
| extern crate wrong_llvm_target_feature; | ||
|
|
||
| //[allow_no_value]~? ERROR codegen option `unsafe-allow-abi-mismatch` requires a comma-separated list of strings |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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).