Skip to content

Commit 047dd55

Browse files
authored
Rollup merge of rust-lang#102099 - InnovativeInventor:re-cold-land, r=nikic
Rebased: Mark drop calls in landing pads cold instead of noinline I noticed that certain inlining optimizations were missing while staring at some compiled code output. I'd like to see this relanded, so I rebased the PR from ``@erikdesjardins`` (PR rust-lang#94823). This PR reapplies rust-lang#92419, which was reverted in rust-lang#94402 due to rust-lang#94390. Fixes rust-lang#46515, fixes rust-lang#87055. Update: fixes rust-lang#97217.
2 parents 448a75b + e5b84f3 commit 047dd55

File tree

7 files changed

+83
-12
lines changed

7 files changed

+83
-12
lines changed

compiler/rustc_codegen_gcc/src/builder.rs

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

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

compiler/rustc_codegen_llvm/src/builder.rs

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

1224-
fn do_not_inline(&mut self, llret: &'ll Value) {
1225-
let noinline = llvm::AttributeKind::NoInline.create_attr(self.llcx);
1226-
attributes::apply_to_callsite(llret, llvm::AttributePlace::Function, &[noinline]);
1224+
fn apply_attrs_to_cleanup_callsite(&mut self, llret: &'ll Value) {
1225+
// Cleanup is always the cold path.
1226+
let cold_inline = llvm::AttributeKind::Cold.create_attr(self.llcx);
1227+
1228+
attributes::apply_to_callsite(llret, llvm::AttributePlace::Function, &[cold_inline]);
12271229
}
12281230
}
12291231

compiler/rustc_codegen_ssa/src/mir/block.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ impl<'a, 'tcx> TerminatorCodegenHelper<'tcx> {
204204
self.funclet(fx),
205205
);
206206
if fx.mir[self.bb].is_cleanup {
207-
bx.do_not_inline(invokeret);
207+
bx.apply_attrs_to_cleanup_callsite(invokeret);
208208
}
209209

210210
if let Some((ret_dest, target)) = destination {
@@ -219,11 +219,7 @@ impl<'a, 'tcx> TerminatorCodegenHelper<'tcx> {
219219
} else {
220220
let llret = bx.call(fn_ty, fn_attrs, Some(&fn_abi), fn_ptr, &llargs, self.funclet(fx));
221221
if fx.mir[self.bb].is_cleanup {
222-
// Cleanup is always the cold path. Don't inline
223-
// drop glue. Also, when there is a deeply-nested
224-
// struct, there are "symmetry" issues that cause
225-
// exponential inlining - see issue #41696.
226-
bx.do_not_inline(llret);
222+
bx.apply_attrs_to_cleanup_callsite(llret);
227223
}
228224

229225
if let Some((ret_dest, target)) = destination {
@@ -1618,7 +1614,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
16181614
let fn_ty = bx.fn_decl_backend_type(&fn_abi);
16191615

16201616
let llret = bx.call(fn_ty, None, Some(&fn_abi), fn_ptr, &[], funclet.as_ref());
1621-
bx.do_not_inline(llret);
1617+
bx.apply_attrs_to_cleanup_callsite(llret);
16221618

16231619
bx.unreachable();
16241620

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
}

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+
}
+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+
}
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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: ret void
16+
#[no_mangle]
17+
pub fn check_no_escape_in_landingpad(f: fn()) {
18+
let x = &*Box::new(0);
19+
let y = &*Box::new(0);
20+
21+
if x as *const _ == y as *const _ {
22+
f();
23+
}
24+
}
25+
26+
// Without inlining, the compiler can't tell that
27+
// dropping an empty string (in a landing pad) does nothing.
28+
// With inlining, the landing pad should be optimized out.
29+
// See https://github.com/rust-lang/rust/issues/87055
30+
// CHECK-LABEL: @check_eliminate_noop_drop
31+
// CHECK: start:
32+
// CHECK-NEXT: call void %g()
33+
// CHECK-NEXT: ret void
34+
#[no_mangle]
35+
pub fn check_eliminate_noop_drop(g: fn()) {
36+
let _var = String::new();
37+
g();
38+
}

0 commit comments

Comments
 (0)