Skip to content

Commit 20434bf

Browse files
authored
[RemoveDIs][NFC] Add DPLabel class [2/3] (#82376)
Patch 2 of 3 to add llvm.dbg.label support to the RemoveDIs project. The patch stack adds the DPLabel class, which is the RemoveDIs llvm.dbg.label equivalent. 1. Add DbgRecord base class for DPValue and the not-yet-added DPLabel class. -> 2. Add the DPLabel class. 3. Enable dbg.label conversion and add support to passes. This will be used (and tested) in the final patch(es), coming next.
1 parent 307409a commit 20434bf

File tree

3 files changed

+85
-13
lines changed

3 files changed

+85
-13
lines changed

llvm/include/llvm/IR/DebugProgramInstruction.h

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,13 @@ class raw_ostream;
7979
/// deleteRecord
8080
/// clone
8181
/// isIdenticalToWhenDefined
82-
/// isEquivalentTo
8382
/// both print methods
8483
class DbgRecord : public ilist_node<DbgRecord> {
8584
public:
8685
/// Marker that this DbgRecord is linked into.
8786
DPMarker *Marker = nullptr;
8887
/// Subclass discriminator.
89-
enum Kind : uint8_t { ValueKind };
88+
enum Kind : uint8_t { ValueKind, LabelKind };
9089

9190
protected:
9291
DebugLoc DbgLoc;
@@ -104,9 +103,11 @@ class DbgRecord : public ilist_node<DbgRecord> {
104103
void print(raw_ostream &O, bool IsForDebug = false) const;
105104
void print(raw_ostream &O, ModuleSlotTracker &MST, bool IsForDebug) const;
106105
bool isIdenticalToWhenDefined(const DbgRecord &R) const;
107-
bool isEquivalentTo(const DbgRecord &R) const;
108106
///@}
109107

108+
/// Same as isIdenticalToWhenDefined but checks DebugLoc too.
109+
bool isEquivalentTo(const DbgRecord &R) const;
110+
110111
Kind getRecordKind() const { return RecordKind; }
111112

112113
void setMarker(DPMarker *M) { Marker = M; }
@@ -156,6 +157,31 @@ class DbgRecord : public ilist_node<DbgRecord> {
156157
~DbgRecord() = default;
157158
};
158159

160+
/// Records a position in IR for a source label (DILabel). Corresponds to the
161+
/// llvm.dbg.label intrinsic.
162+
/// FIXME: Rename DbgLabelRecord when DPValue is renamed to DbgVariableRecord.
163+
class DPLabel : public DbgRecord {
164+
DILabel *Label;
165+
166+
public:
167+
DPLabel(DILabel *Label, DebugLoc DL)
168+
: DbgRecord(LabelKind, DL), Label(Label) {
169+
assert(Label && "Unexpected nullptr");
170+
}
171+
172+
DPLabel *clone() const;
173+
void print(raw_ostream &O, bool IsForDebug = false) const;
174+
void print(raw_ostream &ROS, ModuleSlotTracker &MST, bool IsForDebug) const;
175+
176+
void setLabel(DILabel *NewLabel) { Label = NewLabel; }
177+
DILabel *getLabel() const { return Label; }
178+
179+
/// Support type inquiry through isa, cast, and dyn_cast.
180+
static bool classof(const DbgRecord *E) {
181+
return E->getRecordKind() == LabelKind;
182+
}
183+
};
184+
159185
/// Record of a variable value-assignment, aka a non instruction representation
160186
/// of the dbg.value intrinsic.
161187
///

llvm/lib/IR/AsmWriter.cpp

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,8 @@ static const Module *getModuleFromDPI(const DPMarker *Marker) {
292292
return M ? M->getParent() : nullptr;
293293
}
294294

295-
static const Module *getModuleFromDPI(const DPValue *DPV) {
296-
return DPV->getMarker() ? getModuleFromDPI(DPV->getMarker()) : nullptr;
295+
static const Module *getModuleFromDPI(const DbgRecord *DR) {
296+
return DR->getMarker() ? getModuleFromDPI(DR->getMarker()) : nullptr;
297297
}
298298

299299
static void PrintCallingConv(unsigned cc, raw_ostream &Out) {
@@ -2699,6 +2699,7 @@ class AssemblyWriter {
26992699
void printInstruction(const Instruction &I);
27002700
void printDPMarker(const DPMarker &DPI);
27012701
void printDPValue(const DPValue &DPI);
2702+
void printDPLabel(const DPLabel &DPL);
27022703
void printDbgRecord(const DbgRecord &DPI);
27032704

27042705
void printUseListOrder(const Value *V, const std::vector<unsigned> &Shuffle);
@@ -4602,8 +4603,10 @@ void AssemblyWriter::printDPMarker(const DPMarker &Marker) {
46024603
void AssemblyWriter::printDbgRecord(const DbgRecord &DR) {
46034604
if (auto *DPV = dyn_cast<DPValue>(&DR))
46044605
printDPValue(*DPV);
4606+
else if (auto *DPL = dyn_cast<DPLabel>(&DR))
4607+
printDPLabel(*DPL);
46054608
else
4606-
llvm_unreachable("unsupported dbg record");
4609+
llvm_unreachable("Unexpected DbgRecord kind");
46074610
}
46084611

46094612
void AssemblyWriter::printDPValue(const DPValue &Value) {
@@ -4645,6 +4648,16 @@ void AssemblyWriter::printDPValue(const DPValue &Value) {
46454648
Out << " }";
46464649
}
46474650

4651+
void AssemblyWriter::printDPLabel(const DPLabel &Label) {
4652+
// There's no formal representation of a DPLabel -- print purely as
4653+
// a debugging aid.
4654+
Out << " DPLabel { ";
4655+
auto WriterCtx = getContext();
4656+
WriteAsOperandInternal(Out, Label.getLabel(), WriterCtx, true);
4657+
Out << " marker @" << Label.getMarker();
4658+
Out << " }";
4659+
}
4660+
46484661
void AssemblyWriter::printMetadataAttachments(
46494662
const SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs,
46504663
StringRef Separator) {
@@ -4908,6 +4921,12 @@ void DPMarker::print(raw_ostream &ROS, ModuleSlotTracker &MST,
49084921
W.printDPMarker(*this);
49094922
}
49104923

4924+
void DPLabel::print(raw_ostream &ROS, bool IsForDebug) const {
4925+
4926+
ModuleSlotTracker MST(getModuleFromDPI(this), true);
4927+
print(ROS, MST, IsForDebug);
4928+
}
4929+
49114930
void DPValue::print(raw_ostream &ROS, ModuleSlotTracker &MST,
49124931
bool IsForDebug) const {
49134932
// There's no formal representation of a DPValue -- print purely as a
@@ -4927,6 +4946,24 @@ void DPValue::print(raw_ostream &ROS, ModuleSlotTracker &MST,
49274946
W.printDPValue(*this);
49284947
}
49294948

4949+
void DPLabel::print(raw_ostream &ROS, ModuleSlotTracker &MST,
4950+
bool IsForDebug) const {
4951+
// There's no formal representation of a DbgLabelRecord -- print purely as
4952+
// a debugging aid.
4953+
formatted_raw_ostream OS(ROS);
4954+
SlotTracker EmptySlotTable(static_cast<const Module *>(nullptr));
4955+
SlotTracker &SlotTable =
4956+
MST.getMachine() ? *MST.getMachine() : EmptySlotTable;
4957+
auto incorporateFunction = [&](const Function *F) {
4958+
if (F)
4959+
MST.incorporateFunction(*F);
4960+
};
4961+
incorporateFunction(Marker->getParent() ? Marker->getParent()->getParent()
4962+
: nullptr);
4963+
AssemblyWriter W(OS, SlotTable, getModuleFromDPI(this), nullptr, IsForDebug);
4964+
W.printDPLabel(*this);
4965+
}
4966+
49304967
void Value::print(raw_ostream &ROS, bool IsForDebug) const {
49314968
bool ShouldInitializeAllMetadata = false;
49324969
if (auto *I = dyn_cast<Instruction>(this))

llvm/lib/IR/DebugProgramInstruction.cpp

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ void DbgRecord::deleteRecord() {
6464
case ValueKind:
6565
delete cast<DPValue>(this);
6666
return;
67+
case LabelKind:
68+
delete cast<DPLabel>(this);
69+
return;
6770
}
6871
llvm_unreachable("unsupported DbgRecord kind");
6972
}
@@ -73,6 +76,9 @@ void DbgRecord::print(raw_ostream &O, bool IsForDebug) const {
7376
case ValueKind:
7477
cast<DPValue>(this)->print(O, IsForDebug);
7578
return;
79+
case LabelKind:
80+
cast<DPLabel>(this)->print(O, IsForDebug);
81+
return;
7682
};
7783
llvm_unreachable("unsupported DbgRecord kind");
7884
}
@@ -83,6 +89,9 @@ void DbgRecord::print(raw_ostream &O, ModuleSlotTracker &MST,
8389
case ValueKind:
8490
cast<DPValue>(this)->print(O, MST, IsForDebug);
8591
return;
92+
case LabelKind:
93+
cast<DPLabel>(this)->print(O, MST, IsForDebug);
94+
return;
8695
};
8796
llvm_unreachable("unsupported DbgRecord kind");
8897
}
@@ -93,18 +102,14 @@ bool DbgRecord::isIdenticalToWhenDefined(const DbgRecord &R) const {
93102
switch (RecordKind) {
94103
case ValueKind:
95104
return cast<DPValue>(this)->isIdenticalToWhenDefined(*cast<DPValue>(&R));
105+
case LabelKind:
106+
return cast<DPLabel>(this)->getLabel() == cast<DPLabel>(R).getLabel();
96107
};
97108
llvm_unreachable("unsupported DbgRecord kind");
98109
}
99110

100111
bool DbgRecord::isEquivalentTo(const DbgRecord &R) const {
101-
if (RecordKind != R.RecordKind)
102-
return false;
103-
switch (RecordKind) {
104-
case ValueKind:
105-
return cast<DPValue>(this)->isEquivalentTo(*cast<DPValue>(&R));
106-
};
107-
llvm_unreachable("unsupported DbgRecord kind");
112+
return getDebugLoc() == R.getDebugLoc() && isIdenticalToWhenDefined(R);
108113
}
109114

110115
DPValue *DPValue::createDPValue(Value *Location, DILocalVariable *DV,
@@ -307,12 +312,16 @@ DbgRecord *DbgRecord::clone() const {
307312
switch (RecordKind) {
308313
case ValueKind:
309314
return cast<DPValue>(this)->clone();
315+
case LabelKind:
316+
return cast<DPLabel>(this)->clone();
310317
};
311318
llvm_unreachable("unsupported DbgRecord kind");
312319
}
313320

314321
DPValue *DPValue::clone() const { return new DPValue(*this); }
315322

323+
DPLabel *DPLabel::clone() const { return new DPLabel(Label, getDebugLoc()); }
324+
316325
DbgVariableIntrinsic *
317326
DPValue::createDebugIntrinsic(Module *M, Instruction *InsertBefore) const {
318327
[[maybe_unused]] DICompileUnit *Unit =

0 commit comments

Comments
 (0)