[llvm] 02232b9 - [CoroFrame][NFC] Remove code duplication in insertSpills (#177367)

via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 26 04:15:46 PST 2026


Author: Felipe de Azevedo Piovezan
Date: 2026-01-26T12:15:42Z
New Revision: 02232b9a61b1a134e0c42f74f26135d17f957dd5

URL: https://github.com/llvm/llvm-project/commit/02232b9a61b1a134e0c42f74f26135d17f957dd5
DIFF: https://github.com/llvm/llvm-project/commit/02232b9a61b1a134e0c42f74f26135d17f957dd5.diff

LOG: [CoroFrame][NFC] Remove code duplication in insertSpills (#177367)

This commit removes a duplicated loop looking through loads when finding
dbg records associated with a Value.

Added: 
    

Modified: 
    llvm/lib/Transforms/Coroutines/CoroFrame.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Coroutines/CoroFrame.cpp b/llvm/lib/Transforms/Coroutines/CoroFrame.cpp
index 9bd7e0b48c2d3..2f2ef7f9eee2c 100644
--- a/llvm/lib/Transforms/Coroutines/CoroFrame.cpp
+++ b/llvm/lib/Transforms/Coroutines/CoroFrame.cpp
@@ -1057,6 +1057,37 @@ static Value *createGEPToFramePointer(const FrameDataInfo &FrameData,
   return GEP;
 }
 
+/// Find dbg.declare or dbg.declare_value records referencing `Def`. If none are
+/// found, walk up the load chain to find one.
+template <DbgVariableRecord::LocationType record_type>
+static TinyPtrVector<DbgVariableRecord *>
+findDbgRecordsThroughLoads(Function &F, Value *Def) {
+  static_assert(record_type == DbgVariableRecord::LocationType::Declare ||
+                record_type == DbgVariableRecord::LocationType::DeclareValue);
+  constexpr auto FindFunc =
+      record_type == DbgVariableRecord::LocationType::Declare
+          ? findDVRDeclares
+          : findDVRDeclareValues;
+
+  TinyPtrVector<DbgVariableRecord *> Records = FindFunc(Def);
+
+  if (!F.getSubprogram())
+    return Records;
+
+  Value *CurDef = Def;
+  while (Records.empty() && isa<LoadInst>(CurDef)) {
+    auto *LdInst = cast<LoadInst>(CurDef);
+    if (!LdInst->getType()->isPointerTy())
+      break;
+    CurDef = LdInst->getPointerOperand();
+    if (!isa<AllocaInst, LoadInst>(CurDef))
+      break;
+    Records = FindFunc(CurDef);
+  }
+
+  return Records;
+}
+
 // Replace all alloca and SSA values that are accessed across suspend points
 // with GetElementPointer from coroutine frame + loads and stores. Create an
 // AllocaSpillBB that will become the new entry block for the resume parts of
@@ -1132,23 +1163,8 @@ static void insertSpills(const FrameDataInfo &FrameData, coro::Shape &Shape) {
           CurrentReload = LI;
         }
 
-        TinyPtrVector<DbgVariableRecord *> DVRs = findDVRDeclares(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.
-        if (F->getSubprogram()) {
-          auto *CurDef = Def;
-          while (DVRs.empty() && isa<LoadInst>(CurDef)) {
-            auto *LdInst = cast<LoadInst>(CurDef);
-            // Only consider ptr to ptr same type load.
-            if (LdInst->getPointerOperandType() != LdInst->getType())
-              break;
-            CurDef = LdInst->getPointerOperand();
-            if (!isa<AllocaInst, LoadInst>(CurDef))
-              break;
-            DVRs = findDVRDeclares(CurDef);
-          }
-        }
+        TinyPtrVector<DbgVariableRecord *> DVRs = findDbgRecordsThroughLoads<
+            DbgVariableRecord::LocationType::Declare>(*F, Def);
 
         auto SalvageOne = [&](DbgVariableRecord *DDI) {
           // This dbg.declare is preserved for all coro-split function
@@ -1168,23 +1184,8 @@ static void insertSpills(const FrameDataInfo &FrameData, coro::Shape &Shape) {
       }
 
       TinyPtrVector<DbgVariableRecord *> DVRDeclareValues =
-          findDVRDeclareValues(Def);
-      // Try best to find dbg.declare_value. If the spill is a temp, there may
-      // not be a direct dbg.declare_value. Walk up the load chain to find one
-      // from an alias.
-      if (F->getSubprogram()) {
-        auto *CurDef = Def;
-        while (DVRDeclareValues.empty() && isa<LoadInst>(CurDef)) {
-          auto *LdInst = cast<LoadInst>(CurDef);
-          // Only consider ptr to ptr same type load.
-          if (LdInst->getPointerOperandType() != LdInst->getType())
-            break;
-          CurDef = LdInst->getPointerOperand();
-          if (!isa<AllocaInst, LoadInst>(CurDef))
-            break;
-          DVRDeclareValues = findDVRDeclareValues(CurDef);
-        }
-      }
+          findDbgRecordsThroughLoads<
+              DbgVariableRecord::LocationType::DeclareValue>(*F, Def);
 
       auto SalvageOneCoro = [&](auto *DDI) {
         // This dbg.declare_value is preserved for all coro-split function


        


More information about the llvm-commits mailing list