Skip to content

Commit 4f49627

Browse files
committed
Auto merge of rust-lang#92419 - erikdesjardins:coldland, r=nagisa
Mark drop calls in landing pads `cold` instead of `noinline` Now that deferred inlining has been disabled in LLVM (rust-lang#92110), this shouldn't cause catastrophic size blowup. I confirmed that the test cases from rust-lang#41696 (comment) still compile quickly (<1s) after this change. ~Although note that I wasn't able to reproduce the original issue using a recent rustc/llvm with deferred inlining enabled, so those tests may no longer be representative. I was also unable to create a modified test case that reproduced the original issue.~ (edit: I reproduced it on CI by accident--the first commit timed out on the LLVM 12 builder, because I forgot to make it conditional on LLVM version) r? `@nagisa` cc `@arielb1` (this effectively reverts rust-lang#42771 "mark calls in the unwind path as !noinline") cc `@RalfJung` (fixes rust-lang#46515) edit: also fixes rust-lang#87055
2 parents 028c6f1 + 64da730 commit 4f49627

File tree

9 files changed

+80
-9
lines changed

9 files changed

+80
-9
lines changed

compiler/rustc_codegen_gcc/src/builder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1403,7 +1403,7 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
14031403
self.cx
14041404
}
14051405

1406-
fn do_not_inline(&mut self, _llret: RValue<'gcc>) {
1406+
fn apply_attrs_to_cleanup_callsite(&mut self, _llret: RValue<'gcc>) {
14071407
unimplemented!();
14081408
}
14091409

compiler/rustc_codegen_llvm/src/builder.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -1201,8 +1201,15 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
12011201
unsafe { llvm::LLVMBuildZExt(self.llbuilder, val, dest_ty, UNNAMED) }
12021202
}
12031203

1204-
fn do_not_inline(&mut self, llret: &'ll Value) {
1205-
llvm::Attribute::NoInline.apply_callsite(llvm::AttributePlace::Function, llret);
1204+
fn apply_attrs_to_cleanup_callsite(&mut self, llret: &'ll Value) {
1205+
// Cleanup is always the cold path.
1206+
llvm::Attribute::Cold.apply_callsite(llvm::AttributePlace::Function, llret);
1207+
1208+
// In LLVM versions with deferred inlining (currently, system LLVM < 14),
1209+
// inlining drop glue can lead to exponential size blowup, see #41696 and #92110.
1210+
if !llvm_util::is_rust_llvm() && llvm_util::get_version() < (14, 0, 0) {
1211+
llvm::Attribute::NoInline.apply_callsite(llvm::AttributePlace::Function, llret);
1212+
}
12061213
}
12071214
}
12081215

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1902,6 +1902,8 @@ extern "C" {
19021902
pub fn LLVMRustVersionMinor() -> u32;
19031903
pub fn LLVMRustVersionPatch() -> u32;
19041904

1905+
pub fn LLVMRustIsRustLLVM() -> bool;
1906+
19051907
pub fn LLVMRustAddModuleFlag(M: &Module, name: *const c_char, value: u32);
19061908

19071909
pub fn LLVMRustMetadataAsValue<'a>(C: &'a Context, MD: &'a Metadata) -> &'a Value;

compiler/rustc_codegen_llvm/src/llvm_util.rs

+6
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,12 @@ pub fn get_version() -> (u32, u32, u32) {
223223
}
224224
}
225225

226+
/// Returns `true` if this LLVM is Rust's bundled LLVM (and not system LLVM).
227+
pub fn is_rust_llvm() -> bool {
228+
// Can be called without initializing LLVM
229+
unsafe { llvm::LLVMRustIsRustLLVM() }
230+
}
231+
226232
pub fn print_passes() {
227233
// Can be called without initializing LLVM
228234
unsafe {

compiler/rustc_codegen_ssa/src/mir/block.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,7 @@ impl<'a, 'tcx> TerminatorCodegenHelper<'tcx> {
160160
let llret = bx.call(fn_ty, fn_ptr, &llargs, self.funclet(fx));
161161
bx.apply_attrs_callsite(&fn_abi, llret);
162162
if fx.mir[self.bb].is_cleanup {
163-
// Cleanup is always the cold path. Don't inline
164-
// drop glue. Also, when there is a deeply-nested
165-
// struct, there are "symmetry" issues that cause
166-
// exponential inlining - see issue #41696.
167-
bx.do_not_inline(llret);
163+
bx.apply_attrs_to_cleanup_callsite(llret);
168164
}
169165

170166
if let Some((ret_dest, target)) = destination {

compiler/rustc_codegen_ssa/src/traits/builder.rs

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

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

compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,14 @@ extern "C" uint32_t LLVMRustVersionMinor() { return LLVM_VERSION_MINOR; }
716716

717717
extern "C" uint32_t LLVMRustVersionMajor() { return LLVM_VERSION_MAJOR; }
718718

719+
extern "C" bool LLVMRustIsRustLLVM() {
720+
#ifdef LLVM_RUSTLLVM
721+
return true;
722+
#else
723+
return false;
724+
#endif
725+
}
726+
719727
extern "C" void LLVMRustAddModuleFlag(LLVMModuleRef M, const char *Name,
720728
uint32_t Value) {
721729
unwrap(M)->addModuleFlag(Module::Warning, Name, Value);
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// no-system-llvm: needs #92110
2+
// compile-flags: -Cno-prepopulate-passes
3+
#![crate_type = "lib"]
4+
5+
// This test checks that drop calls in unwind landing pads
6+
// get the `cold` attribute.
7+
8+
// CHECK-LABEL: @check_cold
9+
// CHECK: call void {{.+}}drop_in_place{{.+}} [[ATTRIBUTES:#[0-9]+]]
10+
// CHECK: attributes [[ATTRIBUTES]] = { cold }
11+
#[no_mangle]
12+
pub fn check_cold(f: fn(), x: Box<u32>) {
13+
// this may unwind
14+
f();
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// no-system-llvm: needs #92110 + patch for Rust alloc/dealloc functions
2+
// compile-flags: -Copt-level=3
3+
#![crate_type = "lib"]
4+
5+
// This test checks that we can inline drop_in_place in
6+
// unwind landing pads.
7+
8+
// Without inlining, the box pointers escape via the call to drop_in_place,
9+
// and LLVM will not optimize out the pointer comparison.
10+
// With inlining, everything should be optimized out.
11+
// See https://github.com/rust-lang/rust/issues/46515
12+
// CHECK-LABEL: @check_no_escape_in_landingpad
13+
// CHECK: start:
14+
// CHECK-NEXT: ret void
15+
#[no_mangle]
16+
pub fn check_no_escape_in_landingpad(f: fn()) {
17+
let x = &*Box::new(0);
18+
let y = &*Box::new(0);
19+
20+
if x as *const _ == y as *const _ {
21+
f();
22+
}
23+
}
24+
25+
// Without inlining, the compiler can't tell that
26+
// dropping an empty string (in a landing pad) does nothing.
27+
// With inlining, the landing pad should be optimized out.
28+
// See https://github.com/rust-lang/rust/issues/87055
29+
// CHECK-LABEL: @check_eliminate_noop_drop
30+
// CHECK: start:
31+
// CHECK-NEXT: call void %g()
32+
// CHECK-NEXT: ret void
33+
#[no_mangle]
34+
pub fn check_eliminate_noop_drop(g: fn()) {
35+
let _var = String::new();
36+
g();
37+
}

0 commit comments

Comments
 (0)