Skip to content

Commit d5de680

Browse files
committed
Work around invalid DWARF bugs for fat LTO
Signed-off-by: Yilin Chen <sticnarf@gmail.com>
1 parent 1c03f0d commit d5de680

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

compiler/rustc_codegen_llvm/src/back/lto.rs

+16-2
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,20 @@ fn fat_lto(
325325
drop(linker);
326326
save_temp_bitcode(&cgcx, &module, "lto.input");
327327

328+
// Fat LTO also suffers from the invalid DWARF issue similar to Thin LTO.
329+
// Here we rewrite all `DICompileUnit` pointers if there is only one `DICompileUnit`.
330+
// This only works around the problem when codegen-units = 1.
331+
// Refer to the comments in the `optimize_thin_module` function for more details.
332+
let mut cu1 = ptr::null_mut();
333+
let mut cu2 = ptr::null_mut();
334+
unsafe { llvm::LLVMRustLTOGetDICompileUnit(llmod, &mut cu1, &mut cu2) };
335+
if !cu2.is_null() {
336+
let _timer =
337+
cgcx.prof.generic_activity_with_arg("LLVM_fat_lto_patch_debuginfo", &*module.name);
338+
unsafe { llvm::LLVMRustLTOPatchDICompileUnit(llmod, cu1) };
339+
save_temp_bitcode(cgcx, &module, "fat-lto-after-patch");
340+
}
341+
328342
// Internalize everything below threshold to help strip out more modules and such.
329343
unsafe {
330344
let ptr = symbols_below_threshold.as_ptr();
@@ -748,7 +762,7 @@ pub unsafe fn optimize_thin_module(
748762
// an error.
749763
let mut cu1 = ptr::null_mut();
750764
let mut cu2 = ptr::null_mut();
751-
llvm::LLVMRustThinLTOGetDICompileUnit(llmod, &mut cu1, &mut cu2);
765+
llvm::LLVMRustLTOGetDICompileUnit(llmod, &mut cu1, &mut cu2);
752766
if !cu2.is_null() {
753767
let msg = "multiple source DICompileUnits found";
754768
return Err(write::llvm_err(&diag_handler, msg));
@@ -847,7 +861,7 @@ pub unsafe fn optimize_thin_module(
847861
let _timer = cgcx
848862
.prof
849863
.generic_activity_with_arg("LLVM_thin_lto_patch_debuginfo", thin_module.name());
850-
llvm::LLVMRustThinLTOPatchDICompileUnit(llmod, cu1);
864+
llvm::LLVMRustLTOPatchDICompileUnit(llmod, cu1);
851865
save_temp_bitcode(cgcx, &module, "thin-lto-after-patch");
852866
}
853867

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -2377,12 +2377,8 @@ extern "C" {
23772377
len: usize,
23782378
out_len: &mut usize,
23792379
) -> *const u8;
2380-
pub fn LLVMRustThinLTOGetDICompileUnit(
2381-
M: &Module,
2382-
CU1: &mut *mut c_void,
2383-
CU2: &mut *mut c_void,
2384-
);
2385-
pub fn LLVMRustThinLTOPatchDICompileUnit(M: &Module, CU: *mut c_void);
2380+
pub fn LLVMRustLTOGetDICompileUnit(M: &Module, CU1: &mut *mut c_void, CU2: &mut *mut c_void);
2381+
pub fn LLVMRustLTOPatchDICompileUnit(M: &Module, CU: *mut c_void);
23862382

23872383
pub fn LLVMRustLinkerNew(M: &'a Module) -> &'a mut Linker<'a>;
23882384
pub fn LLVMRustLinkerAdd(

compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1740,7 +1740,7 @@ LLVMRustGetBitcodeSliceFromObjectData(const char *data,
17401740
// Rewrite all `DICompileUnit` pointers to the `DICompileUnit` specified. See
17411741
// the comment in `back/lto.rs` for why this exists.
17421742
extern "C" void
1743-
LLVMRustThinLTOGetDICompileUnit(LLVMModuleRef Mod,
1743+
LLVMRustLTOGetDICompileUnit(LLVMModuleRef Mod,
17441744
DICompileUnit **A,
17451745
DICompileUnit **B) {
17461746
Module *M = unwrap(Mod);
@@ -1758,7 +1758,7 @@ LLVMRustThinLTOGetDICompileUnit(LLVMModuleRef Mod,
17581758
// Rewrite all `DICompileUnit` pointers to the `DICompileUnit` specified. See
17591759
// the comment in `back/lto.rs` for why this exists.
17601760
extern "C" void
1761-
LLVMRustThinLTOPatchDICompileUnit(LLVMModuleRef Mod, DICompileUnit *Unit) {
1761+
LLVMRustLTOPatchDICompileUnit(LLVMModuleRef Mod, DICompileUnit *Unit) {
17621762
Module *M = unwrap(Mod);
17631763

17641764
// If the original source module didn't have a `DICompileUnit` then try to

0 commit comments

Comments
 (0)