Skip to content

Commit

Permalink
x86-retpoline flag (target modifier) to enable retpoline-related targ…
Browse files Browse the repository at this point in the history
…et features
  • Loading branch information
azhogin committed Feb 2, 2025
1 parent 05c88a3 commit f331c60
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 4 deletions.
4 changes: 3 additions & 1 deletion compiler/rustc_codegen_gcc/src/gcc_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ pub(crate) fn global_gcc_features(sess: &Session, diagnostics: bool) -> Vec<Stri
sess.dcx().emit_warn(unknown_feature);
}
Some(&(_, stability, _)) => {
if let Err(reason) = stability.toggle_allowed() {
if let Err(reason) = stability.toggle_allowed(
|flag| sess.opts.target_feature_flag_enabled(flag)
) {
sess.dcx().emit_warn(ForbiddenCTargetFeature {
feature,
enabled: if enable { "enabled" } else { "disabled" },
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_codegen_llvm/src/llvm_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,9 @@ pub(crate) fn global_llvm_features(
sess.dcx().emit_warn(unknown_feature);
}
Some((_, stability, _)) => {
if let Err(reason) = stability.toggle_allowed() {
if let Err(reason) = stability.toggle_allowed(
|flag| sess.opts.target_feature_flag_enabled(flag)
) {
sess.dcx().emit_warn(ForbiddenCTargetFeature {
feature,
enabled: if enable { "enabled" } else { "disabled" },
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_codegen_ssa/src/target_features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ pub(crate) fn from_target_feature_attr(

// Only allow target features whose feature gates have been enabled
// and which are permitted to be toggled.
if let Err(reason) = stability.toggle_allowed() {
if let Err(reason) = stability.toggle_allowed(
|flag| tcx.sess.opts.target_feature_flag_enabled(flag)
) {
tcx.dcx().emit_err(errors::ForbiddenTargetFeatureAttr {
span: item.span(),
feature,
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_session/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2495,6 +2495,8 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M

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

Options::fill_target_features_by_flags(&unstable_opts, &mut cg);

let cg = cg;

let sysroot_opt = matches.opt_str("sysroot").map(|m| PathBuf::from(&m));
Expand Down
25 changes: 25 additions & 0 deletions compiler/rustc_session/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,28 @@ 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 {
"x86-retpoline" => self.unstable_opts.x86_retpoline,
_ => false,
}
}

pub fn fill_target_features_by_flags(
unstable_opts: &UnstableOptions, cg: &mut CodegenOptions
) {
if unstable_opts.x86_retpoline {
if !cg.target_feature.is_empty() {
cg.target_feature.push(',');
}
cg.target_feature.push_str(
"+retpoline-external-thunk,\
+retpoline-indirect-branches,\
+retpoline-indirect-calls"
);
}
}
}
);
}
Expand Down Expand Up @@ -2560,6 +2582,9 @@ written to standard error output)"),
"use spec-compliant C ABI for `wasm32-unknown-unknown` (default: legacy)"),
write_long_types_to_disk: bool = (true, parse_bool, [UNTRACKED],
"whether long type names should be written to files instead of being printed in errors"),
x86_retpoline: bool = (false, parse_bool, [TRACKED TARGET_MODIFIER],
"enable retpoline-external-thunk, retpoline-indirect-branches and retpoline-indirect-calls \
target features (default: no)"),
// tidy-alphabetical-end

// If you add a new option, please update:
Expand Down
26 changes: 25 additions & 1 deletion compiler/rustc_target/src/target_features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
}
use Stability::*;

Expand All @@ -49,6 +52,7 @@ impl<CTX> HashStable<CTX> for Stability {
Stability::Forbidden { reason } => {
reason.hash_stable(hcx, hasher);
}
Stability::EnabledByTargetModifierFlag { .. } => {}
}
}
}
Expand All @@ -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> {
pub fn toggle_allowed(&self, flag_enabled: impl Fn(&str) -> bool) -> Result<(), &'static str> {
match self {
Stability::Forbidden { reason } => Err(reason),
Stability::EnabledByTargetModifierFlag { reason, flag } => {
if !flag_enabled(*flag) {
Err(reason)
} else {
Ok(())
}
}
_ => Ok(()),
}
}
Expand Down Expand Up @@ -414,6 +426,18 @@ const X86_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
("prfchw", Unstable(sym::prfchw_target_feature), &[]),
("rdrand", Stable, &[]),
("rdseed", Stable, &[]),
("retpoline-external-thunk", Stability::EnabledByTargetModifierFlag {
reason: "use `x86-retpoline` target modifier flag instead",
flag: "x86-retpoline",
}, &[]),
("retpoline-indirect-branches", Stability::EnabledByTargetModifierFlag {
reason: "use `x86-retpoline` target modifier flag instead",
flag: "x86-retpoline",
}, &[]),
("retpoline-indirect-calls", Stability::EnabledByTargetModifierFlag {
reason: "use `x86-retpoline` target modifier flag instead",
flag: "x86-retpoline",
}, &[]),
("rtm", Unstable(sym::rtm_target_feature), &[]),
("sha", Stable, &["sse2"]),
("sha512", Unstable(sym::sha512_sm_x86), &["avx2"]),
Expand Down
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

14 changes: 14 additions & 0 deletions tests/ui/target-feature/retpoline-target-feature-flag.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//@ revisions: by_flag by_feature
//@ compile-flags: --target=x86_64-unknown-linux-gnu --crate-type=lib
//@ needs-llvm-components: x86
//@ [by_flag]compile-flags: -Zx86-retpoline
//@ [by_feature]compile-flags: -Ctarget-feature=+retpoline-external-thunk
//@ [by_flag]build-pass
// For now this is just a warning.
//@ [by_feature]build-pass
#![feature(no_core, lang_items)]
#![no_std]
#![no_core]

#[lang = "sized"]
pub trait Sized {}

0 comments on commit f331c60

Please sign in to comment.