From 0aba994a958d12f2fb67ec7ec614a1c1ab884bdb Mon Sep 17 00:00:00 2001 From: OCHyams Date: Mon, 15 Jan 2024 16:25:54 +0000 Subject: [PATCH] [RemoveDIs][NFC] Add DbgLabelRecord class --- .../include/llvm/IR/DebugProgramInstruction.h | 26 ++++++++++++- llvm/lib/IR/AsmWriter.cpp | 39 ++++++++++++++++++- llvm/lib/IR/DebugProgramInstruction.cpp | 11 ++++++ 3 files changed, 73 insertions(+), 3 deletions(-) diff --git a/llvm/include/llvm/IR/DebugProgramInstruction.h b/llvm/include/llvm/IR/DebugProgramInstruction.h index f9b4cdeb3680e..27e7be05aad1c 100644 --- a/llvm/include/llvm/IR/DebugProgramInstruction.h +++ b/llvm/include/llvm/IR/DebugProgramInstruction.h @@ -82,7 +82,7 @@ class DbgRecord : public ilist_node { /// Marker that this DbgRecord is linked into. DbgMarker *Marker = nullptr; /// Subclass discriminator. - enum Kind : uint8_t { ValueKind }; + enum Kind : uint8_t { ValueKind, LabelKind }; protected: DebugLoc DbgLoc; @@ -141,6 +141,30 @@ class DbgRecord : public ilist_node { ~DbgRecord() = default; }; +/// Records a position in IR for a source label (DILabel). Corresponds to the +/// llvm.dbg.label intrinsic. +class DbgLabelRecord : public DbgRecord { + DILabel *Label; + +public: + DbgLabelRecord(DILabel *Label, DebugLoc DL) + : DbgRecord(LabelKind, DL), Label(Label) { + assert(Label && "unexpected nullptr"); + } + + DbgLabelRecord *clone() const; + void print(raw_ostream &O, bool IsForDebug = false) const; + void print(raw_ostream &ROS, ModuleSlotTracker &MST, bool IsForDebug) const; + + void setLabel(DILabel *NewLabel) { Label = NewLabel; } + DILabel *getLabel() const { return Label; } + + /// Support type inquiry through isa, cast, and dyn_cast. + static bool classof(const DbgRecord *E) { + return E->getRecordKind() == LabelKind; + } +}; + /// Record of a variable value-assignment, aka a non instruction representation /// of the dbg.value intrinsic. /// diff --git a/llvm/lib/IR/AsmWriter.cpp b/llvm/lib/IR/AsmWriter.cpp index bbbf8088652bb..fec695d7dacd3 100644 --- a/llvm/lib/IR/AsmWriter.cpp +++ b/llvm/lib/IR/AsmWriter.cpp @@ -292,8 +292,8 @@ static const Module *getModuleFromDPI(const DbgMarker *Marker) { return M ? M->getParent() : nullptr; } -static const Module *getModuleFromDPI(const DbgVariableRecord *DPV) { - return getModuleFromDPI(DPV->getMarker()); +static const Module *getModuleFromDPI(const DbgRecord *DR) { + return getModuleFromDPI(DR->getMarker()); } static void PrintCallingConv(unsigned cc, raw_ostream &Out) { @@ -2667,6 +2667,7 @@ class AssemblyWriter { void printInstruction(const Instruction &I); void printDbgMarker(const DbgMarker &DPI); void printDbgVariableRecord(const DbgVariableRecord &DPI); + void printDbgLabelRecord(const DbgLabelRecord &DPI); void printDbgRecord(const DbgRecord &DPI); void printUseListOrder(const Value *V, const std::vector &Shuffle); @@ -4596,6 +4597,16 @@ void AssemblyWriter::printDbgVariableRecord(const DbgVariableRecord &Value) { Out << " }"; } +void AssemblyWriter::printDbgLabelRecord(const DbgLabelRecord &Value) { + // There's no formal representation of a DbgLabelRecord -- print purely as + // a debugging aid. + Out << " DbgLabelRecord { "; + auto WriterCtx = getContext(); + WriteAsOperandInternal(Out, Value.getLabel(), WriterCtx, true); + Out << " marker @" << Value.getMarker(); + Out << " }"; +} + void AssemblyWriter::printMetadataAttachments( const SmallVectorImpl> &MDs, StringRef Separator) { @@ -4842,6 +4853,12 @@ void DbgVariableRecord::print(raw_ostream &ROS, bool IsForDebug) const { print(ROS, MST, IsForDebug); } +void DbgLabelRecord::print(raw_ostream &ROS, bool IsForDebug) const { + + ModuleSlotTracker MST(getModuleFromDPI(this), true); + print(ROS, MST, IsForDebug); +} + void DbgMarker::print(raw_ostream &ROS, ModuleSlotTracker &MST, bool IsForDebug) const { // There's no formal representation of a DbgMarker -- print purely as a @@ -4877,6 +4894,24 @@ void DbgVariableRecord::print(raw_ostream &ROS, ModuleSlotTracker &MST, W.printDbgVariableRecord(*this); } +void DbgLabelRecord::print(raw_ostream &ROS, ModuleSlotTracker &MST, + bool IsForDebug) const { + // There's no formal representation of a DbgLabelRecord -- print purely as + // a debugging aid. + formatted_raw_ostream OS(ROS); + SlotTracker EmptySlotTable(static_cast(nullptr)); + SlotTracker &SlotTable = + MST.getMachine() ? *MST.getMachine() : EmptySlotTable; + auto incorporateFunction = [&](const Function *F) { + if (F) + MST.incorporateFunction(*F); + }; + incorporateFunction(Marker->getParent() ? Marker->getParent()->getParent() + : nullptr); + AssemblyWriter W(OS, SlotTable, getModuleFromDPI(this), nullptr, IsForDebug); + W.printDbgLabelRecord(*this); +} + void Value::print(raw_ostream &ROS, bool IsForDebug) const { bool ShouldInitializeAllMetadata = false; if (auto *I = dyn_cast(this)) diff --git a/llvm/lib/IR/DebugProgramInstruction.cpp b/llvm/lib/IR/DebugProgramInstruction.cpp index cb0ad6be2771f..27b10a55b2581 100644 --- a/llvm/lib/IR/DebugProgramInstruction.cpp +++ b/llvm/lib/IR/DebugProgramInstruction.cpp @@ -46,6 +46,9 @@ void DbgRecord::deleteRecord() { case ValueKind: delete cast(this); break; + case LabelKind: + delete cast(this); + break; default: llvm_unreachable("unsupported record kind"); } @@ -55,6 +58,9 @@ void DbgRecord::print(raw_ostream &O, bool IsForDebug) const { case ValueKind: cast(this)->print(O, IsForDebug); break; + case LabelKind: + cast(this)->print(O, IsForDebug); + break; default: llvm_unreachable("unsupported record kind"); }; @@ -66,6 +72,9 @@ void DbgRecord::print(raw_ostream &O, ModuleSlotTracker &MST, case ValueKind: cast(this)->print(O, MST, IsForDebug); break; + case LabelKind: + cast(this)->print(O, MST, IsForDebug); + break; default: llvm_unreachable("unsupported record kind"); }; @@ -217,6 +226,8 @@ DbgRecord *DbgRecord::clone() const { switch (RecordKind) { case ValueKind: return cast(this)->clone(); + case LabelKind: + return cast(this)->clone(); default: llvm_unreachable("unsupported record kind"); };