Skip to content

Commit 4fe6d52

Browse files
authored
Rollup merge of #72094 - petrochenkov:overfeature, r=nikic
cmdline: Make target features individually overridable Fixes #56527 Previously `-C target-feature=+avx2 -C target-feature=+fma` was equivalent to `-C target-feature=+fma` because the later `-C target-feature` option fully overridden previous `-C target-feature`. With this PR `-C target-feature=+avx2 -C target-feature=+fma` is equivalent to `-C target-feature=+avx2,+fma` and the options are combined. I'm not sure where the comma-separated features in a single option came from (clang uses a scheme with single feature per-option), but logically these features are entirely independent options. So they should be overridable individually as well to be more useful in hierarchical build system, and more consistent with other rustc options and clang behavior as well. Target feature options have a few other issues (#44815), but fixing those is going to be a bit more invasive.
2 parents ae66c62 + 00dcb66 commit 4fe6d52

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

src/doc/rustc/src/codegen-options/index.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,15 @@ machine. Each target has a default base CPU.
464464

465465
Individual targets will support different features; this flag lets you control
466466
enabling or disabling a feature. Each feature should be prefixed with a `+` to
467-
enable it or `-` to disable it. Separate multiple features with commas.
467+
enable it or `-` to disable it.
468+
469+
Features from multiple `-C target-feature` options are combined. \
470+
Multiple features can be specified in a single option by separating them
471+
with commas - `-C target-feature=+x,-y`. \
472+
If some feature is specified more than once with both `+` and `-`,
473+
then values passed later override values passed earlier. \
474+
For example, `-C target-feature=+x,-y,+z -Ctarget-feature=-x,+y`
475+
is equivalent to `-C target-feature=-x,+y,+z`.
468476

469477
To see the valid options and an example of use, run `rustc --print
470478
target-features`.

src/librustc_session/options.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ macro_rules! options {
271271
"one of supported relocation models (`rustc --print relocation-models`)";
272272
pub const parse_tls_model: &str =
273273
"one of supported TLS models (`rustc --print tls-models`)";
274+
pub const parse_target_feature: &str = parse_string;
274275
}
275276

276277
#[allow(dead_code)]
@@ -647,6 +648,19 @@ macro_rules! options {
647648
}
648649
true
649650
}
651+
652+
fn parse_target_feature(slot: &mut String, v: Option<&str>) -> bool {
653+
match v {
654+
Some(s) => {
655+
if !slot.is_empty() {
656+
slot.push_str(",");
657+
}
658+
slot.push_str(s);
659+
true
660+
}
661+
None => false,
662+
}
663+
}
650664
}
651665
) }
652666

@@ -742,7 +756,7 @@ options! {CodegenOptions, CodegenSetter, basic_codegen_options,
742756
"use soft float ABI (*eabihf targets only) (default: no)"),
743757
target_cpu: Option<String> = (None, parse_opt_string, [TRACKED],
744758
"select target processor (`rustc --print target-cpus` for details)"),
745-
target_feature: String = (String::new(), parse_string, [TRACKED],
759+
target_feature: String = (String::new(), parse_target_feature, [TRACKED],
746760
"target specific attributes. (`rustc --print target-features` for details). \
747761
This feature is unsafe."),
748762

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// only-x86_64
2+
// compile-flags: -C target-feature=+sse2,-avx,+avx2 -C target-feature=+avx,-avx2
3+
4+
#![crate_type = "lib"]
5+
6+
#[no_mangle]
7+
pub fn foo() {
8+
// CHECK: attributes #0 = { {{.*}}"target-features"="+sse2,-avx,+avx2,+avx,-avx2"{{.*}} }
9+
}

0 commit comments

Comments
 (0)