[PATCH] D140260: [LoopVectorize] Fix crash on "vector->scalar" bitcast vectorization

Mindong Chen via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Dec 17 08:03:47 PST 2022


mdchen created this revision.
Herald added subscribers: shiva0217, hiraditya.
Herald added a project: All.
mdchen requested review of this revision.
Herald added subscribers: llvm-commits, pcwang-thead.
Herald added a project: LLVM.

It's possible for a scalar bitcast takes vector input and the current
LoopVectorize cannot manage it correctly and crash. Because a scalar
loop only contains scalar instructions that cannot update vectors, the
first vector operand of such bitcasts must be loop-invariant and should
be uniform.


https://reviews.llvm.org/D140260

Files:
  llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
  llvm/test/Transforms/LoopVectorize/vector2scalar-bitcast.ll


Index: llvm/test/Transforms/LoopVectorize/vector2scalar-bitcast.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/LoopVectorize/vector2scalar-bitcast.ll
@@ -0,0 +1,30 @@
+; REQUIRES: assert
+; RUN: opt < %s -loop-vectorize -force-vector-width=2 -S 2>&1 | FileCheck %s
+
+; Function Attrs: argmemonly nofree norecurse nosync nounwind uwtable
+define dso_local i64 @foo(ptr nocapture noundef %a, i32 noundef %n, <2 x float> %in2) {
+entry:
+  %cmp3 = icmp sgt i32 %n, 0
+  br i1 %cmp3, label %for.body.preheader, label %for.cond.cleanup
+
+for.body.preheader:                               ; preds = %entry
+  %wide.trip.count = zext i32 %n to i64
+  br label %for.body
+
+for.cond.cleanup:                                 ; preds = %for.body, %entry
+  %result = phi i64 [ 0, %entry], [ %illegal, %for.body]
+  ret i64 %result
+
+for.body:                                         ; preds = %for.body.preheader, %for.body
+  %indvars.iv = phi i64 [ 0, %for.body.preheader ], [ %indvars.iv.next, %for.body ]
+  %arrayidx = getelementptr inbounds i32, ptr %a, i64 %indvars.iv
+  %illegal = bitcast <2 x float> %in2 to i64
+  %0 = load i32, ptr %arrayidx, align 4
+  %add = add nsw i32 %0, 1
+  store i32 %add, ptr %arrayidx, align 4
+  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
+  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
+  br i1 %exitcond.not, label %for.cond.cleanup, label %for.body
+}
+
+
Index: llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
===================================================================
--- llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
+++ llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
@@ -4730,6 +4730,16 @@
         }
       }
 
+      // When the first operand of a bitcast instruction is of vector type, its
+      // vector operand must be loop-invariant and the bitcast must be uniform.
+      if (isa<BitCastInst>(&I) && I.getOperand(0)->getType()->isVectorTy()) {
+        assert(TheLoop->hasLoopInvariantOperands(&I) &&
+               "Vector operand must be loop-invariant");
+        LLVM_DEBUG(dbgs() << "LV: Found uniform instruction: " << I << "\n");
+        addToWorklistIfAllowed(&I);
+        continue;
+      }
+
       // ExtractValue instructions must be uniform, because the operands are
       // known to be loop-invariant.
       if (auto *EVI = dyn_cast<ExtractValueInst>(&I)) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D140260.483752.patch
Type: text/x-patch
Size: 2427 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221217/cc40ca17/attachment.bin>


More information about the llvm-commits mailing list