Skip to content

Commit 1a72753

Browse files
committed
WIP fixes
1 parent fa711ec commit 1a72753

File tree

2 files changed

+35
-26
lines changed

2 files changed

+35
-26
lines changed

llvm/lib/IR/Verifier.cpp

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,7 @@ class Verifier : public InstVisitor<Verifier>, VerifierSupport {
545545
void visit(DPValue &DPV);
546546
// InstVisitor overrides...
547547
using InstVisitor<Verifier>::visit;
548+
void visitDbgRecords(Instruction &I);
548549
void visit(Instruction &I);
549550

550551
void visitTruncInst(TruncInst &I);
@@ -670,9 +671,19 @@ class Verifier : public InstVisitor<Verifier>, VerifierSupport {
670671
} \
671672
} while (false)
672673

673-
void Verifier::visit(Instruction &I) {
674-
for (auto &DPV : I.getDbgValueRange())
674+
void Verifier::visitDbgRecords(Instruction &I) {
675+
if (!I.DbgMarker)
676+
return;
677+
CheckDI(I.DbgMarker->MarkedInstr == &I, "Instruction has invalid DbgMarker", &I);
678+
CheckDI(!isa<PHINode>(&I) || !I.hasDbgValues(), "PHI Node must not have any attached DbgRecords", &I);
679+
for (auto &DPV : I.getDbgValueRange()) {
680+
CheckDI(DPV.getMarker() == I.DbgMarker, "DbgRecord had invalid DbgMarker", &I, &DPV);
675681
visit(DPV);
682+
}
683+
}
684+
685+
void Verifier::visit(Instruction &I) {
686+
visitDbgRecords(I);
676687
for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
677688
Check(I.getOperand(i) != nullptr, "Operand is null", &I);
678689
InstVisitor<Verifier>::visit(I);
@@ -3000,6 +3011,7 @@ void Verifier::visitBasicBlock(BasicBlock &BB) {
30003011

30013012
// Confirm that no issues arise from the debug program.
30023013
if (BB.IsNewDbgInfoFormat) {
3014+
CheckDI(!BB.getTrailingDPValues(), "Basic Block has trailing DbgRecords!", &BB);
30033015
// Configure the validate function to not fire assertions, instead print
30043016
// errors and return true if there's a problem.
30053017
bool RetVal = BB.validateDbgValues(false, true, OS);
@@ -6176,33 +6188,24 @@ void Verifier::visit(DPValue &DPV) {
61766188
DPV.getType() == DPValue::LocationType::Declare ||
61776189
DPV.getType() == DPValue::LocationType::Assign,
61786190
"invalid #dbg record type", &DPV, DPV.getType());
6179-
StringRef Kind;
6180-
switch (DPV.getType()) {
6181-
case DPValue::LocationType::Value:
6182-
Kind = "value";
6183-
break;
6184-
case DPValue::LocationType::Declare:
6185-
Kind = "declare";
6186-
break;
6187-
case DPValue::LocationType::Assign:
6188-
Kind = "assign";
6189-
break;
6190-
default:
6191-
llvm_unreachable("Tried to print a DPValue with an invalid LocationType!");
6192-
};
6191+
// The location for a DPValue must be either a ValueAsMetadata, DIArgList, or
6192+
// an empty MDNode (which is a legacy representation for an "undef" location).
61936193
auto *MD = DPV.getRawLocation();
61946194
CheckDI(isa<ValueAsMetadata>(MD) || isa<DIArgList>(MD) ||
61956195
(isa<MDNode>(MD) && !cast<MDNode>(MD)->getNumOperands()),
6196-
"invalid #dbg_" + Kind + " address/value", &DPV, MD);
6196+
"invalid #dbg record address/value", &DPV, MD);
61976197
CheckDI(isa<DILocalVariable>(DPV.getRawVariable()),
6198-
"invalid #dbg_" + Kind + " variable", &DPV, DPV.getRawVariable());
6199-
CheckDI(DPV.getExpression(), "missing #dbg_" + Kind + " expression", &DPV,
6198+
"invalid #dbg record variable", &DPV, DPV.getRawVariable());
6199+
CheckDI(DPV.getExpression(), "missing #dbg record expression", &DPV,
62006200
DPV.getExpression());
62016201

62026202
if (DPV.isDbgAssign()) {
62036203
CheckDI(isa<DIAssignID>(DPV.getRawAssignID()),
62046204
"invalid #dbg_assign DIAssignID", &DPV, DPV.getRawAssignID());
62056205
const auto *RawAddr = DPV.getRawAddress();
6206+
// Similarly to the location above, the address for an assign DPValue must
6207+
// be a ValueAsMetadata or an empty MDNode, which represents an undef
6208+
// address.
62066209
CheckDI(
62076210
isa<ValueAsMetadata>(RawAddr) ||
62086211
(isa<MDNode>(RawAddr) && !cast<MDNode>(RawAddr)->getNumOperands()),
@@ -6217,7 +6220,7 @@ void Verifier::visit(DPValue &DPV) {
62176220
}
62186221

62196222
if (MDNode *N = DPV.getDebugLoc().getAsMDNode()) {
6220-
CheckDI(isa<DILocation>(N), "invalid #dbg_" + Kind + " location", &DPV, N);
6223+
CheckDI(isa<DILocation>(N), "invalid #dbg record location", &DPV, N);
62216224
visitDILocation(*cast<DILocation>(N));
62226225
}
62236226

@@ -6227,16 +6230,15 @@ void Verifier::visit(DPValue &DPV) {
62276230
// The scopes for variables and !dbg attachments must agree.
62286231
DILocalVariable *Var = DPV.getVariable();
62296232
DILocation *Loc = DPV.getDebugLoc();
6230-
CheckDI(Loc, "missing #dbg_" + Kind + " DILocation", &DPV, BB, F);
6233+
CheckDI(Loc, "missing #dbg record DILocation", &DPV, BB, F);
62316234

62326235
DISubprogram *VarSP = getSubprogram(Var->getRawScope());
62336236
DISubprogram *LocSP = getSubprogram(Loc->getRawScope());
62346237
if (!VarSP || !LocSP)
62356238
return; // Broken scope chains are checked elsewhere.
62366239

62376240
CheckDI(VarSP == LocSP,
6238-
"mismatched subprogram between #dbg_" + Kind +
6239-
" variable and DILocation",
6241+
"mismatched subprogram between #dbg record variable and DILocation",
62406242
&DPV, BB, F, Var, Var->getScope()->getSubprogram(), Loc,
62416243
Loc->getScope()->getSubprogram());
62426244

llvm/unittests/IR/DebugInfoTest.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,7 +1082,10 @@ TEST(MetadataTest, DPValueConversionRoutines) {
10821082
EXPECT_FALSE(BB1->IsNewDbgInfoFormat);
10831083
// Validating the block for DPValues / DPMarkers shouldn't fail -- there's
10841084
// no data stored right now.
1085-
EXPECT_FALSE(BB1->validateDbgValues(false, false));
1085+
bool BrokenDebugInfo = false;
1086+
bool Error = verifyModule(*M, &errs(), &BrokenDebugInfo);
1087+
EXPECT_FALSE(Error);
1088+
EXPECT_FALSE(BrokenDebugInfo);
10861089

10871090
// Function and module should be marked as not having the new format too.
10881091
EXPECT_FALSE(F->IsNewDbgInfoFormat);
@@ -1135,13 +1138,17 @@ TEST(MetadataTest, DPValueConversionRoutines) {
11351138
EXPECT_TRUE(!Inst.DbgMarker || Inst.DbgMarker->StoredDPValues.empty());
11361139

11371140
// Validating the first block should continue to not be a problem,
1138-
EXPECT_FALSE(BB1->validateDbgValues(false, false));
1141+
Error = verifyModule(*M, &errs(), &BrokenDebugInfo);
1142+
EXPECT_FALSE(Error);
1143+
EXPECT_FALSE(BrokenDebugInfo);
11391144
// But if we were to break something, it should be able to fire. Don't attempt
11401145
// to comprehensively test the validator, it's a smoke-test rather than a
11411146
// "proper" verification pass.
11421147
DPV1->setMarker(nullptr);
11431148
// A marker pointing the wrong way should be an error.
1144-
EXPECT_TRUE(BB1->validateDbgValues(false, false));
1149+
Error = verifyModule(*M, &errs(), &BrokenDebugInfo);
1150+
EXPECT_FALSE(Error);
1151+
EXPECT_TRUE(BrokenDebugInfo);
11451152
DPV1->setMarker(FirstInst->DbgMarker);
11461153

11471154
DILocalVariable *DLV1 = DPV1->getVariable();

0 commit comments

Comments
 (0)