| 1 |
; RUN: opt -data-layout=A5 -mtriple=amdgcn-unknown-amdhsa -passes=loop-unroll -S %s | FileCheck %s |
| 2 |
|
| 3 |
; Check that we full unroll loop to be able to eliminate alloca |
| 4 |
; CHECK-LABEL: @non_invariant_ind |
| 5 |
; CHECK: for.body: |
| 6 |
; CHECK-NOT: br |
| 7 |
; CHECK: store i32 %tmp15, ptr addrspace(1) %arrayidx7, align 4 |
| 8 |
; CHECK: ret void |
| 9 |
|
| 10 |
define amdgpu_kernel void @non_invariant_ind(ptr addrspace(1) nocapture %a, i32 %x) { |
| 11 |
entry: |
| 12 |
%arr = alloca [64 x i32], align 4, addrspace(5) |
| 13 |
%tmp1 = tail call i32 @llvm.amdgcn.workitem.id.x() #1 |
| 14 |
br label %for.body |
| 15 |
|
| 16 |
for.cond.cleanup: ; preds = %for.body |
| 17 |
%arrayidx5 = getelementptr inbounds [64 x i32], ptr addrspace(5) %arr, i32 0, i32 %x |
| 18 |
%tmp15 = load i32, ptr addrspace(5) %arrayidx5, align 4 |
| 19 |
%arrayidx7 = getelementptr inbounds i32, ptr addrspace(1) %a, i32 %tmp1 |
| 20 |
store i32 %tmp15, ptr addrspace(1) %arrayidx7, align 4 |
| 21 |
ret void |
| 22 |
|
| 23 |
for.body: ; preds = %for.body, %entry |
| 24 |
%i.015 = phi i32 [ 0, %entry ], [ %inc, %for.body ] |
| 25 |
%idxprom = sext i32 %i.015 to i64 |
| 26 |
%arrayidx = getelementptr inbounds i32, ptr addrspace(1) %a, i64 %idxprom |
| 27 |
%tmp16 = load i32, ptr addrspace(1) %arrayidx, align 4 |
| 28 |
%add = add nsw i32 %i.015, %tmp1 |
| 29 |
%rem = srem i32 %add, 64 |
| 30 |
%arrayidx3 = getelementptr inbounds [64 x i32], ptr addrspace(5) %arr, i32 0, i32 %rem |
| 31 |
store i32 %tmp16, ptr addrspace(5) %arrayidx3, align 4 |
| 32 |
%inc = add nuw nsw i32 %i.015, 1 |
| 33 |
%exitcond = icmp eq i32 %inc, 100 |
| 34 |
br i1 %exitcond, label %for.cond.cleanup, label %for.body |
| 35 |
} |
| 36 |
|
| 37 |
declare i32 @func() |
| 38 |
|
| 39 |
; Check that we unroll inner loop but not outer |
| 40 |
; CHECK-LABEL: @invariant_ind |
| 41 |
; CHECK: %[[exitcond:[^ ]+]] = icmp eq i32 %{{.*}}, 32 |
| 42 |
; CHECK: br i1 %[[exitcond]] |
| 43 |
; CHECK-NOT: icmp eq i32 %{{.*}}, 100 |
| 44 |
|
| 45 |
define amdgpu_kernel void @invariant_ind(ptr addrspace(1) nocapture %a, i32 %x) { |
| 46 |
entry: |
| 47 |
%arr = alloca [64 x i32], align 4, addrspace(5) |
| 48 |
%tmp1 = tail call i32 @func() |
| 49 |
br label %for.cond2.preheader |
| 50 |
|
| 51 |
for.cond2.preheader: ; preds = %for.cond.cleanup5, %entry |
| 52 |
%i.026 = phi i32 [ 0, %entry ], [ %inc10, %for.cond.cleanup5 ] |
| 53 |
%idxprom = sext i32 %i.026 to i64 |
| 54 |
%arrayidx = getelementptr inbounds i32, ptr addrspace(1) %a, i64 %idxprom |
| 55 |
%tmp15 = load i32, ptr addrspace(1) %arrayidx, align 4 |
| 56 |
br label %for.body6 |
| 57 |
|
| 58 |
for.cond.cleanup: ; preds = %for.cond.cleanup5 |
| 59 |
%arrayidx13 = getelementptr inbounds [64 x i32], ptr addrspace(5) %arr, i32 0, i32 %x |
| 60 |
%tmp16 = load i32, ptr addrspace(5) %arrayidx13, align 4 |
| 61 |
%arrayidx15 = getelementptr inbounds i32, ptr addrspace(1) %a, i32 %tmp1 |
| 62 |
store i32 %tmp16, ptr addrspace(1) %arrayidx15, align 4 |
| 63 |
ret void |
| 64 |
|
| 65 |
for.cond.cleanup5: ; preds = %for.body6 |
| 66 |
%inc10 = add nuw nsw i32 %i.026, 1 |
| 67 |
%exitcond27 = icmp eq i32 %inc10, 32 |
| 68 |
br i1 %exitcond27, label %for.cond.cleanup, label %for.cond2.preheader |
| 69 |
|
| 70 |
for.body6: ; preds = %for.body6, %for.cond2.preheader |
| 71 |
%j.025 = phi i32 [ 0, %for.cond2.preheader ], [ %inc, %for.body6 ] |
| 72 |
%add = add nsw i32 %j.025, %tmp1 |
| 73 |
%rem = srem i32 %add, 64 |
| 74 |
%arrayidx8 = getelementptr inbounds [64 x i32], ptr addrspace(5) %arr, i32 0, i32 %rem |
| 75 |
store i32 %tmp15, ptr addrspace(5) %arrayidx8, align 4 |
| 76 |
%inc = add nuw nsw i32 %j.025, 1 |
| 77 |
%exitcond = icmp eq i32 %inc, 100 |
| 78 |
br i1 %exitcond, label %for.cond.cleanup5, label %for.body6 |
| 79 |
} |
| 80 |
|
| 81 |
; Check we do not enforce unroll if alloca is too big |
| 82 |
; CHECK-LABEL: @too_big |
| 83 |
; CHECK: for.body: |
| 84 |
; CHECK: icmp eq i32 %{{.*}}, 100 |
| 85 |
; CHECK: br |
| 86 |
|
| 87 |
define amdgpu_kernel void @too_big(ptr addrspace(1) nocapture %a, i32 %x) { |
| 88 |
entry: |
| 89 |
%arr = alloca [256 x i32], align 4, addrspace(5) |
| 90 |
%tmp1 = tail call i32 @llvm.amdgcn.workitem.id.x() #1 |
| 91 |
br label %for.body |
| 92 |
|
| 93 |
for.cond.cleanup: ; preds = %for.body |
| 94 |
%arrayidx5 = getelementptr inbounds [256 x i32], ptr addrspace(5) %arr, i32 0, i32 %x |
| 95 |
%tmp15 = load i32, ptr addrspace(5) %arrayidx5, align 4 |
| 96 |
%arrayidx7 = getelementptr inbounds i32, ptr addrspace(1) %a, i32 %tmp1 |
| 97 |
store i32 %tmp15, ptr addrspace(1) %arrayidx7, align 4 |
| 98 |
ret void |
| 99 |
|
| 100 |
for.body: ; preds = %for.body, %entry |
| 101 |
%i.015 = phi i32 [ 0, %entry ], [ %inc, %for.body ] |
| 102 |
%idxprom = sext i32 %i.015 to i64 |
| 103 |
%arrayidx = getelementptr inbounds i32, ptr addrspace(1) %a, i64 %idxprom |
| 104 |
%tmp16 = load i32, ptr addrspace(1) %arrayidx, align 4 |
| 105 |
%add = add nsw i32 %i.015, %tmp1 |
| 106 |
%rem = srem i32 %add, 64 |
| 107 |
%arrayidx3 = getelementptr inbounds [256 x i32], ptr addrspace(5) %arr, i32 0, i32 %rem |
| 108 |
store i32 %tmp16, ptr addrspace(5) %arrayidx3, align 4 |
| 109 |
%inc = add nuw nsw i32 %i.015, 1 |
| 110 |
%exitcond = icmp eq i32 %inc, 100 |
| 111 |
br i1 %exitcond, label %for.cond.cleanup, label %for.body |
| 112 |
} |
| 113 |
|
| 114 |
; Check we do not enforce unroll if alloca is dynamic |
| 115 |
; CHECK-LABEL: @dynamic_size_alloca( |
| 116 |
; CHECK: alloca i32, i32 %n |
| 117 |
; CHECK: for.body: |
| 118 |
; CHECK: icmp eq i32 %{{.*}}, 100 |
| 119 |
; CHECK: br |
| 120 |
|
| 121 |
define amdgpu_kernel void @dynamic_size_alloca(ptr addrspace(1) nocapture %a, i32 %n, i32 %x) { |
| 122 |
entry: |
| 123 |
%arr = alloca i32, i32 %n, align 4, addrspace(5) |
| 124 |
%tmp1 = tail call i32 @llvm.amdgcn.workitem.id.x() #1 |
| 125 |
br label %for.body |
| 126 |
|
| 127 |
for.cond.cleanup: ; preds = %for.body |
| 128 |
%arrayidx5 = getelementptr inbounds i32, ptr addrspace(5) %arr, i32 %x |
| 129 |
%tmp15 = load i32, ptr addrspace(5) %arrayidx5, align 4 |
| 130 |
%arrayidx7 = getelementptr inbounds i32, ptr addrspace(1) %a, i32 %tmp1 |
| 131 |
store i32 %tmp15, ptr addrspace(1) %arrayidx7, align 4 |
| 132 |
ret void |
| 133 |
|
| 134 |
for.body: ; preds = %for.body, %entry |
| 135 |
%i.015 = phi i32 [ 0, %entry ], [ %inc, %for.body ] |
| 136 |
%idxprom = sext i32 %i.015 to i64 |
| 137 |
%arrayidx = getelementptr inbounds i32, ptr addrspace(1) %a, i64 %idxprom |
| 138 |
%tmp16 = load i32, ptr addrspace(1) %arrayidx, align 4 |
| 139 |
%add = add nsw i32 %i.015, %tmp1 |
| 140 |
%rem = srem i32 %add, 64 |
| 141 |
%arrayidx3 = getelementptr inbounds i32, ptr addrspace(5) %arr, i32 %rem |
| 142 |
store i32 %tmp16, ptr addrspace(5) %arrayidx3, align 4 |
| 143 |
%inc = add nuw nsw i32 %i.015, 1 |
| 144 |
%exitcond = icmp eq i32 %inc, 100 |
| 145 |
br i1 %exitcond, label %for.cond.cleanup, label %for.body |
| 146 |
} |
| 147 |
|
| 148 |
declare ptr addrspace(4) @llvm.amdgcn.dispatch.ptr() #1 |
| 149 |
|
| 150 |
declare i32 @llvm.amdgcn.workitem.id.x() #1 |
| 151 |
|
| 152 |
declare i32 @llvm.amdgcn.workgroup.id.x() #1 |
| 153 |
|
| 154 |
declare ptr addrspace(4) @llvm.amdgcn.implicitarg.ptr() #1 |
| 155 |
|
| 156 |
attributes #1 = { nounwind readnone } |