[llvm] [VectorCombine][AMDGPU] Shrink demanded vector loads (PR #202501)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Sep 15 02:38:06 PDT 2026
================
@@ -2157,6 +2158,173 @@ bool VectorCombine::scalarizeLoadExtract(LoadInst *LI, VectorType *VecTy,
return true;
}
+/// Try to shrink vector loads feeding extractelement instructions when some
+/// of the loaded lanes are not demanded.
+bool VectorCombine::shrinkLoadForExtracts(Instruction &I) {
+ auto *OldLoad = dyn_cast<LoadInst>(&I);
+ if (!OldLoad || !OldLoad->isSimple())
+ return false;
+
+ auto *OldLoadTy = dyn_cast<FixedVectorType>(OldLoad->getType());
+ if (!OldLoadTy)
+ return false;
+
+ Type *EltTy = OldLoadTy->getElementType();
+ if (!EltTy->isSized())
+ return false;
+
+ TypeSize EltSizeInBits = DL->getTypeSizeInBits(EltTy);
+ if (EltSizeInBits.isScalable())
+ return false;
+ uint64_t EltBits = EltSizeInBits.getFixedValue();
+ if (EltBits < 8 || EltBits % 8 != 0)
+ return false;
+ uint64_t EltBytes = EltBits / 8;
+
+ TypeSize OldLoadSize = DL->getTypeStoreSize(OldLoadTy);
+ if (OldLoadSize.isScalable())
+ return false;
+ InstructionCost OldLoadCost =
+ TTI.getMemoryOpCost(Instruction::Load, OldLoadTy, OldLoad->getAlign(),
+ OldLoad->getPointerAddressSpace(), CostKind);
+ if (!OldLoadCost.isValid())
+ return false;
+
+ struct ChunkRun {
+ unsigned Start;
+ unsigned Length;
+ };
+
+ unsigned NumElts = OldLoadTy->getNumElements();
+ SmallVector<ExtractElementInst *, 8> Extracts;
+ APInt DemandedElts = APInt::getZero(NumElts);
+
+ for (User *U : OldLoad->users()) {
+ auto *Ext = dyn_cast<ExtractElementInst>(U);
+ if (!Ext)
+ return false;
+ if (Ext->use_empty())
+ continue;
+
+ auto *Idx = dyn_cast<ConstantInt>(Ext->getIndexOperand());
+ if (!Idx || Idx->getValue().uge(NumElts))
+ return false;
+
+ Extracts.push_back(Ext);
+ DemandedElts.setBit(Idx->getZExtValue());
+ }
+
+ if (Extracts.empty() || DemandedElts.isAllOnes())
+ return false;
+
+ SmallVector<ChunkRun, 4> ElementRuns;
+ SmallVector<Type *, 4> NewLoadTys;
+ SmallVector<uint64_t, 4> NewLoadOffsets;
+
+ for (unsigned I = 0; I != NumElts;) {
+ if (!DemandedElts[I]) {
+ ++I;
+ continue;
+ }
+
+ unsigned Start = I;
+ do {
+ ++I;
+ } while (I != NumElts && DemandedElts[I]);
+ unsigned Length = I - Start;
+
+ // Single element runs are better handled by existing scalarization. Keep
+ // this fold focused on forming narrower vector loads.
+ if (Length == 1)
+ return false;
+
+ ElementRuns.push_back({Start, Length});
+ }
+
+ TypeSize NewLoadSize = TypeSize::getFixed(0);
+ InstructionCost NewLoadCost = 0;
+ for (const ChunkRun &Run : ElementRuns) {
+ auto *NewLoadTy = FixedVectorType::get(EltTy, Run.Length);
+ uint64_t NewLoadOffset = Run.Start * EltBytes;
+ NewLoadTys.push_back(NewLoadTy);
+ NewLoadOffsets.push_back(NewLoadOffset);
+ NewLoadSize += DL->getTypeStoreSize(NewLoadTy);
+ NewLoadCost +=
+ TTI.getMemoryOpCost(Instruction::Load, NewLoadTy,
+ commonAlignment(OldLoad->getAlign(), NewLoadOffset),
+ OldLoad->getPointerAddressSpace(), CostKind);
+ }
+
+ if (NewLoadSize >= OldLoadSize)
+ return false;
+
+ if (!NewLoadCost.isValid() || NewLoadCost > OldLoadCost)
----------------
tianhbai wrote:
For example, the original `<15 x i32>` load is 480 bits and has a VMEM width cost of `ceil(480 / 128) = 4`.
If the demanded elements form five disjoint `<2 x i32>` runs, shrinking creates five separate loads, each with cost 1, for a total cost of 5. Therefore, the transformation loads fewer bytes but requires more memory instructions.
https://github.com/llvm/llvm-project/pull/202501
More information about the llvm-commits
mailing list