Skip to content

Commit c836c24

Browse files
committed
Remove the ThinLTO CU hack
This reverts #46722, commit e0ab5d5. Since #111167, commit 10b69dd, we are generating DWARF subprograms in a way that is meant to be more compatible with LLVM's expectations, so hopefully we don't need this workaround rewriting CUs anymore.
1 parent ce04288 commit c836c24

File tree

3 files changed

+0
-112
lines changed

3 files changed

+0
-112
lines changed

compiler/rustc_codegen_llvm/src/back/lto.rs

-49
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ use std::fs::File;
2525
use std::io;
2626
use std::iter;
2727
use std::path::Path;
28-
use std::ptr;
2928
use std::slice;
3029
use std::sync::Arc;
3130

@@ -709,17 +708,6 @@ pub unsafe fn optimize_thin_module(
709708
let llmod = module.module_llvm.llmod();
710709
save_temp_bitcode(cgcx, &module, "thin-lto-input");
711710

712-
// Before we do much else find the "main" `DICompileUnit` that we'll be
713-
// using below. If we find more than one though then rustc has changed
714-
// in a way we're not ready for, so generate an ICE by returning
715-
// an error.
716-
let mut cu1 = ptr::null_mut();
717-
let mut cu2 = ptr::null_mut();
718-
llvm::LLVMRustThinLTOGetDICompileUnit(llmod, &mut cu1, &mut cu2);
719-
if !cu2.is_null() {
720-
return Err(write::llvm_err(&diag_handler, LlvmError::MultipleSourceDiCompileUnit));
721-
}
722-
723711
// Up next comes the per-module local analyses that we do for Thin LTO.
724712
// Each of these functions is basically copied from the LLVM
725713
// implementation and then tailored to suit this implementation. Ideally
@@ -766,43 +754,6 @@ pub unsafe fn optimize_thin_module(
766754
save_temp_bitcode(cgcx, &module, "thin-lto-after-import");
767755
}
768756

769-
// Ok now this is a bit unfortunate. This is also something you won't
770-
// find upstream in LLVM's ThinLTO passes! This is a hack for now to
771-
// work around bugs in LLVM.
772-
//
773-
// First discovered in #45511 it was found that as part of ThinLTO
774-
// importing passes LLVM will import `DICompileUnit` metadata
775-
// information across modules. This means that we'll be working with one
776-
// LLVM module that has multiple `DICompileUnit` instances in it (a
777-
// bunch of `llvm.dbg.cu` members). Unfortunately there's a number of
778-
// bugs in LLVM's backend which generates invalid DWARF in a situation
779-
// like this:
780-
//
781-
// https://bugs.llvm.org/show_bug.cgi?id=35212
782-
// https://bugs.llvm.org/show_bug.cgi?id=35562
783-
//
784-
// While the first bug there is fixed the second ended up causing #46346
785-
// which was basically a resurgence of #45511 after LLVM's bug 35212 was
786-
// fixed.
787-
//
788-
// This function below is a huge hack around this problem. The function
789-
// below is defined in `PassWrapper.cpp` and will basically "merge"
790-
// all `DICompileUnit` instances in a module. Basically it'll take all
791-
// the objects, rewrite all pointers of `DISubprogram` to point to the
792-
// first `DICompileUnit`, and then delete all the other units.
793-
//
794-
// This is probably mangling to the debug info slightly (but hopefully
795-
// not too much) but for now at least gets LLVM to emit valid DWARF (or
796-
// so it appears). Hopefully we can remove this once upstream bugs are
797-
// fixed in LLVM.
798-
{
799-
let _timer = cgcx
800-
.prof
801-
.generic_activity_with_arg("LLVM_thin_lto_patch_debuginfo", thin_module.name());
802-
llvm::LLVMRustThinLTOPatchDICompileUnit(llmod, cu1);
803-
save_temp_bitcode(cgcx, &module, "thin-lto-after-patch");
804-
}
805-
806757
// Alright now that we've done everything related to the ThinLTO
807758
// analysis it's time to run some optimizations! Here we use the same
808759
// `run_pass_manager` as the "fat" LTO above except that we tell it to

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

-6
Original file line numberDiff line numberDiff line change
@@ -2480,12 +2480,6 @@ extern "C" {
24802480
len: usize,
24812481
out_len: &mut usize,
24822482
) -> *const u8;
2483-
pub fn LLVMRustThinLTOGetDICompileUnit(
2484-
M: &Module,
2485-
CU1: &mut *mut c_void,
2486-
CU2: &mut *mut c_void,
2487-
);
2488-
pub fn LLVMRustThinLTOPatchDICompileUnit(M: &Module, CU: *mut c_void);
24892483

24902484
pub fn LLVMRustLinkerNew(M: &Module) -> &mut Linker<'_>;
24912485
pub fn LLVMRustLinkerAdd(

compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp

-57
Original file line numberDiff line numberDiff line change
@@ -1460,63 +1460,6 @@ LLVMRustGetBitcodeSliceFromObjectData(const char *data,
14601460
return BitcodeOrError->getBufferStart();
14611461
}
14621462

1463-
// Rewrite all `DICompileUnit` pointers to the `DICompileUnit` specified. See
1464-
// the comment in `back/lto.rs` for why this exists.
1465-
extern "C" void
1466-
LLVMRustThinLTOGetDICompileUnit(LLVMModuleRef Mod,
1467-
DICompileUnit **A,
1468-
DICompileUnit **B) {
1469-
Module *M = unwrap(Mod);
1470-
DICompileUnit **Cur = A;
1471-
DICompileUnit **Next = B;
1472-
for (DICompileUnit *CU : M->debug_compile_units()) {
1473-
*Cur = CU;
1474-
Cur = Next;
1475-
Next = nullptr;
1476-
if (Cur == nullptr)
1477-
break;
1478-
}
1479-
}
1480-
1481-
// Rewrite all `DICompileUnit` pointers to the `DICompileUnit` specified. See
1482-
// the comment in `back/lto.rs` for why this exists.
1483-
extern "C" void
1484-
LLVMRustThinLTOPatchDICompileUnit(LLVMModuleRef Mod, DICompileUnit *Unit) {
1485-
Module *M = unwrap(Mod);
1486-
1487-
// If the original source module didn't have a `DICompileUnit` then try to
1488-
// merge all the existing compile units. If there aren't actually any though
1489-
// then there's not much for us to do so return.
1490-
if (Unit == nullptr) {
1491-
for (DICompileUnit *CU : M->debug_compile_units()) {
1492-
Unit = CU;
1493-
break;
1494-
}
1495-
if (Unit == nullptr)
1496-
return;
1497-
}
1498-
1499-
// Use LLVM's built-in `DebugInfoFinder` to find a bunch of debuginfo and
1500-
// process it recursively. Note that we used to specifically iterate over
1501-
// instructions to ensure we feed everything into it, but `processModule`
1502-
// started doing this the same way in LLVM 7 (commit d769eb36ab2b8).
1503-
DebugInfoFinder Finder;
1504-
Finder.processModule(*M);
1505-
1506-
// After we've found all our debuginfo, rewrite all subprograms to point to
1507-
// the same `DICompileUnit`.
1508-
for (auto &F : Finder.subprograms()) {
1509-
F->replaceUnit(Unit);
1510-
}
1511-
1512-
// Erase any other references to other `DICompileUnit` instances, the verifier
1513-
// will later ensure that we don't actually have any other stale references to
1514-
// worry about.
1515-
auto *MD = M->getNamedMetadata("llvm.dbg.cu");
1516-
MD->clearOperands();
1517-
MD->addOperand(Unit);
1518-
}
1519-
15201463
// Computes the LTO cache key for the provided 'ModId' in the given 'Data',
15211464
// storing the result in 'KeyOut'.
15221465
// Currently, this cache key is a SHA-1 hash of anything that could affect

0 commit comments

Comments
 (0)