blob: d5776d77b10d32fbc9440eba2b9122892a9ca878 [file] [log] [blame]
Eli Benderskybbef1722014-04-03 21:18:25 +00001; RUN: llc < %s -march=nvptx -mcpu=sm_20 | FileCheck %s --check-prefix PTX
2; RUN: llc < %s -march=nvptx64 -mcpu=sm_20 | FileCheck %s --check-prefix PTX
Matt Arsenault850657a2017-01-31 01:10:58 +00003; RUN: opt -mtriple=nvptx-- < %s -S -infer-address-spaces | FileCheck %s --check-prefix IR
4; RUN: opt -mtriple=nvptx64-- < %s -S -infer-address-spaces | FileCheck %s --check-prefix IR
Eli Benderskybbef1722014-04-03 21:18:25 +00005
6@array = internal addrspace(3) global [10 x float] zeroinitializer, align 4
7@scalar = internal addrspace(3) global float 0.000000e+00, align 4
Jingyue Wu13755602016-03-20 20:59:20 +00008@generic_scalar = internal global float 0.000000e+00, align 4
9
10define float @ld_from_shared() {
11 %1 = addrspacecast float* @generic_scalar to float addrspace(3)*
12 %2 = load float, float addrspace(3)* %1
13 ret float %2
14}
Eli Benderskybbef1722014-04-03 21:18:25 +000015
16; Verifies nvptx-favor-non-generic correctly optimizes generic address space
17; usage to non-generic address space usage for the patterns we claim to handle:
18; 1. load cast
19; 2. store cast
20; 3. load gep cast
21; 4. store gep cast
22; gep and cast can be an instruction or a constant expression. This function
23; tries all possible combinations.
Jingyue Wu13755602016-03-20 20:59:20 +000024define void @ld_st_shared_f32(i32 %i, float %v) {
Eli Benderskybbef1722014-04-03 21:18:25 +000025; IR-LABEL: @ld_st_shared_f32
26; IR-NOT: addrspacecast
27; PTX-LABEL: ld_st_shared_f32(
28 ; load cast
David Blaikiea79ac142015-02-27 21:17:42 +000029 %1 = load float, float* addrspacecast (float addrspace(3)* @scalar to float*), align 4
Jingyue Wu13755602016-03-20 20:59:20 +000030 call void @use(float %1)
Eli Benderskybbef1722014-04-03 21:18:25 +000031; PTX: ld.shared.f32 %f{{[0-9]+}}, [scalar];
32 ; store cast
33 store float %v, float* addrspacecast (float addrspace(3)* @scalar to float*), align 4
34; PTX: st.shared.f32 [scalar], %f{{[0-9]+}};
35 ; use syncthreads to disable optimizations across components
Justin Bognera4635372016-07-06 20:02:45 +000036 call void @llvm.nvvm.barrier0()
Eli Benderskybbef1722014-04-03 21:18:25 +000037; PTX: bar.sync 0;
38
39 ; cast; load
40 %2 = addrspacecast float addrspace(3)* @scalar to float*
David Blaikiea79ac142015-02-27 21:17:42 +000041 %3 = load float, float* %2, align 4
Jingyue Wu13755602016-03-20 20:59:20 +000042 call void @use(float %3)
Eli Benderskybbef1722014-04-03 21:18:25 +000043; PTX: ld.shared.f32 %f{{[0-9]+}}, [scalar];
44 ; cast; store
45 store float %v, float* %2, align 4
46; PTX: st.shared.f32 [scalar], %f{{[0-9]+}};
Justin Bognera4635372016-07-06 20:02:45 +000047 call void @llvm.nvvm.barrier0()
Eli Benderskybbef1722014-04-03 21:18:25 +000048; PTX: bar.sync 0;
49
50 ; load gep cast
David Blaikief72d05b2015-03-13 18:20:45 +000051 %4 = load float, float* getelementptr inbounds ([10 x float], [10 x float]* addrspacecast ([10 x float] addrspace(3)* @array to [10 x float]*), i32 0, i32 5), align 4
Jingyue Wu13755602016-03-20 20:59:20 +000052 call void @use(float %4)
Eli Benderskybbef1722014-04-03 21:18:25 +000053; PTX: ld.shared.f32 %f{{[0-9]+}}, [array+20];
54 ; store gep cast
David Blaikief72d05b2015-03-13 18:20:45 +000055 store float %v, float* getelementptr inbounds ([10 x float], [10 x float]* addrspacecast ([10 x float] addrspace(3)* @array to [10 x float]*), i32 0, i32 5), align 4
Eli Benderskybbef1722014-04-03 21:18:25 +000056; PTX: st.shared.f32 [array+20], %f{{[0-9]+}};
Justin Bognera4635372016-07-06 20:02:45 +000057 call void @llvm.nvvm.barrier0()
Eli Benderskybbef1722014-04-03 21:18:25 +000058; PTX: bar.sync 0;
59
60 ; gep cast; load
David Blaikie79e6c742015-02-27 19:29:02 +000061 %5 = getelementptr inbounds [10 x float], [10 x float]* addrspacecast ([10 x float] addrspace(3)* @array to [10 x float]*), i32 0, i32 5
David Blaikiea79ac142015-02-27 21:17:42 +000062 %6 = load float, float* %5, align 4
Jingyue Wu13755602016-03-20 20:59:20 +000063 call void @use(float %6)
Eli Benderskybbef1722014-04-03 21:18:25 +000064; PTX: ld.shared.f32 %f{{[0-9]+}}, [array+20];
65 ; gep cast; store
66 store float %v, float* %5, align 4
67; PTX: st.shared.f32 [array+20], %f{{[0-9]+}};
Justin Bognera4635372016-07-06 20:02:45 +000068 call void @llvm.nvvm.barrier0()
Eli Benderskybbef1722014-04-03 21:18:25 +000069; PTX: bar.sync 0;
70
71 ; cast; gep; load
72 %7 = addrspacecast [10 x float] addrspace(3)* @array to [10 x float]*
David Blaikie79e6c742015-02-27 19:29:02 +000073 %8 = getelementptr inbounds [10 x float], [10 x float]* %7, i32 0, i32 %i
David Blaikiea79ac142015-02-27 21:17:42 +000074 %9 = load float, float* %8, align 4
Jingyue Wu13755602016-03-20 20:59:20 +000075 call void @use(float %9)
Eli Benderskybbef1722014-04-03 21:18:25 +000076; PTX: ld.shared.f32 %f{{[0-9]+}}, [%{{(r|rl|rd)[0-9]+}}];
77 ; cast; gep; store
78 store float %v, float* %8, align 4
79; PTX: st.shared.f32 [%{{(r|rl|rd)[0-9]+}}], %f{{[0-9]+}};
Justin Bognera4635372016-07-06 20:02:45 +000080 call void @llvm.nvvm.barrier0()
Eli Benderskybbef1722014-04-03 21:18:25 +000081; PTX: bar.sync 0;
82
Jingyue Wu13755602016-03-20 20:59:20 +000083 ret void
Eli Benderskybbef1722014-04-03 21:18:25 +000084}
85
Jingyue Wubaabe502014-06-15 21:40:57 +000086; When hoisting an addrspacecast between different pointer types, replace the
87; addrspacecast with a bitcast.
Eli Benderskybbef1722014-04-03 21:18:25 +000088define i32 @ld_int_from_float() {
89; IR-LABEL: @ld_int_from_float
David Blaikiea79ac142015-02-27 21:17:42 +000090; IR: load i32, i32 addrspace(3)* bitcast (float addrspace(3)* @scalar to i32 addrspace(3)*)
Eli Benderskybbef1722014-04-03 21:18:25 +000091; PTX-LABEL: ld_int_from_float(
Jingyue Wubaabe502014-06-15 21:40:57 +000092; PTX: ld.shared.u{{(32|64)}}
David Blaikiea79ac142015-02-27 21:17:42 +000093 %1 = load i32, i32* addrspacecast(float addrspace(3)* @scalar to i32*), align 4
Eli Benderskybbef1722014-04-03 21:18:25 +000094 ret i32 %1
95}
96
Jingyue Wu995dde22015-05-29 17:00:27 +000097define i32 @ld_int_from_global_float(float addrspace(1)* %input, i32 %i, i32 %j) {
98; IR-LABEL: @ld_int_from_global_float(
99; PTX-LABEL: ld_int_from_global_float(
100 %1 = addrspacecast float addrspace(1)* %input to float*
101 %2 = getelementptr float, float* %1, i32 %i
102; IR-NEXT: getelementptr float, float addrspace(1)* %input, i32 %i
103 %3 = getelementptr float, float* %2, i32 %j
104; IR-NEXT: getelementptr float, float addrspace(1)* {{%[^,]+}}, i32 %j
105 %4 = bitcast float* %3 to i32*
106; IR-NEXT: bitcast float addrspace(1)* {{%[^ ]+}} to i32 addrspace(1)*
107 %5 = load i32, i32* %4
108; IR-NEXT: load i32, i32 addrspace(1)* {{%.+}}
109; PTX-LABEL: ld.global
110 ret i32 %5
111}
112
Jingyue Wu75589ff2015-06-09 21:50:32 +0000113define void @nested_const_expr() {
114; PTX-LABEL: nested_const_expr(
115 ; store 1 to bitcast(gep(addrspacecast(array), 0, 1))
116 store i32 1, i32* bitcast (float* getelementptr ([10 x float], [10 x float]* addrspacecast ([10 x float] addrspace(3)* @array to [10 x float]*), i64 0, i64 1) to i32*), align 4
117; PTX: mov.u32 %r1, 1;
118; PTX-NEXT: st.shared.u32 [array+4], %r1;
119 ret void
120}
121
122define void @rauw(float addrspace(1)* %input) {
123 %generic_input = addrspacecast float addrspace(1)* %input to float*
124 %addr = getelementptr float, float* %generic_input, i64 10
125 %v = load float, float* %addr
126 store float %v, float* %addr
127 ret void
128; IR-LABEL: @rauw(
Jingyue Wu13755602016-03-20 20:59:20 +0000129; IR-NEXT: %addr = getelementptr float, float addrspace(1)* %input, i64 10
130; IR-NEXT: %v = load float, float addrspace(1)* %addr
131; IR-NEXT: store float %v, float addrspace(1)* %addr
Jingyue Wu75589ff2015-06-09 21:50:32 +0000132; IR-NEXT: ret void
133}
134
Jingyue Wu13755602016-03-20 20:59:20 +0000135define void @loop() {
Justin Lebared1e3122016-10-31 21:51:42 +0000136; IR-LABEL: @loop(
Jingyue Wu13755602016-03-20 20:59:20 +0000137entry:
138 %p = addrspacecast [10 x float] addrspace(3)* @array to float*
139 %end = getelementptr float, float* %p, i64 10
140 br label %loop
141
142loop:
143 %i = phi float* [ %p, %entry ], [ %i2, %loop ]
Justin Lebared1e3122016-10-31 21:51:42 +0000144; IR: phi float addrspace(3)* [ %p, %entry ], [ %i2, %loop ]
Jingyue Wu13755602016-03-20 20:59:20 +0000145 %v = load float, float* %i
Justin Lebared1e3122016-10-31 21:51:42 +0000146; IR: %v = load float, float addrspace(3)* %i
Jingyue Wu13755602016-03-20 20:59:20 +0000147 call void @use(float %v)
148 %i2 = getelementptr float, float* %i, i64 1
Justin Lebared1e3122016-10-31 21:51:42 +0000149; IR: %i2 = getelementptr float, float addrspace(3)* %i, i64 1
Jingyue Wu13755602016-03-20 20:59:20 +0000150 %exit_cond = icmp eq float* %i2, %end
151 br i1 %exit_cond, label %exit, label %loop
152
153exit:
154 ret void
155}
156
157@generic_end = external global float*
158
159define void @loop_with_generic_bound() {
Justin Lebared1e3122016-10-31 21:51:42 +0000160; IR-LABEL: @loop_with_generic_bound(
Jingyue Wu13755602016-03-20 20:59:20 +0000161entry:
162 %p = addrspacecast [10 x float] addrspace(3)* @array to float*
163 %end = load float*, float** @generic_end
164 br label %loop
165
166loop:
167 %i = phi float* [ %p, %entry ], [ %i2, %loop ]
Justin Lebared1e3122016-10-31 21:51:42 +0000168; IR: phi float addrspace(3)* [ %p, %entry ], [ %i2, %loop ]
Jingyue Wu13755602016-03-20 20:59:20 +0000169 %v = load float, float* %i
Justin Lebared1e3122016-10-31 21:51:42 +0000170; IR: %v = load float, float addrspace(3)* %i
Jingyue Wu13755602016-03-20 20:59:20 +0000171 call void @use(float %v)
172 %i2 = getelementptr float, float* %i, i64 1
Justin Lebared1e3122016-10-31 21:51:42 +0000173; IR: %i2 = getelementptr float, float addrspace(3)* %i, i64 1
Jingyue Wu13755602016-03-20 20:59:20 +0000174 %exit_cond = icmp eq float* %i2, %end
Justin Lebared1e3122016-10-31 21:51:42 +0000175; IR: addrspacecast float addrspace(3)* %i2 to float*
176; IR: icmp eq float* %{{[0-9]+}}, %end
Jingyue Wu13755602016-03-20 20:59:20 +0000177 br i1 %exit_cond, label %exit, label %loop
178
179exit:
180 ret void
181}
182
Justin Bognera4635372016-07-06 20:02:45 +0000183declare void @llvm.nvvm.barrier0() #3
Eli Benderskybbef1722014-04-03 21:18:25 +0000184
Jingyue Wu13755602016-03-20 20:59:20 +0000185declare void @use(float)
Eli Benderskybbef1722014-04-03 21:18:25 +0000186
Jingyue Wu13755602016-03-20 20:59:20 +0000187attributes #3 = { noduplicate nounwind }