Skip to content

Commit a4573ee

Browse files
authored
[LoopUnroll] UnrollRuntimeMultiExit takes precedence over TTI. (#134259)
Update UnrollRuntimeLoopRemainder to always give priority to the UnrollRuntimeMultiExit option, if provided. After ad9da92 (#124462), we would ignore the option if the backend indicates multi-exit is profitable. This means it cannot be used to disable runtime unrolling. To be consistent with canProfitablyRuntimeUnrollMultiExitLoop, always respect the option. This surfaced while discussing #131998. PR: #134259
1 parent 1302610 commit a4573ee

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

llvm/lib/Transforms/Utils/LoopUnrollRuntime.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -465,10 +465,6 @@ static bool canProfitablyRuntimeUnrollMultiExitLoop(
465465
Loop *L, SmallVectorImpl<BasicBlock *> &OtherExits, BasicBlock *LatchExit,
466466
bool UseEpilogRemainder) {
467467

468-
// Priority goes to UnrollRuntimeMultiExit if it's supplied.
469-
if (UnrollRuntimeMultiExit.getNumOccurrences())
470-
return UnrollRuntimeMultiExit;
471-
472468
// The main pain point with multi-exit loop unrolling is that once unrolled,
473469
// we will not be able to merge all blocks into a straight line code.
474470
// There are branches within the unrolled loop that go to the OtherExits.
@@ -633,14 +629,20 @@ bool llvm::UnrollRuntimeLoopRemainder(
633629
if (!PreserveLCSSA)
634630
return false;
635631

636-
if (!RuntimeUnrollMultiExit &&
637-
!canProfitablyRuntimeUnrollMultiExitLoop(L, OtherExits, LatchExit,
638-
UseEpilogRemainder)) {
639-
LLVM_DEBUG(
640-
dbgs()
641-
<< "Multiple exit/exiting blocks in loop and multi-exit unrolling not "
642-
"enabled!\n");
643-
return false;
632+
// Priority goes to UnrollRuntimeMultiExit if it's supplied.
633+
if (UnrollRuntimeMultiExit.getNumOccurrences()) {
634+
if (!UnrollRuntimeMultiExit)
635+
return false;
636+
} else {
637+
// Otherwise perform multi-exit unrolling, if either the target indicates
638+
// it is profitable or the general profitability heuristics apply.
639+
if (!RuntimeUnrollMultiExit &&
640+
!canProfitablyRuntimeUnrollMultiExitLoop(L, OtherExits, LatchExit,
641+
UseEpilogRemainder)) {
642+
LLVM_DEBUG(dbgs() << "Multiple exit/exiting blocks in loop and "
643+
"multi-exit unrolling not enabled!\n");
644+
return false;
645+
}
644646
}
645647
}
646648
// Use Scalar Evolution to compute the trip count. This allows more loops to

llvm/test/Transforms/LoopUnroll/AArch64/apple-unrolling-multi-exit.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
; RUN: opt -p loop-unroll -mcpu=apple-m2 -S %s | FileCheck --check-prefix=APPLE %s
44
; RUN: opt -p loop-unroll -mcpu=apple-m3 -S %s | FileCheck --check-prefix=APPLE %s
55
; RUN: opt -p loop-unroll -mcpu=apple-m4 -S %s | FileCheck --check-prefix=APPLE %s
6+
; RUN: opt -p loop-unroll -mcpu=apple-m1 -unroll-runtime-multi-exit=false -S %s | FileCheck --check-prefix=OTHER %s
67
; RUN: opt -p loop-unroll -mcpu=cortex-a57 -S %s | FileCheck --check-prefix=OTHER %s
78

89
target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-n32:64-S128-Fn32"

0 commit comments

Comments
 (0)