[polly] r308113 - [ScopDetection] If a loop is not part of a scop, none of it backedges can be

Tobias Grosser via llvm-commits llvm-commits at lists.llvm.org
Sat Jul 15 15:42:17 PDT 2017


Author: grosser
Date: Sat Jul 15 15:42:17 2017
New Revision: 308113

URL: http://llvm.org/viewvc/llvm-project?rev=308113&view=rev
Log:
[ScopDetection] If a loop is not part of a scop, none of it backedges can be

This patch makes sure that in case a loop is not fully contained within a region
that later forms a SCoP, none of the loop backedges are allowed to be part of
the region. We currently do not support the situation where only some of a loops
backedges are part of a scop. Today, this can break both scop modeling and code
generation. One such breaking test case is for example
test/ScopDetectionDiagnostics/loop_partially_in_scop-2.ll, where we totally
forgot to code generate some of the backedges. Fortunately, it is commonly not
necessary to support these partial loops, it is way more common that either
no backedge is included in a region or all loop backedge are included.

This fixes a recent miscompile in
MultiSource/Benchmarks/MiBench/consumer-typeset which was exposed after
r306477.

Added:
    polly/trunk/test/ScopDetectionDiagnostics/loop_partially_in_scop-2.ll
    polly/trunk/test/ScopDetectionDiagnostics/loop_partially_in_scop.ll
Removed:
    polly/trunk/test/Isl/CodeGen/loop_partially_in_scop.ll
Modified:
    polly/trunk/include/polly/ScopDetectionDiagnostic.h
    polly/trunk/lib/Analysis/ScopDetection.cpp
    polly/trunk/lib/Analysis/ScopDetectionDiagnostic.cpp

