Skip to content

Commit 2e929cb

Browse files
committed
Parser fixes for DPLabel
1 parent ac969d3 commit 2e929cb

File tree

7 files changed

+80
-25
lines changed

7 files changed

+80
-25
lines changed

llvm/include/llvm/AsmParser/LLParser.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ namespace llvm {
576576
bool parseMDNodeTail(MDNode *&N);
577577
bool parseMDNodeVector(SmallVectorImpl<Metadata *> &Elts);
578578
bool parseMetadataAttachment(unsigned &Kind, MDNode *&MD);
579-
bool parseDebugProgramValue(DPValue *&DPV, PerFunctionState &PFS);
579+
bool parseDebugRecord(DbgRecord *&DR, PerFunctionState &PFS);
580580
bool parseInstructionMetadata(Instruction &Inst);
581581
bool parseGlobalObjectMetadataAttachment(GlobalObject &GO);
582582
bool parseOptionalFunctionMetadata(Function &F);

llvm/include/llvm/IR/DebugProgramInstruction.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,18 @@ inline raw_ostream &operator<<(raw_ostream &OS, const DbgRecord &R) {
223223
class DPLabel : public DbgRecord {
224224
DbgRecordParamRef<DILabel> Label;
225225

226+
/// This constructor intentionally left private, so that it is only called via
227+
/// "createUnresolvedDPLabel", which clearly expresses that it is for parsing
228+
/// only.
229+
DPLabel(MDNode *Label, MDNode *DL);
226230
public:
227231
DPLabel(DILabel *Label, DebugLoc DL);
228232

233+
/// For use during parsing; creates a DPLabel from as-of-yet unresolved
234+
/// MDNodes. Trying to access the resulting DPLabel's fields before they are
235+
/// resolved, or if they resolve to the wrong type, will result in a crash.
236+
static DPLabel *createUnresolvedDPLabel(MDNode *Label, MDNode *DL);
237+
229238
DPLabel *clone() const;
230239
void print(raw_ostream &O, bool IsForDebug = false) const;
231240
void print(raw_ostream &ROS, ModuleSlotTracker &MST, bool IsForDebug) const;

llvm/lib/AsmParser/LLLexer.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,7 @@ lltok::Kind LLLexer::LexIdentifier() {
938938
DBGRECORDTYPEKEYWORD(value);
939939
DBGRECORDTYPEKEYWORD(declare);
940940
DBGRECORDTYPEKEYWORD(assign);
941+
DBGRECORDTYPEKEYWORD(label);
941942
#undef DBGRECORDTYPEKEYWORD
942943

943944
if (Keyword.starts_with("DIFlag")) {

llvm/lib/AsmParser/LLParser.cpp

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6422,7 +6422,11 @@ bool LLParser::parseBasicBlock(PerFunctionState &PFS) {
64226422
// Parse the instructions and debug values in this block until we get a
64236423
// terminator.
64246424
Instruction *Inst;
6425-
SmallVector<std::unique_ptr<DPValue>> TrailingDPValues;
6425+
auto DeleteDbgRecord = [](DbgRecord *DR) {
6426+
DR->deleteRecord();
6427+
};
6428+
using DbgRecordPtr = std::unique_ptr<DbgRecord, decltype(DeleteDbgRecord)>;
6429+
SmallVector<DbgRecordPtr> TrailingDbgRecord;
64266430
do {
64276431
// Handle debug records first - there should always be an instruction
64286432
// following the debug records, i.e. they cannot appear after the block
@@ -6436,10 +6440,10 @@ bool LLParser::parseBasicBlock(PerFunctionState &PFS) {
64366440
if (!M->IsNewDbgInfoFormat)
64376441
M->convertToNewDbgValues();
64386442

6439-
DPValue *DPV;
6440-
if (parseDebugProgramValue(DPV, PFS))
6443+
DbgRecord *DR;
6444+
if (parseDebugRecord(DR, PFS))
64416445
return true;
6442-
TrailingDPValues.emplace_back(DPV);
6446+
TrailingDbgRecord.emplace_back(DR, DeleteDbgRecord);
64436447
}
64446448

64456449
// This instruction may have three possibilities for a name: a) none
@@ -6488,35 +6492,58 @@ bool LLParser::parseBasicBlock(PerFunctionState &PFS) {
64886492
return true;
64896493

64906494
// Attach any preceding debug values to this instruction.
6491-
for (std::unique_ptr<DPValue> &DPV : TrailingDPValues)
6492-
BB->insertDPValueBefore(DPV.release(), Inst->getIterator());
6493-
TrailingDPValues.clear();
6495+
for (DbgRecordPtr &DR : TrailingDbgRecord)
6496+
BB->insertDPValueBefore(DR.release(), Inst->getIterator());
6497+
TrailingDbgRecord.clear();
64946498
} while (!Inst->isTerminator());
64956499

6496-
assert(TrailingDPValues.empty() &&
6500+
assert(TrailingDbgRecord.empty() &&
64976501
"All debug values should have been attached to an instruction.");
64986502

64996503
return false;
65006504
}
65016505

6502-
/// parseDebugProgramValue
6506+
/// parseDebugRecord
6507+
/// ::= #dbg_label '(' MDNode ')'
65036508
/// ::= #dbg_type '(' Metadata ',' MDNode ',' Metadata ','
65046509
/// (MDNode ',' Metadata ',' Metadata ',')? MDNode ')'
6505-
bool LLParser::parseDebugProgramValue(DPValue *&DPV, PerFunctionState &PFS) {
6510+
bool LLParser::parseDebugRecord(DbgRecord *&DR, PerFunctionState &PFS) {
6511+
using RecordKind = DbgRecord::Kind;
65066512
using LocType = DPValue::LocationType;
65076513
LocTy DPVLoc = Lex.getLoc();
65086514
if (Lex.getKind() != lltok::DbgRecordType)
65096515
return error(DPVLoc, "expected debug record type here");
6510-
auto Type = StringSwitch<LocType>(Lex.getStrVal())
6516+
RecordKind RecordType = StringSwitch<RecordKind>(Lex.getStrVal())
6517+
.Case("declare", RecordKind::ValueKind)
6518+
.Case("value", RecordKind::ValueKind)
6519+
.Case("assign", RecordKind::ValueKind)
6520+
.Case("label", RecordKind::LabelKind);
6521+
6522+
// Parsing labels is trivial; parse here and early exit, otherwise go into the
6523+
// full DPValue processing stage.
6524+
if (RecordType == RecordKind::LabelKind) {
6525+
Lex.Lex();
6526+
if (parseToken(lltok::lparen, "Expected '(' here"))
6527+
return true;
6528+
MDNode *Label;
6529+
if (parseMDNode(Label))
6530+
return true;
6531+
if (parseToken(lltok::comma, "Expected ',' here"))
6532+
return true;
6533+
MDNode *DbgLoc;
6534+
if (parseMDNode(DbgLoc))
6535+
return true;
6536+
if (parseToken(lltok::rparen, "Expected ')' here"))
6537+
return true;
6538+
DR = DPLabel::createUnresolvedDPLabel(Label, DbgLoc);
6539+
return false;
6540+
}
6541+
6542+
LocType ValueType = StringSwitch<LocType>(Lex.getStrVal())
65116543
.Case("declare", LocType::Declare)
65126544
.Case("value", LocType::Value)
6513-
.Case("assign", LocType::Assign)
6514-
.Default(LocType::End);
6515-
// If the file contained an invalid debug record type then parsing should fail
6516-
// above; the assert here should only fire if the Lexer gives us an invalid
6517-
// value.
6518-
assert(Type != LocType::End &&
6519-
"Lexer returned an invalid DbgRecordType string.");
6545+
.Case("assign", LocType::Assign);
6546+
65206547
Lex.Lex();
65216548
if (parseToken(lltok::lparen, "Expected '(' here"))
65226549
return true;
@@ -6549,7 +6576,7 @@ bool LLParser::parseDebugProgramValue(DPValue *&DPV, PerFunctionState &PFS) {
65496576
MDNode *AssignID = nullptr;
65506577
Metadata *AddressLocation = nullptr;
65516578
Metadata *AddressExpression = nullptr;
6552-
if (Type == LocType::Assign) {
6579+
if (ValueType == LocType::Assign) {
65536580
// Parse DIAssignID.
65546581
if (parseMDNode(AssignID))
65556582
return true;
@@ -6579,8 +6606,8 @@ bool LLParser::parseDebugProgramValue(DPValue *&DPV, PerFunctionState &PFS) {
65796606

65806607
if (parseToken(lltok::rparen, "Expected ')' here"))
65816608
return true;
6582-
DPV = DPValue::createUnresolvedDPValue(
6583-
Type, ValLocMD, Variable, cast<DIExpression>(Expression), AssignID,
6609+
DR = DPValue::createUnresolvedDPValue(
6610+
ValueType, ValLocMD, Variable, cast<DIExpression>(Expression), AssignID,
65846611
AddressLocation, cast_or_null<DIExpression>(AddressExpression), DebugLoc);
65856612
return false;
65866613
}

llvm/lib/IR/DebugProgramInstruction.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,16 +138,26 @@ DbgRecord::createDebugIntrinsic(Module *M, Instruction *InsertBefore) const {
138138
llvm_unreachable("unsupported DbgRecord kind");
139139
}
140140

141+
142+
DPLabel::DPLabel(MDNode *Label, MDNode *DL)
143+
: DbgRecord(LabelKind, DebugLoc(DL)), Label(Label) {
144+
assert(Label && "Unexpected nullptr");
145+
assert(isa<DILabel>(Label) || Label->isTemporary() && "Label type must be or resolve to a DILabel");
146+
}
141147
DPLabel::DPLabel(DILabel *Label, DebugLoc DL)
142148
: DbgRecord(LabelKind, DL), Label(Label) {
143149
assert(Label && "Unexpected nullptr");
144150
}
145151

152+
DPLabel *DPLabel::createUnresolvedDPLabel(MDNode *Label, MDNode *DL) {
153+
return new DPLabel(Label, DL);
154+
}
155+
146156
DPValue::DPValue(DPValue::LocationType Type, Metadata *Val, MDNode *Variable,
147157
MDNode *Expression, MDNode *AssignID, Metadata *Address,
148158
MDNode *AddressExpression, MDNode *DI)
149-
: DebugValueUser({Val, Address, AssignID}), Variable(Variable),
150-
Expression(Expression), DbgLoc(DI), AddressExpression(AddressExpression),
159+
: DbgRecord(ValueKind, DebugLoc(DI)), DebugValueUser({Val, Address, AssignID}), Variable(Variable),
160+
Expression(Expression), AddressExpression(AddressExpression),
151161
Type(Type) {}
152162

153163
DPValue *DPValue::createUnresolvedDPValue(DPValue::LocationType Type,

llvm/lib/IR/Verifier.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,7 @@ void Verifier::visitDbgRecords(Instruction &I) {
684684
&I);
685685
CheckDI(!isa<PHINode>(&I) || !I.hasDbgValues(),
686686
"PHI Node must not have any attached DbgRecords", &I);
687+
//for (DPValue &DPV : DPValue::filter(I.getDbgValueRange())) {
687688
for (DbgRecord &DR : I.getDbgValueRange()) {
688689
CheckDI(DR.getMarker() == I.DbgMarker, "DbgRecord had invalid DbgMarker",
689690
&I, &DR);

llvm/test/DebugInfo/roundtrip-non-instruction-debug-info.ll

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
; CHECK-NEXT: {{^}} %[[VAL_ADD:[0-9a-zA-Z]+]] = add i32 %[[VAL_A]], 5
2929
; OLDDBG-NEXT: call void @llvm.dbg.value(metadata !DIArgList(i32 %[[VAL_A]], i32 %[[VAL_ADD]]), metadata ![[VAR_A]], metadata !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_LLVM_arg, 1, DW_OP_plus)), !dbg ![[LOC_3:[0-9]+]]
3030
; NEWDBG-NEXT: {{^}} #dbg_value(!DIArgList(i32 %[[VAL_A]], i32 %[[VAL_ADD]]), ![[VAR_A]], !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_LLVM_arg, 1, DW_OP_plus), ![[LOC_3:[0-9]+]])
31+
; OLDDBG-NEXT: call void @llvm.dbg.label(metadata ![[LABEL_ID:[0-9]+]]), !dbg ![[LOC_3]]
32+
; NEWDBG-NEXT: {{^}} #dbg_label(![[LABEL_ID:[0-9]+]], ![[LOC_3]])
3133
; CHECK-NEXT: {{^}} store i32 %[[VAL_ADD]]{{.+}}, !DIAssignID ![[ASSIGNID:[0-9]+]]
3234
; OLDDBG-NEXT: call void @llvm.dbg.assign(metadata i32 %[[VAL_ADD]], metadata ![[VAR_B]], metadata !DIExpression(), metadata ![[ASSIGNID]], metadata ptr %[[VAL_B]], metadata !DIExpression()), !dbg ![[LOC_4:[0-9]+]]
3335
; NEWDBG-NEXT: {{^}} #dbg_assign(i32 %[[VAL_ADD]], ![[VAR_B]], !DIExpression(), ![[ASSIGNID]], ptr %[[VAL_B]], !DIExpression(), ![[LOC_4:[0-9]+]])
@@ -36,6 +38,7 @@
3638
; OLDDBG-DAG: declare void @llvm.dbg.value
3739
; OLDDBG-DAG: declare void @llvm.dbg.declare
3840
; OLDDBG-DAG: declare void @llvm.dbg.assign
41+
; OLDDBG-DAG: declare void @llvm.dbg.label
3942

4043
; CHECK-DAG: llvm.dbg.cu
4144
; CHECK-DAG: ![[VAR_A]] = !DILocalVariable(name: "a"
@@ -44,6 +47,7 @@
4447
; CHECK-DAG: ![[LOC_2]] = !DILocation(line: 3, column: 20
4548
; CHECK-DAG: ![[LOC_3]] = !DILocation(line: 3, column: 25
4649
; CHECK-DAG: ![[LOC_4]] = !DILocation(line: 3, column: 30
50+
; CHECK-DAG: ![[LABEL_ID]] = !DILabel(
4751

4852
define dso_local i32 @f(i32 %a) !dbg !7 {
4953
entry:
@@ -52,6 +56,7 @@ entry:
5256
call void @llvm.dbg.declare(metadata ptr %b, metadata !21, metadata !DIExpression()), !dbg !31
5357
%add = add i32 %a, 5, !dbg !31
5458
call void @llvm.dbg.value(metadata !DIArgList(i32 %a, i32 %add), metadata !20, metadata !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_LLVM_arg, 1, DW_OP_plus)), !dbg !32
59+
call void @llvm.dbg.label(metadata !50), !dbg !32
5560
store i32 %add, ptr %b, !dbg !32, !DIAssignID !40
5661
call void @llvm.dbg.assign(metadata i32 %add, metadata !21, metadata !DIExpression(), metadata !40, metadata ptr %b, metadata !DIExpression()), !dbg !33
5762
ret i32 %add, !dbg !33
@@ -61,6 +66,7 @@ entry:
6166
declare void @llvm.dbg.value(metadata, metadata, metadata)
6267
declare void @llvm.dbg.declare(metadata, metadata, metadata)
6368
declare void @llvm.dbg.assign(metadata, metadata, metadata, metadata, metadata, metadata)
69+
declare void @llvm.dbg.label(metadata)
6470

6571
!llvm.dbg.cu = !{!0}
6672
!llvm.module.flags = !{!3, !4, !5}
@@ -84,4 +90,5 @@ declare void @llvm.dbg.assign(metadata, metadata, metadata, metadata, metadata,
8490
!31 = !DILocation(line: 3, column: 20, scope: !7)
8591
!32 = !DILocation(line: 3, column: 25, scope: !7)
8692
!33 = !DILocation(line: 3, column: 30, scope: !7)
87-
!40 = distinct !DIAssignID()
93+
!40 = distinct !DIAssignID()
94+
!50 = !DILabel(scope: !7, name: "label", file: !1, line: 3)

0 commit comments

Comments
 (0)