llvm-project/polly/test/ScopInfo/loop-multiexit-succ-cond.ll

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

104 lines
3.0 KiB
LLVM
Raw Normal View History

; RUN: opt %loadPolly -polly-scops -analyze < %s | FileCheck %s
; RUN: opt %loadPolly -polly-codegen -S < %s | FileCheck %s --check-prefix=IR
;
[ScopDetect] Reject loop with multiple exit blocks. The current statement domain derivation algorithm does not (always) consider that different exit blocks of a loop can have different conditions to be reached. From the code for (int i = n; ; i-=2) { if (i <= 0) goto even; if (i <= 1) goto odd; A[i] = i; } even: A[0] = 42; return; odd: A[1] = 21; return; Polly currently derives the following domains: Stmt_even_critedge Domain := [n] -> { Stmt_even_critedge[] }; Stmt_odd Domain := [n] -> { Stmt_odd[] : (1 + n) mod 2 = 0 and n > 0 }; while the domain for the odd case is correct, Stmt_even is assumed to be executed unconditionally, which is obviously wrong. While projecting out the loop dimension in `adjustDomainDimensions`, it does not consider that there are other exit condition that have matched before. I don't know a how to fix this without changing a lot of code. Therefore This patch rejects loops with multiple exist blocks to fix the miscompile of test-suite's uuencode. The odd condition is transformed by LLVM to %cmp1 = icmp eq i64 %indvars.iv, 1 such that the project_out in adjustDomainDimensions() indeed only matches for odd n (using this condition only, we'd have an infinite loop otherwise). The even condition manifests as %cmp = icmp slt i64 %indvars.iv, 3 Because buildDomainsWithBranchConstraints() does not consider other exit conditions, it has to assume that the induction variable will eventually be lower than 3 and taking this exit. IMHO we need to reuse the algorithm that determines the number of iterations (addLoopBoundsToHeaderDomain) to determine which exit condition applies first. It has to happen in buildDomainsWithBranchConstraints() because the result will need to propagate to successor BBs. Currently addLoopBoundsToHeaderDomain() just look for union of all backedge conditions (which means leaving not the loop here). The patch in llvm.org/PR35465 changes it to look for exit conditions instead. This is required because there might be other exit conditions that do not alternatively go back to the loop header. Differential Revision: https://reviews.llvm.org/D45649 llvm-svn: 330858
2018-04-26 02:53:33 +08:00
; The SCoP contains a loop with multiple exit blocks (BBs after leaving
; the loop). The current implementation of deriving their domain derives
; only a common domain for all of the exit blocks. We disabled loops with
; multiple exit blocks until this is fixed.
; XFAIL: *
;
; Check that we do not crash and generate valid IR.
;
; CHECK: Assumed Context:
; CHECK-NEXT: [count1, dobreak, count2] -> { : }
; CHECK-NEXT: Invalid Context:
; CHECK-NEXT: [count1, dobreak, count2] -> { : (count1 > 0 and dobreak > 0) or count1 <= 0 or (count1 > 0 and dobreak <= 0 and count2 > 0) }
;
; CHECK: Stmt_loop_enter
; CHECK-NEXT: Domain :=
; CHECK-NEXT: [count1, dobreak, count2] -> { Stmt_loop_enter[] : count1 > 0 };
; CHECK: Stmt_loop_break
; CHECK-NEXT: Domain :=
; CHECK-NEXT: [count1, dobreak, count2] -> { Stmt_loop_break[] : count1 > 0 and dobreak > 0 };
; CHECK: Stmt_loop_finish
; CHECK-NEXT: Domain :=
; CHECK-NEXT: [count1, dobreak, count2] -> { Stmt_loop_finish[] : count1 > 0 and dobreak <= 0 and count2 > 0 };
; CHECK: Stmt_loop_skip
; CHECK-NEXT: Domain :=
; CHECK-NEXT: [count1, dobreak, count2] -> { Stmt_loop_skip[] : count1 <= 0 };
; IR: polly.merge_new_and_old:
; IR-NEXT: %phi.ph.merge = phi float [ %phi.ph.final_reload, %polly.exiting ], [ %phi.ph, %return.region_exiting ]
; IR-NEXT: br label %return
;
; IR: return:
; IR-NEXT: %phi = phi float [ %phi.ph.merge, %polly.merge_new_and_old ]
declare void @g();
define void @func(i64 %count1, i64 %count2, i32 %dobreak, float* %A) {
entry:
%fadd = fadd float undef, undef
br label %loopguard
loopguard:
%cmp6 = icmp sgt i64 %count1, 0
br i1 %cmp6, label %loop_enter, label %loop_skip
loop_enter:
store float 1.0, float* %A
br label %loop_header
loop_header:
%indvars.iv63 = phi i64 [ %indvars.iv.next64, %loop_continue ], [ 0, %loop_enter ]
%indvars.iv.next64 = add nuw nsw i64 %indvars.iv63, 1
%add8 = add i64 undef, undef
%cmp_break = icmp sge i32 %dobreak, 1
br i1 %cmp_break, label %loop_break, label %loop_continue
loop_continue:
%cmp9 = icmp eq i64 %indvars.iv.next64, %count2
br i1 %cmp9, label %loop_finish, label %loop_header
loop_break:
store float 2.0, float* %A
br label %loop_break_error
loop_break_error:
%cmp_loop_break = fcmp oeq float %fadd, 2.
br i1 %cmp_loop_break, label %loop_break_g, label %return
loop_break_g:
call void @g()
br label %return
loop_finish:
store float 3.0, float* %A
br label %loop_finish_error
loop_finish_error:
call void @g()
br label %return
loop_skip:
store float 4.0, float* %A
br label %loop_skip_error
loop_skip_error:
call void @g()
br label %return
return:
%phi = phi float [ 0.0, %loop_finish_error ], [ 0.0, %loop_break_error ], [ 2.0, %loop_break_g ], [ 3.0, %loop_skip_error ]
store float 1.0, float* %A
ret void
}