Skip to content

Commit 1d59f66

Browse files
committed
Auto merge of #156849 - saethlin:test-call-site-inline-attributes, r=dianqk
Re-add call site inlining attributes It is very odd that we are blindly setting the call site attributes to the same as the definition, because I think the point of the call site attributes is to differentiate between calls. Probably worth looking into later, but for now I think we should just be undoing the change we made by mistake. This fixes the accidental regression from #156242
2 parents a06c1ca + adbf6f5 commit 1d59f66

3 files changed

Lines changed: 71 additions & 14 deletions

File tree

compiler/rustc_codegen_llvm/src/attributes.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_hir::find_attr;
55
use rustc_middle::middle::codegen_fn_attrs::{
66
CodegenFnAttrFlags, CodegenFnAttrs, PatchableFunctionEntry, SanitizerFnAttrs, TargetFeature,
77
};
8-
use rustc_middle::ty::{self, TyCtxt};
8+
use rustc_middle::ty::{self, Instance, TyCtxt};
99
use rustc_session::config::{BranchProtection, FunctionReturn, OptLevel, PAuthKey, PacRet};
1010
use rustc_span::sym;
1111
use rustc_symbol_mangling::mangle_internal_symbol;
@@ -42,22 +42,31 @@ pub(crate) fn remove_string_attr_from_llfn(llfn: &Value, name: &str) {
4242

4343
/// Get LLVM attribute for the provided inline heuristic.
4444
#[inline]
45-
fn inline_attr<'ll>(
45+
pub(crate) fn inline_attr<'tcx, 'll>(
4646
cx: &SimpleCx<'ll>,
47-
sess: &Session,
48-
inline: InlineAttr,
47+
tcx: TyCtxt<'tcx>,
48+
instance: Instance<'tcx>,
49+
codegen_fn_attrs: &CodegenFnAttrs,
4950
) -> Option<&'ll Attribute> {
50-
if !sess.opts.unstable_opts.inline_llvm {
51+
if !tcx.sess.opts.unstable_opts.inline_llvm {
5152
// disable LLVM inlining
5253
return Some(AttributeKind::NoInline.create_attr(cx.llcx));
5354
}
55+
56+
// `optnone` requires `noinline`
57+
let inline = match (codegen_fn_attrs.inline, &codegen_fn_attrs.optimize) {
58+
(_, OptimizeAttr::DoNotOptimize) => InlineAttr::Never,
59+
(InlineAttr::None, _) if instance.def.requires_inline(tcx) => InlineAttr::Hint,
60+
(inline, _) => inline,
61+
};
62+
5463
match inline {
5564
InlineAttr::Hint => Some(AttributeKind::InlineHint.create_attr(cx.llcx)),
5665
InlineAttr::Always | InlineAttr::Force { .. } => {
5766
Some(AttributeKind::AlwaysInline.create_attr(cx.llcx))
5867
}
5968
InlineAttr::Never => {
60-
if sess.target.arch != Arch::AmdGpu {
69+
if tcx.sess.target.arch != Arch::AmdGpu {
6170
Some(AttributeKind::NoInline.create_attr(cx.llcx))
6271
} else {
6372
None
@@ -412,14 +421,7 @@ pub(crate) fn llfn_attrs_from_instance<'ll, 'tcx>(
412421
}
413422

414423
if let Some(instance) = instance {
415-
// `optnone` requires `noinline`
416-
let inline = match (codegen_fn_attrs.inline, &codegen_fn_attrs.optimize) {
417-
(_, OptimizeAttr::DoNotOptimize) => InlineAttr::Never,
418-
(InlineAttr::None, _) if instance.def.requires_inline(tcx) => InlineAttr::Hint,
419-
(inline, _) => inline,
420-
};
421-
422-
to_add.extend(inline_attr(cx, sess, inline));
424+
to_add.extend(inline_attr(cx, tcx, instance, codegen_fn_attrs));
423425
}
424426

425427
if sess.must_emit_unwind_tables() {

compiler/rustc_codegen_llvm/src/builder.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1419,6 +1419,21 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
14191419
)
14201420
};
14211421

1422+
if let Some(callee_instance) = callee_instance {
1423+
// Attributes on the function definition being called
1424+
let callee_attrs = self.cx.tcx.codegen_fn_attrs(callee_instance.def_id());
1425+
1426+
if let Some(inlining_rule) =
1427+
attributes::inline_attr(&self.cx, self.cx.tcx, callee_instance, callee_attrs)
1428+
{
1429+
attributes::apply_to_callsite(
1430+
call,
1431+
llvm::AttributePlace::Function,
1432+
&[inlining_rule],
1433+
);
1434+
}
1435+
}
1436+
14221437
if let Some(fn_abi) = fn_abi {
14231438
fn_abi.apply_attrs_callsite(self, call);
14241439
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//@ compile-flags: -O -Zinline-mir=no -Cno-prepopulate-passes -Zmerge-functions=disabled
2+
3+
#![crate_type = "lib"]
4+
5+
// This test checks that we add inlinehint for #[inline], noinline for #[inline(never)], and
6+
// alwaysinline for #[inline(always)] to call sites.
7+
8+
#[unsafe(no_mangle)]
9+
fn calls_something_noinline() {
10+
// CHECK-LABEL @calls_something_noinline
11+
// CHECK: call void @{{.*}}noinline_fn() #[[NOINLINE:[0-9]+]]
12+
noinline_fn();
13+
}
14+
15+
#[inline(never)]
16+
fn noinline_fn() {}
17+
18+
#[unsafe(no_mangle)]
19+
fn calls_something_inline() {
20+
// CHECK-LABEL @calls_something_inlinehint
21+
// CHECK: call void @{{.*}}inlinehint_fn() #[[INLINEHINT:[0-9]+]]
22+
inlinehint_fn();
23+
}
24+
25+
#[inline]
26+
fn inlinehint_fn() {}
27+
28+
#[unsafe(no_mangle)]
29+
fn calls_something_alwaysinline() {
30+
// CHECK-LABEL @calls_something_alwaysinline
31+
// CHECK: call void @{{.*}}alwaysinline_fn() #[[ALWAYSINLINE:[0-9]+]]
32+
alwaysinline_fn();
33+
}
34+
35+
#[inline(always)]
36+
fn alwaysinline_fn() {}
37+
38+
//CHECK: attributes #[[NOINLINE]] = {{{.*}} noinline {{.*}}}
39+
//CHECK: attributes #[[INLINEHINT]] = {{{.*}} inlinehint {{.*}}}
40+
//CHECK: attributes #[[ALWAYSINLINE]] = {{{.*}} alwaysinline {{.*}}}

0 commit comments

Comments
 (0)