[PATCH] D133687: [LoopVectorize][Fix] Crash when invariant store address is calculated inside loop

Florian Hahn via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 21 09:37:47 PDT 2022


fhahn requested changes to this revision.
fhahn added a comment.
This revision now requires changes to proceed.

You could add something like `Fixes #57572` to the description, so the issue will be auto-closed once the fix is pushed.



================
Comment at: llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp:954
+      auto *Ptr = dyn_cast<Instruction>(SI->getPointerOperand());
+      if (Ptr && TheLoop->contains(Ptr)) {
+        reportVectorizationFailure(
----------------
I think the place for the check means we now may not vectorize cases we vectorized before, specifically if we have reductions and a store to an invariant address that's defined in the loop body that's not used by a reduction, as in the example below. 

You likely want to drop the `&& !getReductionVars().empty()) {` check above and only check the address if `isInvariantStoreOfReduction` is true.

```
define i32 @test(i32* %dst, i32* readonly %src) {
entry:
  br label %for.body

for.body:                                         ; preds = %for.body, %entry
  %sum = phi i32 [ 0, %entry ], [ %add, %for.body ]
  %iv = phi i64 [ 0, %entry ], [ %iv.next, %for.body ]
  %gep.src = getelementptr inbounds i32, i32* %src, i64 %iv
  %0 = load i32, i32* %gep.src, align 4
  %add = add nsw i32 %sum, %0
  %gep.dst = getelementptr inbounds i32, i32* %dst, i64 42
  store i32 0, i32* %gep.dst, align 4
  %iv.next = add nuw nsw i64 %iv, 1
  %exitcond = icmp eq i64 %iv.next, 1000
  br i1 %exitcond, label %exit, label %for.body

exit:                                             ; preds = %for.body
  %add.lcssa = phi i32 [ %add, %for.body ]
  ret i32 %add.lcssa
}
```


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D133687/new/

https://reviews.llvm.org/D133687



More information about the llvm-commits mailing list