Skip to content

Commit fa711ec

Browse files
committed
Add pre-parse changes (verifier, DPValue class interface)
1 parent b98e6a5 commit fa711ec

File tree

4 files changed

+110
-6
lines changed

4 files changed

+110
-6
lines changed

llvm/include/llvm/IR/DebugProgramInstruction.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ class DPValue : public DbgRecord, protected DebugValueUser {
224224
// DebugValueUser superclass instead. The referred to Value can either be a
225225
// ValueAsMetadata or a DIArgList.
226226

227-
DILocalVariable *Variable;
227+
TrackingMDNodeRef Variable;
228228
DIExpression *Expression;
229229
DIExpression *AddressExpression;
230230

@@ -331,7 +331,7 @@ class DPValue : public DbgRecord, protected DebugValueUser {
331331
void addVariableLocationOps(ArrayRef<Value *> NewValues,
332332
DIExpression *NewExpr);
333333

334-
void setVariable(DILocalVariable *NewVar) { Variable = NewVar; }
334+
void setVariable(DILocalVariable *NewVar);
335335

336336
void setExpression(DIExpression *NewExpr) { Expression = NewExpr; }
337337

@@ -349,7 +349,8 @@ class DPValue : public DbgRecord, protected DebugValueUser {
349349
void setKillLocation();
350350
bool isKillLocation() const;
351351

352-
DILocalVariable *getVariable() const { return Variable; }
352+
DILocalVariable *getVariable() const;
353+
MDNode *getRawVariable() const { return Variable; }
353354

354355
DIExpression *getExpression() const { return Expression; }
355356

llvm/lib/IR/AsmWriter.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,15 +1143,15 @@ void SlotTracker::processDbgRecordMetadata(const DbgRecord &DR) {
11431143
// Process metadata used by DbgRecords; we only specifically care about the
11441144
// DILocalVariable, DILocation, and DIAssignID fields, as the Value and
11451145
// Expression fields should only be printed inline and so do not use a slot.
1146-
CreateMetadataSlot(DPV->getVariable());
1146+
CreateMetadataSlot(DPV->getRawVariable());
11471147
if (DPV->isDbgAssign())
1148-
CreateMetadataSlot(DPV->getAssignID());
1148+
CreateMetadataSlot(cast<MDNode>(DPV->getRawAssignID()));
11491149
} else if (const DPLabel *DPL = dyn_cast<const DPLabel>(&DR)) {
11501150
CreateMetadataSlot(DPL->getLabel());
11511151
} else {
11521152
llvm_unreachable("unsupported DbgRecord kind");
11531153
}
1154-
CreateMetadataSlot(DR.getDebugLoc());
1154+
CreateMetadataSlot(DR.getDebugLoc().getAsMDNode());
11551155
}
11561156

11571157
void SlotTracker::processInstructionMetadata(const Instruction &I) {

llvm/lib/IR/DebugProgramInstruction.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ DPValue *DPValue::createLinkedDPVAssign(Instruction *LinkedInstr, Value *Val,
175175
return NewDPVAssign;
176176
}
177177

178+
void DPValue::setVariable(DILocalVariable *NewVar) { Variable.reset(NewVar); }
179+
178180
iterator_range<DPValue::location_op_iterator> DPValue::location_ops() const {
179181
auto *MD = getRawLocation();
180182
// If a Value has been deleted, the "location" for this DPValue will be
@@ -313,6 +315,10 @@ bool DPValue::isKillLocation() const {
313315
any_of(location_ops(), [](Value *V) { return isa<UndefValue>(V); });
314316
}
315317

318+
DILocalVariable *DPValue::getVariable() const {
319+
return cast<DILocalVariable>(Variable.get());
320+
}
321+
316322
std::optional<uint64_t> DPValue::getFragmentSizeInBits() const {
317323
if (auto Fragment = getExpression()->getFragmentInfo())
318324
return Fragment->SizeInBits;

llvm/lib/IR/Verifier.cpp

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,26 @@ struct VerifierSupport {
178178
V->print(*OS, MST, false);
179179
}
180180

181+
void Write(DPValue::LocationType Type) {
182+
switch (Type) {
183+
case DPValue::LocationType::Value:
184+
*OS << "value";
185+
break;
186+
case DPValue::LocationType::Declare:
187+
*OS << "declare";
188+
break;
189+
case DPValue::LocationType::Assign:
190+
*OS << "assign";
191+
break;
192+
case DPValue::LocationType::End:
193+
*OS << "end";
194+
break;
195+
case DPValue::LocationType::Any:
196+
*OS << "any";
197+
break;
198+
};
199+
}
200+
181201
void Write(const Metadata *MD) {
182202
if (!MD)
183203
return;
@@ -522,6 +542,7 @@ class Verifier : public InstVisitor<Verifier>, VerifierSupport {
522542

523543
void visitTemplateParams(const MDNode &N, const Metadata &RawParams);
524544

545+
void visit(DPValue &DPV);
525546
// InstVisitor overrides...
526547
using InstVisitor<Verifier>::visit;
527548
void visit(Instruction &I);
@@ -650,6 +671,8 @@ class Verifier : public InstVisitor<Verifier>, VerifierSupport {
650671
} while (false)
651672

652673
void Verifier::visit(Instruction &I) {
674+
for (auto &DPV : I.getDbgValueRange())
675+
visit(DPV);
653676
for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
654677
Check(I.getOperand(i) != nullptr, "Operand is null", &I);
655678
InstVisitor<Verifier>::visit(I);
@@ -6148,6 +6171,80 @@ static DISubprogram *getSubprogram(Metadata *LocalScope) {
61486171
return nullptr;
61496172
}
61506173

6174+
void Verifier::visit(DPValue &DPV) {
6175+
CheckDI(DPV.getType() == DPValue::LocationType::Value ||
6176+
DPV.getType() == DPValue::LocationType::Declare ||
6177+
DPV.getType() == DPValue::LocationType::Assign,
6178+
"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+
};
6193+
auto *MD = DPV.getRawLocation();
6194+
CheckDI(isa<ValueAsMetadata>(MD) || isa<DIArgList>(MD) ||
6195+
(isa<MDNode>(MD) && !cast<MDNode>(MD)->getNumOperands()),
6196+
"invalid #dbg_" + Kind + " address/value", &DPV, MD);
6197+
CheckDI(isa<DILocalVariable>(DPV.getRawVariable()),
6198+
"invalid #dbg_" + Kind + " variable", &DPV, DPV.getRawVariable());
6199+
CheckDI(DPV.getExpression(), "missing #dbg_" + Kind + " expression", &DPV,
6200+
DPV.getExpression());
6201+
6202+
if (DPV.isDbgAssign()) {
6203+
CheckDI(isa<DIAssignID>(DPV.getRawAssignID()),
6204+
"invalid #dbg_assign DIAssignID", &DPV, DPV.getRawAssignID());
6205+
const auto *RawAddr = DPV.getRawAddress();
6206+
CheckDI(
6207+
isa<ValueAsMetadata>(RawAddr) ||
6208+
(isa<MDNode>(RawAddr) && !cast<MDNode>(RawAddr)->getNumOperands()),
6209+
"invalid #dbg_assign address", &DPV, DPV.getRawAddress());
6210+
CheckDI(DPV.getAddressExpression(),
6211+
"missing #dbg_assign address expression", &DPV,
6212+
DPV.getAddressExpression());
6213+
// All of the linked instructions should be in the same function as DPV.
6214+
for (Instruction *I : at::getAssignmentInsts(&DPV))
6215+
CheckDI(DPV.getFunction() == I->getFunction(),
6216+
"inst not in same function as #dbg_assign", I, &DPV);
6217+
}
6218+
6219+
if (MDNode *N = DPV.getDebugLoc().getAsMDNode()) {
6220+
CheckDI(isa<DILocation>(N), "invalid #dbg_" + Kind + " location", &DPV, N);
6221+
visitDILocation(*cast<DILocation>(N));
6222+
}
6223+
6224+
BasicBlock *BB = DPV.getParent();
6225+
Function *F = BB ? BB->getParent() : nullptr;
6226+
6227+
// The scopes for variables and !dbg attachments must agree.
6228+
DILocalVariable *Var = DPV.getVariable();
6229+
DILocation *Loc = DPV.getDebugLoc();
6230+
CheckDI(Loc, "missing #dbg_" + Kind + " DILocation", &DPV, BB, F);
6231+
6232+
DISubprogram *VarSP = getSubprogram(Var->getRawScope());
6233+
DISubprogram *LocSP = getSubprogram(Loc->getRawScope());
6234+
if (!VarSP || !LocSP)
6235+
return; // Broken scope chains are checked elsewhere.
6236+
6237+
CheckDI(VarSP == LocSP,
6238+
"mismatched subprogram between #dbg_" + Kind +
6239+
" variable and DILocation",
6240+
&DPV, BB, F, Var, Var->getScope()->getSubprogram(), Loc,
6241+
Loc->getScope()->getSubprogram());
6242+
6243+
// This check is redundant with one in visitLocalVariable().
6244+
CheckDI(isType(Var->getRawType()), "invalid type ref", Var,
6245+
Var->getRawType());
6246+
}
6247+
61516248
void Verifier::visitVPIntrinsic(VPIntrinsic &VPI) {
61526249
if (auto *VPCast = dyn_cast<VPCastIntrinsic>(&VPI)) {
61536250
auto *RetTy = cast<VectorType>(VPCast->getType());

0 commit comments

Comments
 (0)