1 // RUN: %clang_cc1 -triple x86_64-apple-darwin -std=c++11 -emit-llvm -o - %s | FileCheck %s
2 
3 // Verify while loop is recognized after sequence of pragma clang loop directives.
while_test(int * List,int Length)4 void while_test(int *List, int Length) {
5   // CHECK: define {{.*}} @_Z10while_test
6   int i = 0;
7 
8 #pragma clang loop vectorize(enable)
9 #pragma clang loop interleave_count(4)
10 #pragma clang loop vectorize_width(4)
11 #pragma clang loop unroll(full)
12 #pragma clang loop distribute(enable)
13   while (i < Length) {
14     // CHECK: br label {{.*}}, !llvm.loop ![[LOOP_1:.*]]
15     List[i] = i * 2;
16     i++;
17   }
18 }
19 
20 // Verify do loop is recognized after multi-option pragma clang loop directive.
do_test(int * List,int Length)21 void do_test(int *List, int Length) {
22   int i = 0;
23 
24 #pragma clang loop vectorize_width(8) interleave_count(4) unroll(disable) distribute(disable)
25   do {
26     // CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}, !llvm.loop ![[LOOP_2:.*]]
27     List[i] = i * 2;
28     i++;
29   } while (i < Length);
30 }
31 
32 enum struct Tuner : short { Interleave = 4, Unroll = 8 };
33 
34 // Verify for loop is recognized after sequence of pragma clang loop directives.
for_test(int * List,int Length)35 void for_test(int *List, int Length) {
36 #pragma clang loop interleave(enable)
37 #pragma clang loop interleave_count(static_cast<int>(Tuner::Interleave))
38 #pragma clang loop unroll_count(static_cast<int>(Tuner::Unroll))
39   for (int i = 0; i < Length; i++) {
40     // CHECK: br label {{.*}}, !llvm.loop ![[LOOP_3:.*]]
41     List[i] = i * 2;
42   }
43 }
44 
45 // Verify c++11 for range loop is recognized after
46 // sequence of pragma clang loop directives.
for_range_test()47 void for_range_test() {
48   double List[100];
49 
50 #pragma clang loop vectorize_width(2) interleave_count(2)
51   for (int i : List) {
52     // CHECK: br label {{.*}}, !llvm.loop ![[LOOP_4:.*]]
53     List[i] = i;
54   }
55 }
56 
57 // Verify disable pragma clang loop directive generates correct metadata
disable_test(int * List,int Length)58 void disable_test(int *List, int Length) {
59 #pragma clang loop vectorize(disable) unroll(disable) distribute(disable)
60   for (int i = 0; i < Length; i++) {
61     // CHECK: br label {{.*}}, !llvm.loop ![[LOOP_5:.*]]
62     List[i] = i * 2;
63   }
64 }
65 
66 #define VECWIDTH 2
67 #define INTCOUNT 2
68 #define UNROLLCOUNT 8
69 
70 // Verify defines are correctly resolved in pragma clang loop directive
for_define_test(int * List,int Length,int Value)71 void for_define_test(int *List, int Length, int Value) {
72 #pragma clang loop vectorize_width(VECWIDTH) interleave_count(INTCOUNT)
73 #pragma clang loop unroll_count(UNROLLCOUNT)
74   for (int i = 0; i < Length; i++) {
75     // CHECK: br label {{.*}}, !llvm.loop ![[LOOP_6:.*]]
76     List[i] = i * Value;
77   }
78 }
79 
80 // Verify constant expressions are handled correctly.
for_contant_expression_test(int * List,int Length)81 void for_contant_expression_test(int *List, int Length) {
82 #pragma clang loop vectorize_width(1 + 4)
83   for (int i = 0; i < Length; i++) {
84     // CHECK: br label {{.*}}, !llvm.loop ![[LOOP_7:.*]]
85     List[i] = i;
86   }
87 
88 #pragma clang loop vectorize_width(3 + VECWIDTH)
89   for (int i = 0; i < Length; i++) {
90     // CHECK: br label {{.*}}, !llvm.loop ![[LOOP_8:.*]]
91     List[i] += i;
92   }
93 }
94 
95 // Verify metadata is generated when template is used.
96 template <typename A>
for_template_test(A * List,int Length,A Value)97 void for_template_test(A *List, int Length, A Value) {
98 #pragma clang loop vectorize_width(8) interleave_count(8) unroll_count(8)
99   for (int i = 0; i < Length; i++) {
100     // CHECK: br label {{.*}}, !llvm.loop ![[LOOP_9:.*]]
101     List[i] = i * Value;
102   }
103 }
104 
105 // Verify define is resolved correctly when template is used.
106 template <typename A, typename T>
for_template_define_test(A * List,int Length,A Value)107 void for_template_define_test(A *List, int Length, A Value) {
108   const T VWidth = VECWIDTH;
109   const T ICount = INTCOUNT;
110   const T UCount = UNROLLCOUNT;
111 #pragma clang loop vectorize_width(VWidth) interleave_count(ICount)
112 #pragma clang loop unroll_count(UCount)
113   for (int i = 0; i < Length; i++) {
114     // CHECK: br label {{.*}}, !llvm.loop ![[LOOP_10:.*]]
115     List[i] = i * Value;
116   }
117 }
118 
119 // Verify templates and constant expressions are handled correctly.
120 template <typename A, int V, int I, int U>
for_template_constant_expression_test(A * List,int Length)121 void for_template_constant_expression_test(A *List, int Length) {
122 #pragma clang loop vectorize_width(V) interleave_count(I) unroll_count(U)
123   for (int i = 0; i < Length; i++) {
124     // CHECK: br label {{.*}}, !llvm.loop ![[LOOP_11:.*]]
125     List[i] = i;
126   }
127 
128 #pragma clang loop vectorize_width(V * 2 + VECWIDTH) interleave_count(I * 2 + INTCOUNT) unroll_count(U * 2 + UNROLLCOUNT)
129   for (int i = 0; i < Length; i++) {
130     // CHECK: br label {{.*}}, !llvm.loop ![[LOOP_12:.*]]
131     List[i] += i;
132   }
133 
134   const int Scale = 4;
135 #pragma clang loop vectorize_width(Scale * V) interleave_count(Scale * I) unroll_count(Scale * U)
136   for (int i = 0; i < Length; i++) {
137     // CHECK: br label {{.*}}, !llvm.loop ![[LOOP_13:.*]]
138     List[i] += i;
139   }
140 
141 #pragma clang loop vectorize_width((Scale * V) + 2)
142   for (int i = 0; i < Length; i++) {
143     // CHECK: br label {{.*}}, !llvm.loop ![[LOOP_14:.*]]
144     List[i] += i;
145   }
146 }
147 
148 #undef VECWIDTH
149 #undef INTCOUNT
150 #undef UNROLLCOUNT
151 
152 // Use templates defined above. Test verifies metadata is generated correctly.
template_test(double * List,int Length)153 void template_test(double *List, int Length) {
154   double Value = 10;
155 
156   for_template_test<double>(List, Length, Value);
157   for_template_define_test<double, int>(List, Length, Value);
158   for_template_constant_expression_test<double, 2, 4, 8>(List, Length);
159 }
160 
161 // Verify for loop is performing fixed width vectorization
for_test_fixed_16(int * List,int Length)162 void for_test_fixed_16(int *List, int Length) {
163 #pragma clang loop vectorize_width(16, fixed) interleave_count(4) unroll(disable) distribute(disable)
164   for (int i = 0; i < Length; i++) {
165     // CHECK: br label {{.*}}, !llvm.loop ![[LOOP_15:.*]]
166     List[i] = i * 2;
167   }
168 }
169 
170 // Verify for loop is performing scalable vectorization
for_test_scalable_16(int * List,int Length)171 void for_test_scalable_16(int *List, int Length) {
172 #pragma clang loop vectorize_width(16, scalable) interleave_count(4) unroll(disable) distribute(disable)
173   for (int i = 0; i < Length; i++) {
174     // CHECK: br label {{.*}}, !llvm.loop ![[LOOP_16:.*]]
175     List[i] = i * 2;
176   }
177 }
178 
179 // Verify for loop is performing fixed width vectorization
for_test_fixed(int * List,int Length)180 void for_test_fixed(int *List, int Length) {
181 #pragma clang loop vectorize_width(fixed) interleave_count(4) unroll(disable) distribute(disable)
182   for (int i = 0; i < Length; i++) {
183     // CHECK: br label {{.*}}, !llvm.loop ![[LOOP_17:.*]]
184     List[i] = i * 2;
185   }
186 }
187 
188 // Verify for loop is performing scalable vectorization
for_test_scalable(int * List,int Length)189 void for_test_scalable(int *List, int Length) {
190 #pragma clang loop vectorize_width(scalable) interleave_count(4) unroll(disable) distribute(disable)
191   for (int i = 0; i < Length; i++) {
192     // CHECK: br label {{.*}}, !llvm.loop ![[LOOP_18:.*]]
193     List[i] = i * 2;
194   }
195 }
196 
197 // Verify for loop is performing scalable vectorization
for_test_scalable_1(int * List,int Length)198 void for_test_scalable_1(int *List, int Length) {
199 #pragma clang loop vectorize_width(1, scalable) interleave_count(4) unroll(disable) distribute(disable)
200   for (int i = 0; i < Length; i++) {
201     // CHECK: br label {{.*}}, !llvm.loop ![[LOOP_19:.*]]
202     List[i] = i * 2;
203   }
204 }
205 
206 // CHECK: ![[LOOP_1]] = distinct !{![[LOOP_1]], [[MP:![0-9]+]], ![[UNROLL_FULL:.*]]}
207 // CHECK: ![[UNROLL_FULL]] = !{!"llvm.loop.unroll.full"}
208 
209 // CHECK: ![[LOOP_2]] = distinct !{![[LOOP_2]], [[MP]], ![[UNROLL_DISABLE:.*]], ![[DISTRIBUTE_DISABLE:.*]], ![[WIDTH_8:.*]], ![[FIXED_VEC:.*]], ![[INTERLEAVE_4:.*]], ![[VECTORIZE_ENABLE:.*]]}
210 // CHECK: ![[UNROLL_DISABLE]] = !{!"llvm.loop.unroll.disable"}
211 // CHECK: ![[DISTRIBUTE_DISABLE]] = !{!"llvm.loop.distribute.enable", i1 false}
212 // CHECK: ![[WIDTH_8]] = !{!"llvm.loop.vectorize.width", i32 8}
213 // CHECK: ![[FIXED_VEC]] = !{!"llvm.loop.vectorize.scalable.enable", i1 false}
214 // CHECK: ![[INTERLEAVE_4]] = !{!"llvm.loop.interleave.count", i32 4}
215 // CHECK: ![[VECTORIZE_ENABLE]] = !{!"llvm.loop.vectorize.enable", i1 true}
216 
217 // CHECK: ![[LOOP_3]] = distinct !{![[LOOP_3]], [[MP]], ![[INTERLEAVE_4:.*]], ![[VECTORIZE_ENABLE]], ![[FOLLOWUP_VECTOR_3:.*]]}
218 // CHECK: ![[FOLLOWUP_VECTOR_3]] = !{!"llvm.loop.vectorize.followup_all", ![[AFTER_VECTOR_3:.*]]}
219 // CHECK: ![[AFTER_VECTOR_3]] = distinct !{![[AFTER_VECTOR_3]], [[MP]], ![[ISVECTORIZED:.*]], ![[UNROLL_8:.*]]}
220 // CHECK: ![[ISVECTORIZED]] = !{!"llvm.loop.isvectorized"}
221 // CHECK: ![[UNROLL_8]] = !{!"llvm.loop.unroll.count", i32 8}
222 
223 // CHECK: ![[LOOP_4]] = distinct !{![[LOOP_4]], ![[WIDTH_2:.*]], ![[FIXED_VEC]], ![[INTERLEAVE_2:.*]], ![[VECTORIZE_ENABLE]]}
224 // CHECK: ![[WIDTH_2]] = !{!"llvm.loop.vectorize.width", i32 2}
225 // CHECK: ![[INTERLEAVE_2]] = !{!"llvm.loop.interleave.count", i32 2}
226 
227 // CHECK: ![[LOOP_5]] = distinct !{![[LOOP_5]], ![[UNROLL_DISABLE:.*]], ![[DISTRIBUTE_DISABLE:.*]], ![[WIDTH_1:.*]]}
228 // CHECK: ![[WIDTH_1]] = !{!"llvm.loop.vectorize.width", i32 1}
229 
230 // CHECK: ![[LOOP_6]] = distinct !{![[LOOP_6]], [[MP]], ![[WIDTH_2:.*]], ![[FIXED_VEC]], ![[INTERLEAVE_2:.*]], ![[FOLLOWUP_VECTOR_6:.*]]}
231 // CHECK: ![[FOLLOWUP_VECTOR_6]] = !{!"llvm.loop.vectorize.followup_all", ![[AFTER_VECTOR_6:.*]]}
232 // CHECK: ![[AFTER_VECTOR_6]] = distinct !{![[AFTER_VECTOR_6]], ![[ISVECTORIZED:.*]], ![[UNROLL_8:.*]]}
233 
234 // CHECK: ![[LOOP_7]] = distinct !{![[LOOP_7]], [[MP]], ![[WIDTH_5:.*]], ![[FIXED_VEC]], ![[VECTORIZE_ENABLE]]}
235 // CHECK: ![[WIDTH_5]] = !{!"llvm.loop.vectorize.width", i32 5}
236 
237 // CHECK: ![[LOOP_8]] = distinct !{![[LOOP_8]], [[MP]], ![[WIDTH_5:.*]], ![[FIXED_VEC]], ![[VECTORIZE_ENABLE]]}
238 
239 // CHECK: ![[LOOP_9]] = distinct !{![[LOOP_9]], ![[WIDTH_8:.*]], ![[FIXED_VEC]], ![[INTERLEAVE_8:.*]], ![[FOLLOWUP_VECTOR_9:.*]]}
240 // CHECK: ![[FOLLOWUP_VECTOR_9]] = !{!"llvm.loop.vectorize.followup_all", ![[AFTER_VECTOR_9:.*]]}
241 // CHECK: ![[AFTER_VECTOR_9]] = distinct !{![[AFTER_VECTOR_9]], ![[ISVECTORIZED:.*]], ![[UNROLL_8:.*]]}
242 
243 // CHECK: ![[LOOP_10]] = distinct !{![[LOOP_10]], ![[WIDTH_2:.*]], ![[FIXED_VEC]], ![[INTERLEAVE_2:.*]], ![[FOLLOWUP_VECTOR_10:.*]]}
244 // CHECK: ![[FOLLOWUP_VECTOR_10]] = !{!"llvm.loop.vectorize.followup_all", ![[AFTER_VECTOR_10:.*]]}
245 // CHECK: ![[AFTER_VECTOR_10]] = distinct !{![[AFTER_VECTOR_10]], ![[ISVECTORIZED:.*]], ![[UNROLL_8:.*]]}
246 
247 // CHECK: ![[LOOP_11]] = distinct !{![[LOOP_11]], ![[WIDTH_2:.*]], ![[FIXED_VEC]], ![[INTERLEAVE_4:.*]], ![[FOLLOWUP_VECTOR_11:.*]]}
248 // CHECK: ![[FOLLOWUP_VECTOR_11]] = !{!"llvm.loop.vectorize.followup_all", ![[AFTER_VECTOR_11:.*]]}
249 // CHECK: ![[AFTER_VECTOR_11]] = distinct !{![[AFTER_VECTOR_11]], ![[ISVECTORIZED:.*]], ![[UNROLL_8:.*]]}
250 
251 // CHECK: ![[LOOP_12]] = distinct !{![[LOOP_12]], ![[WIDTH_6:.*]], ![[FIXED_VEC]], ![[INTERLEAVE_10:.*]], ![[FOLLOWUP_VECTOR_12:.*]]}
252 // CHECK: ![[FOLLOWUP_VECTOR_12]] = !{!"llvm.loop.vectorize.followup_all", ![[AFTER_VECTOR_12:.*]]}
253 // CHECK: ![[AFTER_VECTOR_12]] = distinct !{![[AFTER_VECTOR_12]], ![[ISVECTORIZED:.*]], ![[UNROLL_24:.*]]}
254 // CHECK: ![[UNROLL_24]] = !{!"llvm.loop.unroll.count", i32 24}
255 
256 // CHECK: ![[LOOP_13]] = distinct !{![[LOOP_13]], ![[WIDTH_8:.*]], ![[INTERLEAVE_16:.*]], ![[VECTORIZE_ENABLE]], ![[FOLLOWUP_VECTOR_13:.*]]}
257 // CHECK: ![[INTERLEAVE_16]] = !{!"llvm.loop.interleave.count", i32 16}
258 // CHECK: ![[FOLLOWUP_VECTOR_13]] = !{!"llvm.loop.vectorize.followup_all", ![[AFTER_VECTOR_13:.*]]}
259 // CHECK: ![[AFTER_VECTOR_13]] = distinct !{![[AFTER_VECTOR_13]], ![[ISVECTORIZED:.*]], ![[UNROLL_32:.*]]}
260 // CHECK: ![[UNROLL_32]] = !{!"llvm.loop.unroll.count", i32 32}
261 
262 // CHECK: ![[LOOP_14]] = distinct !{![[LOOP_14]], [[MP]], ![[WIDTH_10:.*]], ![[FIXED_VEC]], ![[VECTORIZE_ENABLE]]}
263 // CHECK: ![[WIDTH_10]] = !{!"llvm.loop.vectorize.width", i32 10}
264 
265 // CHECK: ![[LOOP_15]] = distinct !{![[LOOP_15]], ![[UNROLL_DISABLE:.*]], ![[DISTRIBUTE_DISABLE:.*]], ![[WIDTH_16:.*]], ![[FIXED_VEC]], ![[INTERLEAVE_4:.*]], ![[VECTORIZE_ENABLE:.*]]}
266 // CHECK: ![[WIDTH_16]] = !{!"llvm.loop.vectorize.width", i32 16}
267 
268 // CHECK: ![[LOOP_16]] = distinct !{![[LOOP_16]], ![[UNROLL_DISABLE:.*]], ![[DISTRIBUTE_DISABLE:.*]], ![[WIDTH_16]], ![[SCALABLE_VEC:.*]], ![[INTERLEAVE_4:.*]], ![[VECTORIZE_ENABLE:.*]]}
269 // CHECK: ![[SCALABLE_VEC]] = !{!"llvm.loop.vectorize.scalable.enable", i1 true}
270 
271 // CHECK: ![[LOOP_17]] = distinct !{![[LOOP_17]], ![[UNROLL_DISABLE:.*]], ![[DISTRIBUTE_DISABLE:.*]], ![[FIXED_VEC]], ![[INTERLEAVE_4:.*]], ![[VECTORIZE_ENABLE:.*]]}
272 // CHECK: ![[LOOP_18]] = distinct !{![[LOOP_18]], ![[UNROLL_DISABLE:.*]], ![[DISTRIBUTE_DISABLE:.*]], ![[SCALABLE_VEC]], ![[INTERLEAVE_4:.*]], ![[VECTORIZE_ENABLE:.*]]}
273 // CHECK: ![[LOOP_19]] = distinct !{![[LOOP_19]], ![[UNROLL_DISABLE:.*]], ![[DISTRIBUTE_DISABLE:.*]], ![[WIDTH_1]], ![[SCALABLE_VEC]], ![[INTERLEAVE_4:.*]], ![[VECTORIZE_ENABLE:.*]]}
274