Skip to content
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

cmdline: Make target features individually overridable #72094

Merged
merged 1 commit into from
May 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/doc/rustc/src/codegen-options/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,15 @@ machine. Each target has a default base CPU.

Individual targets will support different features; this flag lets you control
enabling or disabling a feature. Each feature should be prefixed with a `+` to
enable it or `-` to disable it. Separate multiple features with commas.
enable it or `-` to disable it.

Features from multiple `-C target-feature` options are combined. \
Multiple features can be specified in a single option by separating them
with commas - `-C target-feature=+x,-y`. \
If some feature is specified more than once with both `+` and `-`,
then values passed later override values passed earlier. \
For example, `-C target-feature=+x,-y,+z -Ctarget-feature=-x,+y`
is equivalent to `-C target-feature=-x,+y,+z`.
Copy link
Contributor

Choose a reason for hiding this comment

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

Just out of curiosity, what's up with the \ after periods? I see this is used elsewhere in this file, but have never encountered this Markdown syntax before.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's a line break.
I initially used trailing spaces for this, but it regularly confuses various people's editors, see #71716 (comment).

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks, that makes sense! The choice of syntax for this is a bit confusing, as it has the exact opposite meaning in some programming languages like C...


To see the valid options and an example of use, run `rustc --print
target-features`.
Expand Down
16 changes: 15 additions & 1 deletion src/librustc_session/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ macro_rules! options {
"one of supported relocation models (`rustc --print relocation-models`)";
pub const parse_tls_model: &str =
"one of supported TLS models (`rustc --print tls-models`)";
pub const parse_target_feature: &str = parse_string;
}

#[allow(dead_code)]
Expand Down Expand Up @@ -636,6 +637,19 @@ macro_rules! options {
}
true
}

fn parse_target_feature(slot: &mut String, v: Option<&str>) -> bool {
match v {
Some(s) => {
if !slot.is_empty() {
slot.push_str(",");
}
slot.push_str(s);
true
}
None => false,
}
}
}
) }

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

Expand Down
9 changes: 9 additions & 0 deletions src/test/codegen/target-feature-multiple.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// only-x86_64
// compile-flags: -C target-feature=+sse2,-avx,+avx2 -C target-feature=+avx,-avx2

#![crate_type = "lib"]

#[no_mangle]
pub fn foo() {
// CHECK: attributes #0 = { {{.*}}"target-features"="+sse2,-avx,+avx2,+avx,-avx2"{{.*}} }
Copy link
Contributor Author

Choose a reason for hiding this comment

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

See #44815 (comment) regarding canonicalization of the feature list.
I'll try to fix this some time later together with other issues.

}