Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions compiler/rustc_codegen_llvm/src/back/owned_mc_subtarget_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::ptr::NonNull;
use rustc_data_structures::small_c_str::SmallCStr;

use crate::diagnostics::LlvmError;
use crate::llvm;
use crate::{llvm, llvm_util};

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

pub(crate) fn has_feature(&self, feature: &CStr) -> bool {
// SAFETY: `new` ensures we have a valid pointer created by
pub(crate) fn has_features(&self, features: llvm_util::LLVMFeature<'_>) -> bool {
// Convert to a feature string expected by LLVM's `SubtargetFeatures`.
// All of the required LLVM features must be enabled.
// (The trailing comma is OK, as `SubtargetFeatures::Split` discards empty substrings.)
let features = features.into_iter().flat_map(|feat| ["+", feat, ","]).collect::<String>();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume there's no issues with the trailing comma here at the end?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the split-on-commas in LLVM's SubtargetFeatures::Split sets KeepEmpty=false, so the empty segment after the trailing comma is safely discarded.

I added a comment to mention this.


// SAFETY: `info_unique` is a valid pointer created by
// `llvm::LLVMRustCreateMCSubtargetInfo`.
unsafe {
llvm::LLVMRustMCSubtargetInfoHasFeature(self.info_unique.as_ref(), feature.as_ptr())
llvm::LLVMRustMCSubtargetInfoCheckFeatures(
self.info_unique.as_ref(),
features.as_ptr(),
features.len(),
)
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions compiler/rustc_codegen_llvm/src/llvm/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2422,9 +2422,10 @@ unsafe extern "C" {
Features: *const c_char,
) -> *mut MCSubtargetInfo;

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

pub(crate) fn LLVMRustDisposeMCSubtargetInfo(MCInfo: ptr::NonNull<MCSubtargetInfo>);
Expand Down
14 changes: 3 additions & 11 deletions compiler/rustc_codegen_llvm/src/llvm_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,17 +361,9 @@ pub(crate) fn target_config(sess: &EarlySession) -> TargetConfig {
// do *not* consider the `-Ctarget-feature`s here (that's why we passed `for_cfg: true`
// to `global_llvm_features` above) because that will be handled later in
// `internal_target_features`.
if let Some(feat) = to_llvm_features(&sess.target, feature) {
// All the LLVM features this expands to must be enabled.
for llvm_feature in feat {
let cstr = SmallCStr::new(llvm_feature);
// `has_feature` 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.
Comment on lines -368 to -370

@Zalathar Zalathar Sep 22, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this comment was made obsolete by llvm/llvm-project#130936, which is included in LLVM 21.

View changes since the review

if !mc_subtarget_info.has_feature(&cstr) {
return false;
}
}
if let Some(feat) = to_llvm_features(&sess.target, feature)
&& mc_subtarget_info.has_features(feat)
{
true
} else {
false
Expand Down
7 changes: 4 additions & 3 deletions compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,10 @@ LLVMRustCreateMCSubtargetInfo(const char *TripleStr, const char *CPU,
#endif
}

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

extern "C" void LLVMRustDisposeMCSubtargetInfo(MCSubtargetInfo *MCInfo) {
Expand Down
Loading