[llvm] [LoopUnroll] Introduce UnrollMaxIterations as a hard cap on how many iterations we try to unroll (PR #78648)

via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 23 15:35:50 PST 2024


https://github.com/modiking updated https://github.com/llvm/llvm-project/pull/78648

>From d8c56097c466d9acd979b02036d4158801f115e4 Mon Sep 17 00:00:00 2001
From: modimo <modimo at fb.com>
Date: Thu, 18 Jan 2024 16:06:43 -0800
Subject: [PATCH 1/2] [LoopUnroll] Introduce UnrollMaxIterations as a hard cap
 on how many iterations we try to unroll

---
 llvm/lib/Transforms/Utils/LoopUnroll.cpp   | 12 +++++++
 llvm/test/Transforms/LoopUnroll/pr77842.ll | 37 ++++++++++++++++++++++
 2 files changed, 49 insertions(+)
 create mode 100644 llvm/test/Transforms/LoopUnroll/pr77842.ll

diff --git a/llvm/lib/Transforms/Utils/LoopUnroll.cpp b/llvm/lib/Transforms/Utils/LoopUnroll.cpp
index ee6f7b35750af0f..8529fa1db18e187 100644
--- a/llvm/lib/Transforms/Utils/LoopUnroll.cpp
+++ b/llvm/lib/Transforms/Utils/LoopUnroll.cpp
@@ -109,6 +109,10 @@ UnrollVerifyLoopInfo("unroll-verify-loopinfo", cl::Hidden,
 #endif
                     );
 
+static cl::opt<unsigned>
+    UnrollMaxIterations("unroll-max-iterations", cl::init(1'000'000),
+                        cl::Hidden,
+                        cl::desc("Maximum allowed iterations to unroll."));
 
 /// Check if unrolling created a situation where we need to insert phi nodes to
 /// preserve LCSSA form.
@@ -453,6 +457,14 @@ LoopUnrollResult llvm::UnrollLoop(Loop *L, UnrollLoopOptions ULO, LoopInfo *LI,
     }
   }
 
+  // Certain cases with UBSAN can cause trip count to be calculated as INT_MAX,
+  // Block unrolling at a reasonable limit so that the compiler doesn't hang
+  // trying to unroll the loop. See PR77842
+  if (ULO.Count > UnrollMaxIterations) {
+    LLVM_DEBUG(dbgs() << "Won't unroll; trip count is too large\n");
+    return LoopUnrollResult::Unmodified;
+  }
+
   using namespace ore;
   // Report the unrolling decision.
   if (CompletelyUnroll) {
diff --git a/llvm/test/Transforms/LoopUnroll/pr77842.ll b/llvm/test/Transforms/LoopUnroll/pr77842.ll
new file mode 100644
index 000000000000000..834033bbe3618eb
--- /dev/null
+++ b/llvm/test/Transforms/LoopUnroll/pr77842.ll
@@ -0,0 +1,37 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -passes=loop-unroll -disable-output -debug-only=loop-unroll %s 2>&1 | FileCheck %s
+
+; Validate that loop unroll full doesn't try to fully unroll values whose trip counts are too large.
+
+; CHECK: Exiting block %cont23: TripCount=2147483648, TripMultiple=0, BreakoutTrip=0
+; CHECK-NEXT: Won't unroll; trip count is too large
+
+target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-redhat-linux-gnu"
+
+define void @foo(i64 %end) {
+entry:
+  br label %loopheader
+
+loopheader:
+  %iv = phi i64 [ 0, %entry ], [ %iv_new, %backedge ]
+  %exit = icmp eq i64 %iv, %end
+  br i1 %exit, label %for.cond.cleanup.loopexit, label %cont23
+
+for.cond.cleanup.loopexit:
+  ret void
+
+cont23:
+  %exitcond241 = icmp eq i64 %iv, 2147483647
+  br i1 %exitcond241, label %handler.add_overflow, label %backedge
+
+handler.add_overflow:
+  unreachable
+
+backedge: ; preds = %cont23
+  %iv_new = add i64 %iv, 1
+  br label %loopheader, !llvm.loop !0
+}
+
+!0 = distinct !{!0, !1}
+!1 = !{!"llvm.loop.unroll.full"}

>From 88b1087035fd397996837e35d579d808d6b3f28c Mon Sep 17 00:00:00 2001
From: modimo <modimo at fb.com>
Date: Tue, 23 Jan 2024 15:31:13 -0800
Subject: [PATCH 2/2] Move changes to LoopUnrollPass.cpp, fix up tests

---
 llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp | 13 ++++++++++
 llvm/lib/Transforms/Utils/LoopUnroll.cpp      | 13 ----------
 llvm/test/Transforms/LoopUnroll/pr77842.ll    | 26 ++++++++++++++-----
 3 files changed, 32 insertions(+), 20 deletions(-)

diff --git a/llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp b/llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp
index 7cfeb019af97232..d65c4611b1f347d 100644
--- a/llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp
+++ b/llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp
@@ -173,6 +173,11 @@ static cl::opt<unsigned>
                            cl::desc("Default threshold (max size of unrolled "
                                     "loop), used in all but O3 optimizations"));
 