Modified: polly/trunk/include/polly/ScopDetectionDiagnostic.h
URL: http://llvm.org/viewvc/llvm-project/polly/trunk/include/polly/ScopDetectionDiagnostic.h?rev=308113&r1=308112&r2=308113&view=diff
==============================================================================
--- polly/trunk/include/polly/ScopDetectionDiagnostic.h (original)
+++ polly/trunk/include/polly/ScopDetectionDiagnostic.h Sat Jul 15 15:42:17 2017
@@ -85,6 +85,7 @@ enum class RejectReasonKind {
 
   LoopBound,
   LoopHasNoExit,
+  LoopOnlySomeLatches,
 
   FuncCall,
   NonSimpleMemoryAccess,
@@ -553,6 +554,34 @@ public:
         Loc(L->getStartLoc()) {}
 
   /// @name LLVM-RTTI interface
+  //@{
+  static bool classof(const RejectReason *RR);
+  //@}
+
+  /// @name RejectReason interface
+  //@{
+  virtual std::string getMessage() const override;
+  virtual const DebugLoc &getDebugLoc() const override;
+  virtual std::string getEndUserMessage() const override;
+  //@}
+};
+
+//===----------------------------------------------------------------------===//
+/// Captures errors when not all loop latches are part of the scop.
+class ReportLoopOnlySomeLatches : public RejectReason {
+  //===--------------------------------------------------------------------===//
+
+  /// The loop for which not all loop latches are part of the scop.
+  Loop *L;
+
+  const DebugLoc Loc;
+
+public:
+  ReportLoopOnlySomeLatches(Loop *L)
+      : RejectReason(RejectReasonKind::LoopOnlySomeLatches), L(L),
+        Loc(L->getStartLoc()) {}
+
+  /// @name LLVM-RTTI interface
   //@{
   static bool classof(const RejectReason *RR);
   //@}

Modified: polly/trunk/lib/Analysis/ScopDetection.cpp
URL: http://llvm.org/viewvc/llvm-project/polly/trunk/lib/Analysis/ScopDetection.cpp?rev=308113&r1=308112&r2=308113&view=diff
==============================================================================
--- polly/trunk/lib/Analysis/ScopDetection.cpp (original)
+++ polly/trunk/lib/Analysis/ScopDetection.cpp Sat Jul 15 15:42:17 2017
@@ -1402,9 +1402,19 @@ bool ScopDetection::allBlocksValid(Detec
 
   for (const BasicBlock *BB : CurRegion.blocks()) {
     Loop *L = LI.getLoopFor(BB);
-    if (L && L->getHeader() == BB && CurRegion.contains(L) &&
-        (!isValidLoop(L, Context) && !KeepGoing))
-      return false;
+    if (L && L->getHeader() == BB) {
+      if (CurRegion.contains(L)) {
+        if (!isValidLoop(L, Context) && !KeepGoing)
+          return false;
+      } else {
+        SmallVector<BasicBlock *, 1> Latches;
+        L->getLoopLatches(Latches);
+        for (BasicBlock *Latch : Latches)
+          if (CurRegion.contains(Latch))
+            return invalid<ReportLoopOnlySomeLatches>(Context, /*Assert=*/true,
+                                                      L);
+      }
+    }
   }
 
   for (BasicBlock *BB : CurRegion.blocks()) {

Modified: polly/trunk/lib/Analysis/ScopDetectionDiagnostic.cpp
URL: http://llvm.org/viewvc/llvm-project/polly/trunk/lib/Analysis/ScopDetectionDiagnostic.cpp?rev=308113&r1=308112&r2=308113&view=diff
==============================================================================
--- polly/trunk/lib/Analysis/ScopDetectionDiagnostic.cpp (original)
+++ polly/trunk/lib/Analysis/ScopDetectionDiagnostic.cpp Sat Jul 15 15:42:17 2017
@@ -60,6 +60,7 @@ llvm::Statistic RejectStatistics[] = {
     SCOP_STAT(LastAffFunc, ""),
     SCOP_STAT(LoopBound, "Uncomputable loop bounds"),
     SCOP_STAT(LoopHasNoExit, "Loop without exit"),
+    SCOP_STAT(LoopOnlySomeLatches, "Not all loop latches in scop"),
     SCOP_STAT(FuncCall, "Function call with side effects"),
     SCOP_STAT(NonSimpleMemoryAccess,
               "Compilated access semantics (volatile or atomic)"),
@@ -393,6 +394,25 @@ std::string ReportLoopHasNoExit::getEndU
 }
 
 //===----------------------------------------------------------------------===//
+// ReportLoopOnlySomeLatches
+
+std::string ReportLoopOnlySomeLatches::getMessage() const {
+  return "Not all latches of loop " + L->getHeader()->getName() +
+         " part of scop.";
+}
+
+bool ReportLoopOnlySomeLatches::classof(const RejectReason *RR) {
+  return RR->getKind() == RejectReasonKind::LoopHasNoExit;
+}
+
+const DebugLoc &ReportLoopOnlySomeLatches::getDebugLoc() const { return Loc; }
+
+std::string ReportLoopOnlySomeLatches::getEndUserMessage() const {
+  return "Loop cannot be handled because not all latches are part of loop "
+         "region.";
+}
+
+//===----------------------------------------------------------------------===//
 // ReportFuncCall.
 
 ReportFuncCall::ReportFuncCall(Instruction *Inst)

Removed: polly/trunk/test/Isl/CodeGen/loop_partially_in_scop.ll
URL: http://llvm.org/viewvc/llvm-project/polly/trunk/test/Isl/CodeGen/loop_partially_in_scop.ll?rev=308112&view=auto
==============================================================================
--- polly/trunk/test/Isl/CodeGen/loop_partially_in_scop.ll (original)
+++ polly/trunk/test/Isl/CodeGen/loop_partially_in_scop.ll (removed)
@@ -1,49 +0,0 @@
-; RUN: opt %loadPolly -S -polly-codegen < %s | FileCheck %s
-;
-; Verify we do not crash for this test case and additionally check the code
-; that we generate for the %tmp PHI node in the non-affine region. This code
-; is difficult to generate as some incoming edges are from basic blocks
-; from within the region and others from basic blocks from outside of the
-; non-affine region. As visible in the CHECK lines, the code we generate
-; currently loads from the PHI twice in %polly.stmt.bb2.entry, which is
-; something we should avoid.
-;
-; CHECK: polly.start
-
-; CHECK: polly.stmt.bb2.entry:                             ; preds = %polly.start
-; CHECK-NEXT:   %tmp.phiops.reload = load i32, i32* %tmp.phiops
-; CHECK-NEXT:   br label %polly.stmt.bb2
-
-; CHECK: polly.stmt.bb2:                                   ; preds = %polly.stmt.bb2, %polly.stmt.bb2.entry
-; CHECK-NEXT:   %polly.tmp = phi i32 [ %tmp.phiops.reload, %polly.stmt.bb2.entry ], [ %p_tmp4, %polly.stmt.bb2 ]
-; CHECK-NEXT:   %p_tmp3 = or i32 undef, undef
-; CHECK-NEXT:   %p_tmp4 = udiv i32 %p_tmp3, 10
-; CHECK-NEXT:   %p_tmp6 = icmp eq i8 undef, 0
-; CHECK-NEXT:   br i1 %p_tmp6, label %polly.stmt.polly.merge_new_and_old.exit, label %polly.stmt.bb2
-
-target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
-
-define void @baz(i32 %before) {
-bb:
-  br label %bb1
-
-bb1:                                              ; preds = %bb
-  br label %bb2
-
-bb2:                                              ; preds = %bb8, %bb7, %bb2, %bb1
-  %tmp = phi i32 [ %before, %bb1 ], [ 0, %bb8 ], [ %tmp4, %bb7 ], [ %tmp4, %bb2 ]
-  %tmp3 = or i32 undef, undef
-  %tmp4 = udiv i32 %tmp3, 10
-  %tmp5 = trunc i32 undef to i8
-  %tmp6 = icmp eq i8 %tmp5, 0
-  br i1 %tmp6, label %bb7, label %bb2
-
-bb7:                                              ; preds = %bb2
-  br i1 undef, label %bb8, label %bb2
-
-bb8:                                              ; preds = %bb7
-  br i1 undef, label %bb9, label %bb2
-
-bb9:                                              ; preds = %bb8
-  unreachable
-}

Added: polly/trunk/test/ScopDetectionDiagnostics/loop_partially_in_scop-2.ll
URL: http://llvm.org/viewvc/llvm-project/polly/trunk/test/ScopDetectionDiagnostics/loop_partially_in_scop-2.ll?rev=308113&view=auto
==============================================================================
--- polly/trunk/test/ScopDetectionDiagnostics/loop_partially_in_scop-2.ll (added)
+++ polly/trunk/test/ScopDetectionDiagnostics/loop_partially_in_scop-2.ll Sat Jul 15 15:42:17 2017
@@ -0,0 +1,24 @@
+; RUN: opt %loadPolly -analyze -polly-detect \
+; RUN:     -pass-remarks-missed="polly-detect" \
+; RUN:     < %s 2>&1| FileCheck %s
+
+; CHECK: remark: <unknown>:0:0: Loop cannot be handled because not all latches are part of loop region.
+
+define void @foo(i8* %str0) {
+if.end32:
+  br label %while.cond
+
+while.cond:
+  %str.1 = phi i8* [%str0, %if.end32], [%incdec.ptr58364, %lor.end], [%incdec.ptr58364, %while.cond]
+  %tmp5 = load i8, i8* %str.1, align 1
+  %.off367 = add i8 %tmp5, -48
+  %tmp6 = icmp ult i8 %.off367, 10
+  %incdec.ptr58364 = getelementptr inbounds i8, i8* %str.1, i64 1
+  br i1 %tmp6, label %while.cond, label %lor.end
+
+lor.end:
+  br i1 false, label %exit, label %while.cond
+
+exit:
+  ret void
+}

Added: polly/trunk/test/ScopDetectionDiagnostics/loop_partially_in_scop.ll
URL: http://llvm.org/viewvc/llvm-project/polly/trunk/test/ScopDetectionDiagnostics/loop_partially_in_scop.ll?rev=308113&view=auto
==============================================================================
--- polly/trunk/test/ScopDetectionDiagnostics/loop_partially_in_scop.ll (added)
+++ polly/trunk/test/ScopDetectionDiagnostics/loop_partially_in_scop.ll Sat Jul 15 15:42:17 2017
@@ -0,0 +1,34 @@
+; RUN: opt %loadPolly -analyze -polly-detect \
+; RUN:     -pass-remarks-missed="polly-detect" \
+; RUN:     < %s 2>&1| FileCheck %s
+
+; CHECK: remark: <unknown>:0:0: Loop cannot be handled because not all latches are part of loop region.
+; CHECK: remark: <unknown>:0:0: Loop cannot be handled because not all latches are part of loop region.
+
+
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+
+define void @baz(i32 %before) {
+bb:
+  br label %bb1
+
+bb1:                                              ; preds = %bb
+  br label %bb2
+
+bb2:                                              ; preds = %bb8, %bb7, %bb2, %bb1
+  %tmp = phi i32 [ %before, %bb1 ], [ 0, %bb8 ], [ %tmp4, %bb7 ], [ %tmp4, %bb2 ]
+  %tmp3 = or i32 undef, undef
+  %tmp4 = udiv i32 %tmp3, 10
+  %tmp5 = trunc i32 undef to i8
+  %tmp6 = icmp eq i8 %tmp5, 0
+  br i1 %tmp6, label %bb7, label %bb2
+
+bb7:                                              ; preds = %bb2
+  br i1 undef, label %bb8, label %bb2
+
+bb8:                                              ; preds = %bb7
+  br i1 undef, label %bb9, label %bb2
+
+bb9:                                              ; preds = %bb8
+  unreachable
+}




More information about the llvm-commits mailing list