| ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py |
| ; RUN: opt < %s -passes='loop(indvars)' -S | FileCheck %s |
| |
| ; IndVarSimplify tries to simplify or remove loop induction variables and exit |
| ; conditions that it can prove are redundant. This test ensures that the pass |
| ; preserves the exit condition of a loop whose body contains a convergent |
| ; operation guarded by a convergence.loop token. |
| ; |
| ; This is one of three sibling tests (LoopRotate, IndVarSimplify, |
| ; SimpleLoopUnswitch) that each guard against a pass moving or removing the |
| ; exit condition of a loop with a convergent operation, which can change the |
| ; set of active threads inside the loop. See https://godbolt.org/z/K1a8xW1TE |
| ; for the problematic pass pipeline and |
| ; https://github.com/llvm/llvm-project/issues/180621 for full context. |
| |
| define void @convergent_loop(i32 %tid, ptr %array) #0 { |
| ; CHECK-LABEL: @convergent_loop( |
| ; CHECK-NEXT: entry: |
| ; CHECK-NEXT: [[TOK_ENTRY:%.*]] = call token @llvm.experimental.convergence.entry() |
| ; CHECK-NEXT: br label [[FOR_COND:%.*]] |
| ; CHECK: for.cond: |
| ; CHECK-NEXT: [[I:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[INC:%.*]], [[FOR_BODY:%.*]] ] |
| ; CHECK-NEXT: [[TOK_LOOP:%.*]] = call token @llvm.experimental.convergence.loop() [ "convergencectrl"(token [[TOK_ENTRY]]) ] |
| ; CHECK-NEXT: [[CMP:%.*]] = icmp samesign ult i32 [[I]], 8 |
| ; CHECK-NEXT: br i1 [[CMP]], label [[FOR_BODY]], label [[FOR_END_LOOPEXIT:%.*]] |
| ; CHECK: for.body: |
| ; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i32 [[I]], [[TID:%.*]] |
| ; CHECK-NEXT: [[INC]] = add nuw nsw i32 [[I]], 1 |
| ; CHECK-NEXT: br i1 [[CMP1]], label [[IF_THEN:%.*]], label [[FOR_COND]] |
| ; CHECK: if.then: |
| ; CHECK-NEXT: [[WAVE:%.*]] = call i32 @llvm.dx.wave.reduce.umax.i32(i32 [[TID]]) [ "convergencectrl"(token [[TOK_LOOP]]) ] |
| ; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds i32, ptr [[ARRAY:%.*]], i32 [[TID]] |
| ; CHECK-NEXT: store i32 [[WAVE]], ptr [[GEP]], align 4 |
| ; CHECK-NEXT: br label [[FOR_END:%.*]] |
| ; CHECK: for.end.loopexit: |
| ; CHECK-NEXT: br label [[FOR_END]] |
| ; CHECK: for.end: |
| ; CHECK-NEXT: ret void |
| ; |
| entry: |
| %tok.entry = call token @llvm.experimental.convergence.entry() |
| br label %for.cond |
| |
| for.cond: |
| %i = phi i32 [ 0, %entry ], [ %inc, %for.body ] |
| %tok.loop = call token @llvm.experimental.convergence.loop() [ "convergencectrl"(token %tok.entry) ] |
| %cmp = icmp ult i32 %i, 8 |
| br i1 %cmp, label %for.body, label %for.end |
| |
| for.body: |
| %cmp1 = icmp eq i32 %i, %tid |
| %inc = add nuw i32 %i, 1 |
| br i1 %cmp1, label %if.then, label %for.cond |
| |
| if.then: |
| %wave = call i32 @llvm.dx.wave.reduce.umax.i32(i32 %tid) [ "convergencectrl"(token %tok.loop) ] |
| %gep = getelementptr inbounds i32, ptr %array, i32 %tid |
| store i32 %wave, ptr %gep, align 4 |
| br label %for.end |
| |
| for.end: |
| ret void |
| } |
| |
| declare token @llvm.experimental.convergence.entry() #1 |
| declare token @llvm.experimental.convergence.loop() #1 |
| declare i32 @llvm.dx.wave.reduce.umax.i32(i32) #1 |
| |
| attributes #0 = { convergent mustprogress nounwind "no-trapping-math"="true" } |
| attributes #1 = { convergent } |