[llvm] [Vectorizer] fix GEPs incorrectly marked as "inbounds" (PR #120730)

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Sun Jan 5 05:21:19 PST 2025


================
@@ -8344,10 +8344,10 @@ VPRecipeBuilder::tryToWidenMemory(Instruction *I, ArrayRef<VPValue *> Operands,
         Ptr->getUnderlyingValue()->stripPointerCasts());
     VPSingleDefRecipe *VectorPtr;
     if (Reverse)
+      // N.B. we deliberately do pass getGEPNoWrapFlags here, because this
+      // transform can invalidate `inbounds`.
       VectorPtr = new VPReverseVectorPointerRecipe(
----------------
fhahn wrote:

Given that there are quite a number of unnecessary  test changes now that all tests have been updated, I think it would be better to try and do the precise check straight-away, to avoid unneeded churn

I think the check should be something like below?
```suggestion
     if (Reverse) { 
      // When folding the tail, we may compute an address that we don't in the
      // original scalar loop and it may not be inbounds. Drop Inbounds in that
      // case.
      GEPNoWrapFlags Flags =
          (CM.foldTailByMasking() || !GEP || !GEP->isInBounds())
              ? GEPNoWrapFlags::none()
              : GEPNoWrapFlags::inBounds();
      VectorPtr = new VPReverseVectorPointerRecipe(
          Ptr, &Plan.getVF(), getLoadStoreType(I), Flags, I->getDebugLoc());
    } else
```

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


More information about the llvm-commits mailing list