From 49647465a3f8bb769b5c49e159c4b87d1e76f449 Mon Sep 17 00:00:00 2001 From: OCHyams Date: Fri, 17 Nov 2023 14:24:12 +0000 Subject: [PATCH 1/3] [NFC] Change FindDbgDeclareUsers interface to match findDbgUsers/values This simplifies an upcoming patch to support the RemoveDIs project (tracking variable locations without using intrinsics). --- llvm/include/llvm/IR/DebugInfo.h | 2 +- llvm/lib/IR/DebugInfo.cpp | 11 +++++------ llvm/lib/Transforms/Coroutines/CoroFrame.cpp | 12 ++++++++---- llvm/lib/Transforms/Scalar/SROA.cpp | 13 ++++++++++--- llvm/lib/Transforms/Utils/Local.cpp | 3 ++- llvm/lib/Transforms/Utils/MemoryOpRemark.cpp | 5 +++-- 6 files changed, 29 insertions(+), 17 deletions(-) diff --git a/llvm/include/llvm/IR/DebugInfo.h b/llvm/include/llvm/IR/DebugInfo.h index 2a581eb5f09d9..5ed423bfd1c13 100644 --- a/llvm/include/llvm/IR/DebugInfo.h +++ b/llvm/include/llvm/IR/DebugInfo.h @@ -40,7 +40,7 @@ class Module; /// Finds dbg.declare intrinsics declaring local variables as living in the /// memory that 'V' points to. -TinyPtrVector FindDbgDeclareUses(Value *V); +void findDbgDeclares(SmallVectorImpl &DbgUsers, Value *V); /// Finds the llvm.dbg.value intrinsics describing a value. void findDbgValues(SmallVectorImpl &DbgValues, diff --git a/llvm/lib/IR/DebugInfo.cpp b/llvm/lib/IR/DebugInfo.cpp index 3fe940e19295b..e3aeac3a9fee6 100644 --- a/llvm/lib/IR/DebugInfo.cpp +++ b/llvm/lib/IR/DebugInfo.cpp @@ -44,25 +44,24 @@ using namespace llvm; using namespace llvm::at; using namespace llvm::dwarf; -TinyPtrVector llvm::FindDbgDeclareUses(Value *V) { +void llvm::findDbgDeclares(SmallVectorImpl &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 Declares; for (User *U : MDV->users()) { if (auto *DDI = dyn_cast(U)) Declares.push_back(DDI); } - - return Declares; } template diff --git a/llvm/lib/Transforms/Coroutines/CoroFrame.cpp b/llvm/lib/Transforms/Coroutines/CoroFrame.cpp index fef1a698e1463..fbacfb39f62ac 100644 --- a/llvm/lib/Transforms/Coroutines/CoroFrame.cpp +++ b/llvm/lib/Transforms/Coroutines/CoroFrame.cpp @@ -963,7 +963,8 @@ static void cacheDIVar(FrameDataInfo &FrameData, if (DIVarCache.contains(V)) continue; - auto DDIs = FindDbgDeclareUses(V); + SmallVector DDIs; + findDbgDeclares(DDIs, V); auto *I = llvm::find_if(DDIs, [](DbgDeclareInst *DDI) { return DDI->getExpression()->getNumElements() == 0; }); @@ -1119,7 +1120,8 @@ static void buildFrameDebugInfo(Function &F, coro::Shape &Shape, assert(PromiseAlloca && "Coroutine with switch ABI should own Promise alloca"); - TinyPtrVector DIs = FindDbgDeclareUses(PromiseAlloca); + SmallVector DIs; + findDbgDeclares(DIs, PromiseAlloca); if (DIs.empty()) return; @@ -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 DIs = FindDbgDeclareUses(Def); + SmallVector 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. @@ -1854,7 +1857,8 @@ static void insertSpills(const FrameDataInfo &FrameData, coro::Shape &Shape) { CurDef = LdInst->getPointerOperand(); if (!isa(CurDef)) break; - DIs = FindDbgDeclareUses(CurDef); + DIs.clear(); + findDbgDeclares(DIs, CurDef); } } diff --git a/llvm/lib/Transforms/Scalar/SROA.cpp b/llvm/lib/Transforms/Scalar/SROA.cpp index f578762d2b497..92d5f970ea4cc 100644 --- a/llvm/lib/Transforms/Scalar/SROA.cpp +++ b/llvm/lib/Transforms/Scalar/SROA.cpp @@ -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 DbgVariables; - for (auto *DbgDeclare : FindDbgDeclareUses(&AI)) + SmallVector 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); @@ -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 FragDbgDeclares; + findDbgDeclares(FragDbgDeclares, Fragment.Alloca); + for (DbgDeclareInst *OldDII : FragDbgDeclares) { auto SameVariableFragment = [](const DbgVariableIntrinsic *LHS, const DbgVariableIntrinsic *RHS) { return LHS->getVariable() == RHS->getVariable() && @@ -5147,7 +5152,9 @@ bool SROA::deleteDeadInstructions( // not be able to find it. if (AllocaInst *AI = dyn_cast(I)) { DeletedAllocas.insert(AI); - for (DbgDeclareInst *OldDII : FindDbgDeclareUses(AI)) + SmallVector DbgDeclares; + findDbgDeclares(DbgDeclares, AI); + for (DbgDeclareInst *OldDII : DbgDeclares) OldDII->eraseFromParent(); } diff --git a/llvm/lib/Transforms/Utils/Local.cpp b/llvm/lib/Transforms/Utils/Local.cpp index e399329a58873..4ef5153e20838 100644 --- a/llvm/lib/Transforms/Utils/Local.cpp +++ b/llvm/lib/Transforms/Utils/Local.cpp @@ -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 DbgDeclares; + findDbgDeclares(DbgDeclares, Address); for (DbgVariableIntrinsic *DII : DbgDeclares) { const DebugLoc &Loc = DII->getDebugLoc(); auto *DIVar = DII->getVariable(); diff --git a/llvm/lib/Transforms/Utils/MemoryOpRemark.cpp b/llvm/lib/Transforms/Utils/MemoryOpRemark.cpp index 531b0a624dafa..99c033cab64f8 100644 --- a/llvm/lib/Transforms/Utils/MemoryOpRemark.cpp +++ b/llvm/lib/Transforms/Utils/MemoryOpRemark.cpp @@ -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(V))) { + SmallVector DbgDeclares; + findDbgDeclares(DbgDeclares, const_cast(V)); + for (const DbgVariableIntrinsic *DVI : DbgDeclares) { if (DILocalVariable *DILV = DVI->getVariable()) { std::optional DISize = getSizeInBytes(DILV->getSizeInBits()); VariableInfo Var{DILV->getName(), DISize}; From 610796c5aac70fdbb4f179b67c577c1ebef9de9d Mon Sep 17 00:00:00 2001 From: OCHyams Date: Mon, 11 Dec 2023 14:23:54 +0000 Subject: [PATCH 2/3] set SmallVector size to 1 --- llvm/lib/Transforms/Coroutines/CoroFrame.cpp | 6 +++--- llvm/lib/Transforms/Scalar/SROA.cpp | 6 +++--- llvm/lib/Transforms/Utils/Local.cpp | 2 +- llvm/lib/Transforms/Utils/MemoryOpRemark.cpp | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/llvm/lib/Transforms/Coroutines/CoroFrame.cpp b/llvm/lib/Transforms/Coroutines/CoroFrame.cpp index fbacfb39f62ac..f05619d4541ca 100644 --- a/llvm/lib/Transforms/Coroutines/CoroFrame.cpp +++ b/llvm/lib/Transforms/Coroutines/CoroFrame.cpp @@ -963,7 +963,7 @@ static void cacheDIVar(FrameDataInfo &FrameData, if (DIVarCache.contains(V)) continue; - SmallVector DDIs; + SmallVector DDIs; findDbgDeclares(DDIs, V); auto *I = llvm::find_if(DDIs, [](DbgDeclareInst *DDI) { return DDI->getExpression()->getNumElements() == 0; @@ -1120,7 +1120,7 @@ static void buildFrameDebugInfo(Function &F, coro::Shape &Shape, assert(PromiseAlloca && "Coroutine with switch ABI should own Promise alloca"); - SmallVector DIs; + SmallVector DIs; findDbgDeclares(DIs, PromiseAlloca); if (DIs.empty()) return; @@ -1842,7 +1842,7 @@ static void insertSpills(const FrameDataInfo &FrameData, coro::Shape &Shape) { FrameTy->getElementType(FrameData.getFieldIndex(E.first)), GEP, SpillAlignment, E.first->getName() + Twine(".reload")); - SmallVector DIs; + SmallVector 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 diff --git a/llvm/lib/Transforms/Scalar/SROA.cpp b/llvm/lib/Transforms/Scalar/SROA.cpp index 92d5f970ea4cc..9c72cb67e4d64 100644 --- a/llvm/lib/Transforms/Scalar/SROA.cpp +++ b/llvm/lib/Transforms/Scalar/SROA.cpp @@ -4940,7 +4940,7 @@ bool SROA::splitAlloca(AllocaInst &AI, AllocaSlices &AS) { // Migrate debug information from the old alloca to the new alloca(s) // and the individual partitions. TinyPtrVector DbgVariables; - SmallVector DbgDeclares; + SmallVector DbgDeclares; findDbgDeclares(DbgDeclares, &AI); for (auto *DbgDeclare : DbgDeclares) DbgVariables.push_back(DbgDeclare); @@ -5000,7 +5000,7 @@ bool SROA::splitAlloca(AllocaInst &AI, AllocaSlices &AS) { // Remove any existing intrinsics on the new alloca describing // the variable fragment. - SmallVector FragDbgDeclares; + SmallVector FragDbgDeclares; findDbgDeclares(FragDbgDeclares, Fragment.Alloca); for (DbgDeclareInst *OldDII : FragDbgDeclares) { auto SameVariableFragment = [](const DbgVariableIntrinsic *LHS, @@ -5152,7 +5152,7 @@ bool SROA::deleteDeadInstructions( // not be able to find it. if (AllocaInst *AI = dyn_cast(I)) { DeletedAllocas.insert(AI); - SmallVector DbgDeclares; + SmallVector DbgDeclares; findDbgDeclares(DbgDeclares, AI); for (DbgDeclareInst *OldDII : DbgDeclares) OldDII->eraseFromParent(); diff --git a/llvm/lib/Transforms/Utils/Local.cpp b/llvm/lib/Transforms/Utils/Local.cpp index 4ef5153e20838..1635b765145d8 100644 --- a/llvm/lib/Transforms/Utils/Local.cpp +++ b/llvm/lib/Transforms/Utils/Local.cpp @@ -2103,7 +2103,7 @@ void llvm::insertDebugValuesForPHIs(BasicBlock *BB, bool llvm::replaceDbgDeclare(Value *Address, Value *NewAddress, DIBuilder &Builder, uint8_t DIExprFlags, int Offset) { - SmallVector DbgDeclares; + SmallVector DbgDeclares; findDbgDeclares(DbgDeclares, Address); for (DbgVariableIntrinsic *DII : DbgDeclares) { const DebugLoc &Loc = DII->getDebugLoc(); diff --git a/llvm/lib/Transforms/Utils/MemoryOpRemark.cpp b/llvm/lib/Transforms/Utils/MemoryOpRemark.cpp index 99c033cab64f8..5c3776683d5d6 100644 --- a/llvm/lib/Transforms/Utils/MemoryOpRemark.cpp +++ b/llvm/lib/Transforms/Utils/MemoryOpRemark.cpp @@ -321,7 +321,7 @@ 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. - SmallVector DbgDeclares; + SmallVector DbgDeclares; findDbgDeclares(DbgDeclares, const_cast(V)); for (const DbgVariableIntrinsic *DVI : DbgDeclares) { if (DILocalVariable *DILV = DVI->getVariable()) { From f4c06064983e3a1bbefcbc0c4fbf8b1863e1cbf5 Mon Sep 17 00:00:00 2001 From: OCHyams Date: Mon, 11 Dec 2023 15:10:57 +0000 Subject: [PATCH 3/3] rm shadowing variable --- llvm/lib/IR/DebugInfo.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/llvm/lib/IR/DebugInfo.cpp b/llvm/lib/IR/DebugInfo.cpp index e3aeac3a9fee6..414ec26ee81dc 100644 --- a/llvm/lib/IR/DebugInfo.cpp +++ b/llvm/lib/IR/DebugInfo.cpp @@ -57,10 +57,9 @@ void llvm::findDbgDeclares(SmallVectorImpl &DbgUsers, if (!MDV) return; - TinyPtrVector Declares; for (User *U : MDV->users()) { if (auto *DDI = dyn_cast(U)) - Declares.push_back(DDI); + DbgUsers.push_back(DDI); } }