Skip to content

Commit 708ce85

Browse files
authored
[RemoveDIs][NFC] Use ScopedDbgInfoFormatSetter in more places (#87380)
The class `ScopedDbgInfoFormatSetter` was added as a convenient way to temporarily change the debug info format of a function or module, as part of IR printing; since this process is repeated in a number of other places, this patch uses the format-setter class in those places as well.
1 parent 3cf539f commit 708ce85

File tree

9 files changed

+37
-105
lines changed

9 files changed

+37
-105
lines changed

llvm/include/llvm/IR/DebugProgramInstruction.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,25 @@ getDbgRecordRange(DbgMarker *DebugMarker) {
659659

660660
DEFINE_ISA_CONVERSION_FUNCTIONS(DbgRecord, LLVMDbgRecordRef)
661661

662+
/// Used to temporarily set the debug info format of a function, module, or
663+
/// basic block for the duration of this object's lifetime, after which the
664+
/// prior state will be restored.
665+
template <typename T> class ScopedDbgInfoFormatSetter {
666+
T &Obj;
667+
bool OldState;
668+
669+
public:
670+
ScopedDbgInfoFormatSetter(T &Obj, bool NewState)
671+
: Obj(Obj), OldState(Obj.IsNewDbgInfoFormat) {
672+
Obj.setIsNewDbgInfoFormat(NewState);
673+
}
674+
~ScopedDbgInfoFormatSetter() { Obj.setIsNewDbgInfoFormat(OldState); }
675+
};
676+
677+
template <typename T>
678+
ScopedDbgInfoFormatSetter(T &Obj,
679+
bool NewState) -> ScopedDbgInfoFormatSetter<T>;
680+
662681
} // namespace llvm
663682

664683
#endif // LLVM_IR_DEBUGPROGRAMINSTRUCTION_H

llvm/include/llvm/IR/PassManager.h

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -64,23 +64,6 @@ extern llvm::cl::opt<bool> UseNewDbgInfoFormat;
6464

6565
namespace llvm {
6666

67-
// RemoveDIs: Provide facilities for converting debug-info from one form to
68-
// another, which are no-ops for everything but modules.
69-
template <class IRUnitT> inline bool shouldConvertDbgInfo(IRUnitT &IR) {
70-
return false;
71-
}
72-
template <> inline bool shouldConvertDbgInfo(Module &IR) {
73-
return !IR.IsNewDbgInfoFormat && UseNewDbgInfoFormat;
74-
}
75-
template <class IRUnitT> inline void doConvertDbgInfoToNew(IRUnitT &IR) {}
76-
template <> inline void doConvertDbgInfoToNew(Module &IR) {
77-
IR.convertToNewDbgValues();
78-
}
79-
template <class IRUnitT> inline void doConvertDebugInfoToOld(IRUnitT &IR) {}
80-
template <> inline void doConvertDebugInfoToOld(Module &IR) {
81-
IR.convertFromNewDbgValues();
82-
}
83-
8467
// Forward declare the analysis manager template.
8568
template <typename IRUnitT, typename... ExtraArgTs> class AnalysisManager;
8669

@@ -229,9 +212,7 @@ class PassManager : public PassInfoMixin<
229212

230213
// RemoveDIs: if requested, convert debug-info to DbgRecord representation
231214
// for duration of these passes.
232-
bool ShouldConvertDbgInfo = shouldConvertDbgInfo(IR);
233-
if (ShouldConvertDbgInfo)
234-
doConvertDbgInfoToNew(IR);
215+
ScopedDbgInfoFormatSetter FormatSetter(IR, UseNewDbgInfoFormat);
235216

236217
for (auto &Pass : Passes) {
237218
// Check the PassInstrumentation's BeforePass callbacks before running the
@@ -255,9 +236,6 @@ class PassManager : public PassInfoMixin<
255236
PA.intersect(std::move(PassPA));
256237
}
257238

258-
if (ShouldConvertDbgInfo)
259-
doConvertDebugInfoToOld(IR);
260-
261239
// Invalidation was handled after each pass in the above loop for the
262240
// current unit of IR. Therefore, the remaining analysis results in the
263241
// AnalysisManager are preserved. We mark this with a set so that we don't

llvm/include/llvm/IR/PrintPasses.h

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -78,25 +78,6 @@ std::string doSystemDiff(StringRef Before, StringRef After,
7878
StringRef OldLineFormat, StringRef NewLineFormat,
7979
StringRef UnchangedLineFormat);
8080

81-
/// Used to temporarily set the debug info format of a function, module, or
82-
/// basic block for the duration of this object's lifetime, after which the
83-
/// prior state will be restored.
84-
template <typename T> class ScopedDbgInfoFormatSetter {
85-
T &Obj;
86-
bool OldState;
87-
88-
public:
89-
ScopedDbgInfoFormatSetter(T &Obj, bool NewState)
90-
: Obj(Obj), OldState(Obj.IsNewDbgInfoFormat) {
91-
Obj.setIsNewDbgInfoFormat(NewState);
92-
}
93-
~ScopedDbgInfoFormatSetter() { Obj.setIsNewDbgInfoFormat(OldState); }
94-
};
95-
96-
template <typename T>
97-
ScopedDbgInfoFormatSetter(T &Obj, bool NewState)
98-
-> ScopedDbgInfoFormatSetter<T>;
99-
10081
} // namespace llvm
10182

10283
#endif // LLVM_IR_PRINTPASSES_H

llvm/lib/Bitcode/Writer/BitcodeWriterPass.cpp

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,14 @@ using namespace llvm;
2121
extern bool WriteNewDbgInfoFormatToBitcode;
2222

2323
PreservedAnalyses BitcodeWriterPass::run(Module &M, ModuleAnalysisManager &AM) {
24-
bool ConvertToOldDbgFormatForWrite =
25-
M.IsNewDbgInfoFormat && !WriteNewDbgInfoFormatToBitcode;
26-
if (ConvertToOldDbgFormatForWrite)
27-
M.convertFromNewDbgValues();
24+
ScopedDbgInfoFormatSetter FormatSetter(M, M.IsNewDbgInfoFormat &&
25+
WriteNewDbgInfoFormatToBitcode);
2826

2927
const ModuleSummaryIndex *Index =
3028
EmitSummaryIndex ? &(AM.getResult<ModuleSummaryIndexAnalysis>(M))
3129
: nullptr;
3230
WriteBitcodeToFile(M, OS, ShouldPreserveUseListOrder, Index, EmitModuleHash);
3331

34-
if (ConvertToOldDbgFormatForWrite)
35-
M.convertToNewDbgValues();
36-
3732
return PreservedAnalyses::all();
3833
}
3934

@@ -57,16 +52,12 @@ namespace {
5752
StringRef getPassName() const override { return "Bitcode Writer"; }
5853

5954
bool runOnModule(Module &M) override {
60-
bool ConvertToOldDbgFormatForWrite =
61-
M.IsNewDbgInfoFormat && !WriteNewDbgInfoFormatToBitcode;
62-
if (ConvertToOldDbgFormatForWrite)
63-
M.convertFromNewDbgValues();
55+
ScopedDbgInfoFormatSetter FormatSetter(
56+
M, M.IsNewDbgInfoFormat && WriteNewDbgInfoFormatToBitcode);
6457

6558
WriteBitcodeToFile(M, OS, ShouldPreserveUseListOrder, /*Index=*/nullptr,
6659
/*EmitModuleHash=*/false);
6760

68-
if (ConvertToOldDbgFormatForWrite)
69-
M.convertToNewDbgValues();
7061
return false;
7162
}
7263
void getAnalysisUsage(AnalysisUsage &AU) const override {

llvm/lib/CodeGen/MIRPrinter.cpp

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ static cl::opt<bool> SimplifyMIR(
6969
static cl::opt<bool> PrintLocations("mir-debug-loc", cl::Hidden, cl::init(true),
7070
cl::desc("Print MIR debug-locations"));
7171

72+
extern cl::opt<bool> WriteNewDbgInfoFormat;
73+
7274
namespace {
7375

7476
/// This structure describes how to print out stack object references.
@@ -986,29 +988,19 @@ void MIRFormatter::printIRValue(raw_ostream &OS, const Value &V,
986988
}
987989

988990
void llvm::printMIR(raw_ostream &OS, const Module &M) {
989-
// RemoveDIs: as there's no textual form for DbgRecords yet, print debug-info
990-
// in dbg.value format.
991-
bool IsNewDbgInfoFormat = M.IsNewDbgInfoFormat;
992-
if (IsNewDbgInfoFormat)
993-
const_cast<Module &>(M).convertFromNewDbgValues();
991+
ScopedDbgInfoFormatSetter FormatSetter(const_cast<Module &>(M),
992+
WriteNewDbgInfoFormat);
994993

995994
yaml::Output Out(OS);
996995
Out << const_cast<Module &>(M);
997-
998-
if (IsNewDbgInfoFormat)
999-
const_cast<Module &>(M).convertToNewDbgValues();
1000996
}
1001997

1002998
void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) {
1003999
// RemoveDIs: as there's no textual form for DbgRecords yet, print debug-info
10041000
// in dbg.value format.
1005-
bool IsNewDbgInfoFormat = MF.getFunction().IsNewDbgInfoFormat;
1006-
if (IsNewDbgInfoFormat)
1007-
const_cast<Function &>(MF.getFunction()).convertFromNewDbgValues();
1001+
ScopedDbgInfoFormatSetter FormatSetter(
1002+
const_cast<Function &>(MF.getFunction()), WriteNewDbgInfoFormat);
10081003

10091004
MIRPrinter Printer(OS);
10101005
Printer.print(MF);
1011-
1012-
if (IsNewDbgInfoFormat)
1013-
const_cast<Function &>(MF.getFunction()).convertToNewDbgValues();
10141006
}

llvm/lib/IR/LegacyPassManager.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -531,9 +531,7 @@ bool PassManagerImpl::run(Module &M) {
531531
// RemoveDIs: if a command line flag is given, convert to the
532532
// DbgVariableRecord representation of debug-info for the duration of these
533533
// passes.
534-
bool shouldConvertDbgInfo = UseNewDbgInfoFormat && !M.IsNewDbgInfoFormat;
535-
if (shouldConvertDbgInfo)
536-
M.convertToNewDbgValues();
534+
ScopedDbgInfoFormatSetter FormatSetter(M, UseNewDbgInfoFormat);
537535

538536
for (ImmutablePass *ImPass : getImmutablePasses())
539537
Changed |= ImPass->doInitialization(M);
@@ -547,9 +545,6 @@ bool PassManagerImpl::run(Module &M) {
547545
for (ImmutablePass *ImPass : getImmutablePasses())
548546
Changed |= ImPass->doFinalization(M);
549547

550-
if (shouldConvertDbgInfo)
551-
M.convertFromNewDbgValues();
552-
553548
return Changed;
554549
}
555550
} // namespace legacy

llvm/lib/Linker/IRMover.cpp

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1548,25 +1548,10 @@ Error IRLinker::run() {
15481548
return Err;
15491549

15501550
// Convert source module to match dest for the duration of the link.
1551-
bool SrcModuleNewDbgFormat = SrcM->IsNewDbgInfoFormat;
1552-
if (DstM.IsNewDbgInfoFormat != SrcM->IsNewDbgInfoFormat) {
1553-
if (DstM.IsNewDbgInfoFormat)
1554-
SrcM->convertToNewDbgValues();
1555-
else
1556-
SrcM->convertFromNewDbgValues();
1557-
}
1558-
// Undo debug mode conversion afterwards.
1559-
auto Cleanup = make_scope_exit([&]() {
1560-
if (SrcModuleNewDbgFormat != SrcM->IsNewDbgInfoFormat) {
1561-
if (SrcModuleNewDbgFormat)
1562-
SrcM->convertToNewDbgValues();
1563-
else
1564-
SrcM->convertFromNewDbgValues();
1565-
}
1566-
});
1551+
ScopedDbgInfoFormatSetter FormatSetter(*SrcM, DstM.IsNewDbgInfoFormat);
15671552

1568-
// Inherit the target data from the source module if the destination module
1569-
// doesn't have one already.
1553+
// Inherit the target data from the source module if the destination
1554+
// module doesn't have one already.
15701555
if (DstM.getDataLayout().isDefault())
15711556
DstM.setDataLayout(SrcM->getDataLayout());
15721557

llvm/lib/Transforms/IPO/ThinLTOBitcodeWriter.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -583,10 +583,8 @@ llvm::ThinLTOBitcodeWriterPass::run(Module &M, ModuleAnalysisManager &AM) {
583583

584584
// RemoveDIs: there's no bitcode representation of the DbgVariableRecord
585585
// debug-info, convert to dbg.values before writing out.
586-
bool ConvertToOldDbgFormatForWrite =
587-
M.IsNewDbgInfoFormat && !WriteNewDbgInfoFormatToBitcode;
588-
if (ConvertToOldDbgFormatForWrite)
589-
M.convertFromNewDbgValues();
586+
ScopedDbgInfoFormatSetter FormatSetter(M, M.IsNewDbgInfoFormat &&
587+
WriteNewDbgInfoFormatToBitcode);
590588

591589
bool Changed = writeThinLTOBitcode(
592590
OS, ThinLinkOS,
@@ -595,8 +593,5 @@ llvm::ThinLTOBitcodeWriterPass::run(Module &M, ModuleAnalysisManager &AM) {
595593
},
596594
M, &AM.getResult<ModuleSummaryIndexAnalysis>(M));
597595

598-
if (ConvertToOldDbgFormatForWrite)
599-
M.convertToNewDbgValues();
600-
601596
return Changed ? PreservedAnalyses::none() : PreservedAnalyses::all();
602597
}

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -258,12 +258,8 @@ int main(int argc, char **argv) {
258258
// All that llvm-dis does is write the assembly to a file.
259259
if (!DontPrint) {
260260
if (M) {
261-
bool ChangeDbgFormat = M->IsNewDbgInfoFormat != WriteNewDbgInfoFormat;
262-
if (ChangeDbgFormat)
263-
M->setIsNewDbgInfoFormat(WriteNewDbgInfoFormat);
261+
ScopedDbgInfoFormatSetter FormatSetter(*M, WriteNewDbgInfoFormat);
264262
M->print(Out->os(), Annotator.get(), PreserveAssemblyUseListOrder);
265-
if (ChangeDbgFormat)
266-
M->setIsNewDbgInfoFormat(!WriteNewDbgInfoFormat);
267263
}
268264
if (Index)
269265
Index->print(Out->os());

0 commit comments

Comments
 (0)