<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/77842>77842</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Pragma clang loop unroll(full) and UBSAN causes hangs attempting to unroll INT_MAX+1 times
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            loopoptim,
            hang
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
            modiking
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          modiking
      </td>
    </tr>
</table>

<pre>
    Pseudo-code example:
```
#pragma clang loop unroll(full)
    for (const auto b : std::array<int, 4> arr)
      //some work inside loop
```

Couldn't quite get a C++ example but the reduced IR repro:
```
# cat repro.ll
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"}

# ~/llvm-project/build-rel/bin/opt --version
LLVM (http://llvm.org/):
  LLVM version 18.0.0git
 Optimized build with assertions.
  Default target: x86_64-unknown-linux-gnu
  Host CPU: skylake-avx512
  
# git log
commit 9a2df55f47e4ec02a1efbf8efa776cfeed527df2 (HEAD, origin/main, origin/HEAD)

 # ~/llvm-project/build-debug/bin/opt -passes=loop-unroll -debug-only=loop-unroll manual_reduce.ll --disable-output
Loop Unroll: F[foo] Loop %loopheader
  Loop Size = 6
  Exiting block %loopheader: TripCount=0, TripMultiple=1, BreakoutTrip=1
  Exiting block %cont23: TripCount=2147483648, TripMultiple=0, BreakoutTrip=0
COMPLETELY UNROLLING loop %loopheader with trip count 2147483648!
```

UBSAN introduces an add_overflow handler to the loop. When analyzing for unrolling, every other exit block's Trip Count is unknown but because of the constant compare SCEV identifies the Trip Count as `2147483648` and attempts to unroll as such. This ends up effectively hanging the compiler as it tries to perform the transformation.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyEVktv4zgS_jX0pSBBol72wQc_kp0G0g9Md8_ungJaLMncUKSGpJy4D_vbB6RkR550eoJAFsmvqlivT8WsFa1CXJNiSyjtNBdPQrWEUlLsF2xwR23Wl93FQfPz-ovFgeuo1hwBX1jXSyTZhiR7kmxImUz_45JmvWFtx6CWTLUgte5hUEZLSeiyGfzPaoQCADTaAKHLWivrgA1OwwFItgHruLeQbZgx7EyynVCO0B3kJLsDZsyNEgBC7wm9t7pDeNbmCYSygmMw_vNbhudOD5IrQisHfw7CIbTogMGO0C2h24uncBgcuCOCQT7UyOHD72CwN_oXIYCauREUSznuOma8es4ck-ysBwck2wOhFKOOZBuMelolJNtkNDz8Mr1d-rcyD49IXF9SuiTZJqXLqFkm05sKW-VFvMyjrx5G6c1VnBHevekaL8vyscwjg_zIXCSFGl6iVg1XofHJsREK4aQFB5InjdaELkWZA6EFKk7oCki1HbGonDlfgwRwMCDZAaXH-swckXE0c-2z3VcxQgtxCrfsjwKCrWILia-GYNSZM5BiH9bFdoQ_KnyeAAdWPyFvMWBeVeKLGDMg6q4H_BMmJ8TpqphP8IMBkV5k_OnVi0abuNaKx7VEpoY-9g68QdVaOZrN_XxfLpsK-u3f5e4GXQj_XN9k4X3h9_VNbvnb0Dx9PyI0zat8mZV5NUvna1gm-Ru_j0xxiSZmnD_qE5pG6ucbwCU1c1d-KpRtru4PyiCrj-wgb8Suqjw020JvkNuptG8S8Fof4ZRxfutn-g_VGooj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