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 f331c60

Browse files
committedFeb 2, 2025
x86-retpoline flag (target modifier) to enable retpoline-related target features
1 parent 05c88a3 commit f331c60

File tree

8 files changed

+82
-4
lines changed

8 files changed

+82
-4
lines changed
 

‎compiler/rustc_codegen_gcc/src/gcc_util.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ pub(crate) fn global_gcc_features(sess: &Session, diagnostics: bool) -> Vec<Stri
102102
sess.dcx().emit_warn(unknown_feature);
103103
}
104104
Some(&(_, stability, _)) => {
105-
if let Err(reason) = stability.toggle_allowed() {
105+
if let Err(reason) = stability.toggle_allowed(
106+
|flag| sess.opts.target_feature_flag_enabled(flag)
107+
) {
106108
sess.dcx().emit_warn(ForbiddenCTargetFeature {
107109
feature,
108110
enabled: if enable { "enabled" } else { "disabled" },

‎compiler/rustc_codegen_llvm/src/llvm_util.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,9 @@ pub(crate) fn global_llvm_features(
731731
sess.dcx().emit_warn(unknown_feature);
732732
}
733733
Some((_, stability, _)) => {
734-
if let Err(reason) = stability.toggle_allowed() {
734+
if let Err(reason) = stability.toggle_allowed(
735+
|flag| sess.opts.target_feature_flag_enabled(flag)
736+
) {
735737
sess.dcx().emit_warn(ForbiddenCTargetFeature {
736738
feature,
737739
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) = stability.toggle_allowed(
68+
|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
@@ -2495,6 +2495,8 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
24952495

24962496
let prints = collect_print_requests(early_dcx, &mut cg, &unstable_opts, matches);
24972497

2498+
Options::fill_target_features_by_flags(&unstable_opts, &mut cg);
2499+
24982500
let cg = cg;
24992501

25002502
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
}
@@ -2560,6 +2582,9 @@ written to standard error output)"),
25602582
"use spec-compliant C ABI for `wasm32-unknown-unknown` (default: legacy)"),
25612583
write_long_types_to_disk: bool = (true, parse_bool, [UNTRACKED],
25622584
"whether long type names should be written to files instead of being printed in errors"),
2585+
x86_retpoline: bool = (false, parse_bool, [TRACKED TARGET_MODIFIER],
2586+
"enable retpoline-external-thunk, retpoline-indirect-branches and retpoline-indirect-calls \
2587+
target features (default: no)"),
25632588
// tidy-alphabetical-end
25642589

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

‎compiler/rustc_target/src/target_features.rs

+25-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
}
@@ -414,6 +426,18 @@ const X86_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
414426
("prfchw", Unstable(sym::prfchw_target_feature), &[]),
415427
("rdrand", Stable, &[]),
416428
("rdseed", Stable, &[]),
429+
("retpoline-external-thunk", Stability::EnabledByTargetModifierFlag {
430+
reason: "use `x86-retpoline` target modifier flag instead",
431+
flag: "x86-retpoline",
432+
}, &[]),
433+
("retpoline-indirect-branches", Stability::EnabledByTargetModifierFlag {
434+
reason: "use `x86-retpoline` target modifier flag instead",
435+
flag: "x86-retpoline",
436+
}, &[]),
437+
("retpoline-indirect-calls", Stability::EnabledByTargetModifierFlag {
438+
reason: "use `x86-retpoline` target modifier flag instead",
439+
flag: "x86-retpoline",
440+
}, &[]),
417441
("rtm", Unstable(sym::rtm_target_feature), &[]),
418442
("sha", Stable, &["sse2"]),
419443
("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.