Skip to content

Commit 41a28aa

Browse files
committed
Auto merge of #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 #94823). This PR reapplies #92419, which was reverted in #94402 due to #94390. Fixes #46515, fixes #87055. Update: fixes #97217.
2 parents b0889cb + 5bcf4f2 commit 41a28aa

File tree

8 files changed

+95
-13
lines changed

8 files changed

+95
-13
lines changed

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

compiler/rustc_codegen_llvm/src/builder.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::attributes;
33
use crate::common::Funclet;
44
use crate::context::CodegenCx;
55
use crate::llvm::{self, AtomicOrdering, AtomicRmwBinOp, BasicBlock, False, True};
6+
use crate::llvm_util;
67
use crate::type_::Type;
78
use crate::type_of::LayoutLlvmExt;
89
use crate::value::Value;
@@ -1225,9 +1226,16 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
12251226
unsafe { llvm::LLVMBuildZExt(self.llbuilder, val, dest_ty, UNNAMED) }
12261227
}
12271228

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]);
1229+
fn apply_attrs_to_cleanup_callsite(&mut self, llret: &'ll Value) {
1230+
if llvm_util::get_version() < (17, 0, 2) {
1231+
// Work around https://github.com/llvm/llvm-project/issues/66984.
1232+
let noinline = llvm::AttributeKind::NoInline.create_attr(self.llcx);
1233+
attributes::apply_to_callsite(llret, llvm::AttributePlace::Function, &[noinline]);
1234+
} else {
1235+
// Cleanup is always the cold path.
1236+
let cold_inline = llvm::AttributeKind::Cold.create_attr(self.llcx);
1237+
attributes::apply_to_callsite(llret, llvm::AttributePlace::Function, &[cold_inline]);
1238+
}
12311239
}
12321240
}
12331241

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

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/assembly/stack-protector/stack-protector-heuristics-effect.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// [basic] compile-flags: -Z stack-protector=basic
1010
// [none] compile-flags: -Z stack-protector=none
1111
// compile-flags: -C opt-level=2 -Z merge-functions=disabled
12+
// min-llvm-version: 17.0.2
1213

1314
#![crate_type = "lib"]
1415

@@ -371,7 +372,7 @@ pub fn unsized_fn_param(s: [u8], l: bool, f: fn([u8])) {
371372

372373

373374
// all: __stack_chk_fail
374-
// strong: __stack_chk_fail
375+
// strong-NOT: __stack_chk_fail
375376
// basic-NOT: __stack_chk_fail
376377
// none-NOT: __stack_chk_fail
377378
// missing-NOT: __stack_chk_fail

tests/codegen/issue-97217.rs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// compile-flags: -C opt-level=3
2+
// ignore-debug: the debug assertions get in the way
3+
// min-llvm-version: 17.0.2
4+
#![crate_type = "lib"]
5+
6+
// Regression test for issue 97217 (the following should result in no allocations)
7+
8+
// CHECK-LABEL: @issue97217
9+
#[no_mangle]
10+
pub fn issue97217() -> i32 {
11+
// drop_in_place should be inlined and never appear
12+
// CHECK-NOT: drop_in_place
13+
14+
// __rust_alloc should be optimized out
15+
// CHECK-NOT: __rust_alloc
16+
17+
let v1 = vec![5, 6, 7];
18+
let v1_iter = v1.iter();
19+
let total: i32 = v1_iter.sum();
20+
println!("{}",total);
21+
total
22+
}
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// compile-flags: -Cno-prepopulate-passes
2+
// needs-unwind
3+
// min-llvm-version: 17.0.2
4+
#![crate_type = "lib"]
5+
6+
// This test checks that drop calls in unwind landing pads
7+
// get the `cold` attribute.
8+
9+
// CHECK-LABEL: @check_cold
10+
// CHECK: {{(call|invoke) void .+}}drop_in_place{{.+}} [[ATTRIBUTES:#[0-9]+]]
11+
// CHECK: attributes [[ATTRIBUTES]] = { cold }
12+
#[no_mangle]
13+
pub fn check_cold(f: fn(), x: Box<u32>) {
14+
// this may unwind
15+
f();
16+
}
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// min-llvm-version: 17.0.2
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: call void %g()
34+
// CHECK-NEXT: ret void
35+
#[no_mangle]
36+
pub fn check_eliminate_noop_drop(g: fn()) {
37+
let _var = String::new();
38+
g();
39+
}

0 commit comments

Comments
 (0)