Skip to content

Commit 8e45935

Browse files
authored
[RemoveDIs] Make verify-uselistorder preserve the input debug info format (#87789)
Verify-uselistorder wants to take some input IR and verify that the uselist order is stable after roundtripping to bitcode and assembly. This is disrupted if the file is converted between the new and old debug info formats after parsing - while there's no functional difference, the change to the in-memory representation of the IR modifies the uselist. This patch changes verify-uselistorder to not convert input files between debug info formats by default, preventing changes from being made to the file being checked. In addition, this patch makes it so that when we _do_ print IR in the new debug info format to bitcode or assembly, we delete any lingering debug intrinsic declarations, ensuring that we don't write uselist entries for them.
1 parent 5597d97 commit 8e45935

File tree

6 files changed

+35
-20
lines changed

6 files changed

+35
-20
lines changed

llvm/lib/Bitcode/Writer/BitcodeWriterPass.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ extern bool WriteNewDbgInfoFormatToBitcode;
2323
PreservedAnalyses BitcodeWriterPass::run(Module &M, ModuleAnalysisManager &AM) {
2424
ScopedDbgInfoFormatSetter FormatSetter(M, M.IsNewDbgInfoFormat &&
2525
WriteNewDbgInfoFormatToBitcode);
26+
if (M.IsNewDbgInfoFormat)
27+
M.removeDebugIntrinsicDeclarations();
2628

2729
const ModuleSummaryIndex *Index =
2830
EmitSummaryIndex ? &(AM.getResult<ModuleSummaryIndexAnalysis>(M))
@@ -54,6 +56,8 @@ namespace {
5456
bool runOnModule(Module &M) override {
5557
ScopedDbgInfoFormatSetter FormatSetter(
5658
M, M.IsNewDbgInfoFormat && WriteNewDbgInfoFormatToBitcode);
59+
if (M.IsNewDbgInfoFormat)
60+
M.removeDebugIntrinsicDeclarations();
5761

5862
WriteBitcodeToFile(M, OS, ShouldPreserveUseListOrder, /*Index=*/nullptr,
5963
/*EmitModuleHash=*/false);

llvm/lib/Transforms/IPO/ThinLTOBitcodeWriter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -581,10 +581,10 @@ llvm::ThinLTOBitcodeWriterPass::run(Module &M, ModuleAnalysisManager &AM) {
581581
FunctionAnalysisManager &FAM =
582582
AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
583583

584-
// RemoveDIs: there's no bitcode representation of the DbgVariableRecord
585-
// debug-info, convert to dbg.values before writing out.
586584
ScopedDbgInfoFormatSetter FormatSetter(M, M.IsNewDbgInfoFormat &&
587585
WriteNewDbgInfoFormatToBitcode);
586+
if (M.IsNewDbgInfoFormat)
587+
M.removeDebugIntrinsicDeclarations();
588588

589589
bool Changed = writeThinLTOBitcode(
590590
OS, ThinLinkOS,

llvm/tools/llvm-as/llvm-as.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,10 @@ int main(int argc, char **argv) {
143143

144144
// Convert to new debug format if requested.
145145
assert(!M->IsNewDbgInfoFormat && "Unexpectedly in new debug mode");
146-
if (UseNewDbgInfoFormat && WriteNewDbgInfoFormatToBitcode)
146+
if (UseNewDbgInfoFormat && WriteNewDbgInfoFormatToBitcode) {
147147
M->convertToNewDbgValues();
148+
M->removeDebugIntrinsicDeclarations();
149+
}
148150

149151
std::unique_ptr<ModuleSummaryIndex> Index = std::move(ModuleAndIndex.Index);
150152

llvm/tools/llvm-dis/llvm-dis.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,8 @@ int main(int argc, char **argv) {
259259
if (!DontPrint) {
260260
if (M) {
261261
ScopedDbgInfoFormatSetter FormatSetter(*M, WriteNewDbgInfoFormat);
262+
if (WriteNewDbgInfoFormat)
263+
M->removeDebugIntrinsicDeclarations();
262264
M->print(Out->os(), Annotator.get(), PreserveAssemblyUseListOrder);
263265
}
264266
if (Index)

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ static cl::opt<bool> TryUseNewDbgInfoFormat(
136136

137137
extern cl::opt<bool> UseNewDbgInfoFormat;
138138
extern cl::opt<cl::boolOrDefault> PreserveInputDbgFormat;
139+
extern cl::opt<bool> WriteNewDbgInfoFormat;
140+
extern bool WriteNewDbgInfoFormatToBitcode;
139141

140142
extern cl::opt<cl::boolOrDefault> LoadBitcodeIntoNewDbgInfoFormat;
141143

@@ -545,10 +547,18 @@ int main(int argc, char **argv) {
545547

546548
if (Verbose)
547549
errs() << "Writing bitcode...\n";
550+
auto SetFormat = [&](bool NewFormat) {
551+
Composite->setIsNewDbgInfoFormat(NewFormat);
552+
if (NewFormat)
553+
Composite->removeDebugIntrinsicDeclarations();
554+
};
548555
if (OutputAssembly) {
556+
SetFormat(WriteNewDbgInfoFormat);
549557
Composite->print(Out.os(), nullptr, PreserveAssemblyUseListOrder);
550-
} else if (Force || !CheckBitcodeOutputToConsole(Out.os()))
558+
} else if (Force || !CheckBitcodeOutputToConsole(Out.os())) {
559+
SetFormat(WriteNewDbgInfoFormatToBitcode);
551560
WriteBitcodeToFile(*Composite, Out.os(), PreserveBitcodeUseListOrder);
561+
}
552562

553563
// Declare success.
554564
Out.keep();

llvm/tools/verify-uselistorder/verify-uselistorder.cpp

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ static cl::opt<unsigned>
6868
cl::desc("Number of times to shuffle and verify use-lists"),
6969
cl::init(1), cl::cat(Cat));
7070

71-
extern cl::opt<cl::boolOrDefault> LoadBitcodeIntoNewDbgInfoFormat;
71+
extern cl::opt<cl::boolOrDefault> PreserveInputDbgFormat;
7272

7373
namespace {
7474

@@ -169,9 +169,6 @@ std::unique_ptr<Module> TempFile::readBitcode(LLVMContext &Context) const {
169169
return nullptr;
170170
}
171171

172-
// verify-uselistoder currently only supports old-style debug info mode.
173-
// FIXME: Update mapping code for RemoveDIs.
174-
ModuleOr.get()->setIsNewDbgInfoFormat(false);
175172
return std::move(ModuleOr.get());
176173
}
177174

@@ -181,9 +178,6 @@ std::unique_ptr<Module> TempFile::readAssembly(LLVMContext &Context) const {
181178
std::unique_ptr<Module> M = parseAssemblyFile(Filename, Err, Context);
182179
if (!M.get())
183180
Err.print("verify-uselistorder", errs());
184-
// verify-uselistoder currently only supports old-style debug info mode.
185-
// FIXME: Update mapping code for RemoveDIs.
186-
M->setIsNewDbgInfoFormat(false);
187181
return M;
188182
}
189183

@@ -228,8 +222,15 @@ ValueMapping::ValueMapping(const Module &M) {
228222
map(&I);
229223

230224
// Constants used by instructions.
231-
for (const BasicBlock &BB : F)
232-
for (const Instruction &I : BB)
225+
for (const BasicBlock &BB : F) {
226+
for (const Instruction &I : BB) {
227+
for (const DbgVariableRecord &DVR :
228+
filterDbgVars(I.getDbgRecordRange())) {
229+
for (Value *Op : DVR.location_ops())
230+
map(Op);
231+
if (DVR.isDbgAssign())
232+
map(DVR.getAddress());
233+
}
233234
for (const Value *Op : I.operands()) {
234235
// Look through a metadata wrapper.
235236
if (const auto *MAV = dyn_cast<MetadataAsValue>(Op))
@@ -240,6 +241,8 @@ ValueMapping::ValueMapping(const Module &M) {
240241
isa<InlineAsm>(Op))
241242
map(Op);
242243
}
244+
}
245+
}
243246
}
244247
}
245248

@@ -536,6 +539,7 @@ static void reverseUseLists(Module &M) {
536539
}
537540

538541
int main(int argc, char **argv) {
542+
PreserveInputDbgFormat = cl::boolOrDefault::BOU_TRUE;
539543
InitLLVM X(argc, argv);
540544

541545
// Enable debug stream buffering.
@@ -545,18 +549,11 @@ int main(int argc, char **argv) {
545549
cl::ParseCommandLineOptions(argc, argv,
546550
"llvm tool to verify use-list order\n");
547551

548-
// Do not load bitcode into the new debug info format by default.
549-
if (LoadBitcodeIntoNewDbgInfoFormat == cl::boolOrDefault::BOU_UNSET)
550-
LoadBitcodeIntoNewDbgInfoFormat = cl::boolOrDefault::BOU_FALSE;
551-
552552
LLVMContext Context;
553553
SMDiagnostic Err;
554554

555555
// Load the input module...
556556
std::unique_ptr<Module> M = parseIRFile(InputFilename, Err, Context);
557-
// verify-uselistoder currently only supports old-style debug info mode.
558-
// FIXME: Update mapping code for RemoveDIs.
559-
M->setIsNewDbgInfoFormat(false);
560557

561558
if (!M.get()) {
562559
Err.print(argv[0], errs());

0 commit comments

Comments
 (0)