Skip to content

Commit 899d66b

Browse files
committed
[LoopInterchange] Don't consider loops with BTC=0
Do not consider loops with a zero backedge taken count as candidates for interchange. This seems like a sensible thing to do to me, because it suggests the loop doesn't execute and there is no point in interchanging. This avoids triggering an assert about phis and their uses. I have a feeling that this fix might be hiding the issue, but I haven't yet been able to trigger the assert with other test cases; every time the loops are rejected for other reasons. Since I think this is a self-contained improvement that avoids a lot of test failures, I propose to reject this loops while I investigate further if I can still trigger this in some way. (Partial) fix for #163954
1 parent dbce713 commit 899d66b

File tree

7 files changed

+20
-6
lines changed

7 files changed

+20
-6
lines changed

llvm/lib/Transforms/Scalar/LoopInterchange.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ static cl::opt<unsigned int> MaxLoopNestDepth(
101101
"loop-interchange-max-loop-nest-depth", cl::init(10), cl::Hidden,
102102
cl::desc("Maximum depth of loop nest considered for the transform"));
103103

104+
// This is mainly for testing purposes, and certain tests that rely on
105+
// behaviour that is more difficult to trigger otherwise.
106+
static cl::opt<bool> SkipLoopsWithZeroBTC(
107+
"loop-interchange-skip-zero-btc", cl::init(true), cl::Hidden,
108+
cl::desc("Do not consider loops with a backedge taken count of 0"));
109+
104110
// We prefer cache cost to vectorization by default.
105111
static cl::list<RuleTy> Profitabilities(
106112
"loop-interchange-profitabilities", cl::ZeroOrMore,
@@ -428,6 +434,13 @@ static bool isComputableLoopNest(ScalarEvolution *SE,
428434
LLVM_DEBUG(dbgs() << "Couldn't compute backedge count\n");
429435
return false;
430436
}
437+
// A loop with a backedge that isn't taken, e.g. an unconditional branch
438+
// true, isn't really a loop and we don't want to consider it as a
439+
// candidate.
440+
if (ExitCountOuter && SkipLoopsWithZeroBTC && ExitCountOuter->isZero()) {
441+
LLVM_DEBUG(dbgs() << "Single iteration loop\n");
442+
return false;
443+
}
431444
if (L->getNumBackEdges() != 1) {
432445
LLVM_DEBUG(dbgs() << "NumBackEdges is not equal to 1\n");
433446
return false;
@@ -1808,6 +1821,7 @@ static void moveLCSSAPhis(BasicBlock *InnerExit, BasicBlock *InnerHeader,
18081821

18091822
assert(all_of(P.users(),
18101823
[OuterHeader, OuterExit, IncI, InnerHeader](User *U) {
1824+
dbgs() << "USER: "; U->dump();
18111825
return (cast<PHINode>(U)->getParent() == OuterHeader &&
18121826
IncI->getParent() == InnerHeader) ||
18131827
cast<PHINode>(U)->getParent() == OuterExit;

llvm/test/Transforms/LoopInterchange/interchanged-loop-nest-4.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
; REQUIRES: asserts
2-
; RUN: opt < %s -passes="loop(loop-interchange,loop-interchange)" -cache-line-size=8 -verify-dom-info -verify-loop-info \
2+
; RUN: opt < %s -passes="loop(loop-interchange,loop-interchange)" -cache-line-size=8 -verify-dom-info -verify-loop-info -loop-interchange-skip-zero-btc=false \
33
; RUN: -debug-only=loop-interchange 2>&1 | FileCheck %s
44

55
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"

llvm/test/Transforms/LoopInterchange/lcssa-phi-outer-latch.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
2-
; RUN: opt < %s -passes=loop-interchange -cache-line-size=64 -verify-dom-info -verify-loop-info -verify-scev -verify-loop-lcssa -S | FileCheck %s
2+
; RUN: opt < %s -passes=loop-interchange -loop-interchange-skip-zero-btc=false -cache-line-size=64 -verify-dom-info -verify-loop-info -verify-scev -verify-loop-lcssa -S | FileCheck %s
33

44
; This test is checking that blocks outer.body and outer.latch, where outer.body is the exit
55
; block of the inner loop and outer.latch the latch of the outer loop, correctly

llvm/test/Transforms/LoopInterchange/pr43176-move-to-new-latch.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2-
; RUN: opt < %s -passes=loop-interchange -cache-line-size=64 -pass-remarks-missed='loop-interchange' -pass-remarks-output=%t -S
2+
; RUN: opt < %s -passes=loop-interchange -cache-line-size=64 -pass-remarks-missed='loop-interchange' -pass-remarks-output=%t -loop-interchange-skip-zero-btc=false -S
33
; RUN: FileCheck --input-file=%t %s
44

55
@b = external dso_local global [5 x i32], align 16

llvm/test/Transforms/LoopInterchange/pr43326.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
; RUN: opt < %s -passes=loop-interchange -cache-line-size=64 -pass-remarks-missed='loop-interchange' -pass-remarks-output=%t -S \
2-
; RUN: -verify-dom-info -verify-loop-info -verify-loop-lcssa -stats 2>&1
2+
; RUN: -loop-interchange-skip-zero-btc=false -verify-dom-info -verify-loop-info -verify-loop-lcssa -stats 2>&1
33
; RUN: FileCheck --input-file=%t --check-prefix=REMARKS %s
44

55
@a = global i32 0

llvm/test/Transforms/LoopInterchange/pr57148.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2-
; RUN: opt < %s -passes=loop-interchange -cache-line-size=4 -loop-interchange-threshold=-100 -verify-dom-info -verify-loop-info -verify-scev -verify-loop-lcssa -S | FileCheck %s
2+
; RUN: opt < %s -passes=loop-interchange -loop-interchange-skip-zero-btc=false -cache-line-size=4 -loop-interchange-threshold=-100 -verify-dom-info -verify-loop-info -verify-scev -verify-loop-lcssa -S | FileCheck %s
33

44
; Make sure the loops are in LCSSA form after loop interchange,
55
; and loop interchange does not hit assertion errors and crash.

llvm/test/Transforms/LoopInterchange/reductions-across-inner-and-outer-loop.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
; RUN: opt < %s -passes=loop-interchange -cache-line-size=64 -pass-remarks-missed='loop-interchange' -pass-remarks-output=%t -S \
2-
; RUN: -verify-dom-info -verify-loop-info -verify-loop-lcssa -stats 2>&1 | FileCheck %s
2+
; RUN: -loop-interchange-skip-zero-btc=false -verify-dom-info -verify-loop-info -verify-loop-lcssa -stats 2>&1 | FileCheck %s
33
; RUN: FileCheck --input-file=%t --check-prefix=REMARKS %s
44

55

0 commit comments

Comments
 (0)