Skip to content

Commit

Permalink
[RemoveDIs] Make verify-uselistorder preserve the input debug info fo…
Browse files Browse the repository at this point in the history
…rmat (#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.
  • Loading branch information
SLTozer committed Apr 22, 2024
1 parent 5597d97 commit 8e45935
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 20 deletions.
4 changes: 4 additions & 0 deletions llvm/lib/Bitcode/Writer/BitcodeWriterPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ extern bool WriteNewDbgInfoFormatToBitcode;
PreservedAnalyses BitcodeWriterPass::run(Module &M, ModuleAnalysisManager &AM) {
ScopedDbgInfoFormatSetter FormatSetter(M, M.IsNewDbgInfoFormat &&
WriteNewDbgInfoFormatToBitcode);
if (M.IsNewDbgInfoFormat)
M.removeDebugIntrinsicDeclarations();

const ModuleSummaryIndex *Index =
EmitSummaryIndex ? &(AM.getResult<ModuleSummaryIndexAnalysis>(M))
Expand Down Expand Up @@ -54,6 +56,8 @@ namespace {
bool runOnModule(Module &M) override {
ScopedDbgInfoFormatSetter FormatSetter(
M, M.IsNewDbgInfoFormat && WriteNewDbgInfoFormatToBitcode);
if (M.IsNewDbgInfoFormat)
M.removeDebugIntrinsicDeclarations();

WriteBitcodeToFile(M, OS, ShouldPreserveUseListOrder, /*Index=*/nullptr,
/*EmitModuleHash=*/false);
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Transforms/IPO/ThinLTOBitcodeWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -581,10 +581,10 @@ llvm::ThinLTOBitcodeWriterPass::run(Module &M, ModuleAnalysisManager &AM) {
FunctionAnalysisManager &FAM =
AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();

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

bool Changed = writeThinLTOBitcode(
OS, ThinLinkOS,
Expand Down
4 changes: 3 additions & 1 deletion llvm/tools/llvm-as/llvm-as.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,10 @@ int main(int argc, char **argv) {

// Convert to new debug format if requested.
assert(!M->IsNewDbgInfoFormat && "Unexpectedly in new debug mode");
if (UseNewDbgInfoFormat && WriteNewDbgInfoFormatToBitcode)
if (UseNewDbgInfoFormat && WriteNewDbgInfoFormatToBitcode) {
M->convertToNewDbgValues();
M->removeDebugIntrinsicDeclarations();
}

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

Expand Down
2 changes: 2 additions & 0 deletions llvm/tools/llvm-dis/llvm-dis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@ int main(int argc, char **argv) {
if (!DontPrint) {
if (M) {
ScopedDbgInfoFormatSetter FormatSetter(*M, WriteNewDbgInfoFormat);
if (WriteNewDbgInfoFormat)
M->removeDebugIntrinsicDeclarations();
M->print(Out->os(), Annotator.get(), PreserveAssemblyUseListOrder);
}
if (Index)
Expand Down
12 changes: 11 additions & 1 deletion llvm/tools/llvm-link/llvm-link.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ static cl::opt<bool> TryUseNewDbgInfoFormat(

extern cl::opt<bool> UseNewDbgInfoFormat;
extern cl::opt<cl::boolOrDefault> PreserveInputDbgFormat;
extern cl::opt<bool> WriteNewDbgInfoFormat;
extern bool WriteNewDbgInfoFormatToBitcode;

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

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

if (Verbose)
errs() << "Writing bitcode...\n";
auto SetFormat = [&](bool NewFormat) {
Composite->setIsNewDbgInfoFormat(NewFormat);
if (NewFormat)
Composite->removeDebugIntrinsicDeclarations();
};
if (OutputAssembly) {
SetFormat(WriteNewDbgInfoFormat);
Composite->print(Out.os(), nullptr, PreserveAssemblyUseListOrder);
} else if (Force || !CheckBitcodeOutputToConsole(Out.os()))
} else if (Force || !CheckBitcodeOutputToConsole(Out.os())) {
SetFormat(WriteNewDbgInfoFormatToBitcode);
WriteBitcodeToFile(*Composite, Out.os(), PreserveBitcodeUseListOrder);
}

// Declare success.
Out.keep();
Expand Down
29 changes: 13 additions & 16 deletions llvm/tools/verify-uselistorder/verify-uselistorder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ static cl::opt<unsigned>
cl::desc("Number of times to shuffle and verify use-lists"),
cl::init(1), cl::cat(Cat));

extern cl::opt<cl::boolOrDefault> LoadBitcodeIntoNewDbgInfoFormat;
extern cl::opt<cl::boolOrDefault> PreserveInputDbgFormat;

namespace {

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

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

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

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

// Constants used by instructions.
for (const BasicBlock &BB : F)
for (const Instruction &I : BB)
for (const BasicBlock &BB : F) {
for (const Instruction &I : BB) {
for (const DbgVariableRecord &DVR :
filterDbgVars(I.getDbgRecordRange())) {
for (Value *Op : DVR.location_ops())
map(Op);
if (DVR.isDbgAssign())
map(DVR.getAddress());
}
for (const Value *Op : I.operands()) {
// Look through a metadata wrapper.
if (const auto *MAV = dyn_cast<MetadataAsValue>(Op))
Expand All @@ -240,6 +241,8 @@ ValueMapping::ValueMapping(const Module &M) {
isa<InlineAsm>(Op))
map(Op);
}
}
}
}
}

Expand Down Expand Up @@ -536,6 +539,7 @@ static void reverseUseLists(Module &M) {
}

int main(int argc, char **argv) {
PreserveInputDbgFormat = cl::boolOrDefault::BOU_TRUE;
InitLLVM X(argc, argv);

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

// Do not load bitcode into the new debug info format by default.
if (LoadBitcodeIntoNewDbgInfoFormat == cl::boolOrDefault::BOU_UNSET)
LoadBitcodeIntoNewDbgInfoFormat = cl::boolOrDefault::BOU_FALSE;

LLVMContext Context;
SMDiagnostic Err;

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

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

0 comments on commit 8e45935

Please sign in to comment.