Skip to content

Commit d9ed836

Browse files
committed
Fix issue with callsite inline attribute not being applied sometimes.
If the calling function had more target features enabled than the callee than the attribute wasn't being applied as the arguments for the check had been swapped round. Also includes target features that are part of the global set as the warning was checking those but when adding the attribute they were not checked. Add a codegen-llvm test to check that the attribute is actually applied as previously only the warning was being checked.
1 parent 95b6747 commit d9ed836

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

compiler/rustc_codegen_llvm/src/builder.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use rustc_codegen_ssa::mir::place::PlaceRef;
1616
use rustc_codegen_ssa::traits::*;
1717
use rustc_data_structures::small_c_str::SmallCStr;
1818
use rustc_hir::def_id::DefId;
19-
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs;
19+
use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrs, TargetFeature, TargetFeatureKind};
2020
use rustc_middle::ty::layout::{
2121
FnAbiError, FnAbiOfHelpers, FnAbiRequest, HasTypingEnv, LayoutError, LayoutOfHelpers,
2222
TyAndLayout,
@@ -1405,14 +1405,18 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
14051405
// Attributes on the function definition being called
14061406
let fn_defn_attrs = self.cx.tcx.codegen_fn_attrs(instance.def_id());
14071407
if let Some(fn_call_attrs) = fn_call_attrs
1408-
&& !fn_call_attrs.target_features.is_empty()
14091408
// If there is an inline attribute and a target feature that matches
14101409
// we will add the attribute to the callsite otherwise we'll omit
14111410
// this and not add the attribute to prevent soundness issues.
14121411
&& let Some(inlining_rule) = attributes::inline_attr(&self.cx, self.cx.tcx, instance)
14131412
&& self.cx.tcx.is_target_feature_call_safe(
1414-
&fn_call_attrs.target_features,
14151413
&fn_defn_attrs.target_features,
1414+
&fn_call_attrs.target_features.iter().cloned().chain(
1415+
self.cx.tcx.sess.target_features.iter().map(|feat| TargetFeature {
1416+
name: *feat,
1417+
kind: TargetFeatureKind::Implied,
1418+
})
1419+
).collect::<Vec<_>>(),
14161420
)
14171421
{
14181422
attributes::apply_to_callsite(

compiler/rustc_middle/src/ty/context.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2090,6 +2090,8 @@ impl<'tcx> TyCtxt<'tcx> {
20902090
self.sess.dcx()
20912091
}
20922092

2093+
/// Checks to see if the caller (`body_features`) has all the features required by the callee
2094+
/// (`callee_features`).
20932095
pub fn is_target_feature_call_safe(
20942096
self,
20952097
callee_features: &[TargetFeature],

tests/codegen-llvm/inline-always-callsite.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//@ add-core-stubs
1+
//@ add-minicore
22
//@ compile-flags: --target aarch64-unknown-linux-gnu -Zinline-mir=no -C no-prepopulate-passes
33
//@ needs-llvm-components: aarch64
44

@@ -19,19 +19,23 @@ pub fn single_target_feature() -> i32 {
1919
#[inline(always)]
2020
#[target_feature(enable = "neon,i8mm")]
2121
#[no_mangle]
22-
// CHECK: define noundef i32 @multiple_target_features() unnamed_addr #1 {
22+
// CHECK: define{{( noundef)?}} i32 @multiple_target_features() unnamed_addr #1 {
2323
pub fn multiple_target_features() -> i32 {
24-
// CHECK: %_0 = call noundef i32 @single_target_feature() #3
24+
// CHECK: %_0 = call{{( noundef)?}} i32 @single_target_feature() #3
2525
single_target_feature()
2626
}
2727

2828
#[no_mangle]
29-
// CHECK: define noundef i32 @inherits_from_global() unnamed_addr #2 {
29+
// CHECK: define{{( noundef)?}} i32 @inherits_from_global() unnamed_addr #2 {
3030
pub fn inherits_from_global() -> i32 {
3131
unsafe {
32-
// CHECK: %_0 = call noundef i32 @single_target_feature() #3
32+
// CHECK: %_0 = call{{( noundef)?}} i32 @single_target_feature() #3
3333
single_target_feature()
3434
}
3535
}
3636

37-
// CHECK: attributes #3 = { nounwind }
37+
// Attribute #3 requires the alwaysinline attribute, the alwaysinline attribute is not emitted on a
38+
// function definition when target features are present, rather it will be moved onto the function
39+
// call, if the features match up.
40+
//
41+
// CHECK: attributes #3 = { alwaysinline nounwind }

0 commit comments

Comments
 (0)