-
Notifications
You must be signed in to change notification settings - Fork 13.3k
retpoline and retpoline-external-thunk flags (target modifiers) to enable retpoline-related target features #135927
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -286,6 +286,43 @@ macro_rules! top_level_options { | |
mods.sort_by(|a, b| a.opt.cmp(&b.opt)); | ||
mods | ||
} | ||
|
||
pub fn target_feature_flag_enabled(&self, flag: &str) -> bool { | ||
match flag { | ||
"retpoline" => self.unstable_opts.retpoline, | ||
"retpoline-external-thunk" => self.unstable_opts.retpoline_external_thunk, | ||
_ => false, | ||
} | ||
} | ||
|
||
pub fn fill_target_features_by_flags( | ||
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 doesn't feel like the right place to do this, ideally there would be a query or something that we could add this logic to that would mean that codegen backends just get the features they need to enable and we'd insert this logic into that. |
||
unstable_opts: &UnstableOptions, cg: &mut CodegenOptions | ||
) { | ||
// -Zretpoline without -Zretpoline-external-thunk enables | ||
// retpoline-indirect-branches and retpoline-indirect-calls target features | ||
if unstable_opts.retpoline && !unstable_opts.retpoline_external_thunk { | ||
if !cg.target_feature.is_empty() { | ||
cg.target_feature.push(','); | ||
} | ||
cg.target_feature.push_str( | ||
"+retpoline-indirect-branches,\ | ||
+retpoline-indirect-calls" | ||
); | ||
} | ||
// -Zretpoline-external-thunk (maybe, with -Zretpoline too) enables | ||
// retpoline-external-thunk, retpoline-indirect-branches and | ||
// retpoline-indirect-calls target features | ||
if unstable_opts.retpoline_external_thunk { | ||
if !cg.target_feature.is_empty() { | ||
cg.target_feature.push(','); | ||
} | ||
cg.target_feature.push_str( | ||
"+retpoline-external-thunk,\ | ||
+retpoline-indirect-branches,\ | ||
+retpoline-indirect-calls" | ||
); | ||
} | ||
} | ||
} | ||
); | ||
} | ||
|
@@ -2423,6 +2460,11 @@ options! { | |
remark_dir: Option<PathBuf> = (None, parse_opt_pathbuf, [UNTRACKED], | ||
"directory into which to write optimization remarks (if not specified, they will be \ | ||
written to standard error output)"), | ||
retpoline: bool = (false, parse_bool, [TRACKED TARGET_MODIFIER], | ||
"enables retpoline-indirect-branches and retpoline-indirect-calls target features (default: no)"), | ||
retpoline_external_thunk: bool = (false, parse_bool, [TRACKED TARGET_MODIFIER], | ||
"enables retpoline-external-thunk, retpoline-indirect-branches and retpoline-indirect-calls \ | ||
target features (default: no)"), | ||
sanitizer: SanitizerSet = (SanitizerSet::empty(), parse_sanitizers, [TRACKED], | ||
"use a sanitizer"), | ||
sanitizer_cfi_canonical_jump_tables: Option<bool> = (Some(true), parse_opt_bool, [TRACKED], | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -34,6 +34,9 @@ pub enum Stability { | |||||
/// particular for features are actually ABI configuration flags (not all targets are as nice as | ||||||
/// RISC-V and have an explicit way to set the ABI separate from target features). | ||||||
Forbidden { reason: &'static str }, | ||||||
/// This feature can not be set via `-Ctarget-feature` or `#[target_feature]`, it can only be set | ||||||
/// by target modifier flag. Target modifier flags are tracked to be consistent in linked modules. | ||||||
EnabledByTargetModifierFlag { reason: &'static str, flag: &'static str }, | ||||||
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.
Suggested change
|
||||||
} | ||||||
use Stability::*; | ||||||
|
||||||
|
@@ -49,6 +52,7 @@ impl<CTX> HashStable<CTX> for Stability { | |||||
Stability::Forbidden { reason } => { | ||||||
reason.hash_stable(hcx, hasher); | ||||||
} | ||||||
Stability::EnabledByTargetModifierFlag { .. } => {} | ||||||
} | ||||||
} | ||||||
} | ||||||
|
@@ -74,15 +78,23 @@ impl Stability { | |||||
Stability::Unstable(nightly_feature) => Some(nightly_feature), | ||||||
Stability::Stable { .. } => None, | ||||||
Stability::Forbidden { .. } => panic!("forbidden features should not reach this far"), | ||||||
Stability::EnabledByTargetModifierFlag { .. } => None, | ||||||
} | ||||||
} | ||||||
|
||||||
/// Returns whether the feature may be toggled via `#[target_feature]` or `-Ctarget-feature`. | ||||||
/// (It might still be nightly-only even if this returns `true`, so make sure to also check | ||||||
/// `requires_nightly`.) | ||||||
pub fn toggle_allowed(&self) -> Result<(), &'static str> { | ||||||
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 API seems confusing, at the very least change the doc comment. |
||||||
pub fn toggle_allowed(&self, flag_enabled: impl Fn(&str) -> bool) -> Result<(), &'static str> { | ||||||
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. pre-existing: this name is a little ambiguous, is it toggling something named 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. Could we add a |
||||||
match self { | ||||||
Stability::Forbidden { reason } => Err(reason), | ||||||
Stability::EnabledByTargetModifierFlag { reason, flag } => { | ||||||
if !flag_enabled(*flag) { | ||||||
Err(reason) | ||||||
} else { | ||||||
Ok(()) | ||||||
} | ||||||
} | ||||||
_ => Ok(()), | ||||||
} | ||||||
} | ||||||
|
@@ -423,6 +435,30 @@ static X86_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[ | |||||
("prfchw", Unstable(sym::prfchw_target_feature), &[]), | ||||||
("rdrand", Stable, &[]), | ||||||
("rdseed", Stable, &[]), | ||||||
( | ||||||
"retpoline-external-thunk", | ||||||
Stability::EnabledByTargetModifierFlag { | ||||||
reason: "use `retpoline-external-thunk` target modifier flag instead", | ||||||
flag: "retpoline-external-thunk", | ||||||
}, | ||||||
&[], | ||||||
), | ||||||
( | ||||||
"retpoline-indirect-branches", | ||||||
Stability::EnabledByTargetModifierFlag { | ||||||
reason: "use `retpoline` target modifier flag instead", | ||||||
flag: "retpoline", | ||||||
}, | ||||||
&[], | ||||||
), | ||||||
( | ||||||
"retpoline-indirect-calls", | ||||||
Stability::EnabledByTargetModifierFlag { | ||||||
reason: "use `retpoline` target modifier flag instead", | ||||||
flag: "retpoline", | ||||||
}, | ||||||
&[], | ||||||
), | ||||||
("rtm", Unstable(sym::rtm_target_feature), &[]), | ||||||
("sha", Stable, &["sse2"]), | ||||||
("sha512", Unstable(sym::sha512_sm_x86), &["avx2"]), | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// ignore-tidy-linelength | ||
// Test that the | ||
// `retpoline-external-thunk`, `retpoline-indirect-branches`, `retpoline-indirect-calls` | ||
// target features are (not) emitted when the `retpoline/retpoline-external-thunk` flag is (not) set. | ||
|
||
//@ revisions: disabled enabled_retpoline enabled_retpoline_external_thunk | ||
//@ needs-llvm-components: x86 | ||
//@ compile-flags: --target x86_64-unknown-linux-gnu | ||
//@ [enabled_retpoline] compile-flags: -Zretpoline | ||
//@ [enabled_retpoline_external_thunk] compile-flags: -Zretpoline-external-thunk | ||
|
||
#![crate_type = "lib"] | ||
#![feature(no_core, lang_items)] | ||
#![no_core] | ||
|
||
#[lang = "sized"] | ||
trait Sized {} | ||
|
||
#[no_mangle] | ||
pub fn foo() { | ||
// CHECK: @foo() unnamed_addr #0 | ||
|
||
// disabled-NOT: attributes #0 = { {{.*}}"target-features"="{{[^"]*}}+retpoline-external-thunk{{.*}} } | ||
// disabled-NOT: attributes #0 = { {{.*}}"target-features"="{{[^"]*}}+retpoline-indirect-branches{{.*}} } | ||
// disabled-NOT: attributes #0 = { {{.*}}"target-features"="{{[^"]*}}+retpoline-indirect-calls{{.*}} } | ||
|
||
// enabled_retpoline: attributes #0 = { {{.*}}"target-features"="{{[^"]*}}+retpoline-indirect-branches,+retpoline-indirect-calls{{.*}} } | ||
// enabled_retpoline_external_thunk: attributes #0 = { {{.*}}"target-features"="{{[^"]*}}+retpoline-external-thunk,+retpoline-indirect-branches,+retpoline-indirect-calls{{.*}} } | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
warning: target feature `retpoline-external-thunk` cannot be enabled with `-Ctarget-feature`: use `x86-retpoline` target modifier flag instead | ||
| | ||
= note: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! | ||
= note: for more information, see issue #116344 <https://github.com/rust-lang/rust/issues/116344> | ||
|
||
warning: 1 warning emitted | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
warning: target feature `retpoline-external-thunk` cannot be enabled with `-Ctarget-feature`: use `retpoline-external-thunk` target modifier flag instead | ||
| | ||
= note: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! | ||
= note: for more information, see issue #116344 <https://github.com/rust-lang/rust/issues/116344> | ||
|
||
warning: 1 warning emitted | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
warning: target feature `retpoline-indirect-branches` cannot be enabled with `-Ctarget-feature`: use `retpoline` target modifier flag instead | ||
| | ||
= note: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! | ||
= note: for more information, see issue #116344 <https://github.com/rust-lang/rust/issues/116344> | ||
|
||
warning: 1 warning emitted | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
warning: target feature `retpoline-indirect-calls` cannot be enabled with `-Ctarget-feature`: use `retpoline` target modifier flag instead | ||
| | ||
= note: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! | ||
= note: for more information, see issue #116344 <https://github.com/rust-lang/rust/issues/116344> | ||
|
||
warning: 1 warning emitted | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
//@ revisions: by_flag by_feature1 by_feature2 by_feature3 | ||
//@ compile-flags: --target=x86_64-unknown-linux-gnu --crate-type=lib | ||
//@ needs-llvm-components: x86 | ||
//@ [by_flag]compile-flags: -Zretpoline | ||
|
||
//@ [by_feature1]compile-flags: -Ctarget-feature=+retpoline-external-thunk | ||
//@ [by_feature2]compile-flags: -Ctarget-feature=+retpoline-indirect-branches | ||
//@ [by_feature3]compile-flags: -Ctarget-feature=+retpoline-indirect-calls | ||
//@ [by_flag]build-pass | ||
// For now this is just a warning. | ||
//@ [by_feature1]build-pass | ||
//@ [by_feature2]build-pass | ||
//@ [by_feature3]build-pass | ||
#![feature(no_core, lang_items)] | ||
#![no_std] | ||
#![no_core] | ||
|
||
#[lang = "sized"] | ||
pub trait Sized {} |
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.
There's a bunch of duplication in the code around this between codegen backends and
rustc_codegen_ssa
, it's pre-existing, you could sort it if you wanted, but someone ought to.