Skip to content

Commit 4e481c6

Browse files
committed
cg_gcc: properly populate cfg(target_features) with -Ctarget-features
1 parent 03467f9 commit 4e481c6

File tree

3 files changed

+35
-45
lines changed

3 files changed

+35
-45
lines changed

compiler/rustc_codegen_gcc/src/lib.rs

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,9 @@ use rustc_codegen_ssa::back::write::{
101101
CodegenContext, FatLtoInput, ModuleConfig, TargetMachineFactoryFn,
102102
};
103103
use rustc_codegen_ssa::base::codegen_crate;
104+
use rustc_codegen_ssa::target_features::cfg_target_feature;
104105
use rustc_codegen_ssa::traits::{CodegenBackend, ExtraBackendMethods, WriteBackendMethods};
105-
use rustc_codegen_ssa::{
106-
CodegenResults, CompiledModule, ModuleCodegen, TargetConfig, target_features,
107-
};
106+
use rustc_codegen_ssa::{CodegenResults, CompiledModule, ModuleCodegen, TargetConfig};
108107
use rustc_data_structures::fx::FxIndexMap;
109108
use rustc_data_structures::sync::IntoDynSyncSend;
110109
use rustc_errors::DiagCtxtHandle;
@@ -488,23 +487,19 @@ fn to_gcc_opt_level(optlevel: Option<OptLevel>) -> OptimizationLevel {
488487

489488
/// Returns the features that should be set in `cfg(target_feature)`.
490489
fn target_config(sess: &Session, target_info: &LockedTargetInfo) -> TargetConfig {
491-
let (unstable_target_features, target_features) = target_features::cfg_target_feature(
492-
sess,
493-
/* FIXME: we ignore `-Ctarget-feature` */ "",
494-
|feature| {
495-
// TODO: we disable Neon for now since we don't support the LLVM intrinsics for it.
496-
if feature == "neon" {
497-
return false;
498-
}
499-
target_info.cpu_supports(feature)
500-
/*
501-
adx, aes, avx, avx2, avx512bf16, avx512bitalg, avx512bw, avx512cd, avx512dq, avx512er, avx512f, avx512fp16, avx512ifma,
502-
avx512pf, avx512vbmi, avx512vbmi2, avx512vl, avx512vnni, avx512vp2intersect, avx512vpopcntdq,
503-
bmi1, bmi2, cmpxchg16b, ermsb, f16c, fma, fxsr, gfni, lzcnt, movbe, pclmulqdq, popcnt, rdrand, rdseed, rtm,
504-
sha, sse, sse2, sse3, sse4.1, sse4.2, sse4a, ssse3, tbm, vaes, vpclmulqdq, xsave, xsavec, xsaveopt, xsaves
505-
*/
506-
},
507-
);
490+
let (unstable_target_features, target_features) = cfg_target_feature(sess, |feature| {
491+
// TODO: we disable Neon for now since we don't support the LLVM intrinsics for it.
492+
if feature == "neon" {
493+
return false;
494+
}
495+
target_info.cpu_supports(feature)
496+
/*
497+
adx, aes, avx, avx2, avx512bf16, avx512bitalg, avx512bw, avx512cd, avx512dq, avx512er, avx512f, avx512fp16, avx512ifma,
498+
avx512pf, avx512vbmi, avx512vbmi2, avx512vl, avx512vnni, avx512vp2intersect, avx512vpopcntdq,
499+
bmi1, bmi2, cmpxchg16b, ermsb, f16c, fma, fxsr, gfni, lzcnt, movbe, pclmulqdq, popcnt, rdrand, rdseed, rtm,
500+
sha, sse, sse2, sse3, sse4.1, sse4.2, sse4a, ssse3, tbm, vaes, vpclmulqdq, xsave, xsavec, xsaveopt, xsaves
501+
*/
502+
});
508503

509504
TargetConfig {
510505
target_features,

compiler/rustc_codegen_llvm/src/llvm_util.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::{ptr, slice, str};
77

88
use libc::c_int;
99
use rustc_codegen_ssa::base::wants_wasm_eh;
10+
use rustc_codegen_ssa::target_features::cfg_target_feature;
1011
use rustc_codegen_ssa::{TargetConfig, target_features};
1112
use rustc_data_structures::fx::FxHashSet;
1213
use rustc_data_structures::small_c_str::SmallCStr;
@@ -323,24 +324,23 @@ pub(crate) fn target_config(sess: &Session) -> TargetConfig {
323324
// by LLVM.
324325
let target_machine = create_informational_target_machine(sess, true);
325326

326-
let (unstable_target_features, target_features) =
327-
target_features::cfg_target_feature(sess, &sess.opts.cg.target_feature, |feature| {
328-
if let Some(feat) = to_llvm_features(sess, feature) {
329-
// All the LLVM features this expands to must be enabled.
330-
for llvm_feature in feat {
331-
let cstr = SmallCStr::new(llvm_feature);
332-
// `LLVMRustHasFeature` is moderately expensive. On targets with many
333-
// features (e.g. x86) these calls take a non-trivial fraction of runtime
334-
// when compiling very small programs.
335-
if !unsafe { llvm::LLVMRustHasFeature(target_machine.raw(), cstr.as_ptr()) } {
336-
return false;
337-
}
327+
let (unstable_target_features, target_features) = cfg_target_feature(sess, |feature| {
328+
if let Some(feat) = to_llvm_features(sess, feature) {
329+
// All the LLVM features this expands to must be enabled.
330+
for llvm_feature in feat {
331+
let cstr = SmallCStr::new(llvm_feature);
332+
// `LLVMRustHasFeature` is moderately expensive. On targets with many
333+
// features (e.g. x86) these calls take a non-trivial fraction of runtime
334+
// when compiling very small programs.
335+
if !unsafe { llvm::LLVMRustHasFeature(target_machine.raw(), cstr.as_ptr()) } {
336+
return false;
338337
}
339-
true
340-
} else {
341-
false
342338
}
343-
});
339+
true
340+
} else {
341+
false
342+
}
343+
});
344344

345345
let mut cfg = TargetConfig {
346346
target_features,

compiler/rustc_codegen_ssa/src/target_features.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,7 @@ pub(crate) fn check_target_feature_trait_unsafe(tcx: TyCtxt<'_>, id: LocalDefId,
163163
/// and call the closure for each (expanded) Rust feature. If the list contains
164164
/// a syntactically invalid item (not starting with `+`/`-`), the error callback is invoked.
165165
fn parse_rust_feature_flag<'a>(
166-
sess: &Session,
167-
target_feature_flag: &'a str,
166+
sess: &'a Session,
168167
err_callback: impl Fn(&'a str),
169168
mut callback: impl FnMut(
170169
/* base_feature */ &'a str,
@@ -175,7 +174,7 @@ fn parse_rust_feature_flag<'a>(
175174
// A cache for the backwards implication map.
176175
let mut inverse_implied_features: Option<FxHashMap<&str, FxHashSet<&str>>> = None;
177176

178-
for feature in target_feature_flag.split(',') {
177+
for feature in sess.opts.cg.target_feature.split(',') {
179178
if let Some(base_feature) = feature.strip_prefix('+') {
180179
callback(base_feature, sess.target.implied_target_features(base_feature), true)
181180
} else if let Some(base_feature) = feature.strip_prefix('-') {
@@ -215,15 +214,13 @@ fn parse_rust_feature_flag<'a>(
215214
/// to populate `sess.unstable_target_features` and `sess.target_features` (these are the first and
216215
/// 2nd component of the return value, respectively).
217216
///
218-
/// `target_feature_flag` is the value of `-Ctarget-feature` (giving the caller a chance to override it).
219217
/// `target_base_has_feature` should check whether the given feature (a Rust feature name!) is enabled
220218
/// in the "base" target machine, i.e., without applying `-Ctarget-feature`.
221219
///
222220
/// We do not have to worry about RUSTC_SPECIFIC_FEATURES here, those are handled elsewhere.
223221
pub fn cfg_target_feature(
224222
sess: &Session,
225-
target_feature_flag: &str,
226-
mut is_feature_enabled: impl FnMut(&str) -> bool,
223+
mut target_base_has_feature: impl FnMut(&str) -> bool,
227224
) -> (Vec<Symbol>, Vec<Symbol>) {
228225
// Compute which of the known target features are enabled in the 'base' target machine. We only
229226
// consider "supported" features; "forbidden" features are not reflected in `cfg` as of now.
@@ -236,15 +233,14 @@ pub fn cfg_target_feature(
236233
if RUSTC_SPECIAL_FEATURES.contains(feature) {
237234
return true;
238235
}
239-
is_feature_enabled(feature)
236+
target_base_has_feature(feature)
240237
})
241238
.map(|(feature, _, _)| Symbol::intern(feature))
242239
.collect();
243240

244241
// Add enabled and remove disabled features.
245242
parse_rust_feature_flag(
246243
sess,
247-
target_feature_flag,
248244
/* err_callback */ |_| {},
249245
|_base_feature, new_features, enabled| {
250246
// Iteration order is irrelevant since this only influences an `UnordSet`.
@@ -323,7 +319,6 @@ pub fn flag_to_backend_features<'a, const N: usize>(
323319
let mut rust_features = vec![];
324320
parse_rust_feature_flag(
325321
sess,
326-
&sess.opts.cg.target_feature,
327322
/* err_callback */
328323
|feature| {
329324
if diagnostics {

0 commit comments

Comments
 (0)