Skip to content

Commit

Permalink
LoopUnrollAndJam] Only allow loops with single exit(ing) blocks
Browse files Browse the repository at this point in the history
Summary:
This resolves an issue posted on Bugzilla. https://bugs.llvm.org/show_bug.cgi?id=48764
In this issue, the loop had  multiple exit blocks, which resulted in the
function getExitBlock to return a nullptr, which resulted in hitting the assert.
This patch ensures that loops which only have one exit block as allowed to be
unrolled and jammed.

Reviewed By: Whitney, Meinersbur, dmgreen

Differential Revision: https://reviews.llvm.org/D95806
  • Loading branch information
Sidharth Baveja committed Feb 5, 2021
1 parent d88c55a commit 22ebbc4
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
17 changes: 17 additions & 0 deletions llvm/lib/Transforms/Utils/LoopUnrollAndJam.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,23 @@ static bool isEligibleLoopForm(const Loop &Root) {
if (SubLoopsSize != 1)
return false;

// Only loops with a single exit block can be unrolled and jammed.
// The function getExitBlock() is used for this check, rather than
// getUniqueExitBlock() to ensure loops with mulitple exit edges are
// disallowed.
if (!L->getExitBlock()) {
LLVM_DEBUG(dbgs() << "Won't unroll-and-jam; only loops with single exit "
"blocks can be unrolled and jammed.\n");
return false;
}

// Only loops with a single exiting block can be unrolled and jammed.
if (!L->getExitingBlock()) {
LLVM_DEBUG(dbgs() << "Won't unroll-and-jam; only loops with single "
"exiting blocks can be unrolled and jammed.\n");
return false;
}

L = L->getSubLoops()[0];
} while (L);

Expand Down
49 changes: 49 additions & 0 deletions llvm/test/Transforms/LoopUnrollAndJam/multiple_exit_blocks.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
; REQUIRES: asserts
; RUN: opt -debug-only=loop-unroll-and-jam -passes="loop-unroll-and-jam" -enable-unroll-and-jam -allow-unroll-and-jam -unroll-and-jam-count=8 -disable-output %s 2>&1 | FileCheck %s

; CHECK: Loop Unroll and Jam: F[h] Loop %bb8
; CHECK: Won't unroll-and-jam; only loops with single exit blocks can be unrolled and jammed

@a = external global i16, align 2
@e = external global i32, align 4
@f = external global i16, align 2
@b = external global i16, align 2
@c = external global i64, align 8

define void @h() {
bb:
store i32 4, i32* @e, align 4
%i15 = load i16, i16* @b, align 2
%i17 = icmp slt i16 %i15, 1
br label %bb8

bb8: ; preds = %bb, %bb47
%storemerge15 = phi i32 [ 4, %bb ], [ %i49, %bb47 ]
br label %bb24

bb24: ; preds = %bb43, %bb8
%storemerge312 = phi i16 [ 0, %bb8 ], [ %i45, %bb43 ]
br i1 %i17, label %bb46.preheader, label %bb43

bb46.preheader: ; preds = %bb24
store i16 %storemerge312, i16* @f, align 2
br label %bb46

bb43: ; preds = %bb24
%i45 = add nuw nsw i16 %storemerge312, 1
%i13 = icmp ult i16 %storemerge312, 7
br i1 %i13, label %bb24, label %bb47

bb46: ; preds = %bb46.preheader, %bb46
br label %bb46

bb47: ; preds = %bb43
%i49 = add nsw i32 %storemerge15, -1
store i32 %i49, i32* @e, align 4
%i7.not = icmp eq i32 %i49, 0
br i1 %i7.not, label %bb50, label %bb8

bb50: ; preds = %bb47
store i16 %i45, i16* @f, align 2
ret void
}

0 comments on commit 22ebbc4

Please sign in to comment.