• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1; RUN: opt -mtriple=amdgcn-- -loop-unroll -simplifycfg -sroa %s -S -o - | FileCheck %s
2; RUN: opt -mtriple=r600-- -loop-unroll -simplifycfg -sroa %s -S -o - | FileCheck %s
3
4
5; This test contains a simple loop that initializes an array declared in
6; private memory.  We want to make sure these kinds of loops are always
7; unrolled, because private memory is slow.
8
9; CHECK-LABEL: @private_memory
10; CHECK-NOT: alloca
11; CHECK: store i32 5, i32 addrspace(1)* %out
12define amdgpu_kernel void @private_memory(i32 addrspace(1)* %out) {
13entry:
14  %0 = alloca [32 x i32]
15  br label %loop.header
16
17loop.header:
18  %counter = phi i32 [0, %entry], [%inc, %loop.inc]
19  br label %loop.body
20
21loop.body:
22  %ptr = getelementptr [32 x i32], [32 x i32]* %0, i32 0, i32 %counter
23  store i32 %counter, i32* %ptr
24  br label %loop.inc
25
26loop.inc:
27  %inc = add i32 %counter, 1
28  %1 = icmp sge i32 %counter, 32
29  br i1 %1, label  %exit, label %loop.header
30
31exit:
32  %2 = getelementptr [32 x i32], [32 x i32]* %0, i32 0, i32 5
33  %3 = load i32, i32* %2
34  store i32 %3, i32 addrspace(1)* %out
35  ret void
36}
37
38; Check that loop is unrolled for local memory references
39
40; CHECK-LABEL: @local_memory
41; CHECK: getelementptr i32, i32 addrspace(1)* %out, i32 128
42; CHECK-NEXT: store
43; CHECK-NEXT: ret
44define amdgpu_kernel void @local_memory(i32 addrspace(1)* %out, i32 addrspace(3)* %lds) {
45entry:
46  br label %loop.header
47
48loop.header:
49  %counter = phi i32 [0, %entry], [%inc, %loop.inc]
50  br label %loop.body
51
52loop.body:
53  %ptr_lds = getelementptr i32, i32 addrspace(3)* %lds, i32 %counter
54  %val = load i32, i32 addrspace(3)* %ptr_lds
55  %ptr_out = getelementptr i32, i32 addrspace(1)* %out, i32 %counter
56  store i32 %val, i32 addrspace(1)* %ptr_out
57  br label %loop.inc
58
59loop.inc:
60  %inc = add i32 %counter, 1
61  %cond = icmp sge i32 %counter, 128
62  br i1 %cond, label  %exit, label %loop.header
63
64exit:
65  ret void
66}
67
68; Check that a loop with if inside completely unrolled to eliminate phi and if
69
70; CHECK-LABEL: @unroll_for_if
71; CHECK: entry:
72; CHECK-NEXT: getelementptr
73; CHECK-NEXT: store
74; CHECK-NEXT: getelementptr
75; CHECK-NEXT: store
76; CHECK-NOT: br
77define amdgpu_kernel void @unroll_for_if(i32* %a) {
78entry:
79  br label %for.body
80
81for.body:                                         ; preds = %entry, %for.inc
82  %i1 = phi i32 [ 0, %entry ], [ %inc, %for.inc ]
83  %and = and i32 %i1, 1
84  %tobool = icmp eq i32 %and, 0
85  br i1 %tobool, label %for.inc, label %if.then
86
87if.then:                                          ; preds = %for.body
88  %0 = sext i32 %i1 to i64
89  %arrayidx = getelementptr inbounds i32, i32* %a, i64 %0
90  store i32 0, i32* %arrayidx, align 4
91  br label %for.inc
92
93for.inc:                                          ; preds = %for.body, %if.then
94  %inc = add nuw nsw i32 %i1, 1
95  %cmp = icmp ult i32 %inc, 48
96  br i1 %cmp, label %for.body, label %for.end
97
98for.end:                                          ; preds = %for.cond
99  ret void
100}
101