Skip to content

[RemoveDIs][NFC] Use ScopedDbgInfoFormatSetter in more places #87380

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions llvm/include/llvm/IR/DebugProgramInstruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,25 @@ getDbgRecordRange(DbgMarker *DebugMarker) {

DEFINE_ISA_CONVERSION_FUNCTIONS(DbgRecord, LLVMDbgRecordRef)

/// Used to temporarily set the debug info format of a function, module, or
/// basic block for the duration of this object's lifetime, after which the
/// prior state will be restored.
template <typename T> class ScopedDbgInfoFormatSetter {
T &Obj;
bool OldState;

public:
ScopedDbgInfoFormatSetter(T &Obj, bool NewState)
: Obj(Obj), OldState(Obj.IsNewDbgInfoFormat) {
Obj.setIsNewDbgInfoFormat(NewState);
}
~ScopedDbgInfoFormatSetter() { Obj.setIsNewDbgInfoFormat(OldState); }
};

template <typename T>
ScopedDbgInfoFormatSetter(T &Obj,
bool NewState) -> ScopedDbgInfoFormatSetter<T>;

} // namespace llvm

#endif // LLVM_IR_DEBUGPROGRAMINSTRUCTION_H
24 changes: 1 addition & 23 deletions llvm/include/llvm/IR/PassManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,6 @@ extern llvm::cl::opt<bool> UseNewDbgInfoFormat;

namespace llvm {

// RemoveDIs: Provide facilities for converting debug-info from one form to
// another, which are no-ops for everything but modules.
template <class IRUnitT> inline bool shouldConvertDbgInfo(IRUnitT &IR) {
return false;
}
template <> inline bool shouldConvertDbgInfo(Module &IR) {
return !IR.IsNewDbgInfoFormat && UseNewDbgInfoFormat;
}
template <class IRUnitT> inline void doConvertDbgInfoToNew(IRUnitT &IR) {}
template <> inline void doConvertDbgInfoToNew(Module &IR) {
IR.convertToNewDbgValues();
}
template <class IRUnitT> inline void doConvertDebugInfoToOld(IRUnitT &IR) {}
template <> inline void doConvertDebugInfoToOld(Module &IR) {
IR.convertFromNewDbgValues();
}

// Forward declare the analysis manager template.
template <typename IRUnitT, typename... ExtraArgTs> class AnalysisManager;

Expand Down Expand Up @@ -229,9 +212,7 @@ class PassManager : public PassInfoMixin<

// RemoveDIs: if requested, convert debug-info to DbgRecord representation
// for duration of these passes.
bool ShouldConvertDbgInfo = shouldConvertDbgInfo(IR);
if (ShouldConvertDbgInfo)
doConvertDbgInfoToNew(IR);
ScopedDbgInfoFormatSetter FormatSetter(IR, UseNewDbgInfoFormat);

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

if (ShouldConvertDbgInfo)
doConvertDebugInfoToOld(IR);

// Invalidation was handled after each pass in the above loop for the
// current unit of IR. Therefore, the remaining analysis results in the
// AnalysisManager are preserved. We mark this with a set so that we don't
Expand Down
19 changes: 0 additions & 19 deletions llvm/include/llvm/IR/PrintPasses.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,25 +78,6 @@ std::string doSystemDiff(StringRef Before, StringRef After,
StringRef OldLineFormat, StringRef NewLineFormat,
StringRef UnchangedLineFormat);

/// Used to temporarily set the debug info format of a function, module, or
/// basic block for the duration of this object's lifetime, after which the
/// prior state will be restored.
template <typename T> class ScopedDbgInfoFormatSetter {
T &Obj;
bool OldState;

public:
ScopedDbgInfoFormatSetter(T &Obj, bool NewState)
: Obj(Obj), OldState(Obj.IsNewDbgInfoFormat) {
Obj.setIsNewDbgInfoFormat(NewState);
}
~ScopedDbgInfoFormatSetter() { Obj.setIsNewDbgInfoFormat(OldState); }
};

template <typename T>
ScopedDbgInfoFormatSetter(T &Obj, bool NewState)
-> ScopedDbgInfoFormatSetter<T>;

} // namespace llvm

#endif // LLVM_IR_PRINTPASSES_H
17 changes: 4 additions & 13 deletions llvm/lib/Bitcode/Writer/BitcodeWriterPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,14 @@ using namespace llvm;
extern bool WriteNewDbgInfoFormatToBitcode;

PreservedAnalyses BitcodeWriterPass::run(Module &M, ModuleAnalysisManager &AM) {
bool ConvertToOldDbgFormatForWrite =
M.IsNewDbgInfoFormat && !WriteNewDbgInfoFormatToBitcode;
if (ConvertToOldDbgFormatForWrite)
M.convertFromNewDbgValues();
ScopedDbgInfoFormatSetter FormatSetter(M, M.IsNewDbgInfoFormat &&
WriteNewDbgInfoFormatToBitcode);

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

if (ConvertToOldDbgFormatForWrite)
M.convertToNewDbgValues();

return PreservedAnalyses::all();
}

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

