[llvm] [VectorCombine] Fold contiguous loads into a single vector load (PR #185736)

Simon Pilgrim via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 1 05:53:59 PDT 2026


================
@@ -5749,6 +5748,91 @@ bool VectorCombine::shrinkPhiOfShuffles(Instruction &I) {
   return true;
 }
 
+/// Check if a vector instruction's lanes originate from contiguous memory
+/// accesses. Fold the original loads and shuffles into a single vector load
+/// if it is profitable. For example:
+///     shufflevector(load <4 x float> ptr), poison, <2, 3>
+///       -> load <2 x float> (ptradd ptr, 8)
+/// Cost model calculations take into account the cost of the original
+/// unique load(s) and the target instruction versus the cost of the new
+/// aligned vector load.
+bool VectorCombine::foldContiguousLoads(Instruction &I) {
+  auto *VT = dyn_cast<FixedVectorType>(I.getType());
+  if (!VT || I.use_empty())
+    return false;
+
+  unsigned ElementSize = VT->getElementType()->getScalarSizeInBits();
+  unsigned NumElts = VT->getNumElements();
+  Type *EltTy = VT->getElementType();
+  Value *CommonBase = nullptr;
+  int64_t ExpectedBaseBitOffset = 0, FirstLoadOffset = 0;
+  LoadInst *FirstLI = nullptr;
+  SmallPtrSet<LoadInst *, 4> Loads;
+  for (unsigned Lane = 0; Lane < NumElts; ++Lane) {
+    InstLane IL = lookThroughShuffles(&*I.use_begin(), Lane);
+    if (!IL.first)
+      return false;
+
+    auto *LI = dyn_cast<LoadInst>(IL.first->get());
+    if (!LI)
+      return false;
+
+    if (!LI->isSimple() || !LI->hasOneUse())
+      return false;
+
+    auto *LIVTy = dyn_cast<FixedVectorType>(LI->getType());
+    if (!LIVTy || LIVTy->getElementType() != EltTy)
----------------
RKSimon wrote:

any luck with this?

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


More information about the llvm-commits mailing list