Skip to content

Commit 675f114

Browse files
committed
Auto merge of #79106 - tmiasko:inline-hint, r=nagisa,eddyb
Fix setting inline hint based on `InstanceDef::requires_inline` For instances where `InstanceDef::requires_inline` is true, an attempt is made to set an inline hint though a call to the `inline` function. The attempt is ineffective, since all attributes will be usually removed by the second call. Fix the issue by applying the attributes only once, with user provided attributes having a priority when provided. Closes #79108.
2 parents 8256379 + 4ea25da commit 675f114

File tree

2 files changed

+35
-12
lines changed

2 files changed

+35
-12
lines changed

compiler/rustc_codegen_llvm/src/attributes.rs

+4-12
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use crate::value::Value;
2525

2626
/// Mark LLVM function to use provided inline heuristic.
2727
#[inline]
28-
fn inline(cx: &CodegenCx<'ll, '_>, val: &'ll Value, inline: InlineAttr) {
28+
fn inline(cx: &CodegenCx<'ll, '_>, val: &'ll Value, inline: InlineAttr, requires_inline: bool) {
2929
use self::InlineAttr::*;
3030
match inline {
3131
Hint => Attribute::InlineHint.apply_llfn(Function, val),
@@ -35,11 +35,8 @@ fn inline(cx: &CodegenCx<'ll, '_>, val: &'ll Value, inline: InlineAttr) {
3535
Attribute::NoInline.apply_llfn(Function, val);
3636
}
3737
}
38-
None => {
39-
Attribute::InlineHint.unapply_llfn(Function, val);
40-
Attribute::AlwaysInline.unapply_llfn(Function, val);
41-
Attribute::NoInline.unapply_llfn(Function, val);
42-
}
38+
None if requires_inline => Attribute::InlineHint.apply_llfn(Function, val),
39+
None => {}
4340
};
4441
}
4542

@@ -229,12 +226,7 @@ pub fn from_fn_attrs(cx: &CodegenCx<'ll, 'tcx>, llfn: &'ll Value, instance: ty::
229226
}
230227
}
231228

232-
// FIXME(eddyb) consolidate these two `inline` calls (and avoid overwrites).
233-
if instance.def.requires_inline(cx.tcx) {
234-
inline(cx, llfn, attributes::InlineAttr::Hint);
235-
}
236-
237-
inline(cx, llfn, codegen_fn_attrs.inline.clone());
229+
inline(cx, llfn, codegen_fn_attrs.inline.clone(), instance.def.requires_inline(cx.tcx));
238230

239231
// The `uwtable` attribute according to LLVM is:
240232
//

src/test/codegen/inline-hint.rs

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Checks that closures, constructors, and shims except
2+
// for a drop glue receive inline hint by default.
3+
//
4+
// compile-flags: -Cno-prepopulate-passes -Zsymbol-mangling-version=v0
5+
#![crate_type = "lib"]
6+
7+
pub fn f() {
8+
let a = A;
9+
let b = (0i32, 1i32, 2i32, 3i32);
10+
let c = || {};
11+
12+
a(String::new(), String::new());
13+
b.clone();
14+
c();
15+
}
16+
17+
struct A(String, String);
18+
19+
// CHECK: ; core::ptr::drop_in_place::<inline_hint::A>
20+
// CHECK-NEXT: ; Function Attrs:
21+
// CHECK-NOT: inlinehint
22+
// CHECK-SAME: {{$}}
23+
24+
// CHECK: ; <(i32, i32, i32, i32) as core::clone::Clone>::clone
25+
// CHECK-NEXT: ; Function Attrs: inlinehint
26+
27+
// CHECK: ; inline_hint::f::{closure#0}
28+
// CHECK-NEXT: ; Function Attrs: inlinehint
29+
30+
// CHECK: ; inline_hint::A
31+
// CHECK-NEXT: ; Function Attrs: inlinehint

0 commit comments

Comments
 (0)