bool runOnModule(Module &M) override {
bool ConvertToOldDbgFormatForWrite =
M.IsNewDbgInfoFormat && !WriteNewDbgInfoFormatToBitcode;
if (ConvertToOldDbgFormatForWrite)
M.convertFromNewDbgValues();
ScopedDbgInfoFormatSetter FormatSetter(
M, M.IsNewDbgInfoFormat && WriteNewDbgInfoFormatToBitcode);

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

if (ConvertToOldDbgFormatForWrite)
M.convertToNewDbgValues();
return false;
}
void getAnalysisUsage(AnalysisUsage &AU) const override {
Expand Down
20 changes: 6 additions & 14 deletions llvm/lib/CodeGen/MIRPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ static cl::opt<bool> SimplifyMIR(
static cl::opt<bool> PrintLocations("mir-debug-loc", cl::Hidden, cl::init(true),
cl::desc("Print MIR debug-locations"));

extern cl::opt<bool> WriteNewDbgInfoFormat;

namespace {

/// This structure describes how to print out stack object references.
Expand Down Expand Up @@ -982,29 +984,19 @@ void MIRFormatter::printIRValue(raw_ostream &OS, const Value &V,
}

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

yaml::Output Out(OS);
Out << const_cast<Module &>(M);

if (IsNewDbgInfoFormat)
const_cast<Module &>(M).convertToNewDbgValues();
}

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

MIRPrinter Printer(OS);
Printer.print(MF);

if (IsNewDbgInfoFormat)
const_cast<Function &>(MF.getFunction()).convertToNewDbgValues();
}
7 changes: 1 addition & 6 deletions llvm/lib/IR/LegacyPassManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -531,9 +531,7 @@ bool PassManagerImpl::run(Module &M) {
// RemoveDIs: if a command line flag is given, convert to the
// DbgVariableRecord representation of debug-info for the duration of these
// passes.
bool shouldConvertDbgInfo = UseNewDbgInfoFormat && !M.IsNewDbgInfoFormat;
if (shouldConvertDbgInfo)
M.convertToNewDbgValues();
ScopedDbgInfoFormatSetter FormatSetter(M, UseNewDbgInfoFormat);

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

if (shouldConvertDbgInfo)
M.convertFromNewDbgValues();

return Changed;
}
} // namespace legacy
Expand Down
21 changes: 3 additions & 18 deletions llvm/lib/Linker/IRMover.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1548,25 +1548,10 @@ Error IRLinker::run() {
return Err;

// Convert source module to match dest for the duration of the link.
bool SrcModuleNewDbgFormat = SrcM->IsNewDbgInfoFormat;
if (DstM.IsNewDbgInfoFormat != SrcM->IsNewDbgInfoFormat) {
if (DstM.IsNewDbgInfoFormat)
SrcM->convertToNewDbgValues();
else
SrcM->convertFromNewDbgValues();
}
// Undo debug mode conversion afterwards.
auto Cleanup = make_scope_exit([&]() {
if (SrcModuleNewDbgFormat != SrcM->IsNewDbgInfoFormat) {
if (SrcModuleNewDbgFormat)
SrcM->convertToNewDbgValues();
else
SrcM->convertFromNewDbgValues();
}
});
ScopedDbgInfoFormatSetter FormatSetter(*SrcM, DstM.IsNewDbgInfoFormat);

// Inherit the target data from the source module if the destination module
// doesn't have one already.
// Inherit the target data from the source module if the destination
// module doesn't have one already.
if (DstM.getDataLayout().isDefault())
DstM.setDataLayout(SrcM->getDataLayout());

Expand Down
9 changes: 2 additions & 7 deletions llvm/lib/Transforms/IPO/ThinLTOBitcodeWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -583,10 +583,8 @@ llvm::ThinLTOBitcodeWriterPass::run(Module &M, ModuleAnalysisManager &AM) {

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

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

if (ConvertToOldDbgFormatForWrite)
M.convertToNewDbgValues();

return Changed ? PreservedAnalyses::none() : PreservedAnalyses::all();
}
6 changes: 1 addition & 5 deletions llvm/tools/llvm-dis/llvm-dis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,12 +252,8 @@ int main(int argc, char **argv) {
// All that llvm-dis does is write the assembly to a file.
if (!DontPrint) {
if (M) {
bool ChangeDbgFormat = M->IsNewDbgInfoFormat != WriteNewDbgInfoFormat;
if (ChangeDbgFormat)
M->setIsNewDbgInfoFormat(WriteNewDbgInfoFormat);
ScopedDbgInfoFormatSetter FormatSetter(*M, WriteNewDbgInfoFormat);
M->print(Out->os(), Annotator.get(), PreserveAssemblyUseListOrder);
if (ChangeDbgFormat)
M->setIsNewDbgInfoFormat(!WriteNewDbgInfoFormat);
}
if (Index)
Index->print(Out->os());
Expand Down