[llvm] [LoopPeel] Do not set DesiredPeelCountLast in compare-elimination path (PR #231045)

via llvm-commits llvm-commits at lists.llvm.org
Sun Oct 11 15:54:05 PDT 2026


https://github.com/knowrohit created https://github.com/llvm/llvm-project/pull/231045

Fixes https://github.com/llvm/llvm-project/issues/231037

## Summary
`opt -passes='loop-unroll<O3>'` crashed with an assertion in `peelLoop` (`LoopPeel.cpp`):

    (!PeelLast || (canPeelLastIteration(*L,*SE) && PeelCount == 1))

## Root cause
`countToEliminateCompares()` was setting `DesiredPeelCountLast` (the "peel the last iteration" count) to the front peel count `NewPeelCount` in the compare-elimination success path, without checking `canPeelLastIteration()`, and the value can exceed 1.

When the front peel was then blocked by the already-peeled-count limit (`DesiredPeelCount + AlreadyPeeled > UnrollPeelMaxCount`), `computePeelCount()` fell through to the `PeelLast` path and called `peelLoop()` with `PeelLast=true`, `PeelCount>1` and `canPeelLastIteration()=false`, tripping the assertion.

The trigger is an infinite loop (no exit block) containing a min/max intrinsic (e.g. `llvm.umin.i64`) plus a compare and `!llvm.loop.peeled.count` metadata.

## Fix
Remove the invalid assignment so `DesiredPeelCountLast` is only ever set to `1` via `shouldPeelLastIteration()` (which checks `canPeelLastIteration()`). This upholds the `peelLoop()` invariant.

## Verification
- Reproducer no longer crashes (loop correctly left unchanged — it is infinite).
- All `llvm/test/Transforms/LoopUnroll/` tests pass.
- Added regression test `peel-last-iteration-minmax-no-exit.ll` (crashes on the pre-fix code, passes with the fix).


>From 0e98777f1accac192066fa2c632338bfef104f82 Mon Sep 17 00:00:00 2001
From: Rohit <knowrohit.work at gmail.com>
Date: Sun, 11 Oct 2026 18:52:57 -0400
Subject: [PATCH] [LoopPeel] Do not set DesiredPeelCountLast in
 compare-elimination path

countToEliminateCompares() was setting DesiredPeelCountLast to the front peel
count (NewPeelCount) in the compare-elimination success path, without checking
canPeelLastIteration(). That value can also exceed 1.

When the front peel was then blocked by the already-peeled-count limit
(DesiredPeelCount + AlreadyPeeled > UnrollPeelMaxCount), computePeelCount() fell
through to the PeelLast path and called peelLoop() with PeelLast=true,
PeelCount>1 and canPeelLastIteration() false, tripping the assertion:

  "when peeling the last iteration, the loop must be supported and can only
   peel a single iteration"

DesiredPeelCountLast is now only ever set to 1 via shouldPeelLastIteration(),
which checks canPeelLastIteration(), so the peelLoop() invariant is upheld.

Fixes https://github.com/llvm/llvm-project/issues/231037
---
 llvm/lib/Transforms/Utils/LoopPeel.cpp        |  1 -
 .../peel-last-iteration-minmax-no-exit.ll     | 62 +++++++++++++++++++
 2 files changed, 62 insertions(+), 1 deletion(-)
 create mode 100644 llvm/test/Transforms/LoopUnroll/peel-last-iteration-minmax-no-exit.ll

diff --git a/llvm/lib/Transforms/Utils/LoopPeel.cpp b/llvm/lib/Transforms/Utils/LoopPeel.cpp
index 2ad7ef5792288..01f82fccc712b 100644
--- a/llvm/lib/Transforms/Utils/LoopPeel.cpp
+++ b/llvm/lib/Transforms/Utils/LoopPeel.cpp
@@ -656,7 +656,6 @@ countToEliminateCompares(Loop &L, unsigned MaxPeelCount, ScalarEvolution &SE,
     }
 
     DesiredPeelCount = std::max(DesiredPeelCount, NewPeelCount);
-    DesiredPeelCountLast = std::max(DesiredPeelCountLast, NewPeelCount);
   };
 
   auto ComputePeelCountMinMax = [&](MinMaxIntrinsic *MinMax) {
diff --git a/llvm/test/Transforms/LoopUnroll/peel-last-iteration-minmax-no-exit.ll b/llvm/test/Transforms/LoopUnroll/peel-last-iteration-minmax-no-exit.ll
new file mode 100644
index 0000000000000..bc1f457d5de71
--- /dev/null
+++ b/llvm/test/Transforms/LoopUnroll/peel-last-iteration-minmax-no-exit.ll
@@ -0,0 +1,62 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt -passes='loop-unroll<O3>' -S %s | FileCheck %s
+
+; Regression test for https://github.com/llvm/llvm-project/issues/231037
+;
+; The loop below has no exit block (it is an infinite loop) and contains a
+; min/max intrinsic. countToEliminateCompares() used to set DesiredPeelCountLast
+; to the front peel count (NewPeelCount) in the compare-elimination success
+; path, without checking canPeelLastIteration(). When the front peel was then
+; blocked by the already-peeled-count limit (DesiredPeelCount + AlreadyPeeled >
+; UnrollPeelMaxCount), computePeelCount() fell through to the PeelLast path and
+; called peelLoop() with PeelLast=true, PeelCount>1 and canPeelLastIteration()
+; false, tripping the assertion:
+;   "when peeling the last iteration, the loop must be supported and can only
+;    peel a single iteration"
+
+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-unknown-linux-gnu"
+
+define i8 @f() {
+; CHECK-LABEL: define i8 @f() {
+; CHECK-NEXT:  [[DOTCRITEDGE:.*]]:
+; CHECK-NEXT:    br label %[[DOTLR_PH8:.*]]
+; CHECK:       [[DOTLOOPEXIT:.*]]:
+; CHECK-NEXT:    [[INDVARS_IV_NEXT:%.*]] = add nsw i64 [[INDVARS_IV:%.*]], 1
+; CHECK-NEXT:    br label %[[DOTLR_PH8]], !llvm.loop [[LOOP0:![0-9]+]]
+; CHECK:       [[DOTLR_PH8]]:
+; CHECK-NEXT:    [[INDVARS_IV]] = phi i64 [ [[INDVARS_IV_NEXT]], %[[DOTLOOPEXIT]] ], [ 1, %[[DOTCRITEDGE]] ]
+; CHECK-NEXT:    [[DOTNOT123:%.*]] = icmp ult i64 [[INDVARS_IV]], 3
+; CHECK-NEXT:    br i1 [[DOTNOT123]], label %[[DOTLOOPEXIT]], label %[[DOTLR_PH_PREHEADER:.*]]
+; CHECK:       [[DOTLR_PH_PREHEADER]]:
+; CHECK-NEXT:    [[UMIN:%.*]] = call i64 @llvm.umin.i64(i64 [[INDVARS_IV]], i64 8)
+; CHECK-NEXT:    br label %[[DOTLOOPEXIT]]
+;
+.critedge:
+  br label %.lr.ph8
+
+.loopexit:                                        ; preds = %.lr.ph.preheader, %.lr.ph8
+  %indvars.iv.next = add nsw i64 %indvars.iv, 1
+  br label %.lr.ph8, !llvm.loop !0
+
+.lr.ph8:                                          ; preds = %.loopexit, %.critedge
+  %indvars.iv = phi i64 [ %indvars.iv.next, %.loopexit ], [ 1, %.critedge ]
+  %.not123 = icmp ult i64 %indvars.iv, 3
+  br i1 %.not123, label %.loopexit, label %.lr.ph.preheader
+
+.lr.ph.preheader:                                 ; preds = %.lr.ph8
+  %umin = call i64 @llvm.umin.i64(i64 %indvars.iv, i64 8)
+  br label %.loopexit
+}
+
+; Function Attrs: nocallback nocreateundeforpoison nofree nosync nounwind speculatable willreturn memory(none)
+declare i64 @llvm.umin.i64(i64, i64) #0
+
+attributes #0 = { nocallback nocreateundeforpoison nofree nosync nounwind speculatable willreturn memory(none) }
+
+!0 = distinct !{!0, !1}
+!1 = !{!"llvm.loop.peeled.count", i32 1}
+;.
+; CHECK: [[LOOP0]] = distinct !{[[LOOP0]], [[META1:![0-9]+]]}
+; CHECK: [[META1]] = !{!"llvm.loop.peeled.count", i32 1}
+;.



More information about the llvm-commits mailing list