Skip to content

Commit 379628d

Browse files
authored
[RemoveDIs] Add flag to preserve the debug info format of input IR (#87379)
This patch adds a new flag: `--preserve-input-debuginfo-format` This flag instructs the tool to not convert the debug info format (intrinsics/records) of input IR, but to instead determine the format of the input IR and overwrite the other format-determining flags so that we process and output the file in the same format that we received it in. This flag is turned off by llvm-link, llvm-lto, and llvm-lto2, and should be turned off by any other tool that expects to parse multiple IR modules and have their debug info formats match. The motivation for this flag is to allow tools to not convert the debug info format - verify-uselistorder and llvm-reduce, and any downstream tools that seek to test or mutate IR as-is, without applying extraneous modifications to the input. This is a necessary step to using debug records by default in all (other) LLVM tools.
1 parent 6d2f57d commit 379628d

File tree

14 files changed

+145
-26
lines changed

14 files changed

+145
-26
lines changed

llvm/include/llvm/AsmParser/LLParser.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ namespace llvm {
335335

336336
// Top-Level Entities
337337
bool parseTopLevelEntities();
338+
bool finalizeDebugInfoFormat(Module *M);
338339
void dropUnknownMetadataReferences();
339340
bool validateEndOfModule(bool UpgradeDebugInfo);
340341
bool validateEndOfIndex();

llvm/include/llvm/IR/BasicBlock.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ class BasicBlock final : public Value, // Basic blocks are data objects also
9292
/// in the new format (\p NewFlag == true), converting to the desired format
9393
/// if necessary.
9494
void setIsNewDbgInfoFormat(bool NewFlag);
95+
void setNewDbgInfoFormatFlag(bool NewFlag);
9596

9697
/// Record that the collection of DbgRecords in \p M "trails" after the last
9798
/// instruction of this block. These are equivalent to dbg.value intrinsics

llvm/include/llvm/IR/Function.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ class LLVM_EXTERNAL_VISIBILITY Function : public GlobalObject,
120120
void convertFromNewDbgValues();
121121

122122
void setIsNewDbgInfoFormat(bool NewVal);
123+
void setNewDbgInfoFormatFlag(bool NewVal);
123124

124125
private:
125126
friend class TargetLibraryInfoImpl;

llvm/include/llvm/IR/Module.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,12 @@ class LLVM_EXTERNAL_VISIBILITY Module {
245245
else if (!UseNewFormat && IsNewDbgInfoFormat)
246246
convertFromNewDbgValues();
247247
}
248+
void setNewDbgInfoFormatFlag(bool NewFlag) {
249+
for (auto &F : *this) {
250+
F.setNewDbgInfoFormatFlag(NewFlag);
251+
}
252+
IsNewDbgInfoFormat = NewFlag;
253+
}
248254

249255
/// The Module constructor. Note that there is no default constructor. You
250256
/// must provide a name for the module upon construction.

llvm/lib/AsmParser/LLParser.cpp

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ static cl::opt<bool> AllowIncompleteIR(
6363
"metadata will be dropped)"));
6464

6565
extern llvm::cl::opt<bool> UseNewDbgInfoFormat;
66+
extern cl::opt<cl::boolOrDefault> PreserveInputDbgFormat;
67+
extern bool WriteNewDbgInfoFormatToBitcode;
68+
extern cl::opt<bool> WriteNewDbgInfoFormat;
6669

6770
static std::string getTypeString(Type *T) {
6871
std::string Result;
@@ -71,12 +74,20 @@ static std::string getTypeString(Type *T) {
7174
return Tmp.str();
7275
}
7376

74-
// Currently, we should always process modules in the old debug info format by
75-
// default regardless of the module's format in IR; convert it to the old format
76-
// here.
77-
bool finalizeDebugInfoFormat(Module *M) {
78-
if (M)
77+
// Whatever debug info format we parsed, we should convert to the expected debug
78+
// info format immediately afterwards.
79+
bool LLParser::finalizeDebugInfoFormat(Module *M) {
80+
// We should have already returned an error if we observed both intrinsics and
81+
// records in this IR.
82+
assert(!(SeenNewDbgInfoFormat && SeenOldDbgInfoFormat) &&
83+
"Mixed debug intrinsics/records seen without a parsing error?");
84+
if (PreserveInputDbgFormat == cl::boolOrDefault::BOU_TRUE) {
85+
UseNewDbgInfoFormat = SeenNewDbgInfoFormat;
86+
WriteNewDbgInfoFormatToBitcode = SeenNewDbgInfoFormat;
87+
WriteNewDbgInfoFormat = SeenNewDbgInfoFormat;
88+
} else if (M) {
7989
M->setIsNewDbgInfoFormat(false);
90+
}
8091
return false;
8192
}
8293

@@ -6511,10 +6522,10 @@ bool LLParser::parseBasicBlock(PerFunctionState &PFS) {
65116522
if (SeenOldDbgInfoFormat)
65126523
return error(Lex.getLoc(), "debug record should not appear in a module "
65136524
"containing debug info intrinsics");
6525+
if (!SeenNewDbgInfoFormat)
6526+
M->setNewDbgInfoFormatFlag(true);
65146527
SeenNewDbgInfoFormat = true;
65156528
Lex.Lex();
6516-
if (!M->IsNewDbgInfoFormat)
6517-
M->convertToNewDbgValues();
65186529

65196530
DbgRecord *DR;
65206531
if (parseDebugRecord(DR, PFS))
@@ -7928,6 +7939,8 @@ bool LLParser::parseCall(Instruction *&Inst, PerFunctionState &PFS,
79287939
return error(CallLoc, "llvm.dbg intrinsic should not appear in a module "
79297940
"using non-intrinsic debug info");
79307941
}
7942+
if (!SeenOldDbgInfoFormat)
7943+
M->setNewDbgInfoFormatFlag(false);
79317944
SeenOldDbgInfoFormat = true;
79327945
}
79337946
CI->setAttributes(PAL);

llvm/lib/Bitcode/Reader/BitcodeReader.cpp

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ cl::opt<cl::boolOrDefault> LoadBitcodeIntoNewDbgInfoFormat(
108108
"load-bitcode-into-experimental-debuginfo-iterators", cl::Hidden,
109109
cl::desc("Load bitcode directly into the new debug info format (regardless "
110110
"of input format)"));
111+
extern cl::opt<cl::boolOrDefault> PreserveInputDbgFormat;
112+
extern bool WriteNewDbgInfoFormatToBitcode;
113+
extern cl::opt<bool> WriteNewDbgInfoFormat;
111114

112115
namespace {
113116

@@ -682,6 +685,11 @@ class BitcodeReader : public BitcodeReaderBase, public GVMaterializer {
682685
/// (e.g.) blockaddress forward references.
683686
bool WillMaterializeAllForwardRefs = false;
684687

688+
/// Tracks whether we have seen debug intrinsics or records in this bitcode;
689+
/// seeing both in a single module is currently a fatal error.
690+
bool SeenDebugIntrinsic = false;
691+
bool SeenDebugRecord = false;
692+
685693
bool StripDebugInfo = false;
686694
TBAAVerifier TBAAVerifyHelper;
687695

@@ -3774,7 +3782,11 @@ Error BitcodeReader::globalCleanup() {
37743782
for (Function &F : *TheModule) {
37753783
MDLoader->upgradeDebugIntrinsics(F);
37763784
Function *NewFn;
3777-
if (UpgradeIntrinsicFunction(&F, NewFn))
3785+
// If PreserveInputDbgFormat=true, then we don't know whether we want
3786+
// intrinsics or records, and we won't perform any conversions in either
3787+
// case, so don't upgrade intrinsics to records.
3788+
if (UpgradeIntrinsicFunction(
3789+
&F, NewFn, PreserveInputDbgFormat != cl::boolOrDefault::BOU_TRUE))
37783790
UpgradedIntrinsics[&F] = NewFn;
37793791
// Look for functions that rely on old function attribute behavior.
37803792
UpgradeFunctionAttributes(F);
@@ -4301,10 +4313,13 @@ Error BitcodeReader::parseModule(uint64_t ResumeBit,
43014313
bool ShouldLazyLoadMetadata,
43024314
ParserCallbacks Callbacks) {
43034315
// Load directly into RemoveDIs format if LoadBitcodeIntoNewDbgInfoFormat
4304-
// has been set to true (default action: load into the old debug format).
4305-
TheModule->IsNewDbgInfoFormat =
4306-
UseNewDbgInfoFormat &&
4307-
LoadBitcodeIntoNewDbgInfoFormat == cl::boolOrDefault::BOU_TRUE;
4316+
// has been set to true and we aren't attempting to preserve the existing
4317+
// format in the bitcode (default action: load into the old debug format).
4318+
if (PreserveInputDbgFormat != cl::boolOrDefault::BOU_TRUE) {
4319+
TheModule->IsNewDbgInfoFormat =
4320+
UseNewDbgInfoFormat &&
4321+
LoadBitcodeIntoNewDbgInfoFormat == cl::boolOrDefault::BOU_TRUE;
4322+
}
43084323

43094324
this->ValueTypeCallback = std::move(Callbacks.ValueType);
43104325
if (ResumeBit) {
@@ -6453,6 +6468,7 @@ Error BitcodeReader::parseFunctionBody(Function *F) {
64536468
case bitc::FUNC_CODE_DEBUG_RECORD_ASSIGN: {
64546469
// DbgVariableRecords are placed after the Instructions that they are
64556470
// attached to.
6471+
SeenDebugRecord = true;
64566472
Instruction *Inst = getLastInstruction();
64576473
if (!Inst)
64586474
return error("Invalid dbg record: missing instruction");
@@ -6613,6 +6629,8 @@ Error BitcodeReader::parseFunctionBody(Function *F) {
66136629
TCK = CallInst::TCK_NoTail;
66146630
cast<CallInst>(I)->setTailCallKind(TCK);
66156631
cast<CallInst>(I)->setAttributes(PAL);
6632+
if (isa<DbgInfoIntrinsic>(I))
6633+
SeenDebugIntrinsic = true;
66166634
if (Error Err = propagateAttributeTypes(cast<CallBase>(I), ArgTyIDs)) {
66176635
I->deleteValue();
66186636
return Err;
@@ -6801,20 +6819,48 @@ Error BitcodeReader::materialize(GlobalValue *GV) {
68016819
if (Error JumpFailed = Stream.JumpToBit(DFII->second))
68026820
return JumpFailed;
68036821

6804-
// Set the debug info mode to "new", possibly creating a mismatch between
6805-
// module and function debug modes. This is okay because we'll convert
6806-
// everything back to the old mode after parsing if needed.
6807-
// FIXME: Remove this once all tools support RemoveDIs.
6822+
// Regardless of the debug info format we want to end up in, we need
6823+
// IsNewDbgInfoFormat=true to construct any debug records seen in the bitcode.
68086824
F->IsNewDbgInfoFormat = true;
68096825

68106826
if (Error Err = parseFunctionBody(F))
68116827
return Err;
68126828
F->setIsMaterializable(false);
68136829

6814-
// Convert new debug info records into intrinsics.
6815-
// FIXME: Remove this once all tools support RemoveDIs.
6816-
if (!F->getParent()->IsNewDbgInfoFormat)
6817-
F->convertFromNewDbgValues();
6830+
// All parsed Functions should load into the debug info format dictated by the
6831+
// Module, unless we're attempting to preserve the input debug info format.
6832+
if (SeenDebugIntrinsic && SeenDebugRecord)
6833+
return error("Mixed debug intrinsics and debug records in bitcode module!");
6834+
if (PreserveInputDbgFormat == cl::boolOrDefault::BOU_TRUE) {
6835+
bool SeenAnyDebugInfo = SeenDebugIntrinsic || SeenDebugRecord;
6836+
bool NewDbgInfoFormatDesired =
6837+
SeenAnyDebugInfo ? SeenDebugRecord : F->getParent()->IsNewDbgInfoFormat;
6838+
if (SeenAnyDebugInfo) {
6839+
UseNewDbgInfoFormat = SeenDebugRecord;
6840+
WriteNewDbgInfoFormatToBitcode = SeenDebugRecord;
6841+
WriteNewDbgInfoFormat = SeenDebugRecord;
6842+
}
6843+
// If the module's debug info format doesn't match the observed input
6844+
// format, then set its format now; we don't need to call the conversion
6845+
// function because there must be no existing intrinsics to convert.
6846+
// Otherwise, just set the format on this function now.
6847+
if (NewDbgInfoFormatDesired != F->getParent()->IsNewDbgInfoFormat)
6848+
F->getParent()->setNewDbgInfoFormatFlag(NewDbgInfoFormatDesired);
6849+
else
6850+
F->setNewDbgInfoFormatFlag(NewDbgInfoFormatDesired);
6851+
} else {
6852+
// If we aren't preserving formats, we use the Module flag to get our
6853+
// desired format instead of reading flags, in case we are lazy-loading and
6854+
// the format of the module has been changed since it was set by the flags.
6855+
// We only need to convert debug info here if we have debug records but
6856+
// desire the intrinsic format; everything else is a no-op or handled by the
6857+
// autoupgrader.
6858+
bool ModuleIsNewDbgInfoFormat = F->getParent()->IsNewDbgInfoFormat;
6859+
if (ModuleIsNewDbgInfoFormat || !SeenDebugRecord)
6860+
F->setNewDbgInfoFormatFlag(ModuleIsNewDbgInfoFormat);
6861+
else
6862+
F->setIsNewDbgInfoFormat(ModuleIsNewDbgInfoFormat);
6863+
}
68186864

68196865
if (StripDebugInfo)
68206866
stripDebugInfo(*F);

llvm/lib/IR/BasicBlock.cpp

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,19 @@ using namespace llvm;
3030
#define DEBUG_TYPE "ir"
3131
STATISTIC(NumInstrRenumberings, "Number of renumberings across all blocks");
3232

33-
cl::opt<bool>
34-
UseNewDbgInfoFormat("experimental-debuginfo-iterators",
35-
cl::desc("Enable communicating debuginfo positions "
36-
"through iterators, eliminating intrinsics"),
37-
cl::init(true));
33+
cl::opt<bool> UseNewDbgInfoFormat(
34+
"experimental-debuginfo-iterators",
35+
cl::desc("Enable communicating debuginfo positions through iterators, "
36+
"eliminating intrinsics. Has no effect if "
37+
"--preserve-input-debuginfo-format=true."),
38+
cl::init(true));
39+
cl::opt<cl::boolOrDefault> PreserveInputDbgFormat(
40+
"preserve-input-debuginfo-format", cl::Hidden,
41+
cl::desc("When set to true, IR files will be processed and printed in "
42+
"their current debug info format, regardless of default behaviour "
43+
"or other flags passed. Has no effect if input IR does not "
44+
"contain debug records or intrinsics. Ignored in llvm-link, "
45+
"llvm-lto, and llvm-lto2."));
3846

3947
bool WriteNewDbgInfoFormatToBitcode /*set default value in cl::init() below*/;
4048
cl::opt<bool, true> WriteNewDbgInfoFormatToBitcode2(
@@ -147,6 +155,9 @@ void BasicBlock::setIsNewDbgInfoFormat(bool NewFlag) {
147155
else if (!NewFlag && IsNewDbgInfoFormat)
148156
convertFromNewDbgValues();
149157
}
158+
void BasicBlock::setNewDbgInfoFormatFlag(bool NewFlag) {
159+
IsNewDbgInfoFormat = NewFlag;
160+
}
150161

151162
ValueSymbolTable *BasicBlock::getValueSymbolTable() {
152163
if (Function *F = getParent())

llvm/lib/IR/Function.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ void Function::setIsNewDbgInfoFormat(bool NewFlag) {
103103
else if (!NewFlag && IsNewDbgInfoFormat)
104104
convertFromNewDbgValues();
105105
}
106+
void Function::setNewDbgInfoFormatFlag(bool NewFlag) {
107+
for (auto &BB : *this) {
108+
BB.setNewDbgInfoFormatFlag(NewFlag);
109+
}
110+
IsNewDbgInfoFormat = NewFlag;
111+
}
106112

107113
//===----------------------------------------------------------------------===//
108114
// Argument Implementation

llvm/lib/IR/IRPrintingPasses.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ using namespace llvm;
2525

2626
cl::opt<bool> WriteNewDbgInfoFormat(
2727
"write-experimental-debuginfo",
28-
cl::desc("Write debug info in the new non-intrinsic format"),
28+
cl::desc("Write debug info in the new non-intrinsic format. Has no effect "
29+
"if --preserve-input-debuginfo-format=true."),
2930
cl::init(false));
3031

3132
namespace {

llvm/test/Bitcode/dbg-record-roundtrip.ll

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@
1515
; RUN: | llvm-dis --load-bitcode-into-experimental-debuginfo-iterators=true --write-experimental-debuginfo=true \
1616
; RUN: | FileCheck %s --check-prefixes=RECORDS
1717

18+
;; When preserving, we should output the format the bitcode was written in
19+
;; regardless of the value of the write flag.
20+
; RUN: llvm-as --write-experimental-debuginfo-iterators-to-bitcode=true %s -o - \
21+
; RUN: | llvm-dis --preserve-input-debuginfo-format=true --write-experimental-debuginfo=false \
22+
; RUN: | FileCheck %s --check-prefixes=RECORDS
23+
24+
; RUN: llvm-as --write-experimental-debuginfo-iterators-to-bitcode=false %s -o - \
25+
; RUN: | llvm-dis --preserve-input-debuginfo-format=true --write-experimental-debuginfo=true \
26+
; RUN: | FileCheck %s
27+
1828
;; Check that verify-uselistorder passes regardless of input format.
1929
; RUN: llvm-as %s --write-experimental-debuginfo-iterators-to-bitcode=true -o - | verify-uselistorder
2030
; RUN: verify-uselistorder %s

llvm/test/DebugInfo/roundtrip-non-instruction-debug-info.ll

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@
1818
; RUN: opt --passes=verify -S --try-experimental-debuginfo-iterators --write-experimental-debuginfo=true < %s \
1919
; RUN: | FileCheck %s --check-prefixes=CHECK,NEWDBG --implicit-check-not=llvm.dbg --implicit-check-not=#dbg
2020

21+
;; Test that the preserving flag overrides the write flag.
22+
; RUN: opt --passes=verify -S --preserve-input-debuginfo-format=true --write-experimental-debuginfo=true < %s \
23+
; RUN: | FileCheck %s --check-prefixes=CHECK,OLDDBG --implicit-check-not=llvm.dbg --implicit-check-not=#dbg
24+
25+
; RUN: opt --passes=verify -S --write-experimental-debuginfo=true < %s \
26+
; RUN: | opt --passes=verify -S --preserve-input-debuginfo-format=true --write-experimental-debuginfo=false \
27+
; RUN: | FileCheck %s --check-prefixes=CHECK,NEWDBG --implicit-check-not=llvm.dbg --implicit-check-not=#dbg
28+
2129
; CHECK: @f(i32 %[[VAL_A:[0-9a-zA-Z]+]])
2230
; CHECK-NEXT: entry:
2331
; OLDDBG-NEXT: call void @llvm.dbg.value(metadata i32 %[[VAL_A]], metadata ![[VAR_A:[0-9]+]], metadata !DIExpression()), !dbg ![[LOC_1:[0-9]+]]

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ static cl::opt<bool> TryUseNewDbgInfoFormat(
135135
cl::init(false));
136136

137137
extern cl::opt<bool> UseNewDbgInfoFormat;
138+
extern cl::opt<cl::boolOrDefault> PreserveInputDbgFormat;
138139

139140
extern cl::opt<cl::boolOrDefault> LoadBitcodeIntoNewDbgInfoFormat;
140141

@@ -492,6 +493,10 @@ int main(int argc, char **argv) {
492493
// Turn the new debug-info format on.
493494
UseNewDbgInfoFormat = true;
494495
}
496+
// Since llvm-link collects multiple IR modules together, for simplicity's
497+
// sake we disable the "PreserveInputDbgFormat" flag to enforce a single
498+
// debug info format.
499+
PreserveInputDbgFormat = cl::boolOrDefault::BOU_FALSE;
495500

496501
LLVMContext Context;
497502
Context.setDiagnosticHandler(std::make_unique<LLVMLinkDiagnosticHandler>(),

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ static cl::opt<bool> TryUseNewDbgInfoFormat(
271271

272272
extern cl::opt<bool> UseNewDbgInfoFormat;
273273
extern cl::opt<cl::boolOrDefault> LoadBitcodeIntoNewDbgInfoFormat;
274+
extern cl::opt<cl::boolOrDefault> PreserveInputDbgFormat;
274275

275276
namespace {
276277

@@ -954,6 +955,10 @@ int main(int argc, char **argv) {
954955
// Turn the new debug-info format on.
955956
UseNewDbgInfoFormat = true;
956957
}
958+
// Since llvm-lto collects multiple IR modules together, for simplicity's sake
959+
// we disable the "PreserveInputDbgFormat" flag to enforce a single debug info
960+
// format.
961+
PreserveInputDbgFormat = cl::boolOrDefault::BOU_FALSE;
957962

958963
if (OptLevel < '0' || OptLevel > '3')
959964
error("optimization level must be between 0 and 3");

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ static cl::opt<bool> TryUseNewDbgInfoFormat(
194194

195195
extern cl::opt<bool> UseNewDbgInfoFormat;
196196
extern cl::opt<cl::boolOrDefault> LoadBitcodeIntoNewDbgInfoFormat;
197+
extern cl::opt<cl::boolOrDefault> PreserveInputDbgFormat;
197198

198199
static void check(Error E, std::string Msg) {
199200
if (!E)
@@ -239,6 +240,10 @@ static int run(int argc, char **argv) {
239240
// Turn the new debug-info format on.
240241
UseNewDbgInfoFormat = true;
241242
}
243+
// Since llvm-lto2 collects multiple IR modules together, for simplicity's
244+
// sake we disable the "PreserveInputDbgFormat" flag to enforce a single debug
245+
// info format.
246+
PreserveInputDbgFormat = cl::boolOrDefault::BOU_FALSE;
242247

243248
// FIXME: Workaround PR30396 which means that a symbol can appear
244249
// more than once if it is defined in module-level assembly and

0 commit comments

Comments
 (0)