Skip to content

Commit 88ab319

Browse files
erikdesjardinsInnovativeInventor
authored andcommitted
Reapply: Mark drop calls in landing pads cold instead of noinline
This reverts commit 4a56cbe, reversing changes made to 6e5a6ff.
1 parent 63b2ee0 commit 88ab319

File tree

9 files changed

+88
-12
lines changed

9 files changed

+88
-12
lines changed

compiler/rustc_codegen_gcc/src/builder.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1392,8 +1392,9 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
13921392
self.cx
13931393
}
13941394

1395-
fn do_not_inline(&mut self, _llret: RValue<'gcc>) {
1395+
fn apply_attrs_to_cleanup_callsite(&mut self, _llret: RValue<'gcc>) {
13961396
// FIXME(bjorn3): implement
1397+
unimplemented!();
13971398
}
13981399

13991400
fn set_span(&mut self, _span: Span) {}

compiler/rustc_codegen_llvm/src/builder.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use rustc_span::Span;
2424
use rustc_symbol_mangling::typeid::{kcfi_typeid_for_fnabi, typeid_for_fnabi, TypeIdOptions};
2525
use rustc_target::abi::{self, call::FnAbi, Align, Size, WrappingRange};
2626
use rustc_target::spec::{HasTargetSpec, SanitizerSet, Target};
27+
use smallvec::SmallVec;
2728
use std::borrow::Cow;
2829
use std::ffi::CStr;
2930
use std::iter;
@@ -1229,9 +1230,19 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
12291230
unsafe { llvm::LLVMBuildZExt(self.llbuilder, val, dest_ty, UNNAMED) }
12301231
}
12311232

1232-
fn do_not_inline(&mut self, llret: &'ll Value) {
1233-
let noinline = llvm::AttributeKind::NoInline.create_attr(self.llcx);
1234-
attributes::apply_to_callsite(llret, llvm::AttributePlace::Function, &[noinline]);
1233+
fn apply_attrs_to_cleanup_callsite(&mut self, llret: &'ll Value) {
1234+
let mut attrs = SmallVec::<[_; 2]>::new();
1235+
1236+
// Cleanup is always the cold path.
1237+
attrs.push(llvm::AttributeKind::Cold.create_attr(self.llcx));
1238+
1239+
// In LLVM versions with deferred inlining (currently, system LLVM < 14),
1240+
// inlining drop glue can lead to exponential size blowup, see #41696 and #92110.
1241+
if !llvm_util::is_rust_llvm() && llvm_util::get_version() < (14, 0, 0) {
1242+
attrs.push(llvm::AttributeKind::NoInline.create_attr(self.llcx));
1243+
}
1244+
1245+
attributes::apply_to_callsite(llret, llvm::AttributePlace::Function, &attrs);
12351246
}
12361247
}
12371248

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1921,6 +1921,8 @@ extern "C" {
19211921
pub fn LLVMRustVersionMinor() -> u32;
19221922
pub fn LLVMRustVersionPatch() -> u32;
19231923

1924+
pub fn LLVMRustIsRustLLVM() -> bool;
1925+
19241926
/// Add LLVM module flags.
19251927
///
19261928
/// In order for Rust-C LTO to work, module flags must be compatible with Clang. What

compiler/rustc_codegen_llvm/src/llvm_util.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,12 @@ pub fn get_version() -> (u32, u32, u32) {
244244
}
245245
}
246246

247+
/// Returns `true` if this LLVM is Rust's bundled LLVM (and not system LLVM).
248+
pub fn is_rust_llvm() -> bool {
249+
// Can be called without initializing LLVM
250+
unsafe { llvm::LLVMRustIsRustLLVM() }
251+
}
252+
247253
pub fn print_passes() {
248254
// Can be called without initializing LLVM
249255
unsafe {

compiler/rustc_codegen_ssa/src/mir/block.rs

Lines changed: 3 additions & 7 deletions
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 {
@@ -1611,7 +1607,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
16111607
let fn_ty = bx.fn_decl_backend_type(&fn_abi);
16121608

16131609
let llret = bx.call(fn_ty, None, Some(&fn_abi), fn_ptr, &[], funclet.as_ref());
1614-
bx.do_not_inline(llret);
1610+
bx.apply_attrs_to_cleanup_callsite(llret);
16151611

16161612
bx.unreachable();
16171613

compiler/rustc_codegen_ssa/src/traits/builder.rs

Lines changed: 1 addition & 1 deletion
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
}

compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,14 @@ extern "C" uint32_t LLVMRustVersionMinor() { return LLVM_VERSION_MINOR; }
731731

732732
extern "C" uint32_t LLVMRustVersionMajor() { return LLVM_VERSION_MAJOR; }
733733

734+
extern "C" bool LLVMRustIsRustLLVM() {
735+
#ifdef LLVM_RUSTLLVM
736+
return true;
737+
#else
738+
return false;
739+
#endif
740+
}
741+
734742
extern "C" void LLVMRustAddModuleFlag(
735743
LLVMModuleRef M,
736744
Module::ModFlagBehavior MergeBehavior,
Lines changed: 15 additions & 0 deletions
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|invoke) 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+
}
Lines changed: 37 additions & 0 deletions
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)