Skip to content

Commit 5c845c6

Browse files
Rollup merge of #163143 - Zalathar:check-features, r=davidtwco
cg_llvm: Use fewer FFI calls to check the target CPU's features The existing code makes a separate FFI call to `MCSubtargetInfo::checkFeatures` for each feature-dependency of the feature being checked, and also performs string-manipulation on the C++ side to add a leading `+` to each feature name. What we can do instead is prepare a single string on the Rust side in the form `"+foo,+bar,+baz,"` for each Rust target feature, and pass that to LLVM as a pointer/length string. LLVM already knows how to check that all of the listed features are present. This change makes the code simpler overall. All of the string manipulation now takes place either in Rust code or in LLVM itself, and not in our C++ wrapper. There should be no change to compiler output.
2 parents 2136250 + 733daee commit 5c845c6

4 files changed

Lines changed: 23 additions & 20 deletions

File tree

‎compiler/rustc_codegen_llvm/src/back/owned_mc_subtarget_info.rs‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::ptr::NonNull;
44
use rustc_data_structures::small_c_str::SmallCStr;
55

66
use crate::diagnostics::LlvmError;
7-
use crate::llvm;
7+
use crate::{llvm, llvm_util};
88

99
/// Responsible for safely creating and disposing llvm::MCSubtargetInfo via ffi functions.
1010
/// Not cloneable as there is no clone function for llvm::MCSubtargetInfo.
@@ -28,11 +28,20 @@ impl OwnedMCSubtargetInfo {
2828
.ok_or_else(|| LlvmError::CreateMCSubtargetInfo { triple: SmallCStr::from(triple) })
2929
}
3030

31-
pub(crate) fn has_feature(&self, feature: &CStr) -> bool {
32-
// SAFETY: `new` ensures we have a valid pointer created by
31+
pub(crate) fn has_features(&self, features: llvm_util::LLVMFeature<'_>) -> bool {
32+
// Convert to a feature string expected by LLVM's `SubtargetFeatures`.
33+
// All of the required LLVM features must be enabled.
34+
// (The trailing comma is OK, as `SubtargetFeatures::Split` discards empty substrings.)
35+
let features = features.into_iter().flat_map(|feat| ["+", feat, ","]).collect::<String>();
36+
37+
// SAFETY: `info_unique` is a valid pointer created by
3338
// `llvm::LLVMRustCreateMCSubtargetInfo`.
3439
unsafe {
35-
llvm::LLVMRustMCSubtargetInfoHasFeature(self.info_unique.as_ref(), feature.as_ptr())
40+
llvm::LLVMRustMCSubtargetInfoCheckFeatures(
41+
self.info_unique.as_ref(),
42+
features.as_ptr(),
43+
features.len(),
44+
)
3645
}
3746
}
3847
}

‎compiler/rustc_codegen_llvm/src/llvm/ffi.rs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2422,9 +2422,10 @@ unsafe extern "C" {
24222422
Features: *const c_char,
24232423
) -> *mut MCSubtargetInfo;
24242424

2425-
pub(crate) fn LLVMRustMCSubtargetInfoHasFeature(
2425+
pub(crate) fn LLVMRustMCSubtargetInfoCheckFeatures(
24262426
MCInfo: &MCSubtargetInfo,
2427-
Feature: *const c_char,
2427+
Features: *const c_uchar, // See "PTR_LEN_STR".
2428+
FeaturesLen: usize,
24282429
) -> bool;
24292430

24302431
pub(crate) fn LLVMRustDisposeMCSubtargetInfo(MCInfo: ptr::NonNull<MCSubtargetInfo>);

‎compiler/rustc_codegen_llvm/src/llvm_util.rs‎

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -361,17 +361,9 @@ pub(crate) fn target_config(sess: &EarlySession) -> TargetConfig {
361361
// do *not* consider the `-Ctarget-feature`s here (that's why we passed `for_cfg: true`
362362
// to `global_llvm_features` above) because that will be handled later in
363363
// `internal_target_features`.
364-
if let Some(feat) = to_llvm_features(&sess.target, feature) {
365-
// All the LLVM features this expands to must be enabled.
366-
for llvm_feature in feat {
367-
let cstr = SmallCStr::new(llvm_feature);
368-
// `has_feature` is moderately expensive. On targets with many
369-
// features (e.g. x86) these calls take a non-trivial fraction of runtime
370-
// when compiling very small programs.
371-
if !mc_subtarget_info.has_feature(&cstr) {
372-
return false;
373-
}
374-
}
364+
if let Some(feat) = to_llvm_features(&sess.target, feature)
365+
&& mc_subtarget_info.has_features(feat)
366+
{
375367
true
376368
} else {
377369
false

‎compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,10 @@ LLVMRustCreateMCSubtargetInfo(const char *TripleStr, const char *CPU,
107107
#endif
108108
}
109109

110-
extern "C" bool LLVMRustMCSubtargetInfoHasFeature(MCSubtargetInfo *MCInfo,
111-
const char *Feature) {
112-
return MCInfo->checkFeatures(std::string("+") + Feature);
110+
extern "C" bool LLVMRustMCSubtargetInfoCheckFeatures(MCSubtargetInfo *MCInfo,
111+
const char *Features,
112+
size_t FeaturesLen) {
113+
return MCInfo->checkFeatures(StringRef{Features, FeaturesLen});
113114
}
114115

115116
extern "C" void LLVMRustDisposeMCSubtargetInfo(MCSubtargetInfo *MCInfo) {

0 commit comments

Comments
 (0)