blob: 62520be2cf5a07e8ef0ebb36f2bf1b40719564ea [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
8
9; Verifies nvptx-favor-non-generic correctly optimizes generic address space
10; usage to non-generic address space usage for the patterns we claim to handle:
11; 1. load cast
12; 2. store cast
13; 3. load gep cast
14; 4. store gep cast
15; gep and cast can be an instruction or a constant expression. This function
16; tries all possible combinations.
Jingyue Wu13755602016-03-20 20:59:20 +000017define void @ld_st_shared_f32(i32 %i, float %v) {
Eli Benderskybbef1722014-04-03 21:18:25 +000018; IR-LABEL: @ld_st_shared_f32
19; IR-NOT: addrspacecast
20; PTX-LABEL: ld_st_shared_f32(
21 ; load cast
David Blaikiea79ac142015-02-27 21:17:42 +000022 %1 = load float, float* addrspacecast (float addrspace(3)* @scalar to float*), align 4
Jingyue Wu13755602016-03-20 20:59:20 +000023 call void @use(float %1)
Eli Benderskybbef1722014-04-03 21:18:25 +000024; PTX: ld.shared.f32 %f{{[0-9]+}}, [scalar];
25 ; store cast
26 store float %v, float* addrspacecast (float addrspace(3)* @scalar to float*), align 4
27; PTX: st.shared.f32 [scalar], %f{{[0-9]+}};
28 ; use syncthreads to disable optimizations across components
Justin Bognera4635372016-07-06 20:02:45 +000029 call void @llvm.nvvm.barrier0()
Eli Benderskybbef1722014-04-03 21:18:25 +000030; PTX: bar.sync 0;
31
32 ; cast; load
33 %2 = addrspacecast float addrspace(3)* @scalar to float*
David Blaikiea79ac142015-02-27 21:17:42 +000034 %3 = load float, float* %2, align 4
Jingyue Wu13755602016-03-20 20:59:20 +000035 call void @use(float %3)
Eli Benderskybbef1722014-04-03 21:18:25 +000036; PTX: ld.shared.f32 %f{{[0-9]+}}, [scalar];
37 ; cast; store
38 store float %v, float* %2, align 4
39; PTX: st.shared.f32 [scalar], %f{{[0-9]+}};
Justin Bognera4635372016-07-06 20:02:45 +000040 call void @llvm.nvvm.barrier0()
Eli Benderskybbef1722014-04-03 21:18:25 +000041; PTX: bar.sync 0;
42
43 ; load gep cast
David Blaikief72d05b2015-03-13 18:20:45 +000044 %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 +000045 call void @use(float %4)
Eli Benderskybbef1722014-04-03 21:18:25 +000046; PTX: ld.shared.f32 %f{{[0-9]+}}, [array+20];
47 ; store gep cast
David Blaikief72d05b2015-03-13 18:20:45 +000048 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 +000049; PTX: st.shared.f32 [array+20], %f{{[0-9]+}};
Justin Bognera4635372016-07-06 20:02:45 +000050 call void @llvm.nvvm.barrier0()
Eli Benderskybbef1722014-04-03 21:18:25 +000051; PTX: bar.sync 0;
52
53 ; gep cast; load
David Blaikie79e6c742015-02-27 19:29:02 +000054 %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 +000055 %6 = load float, float* %5, align 4
Jingyue Wu13755602016-03-20 20:59:20 +000056 call void @use(float %6)
Eli Benderskybbef1722014-04-03 21:18:25 +000057; PTX: ld.shared.f32 %f{{[0-9]+}}, [array+20];
58 ; gep cast; store
59 store float %v, float* %5, align 4
60; PTX: st.shared.f32 [array+20], %f{{[0-9]+}};
Justin Bognera4635372016-07-06 20:02:45 +000061 call void @llvm.nvvm.barrier0()
Eli Benderskybbef1722014-04-03 21:18:25 +000062; PTX: bar.sync 0;
63
64 ; cast; gep; load
65 %7 = addrspacecast [10 x float] addrspace(3)* @array to [10 x float]*
David Blaikie79e6c742015-02-27 19:29:02 +000066 %8 = getelementptr inbounds [10 x float], [10 x float]* %7, i32 0, i32 %i
David Blaikiea79ac142015-02-27 21:17:42 +000067 %9 = load float, float* %8, align 4
Jingyue Wu13755602016-03-20 20:59:20 +000068 call void @use(float %9)
Eli Benderskybbef1722014-04-03 21:18:25 +000069; PTX: ld.shared.f32 %f{{[0-9]+}}, [%{{(r|rl|rd)[0-9]+}}];
70 ; cast; gep; store
71 store float %v, float* %8, align 4
72; PTX: st.shared.f32 [%{{(r|rl|rd)[0-9]+}}], %f{{[0-9]+}};
Justin Bognera4635372016-07-06 20:02:45 +000073 call void @llvm.nvvm.barrier0()
Eli Benderskybbef1722014-04-03 21:18:25 +000074; PTX: bar.sync 0;
75
Jingyue Wu13755602016-03-20 20:59:20 +000076 ret void
Eli Benderskybbef1722014-04-03 21:18:25 +000077}
78
Jingyue Wubaabe502014-06-15 21:40:57 +000079; When hoisting an addrspacecast between different pointer types, replace the
80; addrspacecast with a bitcast.
Eli Benderskybbef1722014-04-03 21:18:25 +000081define i32 @ld_int_from_float() {
82; IR-LABEL: @ld_int_from_float
David Blaikiea79ac142015-02-27 21:17:42 +000083; IR: load i32, i32 addrspace(3)* bitcast (float addrspace(3)* @scalar to i32 addrspace(3)*)
Eli Benderskybbef1722014-04-03 21:18:25 +000084; PTX-LABEL: ld_int_from_float(
Jingyue Wubaabe502014-06-15 21:40:57 +000085; PTX: ld.shared.u{{(32|64)}}
David Blaikiea79ac142015-02-27 21:17:42 +000086 %1 = load i32, i32* addrspacecast(float addrspace(3)* @scalar to i32*), align 4
Eli Benderskybbef1722014-04-03 21:18:25 +000087 ret i32 %1
88}
89
Jingyue Wu995dde22015-05-29 17:00:27 +000090define i32 @ld_int_from_global_float(float addrspace(1)* %input, i32 %i, i32 %j) {
91; IR-LABEL: @ld_int_from_global_float(
92; PTX-LABEL: ld_int_from_global_float(
93 %1 = addrspacecast float addrspace(1)* %input to float*
94 %2 = getelementptr float, float* %1, i32 %i
95; IR-NEXT: getelementptr float, float addrspace(1)* %input, i32 %i
96 %3 = getelementptr float, float* %2, i32 %j
97; IR-NEXT: getelementptr float, float addrspace(1)* {{%[^,]+}}, i32 %j
98 %4 = bitcast float* %3 to i32*
99; IR-NEXT: bitcast float addrspace(1)* {{%[^ ]+}} to i32 addrspace(1)*
100 %5 = load i32, i32* %4
101; IR-NEXT: load i32, i32 addrspace(1)* {{%.+}}
102; PTX-LABEL: ld.global
103 ret i32 %5
104}
105
Jingyue Wu75589ff2015-06-09 21:50:32 +0000106define void @nested_const_expr() {
107; PTX-LABEL: nested_const_expr(
108 ; store 1 to bitcast(gep(addrspacecast(array), 0, 1))
109 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
110; PTX: mov.u32 %r1, 1;
111; PTX-NEXT: st.shared.u32 [array+4], %r1;
112 ret void
113}
114
115define void @rauw(float addrspace(1)* %input) {
116 %generic_input = addrspacecast float addrspace(1)* %input to float*
117 %addr = getelementptr float, float* %generic_input, i64 10
118 %v = load float, float* %addr
119 store float %v, float* %addr
120 ret void
121; IR-LABEL: @rauw(
Jingyue Wu13755602016-03-20 20:59:20 +0000122; IR-NEXT: %addr = getelementptr float, float addrspace(1)* %input, i64 10
123; IR-NEXT: %v = load float, float addrspace(1)* %addr
124; IR-NEXT: store float %v, float addrspace(1)* %addr
Jingyue Wu75589ff2015-06-09 21:50:32 +0000125; IR-NEXT: ret void
126}
127
Jingyue Wu13755602016-03-20 20:59:20 +0000128define void @loop() {
Justin Lebared1e3122016-10-31 21:51:42 +0000129; IR-LABEL: @loop(
Jingyue Wu13755602016-03-20 20:59:20 +0000130entry:
131 %p = addrspacecast [10 x float] addrspace(3)* @array to float*
132 %end = getelementptr float, float* %p, i64 10
133 br label %loop
134
135loop:
136 %i = phi float* [ %p, %entry ], [ %i2, %loop ]
Justin Lebared1e3122016-10-31 21:51:42 +0000137; IR: phi float addrspace(3)* [ %p, %entry ], [ %i2, %loop ]
Jingyue Wu13755602016-03-20 20:59:20 +0000138 %v = load float, float* %i
Justin Lebared1e3122016-10-31 21:51:42 +0000139; IR: %v = load float, float addrspace(3)* %i
Jingyue Wu13755602016-03-20 20:59:20 +0000140 call void @use(float %v)
141 %i2 = getelementptr float, float* %i, i64 1
Justin Lebared1e3122016-10-31 21:51:42 +0000142; IR: %i2 = getelementptr float, float addrspace(3)* %i, i64 1
Jingyue Wu13755602016-03-20 20:59:20 +0000143 %exit_cond = icmp eq float* %i2, %end
144 br i1 %exit_cond, label %exit, label %loop
145
146exit:
147 ret void
148}
149
150@generic_end = external global float*
151
152define void @loop_with_generic_bound() {
Justin Lebared1e3122016-10-31 21:51:42 +0000153; IR-LABEL: @loop_with_generic_bound(
Jingyue Wu13755602016-03-20 20:59:20 +0000154entry:
155 %p = addrspacecast [10 x float] addrspace(3)* @array to float*
156 %end = load float*, float** @generic_end
157 br label %loop
158
159loop:
160 %i = phi float* [ %p, %entry ], [ %i2, %loop ]
Justin Lebared1e3122016-10-31 21:51:42 +0000161; IR: phi float addrspace(3)* [ %p, %entry ], [ %i2, %loop ]
Jingyue Wu13755602016-03-20 20:59:20 +0000162 %v = load float, float* %i
Justin Lebared1e3122016-10-31 21:51:42 +0000163; IR: %v = load float, float addrspace(3)* %i
Jingyue Wu13755602016-03-20 20:59:20 +0000164 call void @use(float %v)
165 %i2 = getelementptr float, float* %i, i64 1
Justin Lebared1e3122016-10-31 21:51:42 +0000166; IR: %i2 = getelementptr float, float addrspace(3)* %i, i64 1
Jingyue Wu13755602016-03-20 20:59:20 +0000167 %exit_cond = icmp eq float* %i2, %end
Justin Lebared1e3122016-10-31 21:51:42 +0000168; IR: addrspacecast float addrspace(3)* %i2 to float*
169; IR: icmp eq float* %{{[0-9]+}}, %end
Jingyue Wu13755602016-03-20 20:59:20 +0000170 br i1 %exit_cond, label %exit, label %loop
171
172exit:
173 ret void
174}
175
Justin Bognera4635372016-07-06 20:02:45 +0000176declare void @llvm.nvvm.barrier0() #3
Eli Benderskybbef1722014-04-03 21:18:25 +0000177
Jingyue Wu13755602016-03-20 20:59:20 +0000178declare void @use(float)
Eli Benderskybbef1722014-04-03 21:18:25 +0000179
Jingyue Wu13755602016-03-20 20:59:20 +0000180attributes #3 = { noduplicate nounwind }