[llvm] [LoadStoreVectorizer] Fill gaps in load/store chains to enable vectorization (PR #159388)

Akshay Deodhar via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 5 10:32:18 PST 2025


================
@@ -623,6 +658,39 @@ std::vector<Chain> Vectorizer::splitChainByContiguity(Chain &C) {
     dumpChain(C);
   });
 
+  // If the chain is not contiguous, we try to fill the gap with "extra"
+  // elements to artificially make it contiguous, to try to enable
+  // vectorization. We only fill gaps if there is potential to end up with a
+  // legal masked load/store given the target, address space, and element type.
+  // At this point, when querying the TTI, optimistically assume max alignment
+  // and max vector size, as splitChainByAlignment will ensure the final vector
+  // shape passes the legalization check.
+  unsigned AS = getLoadStoreAddressSpace(C[0].Inst);
+  Type *ElementType = getLoadStoreType(C[0].Inst)->getScalarType();
+  unsigned MaxVecRegBits = TTI.getLoadStoreVecRegBitWidth(AS);
+  Align OptimisticAlign = Align(MaxVecRegBits / 8);
+  unsigned int MaxVectorNumElems =
+      MaxVecRegBits / DL.getTypeSizeInBits(ElementType);
+  FixedVectorType *OptimisticVectorType =
+      FixedVectorType::get(ElementType, MaxVectorNumElems);
+  bool TryFillGaps =
+      isa<LoadInst>(C[0].Inst)
+          ? TTI.isLegalMaskedLoad(OptimisticVectorType, OptimisticAlign, AS,
+                                  TTI::MaskKind::ConstantMask)
+          : TTI.isLegalMaskedStore(OptimisticVectorType, OptimisticAlign, AS,
+                                   TTI::MaskKind::ConstantMask);
+
+  // Derive the alignment of the leader of the chain (which every
+  // OffsetFromLeader is based on) using the current first element of the chain.
+  // We could derive a better alignment by iterating over the entire chain but
+  // this should be sufficient. We use this value to derive the alignment of any
+  // extra elements we create while gap filling.
+  Align LeaderOfChainAlign =
+      commonAlignment(getLoadStoreAlignment(C[0].Inst),
+                      C[0].OffsetFromLeader.abs().getLimitedValue());
+
+  unsigned ASPtrBits = DL.getIndexSizeInBits(AS);
+
   std::vector<Chain> Ret;
----------------
akshayrdeodhar wrote:

If we have an estimate for how long chains will be, usually (4 elements, say), then having a smallvector [avoids dynamic allocation](https://llvm.org/docs/ProgrammersManual.html), and is recommended.

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


More information about the llvm-commits mailing list