1 // Check code generation
2 // RUN: %clang_cc1 -verify -triple x86_64-pc-linux-gnu -fopenmp -emit-llvm %s -o - | FileCheck %s --check-prefix=IR
4 // Check same results after serialization round-trip
5 // RUN: %clang_cc1 -verify -triple x86_64-pc-linux-gnu -fopenmp -emit-pch -o %t %s
6 // RUN: %clang_cc1 -verify -triple x86_64-pc-linux-gnu -fopenmp -include-pch %t -emit-llvm %s -o - | FileCheck %s --check-prefix=IR
7 // expected-no-diagnostics
9 // Account for multiple transformations of a loop before consumed by
15 // placeholder for loop body code.
16 extern "C" void body(...) {}
19 // IR-LABEL: define {{.*}}@func(
20 // IR-NEXT: [[ENTRY:.*]]:
21 // IR-NEXT: %[[START_ADDR:.+]] = alloca i32, align 4
22 // IR-NEXT: %[[END_ADDR:.+]] = alloca i32, align 4
23 // IR-NEXT: %[[STEP_ADDR:.+]] = alloca i32, align 4
24 // IR-NEXT: %[[DOTOMP_IV:.+]] = alloca i32, align 4
25 // IR-NEXT: %[[TMP:.+]] = alloca i32, align 4
26 // IR-NEXT: %[[I:.+]] = alloca i32, align 4
27 // IR-NEXT: %[[DOTCAPTURE_EXPR_:.+]] = alloca i32, align 4
28 // IR-NEXT: %[[DOTCAPTURE_EXPR_1:.+]] = alloca i32, align 4
29 // IR-NEXT: %[[DOTNEW_STEP:.+]] = alloca i32, align 4
30 // IR-NEXT: %[[DOTCAPTURE_EXPR_2:.+]] = alloca i32, align 4
31 // IR-NEXT: %[[DOTFLOOR_0_IV_I:.+]] = alloca i32, align 4
32 // IR-NEXT: %[[DOTCAPTURE_EXPR_5:.+]] = alloca i32, align 4
33 // IR-NEXT: %[[DOTCAPTURE_EXPR_7:.+]] = alloca i32, align 4
34 // IR-NEXT: %[[DOTCAPTURE_EXPR_11:.+]] = alloca i32, align 4
35 // IR-NEXT: %[[DOTCAPTURE_EXPR_13:.+]] = alloca i32, align 4
36 // IR-NEXT: %[[DOTFLOOR_0_IV__FLOOR_0_IV_I:.+]] = alloca i32, align 4
37 // IR-NEXT: %[[DOTOMP_LB:.+]] = alloca i32, align 4
38 // IR-NEXT: %[[DOTOMP_UB:.+]] = alloca i32, align 4
39 // IR-NEXT: %[[DOTOMP_STRIDE:.+]] = alloca i32, align 4
40 // IR-NEXT: %[[DOTOMP_IS_LAST:.+]] = alloca i32, align 4
41 // IR-NEXT: %[[DOTFLOOR_0_IV__FLOOR_0_IV_I17:.+]] = alloca i32, align 4
42 // IR-NEXT: %[[DOTTILE_0_IV__FLOOR_0_IV_I:.+]] = alloca i32, align 4
43 // IR-NEXT: %[[DOTTILE_0_IV_I:.+]] = alloca i32, align 4
44 // IR-NEXT: %[[TMP0:.+]] = call i32 @__kmpc_global_thread_num(ptr @[[GLOB2:.+]])
45 // IR-NEXT: store i32 %[[START:.+]], ptr %[[START_ADDR]], align 4
46 // IR-NEXT: store i32 %[[END:.+]], ptr %[[END_ADDR]], align 4
47 // IR-NEXT: store i32 %[[STEP:.+]], ptr %[[STEP_ADDR]], align 4
48 // IR-NEXT: %[[TMP1:.+]] = load i32, ptr %[[START_ADDR]], align 4
49 // IR-NEXT: store i32 %[[TMP1]], ptr %[[I]], align 4
50 // IR-NEXT: %[[TMP2:.+]] = load i32, ptr %[[START_ADDR]], align 4
51 // IR-NEXT: store i32 %[[TMP2]], ptr %[[DOTCAPTURE_EXPR_]], align 4
52 // IR-NEXT: %[[TMP3:.+]] = load i32, ptr %[[END_ADDR]], align 4
53 // IR-NEXT: store i32 %[[TMP3]], ptr %[[DOTCAPTURE_EXPR_1]], align 4
54 // IR-NEXT: %[[TMP4:.+]] = load i32, ptr %[[STEP_ADDR]], align 4
55 // IR-NEXT: store i32 %[[TMP4]], ptr %[[DOTNEW_STEP]], align 4
56 // IR-NEXT: %[[TMP5:.+]] = load i32, ptr %[[DOTCAPTURE_EXPR_1]], align 4
57 // IR-NEXT: %[[TMP6:.+]] = load i32, ptr %[[DOTCAPTURE_EXPR_]], align 4
58 // IR-NEXT: %[[SUB:.+]] = sub i32 %[[TMP5]], %[[TMP6]]
59 // IR-NEXT: %[[SUB3:.+]] = sub i32 %[[SUB]], 1
60 // IR-NEXT: %[[TMP7:.+]] = load i32, ptr %[[DOTNEW_STEP]], align 4
61 // IR-NEXT: %[[ADD:.+]] = add i32 %[[SUB3]], %[[TMP7]]
62 // IR-NEXT: %[[TMP8:.+]] = load i32, ptr %[[DOTNEW_STEP]], align 4
63 // IR-NEXT: %[[DIV:.+]] = udiv i32 %[[ADD]], %[[TMP8]]
64 // IR-NEXT: %[[SUB4:.+]] = sub i32 %[[DIV]], 1
65 // IR-NEXT: store i32 %[[SUB4]], ptr %[[DOTCAPTURE_EXPR_2]], align 4
66 // IR-NEXT: store i32 0, ptr %[[DOTFLOOR_0_IV_I]], align 4
67 // IR-NEXT: %[[TMP9:.+]] = load i32, ptr %[[DOTCAPTURE_EXPR_2]], align 4
68 // IR-NEXT: %[[ADD6:.+]] = add i32 %[[TMP9]], 1
69 // IR-NEXT: store i32 %[[ADD6]], ptr %[[DOTCAPTURE_EXPR_5]], align 4
70 // IR-NEXT: %[[TMP10:.+]] = load i32, ptr %[[DOTCAPTURE_EXPR_5]], align 4
71 // IR-NEXT: %[[SUB8:.+]] = sub i32 %[[TMP10]], -3
72 // IR-NEXT: %[[DIV9:.+]] = udiv i32 %[[SUB8]], 4
73 // IR-NEXT: %[[SUB10:.+]] = sub i32 %[[DIV9]], 1
74 // IR-NEXT: store i32 %[[SUB10]], ptr %[[DOTCAPTURE_EXPR_7]], align 4
75 // IR-NEXT: %[[TMP11:.+]] = load i32, ptr %[[DOTCAPTURE_EXPR_7]], align 4
76 // IR-NEXT: %[[ADD12:.+]] = add i32 %[[TMP11]], 1
77 // IR-NEXT: store i32 %[[ADD12]], ptr %[[DOTCAPTURE_EXPR_11]], align 4
78 // IR-NEXT: %[[TMP12:.+]] = load i32, ptr %[[DOTCAPTURE_EXPR_11]], align 4
79 // IR-NEXT: %[[SUB14:.+]] = sub i32 %[[TMP12]], -2
80 // IR-NEXT: %[[DIV15:.+]] = udiv i32 %[[SUB14]], 3
81 // IR-NEXT: %[[SUB16:.+]] = sub i32 %[[DIV15]], 1
82 // IR-NEXT: store i32 %[[SUB16]], ptr %[[DOTCAPTURE_EXPR_13]], align 4
83 // IR-NEXT: store i32 0, ptr %[[DOTFLOOR_0_IV__FLOOR_0_IV_I]], align 4
84 // IR-NEXT: %[[TMP13:.+]] = load i32, ptr %[[DOTCAPTURE_EXPR_11]], align 4
85 // IR-NEXT: %[[CMP:.+]] = icmp ult i32 0, %[[TMP13]]
86 // IR-NEXT: br i1 %[[CMP]], label %[[OMP_PRECOND_THEN:.+]], label %[[OMP_PRECOND_END:.+]]
88 // IR-NEXT: [[OMP_PRECOND_THEN]]:
89 // IR-NEXT: store i32 0, ptr %[[DOTOMP_LB]], align 4
90 // IR-NEXT: %[[TMP14:.+]] = load i32, ptr %[[DOTCAPTURE_EXPR_13]], align 4
91 // IR-NEXT: store i32 %[[TMP14]], ptr %[[DOTOMP_UB]], align 4
92 // IR-NEXT: store i32 1, ptr %[[DOTOMP_STRIDE]], align 4
93 // IR-NEXT: store i32 0, ptr %[[DOTOMP_IS_LAST]], align 4
94 // IR-NEXT: call void @__kmpc_for_static_init_4u(ptr @[[GLOB1:.+]], i32 %[[TMP0]], i32 34, ptr %[[DOTOMP_IS_LAST]], ptr %[[DOTOMP_LB]], ptr %[[DOTOMP_UB]], ptr %[[DOTOMP_STRIDE]], i32 1, i32 1)
95 // IR-NEXT: %[[TMP15:.+]] = load i32, ptr %[[DOTOMP_UB]], align 4
96 // IR-NEXT: %[[TMP16:.+]] = load i32, ptr %[[DOTCAPTURE_EXPR_13]], align 4
97 // IR-NEXT: %[[CMP18:.+]] = icmp ugt i32 %[[TMP15]], %[[TMP16]]
98 // IR-NEXT: br i1 %[[CMP18]], label %[[COND_TRUE:.+]], label %[[COND_FALSE:.+]]
100 // IR-NEXT: [[COND_TRUE]]:
101 // IR-NEXT: %[[TMP17:.+]] = load i32, ptr %[[DOTCAPTURE_EXPR_13]], align 4
102 // IR-NEXT: br label %[[COND_END:.+]]
104 // IR-NEXT: [[COND_FALSE]]:
105 // IR-NEXT: %[[TMP18:.+]] = load i32, ptr %[[DOTOMP_UB]], align 4
106 // IR-NEXT: br label %[[COND_END]]
108 // IR-NEXT: [[COND_END]]:
109 // IR-NEXT: %[[COND:.+]] = phi i32 [ %[[TMP17]], %[[COND_TRUE]] ], [ %[[TMP18]], %[[COND_FALSE]] ]
110 // IR-NEXT: store i32 %[[COND]], ptr %[[DOTOMP_UB]], align 4
111 // IR-NEXT: %[[TMP19:.+]] = load i32, ptr %[[DOTOMP_LB]], align 4
112 // IR-NEXT: store i32 %[[TMP19]], ptr %[[DOTOMP_IV]], align 4
113 // IR-NEXT: br label %[[OMP_INNER_FOR_COND:.+]]
115 // IR-NEXT: [[OMP_INNER_FOR_COND]]:
116 // IR-NEXT: %[[TMP20:.+]] = load i32, ptr %[[DOTOMP_IV]], align 4
117 // IR-NEXT: %[[TMP21:.+]] = load i32, ptr %[[DOTOMP_UB]], align 4
118 // IR-NEXT: %[[ADD19:.+]] = add i32 %[[TMP21]], 1
119 // IR-NEXT: %[[CMP20:.+]] = icmp ult i32 %[[TMP20]], %[[ADD19]]
120 // IR-NEXT: br i1 %[[CMP20]], label %[[OMP_INNER_FOR_BODY:.+]], label %[[OMP_INNER_FOR_END:.+]]
122 // IR-NEXT: [[OMP_INNER_FOR_BODY]]:
123 // IR-NEXT: %[[TMP22:.+]] = load i32, ptr %[[DOTOMP_IV]], align 4
124 // IR-NEXT: %[[MUL:.+]] = mul i32 %[[TMP22]], 3
125 // IR-NEXT: %[[ADD21:.+]] = add i32 0, %[[MUL]]
126 // IR-NEXT: store i32 %[[ADD21]], ptr %[[DOTFLOOR_0_IV__FLOOR_0_IV_I17]], align 4
127 // IR-NEXT: %[[TMP23:.+]] = load i32, ptr %[[DOTFLOOR_0_IV__FLOOR_0_IV_I17]], align 4
128 // IR-NEXT: store i32 %[[TMP23]], ptr %[[DOTTILE_0_IV__FLOOR_0_IV_I]], align 4
129 // IR-NEXT: br label %[[FOR_COND:.+]]
131 // IR-NEXT: [[FOR_COND]]:
132 // IR-NEXT: %[[TMP24:.+]] = load i32, ptr %[[DOTTILE_0_IV__FLOOR_0_IV_I]], align 4
133 // IR-NEXT: %[[TMP25:.+]] = load i32, ptr %[[DOTCAPTURE_EXPR_7]], align 4
134 // IR-NEXT: %[[ADD22:.+]] = add i32 %[[TMP25]], 1
135 // IR-NEXT: %[[TMP26:.+]] = load i32, ptr %[[DOTFLOOR_0_IV__FLOOR_0_IV_I17]], align 4
136 // IR-NEXT: %[[ADD23:.+]] = add i32 %[[TMP26]], 3
137 // IR-NEXT: %[[CMP24:.+]] = icmp ult i32 %[[ADD22]], %[[ADD23]]
138 // IR-NEXT: br i1 %[[CMP24]], label %[[COND_TRUE25:.+]], label %[[COND_FALSE27:.+]]
140 // IR-NEXT: [[COND_TRUE25]]:
141 // IR-NEXT: %[[TMP27:.+]] = load i32, ptr %[[DOTCAPTURE_EXPR_7]], align 4
142 // IR-NEXT: %[[ADD26:.+]] = add i32 %[[TMP27]], 1
143 // IR-NEXT: br label %[[COND_END29:.+]]
145 // IR-NEXT: [[COND_FALSE27]]:
146 // IR-NEXT: %[[TMP28:.+]] = load i32, ptr %[[DOTFLOOR_0_IV__FLOOR_0_IV_I17]], align 4
147 // IR-NEXT: %[[ADD28:.+]] = add i32 %[[TMP28]], 3
148 // IR-NEXT: br label %[[COND_END29]]
150 // IR-NEXT: [[COND_END29]]:
151 // IR-NEXT: %[[COND30:.+]] = phi i32 [ %[[ADD26]], %[[COND_TRUE25]] ], [ %[[ADD28]], %[[COND_FALSE27]] ]
152 // IR-NEXT: %[[CMP31:.+]] = icmp ult i32 %[[TMP24]], %[[COND30]]
153 // IR-NEXT: br i1 %[[CMP31]], label %[[FOR_BODY:.+]], label %[[FOR_END50:.+]]
155 // IR-NEXT: [[FOR_BODY]]:
156 // IR-NEXT: %[[TMP29:.+]] = load i32, ptr %[[DOTTILE_0_IV__FLOOR_0_IV_I]], align 4
157 // IR-NEXT: %[[MUL32:.+]] = mul i32 %[[TMP29]], 4
158 // IR-NEXT: %[[ADD33:.+]] = add i32 0, %[[MUL32]]
159 // IR-NEXT: store i32 %[[ADD33]], ptr %[[DOTFLOOR_0_IV_I]], align 4
160 // IR-NEXT: %[[TMP30:.+]] = load i32, ptr %[[DOTFLOOR_0_IV_I]], align 4
161 // IR-NEXT: store i32 %[[TMP30]], ptr %[[DOTTILE_0_IV_I]], align 4
162 // IR-NEXT: br label %[[FOR_COND34:.+]]
164 // IR-NEXT: [[FOR_COND34]]:
165 // IR-NEXT: %[[TMP31:.+]] = load i32, ptr %[[DOTTILE_0_IV_I]], align 4
166 // IR-NEXT: %[[TMP32:.+]] = load i32, ptr %[[DOTCAPTURE_EXPR_2]], align 4
167 // IR-NEXT: %[[ADD35:.+]] = add i32 %[[TMP32]], 1
168 // IR-NEXT: %[[TMP33:.+]] = load i32, ptr %[[DOTFLOOR_0_IV_I]], align 4
169 // IR-NEXT: %[[ADD36:.+]] = add i32 %[[TMP33]], 4
170 // IR-NEXT: %[[CMP37:.+]] = icmp ult i32 %[[ADD35]], %[[ADD36]]
171 // IR-NEXT: br i1 %[[CMP37]], label %[[COND_TRUE38:.+]], label %[[COND_FALSE40:.+]]
173 // IR-NEXT: [[COND_TRUE38]]:
174 // IR-NEXT: %[[TMP34:.+]] = load i32, ptr %[[DOTCAPTURE_EXPR_2]], align 4
175 // IR-NEXT: %[[ADD39:.+]] = add i32 %[[TMP34]], 1
176 // IR-NEXT: br label %[[COND_END42:.+]]
178 // IR-NEXT: [[COND_FALSE40]]:
179 // IR-NEXT: %[[TMP35:.+]] = load i32, ptr %[[DOTFLOOR_0_IV_I]], align 4
180 // IR-NEXT: %[[ADD41:.+]] = add i32 %[[TMP35]], 4
181 // IR-NEXT: br label %[[COND_END42]]
183 // IR-NEXT: [[COND_END42]]:
184 // IR-NEXT: %[[COND43:.+]] = phi i32 [ %[[ADD39]], %[[COND_TRUE38]] ], [ %[[ADD41]], %[[COND_FALSE40]] ]
185 // IR-NEXT: %[[CMP44:.+]] = icmp ult i32 %[[TMP31]], %[[COND43]]
186 // IR-NEXT: br i1 %[[CMP44]], label %[[FOR_BODY45:.+]], label %[[FOR_END:.+]]
188 // IR-NEXT: [[FOR_BODY45]]:
189 // IR-NEXT: %[[TMP36:.+]] = load i32, ptr %[[DOTCAPTURE_EXPR_]], align 4
190 // IR-NEXT: %[[TMP37:.+]] = load i32, ptr %[[DOTTILE_0_IV_I]], align 4
191 // IR-NEXT: %[[TMP38:.+]] = load i32, ptr %[[DOTNEW_STEP]], align 4
192 // IR-NEXT: %[[MUL46:.+]] = mul i32 %[[TMP37]], %[[TMP38]]
193 // IR-NEXT: %[[ADD47:.+]] = add i32 %[[TMP36]], %[[MUL46]]
194 // IR-NEXT: store i32 %[[ADD47]], ptr %[[I]], align 4
195 // IR-NEXT: %[[TMP39:.+]] = load i32, ptr %[[START_ADDR]], align 4
196 // IR-NEXT: %[[TMP40:.+]] = load i32, ptr %[[END_ADDR]], align 4
197 // IR-NEXT: %[[TMP41:.+]] = load i32, ptr %[[STEP_ADDR]], align 4
198 // IR-NEXT: %[[TMP42:.+]] = load i32, ptr %[[I]], align 4
199 // IR-NEXT: call void (...) @body(i32 noundef %[[TMP39]], i32 noundef %[[TMP40]], i32 noundef %[[TMP41]], i32 noundef %[[TMP42]])
200 // IR-NEXT: br label %[[FOR_INC:.+]]
202 // IR-NEXT: [[FOR_INC]]:
203 // IR-NEXT: %[[TMP43:.+]] = load i32, ptr %[[DOTTILE_0_IV_I]], align 4
204 // IR-NEXT: %[[INC:.+]] = add i32 %[[TMP43]], 1
205 // IR-NEXT: store i32 %[[INC]], ptr %[[DOTTILE_0_IV_I]], align 4
206 // IR-NEXT: br label %[[FOR_COND34]], !llvm.loop ![[LOOP3:[0-9]+]]
208 // IR-NEXT: [[FOR_END]]:
209 // IR-NEXT: br label %[[FOR_INC48:.+]]
211 // IR-NEXT: [[FOR_INC48]]:
212 // IR-NEXT: %[[TMP44:.+]] = load i32, ptr %[[DOTTILE_0_IV__FLOOR_0_IV_I]], align 4
213 // IR-NEXT: %[[INC49:.+]] = add i32 %[[TMP44]], 1
214 // IR-NEXT: store i32 %[[INC49]], ptr %[[DOTTILE_0_IV__FLOOR_0_IV_I]], align 4
215 // IR-NEXT: br label %[[FOR_COND]], !llvm.loop ![[LOOP5:[0-9]+]]
217 // IR-NEXT: [[FOR_END50]]:
218 // IR-NEXT: br label %[[OMP_BODY_CONTINUE:.+]]
220 // IR-NEXT: [[OMP_BODY_CONTINUE]]:
221 // IR-NEXT: br label %[[OMP_INNER_FOR_INC:.+]]
223 // IR-NEXT: [[OMP_INNER_FOR_INC]]:
224 // IR-NEXT: %[[TMP45:.+]] = load i32, ptr %[[DOTOMP_IV]], align 4
225 // IR-NEXT: %[[ADD51:.+]] = add i32 %[[TMP45]], 1
226 // IR-NEXT: store i32 %[[ADD51]], ptr %[[DOTOMP_IV]], align 4
227 // IR-NEXT: br label %[[OMP_INNER_FOR_COND]]
229 // IR-NEXT: [[OMP_INNER_FOR_END]]:
230 // IR-NEXT: br label %[[OMP_LOOP_EXIT:.+]]
232 // IR-NEXT: [[OMP_LOOP_EXIT]]:
233 // IR-NEXT: call void @__kmpc_for_static_fini(ptr @[[GLOB1]], i32 %[[TMP0]])
234 // IR-NEXT: br label %[[OMP_PRECOND_END]]
236 // IR-NEXT: [[OMP_PRECOND_END]]:
237 // IR-NEXT: call void @__kmpc_barrier(ptr @[[GLOB3:.+]], i32 %[[TMP0]])
242 extern "C" void func(int start
, int end
, int step
) {
244 #pragma omp tile sizes(3)
245 #pragma omp tile sizes(4)
246 for (int i
= start
; i
< end
; i
+= step
)
247 body(start
, end
, step
, i
);
252 // IR: ![[META0:[0-9]+]] = !{i32 1, !"wchar_size", i32 4}
253 // IR: ![[META1:[0-9]+]] = !{i32 7, !"openmp", i32 51}
254 // IR: ![[META2:[0-9]+]] =
255 // IR: ![[LOOP3]] = distinct !{![[LOOP3]], ![[LOOPPROP4:[0-9]+]]}
256 // IR: ![[LOOPPROP4]] = !{!"llvm.loop.mustprogress"}
257 // IR: ![[LOOP5]] = distinct !{![[LOOP5]], ![[LOOPPROP4]]}