[llvm] [AMDGPU] Promote allocas used by phi/select (PR #221870)
Matt Arsenault via llvm-commits
llvm-commits at lists.llvm.org
Sun Sep 13 07:37:57 PDT 2026
================
@@ -464,17 +549,52 @@ static bool isSupportedMemset(MemSetInst *I, AllocaInst *AI,
}
static Value *calculateVectorIndex(Value *Ptr, AllocaAnalysis &AA) {
- IRBuilder<> B(Ptr->getContext());
-
+ LLVMContext &Ctx = Ptr->getContext();
Ptr = Ptr->stripPointerCasts();
- if (Ptr == AA.Alloca)
- return B.getInt32(0);
+
+ // Already computed (also breaks cycles for pointer phis).
+ auto Cached = AA.Vector.IndexCache.find(Ptr);
+ if (Cached != AA.Vector.IndexCache.end())
+ return Cached->second;
+
+ // A pointer that is directly one of the member allocas indexes lane
+ // BaseLane[member].
+ if (auto *AI = dyn_cast<AllocaInst>(Ptr)) {
+ Value *Idx =
+ ConstantInt::get(Type::getInt32Ty(Ctx), AA.Vector.BaseLane.lookup(AI));
+ AA.Vector.IndexCache[Ptr] = Idx;
+ return Idx;
+ }
+
+ // Pointer phi: build a parallel phi of lane indices. Insert and cache it
+ // before recursing so self-referential phis terminate.
+ if (auto *Phi = dyn_cast<PHINode>(Ptr)) {
+ IRBuilder<> B(Phi);
+ PHINode *IdxPhi = B.CreatePHI(B.getInt32Ty(), Phi->getNumIncomingValues(),
+ "promotealloca.idx");
+ AA.Vector.IndexCache[Ptr] = IdxPhi;
+ for (unsigned I = 0, E = Phi->getNumIncomingValues(); I != E; ++I)
----------------
arsenm wrote:
Braces
https://github.com/llvm/llvm-project/pull/221870
More information about the llvm-commits
mailing list