[llvm] [VectorCombine][AMDGPU] Shrink demanded vector loads (PR #202501)
Matt Arsenault via llvm-commits
llvm-commits at lists.llvm.org
Mon Sep 14 01:07:26 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]);
----------------
arsenm wrote:
DemandedElts.countLeadingOnes?
https://github.com/llvm/llvm-project/pull/202501
More information about the llvm-commits
mailing list