Skip to content

Commit b7ed069

Browse files
committed
Move warning reporting from flag_to_backend_features to cfg_target_feature
This way warnings are emitted even in a check build.
1 parent f15a7f3 commit b7ed069

21 files changed

+233
-213
lines changed

compiler/rustc_codegen_gcc/src/gcc_util.rs

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ fn gcc_features_by_flags(sess: &Session, features: &mut Vec<String>) {
1111

1212
/// The list of GCC features computed from CLI flags (`-Ctarget-cpu`, `-Ctarget-feature`,
1313
/// `--target` and similar).
14-
pub(crate) fn global_gcc_features(sess: &Session, diagnostics: bool) -> Vec<String> {
14+
pub(crate) fn global_gcc_features(sess: &Session) -> Vec<String> {
1515
// Features that come earlier are overridden by conflicting features later in the string.
1616
// Typically we'll want more explicit settings to override the implicit ones, so:
1717
//
@@ -36,27 +36,18 @@ pub(crate) fn global_gcc_features(sess: &Session, diagnostics: bool) -> Vec<Stri
3636
features.extend(sess.target.features.split(',').filter(|v| !v.is_empty()).map(String::from));
3737

3838
// -Ctarget-features
39-
target_features::flag_to_backend_features(
40-
sess,
41-
diagnostics,
42-
|feature| to_gcc_features(sess, feature),
43-
|feature, enable| {
44-
// We run through `to_gcc_features` when
45-
// passing requests down to GCC. This means that all in-language
46-
// features also work on the command line instead of having two
47-
// different names when the GCC name and the Rust name differ.
48-
features.extend(
49-
to_gcc_features(sess, feature)
50-
.iter()
51-
.flat_map(|feat| to_gcc_features(sess, feat).into_iter())
52-
.map(
53-
|feature| {
54-
if !enable { format!("-{}", feature) } else { feature.to_string() }
55-
},
56-
),
57-
);
58-
},
59-
);
39+
target_features::flag_to_backend_features(sess, |feature, enable| {
40+
// We run through `to_gcc_features` when
41+
// passing requests down to GCC. This means that all in-language
42+
// features also work on the command line instead of having two
43+
// different names when the GCC name and the Rust name differ.
44+
features.extend(
45+
to_gcc_features(sess, feature)
46+
.iter()
47+
.flat_map(|feat| to_gcc_features(sess, feat).into_iter())
48+
.map(|feature| if !enable { format!("-{}", feature) } else { feature.to_string() }),
49+
);
50+
});
6051

6152
gcc_features_by_flags(sess, &mut features);
6253

compiler/rustc_codegen_gcc/src/lib.rs

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ use rustc_target::spec::RelocModel;
107107
use tempfile::TempDir;
108108

109109
use crate::back::lto::ModuleBuffer;
110-
use crate::gcc_util::target_cpu;
110+
use crate::gcc_util::{target_cpu, to_gcc_features};
111111

112112
rustc_fluent_macro::fluent_messages! { "../messages.ftl" }
113113

@@ -220,7 +220,7 @@ impl CodegenBackend for GccCodegenBackend {
220220
}
221221

222222
fn provide(&self, providers: &mut Providers) {
223-
providers.global_backend_features = |tcx, ()| gcc_util::global_gcc_features(tcx.sess, true)
223+
providers.global_backend_features = |tcx, ()| gcc_util::global_gcc_features(tcx.sess)
224224
}
225225

226226
fn codegen_crate(&self, tcx: TyCtxt<'_>) -> Box<dyn Any> {
@@ -446,21 +446,25 @@ fn to_gcc_opt_level(optlevel: Option<OptLevel>) -> OptimizationLevel {
446446

447447
/// Returns the features that should be set in `cfg(target_feature)`.
448448
fn target_config(sess: &Session, target_info: &LockedTargetInfo) -> TargetConfig {
449-
let (unstable_target_features, target_features) = cfg_target_feature(sess, |feature| {
450-
// TODO: we disable Neon for now since we don't support the LLVM intrinsics for it.
451-
if feature == "neon" {
452-
return false;
453-
}
454-
target_info.cpu_supports(feature)
455-
// cSpell:disable
456-
/*
457-
adx, aes, avx, avx2, avx512bf16, avx512bitalg, avx512bw, avx512cd, avx512dq, avx512er, avx512f, avx512fp16, avx512ifma,
458-
avx512pf, avx512vbmi, avx512vbmi2, avx512vl, avx512vnni, avx512vp2intersect, avx512vpopcntdq,
459-
bmi1, bmi2, cmpxchg16b, ermsb, f16c, fma, fxsr, gfni, lzcnt, movbe, pclmulqdq, popcnt, rdrand, rdseed, rtm,
460-
sha, sse, sse2, sse3, sse4.1, sse4.2, sse4a, ssse3, tbm, vaes, vpclmulqdq, xsave, xsavec, xsaveopt, xsaves
461-
*/
462-
// cSpell:enable
463-
});
449+
let (unstable_target_features, target_features) = cfg_target_feature(
450+
sess,
451+
|feature| to_gcc_features(sess, feature),
452+
|feature| {
453+
// TODO: we disable Neon for now since we don't support the LLVM intrinsics for it.
454+
if feature == "neon" {
455+
return false;
456+
}
457+
target_info.cpu_supports(feature)
458+
// cSpell:disable
459+
/*
460+
adx, aes, avx, avx2, avx512bf16, avx512bitalg, avx512bw, avx512cd, avx512dq, avx512er, avx512f, avx512fp16, avx512ifma,
461+
avx512pf, avx512vbmi, avx512vbmi2, avx512vl, avx512vnni, avx512vp2intersect, avx512vpopcntdq,
462+
bmi1, bmi2, cmpxchg16b, ermsb, f16c, fma, fxsr, gfni, lzcnt, movbe, pclmulqdq, popcnt, rdrand, rdseed, rtm,
463+
sha, sse, sse2, sse3, sse4.1, sse4.2, sse4a, ssse3, tbm, vaes, vpclmulqdq, xsave, xsavec, xsaveopt, xsaves
464+
*/
465+
// cSpell:enable
466+
},
467+
);
464468

465469
let has_reliable_f16 = target_info.supports_target_dependent_type(CType::Float16);
466470
let has_reliable_f128 = target_info.supports_target_dependent_type(CType::Float128);

compiler/rustc_codegen_llvm/src/back/write.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ pub(crate) fn create_informational_target_machine(
107107
let config = TargetMachineFactoryConfig { split_dwarf_file: None, output_obj_file: None };
108108
// Can't use query system here quite yet because this function is invoked before the query
109109
// system/tcx is set up.
110-
let features = llvm_util::global_llvm_features(sess, false, only_base_features);
110+
let features = llvm_util::global_llvm_features(sess, only_base_features);
111111
target_machine_factory(sess, config::OptLevel::No, &features)(config)
112112
.unwrap_or_else(|err| llvm_err(sess.dcx(), err))
113113
}

compiler/rustc_codegen_llvm/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ impl CodegenBackend for LlvmCodegenBackend {
247247

248248
fn provide(&self, providers: &mut Providers) {
249249
providers.global_backend_features =
250-
|tcx, ()| llvm_util::global_llvm_features(tcx.sess, true, false)
250+
|tcx, ()| llvm_util::global_llvm_features(tcx.sess, false)
251251
}
252252

253253
fn print(&self, req: &PrintRequest, out: &mut String, sess: &Session) {

compiler/rustc_codegen_llvm/src/llvm_util.rs

Lines changed: 43 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -291,26 +291,34 @@ pub(crate) fn to_llvm_features<'a>(sess: &Session, s: &'a str) -> Option<LLVMFea
291291
pub(crate) fn target_config(sess: &Session) -> TargetConfig {
292292
let target_machine = create_informational_target_machine(sess, true);
293293

294-
let (unstable_target_features, target_features) = cfg_target_feature(sess, |feature| {
295-
// This closure determines whether the target CPU has the feature according to LLVM. We do
296-
// *not* consider the `-Ctarget-feature`s here, as that will be handled later in
297-
// `cfg_target_feature`.
298-
if let Some(feat) = to_llvm_features(sess, feature) {
299-
// All the LLVM features this expands to must be enabled.
300-
for llvm_feature in feat {
301-
let cstr = SmallCStr::new(llvm_feature);
302-
// `LLVMRustHasFeature` is moderately expensive. On targets with many
303-
// features (e.g. x86) these calls take a non-trivial fraction of runtime
304-
// when compiling very small programs.
305-
if !unsafe { llvm::LLVMRustHasFeature(target_machine.raw(), cstr.as_ptr()) } {
306-
return false;
294+
let (unstable_target_features, target_features) = cfg_target_feature(
295+
sess,
296+
|feature| {
297+
to_llvm_features(sess, feature)
298+
.map(|f| SmallVec::<[&str; 2]>::from_iter(f.into_iter()))
299+
.unwrap_or_default()
300+
},
301+
|feature| {
302+
// This closure determines whether the target CPU has the feature according to LLVM. We do
303+
// *not* consider the `-Ctarget-feature`s here, as that will be handled later in
304+
// `cfg_target_feature`.
305+
if let Some(feat) = to_llvm_features(sess, feature) {
306+
// All the LLVM features this expands to must be enabled.
307+
for llvm_feature in feat {
308+
let cstr = SmallCStr::new(llvm_feature);
309+
// `LLVMRustHasFeature` is moderately expensive. On targets with many
310+
// features (e.g. x86) these calls take a non-trivial fraction of runtime
311+
// when compiling very small programs.
312+
if !unsafe { llvm::LLVMRustHasFeature(target_machine.raw(), cstr.as_ptr()) } {
313+
return false;
314+
}
307315
}
316+
true
317+
} else {
318+
false
308319
}
309-
true
310-
} else {
311-
false
312-
}
313-
});
320+
},
321+
);
314322

315323
let mut cfg = TargetConfig {
316324
target_features,
@@ -615,11 +623,7 @@ fn llvm_features_by_flags(sess: &Session, features: &mut Vec<String>) {
615623

616624
/// The list of LLVM features computed from CLI flags (`-Ctarget-cpu`, `-Ctarget-feature`,
617625
/// `--target` and similar).
618-
pub(crate) fn global_llvm_features(
619-
sess: &Session,
620-
diagnostics: bool,
621-
only_base_features: bool,
622-
) -> Vec<String> {
626+
pub(crate) fn global_llvm_features(sess: &Session, only_base_features: bool) -> Vec<String> {
623627
// Features that come earlier are overridden by conflicting features later in the string.
624628
// Typically we'll want more explicit settings to override the implicit ones, so:
625629
//
@@ -679,39 +683,27 @@ pub(crate) fn global_llvm_features(
679683

680684
// -Ctarget-features
681685
if !only_base_features {
682-
target_features::flag_to_backend_features(
683-
sess,
684-
diagnostics,
685-
|feature| {
686-
to_llvm_features(sess, feature)
687-
.map(|f| SmallVec::<[&str; 2]>::from_iter(f.into_iter()))
688-
.unwrap_or_default()
689-
},
690-
|feature, enable| {
691-
let enable_disable = if enable { '+' } else { '-' };
692-
// We run through `to_llvm_features` when
693-
// passing requests down to LLVM. This means that all in-language
694-
// features also work on the command line instead of having two
695-
// different names when the LLVM name and the Rust name differ.
696-
let Some(llvm_feature) = to_llvm_features(sess, feature) else { return };
697-
698-
features.extend(
699-
std::iter::once(format!(
700-
"{}{}",
701-
enable_disable, llvm_feature.llvm_feature_name
702-
))
703-
.chain(llvm_feature.dependencies.into_iter().filter_map(
704-
move |feat| match (enable, feat) {
686+
target_features::flag_to_backend_features(sess, |feature, enable| {
687+
let enable_disable = if enable { '+' } else { '-' };
688+
// We run through `to_llvm_features` when
689+
// passing requests down to LLVM. This means that all in-language
690+
// features also work on the command line instead of having two
691+
// different names when the LLVM name and the Rust name differ.
692+
let Some(llvm_feature) = to_llvm_features(sess, feature) else { return };
693+
694+
features.extend(
695+
std::iter::once(format!("{}{}", enable_disable, llvm_feature.llvm_feature_name))
696+
.chain(llvm_feature.dependencies.into_iter().filter_map(move |feat| {
697+
match (enable, feat) {
705698
(_, TargetFeatureFoldStrength::Both(f))
706699
| (true, TargetFeatureFoldStrength::EnableOnly(f)) => {
707700
Some(format!("{enable_disable}{f}"))
708701
}
709702
_ => None,
710-
},
711-
)),
712-
)
713-
},
714-
);
703+
}
704+
})),
705+
)
706+
});
715707
}
716708

717709
// We add this in the "base target" so that these show up in `sess.unstable_target_features`.

0 commit comments

Comments
 (0)