xref: /llvm-project/clang/test/OpenMP/taskgroup_task_reduction_codegen.cpp (revision a009a60a917bc30940422bcef73f8270566d78db)
1 // RUN: %clang_cc1 -verify -triple x86_64-apple-darwin10 -fopenmp -x c++ -emit-llvm %s -o - | FileCheck %s
2 // RUN: %clang_cc1 -fopenmp -x c++ -triple x86_64-apple-darwin10 -emit-pch -o %t %s
3 // RUN: %clang_cc1 -fopenmp -x c++ -triple x86_64-apple-darwin10 -include-pch %t -verify %s -emit-llvm -o - | FileCheck %s
4 // RUN: %clang_cc1 -fopenmp -x c++ %s -verify -debug-info-kind=limited -emit-llvm -o - -triple x86_64-apple-darwin10 | FileCheck %s --check-prefix=CHECK --check-prefix=DEBUG
5 
6 // RUN: %clang_cc1 -verify -triple x86_64-apple-darwin10 -fopenmp-simd -x c++ -emit-llvm %s -o - | FileCheck --check-prefix SIMD-ONLY0 %s
7 // RUN: %clang_cc1 -fopenmp-simd -x c++ -triple x86_64-apple-darwin10 -emit-pch -o %t %s
8 // RUN: %clang_cc1 -fopenmp-simd -x c++ -triple x86_64-apple-darwin10 -include-pch %t -verify %s -emit-llvm -o - | FileCheck --check-prefix SIMD-ONLY0 %s
9 // RUN: %clang_cc1 -fopenmp-simd -x c++ %s -verify -debug-info-kind=limited -emit-llvm -o - -triple x86_64-apple-darwin10 | FileCheck --check-prefix SIMD-ONLY0 %s
10 // SIMD-ONLY0-NOT: {{__kmpc|__tgt}}
11 // expected-no-diagnostics
12 #ifndef HEADER
13 #define HEADER
14 
15 typedef void **omp_allocator_handle_t;
16 extern const omp_allocator_handle_t omp_default_mem_alloc;
17 extern const omp_allocator_handle_t omp_large_cap_mem_alloc;
18 extern const omp_allocator_handle_t omp_const_mem_alloc;
19 extern const omp_allocator_handle_t omp_high_bw_mem_alloc;
20 extern const omp_allocator_handle_t omp_low_lat_mem_alloc;
21 extern const omp_allocator_handle_t omp_cgroup_mem_alloc;
22 extern const omp_allocator_handle_t omp_pteam_mem_alloc;
23 extern const omp_allocator_handle_t omp_thread_mem_alloc;
24 
25 // CHECK-DAG: @reduction_size.[[ID:.+]]_[[CID:[0-9]+]].artificial.
26 // CHECK-DAG: @reduction_size.[[ID]]_[[CID]].artificial..cache.
27 
28 struct S {
29   int a;
30   S() : a(0) {}
31   S(const S&) {}
32   S& operator=(const S&) {return *this;}
33   ~S() {}
34   friend S operator+(const S&a, const S&b) {return a;}
35 };
36 
37 int main(int argc, char **argv) {
38   int a;
39   float b;
40   S c[5];
41   short d[argc];
42 #pragma omp taskgroup allocate(omp_pteam_mem_alloc: a) task_reduction(+: a, b, argc)
43   {
44 #pragma omp taskgroup task_reduction(-:c, d)
45     ;
46   }
47   return 0;
48 }
49 // CHECK-LABEL: @main
50 // CHECK:       alloca i32,
51 // CHECK:       [[ARGC_ADDR:%.+]] = alloca i32,
52 // CHECK:       [[ARGV_ADDR:%.+]] = alloca i8**,
53 // CHECK:       [[A:%.+]] = alloca i32,
54 // CHECK:       [[B:%.+]] = alloca float,
55 // CHECK:       [[C:%.+]] = alloca [5 x %struct.S],
56 // CHECK:       [[RD_IN1:%.+]] = alloca [3 x [[T1:%[^,]+]]],
57 // CHECK:       [[TD1:%.+]] = alloca i8*,
58 // CHECK:       [[RD_IN2:%.+]] = alloca [2 x [[T2:%[^,]+]]],
59 // CHECK:       [[TD2:%.+]] = alloca i8*,
60 
61 // CHECK:       [[GTID:%.+]] = call i32 @__kmpc_global_thread_num(%struct.ident_t*
62 // CHECK:       [[VLA:%.+]] = alloca i16, i64 [[VLA_SIZE:%[^,]+]],
63 
64 // CHECK:       call void @__kmpc_taskgroup(%struct.ident_t* {{[^,]+}}, i32 [[GTID]])
65 // CHECK-DAG:   [[BC_A:%.+]] = bitcast i32* [[A]] to i8*
66 // CHECK-DAG:   store i8* [[BC_A]], i8** [[A_REF:[^,]+]],
67 // CHECK-DAG:   [[A_REF]] = getelementptr inbounds [[T1]], [[T1]]* [[GEPA:%[^,]+]], i32 0, i32 0
68 // CHECK-DAG:   [[GEPA]] = getelementptr inbounds [3 x [[T1]]], [3 x [[T1]]]* [[RD_IN1]], i64 0, i64
69 // CHECK-DAG:   [[TMP6:%.+]] = getelementptr inbounds [[T1]], [[T1]]* [[GEPA]], i32 0, i32 1
70 // CHECK-DAG:   store i64 4, i64* [[TMP6]],
71 // CHECK-DAG:   [[TMP7:%.+]] = getelementptr inbounds [[T1]], [[T1]]* [[GEPA]], i32 0, i32 2
72 // CHECK-DAG:   store i8* bitcast (void (i8*)* @[[AINIT:.+]] to i8*), i8** [[TMP7]],
73 // CHECK-DAG:   [[TMP8:%.+]] = getelementptr inbounds [[T1]], [[T1]]* [[GEPA]], i32 0, i32 3
74 // CHECK-DAG:   store i8* null, i8** [[TMP8]],
75 // CHECK-DAG:   [[TMP9:%.+]] = getelementptr inbounds [[T1]], [[T1]]* [[GEPA]], i32 0, i32 4
76 // CHECK-DAG:   store i8* bitcast (void (i8*, i8*)* @[[ACOMB:.+]] to i8*), i8** [[TMP9]],
77 // CHECK-DAG:   [[TMP10:%.+]] = getelementptr inbounds [[T1]], [[T1]]* [[GEPA]], i32 0, i32 5
78 // CHECK-DAG:   [[TMP11:%.+]] = bitcast i32* [[TMP10]] to i8*
79 // CHECK-DAG:   call void @llvm.memset.p0i8.i64(i8* align 8 [[TMP11]], i8 0, i64 4, i1 false)
80 // CHECK-DAG:   [[TMP13:%.+]] = bitcast float* [[B]] to i8*
81 // CHECK-DAG:   store i8* [[TMP13]], i8** [[TMP12:%[^,]+]],
82 // CHECK-DAG:   [[TMP12]] = getelementptr inbounds [[T1]], [[T1]]* [[GEPB:%[^,]+]], i32 0, i32 0
83 // CHECK-DAG:   [[GEPB]] = getelementptr inbounds [3 x [[T1]]], [3 x [[T1]]]* [[RD_IN1]], i64 0, i64
84 // CHECK-DAG:   [[TMP14:%.+]] = getelementptr inbounds [[T1]], [[T1]]* [[GEPB]], i32 0, i32 1
85 // CHECK-DAG:   store i64 4, i64* [[TMP14]],
86 // CHECK-DAG:   [[TMP15:%.+]] = getelementptr inbounds [[T1]], [[T1]]* [[GEPB]], i32 0, i32 2
87 // CHECK-DAG:   store i8* bitcast (void (i8*)* @[[BINIT:.+]] to i8*), i8** [[TMP15]],
88 // CHECK-DAG:   [[TMP16:%.+]] = getelementptr inbounds [[T1]], [[T1]]* [[GEPB]], i32 0, i32 3
89 // CHECK-DAG:   store i8* null, i8** [[TMP16]],
90 // CHECK-DAG:   [[TMP17:%.+]] = getelementptr inbounds [[T1]], [[T1]]* [[GEPB]], i32 0, i32 4
91 // CHECK-DAG:   store i8* bitcast (void (i8*, i8*)* @[[BCOMB:.+]] to i8*), i8** [[TMP17]],
92 // CHECK-DAG:   [[TMP18:%.+]] = getelementptr inbounds [[T1]], [[T1]]* [[GEPB]], i32 0, i32 5
93 // CHECK-DAG:   [[TMP19:%.+]] = bitcast i32* [[TMP18]] to i8*
94 // CHECK-DAG:   call void @llvm.memset.p0i8.i64(i8* align 8 [[TMP19]], i8 0, i64 4, i1 false)
95 // CHECK-DAG:   [[TMP21:%.+]] = bitcast i32* [[ARGC_ADDR]] to i8*
96 // CHECK-DAG:   store i8* [[TMP21]], i8** [[TMP20:%[^,]+]],
97 // CHECK-DAG:   [[TMP20]] = getelementptr inbounds [[T1]], [[T1]]* [[GEPARGC:%[^,]+]], i32 0, i32 0
98 // CHECK-DAG:   [[GEPARGC]] = getelementptr inbounds [3 x [[T1]]], [3 x [[T1]]]* [[RD_IN1]], i64 0, i64
99 // CHECK-DAG:   [[TMP22:%.+]] = getelementptr inbounds [[T1]], [[T1]]* [[GEPARGC]], i32 0, i32 1
100 // CHECK-DAG:   store i64 4, i64* [[TMP22]],
101 // CHECK-DAG:   [[TMP23:%.+]] = getelementptr inbounds [[T1]], [[T1]]* [[GEPARGC]], i32 0, i32 2
102 // CHECK-DAG:   store i8* bitcast (void (i8*)* @[[ARGCINIT:.+]] to i8*), i8** [[TMP23]],
103 // CHECK-DAG:   [[TMP24:%.+]] = getelementptr inbounds [[T1]], [[T1]]* [[GEPARGC]], i32 0, i32 3
104 // CHECK-DAG:   store i8* null, i8** [[TMP24]],
105 // CHECK-DAG:   [[TMP25:%.+]] = getelementptr inbounds [[T1]], [[T1]]* [[GEPARGC]], i32 0, i32 4
106 // CHECK-DAG:   store i8* bitcast (void (i8*, i8*)* @[[ARGCCOMB:.+]] to i8*), i8** [[TMP25]],
107 // CHECK-DAG:   [[TMP26:%.+]] = getelementptr inbounds [[T1]], [[T1]]* [[GEPARGC]], i32 0, i32 5
108 // CHECK-DAG:   [[TMP27:%.+]] = bitcast i32* [[TMP26]] to i8*
109 // CHECK-DAG:   call void @llvm.memset.p0i8.i64(i8* align 8 [[TMP27]], i8 0, i64 4, i1 false)
110 // CHECK-DAG:   [[TMP28:%.+]] = bitcast [3 x [[T1]]]* [[RD_IN1]] to i8*
111 // CHECK-DAG:   [[TMP29:%.+]] = call i8* @__kmpc_task_reduction_init(i32 [[GTID]], i32 3, i8* [[TMP28]])
112 // DEBUG-DAG:   call void @llvm.dbg.declare(metadata i8** [[TD1]],
113 // CHECK-DAG:   store i8* [[TMP29]], i8** [[TD1]],
114 // CHECK-DAG:   call void @__kmpc_taskgroup(%struct.ident_t* {{[^,]+}}, i32 [[GTID]])
115 // CHECK-DAG:   [[TMP31:%.+]] = bitcast [5 x %struct.S]* [[C]] to i8*
116 // CHECK-DAG:   store i8* [[TMP31]], i8** [[TMP30:%[^,]+]],
117 // CHECK-DAG:   [[TMP30]] = getelementptr inbounds [[T2]], [[T2]]* [[GEPC:%[^,]+]], i32 0, i32 0
118 // CHECK-DAG:   [[GEPC]] = getelementptr inbounds [2 x [[T2]]], [2 x [[T2]]]* [[RD_IN2]], i64 0, i64
119 // CHECK-DAG:   [[TMP32:%.+]] = getelementptr inbounds [[T2]], [[T2]]* [[GEPC]], i32 0, i32 1
120 // CHECK-DAG:   store i64 20, i64* [[TMP32]],
121 // CHECK-DAG:   [[TMP33:%.+]] = getelementptr inbounds [[T2]], [[T2]]* [[GEPC]], i32 0, i32 2
122 // CHECK-DAG:   store i8* bitcast (void (i8*)* @[[CINIT:.+]] to i8*), i8** [[TMP33]],
123 // CHECK-DAG:   [[TMP34:%.+]] = getelementptr inbounds [[T2]], [[T2]]* [[GEPC]], i32 0, i32 3
124 // CHECK-DAG:   store i8* bitcast (void (i8*)* @[[CFINI:.+]] to i8*), i8** [[TMP34]],
125 // CHECK-DAG:   [[TMP35:%.+]] = getelementptr inbounds [[T2]], [[T2]]* [[GEPC]], i32 0, i32 4
126 // CHECK-DAG:   store i8* bitcast (void (i8*, i8*)* @[[CCOMB:.+]] to i8*), i8** [[TMP35]],
127 // CHECK-DAG:   [[TMP36:%.+]] = getelementptr inbounds [[T2]], [[T2]]* [[GEPC]], i32 0, i32 5
128 // CHECK-DAG:   [[TMP37:%.+]] = bitcast i32* [[TMP36]] to i8*
129 // CHECK-DAG:   call void @llvm.memset.p0i8.i64(i8* align 8 [[TMP37]], i8 0, i64 4, i1 false)
130 // CHECK-DAG:   [[TMP39:%.+]] = bitcast i16* [[VLA]] to i8*
131 // CHECK-DAG:   store i8* [[TMP39]], i8** [[TMP38:%[^,]+]],
132 // CHECK-DAG:   [[TMP38]] = getelementptr inbounds [[T2]], [[T2]]* [[GEPVLA:%[^,]+]], i32 0, i32 0
133 // CHECK-DAG:   [[GEPVLA]] = getelementptr inbounds [2 x [[T2]]], [2 x [[T2]]]* [[RD_IN2]], i64 0, i64
134 // CHECK-DAG:   [[TMP40:%.+]] = mul nuw i64 [[VLA_SIZE]], 2
135 // CHECK-DAG:   [[TMP41:%.+]] = udiv exact i64 [[TMP40]], ptrtoint (i16* getelementptr (i16, i16* null, i32 1) to i64)
136 // CHECK-DAG:   [[TMP42:%.+]] = getelementptr inbounds [[T2]], [[T2]]* [[GEPVLA]], i32 0, i32 1
137 // CHECK-DAG:   store i64 [[TMP40]], i64* [[TMP42]],
138 // CHECK-DAG:   [[TMP43:%.+]] = getelementptr inbounds [[T2]], [[T2]]* [[GEPVLA]], i32 0, i32 2
139 // CHECK-DAG:   store i8* bitcast (void (i8*)* @[[VLAINIT:.+]] to i8*), i8** [[TMP43]],
140 // CHECK-DAG:   [[TMP44:%.+]] = getelementptr inbounds [[T2]], [[T2]]* [[GEPVLA]], i32 0, i32 3
141 // CHECK-DAG:   store i8* null, i8** [[TMP44]],
142 // CHECK-DAG:   [[TMP45:%.+]] = getelementptr inbounds [[T2]], [[T2]]* [[GEPVLA]], i32 0, i32 4
143 // CHECK-DAG:   store i8* bitcast (void (i8*, i8*)* @[[VLACOMB:.+]] to i8*), i8** [[TMP45]],
144 // CHECK-DAG:   [[TMP46:%.+]] = getelementptr inbounds [[T2]], [[T2]]* [[GEPVLA]], i32 0, i32 5
145 // CHECK-DAG:   store i32 1, i32* [[TMP46]],
146 // CHECK:       [[TMP47:%.+]] = bitcast [2 x [[T2]]]* [[RD_IN2]] to i8*
147 // CHECK:       [[TMP48:%.+]] = call i8* @__kmpc_task_reduction_init(i32 [[GTID]], i32 2, i8* [[TMP47]])
148 // CHECK:       store i8* [[TMP48]], i8** [[TD2]],
149 // CHECK:       call void @__kmpc_end_taskgroup(%struct.ident_t* {{[^,]+}}, i32 [[GTID]])
150 // CHECK:       call void @__kmpc_end_taskgroup(%struct.ident_t* {{[^,]+}}, i32 [[GTID]])
151 
152 // CHECK-DAG: define internal void @[[AINIT]](i8* %0)
153 // CHECK-DAG: store i32 0, i32* %
154 // CHECK-DAG: ret void
155 // CHECK-DAG: }
156 
157 // CHECK-DAG: define internal void @[[ACOMB]](i8* %0, i8* %1)
158 // CHECK-DAG: add nsw i32 %
159 // CHECK-DAG: store i32 %
160 // CHECK-DAG: ret void
161 // CHECK-DAG: }
162 
163 // CHECK-DAG: define internal void @[[BINIT]](i8* %0)
164 // CHECK-DAG: store float 0.000000e+00, float* %
165 // CHECK-DAG: ret void
166 // CHECK-DAG: }
167 
168 // CHECK-DAG: define internal void @[[BCOMB]](i8* %0, i8* %1)
169 // CHECK-DAG: fadd float %
170 // CHECK-DAG: store float %
171 // CHECK-DAG: ret void
172 // CHECK-DAG: }
173 
174 // CHECK-DAG: define internal void @[[ARGCINIT]](i8* %0)
175 // CHECK-DAG: store i32 0, i32* %
176 // CHECK-DAG: ret void
177 // CHECK-DAG: }
178 
179 // CHECK-DAG: define internal void @[[ARGCCOMB]](i8* %0, i8* %1)
180 // CHECK-DAG: add nsw i32 %
181 // CHECK-DAG: store i32 %
182 // CHECK-DAG: ret void
183 // CHECK-DAG: }
184 
185 // CHECK-DAG: define internal void @[[CINIT]](i8* %0)
186 // CHECK-DAG: phi %struct.S* [
187 // CHECK-DAG: call {{.+}}(%struct.S* {{.+}})
188 // CHECK-DAG: br i1 %
189 // CHECK-DAG: ret void
190 // CHECK-DAG: }
191 
192 // CHECK-DAG: define internal void @[[CFINI]](i8* %0)
193 // CHECK-DAG: phi %struct.S* [
194 // CHECK-DAG: call {{.+}}(%struct.S* {{.+}})
195 // CHECK-DAG: br i1 %
196 // CHECK-DAG: ret void
197 // CHECK-DAG: }
198 
199 // CHECK-DAG: define internal void @[[CCOMB]](i8* %0, i8* %1)
200 // CHECK-DAG: phi %struct.S* [
201 // CHECK-DAG: phi %struct.S* [
202 // CHECK-DAG: call {{.+}}(%struct.S* {{.+}}, %struct.S* {{.+}}, %struct.S* {{.+}})
203 // CHECK-DAG: call {{.+}}(%struct.S* {{.+}}, %struct.S* {{.+}})
204 // CHECK-DAG: call {{.+}}(%struct.S* {{.+}})
205 // CHECK-DAG: br i1 %
206 // CHECK-DAG: ret void
207 // CHECK_DAG: }
208 
209 // CHECK-DAG: define internal void @[[VLAINIT]](i8* %0)
210 // CHECK-DAG: call i32 @__kmpc_global_thread_num(%struct.ident_t* {{[^,]+}})
211 // CHECK-DAG: call i8* @__kmpc_threadprivate_cached(%struct.ident_t*
212 // CHECK-DAG: phi i16* [
213 // CHECK-DAG: store i16 0, i16* %
214 // CHECK-DAG: br i1 %
215 // CHECK-DAG: ret void
216 // CHECK-DAG: }
217 
218 // CHECK-DAG: define internal void @[[VLACOMB]](i8* %0, i8* %1)
219 // CHECK-DAG: call i32 @__kmpc_global_thread_num(%struct.ident_t* {{[^,]+}})
220 // CHECK-DAG: call i8* @__kmpc_threadprivate_cached(%struct.ident_t*
221 // CHECK-DAG: phi i16* [
222 // CHECK-DAG: phi i16* [
223 // CHECK-DAG: sext i16 %{{.+}} to i32
224 // CHECK-DAG: add nsw i32 %
225 // CHECK-DAG: trunc i32 %{{.+}} to i16
226 // CHECK-DAG: store i16 %
227 // CHECK_DAG: br i1 %
228 // CHECK-DAG: ret void
229 // CHECK-DAG: }
230 #endif
231 
232 // DEBUG-LABEL: distinct !DICompileUnit
233 // DEBUG-DAG: distinct !DISubprogram(linkageName: "[[AINIT]]",
234 // DEBUG-DAG: distinct !DISubprogram(linkageName: "[[ACOMB]]",
235 // DEBUG-DAG: distinct !DISubprogram(linkageName: "[[BINIT]]",
236 // DEBUG-DAG: distinct !DISubprogram(linkageName: "[[BCOMB]]",
237 // DEBUG-DAG: distinct !DISubprogram(linkageName: "[[ARGCINIT]]",
238 // DEBUG-DAG: distinct !DISubprogram(linkageName: "[[ARGCCOMB]]",
239 // DEBUG-DAG: distinct !DISubprogram(linkageName: "[[CINIT]]",
240 // DEBUG-DAG: distinct !DISubprogram(linkageName: "[[CFINI]]",
241 // DEBUG-DAG: distinct !DISubprogram(linkageName: "[[CCOMB]]",
242 // DEBUG-DAG: distinct !DISubprogram(linkageName: "[[VLAINIT]]",
243 // DEBUG-DAG: distinct !DISubprogram(linkageName: "[[VLACOMB]]",
244