Skip to content

Commit 31ee8b1

Browse files
erikdesjardinsInnovativeInventornikic
committed
Reapply: Mark drop calls in landing pads cold instead of noinline
Co-authored-by: Max Fan <[email protected]> Co-authored-by: Nikita Popov <[email protected]>
1 parent 1578329 commit 31ee8b1

File tree

7 files changed

+85
-12
lines changed

7 files changed

+85
-12
lines changed

Diff for: compiler/rustc_codegen_gcc/src/builder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1420,7 +1420,7 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
14201420
self.cx
14211421
}
14221422

1423-
fn do_not_inline(&mut self, _llret: RValue<'gcc>) {
1423+
fn apply_attrs_to_cleanup_callsite(&mut self, _llret: RValue<'gcc>) {
14241424
// FIXME(bjorn3): implement
14251425
}
14261426

Diff for: compiler/rustc_codegen_llvm/src/builder.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -1225,9 +1225,11 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
12251225
unsafe { llvm::LLVMBuildZExt(self.llbuilder, val, dest_ty, UNNAMED) }
12261226
}
12271227

1228-
fn do_not_inline(&mut self, llret: &'ll Value) {
1229-
let noinline = llvm::AttributeKind::NoInline.create_attr(self.llcx);
1230-
attributes::apply_to_callsite(llret, llvm::AttributePlace::Function, &[noinline]);
1228+
fn apply_attrs_to_cleanup_callsite(&mut self, llret: &'ll Value) {
1229+
// Cleanup is always the cold path.
1230+
let cold_inline = llvm::AttributeKind::Cold.create_attr(self.llcx);
1231+
1232+
attributes::apply_to_callsite(llret, llvm::AttributePlace::Function, &[cold_inline]);
12311233
}
12321234
}
12331235

Diff for: compiler/rustc_codegen_ssa/src/mir/block.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ impl<'a, 'tcx> TerminatorCodegenHelper<'tcx> {
213213
self.funclet(fx),
214214
);
215215
if fx.mir[self.bb].is_cleanup {
216-
bx.do_not_inline(invokeret);
216+
bx.apply_attrs_to_cleanup_callsite(invokeret);
217217
}
218218

219219
if let Some((ret_dest, target)) = destination {
@@ -228,11 +228,7 @@ impl<'a, 'tcx> TerminatorCodegenHelper<'tcx> {
228228
} else {
229229
let llret = bx.call(fn_ty, fn_attrs, Some(&fn_abi), fn_ptr, &llargs, self.funclet(fx));
230230
if fx.mir[self.bb].is_cleanup {
231-
// Cleanup is always the cold path. Don't inline
232-
// drop glue. Also, when there is a deeply-nested
233-
// struct, there are "symmetry" issues that cause
234-
// exponential inlining - see issue #41696.
235-
bx.do_not_inline(llret);
231+
bx.apply_attrs_to_cleanup_callsite(llret);
236232
}
237233

238234
if let Some((ret_dest, target)) = destination {
@@ -1627,7 +1623,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
16271623
let fn_ty = bx.fn_decl_backend_type(&fn_abi);
16281624

16291625
let llret = bx.call(fn_ty, None, Some(&fn_abi), fn_ptr, &[], funclet.as_ref());
1630-
bx.do_not_inline(llret);
1626+
bx.apply_attrs_to_cleanup_callsite(llret);
16311627

16321628
bx.unreachable();
16331629

Diff for: compiler/rustc_codegen_ssa/src/traits/builder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -332,5 +332,5 @@ pub trait BuilderMethods<'a, 'tcx>:
332332
) -> Self::Value;
333333
fn zext(&mut self, val: Self::Value, dest_ty: Self::Type) -> Self::Value;
334334

335-
fn do_not_inline(&mut self, llret: Self::Value);
335+
fn apply_attrs_to_cleanup_callsite(&mut self, llret: Self::Value);
336336
}

Diff for: tests/codegen/issue-97217.rs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// compile-flags: -C opt-level=3
2+
// ignore-debug: the debug assertions get in the way
3+
#![crate_type = "lib"]
4+
5+
// Regression test for issue 97217 (the following should result in no allocations)
6+
7+
// CHECK-LABEL: @issue97217
8+
#[no_mangle]
9+
pub fn issue97217() -> i32 {
10+
// drop_in_place should be inlined and never appear
11+
// CHECK-NOT: drop_in_place
12+
13+
// __rust_alloc should be optimized out
14+
// CHECK-NOT: __rust_alloc
15+
16+
let v1 = vec![5, 6, 7];
17+
let v1_iter = v1.iter();
18+
let total: i32 = v1_iter.sum();
19+
println!("{}",total);
20+
total
21+
}

Diff for: tests/codegen/unwind-landingpad-cold.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// compile-flags: -Cno-prepopulate-passes
2+
#![crate_type = "lib"]
3+
4+
// This test checks that drop calls in unwind landing pads
5+
// get the `cold` attribute.
6+
7+
// CHECK-LABEL: @check_cold
8+
// CHECK: {{(call|invoke) void .+}}drop_in_place{{.+}} [[ATTRIBUTES:#[0-9]+]]
9+
// CHECK: attributes [[ATTRIBUTES]] = { cold }
10+
#[no_mangle]
11+
pub fn check_cold(f: fn(), x: Box<u32>) {
12+
// this may unwind
13+
f();
14+
}

Diff for: tests/codegen/unwind-landingpad-inline.rs

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// min-llvm-version: 15.0.0
2+
// compile-flags: -Copt-level=3
3+
// ignore-debug: the debug assertions get in the way
4+
#![crate_type = "lib"]
5+
6+
// This test checks that we can inline drop_in_place in
7+
// unwind landing pads.
8+
9+
// Without inlining, the box pointers escape via the call to drop_in_place,
10+
// and LLVM will not optimize out the pointer comparison.
11+
// With inlining, everything should be optimized out.
12+
// See https://github.com/rust-lang/rust/issues/46515
13+
// CHECK-LABEL: @check_no_escape_in_landingpad
14+
// CHECK: start:
15+
// CHECK-NEXT: __rust_no_alloc_shim_is_unstable
16+
// CHECK-NEXT: __rust_no_alloc_shim_is_unstable
17+
// CHECK-NEXT: ret void
18+
#[no_mangle]
19+
pub fn check_no_escape_in_landingpad(f: fn()) {
20+
let x = &*Box::new(0);
21+
let y = &*Box::new(0);
22+
23+
if x as *const _ == y as *const _ {
24+
f();
25+
}
26+
}
27+
28+
// Without inlining, the compiler can't tell that
29+
// dropping an empty string (in a landing pad) does nothing.
30+
// With inlining, the landing pad should be optimized out.
31+
// See https://github.com/rust-lang/rust/issues/87055
32+
// CHECK-LABEL: @check_eliminate_noop_drop
33+
// CHECK: start:
34+
// CHECK-NEXT: call void %g()
35+
// CHECK-NEXT: ret void
36+
#[no_mangle]
37+
pub fn check_eliminate_noop_drop(g: fn()) {
38+
let _var = String::new();
39+
g();
40+
}

0 commit comments

Comments
 (0)