[llvm] [AMDGPU] Visit PromoteAlloca users in dominance order (PR #215686)

Shilei Tian via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 11 16:48:10 PDT 2026


================
@@ -848,28 +849,45 @@ static bool isSupportedAccessType(FixedVectorType *VecTy, Type *AccessTy,
 }
 
 /// Iterates over an instruction worklist that may contain multiple instructions
-/// from the same basic block, but in a different order.
+/// from the same basic block, but in a different order. Order the basic blocks
+/// in dominance-compatible order.
 template <typename InstContainer>
 static void forEachWorkListItem(const InstContainer &WorkList,
                                 std::function<void(Instruction *)> Fn) {
+  if (WorkList.empty())
+    return;
+
   // Bucket up uses of the alloca by the block they occur in.
   // This is important because we have to handle multiple defs/uses in a block
   // ourselves: SSAUpdater is purely for cross-block references.
   DenseMap<BasicBlock *, SmallDenseSet<Instruction *>> UsesByBlock;
-  for (Instruction *User : WorkList)
-    UsesByBlock[User->getParent()].insert(User);
-
+  SmallVector<BasicBlock *> Blocks;
   for (Instruction *User : WorkList) {
-    BasicBlock *BB = User->getParent();
-    auto &BlockUses = UsesByBlock[BB];
-
-    // Already processed, skip.
+    auto &BlockUses = UsesByBlock[User->getParent()];
     if (BlockUses.empty())
-      continue;
+      Blocks.push_back(User->getParent());
+    BlockUses.insert(User);
+  }
+
+  // Visit blocks in reverse post-order so that a block is processed after
+  // any block that dominates it. Blocks unreachable from the entry keep
+  // their first-encounter order after all reachable blocks.
+  if (Blocks.size() > 1) {
+    DenseMap<BasicBlock *, unsigned> RPONumber;
+    for (BasicBlock *BB :
+         ReversePostOrderTraversal<Function *>(Blocks.front()->getParent()))
+      RPONumber.try_emplace(BB, RPONumber.size());
+    stable_sort(Blocks, [&](BasicBlock *A, BasicBlock *B) {
+      return RPONumber.lookup_or(A, ~0u) < RPONumber.lookup_or(B, ~0u);
+    });
----------------
shiltian wrote:

Does it have to be this complicated? Just a walk of RPO doesn't work?

https://github.com/llvm/llvm-project/pull/215686


More information about the llvm-commits mailing list