Skip to content

Commit

Permalink
[NFC] Change FindDbgDeclareUsers interface to match findDbgUsers/valu…
Browse files Browse the repository at this point in the history
…es (#73498)

This simplifies an upcoming patch to support the RemoveDIs project (tracking
variable locations without using intrinsics).

Next in this series is #73500.
  • Loading branch information
OCHyams authored Dec 12, 2023
1 parent 6111f5c commit 2d9d9a1
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 19 deletions.
2 changes: 1 addition & 1 deletion llvm/include/llvm/IR/DebugInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Module;

/// Finds dbg.declare intrinsics declaring local variables as living in the
/// memory that 'V' points to.
TinyPtrVector<DbgDeclareInst *> FindDbgDeclareUses(Value *V);
void findDbgDeclares(SmallVectorImpl<DbgDeclareInst *> &DbgUsers, Value *V);

/// Finds the llvm.dbg.value intrinsics describing a value.
void findDbgValues(SmallVectorImpl<DbgValueInst *> &DbgValues,
Expand Down
14 changes: 6 additions & 8 deletions llvm/lib/IR/DebugInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,23 @@ using namespace llvm;
using namespace llvm::at;
using namespace llvm::dwarf;

TinyPtrVector<DbgDeclareInst *> llvm::FindDbgDeclareUses(Value *V) {
void llvm::findDbgDeclares(SmallVectorImpl<DbgDeclareInst *> &DbgUsers,
Value *V) {
// This function is hot. Check whether the value has any metadata to avoid a
// DenseMap lookup.
if (!V->isUsedByMetadata())
return {};
return;
auto *L = LocalAsMetadata::getIfExists(V);
if (!L)
return {};
return;
auto *MDV = MetadataAsValue::getIfExists(V->getContext(), L);
if (!MDV)
return {};
return;

TinyPtrVector<DbgDeclareInst *> Declares;
for (User *U : MDV->users()) {
if (auto *DDI = dyn_cast<DbgDeclareInst>(U))
Declares.push_back(DDI);
DbgUsers.push_back(DDI);
}

return Declares;
}

template <typename IntrinsicT>
Expand Down
12 changes: 8 additions & 4 deletions llvm/lib/Transforms/Coroutines/CoroFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -963,7 +963,8 @@ static void cacheDIVar(FrameDataInfo &FrameData,
if (DIVarCache.contains(V))
continue;

auto DDIs = FindDbgDeclareUses(V);
SmallVector<DbgDeclareInst *, 1> DDIs;
findDbgDeclares(DDIs, V);
auto *I = llvm::find_if(DDIs, [](DbgDeclareInst *DDI) {
return DDI->getExpression()->getNumElements() == 0;
});
Expand Down Expand Up @@ -1119,7 +1120,8 @@ static void buildFrameDebugInfo(Function &F, coro::Shape &Shape,
assert(PromiseAlloca &&
"Coroutine with switch ABI should own Promise alloca");

TinyPtrVector<DbgDeclareInst *> DIs = FindDbgDeclareUses(PromiseAlloca);
SmallVector<DbgDeclareInst *, 1> DIs;
findDbgDeclares(DIs, PromiseAlloca);
if (DIs.empty())
return;

Expand Down Expand Up @@ -1840,7 +1842,8 @@ static void insertSpills(const FrameDataInfo &FrameData, coro::Shape &Shape) {
FrameTy->getElementType(FrameData.getFieldIndex(E.first)), GEP,
SpillAlignment, E.first->getName() + Twine(".reload"));

TinyPtrVector<DbgDeclareInst *> DIs = FindDbgDeclareUses(Def);
SmallVector<DbgDeclareInst *, 1> DIs;
findDbgDeclares(DIs, Def);
// Try best to find dbg.declare. If the spill is a temp, there may not
// be a direct dbg.declare. Walk up the load chain to find one from an
// alias.
Expand All @@ -1854,7 +1857,8 @@ static void insertSpills(const FrameDataInfo &FrameData, coro::Shape &Shape) {
CurDef = LdInst->getPointerOperand();
if (!isa<AllocaInst, LoadInst>(CurDef))
break;
DIs = FindDbgDeclareUses(CurDef);
DIs.clear();
findDbgDeclares(DIs, CurDef);
}
}

Expand Down
13 changes: 10 additions & 3 deletions llvm/lib/Transforms/Scalar/SROA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4940,10 +4940,13 @@ bool SROA::splitAlloca(AllocaInst &AI, AllocaSlices &AS) {
// Migrate debug information from the old alloca to the new alloca(s)
// and the individual partitions.
TinyPtrVector<DbgVariableIntrinsic *> DbgVariables;
for (auto *DbgDeclare : FindDbgDeclareUses(&AI))
SmallVector<DbgDeclareInst *, 1> DbgDeclares;
findDbgDeclares(DbgDeclares, &AI);
for (auto *DbgDeclare : DbgDeclares)
DbgVariables.push_back(DbgDeclare);
for (auto *DbgAssign : at::getAssignmentMarkers(&AI))
DbgVariables.push_back(DbgAssign);

for (DbgVariableIntrinsic *DbgVariable : DbgVariables) {
auto *Expr = DbgVariable->getExpression();
DIBuilder DIB(*AI.getModule(), /*AllowUnresolved*/ false);
Expand Down Expand Up @@ -4997,7 +5000,9 @@ bool SROA::splitAlloca(AllocaInst &AI, AllocaSlices &AS) {

// Remove any existing intrinsics on the new alloca describing
// the variable fragment.
for (DbgDeclareInst *OldDII : FindDbgDeclareUses(Fragment.Alloca)) {
SmallVector<DbgDeclareInst *, 1> FragDbgDeclares;
findDbgDeclares(FragDbgDeclares, Fragment.Alloca);
for (DbgDeclareInst *OldDII : FragDbgDeclares) {
auto SameVariableFragment = [](const DbgVariableIntrinsic *LHS,
const DbgVariableIntrinsic *RHS) {
return LHS->getVariable() == RHS->getVariable() &&
Expand Down Expand Up @@ -5147,7 +5152,9 @@ bool SROA::deleteDeadInstructions(
// not be able to find it.
if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) {
DeletedAllocas.insert(AI);
for (DbgDeclareInst *OldDII : FindDbgDeclareUses(AI))
SmallVector<DbgDeclareInst *, 1> DbgDeclares;
findDbgDeclares(DbgDeclares, AI);
for (DbgDeclareInst *OldDII : DbgDeclares)
OldDII->eraseFromParent();
}

Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/Transforms/Utils/Local.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2103,7 +2103,8 @@ void llvm::insertDebugValuesForPHIs(BasicBlock *BB,
bool llvm::replaceDbgDeclare(Value *Address, Value *NewAddress,
DIBuilder &Builder, uint8_t DIExprFlags,
int Offset) {
auto DbgDeclares = FindDbgDeclareUses(Address);
SmallVector<DbgDeclareInst *, 1> DbgDeclares;
findDbgDeclares(DbgDeclares, Address);
for (DbgVariableIntrinsic *DII : DbgDeclares) {
const DebugLoc &Loc = DII->getDebugLoc();
auto *DIVar = DII->getVariable();
Expand Down
5 changes: 3 additions & 2 deletions llvm/lib/Transforms/Utils/MemoryOpRemark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,9 @@ void MemoryOpRemark::visitVariable(const Value *V,
bool FoundDI = false;
// Try to get an llvm.dbg.declare, which has a DILocalVariable giving us the
// real debug info name and size of the variable.
for (const DbgVariableIntrinsic *DVI :
FindDbgDeclareUses(const_cast<Value *>(V))) {
SmallVector<DbgDeclareInst *, 1> DbgDeclares;
findDbgDeclares(DbgDeclares, const_cast<Value *>(V));
for (const DbgVariableIntrinsic *DVI : DbgDeclares) {
if (DILocalVariable *DILV = DVI->getVariable()) {
std::optional<uint64_t> DISize = getSizeInBytes(DILV->getSizeInBits());
VariableInfo Var{DILV->getName(), DISize};
Expand Down

0 comments on commit 2d9d9a1

Please sign in to comment.