1
Fork 0

Avoid creating SmallVecs in global_llvm_features

This commit is contained in:
SparrowLii 2022-06-06 18:05:07 +08:00
parent c35035cefc
commit b3cd892ae1

View file

@ -218,6 +218,7 @@ pub fn check_tied_features(
sess: &Session, sess: &Session,
features: &FxHashMap<&str, bool>, features: &FxHashMap<&str, bool>,
) -> Option<&'static [&'static str]> { ) -> Option<&'static [&'static str]> {
if !features.is_empty() {
for tied in tied_target_features(sess) { for tied in tied_target_features(sess) {
// Tied features must be set to the same value, or not set at all // Tied features must be set to the same value, or not set at all
let mut tied_iter = tied.iter(); let mut tied_iter = tied.iter();
@ -226,7 +227,8 @@ pub fn check_tied_features(
return Some(tied); return Some(tied);
} }
} }
None }
return None;
} }
// Used to generate cfg variables and apply features // Used to generate cfg variables and apply features
@ -440,6 +442,7 @@ pub(crate) fn global_llvm_features(sess: &Session, diagnostics: bool) -> Vec<Str
// -Ctarget-features // -Ctarget-features
let supported_features = supported_target_features(sess); let supported_features = supported_target_features(sess);
let mut featsmap = FxHashMap::default();
let feats = sess let feats = sess
.opts .opts
.cg .cg
@ -485,35 +488,36 @@ pub(crate) fn global_llvm_features(sess: &Session, diagnostics: bool) -> Vec<Str
} }
diag.emit(); diag.emit();
} }
Some((enable_disable, feature))
})
.collect::<SmallVec<[(char, &str); 8]>>();
if diagnostics { if diagnostics {
// FIXME(nagisa): figure out how to not allocate a full hashset here. // FIXME(nagisa): figure out how to not allocate a full hashset here.
let featmap = feats.iter().map(|&(flag, feat)| (feat, flag == '+')).collect(); featsmap.insert(feature, enable_disable == '+');
if let Some(f) = check_tied_features(sess, &featmap) {
sess.err(&format!(
"target features {} must all be enabled or disabled together",
f.join(", ")
));
}
} }
features.extend(feats.into_iter().flat_map(|(enable_disable, feature)| {
// rustc-specific features do not get passed down to LLVM… // rustc-specific features do not get passed down to LLVM…
if RUSTC_SPECIFIC_FEATURES.contains(&feature) { if RUSTC_SPECIFIC_FEATURES.contains(&feature) {
return SmallVec::<[_; 2]>::new(); return None;
} }
// ... otherwise though we run through `to_llvm_features` when // ... otherwise though we run through `to_llvm_features` when
// passing requests down to LLVM. This means that all in-language // passing requests down to LLVM. This means that all in-language
// features also work on the command line instead of having two // features also work on the command line instead of having two
// different names when the LLVM name and the Rust name differ. // different names when the LLVM name and the Rust name differ.
Some(
to_llvm_features(sess, feature) to_llvm_features(sess, feature)
.into_iter() .into_iter()
.map(|f| format!("{}{}", enable_disable, f)) .map(move |f| format!("{}{}", enable_disable, f)),
.collect() )
})); })
.flatten();
features.extend(feats);
if diagnostics && let Some(f) = check_tied_features(sess, &featsmap) {
sess.err(&format!(
"target features {} must all be enabled or disabled together",
f.join(", ")
));
}
features features
} }