[polly] r297151 - [ScopDetection] Do not detect scops that exit to an unreachable

Tobias Grosser via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 7 07:50:43 PST 2017


Author: grosser
Date: Tue Mar  7 09:50:43 2017
New Revision: 297151

URL: http://llvm.org/viewvc/llvm-project?rev=297151&view=rev
Log:
[ScopDetection] Do not detect scops that exit to an unreachable

Scops that exit with an unreachable are today still permitted, but make little
sense to optimize. We therefore can already skip them during scop detection.
This speeds up scop detection in certain cases and also ensures that bugpoint
does not introduce unreachables when reducing test cases.

In practice this change should have little impact, as the performance of
unreachable code is unlikely to matter.

This commit is part of a series that makes Polly more robust in the presence
of unreachables.

Added:
    polly/trunk/test/ScopDetectionDiagnostics/ReportUnreachableInExit.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=297151&r1=297150&r2=297151&view=diff
==============================================================================
--- polly/trunk/include/polly/ScopDetectionDiagnostic.h (original)
+++ polly/trunk/include/polly/ScopDetectionDiagnostic.h Tue Mar  7 09:50:43 2017
@@ -67,6 +67,7 @@ enum class RejectReasonKind {
   CFG,
   InvalidTerminator,
   IrreducibleRegion,
+  UnreachableInExit,
   LastCFG,
 
   // Non-Affinity
@@ -218,6 +219,30 @@ public:
 
   /// @name LLVM-RTTI interface
   //@{
+  static bool classof(const RejectReason *RR);
+  //@}
+
+  /// @name RejectReason interface
+  //@{
+  virtual std::string getMessage() const override;
+  virtual std::string getEndUserMessage() const override;
+  virtual const DebugLoc &getDebugLoc() const override;
+  //@}
+};
+
+//===----------------------------------------------------------------------===//
+/// Captures regions with an unreachable in the exit block.
+class ReportUnreachableInExit : public ReportCFG {
+  BasicBlock *BB;
+  DebugLoc DbgLoc;
+
+public:
+  ReportUnreachableInExit(BasicBlock *BB, DebugLoc DbgLoc)
+      : ReportCFG(RejectReasonKind::UnreachableInExit), BB(BB), DbgLoc(DbgLoc) {
+  }
+
+  /// @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=297151&r1=297150&r2=297151&view=diff
==============================================================================
--- polly/trunk/lib/Analysis/ScopDetection.cpp (original)
+++ polly/trunk/lib/Analysis/ScopDetection.cpp Tue Mar  7 09:50:43 2017
@@ -1428,6 +1428,13 @@ bool ScopDetection::isValidRegion(Detect
     return false;
   }
 
+  DebugLoc DbgLoc;
+  if (isa<UnreachableInst>(CurRegion.getExit()->getTerminator())) {
+    DEBUG(dbgs() << "Unreachable in exit\n");
+    return invalid<ReportUnreachableInExit>(Context, /*Assert=*/true,
+                                            CurRegion.getExit(), DbgLoc);
+  }
+
   if (!CurRegion.getEntry()->getName().count(OnlyRegion)) {
     DEBUG({
       dbgs() << "Region entry does not match -polly-region-only";
@@ -1445,7 +1452,6 @@ bool ScopDetection::isValidRegion(Detect
   if (!allBlocksValid(Context))
     return false;
 
-  DebugLoc DbgLoc;
   if (!isReducibleRegion(CurRegion, DbgLoc))
     return invalid<ReportIrreducibleRegion>(Context, /*Assert=*/true,
                                             &CurRegion, DbgLoc);

Modified: polly/trunk/lib/Analysis/ScopDetectionDiagnostic.cpp
URL: http://llvm.org/viewvc/llvm-project/polly/trunk/lib/Analysis/ScopDetectionDiagnostic.cpp?rev=297151&r1=297150&r2=297151&view=diff
==============================================================================
--- polly/trunk/lib/Analysis/ScopDetectionDiagnostic.cpp (original)
+++ polly/trunk/lib/Analysis/ScopDetectionDiagnostic.cpp Tue Mar  7 09:50:43 2017
@@ -44,6 +44,7 @@ using namespace llvm;
 llvm::Statistic RejectStatistics[] = {
     SCOP_STAT(CFG, ""),
     SCOP_STAT(InvalidTerminator, "Unsupported terminator instruction"),
+    SCOP_STAT(UnreachableInExit, "Unreachable in exit block"),
     SCOP_STAT(IrreducibleRegion, "Irreducible loops"),
     SCOP_STAT(LastCFG, ""),
     SCOP_STAT(AffFunc, ""),
@@ -190,6 +191,24 @@ bool ReportInvalidTerminator::classof(co
 }
 
 //===----------------------------------------------------------------------===//
+// UnreachableInExit.
+
+std::string ReportUnreachableInExit::getMessage() const {
+  std::string BBName = BB->getName();
+  return "Unreachable in exit block" + BBName;
+}
+
+const DebugLoc &ReportUnreachableInExit::getDebugLoc() const { return DbgLoc; }
+
+std::string ReportUnreachableInExit::getEndUserMessage() const {
+  return "Unreachable in exit block.";
+}
+
+bool ReportUnreachableInExit::classof(const RejectReason *RR) {
+  return RR->getKind() == RejectReasonKind::UnreachableInExit;
+}
+
+//===----------------------------------------------------------------------===//
 // ReportIrreducibleRegion.
 
 std::string ReportIrreducibleRegion::getMessage() const {

Added: polly/trunk/test/ScopDetectionDiagnostics/ReportUnreachableInExit.ll
URL: http://llvm.org/viewvc/llvm-project/polly/trunk/test/ScopDetectionDiagnostics/ReportUnreachableInExit.ll?rev=297151&view=auto
==============================================================================
--- polly/trunk/test/ScopDetectionDiagnostics/ReportUnreachableInExit.ll (added)
+++ polly/trunk/test/ScopDetectionDiagnostics/ReportUnreachableInExit.ll Tue Mar  7 09:50:43 2017
@@ -0,0 +1,31 @@
+; RUN: opt %loadPolly -polly-detect -analyze < %s \
+; RUN:     -pass-remarks-missed="polly-detect" 2>&1 | FileCheck %s
+
+; void f(long A[], long N) {
+;   long i;
+;   for (i = 0; i < N; ++i)
+;     A[i] = i;
+;   unreachable()
+; }
+
+target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128"
+
+define void @f(i64* %A, i64 %N) nounwind {
+entry:
+  fence seq_cst
+  br label %for.i
+
+for.i:
+  %indvar = phi i64 [ 0, %entry ], [ %indvar.next, %for.i ]
+  %scevgep = getelementptr i64, i64* %A, i64 %indvar
+  store i64 %indvar, i64* %scevgep
+  %indvar.next = add nsw i64 %indvar, 1
+  %exitcond = icmp eq i64 %indvar.next, %N
+  br i1 %exitcond, label %return, label %for.i
+
+return:
+  fence seq_cst
+  unreachable
+}
+
+; CHECK: Unreachable in exit block




More information about the llvm-commits mailing list