Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 945d0f1

Browse files
committedFeb 3, 2025
x86-retpoline flag (target modifier) to enable retpoline-related target features
1 parent 7daf4cf commit 945d0f1

File tree

8 files changed

+94
-4
lines changed

8 files changed

+94
-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
@@ -728,7 +728,9 @@ pub(crate) fn global_llvm_features(
728728
sess.dcx().emit_warn(unknown_feature);
729729
}
730730
Some((_, stability, _)) => {
731-
if let Err(reason) = stability.toggle_allowed() {
731+
if let Err(reason) = stability
732+
.toggle_allowed(|flag| sess.opts.target_feature_flag_enabled(flag))
733+
{
732734
sess.dcx().emit_warn(ForbiddenCTargetFeature {
733735
feature,
734736
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

+2
Original file line numberDiff line numberDiff line change
@@ -2528,6 +2528,8 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
25282528

25292529
let prints = collect_print_requests(early_dcx, &mut cg, &unstable_opts, matches);
25302530

2531+
Options::fill_target_features_by_flags(&unstable_opts, &mut cg);
2532+
25312533
let cg = cg;
25322534

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

‎compiler/rustc_session/src/options.rs

+25
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,28 @@ 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+
"x86-retpoline" => self.unstable_opts.x86_retpoline,
293+
_ => false,
294+
}
295+
}
296+
297+
pub fn fill_target_features_by_flags(
298+
unstable_opts: &UnstableOptions, cg: &mut CodegenOptions
299+
) {
300+
if unstable_opts.x86_retpoline {
301+
if !cg.target_feature.is_empty() {
302+
cg.target_feature.push(',');
303+
}
304+
cg.target_feature.push_str(
305+
"+retpoline-external-thunk,\
306+
+retpoline-indirect-branches,\
307+
+retpoline-indirect-calls"
308+
);
309+
}
310+
}
289311
}
290312
);
291313
}
@@ -2606,6 +2628,9 @@ written to standard error output)"),
26062628
"use spec-compliant C ABI for `wasm32-unknown-unknown` (default: legacy)"),
26072629
write_long_types_to_disk: bool = (true, parse_bool, [UNTRACKED],
26082630
"whether long type names should be written to files instead of being printed in errors"),
2631+
x86_retpoline: bool = (false, parse_bool, [TRACKED TARGET_MODIFIER],
2632+
"enable retpoline-external-thunk, retpoline-indirect-branches and retpoline-indirect-calls \
2633+
target features (default: no)"),
26092634
// tidy-alphabetical-end
26102635

26112636
// If you add a new option, please update:

‎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
}
@@ -417,6 +429,30 @@ const X86_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
417429
("prfchw", Unstable(sym::prfchw_target_feature), &[]),
418430
("rdrand", Stable, &[]),
419431
("rdseed", Stable, &[]),
432+
(
433+
"retpoline-external-thunk",
434+
Stability::EnabledByTargetModifierFlag {
435+
reason: "use `x86-retpoline` target modifier flag instead",
436+
flag: "x86-retpoline",
437+
},
438+
&[],
439+
),
440+
(
441+
"retpoline-indirect-branches",
442+
Stability::EnabledByTargetModifierFlag {
443+
reason: "use `x86-retpoline` target modifier flag instead",
444+
flag: "x86-retpoline",
445+
},
446+
&[],
447+
),
448+
(
449+
"retpoline-indirect-calls",
450+
Stability::EnabledByTargetModifierFlag {
451+
reason: "use `x86-retpoline` target modifier flag instead",
452+
flag: "x86-retpoline",
453+
},
454+
&[],
455+
),
420456
("rtm", Unstable(sym::rtm_target_feature), &[]),
421457
("sha", Stable, &["sse2"]),
422458
("sha512", Unstable(sym::sha512_sm_x86), &["avx2"]),
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,14 @@
1+
//@ revisions: by_flag by_feature
2+
//@ compile-flags: --target=x86_64-unknown-linux-gnu --crate-type=lib
3+
//@ needs-llvm-components: x86
4+
//@ [by_flag]compile-flags: -Zx86-retpoline
5+
//@ [by_feature]compile-flags: -Ctarget-feature=+retpoline-external-thunk
6+
//@ [by_flag]build-pass
7+
// For now this is just a warning.
8+
//@ [by_feature]build-pass
9+
#![feature(no_core, lang_items)]
10+
#![no_std]
11+
#![no_core]
12+
13+
#[lang = "sized"]
14+
pub trait Sized {}

0 commit comments

Comments
 (0)
Failed to load comments.