Skip to content

Commit 215b8f1

Browse files
authored
[DebugInfo][RemoveDIs] Convert debug-info modes when loading bitcode (#78967)
As part of eliminating debug-intrinsics in LLVM, we'll shortly be pushing the conversion from "old" dbg.value mode to "new" DPValue mode out from when the pass manager runs, to when modules are loaded. This patch adds that conversion process and some (temporary) options to llvm-lto{,2} to help test it. Specifically: now whenever we load a bitcode module, consider a flag of whether to "upgrade" it into the new debug-info mode, and if we're lazily materializing functions then do that lazily too. Doing this exposes an error in the IRLinker/materializer handling of DPValues, where we need to transfer the debug-info format flag correctly, and in ValueMapper we need to remap the Values that DPValues point at. I've added some test coverage in the modified tests; these will be exercised by our llvm-new-debug-iterators buildbot. This upgrading of debug-info won't be happening for the llvm18 release, instead we'll turn it on after the branch date, thenbe push the boundary of where "new" debug-info starts and ends down into the existing debug-info upgrade path over the course of the next release.
1 parent cd0b005 commit 215b8f1

File tree

12 files changed

+111
-4
lines changed

12 files changed

+111
-4
lines changed

llvm/lib/Bitcode/Reader/BitcodeReader.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ static cl::opt<bool> ExpandConstantExprs(
100100
cl::desc(
101101
"Expand constant expressions to instructions for testing purposes"));
102102

103+
// Declare external flag for whether we're using the new debug-info format.
104+
extern llvm::cl::opt<bool> UseNewDbgInfoFormat;
105+
103106
namespace {
104107

105108
enum {
@@ -6629,6 +6632,9 @@ Error BitcodeReader::materialize(GlobalValue *GV) {
66296632
if (Error Err = materializeMetadata())
66306633
return Err;
66316634

6635+
bool NewDebugInfoRequested = F->IsNewDbgInfoFormat;
6636+
F->IsNewDbgInfoFormat = false;
6637+
66326638
// Move the bit stream to the saved position of the deferred function body.
66336639
if (Error JumpFailed = Stream.JumpToBit(DFII->second))
66346640
return JumpFailed;
@@ -6704,6 +6710,14 @@ Error BitcodeReader::materialize(GlobalValue *GV) {
67046710
// Look for functions that rely on old function attribute behavior.
67056711
UpgradeFunctionAttributes(*F);
67066712

6713+
// If we've materialized a function set up in "new" debug-info mode, the
6714+
// contents just loaded will still be in dbg.value mode. Switch to the new
6715+
// mode now. NB: we can add more complicated logic here in the future to
6716+
// correctly identify when we do and don't need to autoupgrade.
6717+
if (NewDebugInfoRequested) {
6718+
F->convertToNewDbgValues();
6719+
}
6720+
67076721
// Bring in any functions that this function forward-referenced via
67086722
// blockaddresses.
67096723
return materializeForwardReferencedFunctions();
@@ -8027,6 +8041,15 @@ BitcodeModule::getModuleImpl(LLVMContext &Context, bool MaterializeAll,
80278041
if (Error Err = R->materializeForwardReferencedFunctions())
80288042
return std::move(Err);
80298043
}
8044+
8045+
// If we are operating in a "new debug-info" context, upgrade the debug-info
8046+
// in the loaded module. This is a transitional approach as we enable "new"
8047+
// debug-info in LLVM, which will eventually be pushed down into the
8048+
// autoupgrade path once the bitcode-encoding is finalised. Non-materialised
8049+
// functions will be upgraded in the materialize method.
8050+
if (UseNewDbgInfoFormat && !M->IsNewDbgInfoFormat)
8051+
M->convertToNewDbgValues();
8052+
80308053
return std::move(M);
80318054
}
80328055

llvm/lib/IR/LegacyPassManager.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,8 @@ bool PassManagerImpl::run(Module &M) {
530530

531531
// RemoveDIs: if a command line flag is given, convert to the DPValue
532532
// representation of debug-info for the duration of these passes.
533-
if (UseNewDbgInfoFormat)
533+
bool shouldConvertDbgInfo = UseNewDbgInfoFormat && !M.IsNewDbgInfoFormat;
534+
if (shouldConvertDbgInfo)
534535
M.convertToNewDbgValues();
535536

536537
for (ImmutablePass *ImPass : getImmutablePasses())
@@ -545,7 +546,8 @@ bool PassManagerImpl::run(Module &M) {
545546
for (ImmutablePass *ImPass : getImmutablePasses())
546547
Changed |= ImPass->doFinalization(M);
547548

548-
M.convertFromNewDbgValues();
549+
if (shouldConvertDbgInfo)
550+
M.convertFromNewDbgValues();
549551

550552
return Changed;
551553
}

llvm/lib/Linker/IRMover.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,7 @@ Function *IRLinker::copyFunctionProto(const Function *SF) {
694694
SF->getAddressSpace(), SF->getName(), &DstM);
695695
F->copyAttributesFrom(SF);
696696
F->setAttributes(mapAttributeTypes(F->getContext(), F->getAttributes()));
697+
F->IsNewDbgInfoFormat = SF->IsNewDbgInfoFormat;
697698
return F;
698699
}
699700

llvm/lib/Transforms/Utils/ValueMapper.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,9 +1056,13 @@ void Mapper::remapFunction(Function &F) {
10561056
A.mutateType(TypeMapper->remapType(A.getType()));
10571057

10581058
// Remap the instructions.
1059-
for (BasicBlock &BB : F)
1060-
for (Instruction &I : BB)
1059+
for (BasicBlock &BB : F) {
1060+
for (Instruction &I : BB) {
10611061
remapInstruction(&I);
1062+
for (DPValue &DPV : I.DbgMarker->getDbgValueRange())
1063+
remapDPValue(DPV);
1064+
}
1065+
}
10621066
}
10631067

10641068
void Mapper::mapAppendingVariable(GlobalVariable &GV, Constant *InitPrefix,

llvm/test/LTO/X86/pr38046.ll

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@
55
; RUN: llvm-dis %t.lto.o.0.2.internalize.bc >/dev/null 2>%t.dis.stderr || true
66
; RUN: FileCheck -allow-empty %s < %t.dis.stderr
77

8+
;; Re-run with "new" debug-info mode to ensure the variable location information
9+
;; is handled gracefully.
10+
; RUN: llvm-lto2 run -save-temps -o %t.lto.o %t.o \
11+
; RUN: -r=%t.o,foo,plx \
12+
; RUN: -r=%t.o,get,pl --try-experimental-debuginfo-iterators
13+
; RUN: llvm-dis %t.lto.o.0.2.internalize.bc >/dev/null 2>%t.dis.stderr || true
14+
; RUN: FileCheck -allow-empty %s < %t.dis.stderr
15+
816
; CHECK-NOT: Global is external, but doesn't have external or weak linkage
917

1018
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"

llvm/test/Linker/debug-info-use-before-def.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
; RUN: llvm-link -S %s | FileCheck %s
2+
; RUN: llvm-link -S %s --try-experimental-debuginfo-iterators | FileCheck %s
23

34
; Test that when a debug metadata use-before-def is run through llvm-link, the
45
; value reference is preserved. Tests both singular uses and DIArgList uses of

llvm/test/Linker/thinlto_funcimport_debug.ll

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
; If we import func1 and not func2 we should only link DISubprogram for func1
77
; RUN: llvm-link %t2.bc -summary-index=%t3.thinlto.bc -import=func1:%t.bc -S | FileCheck %s
88

9+
;; Repeat runlines using "new" debuginfo iterators mode.
10+
; RUN: llvm-lto -thinlto -o %t3 %t.bc %t2.bc --try-experimental-debuginfo-iterators
11+
; RUN: llvm-link %t2.bc -summary-index=%t3.thinlto.bc -import=func1:%t.bc -S --try-experimental-debuginfo-iterators | FileCheck %s
12+
913
; CHECK: declare i32 @func2
1014
; CHECK: define available_externally i32 @func1
1115

llvm/test/ThinLTO/X86/debuginfo-cu-import.ll

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
; CHECK-NOT: DICompileUnit{{.*}} globals:
1414
; CHECK-NOT: DICompileUnit{{.*}} imports:
1515

16+
;; Repeat test in RemoveDIs debug-info mode to check that bitcode is loaded and
17+
;; converted correctly.
18+
; RUN: llvm-lto -thinlto-action=import %t2.bc -thinlto-index=%t.index.bc -o - --try-experimental-debuginfo-iterators | llvm-dis -o - | FileCheck %s
19+
1620
; ModuleID = 'debuginfo-cu-import.c'
1721
source_filename = "debuginfo-cu-import.c"
1822
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"

llvm/test/ThinLTO/X86/pr35472.ll

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99
; RUN: llvm-nm %t1.bc.thinlto.o | FileCheck %s -check-prefix=ThinLTOa
1010
; RUN: llvm-nm %t2.bc.thinlto.o | FileCheck %s -check-prefix=ThinLTOb
1111

12+
;; Re-run with "new" debug-info mode, checking that we load / convert / emit
13+
;; the dbg.declares below correctly.
14+
; RUN: llvm-lto -thinlto-action=run %t1.bc %t2.bc -exported-symbol=_Z5Alphav --try-experimental-debuginfo-iterators
15+
; RUN: llvm-nm %t1.bc.thinlto.o | FileCheck %s -check-prefix=ThinLTOa
16+
; RUN: llvm-nm %t2.bc.thinlto.o | FileCheck %s -check-prefix=ThinLTOb
17+
1218
; ThinLTOa-DAG: T _Z5Bravov
1319
; ThinLTOa-DAG: W _ZN4EchoD2Ev
1420
; ThinLTOb-DAG: T _Z5Alphav

llvm/tools/llvm-link/llvm-link.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,13 @@ static cl::opt<bool> IgnoreNonBitcode(
129129
cl::desc("Do not report an error for non-bitcode files in archives"),
130130
cl::Hidden);
131131

132+
static cl::opt<bool> TryUseNewDbgInfoFormat(
133+
"try-experimental-debuginfo-iterators",
134+
cl::desc("Enable debuginfo iterator positions, if they're built in"),
135+
cl::init(false));
136+
137+
extern cl::opt<bool> UseNewDbgInfoFormat;
138+
132139
static ExitOnError ExitOnErr;
133140

134141
// Read the specified bitcode file in and return it. This routine searches the
@@ -465,6 +472,17 @@ int main(int argc, char **argv) {
465472
cl::HideUnrelatedOptions({&LinkCategory, &getColorCategory()});
466473
cl::ParseCommandLineOptions(argc, argv, "llvm linker\n");
467474

475+
// RemoveDIs debug-info transition: tests may request that we /try/ to use the
476+
// new debug-info format, if it's built in.
477+
#ifdef EXPERIMENTAL_DEBUGINFO_ITERATORS
478+
if (TryUseNewDbgInfoFormat) {
479+
// If LLVM was built with support for this, turn the new debug-info format
480+
// on.
481+
UseNewDbgInfoFormat = true;
482+
}
483+
#endif
484+
(void)TryUseNewDbgInfoFormat;
485+
468486
LLVMContext Context;
469487
Context.setDiagnosticHandler(std::make_unique<LLVMLinkDiagnosticHandler>(),
470488
true);

llvm/tools/llvm-lto/llvm-lto.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,13 @@ static cl::opt<bool>
264264
LTOSaveBeforeOpt("lto-save-before-opt", cl::init(false),
265265
cl::desc("Save the IR before running optimizations"));
266266

267+
static cl::opt<bool> TryUseNewDbgInfoFormat(
268+
"try-experimental-debuginfo-iterators",
269+
cl::desc("Enable debuginfo iterator positions, if they're built in"),
270+
cl::init(false), cl::Hidden);
271+
272+
extern cl::opt<bool> UseNewDbgInfoFormat;
273+
267274
namespace {
268275

269276
struct ModuleInfo {
@@ -937,6 +944,17 @@ int main(int argc, char **argv) {
937944
cl::HideUnrelatedOptions({&LTOCategory, &getColorCategory()});
938945
cl::ParseCommandLineOptions(argc, argv, "llvm LTO linker\n");
939946

947+
// RemoveDIs debug-info transition: tests may request that we /try/ to use the
948+
// new debug-info format, if it's built in.
949+
#ifdef EXPERIMENTAL_DEBUGINFO_ITERATORS
950+
if (TryUseNewDbgInfoFormat) {
951+
// If LLVM was built with support for this, turn the new debug-info format
952+
// on.
953+
UseNewDbgInfoFormat = true;
954+
}
955+
#endif
956+
(void)TryUseNewDbgInfoFormat;
957+
940958
if (OptLevel < '0' || OptLevel > '3')
941959
error("optimization level must be between 0 and 3");
942960

llvm/tools/llvm-lto2/llvm-lto2.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,13 @@ static cl::opt<bool> EnableFreestanding(
187187
cl::desc("Enable Freestanding (disable builtins / TLI) during LTO"),
188188
cl::Hidden);
189189

190+
static cl::opt<bool> TryUseNewDbgInfoFormat(
191+
"try-experimental-debuginfo-iterators",
192+
cl::desc("Enable debuginfo iterator positions, if they're built in"),
193+
cl::init(false), cl::Hidden);
194+
195+
extern cl::opt<bool> UseNewDbgInfoFormat;
196+
190197
static void check(Error E, std::string Msg) {
191198
if (!E)
192199
return;
@@ -222,6 +229,17 @@ static int usage() {
222229
static int run(int argc, char **argv) {
223230
cl::ParseCommandLineOptions(argc, argv, "Resolution-based LTO test harness");
224231

232+
// RemoveDIs debug-info transition: tests may request that we /try/ to use the
233+
// new debug-info format, if it's built in.
234+
#ifdef EXPERIMENTAL_DEBUGINFO_ITERATORS
235+
if (TryUseNewDbgInfoFormat) {
236+
// If LLVM was built with support for this, turn the new debug-info format
237+
// on.
238+
UseNewDbgInfoFormat = true;
239+
}
240+
#endif
241+
(void)TryUseNewDbgInfoFormat;
242+
225243
// FIXME: Workaround PR30396 which means that a symbol can appear
226244
// more than once if it is defined in module-level assembly and
227245
// has a GV declaration. We allow (file, symbol) pairs to have multiple

0 commit comments

Comments
 (0)