[llvm] [LV] Vectorize bounded (i % 2^N) loads in read only loops w/o RT checks. (PR #207279)

David Sherwood via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 23 08:00:40 PDT 2026


================
@@ -7332,6 +7332,35 @@ static std::optional<int64_t> getConstantStride(VPValue *Addr, Type *AccessTy,
   return getStrideFromAddRec(AddRec, L, AccessTy, /*Ptr=*/nullptr, PSE);
 }
 
+/// Check if \p VPI is a bounded (i % 2^N) load we can widen. This requires the
+/// vector factor to divide the bound, so that each VF-wide load stays within a
+/// single 2^N window.
+static bool canCreateBoundedLoad(VPInstruction *VPI, VFRange &Range,
+                                 VPCostContext &Ctx) {
+  if (VPI->getOpcode() != Instruction::Load)
+    return false;
+
+  Type *ScalarTy = VPI->getScalarType();
+  if (hasIrregularType(ScalarTy, Ctx.L->getHeader()->getDataLayout()))
+    return false;
+
+  const SCEV *PtrSCEV =
+      vputils::getSCEVExprForVPValue(VPI->getOperand(0), Ctx.PSE, Ctx.L);
+  std::optional<uint64_t> Bound =
+      getBoundedAccessBound(PtrSCEV, ScalarTy, Ctx.L, *Ctx.PSE.getSE());
+  if (!Bound)
+    return false;
+
+  // Only widen for VFs that divide the bound, so each VF-wide load stays within
+  // a single window and does not wrap.
+  auto DividesBound = [&](ElementCount VF) {
+    return VF.isFixed() && VF.getFixedValue() <= *Bound &&
----------------
david-arm wrote:

See earlier comment about scalable vectors. Would be good to commonise the code with that in `memoryInstructionCanBeWidened`.

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


More information about the llvm-commits mailing list