cg_gcc: properly populate cfg(target_features) with -Ctarget-features

This commit is contained in:
Ralf Jung
2025-05-11 12:50:48 +02:00
parent e46c234ca4
commit 8bec5bb5ad
3 changed files with 37 additions and 47 deletions

View File

@@ -7,6 +7,7 @@ use std::{ptr, slice, str};
use libc::c_int;
use rustc_codegen_ssa::base::wants_wasm_eh;
use rustc_codegen_ssa::target_features::cfg_target_feature;
use rustc_codegen_ssa::{TargetConfig, target_features};
use rustc_data_structures::fx::FxHashSet;
use rustc_data_structures::small_c_str::SmallCStr;
@@ -331,24 +332,23 @@ pub(crate) fn target_config(sess: &Session) -> TargetConfig {
// by LLVM.
let target_machine = create_informational_target_machine(sess, true);
let (unstable_target_features, target_features) =
target_features::cfg_target_feature(sess, &sess.opts.cg.target_feature, |feature| {
if let Some(feat) = to_llvm_features(sess, feature) {
// All the LLVM features this expands to must be enabled.
for llvm_feature in feat {
let cstr = SmallCStr::new(llvm_feature);
// `LLVMRustHasFeature` is moderately expensive. On targets with many
// features (e.g. x86) these calls take a non-trivial fraction of runtime
// when compiling very small programs.
if !unsafe { llvm::LLVMRustHasFeature(target_machine.raw(), cstr.as_ptr()) } {
return false;
}
let (unstable_target_features, target_features) = cfg_target_feature(sess, |feature| {
if let Some(feat) = to_llvm_features(sess, feature) {
// All the LLVM features this expands to must be enabled.
for llvm_feature in feat {
let cstr = SmallCStr::new(llvm_feature);
// `LLVMRustHasFeature` is moderately expensive. On targets with many
// features (e.g. x86) these calls take a non-trivial fraction of runtime
// when compiling very small programs.
if !unsafe { llvm::LLVMRustHasFeature(target_machine.raw(), cstr.as_ptr()) } {
return false;
}
true
} else {
false
}
});
true
} else {
false
}
});
let mut cfg = TargetConfig {
target_features,