Skip to content

Commit 095d818

Browse files
committed
Always include global target features in function attributes
This ensures that information about target features configured with `-C target-feature=...` or detected with `-C target-cpu=native` is retained for subsequent consumers of LLVM bitcode. This is crucial for linker plugin LTO, since this information is not conveyed to the plugin otherwise.
1 parent b6f845f commit 095d818

File tree

2 files changed

+9
-10
lines changed

2 files changed

+9
-10
lines changed

compiler/rustc_codegen_llvm/src/attributes.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -378,13 +378,12 @@ pub fn from_fn_attrs<'ll, 'tcx>(
378378
}
379379
}
380380

381-
if !function_features.is_empty() {
382-
let global_features = cx.tcx.global_backend_features(()).iter().map(|s| &s[..]);
383-
let val = global_features
384-
.chain(function_features.iter().map(|s| &s[..]))
385-
.intersperse(",")
386-
.collect::<SmallStr<1024>>();
387-
to_add.push(llvm::CreateAttrStringValue(cx.llcx, "target-features", &val));
381+
let global_features = cx.tcx.global_backend_features(()).iter().map(|s| s.as_str());
382+
let function_features = function_features.iter().map(|s| s.as_str());
383+
let target_features =
384+
global_features.chain(function_features).intersperse(",").collect::<SmallStr<1024>>();
385+
if !target_features.is_empty() {
386+
to_add.push(llvm::CreateAttrStringValue(cx.llcx, "target-features", &target_features));
388387
}
389388

390389
attributes::apply_to_llfn(llfn, Function, &to_add);

src/test/codegen/target-feature-overrides.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub unsafe fn apple() -> u32 {
2929
peach()
3030
}
3131

32-
// target features same as global (not reflected or overriden in IR)
32+
// target features same as global
3333
#[no_mangle]
3434
pub unsafe fn banana() -> u32 {
3535
// CHECK-LABEL: @banana()
@@ -43,5 +43,5 @@ pub unsafe fn banana() -> u32 {
4343
// COMPAT-SAME: "target-features"="+avx2,+avx,+avx"
4444
// INCOMPAT-SAME: "target-features"="-avx2,-avx,+avx"
4545
// CHECK: attributes [[BANANAATTRS]]
46-
// CHECK-NOT: target-features
47-
// CHECK-SAME: }
46+
// COMPAT-SAME: "target-features"="+avx2,+avx"
47+
// INCOMPAT-SAME: "target-features"="-avx2,-avx"

0 commit comments

Comments
 (0)