Skip to content

Commit c139cc5

Browse files
committed
rustc_codegen_llvm: properly passing backchain attribute to LLVM ...
... this is a special attribute that was made to be a target-feature in LLVM 18+, but in all previous versions, this "feature" is a naked attribute. We will have to handle this situation differently than all other target-features.
1 parent abdac0d commit c139cc5

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

compiler/rustc_codegen_llvm/src/attributes.rs

+14
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,17 @@ fn stackprotector_attr<'ll>(cx: &CodegenCx<'ll, '_>) -> Option<&'ll Attribute> {
272272
Some(sspattr.create_attr(cx.llcx))
273273
}
274274

275+
fn backchain_attr<'ll>(cx: &CodegenCx<'ll, '_>) -> Option<&'ll Attribute> {
276+
if cx.sess().target.arch != "s390x" {
277+
return None;
278+
}
279+
280+
let requested_features = cx.sess().opts.cg.target_feature.split(',');
281+
let found_positive = requested_features.clone().any(|r| r == "+backchain");
282+
283+
if found_positive { Some(llvm::CreateAttrString(cx.llcx, "backchain")) } else { None }
284+
}
285+
275286
pub fn target_cpu_attr<'ll>(cx: &CodegenCx<'ll, '_>) -> &'ll Attribute {
276287
let target_cpu = llvm_util::target_cpu(cx.tcx.sess);
277288
llvm::CreateAttrStringValue(cx.llcx, "target-cpu", target_cpu)
@@ -448,6 +459,9 @@ pub fn from_fn_attrs<'ll, 'tcx>(
448459
if let Some(align) = codegen_fn_attrs.alignment {
449460
llvm::set_alignment(llfn, align);
450461
}
462+
if let Some(backchain) = backchain_attr(cx) {
463+
to_add.push(backchain);
464+
}
451465
to_add.extend(sanitize_attrs(cx, codegen_fn_attrs.no_sanitize));
452466
to_add.extend(patchable_function_entry_attrs(cx, codegen_fn_attrs.patchable_function_entry));
453467

compiler/rustc_codegen_llvm/src/llvm_util.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_session::config::{PrintKind, PrintRequest};
1414
use rustc_session::Session;
1515
use rustc_span::symbol::Symbol;
1616
use rustc_target::spec::{MergeFunctions, PanicStrategy};
17-
use rustc_target::target_features::RUSTC_SPECIFIC_FEATURES;
17+
use rustc_target::target_features::{RUSTC_SPECIAL_FEATURES, RUSTC_SPECIFIC_FEATURES};
1818

1919
use std::ffi::{c_char, c_void, CStr, CString};
2020
use std::fmt::Write;
@@ -317,6 +317,10 @@ pub fn target_features(sess: &Session, allow_unstable: bool) -> Vec<Symbol> {
317317
}
318318
})
319319
.filter(|feature| {
320+
// skip checking special features, as LLVM may not understands them
321+
if RUSTC_SPECIAL_FEATURES.contains(feature) {
322+
return true;
323+
}
320324
// check that all features in a given smallvec are enabled
321325
for llvm_feature in to_llvm_features(sess, feature) {
322326
let cstr = SmallCStr::new(llvm_feature);
@@ -542,6 +546,7 @@ pub(crate) fn global_llvm_features(sess: &Session, diagnostics: bool) -> Vec<Str
542546

543547
// -Ctarget-features
544548
let supported_features = sess.target.supported_target_features();
549+
let (llvm_major, _, _) = get_version();
545550
let mut featsmap = FxHashMap::default();
546551
let feats = sess
547552
.opts
@@ -600,6 +605,13 @@ pub(crate) fn global_llvm_features(sess: &Session, diagnostics: bool) -> Vec<Str
600605
if RUSTC_SPECIFIC_FEATURES.contains(&feature) {
601606
return None;
602607
}
608+
609+
// if the target-feature is "backchain" and LLVM version is greater than 18
610+
// then we also need to add "+backchain" to the target-features attribute.
611+
// otherwise, we will only add the naked `backchain` attribute to the attribute-group.
612+
if feature == "backchain" && llvm_major < 18 {
613+
return None;
614+
}
603615
// ... otherwise though we run through `to_llvm_features` when
604616
// passing requests down to LLVM. This means that all in-language
605617
// features also work on the command line instead of having two

compiler/rustc_target/src/target_features.rs

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ use rustc_span::symbol::Symbol;
44
/// Features that control behaviour of rustc, rather than the codegen.
55
pub const RUSTC_SPECIFIC_FEATURES: &[&str] = &["crt-static"];
66

7+
/// Features that require special handling when passing to LLVM.
8+
pub const RUSTC_SPECIAL_FEATURES: &[&str] = &["backchain"];
9+
710
/// Stability information for target features.
811
#[derive(Debug, Clone, Copy)]
912
pub enum Stability {

0 commit comments

Comments
 (0)