+static cl::opt<unsigned>
+    UnrollMaxIterations("unroll-max-iterations", cl::init(1'000'000),
+                        cl::Hidden,
+                        cl::desc("Maximum allowed iterations to unroll."));
+
 /// A magic value for use with the Threshold parameter to indicate
 /// that the loop unroll should be performed regardless of how much
 /// code expansion would result.
@@ -1288,6 +1293,14 @@ tryToUnrollLoop(Loop *L, DominatorTree &DT, LoopInfo *LI, ScalarEvolution &SE,
     return LoopUnrollResult::Unmodified;
   }
 
+  // Certain cases with UBSAN can cause trip count to be calculated as INT_MAX,
+  // Block unrolling at a reasonable limit so that the compiler doesn't hang
+  // trying to unroll the loop. See PR77842
+  if (UP.Count > UnrollMaxIterations) {
+    LLVM_DEBUG(dbgs() << "Won't unroll; trip count is too large\n");
+    return LoopUnrollResult::Unmodified;
+  }
+
   // At this point, UP.Runtime indicates that run-time unrolling is allowed.
   // However, we only want to actually perform it if we don't know the trip
   // count and the unroll count doesn't divide the known trip multiple.
diff --git a/llvm/lib/Transforms/Utils/LoopUnroll.cpp b/llvm/lib/Transforms/Utils/LoopUnroll.cpp
index 8529fa1db18e187..72d950fd2dc66ca 100644
--- a/llvm/lib/Transforms/Utils/LoopUnroll.cpp
+++ b/llvm/lib/Transforms/Utils/LoopUnroll.cpp
@@ -109,11 +109,6 @@ UnrollVerifyLoopInfo("unroll-verify-loopinfo", cl::Hidden,
 #endif
                     );
 
-static cl::opt<unsigned>
-    UnrollMaxIterations("unroll-max-iterations", cl::init(1'000'000),
-                        cl::Hidden,
-                        cl::desc("Maximum allowed iterations to unroll."));
-
 /// Check if unrolling created a situation where we need to insert phi nodes to
 /// preserve LCSSA form.
 /// \param Blocks is a vector of basic blocks representing unrolled loop.
@@ -457,14 +452,6 @@ LoopUnrollResult llvm::UnrollLoop(Loop *L, UnrollLoopOptions ULO, LoopInfo *LI,
     }
   }
 
-  // Certain cases with UBSAN can cause trip count to be calculated as INT_MAX,
-  // Block unrolling at a reasonable limit so that the compiler doesn't hang
-  // trying to unroll the loop. See PR77842
-  if (ULO.Count > UnrollMaxIterations) {
-    LLVM_DEBUG(dbgs() << "Won't unroll; trip count is too large\n");
-    return LoopUnrollResult::Unmodified;
-  }
-
   using namespace ore;
   // Report the unrolling decision.
   if (CompletelyUnroll) {
diff --git a/llvm/test/Transforms/LoopUnroll/pr77842.ll b/llvm/test/Transforms/LoopUnroll/pr77842.ll
index 834033bbe3618eb..c735a91e614ebff 100644
--- a/llvm/test/Transforms/LoopUnroll/pr77842.ll
+++ b/llvm/test/Transforms/LoopUnroll/pr77842.ll
@@ -1,15 +1,27 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
-; RUN: opt -passes=loop-unroll -disable-output -debug-only=loop-unroll %s 2>&1 | FileCheck %s
+; RUN: opt -passes=loop-unroll -S %s | FileCheck %s
 
 ; Validate that loop unroll full doesn't try to fully unroll values whose trip counts are too large.
 
-; CHECK: Exiting block %cont23: TripCount=2147483648, TripMultiple=0, BreakoutTrip=0
-; CHECK-NEXT: Won't unroll; trip count is too large
-
-target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
-target triple = "x86_64-redhat-linux-gnu"
-
 define void @foo(i64 %end) {
+; CHECK-LABEL: @foo(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    br label [[LOOPHEADER:%.*]]
+; CHECK:       loopheader:
+; CHECK-NEXT:    [[IV:%.*]] = phi i64 [ 0, [[ENTRY:%.*]] ], [ [[IV_NEW:%.*]], [[BACKEDGE:%.*]] ]
+; CHECK-NEXT:    [[EXIT:%.*]] = icmp eq i64 [[IV]], [[END:%.*]]
+; CHECK-NEXT:    br i1 [[EXIT]], label [[FOR_COND_CLEANUP_LOOPEXIT:%.*]], label [[CONT23:%.*]]
+; CHECK:       for.cond.cleanup.loopexit:
+; CHECK-NEXT:    ret void
+; CHECK:       cont23:
+; CHECK-NEXT:    [[EXITCOND241:%.*]] = icmp eq i64 [[IV]], 2147483647
+; CHECK-NEXT:    br i1 [[EXITCOND241]], label [[HANDLER_ADD_OVERFLOW:%.*]], label [[BACKEDGE]]
+; CHECK:       handler.add_overflow:
+; CHECK-NEXT:    unreachable
+; CHECK:       backedge:
+; CHECK-NEXT:    [[IV_NEW]] = add i64 [[IV]], 1
+; CHECK-NEXT:    br label [[LOOPHEADER]], !llvm.loop [[LOOP0:![0-9]+]]
+;
 entry:
   br label %loopheader
 



More information about the llvm-commits mailing list