[PATCH] D28490: [LV] Don't panic when encountering the IV of an outer loop.

Michael Kuperstein via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 9 15:10:23 PST 2017


mkuper created this revision.
mkuper added reviewers: delena, mssimpso, sbaranga.
mkuper added subscribers: llvm-commits, davide.

Bail out instead of asserting when we encounter this situation - which can actually happen.
Note that the assert I removed doesn't actually get hit (because it checks the wrong condition), but we assert a bit further down the line.

The reason the test runs LICM is because otherwise this situation - incidentally - gets cleaned up by LoopSimplify. But LICM creates the "bad" phi and preserves loop simplify form, so the cleanup has no chance to run.

This fixes PR31190.
We may want to solve this in a less conservative manner, since this phi is actually uniform within the inner loop (or we may want LICM to output a cleaner promotion to begin with), but I'd rather fix the crash for now.


https://reviews.llvm.org/D28490

Files:
  lib/Transforms/Utils/LoopUtils.cpp
  test/Transforms/LoopVectorize/pr31190.ll


Index: test/Transforms/LoopVectorize/pr31190.ll
===================================================================
--- test/Transforms/LoopVectorize/pr31190.ll
+++ test/Transforms/LoopVectorize/pr31190.ll
@@ -0,0 +1,34 @@
+; RUN: opt -licm -loop-vectorize < %s 2>&1 | FileCheck %s
+; REQUIRES: asserts
+
+ at c = external global i32, align 4
+ at a = external global i32, align 4
+ at b = external global [1 x i32], align 4
+
+; CHECK: LV: PHI is a recurrence with respect to an outer loop.
+; CHECK: LV: Not vectorizing: Cannot prove legality.
+; CHECK-LABEL: @test
+define void @test() {
+entry:
+  br label %for.cond1.preheader
+
+for.cond1.preheader:
+  br label %for.body3
+
+for.body3:
+  %0 = phi i32 [ undef, %for.cond1.preheader ], [ %2, %for.body3 ]
+  %idxprom = sext i32 %0 to i64
+  %arrayidx = getelementptr inbounds [1 x i32], [1 x i32]* @b, i64 0, i64 %idxprom
+  store i32 4, i32* %arrayidx, align 4
+  %1 = load i32, i32* @a, align 4
+  %inc = add nsw i32 %1, 1
+  store i32 %inc, i32* @a, align 4
+  %tobool2 = icmp eq i32 %inc, 0
+  %2 = load i32, i32* @c, align 4
+  br i1 %tobool2, label %for.cond1.for.inc4_crit_edge, label %for.body3
+
+for.cond1.for.inc4_crit_edge:
+  %inc5 = add nsw i32 %2, 1
+  store i32 %inc5, i32* @c, align 4
+  br label %for.cond1.preheader
+}
Index: lib/Transforms/Utils/LoopUtils.cpp
===================================================================
--- lib/Transforms/Utils/LoopUtils.cpp
+++ lib/Transforms/Utils/LoopUtils.cpp
@@ -869,8 +869,12 @@
     return false;
   }
 
-  assert(TheLoop->getHeader() == Phi->getParent() &&
-         "PHI is an AddRec for a different loop?!");
+  if (AR->getLoop() != TheLoop) {
+    // FIXME: We should probably just treat this as a uniform.
+    DEBUG(dbgs() << "LV: PHI is a recurrence with respect to an outer loop.\n");
+    return false;    
+  }
+
   Value *StartValue =
     Phi->getIncomingValueForBlock(AR->getLoop()->getLoopPreheader());
   const SCEV *Step = AR->getStepRecurrence(*SE);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D28490.83714.patch
Type: text/x-patch
Size: 1983 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170109/196cf117/attachment.bin>


More information about the llvm-commits mailing list