Skip to content

Commit 2db9244

Browse files
authored
[RemoveDIs][DebugInfo] Make DIAssignID always replaceable (#78300)
This patch is a necessary step to allowing the new non-intrinsic debug info to replace llvm.dbg.assign intrinsics. DIAssignIDs must be able to look up the debug assigns that refer to them, and this patch makes them always be considered "replaceable", allowing us to track and replace uses for non-temporary instances.
1 parent f12059e commit 2db9244

File tree

5 files changed

+31
-13
lines changed

5 files changed

+31
-13
lines changed

llvm/include/llvm/IR/DebugInfoMetadata.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,10 @@ class DIAssignID : public MDNode {
323323
// This node has no operands to replace.
324324
void replaceOperandWith(unsigned I, Metadata *New) = delete;
325325

326+
SmallVector<DPValue *> getAllDPValueUsers() {
327+
return Context.getReplaceableUses()->getAllDPValueUsers();
328+
}
329+
326330
static DIAssignID *getDistinct(LLVMContext &Context) {
327331
return getImpl(Context, Distinct);
328332
}

llvm/include/llvm/IR/Metadata.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1057,6 +1057,7 @@ struct TempMDNodeDeleter {
10571057
class MDNode : public Metadata {
10581058
friend class ReplaceableMetadataImpl;
10591059
friend class LLVMContextImpl;
1060+
friend class DIAssignID;
10601061

10611062
/// The header that is coallocated with an MDNode along with its "small"
10621063
/// operands. It is located immediately before the main body of the node.
@@ -1239,7 +1240,8 @@ class MDNode : public Metadata {
12391240
bool isDistinct() const { return Storage == Distinct; }
12401241
bool isTemporary() const { return Storage == Temporary; }
12411242

1242-
bool isReplaceable() const { return isTemporary(); }
1243+
bool isReplaceable() const { return isTemporary() || isAlwaysReplaceable(); }
1244+
bool isAlwaysReplaceable() const { return getMetadataID() == DIAssignIDKind; }
12431245

12441246
/// RAUW a temporary.
12451247
///

llvm/lib/IR/DebugInfo.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1787,13 +1787,6 @@ void at::deleteAssignmentMarkers(const Instruction *Inst) {
17871787
}
17881788

17891789
void at::RAUW(DIAssignID *Old, DIAssignID *New) {
1790-
// Replace MetadataAsValue uses.
1791-
if (auto *OldIDAsValue =
1792-
MetadataAsValue::getIfExists(Old->getContext(), Old)) {
1793-
auto *NewIDAsValue = MetadataAsValue::get(Old->getContext(), New);
1794-
OldIDAsValue->replaceAllUsesWith(NewIDAsValue);
1795-
}
1796-
17971790
// Replace attachments.
17981791
AssignmentInstRange InstRange = getAssignmentInsts(Old);
17991792
// Use intermediate storage for the instruction ptrs because the
@@ -1802,6 +1795,8 @@ void at::RAUW(DIAssignID *Old, DIAssignID *New) {
18021795
SmallVector<Instruction *> InstVec(InstRange.begin(), InstRange.end());
18031796
for (auto *I : InstVec)
18041797
I->setMetadata(LLVMContext::MD_DIAssignID, New);
1798+
1799+
Old->replaceAllUsesWith(New);
18051800
}
18061801

18071802
void at::deleteAll(Function *F) {

llvm/lib/IR/Metadata.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -439,24 +439,30 @@ void ReplaceableMetadataImpl::resolveAllUses(bool ResolveUsers) {
439439
// commentry in DIArgList::handleChangedOperand for details. Hidden behind
440440
// conditional compilation to avoid a compile time regression.
441441
ReplaceableMetadataImpl *ReplaceableMetadataImpl::getOrCreate(Metadata &MD) {
442-
if (auto *N = dyn_cast<MDNode>(&MD))
443-
return N->isResolved() ? nullptr : N->Context.getOrCreateReplaceableUses();
442+
if (auto *N = dyn_cast<MDNode>(&MD)) {
443+
return !N->isResolved() || N->isAlwaysReplaceable()
444+
? N->Context.getOrCreateReplaceableUses()
445+
: nullptr;
446+
}
444447
if (auto ArgList = dyn_cast<DIArgList>(&MD))
445448
return ArgList;
446449
return dyn_cast<ValueAsMetadata>(&MD);
447450
}
448451

449452
ReplaceableMetadataImpl *ReplaceableMetadataImpl::getIfExists(Metadata &MD) {
450-
if (auto *N = dyn_cast<MDNode>(&MD))
451-
return N->isResolved() ? nullptr : N->Context.getReplaceableUses();
453+
if (auto *N = dyn_cast<MDNode>(&MD)) {
454+
return !N->isResolved() || N->isAlwaysReplaceable()
455+
? N->Context.getReplaceableUses()
456+
: nullptr;
457+
}
452458
if (auto ArgList = dyn_cast<DIArgList>(&MD))
453459
return ArgList;
454460
return dyn_cast<ValueAsMetadata>(&MD);
455461
}
456462

457463
bool ReplaceableMetadataImpl::isReplaceable(const Metadata &MD) {
458464
if (auto *N = dyn_cast<MDNode>(&MD))
459-
return !N->isResolved();
465+
return !N->isResolved() || N->isAlwaysReplaceable();
460466
return isa<ValueAsMetadata>(&MD) || isa<DIArgList>(&MD);
461467
}
462468

llvm/lib/IR/Verifier.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,11 @@ struct VerifierSupport {
173173
}
174174
}
175175

176+
void Write(const DPValue *V) {
177+
if (V)
178+
V->print(*OS, MST, false);
179+
}
180+
176181
void Write(const Metadata *MD) {
177182
if (!MD)
178183
return;
@@ -4740,6 +4745,12 @@ void Verifier::visitDIAssignIDMetadata(Instruction &I, MDNode *MD) {
47404745
"dbg.assign not in same function as inst", DAI, &I);
47414746
}
47424747
}
4748+
for (DPValue *DPV : cast<DIAssignID>(MD)->getAllDPValueUsers()) {
4749+
CheckDI(DPV->isDbgAssign(),
4750+
"!DIAssignID should only be used by Assign DPVs.", MD, DPV);
4751+
CheckDI(DPV->getFunction() == I.getFunction(),
4752+
"DPVAssign not in same function as inst", DPV, &I);
4753+
}
47434754
}
47444755

47454756
void Verifier::visitCallStackMetadata(MDNode *MD) {

0 commit comments

Comments
 (0)