Skip to content

Commit 8915a90

Browse files
committed
cg_gcc: properly populate cfg(target_features) with -Ctarget-features
1 parent 3d8a3cb commit 8915a90

File tree

3 files changed

+32
-39
lines changed

3 files changed

+32
-39
lines changed

compiler/rustc_codegen_gcc/src/lib.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -487,20 +487,19 @@ fn to_gcc_opt_level(optlevel: Option<OptLevel>) -> OptimizationLevel {
487487

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

505504
TargetConfig {
506505
target_features,

compiler/rustc_codegen_llvm/src/llvm_util.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -323,24 +323,23 @@ pub(crate) fn target_config(sess: &Session) -> TargetConfig {
323323
// by LLVM.
324324
let target_machine = create_informational_target_machine(sess, true);
325325

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

345344
let mut cfg = TargetConfig {
346345
target_features,

compiler/rustc_middle/src/target_features.rs

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

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

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

0 commit comments

Comments
 (0)