Skip to content

Commit 597eb26

Browse files
committed
-Zretpoline and -Zretpoline-external-thunk flags (target modifiers) to enable retpoline-related target features
1 parent ae8ab87 commit 597eb26

13 files changed

+177
-4
lines changed

compiler/rustc_codegen_gcc/src/gcc_util.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ pub(crate) fn global_gcc_features(sess: &Session, diagnostics: bool) -> Vec<Stri
9494
sess.dcx().emit_warn(unknown_feature);
9595
}
9696
Some(&(_, stability, _)) => {
97-
if let Err(reason) = stability.toggle_allowed() {
97+
if let Err(reason) =
98+
stability.toggle_allowed(|flag| sess.opts.target_feature_flag_enabled(flag))
99+
{
98100
sess.dcx().emit_warn(ForbiddenCTargetFeature {
99101
feature,
100102
enabled: if enable { "enabled" } else { "disabled" },

compiler/rustc_codegen_llvm/src/llvm_util.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,9 @@ pub(crate) fn global_llvm_features(
733733
sess.dcx().emit_warn(unknown_feature);
734734
}
735735
Some((_, stability, _)) => {
736-
if let Err(reason) = stability.toggle_allowed() {
736+
if let Err(reason) = stability
737+
.toggle_allowed(|flag| sess.opts.target_feature_flag_enabled(flag))
738+
{
737739
sess.dcx().emit_warn(ForbiddenCTargetFeature {
738740
feature,
739741
enabled: if enable { "enabled" } else { "disabled" },

compiler/rustc_codegen_ssa/src/target_features.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ pub(crate) fn from_target_feature_attr(
6464

6565
// Only allow target features whose feature gates have been enabled
6666
// and which are permitted to be toggled.
67-
if let Err(reason) = stability.toggle_allowed() {
67+
if let Err(reason) =
68+
stability.toggle_allowed(|flag| tcx.sess.opts.target_feature_flag_enabled(flag))
69+
{
6870
tcx.dcx().emit_err(errors::ForbiddenTargetFeatureAttr {
6971
span: item.span(),
7072
feature,

compiler/rustc_session/src/config.rs

+10
Original file line numberDiff line numberDiff line change
@@ -2531,6 +2531,16 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
25312531

25322532
let prints = collect_print_requests(early_dcx, &mut cg, &unstable_opts, matches);
25332533

2534+
// -Zretpoline-external-thunk also requires -Zretpoline
2535+
if unstable_opts.retpoline_external_thunk {
2536+
unstable_opts.retpoline = true;
2537+
target_modifiers.insert(
2538+
OptionsTargetModifiers::UnstableOptions(UnstableOptionsTargetModifiers::retpoline),
2539+
"true".to_string(),
2540+
);
2541+
}
2542+
Options::fill_target_features_by_flags(&unstable_opts, &mut cg);
2543+
25342544
let cg = cg;
25352545

25362546
let sysroot_opt = matches.opt_str("sysroot").map(|m| PathBuf::from(&m));

compiler/rustc_session/src/options.rs

+42
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,43 @@ macro_rules! top_level_options {
286286
mods.sort_by(|a, b| a.opt.cmp(&b.opt));
287287
mods
288288
}
289+
290+
pub fn target_feature_flag_enabled(&self, flag: &str) -> bool {
291+
match flag {
292+
"retpoline" => self.unstable_opts.retpoline,
293+
"retpoline-external-thunk" => self.unstable_opts.retpoline_external_thunk,
294+
_ => false,
295+
}
296+
}
297+
298+
pub fn fill_target_features_by_flags(
299+
unstable_opts: &UnstableOptions, cg: &mut CodegenOptions
300+
) {
301+
// -Zretpoline without -Zretpoline-external-thunk enables
302+
// retpoline-indirect-branches and retpoline-indirect-calls target features
303+
if unstable_opts.retpoline && !unstable_opts.retpoline_external_thunk {
304+
if !cg.target_feature.is_empty() {
305+
cg.target_feature.push(',');
306+
}
307+
cg.target_feature.push_str(
308+
"+retpoline-indirect-branches,\
309+
+retpoline-indirect-calls"
310+
);
311+
}
312+
// -Zretpoline-external-thunk (maybe, with -Zretpoline too) enables
313+
// retpoline-external-thunk, retpoline-indirect-branches and
314+
// retpoline-indirect-calls target features
315+
if unstable_opts.retpoline_external_thunk {
316+
if !cg.target_feature.is_empty() {
317+
cg.target_feature.push(',');
318+
}
319+
cg.target_feature.push_str(
320+
"+retpoline-external-thunk,\
321+
+retpoline-indirect-branches,\
322+
+retpoline-indirect-calls"
323+
);
324+
}
325+
}
289326
}
290327
);
291328
}
@@ -2423,6 +2460,11 @@ options! {
24232460
remark_dir: Option<PathBuf> = (None, parse_opt_pathbuf, [UNTRACKED],
24242461
"directory into which to write optimization remarks (if not specified, they will be \
24252462
written to standard error output)"),
2463+
retpoline: bool = (false, parse_bool, [TRACKED TARGET_MODIFIER],
2464+
"enables retpoline-indirect-branches and retpoline-indirect-calls target features (default: no)"),
2465+
retpoline_external_thunk: bool = (false, parse_bool, [TRACKED TARGET_MODIFIER],
2466+
"enables retpoline-external-thunk, retpoline-indirect-branches and retpoline-indirect-calls \
2467+
target features (default: no)"),
24262468
sanitizer: SanitizerSet = (SanitizerSet::empty(), parse_sanitizers, [TRACKED],
24272469
"use a sanitizer"),
24282470
sanitizer_cfi_canonical_jump_tables: Option<bool> = (Some(true), parse_opt_bool, [TRACKED],

compiler/rustc_target/src/target_features.rs

+37-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ pub enum Stability {
3434
/// particular for features are actually ABI configuration flags (not all targets are as nice as
3535
/// RISC-V and have an explicit way to set the ABI separate from target features).
3636
Forbidden { reason: &'static str },
37+
/// This feature can not be set via `-Ctarget-feature` or `#[target_feature]`, it can only be set
38+
/// by target modifier flag. Target modifier flags are tracked to be consistent in linked modules.
39+
EnabledByTargetModifierFlag { reason: &'static str, flag: &'static str },
3740
}
3841
use Stability::*;
3942

@@ -49,6 +52,7 @@ impl<CTX> HashStable<CTX> for Stability {
4952
Stability::Forbidden { reason } => {
5053
reason.hash_stable(hcx, hasher);
5154
}
55+
Stability::EnabledByTargetModifierFlag { .. } => {}
5256
}
5357
}
5458
}
@@ -74,15 +78,23 @@ impl Stability {
7478
Stability::Unstable(nightly_feature) => Some(nightly_feature),
7579
Stability::Stable { .. } => None,
7680
Stability::Forbidden { .. } => panic!("forbidden features should not reach this far"),
81+
Stability::EnabledByTargetModifierFlag { .. } => None,
7782
}
7883
}
7984

8085
/// Returns whether the feature may be toggled via `#[target_feature]` or `-Ctarget-feature`.
8186
/// (It might still be nightly-only even if this returns `true`, so make sure to also check
8287
/// `requires_nightly`.)
83-
pub fn toggle_allowed(&self) -> Result<(), &'static str> {
88+
pub fn toggle_allowed(&self, flag_enabled: impl Fn(&str) -> bool) -> Result<(), &'static str> {
8489
match self {
8590
Stability::Forbidden { reason } => Err(reason),
91+
Stability::EnabledByTargetModifierFlag { reason, flag } => {
92+
if !flag_enabled(*flag) {
93+
Err(reason)
94+
} else {
95+
Ok(())
96+
}
97+
}
8698
_ => Ok(()),
8799
}
88100
}
@@ -423,6 +435,30 @@ static X86_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
423435
("prfchw", Unstable(sym::prfchw_target_feature), &[]),
424436
("rdrand", Stable, &[]),
425437
("rdseed", Stable, &[]),
438+
(
439+
"retpoline-external-thunk",
440+
Stability::EnabledByTargetModifierFlag {
441+
reason: "use `retpoline-external-thunk` target modifier flag instead",
442+
flag: "retpoline-external-thunk",
443+
},
444+
&[],
445+
),
446+
(
447+
"retpoline-indirect-branches",
448+
Stability::EnabledByTargetModifierFlag {
449+
reason: "use `retpoline` target modifier flag instead",
450+
flag: "retpoline",
451+
},
452+
&[],
453+
),
454+
(
455+
"retpoline-indirect-calls",
456+
Stability::EnabledByTargetModifierFlag {
457+
reason: "use `retpoline` target modifier flag instead",
458+
flag: "retpoline",
459+
},
460+
&[],
461+
),
426462
("rtm", Unstable(sym::rtm_target_feature), &[]),
427463
("sha", Stable, &["sse2"]),
428464
("sha512", Unstable(sym::sha512_sm_x86), &["avx2"]),

tests/codegen/retpoline.rs

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// ignore-tidy-linelength
2+
// Test that the
3+
// `retpoline-external-thunk`, `retpoline-indirect-branches`, `retpoline-indirect-calls`
4+
// target features are (not) emitted when the `retpoline/retpoline-external-thunk` flag is (not) set.
5+
6+
//@ revisions: disabled enabled_retpoline enabled_retpoline_external_thunk
7+
//@ needs-llvm-components: x86
8+
//@ compile-flags: --target x86_64-unknown-linux-gnu
9+
//@ [enabled_retpoline] compile-flags: -Zretpoline
10+
//@ [enabled_retpoline_external_thunk] compile-flags: -Zretpoline-external-thunk
11+
12+
#![crate_type = "lib"]
13+
#![feature(no_core, lang_items)]
14+
#![no_core]
15+
16+
#[lang = "sized"]
17+
trait Sized {}
18+
19+
#[no_mangle]
20+
pub fn foo() {
21+
// CHECK: @foo() unnamed_addr #0
22+
23+
// disabled-NOT: attributes #0 = { {{.*}}"target-features"="{{[^"]*}}+retpoline-external-thunk{{.*}} }
24+
// disabled-NOT: attributes #0 = { {{.*}}"target-features"="{{[^"]*}}+retpoline-indirect-branches{{.*}} }
25+
// disabled-NOT: attributes #0 = { {{.*}}"target-features"="{{[^"]*}}+retpoline-indirect-calls{{.*}} }
26+
27+
// enabled_retpoline: attributes #0 = { {{.*}}"target-features"="{{[^"]*}}+retpoline-indirect-branches,+retpoline-indirect-calls{{.*}} }
28+
// enabled_retpoline_external_thunk: attributes #0 = { {{.*}}"target-features"="{{[^"]*}}+retpoline-external-thunk,+retpoline-indirect-branches,+retpoline-indirect-calls{{.*}} }
29+
}

tests/ui/check-cfg/target_feature.stderr

+3
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,9 @@ LL | cfg!(target_feature = "_UNEXPECTED_VALUE");
192192
`relax`
193193
`relaxed-simd`
194194
`reserve-x18`
195+
`retpoline-external-thunk`
196+
`retpoline-indirect-branches`
197+
`retpoline-indirect-calls`
195198
`rtm`
196199
`sb`
197200
`scq`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
warning: target feature `retpoline-external-thunk` cannot be enabled with `-Ctarget-feature`: use `x86-retpoline` target modifier flag instead
2+
|
3+
= note: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
4+
= note: for more information, see issue #116344 <https://github.com/rust-lang/rust/issues/116344>
5+
6+
warning: 1 warning emitted
7+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
warning: target feature `retpoline-external-thunk` cannot be enabled with `-Ctarget-feature`: use `retpoline-external-thunk` target modifier flag instead
2+
|
3+
= note: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
4+
= note: for more information, see issue #116344 <https://github.com/rust-lang/rust/issues/116344>
5+
6+
warning: 1 warning emitted
7+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
warning: target feature `retpoline-indirect-branches` cannot be enabled with `-Ctarget-feature`: use `retpoline` target modifier flag instead
2+
|
3+
= note: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
4+
= note: for more information, see issue #116344 <https://github.com/rust-lang/rust/issues/116344>
5+
6+
warning: 1 warning emitted
7+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
warning: target feature `retpoline-indirect-calls` cannot be enabled with `-Ctarget-feature`: use `retpoline` target modifier flag instead
2+
|
3+
= note: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
4+
= note: for more information, see issue #116344 <https://github.com/rust-lang/rust/issues/116344>
5+
6+
warning: 1 warning emitted
7+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//@ revisions: by_flag by_feature1 by_feature2 by_feature3
2+
//@ compile-flags: --target=x86_64-unknown-linux-gnu --crate-type=lib
3+
//@ needs-llvm-components: x86
4+
//@ [by_flag]compile-flags: -Zretpoline
5+
6+
//@ [by_feature1]compile-flags: -Ctarget-feature=+retpoline-external-thunk
7+
//@ [by_feature2]compile-flags: -Ctarget-feature=+retpoline-indirect-branches
8+
//@ [by_feature3]compile-flags: -Ctarget-feature=+retpoline-indirect-calls
9+
//@ [by_flag]build-pass
10+
// For now this is just a warning.
11+
//@ [by_feature1]build-pass
12+
//@ [by_feature2]build-pass
13+
//@ [by_feature3]build-pass
14+
#![feature(no_core, lang_items)]
15+
#![no_std]
16+
#![no_core]
17+
18+
#[lang = "sized"]
19+
pub trait Sized {}

0 commit comments

Comments
 (0)