xref: /aosp_15_r20/external/clang/test/Parser/pragma-loop.cpp (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li // RUN: %clang_cc1 -std=c++11 -verify %s
2*67e74705SXin Li 
3*67e74705SXin Li // Note that this puts the expected lines before the directives to work around
4*67e74705SXin Li // limitations in the -verify mode.
5*67e74705SXin Li 
6*67e74705SXin Li template <int V, int I>
test_nontype_template_param(int * List,int Length)7*67e74705SXin Li void test_nontype_template_param(int *List, int Length) {
8*67e74705SXin Li #pragma clang loop vectorize_width(V) interleave_count(I)
9*67e74705SXin Li   for (int i = 0; i < Length; i++) {
10*67e74705SXin Li     List[i] = i;
11*67e74705SXin Li   }
12*67e74705SXin Li 
13*67e74705SXin Li #pragma clang loop vectorize_width(V + 4) interleave_count(I + 4)
14*67e74705SXin Li   for (int i = 0; i < Length; i++) {
15*67e74705SXin Li     List[i] = i;
16*67e74705SXin Li   }
17*67e74705SXin Li }
18*67e74705SXin Li 
19*67e74705SXin Li template <int V>
test_nontype_template_vectorize(int * List,int Length)20*67e74705SXin Li void test_nontype_template_vectorize(int *List, int Length) {
21*67e74705SXin Li   /* expected-error {{invalid value '-1'; must be positive}} */ #pragma clang loop vectorize_width(V)
22*67e74705SXin Li   for (int i = 0; i < Length; i++) {
23*67e74705SXin Li     List[i] = i;
24*67e74705SXin Li   }
25*67e74705SXin Li 
26*67e74705SXin Li   /* expected-error {{invalid value '0'; must be positive}} */ #pragma clang loop vectorize_width(V / 2)
27*67e74705SXin Li   for (int i = 0; i < Length; i++) {
28*67e74705SXin Li     List[i] += i;
29*67e74705SXin Li   }
30*67e74705SXin Li }
31*67e74705SXin Li 
32*67e74705SXin Li template <int I>
test_nontype_template_interleave(int * List,int Length)33*67e74705SXin Li void test_nontype_template_interleave(int *List, int Length) {
34*67e74705SXin Li   /* expected-error {{invalid value '-1'; must be positive}} */ #pragma clang loop interleave_count(I)
35*67e74705SXin Li   for (int i = 0; i < Length; i++) {
36*67e74705SXin Li     List[i] = i;
37*67e74705SXin Li   }
38*67e74705SXin Li 
39*67e74705SXin Li   /* expected-error {{invalid value '0'; must be positive}} */ #pragma clang loop interleave_count(2 % I)
40*67e74705SXin Li   for (int i = 0; i < Length; i++) {
41*67e74705SXin Li     List[i] = i;
42*67e74705SXin Li   }
43*67e74705SXin Li }
44*67e74705SXin Li 
45*67e74705SXin Li template <char V>
test_nontype_template_char(int * List,int Length)46*67e74705SXin Li void test_nontype_template_char(int *List, int Length) {
47*67e74705SXin Li   /* expected-error {{invalid argument of type 'char'; expected an integer type}} */ #pragma clang loop vectorize_width(V)
48*67e74705SXin Li   for (int i = 0; i < Length; i++) {
49*67e74705SXin Li     List[i] = i;
50*67e74705SXin Li   }
51*67e74705SXin Li }
52*67e74705SXin Li 
53*67e74705SXin Li template <bool V>
test_nontype_template_bool(int * List,int Length)54*67e74705SXin Li void test_nontype_template_bool(int *List, int Length) {
55*67e74705SXin Li   /* expected-error {{invalid argument of type 'bool'; expected an integer type}} */ #pragma clang loop vectorize_width(V)
56*67e74705SXin Li   for (int i = 0; i < Length; i++) {
57*67e74705SXin Li     List[i] = i;
58*67e74705SXin Li   }
59*67e74705SXin Li }
60*67e74705SXin Li 
61*67e74705SXin Li template <int V, int I>
test_nontype_template_badarg(int * List,int Length)62*67e74705SXin Li void test_nontype_template_badarg(int *List, int Length) {
63*67e74705SXin Li   /* expected-error {{use of undeclared identifier 'Vec'}} */ #pragma clang loop vectorize_width(Vec) interleave_count(I)
64*67e74705SXin Li   /* expected-error {{use of undeclared identifier 'Int'}} */ #pragma clang loop vectorize_width(V) interleave_count(Int)
65*67e74705SXin Li   for (int i = 0; i < Length; i++) {
66*67e74705SXin Li     List[i] = i;
67*67e74705SXin Li   }
68*67e74705SXin Li }
69*67e74705SXin Li 
70*67e74705SXin Li template <typename T>
test_type_template_vectorize(int * List,int Length)71*67e74705SXin Li void test_type_template_vectorize(int *List, int Length) {
72*67e74705SXin Li   const T Value = -1;
73*67e74705SXin Li   /* expected-error {{invalid value '-1'; must be positive}} */ #pragma clang loop vectorize_width(Value)
74*67e74705SXin Li   for (int i = 0; i < Length; i++) {
75*67e74705SXin Li     List[i] = i;
76*67e74705SXin Li   }
77*67e74705SXin Li }
78*67e74705SXin Li 
test(int * List,int Length)79*67e74705SXin Li void test(int *List, int Length) {
80*67e74705SXin Li   int i = 0;
81*67e74705SXin Li 
82*67e74705SXin Li #pragma clang loop vectorize(enable)
83*67e74705SXin Li #pragma clang loop interleave(enable)
84*67e74705SXin Li #pragma clang loop unroll(full)
85*67e74705SXin Li   while (i + 1 < Length) {
86*67e74705SXin Li     List[i] = i;
87*67e74705SXin Li   }
88*67e74705SXin Li 
89*67e74705SXin Li #pragma clang loop vectorize_width(4)
90*67e74705SXin Li #pragma clang loop interleave_count(8)
91*67e74705SXin Li #pragma clang loop unroll_count(16)
92*67e74705SXin Li   while (i < Length) {
93*67e74705SXin Li     List[i] = i;
94*67e74705SXin Li   }
95*67e74705SXin Li 
96*67e74705SXin Li #pragma clang loop vectorize(disable)
97*67e74705SXin Li #pragma clang loop interleave(disable)
98*67e74705SXin Li #pragma clang loop unroll(disable)
99*67e74705SXin Li   while (i - 1 < Length) {
100*67e74705SXin Li     List[i] = i;
101*67e74705SXin Li   }
102*67e74705SXin Li 
103*67e74705SXin Li #pragma clang loop vectorize_width(4) interleave_count(8) unroll_count(16)
104*67e74705SXin Li   while (i - 2 < Length) {
105*67e74705SXin Li     List[i] = i;
106*67e74705SXin Li   }
107*67e74705SXin Li 
108*67e74705SXin Li #pragma clang loop interleave_count(16)
109*67e74705SXin Li   while (i - 3 < Length) {
110*67e74705SXin Li     List[i] = i;
111*67e74705SXin Li   }
112*67e74705SXin Li 
113*67e74705SXin Li   int VList[Length];
114*67e74705SXin Li #pragma clang loop vectorize(disable) interleave(disable) unroll(disable)
115*67e74705SXin Li   for (int j : VList) {
116*67e74705SXin Li     VList[j] = List[j];
117*67e74705SXin Li   }
118*67e74705SXin Li 
119*67e74705SXin Li #pragma clang loop distribute(enable)
120*67e74705SXin Li   for (int j : VList) {
121*67e74705SXin Li     VList[j] = List[j];
122*67e74705SXin Li   }
123*67e74705SXin Li 
124*67e74705SXin Li #pragma clang loop distribute(disable)
125*67e74705SXin Li   for (int j : VList) {
126*67e74705SXin Li     VList[j] = List[j];
127*67e74705SXin Li   }
128*67e74705SXin Li 
129*67e74705SXin Li   test_nontype_template_param<4, 8>(List, Length);
130*67e74705SXin Li 
131*67e74705SXin Li /* expected-error {{expected '('}} */ #pragma clang loop vectorize
132*67e74705SXin Li /* expected-error {{expected '('}} */ #pragma clang loop interleave
133*67e74705SXin Li /* expected-error {{expected '('}} */ #pragma clang loop unroll
134*67e74705SXin Li /* expected-error {{expected '('}} */ #pragma clang loop distribute
135*67e74705SXin Li 
136*67e74705SXin Li /* expected-error {{expected ')'}} */ #pragma clang loop vectorize(enable
137*67e74705SXin Li /* expected-error {{expected ')'}} */ #pragma clang loop interleave(enable
138*67e74705SXin Li /* expected-error {{expected ')'}} */ #pragma clang loop unroll(full
139*67e74705SXin Li /* expected-error {{expected ')'}} */ #pragma clang loop distribute(enable
140*67e74705SXin Li 
141*67e74705SXin Li /* expected-error {{expected ')'}} */ #pragma clang loop vectorize_width(4
142*67e74705SXin Li /* expected-error {{expected ')'}} */ #pragma clang loop interleave_count(4
143*67e74705SXin Li /* expected-error {{expected ')'}} */ #pragma clang loop unroll_count(4
144*67e74705SXin Li 
145*67e74705SXin Li /* expected-error {{missing argument; expected 'enable', 'assume_safety' or 'disable'}} */ #pragma clang loop vectorize()
146*67e74705SXin Li /* expected-error {{missing argument; expected an integer value}} */ #pragma clang loop interleave_count()
147*67e74705SXin Li /* expected-error {{missing argument; expected 'enable', 'full' or 'disable'}} */ #pragma clang loop unroll()
148*67e74705SXin Li /* expected-error {{missing argument; expected 'enable' or 'disable'}} */ #pragma clang loop distribute()
149*67e74705SXin Li 
150*67e74705SXin Li /* expected-error {{missing option; expected vectorize, vectorize_width, interleave, interleave_count, unroll, unroll_count, or distribute}} */ #pragma clang loop
151*67e74705SXin Li /* expected-error {{invalid option 'badkeyword'}} */ #pragma clang loop badkeyword
152*67e74705SXin Li /* expected-error {{invalid option 'badkeyword'}} */ #pragma clang loop badkeyword(enable)
153*67e74705SXin Li /* expected-error {{invalid option 'badkeyword'}} */ #pragma clang loop vectorize(enable) badkeyword(4)
154*67e74705SXin Li /* expected-warning {{extra tokens at end of '#pragma clang loop'}} */ #pragma clang loop vectorize(enable) ,
155*67e74705SXin Li   while (i-4 < Length) {
156*67e74705SXin Li     List[i] = i;
157*67e74705SXin Li   }
158*67e74705SXin Li 
159*67e74705SXin Li /* expected-error {{invalid value '0'; must be positive}} */ #pragma clang loop vectorize_width(0)
160*67e74705SXin Li /* expected-error {{invalid value '0'; must be positive}} */ #pragma clang loop interleave_count(0)
161*67e74705SXin Li /* expected-error {{invalid value '0'; must be positive}} */ #pragma clang loop unroll_count(0)
162*67e74705SXin Li 
163*67e74705SXin Li /* expected-error {{expression is not an integral constant expression}} expected-note {{division by zero}} */ #pragma clang loop vectorize_width(10 / 0)
164*67e74705SXin Li /* expected-error {{invalid value '0'; must be positive}} */ #pragma clang loop interleave_count(10 / 5 - 2)
165*67e74705SXin Li   while (i-5 < Length) {
166*67e74705SXin Li     List[i] = i;
167*67e74705SXin Li   }
168*67e74705SXin Li 
169*67e74705SXin Li test_nontype_template_vectorize<4>(List, Length);
170*67e74705SXin Li /* expected-note {{in instantiation of function template specialization}} */ test_nontype_template_vectorize<-1>(List, Length);
171*67e74705SXin Li test_nontype_template_interleave<8>(List, Length);
172*67e74705SXin Li /* expected-note {{in instantiation of function template specialization}} */ test_nontype_template_interleave<-1>(List, Length);
173*67e74705SXin Li 
174*67e74705SXin Li /* expected-note {{in instantiation of function template specialization}} */ test_nontype_template_char<'A'>(List, Length); // Loop hint arg cannot be a char.
175*67e74705SXin Li /* expected-note {{in instantiation of function template specialization}} */ test_nontype_template_bool<true>(List, Length);  // Or a bool.
176*67e74705SXin Li /* expected-note {{in instantiation of function template specialization}} */ test_type_template_vectorize<int>(List, Length); // Or a template type.
177*67e74705SXin Li 
178*67e74705SXin Li /* expected-error {{value '3000000000' is too large}} */ #pragma clang loop vectorize_width(3000000000)
179*67e74705SXin Li /* expected-error {{value '3000000000' is too large}} */ #pragma clang loop interleave_count(3000000000)
180*67e74705SXin Li /* expected-error {{value '3000000000' is too large}} */ #pragma clang loop unroll_count(3000000000)
181*67e74705SXin Li   while (i-6 < Length) {
182*67e74705SXin Li     List[i] = i;
183*67e74705SXin Li   }
184*67e74705SXin Li 
185*67e74705SXin Li /* expected-warning {{extra tokens at end of '#pragma clang loop'}} */ #pragma clang loop vectorize_width(1 +) 1
186*67e74705SXin Li /* expected-warning {{extra tokens at end of '#pragma clang loop'}} */ #pragma clang loop vectorize_width(1) +1
187*67e74705SXin Li const int VV = 4;
188*67e74705SXin Li /* expected-error {{expected expression}} */ #pragma clang loop vectorize_width(VV +/ 2)
189*67e74705SXin Li /* expected-error {{use of undeclared identifier 'undefined'}} */ #pragma clang loop vectorize_width(VV+undefined)
190*67e74705SXin Li /* expected-error {{expected ')'}} */ #pragma clang loop vectorize_width(1+(^*/2 * ()
191*67e74705SXin Li /* expected-warning {{extra tokens at end of '#pragma clang loop' - ignored}} */ #pragma clang loop vectorize_width(1+(-0[0]))))))
192*67e74705SXin Li 
193*67e74705SXin Li /* expected-error {{use of undeclared identifier 'badvalue'}} */ #pragma clang loop vectorize_width(badvalue)
194*67e74705SXin Li /* expected-error {{use of undeclared identifier 'badvalue'}} */ #pragma clang loop interleave_count(badvalue)
195*67e74705SXin Li /* expected-error {{use of undeclared identifier 'badvalue'}} */ #pragma clang loop unroll_count(badvalue)
196*67e74705SXin Li   while (i-6 < Length) {
197*67e74705SXin Li     List[i] = i;
198*67e74705SXin Li   }
199*67e74705SXin Li 
200*67e74705SXin Li /* expected-error {{invalid argument; expected 'enable', 'assume_safety' or 'disable'}} */ #pragma clang loop vectorize(badidentifier)
201*67e74705SXin Li /* expected-error {{invalid argument; expected 'enable', 'assume_safety' or 'disable'}} */ #pragma clang loop interleave(badidentifier)
202*67e74705SXin Li /* expected-error {{invalid argument; expected 'enable', 'full' or 'disable'}} */ #pragma clang loop unroll(badidentifier)
203*67e74705SXin Li /* expected-error {{invalid argument; expected 'enable' or 'disable'}} */ #pragma clang loop distribute(badidentifier)
204*67e74705SXin Li   while (i-7 < Length) {
205*67e74705SXin Li     List[i] = i;
206*67e74705SXin Li   }
207*67e74705SXin Li 
208*67e74705SXin Li // PR20069 - Loop pragma arguments that are not identifiers or numeric
209*67e74705SXin Li // constants crash FE.
210*67e74705SXin Li /* expected-error {{expected ')'}} */ #pragma clang loop vectorize(()
211*67e74705SXin Li /* expected-error {{invalid argument; expected 'enable', 'assume_safety' or 'disable'}} */ #pragma clang loop interleave(*)
212*67e74705SXin Li /* expected-error {{invalid argument; expected 'enable', 'full' or 'disable'}} */ #pragma clang loop unroll(=)
213*67e74705SXin Li /* expected-error {{invalid argument; expected 'enable' or 'disable'}} */ #pragma clang loop distribute(+)
214*67e74705SXin Li /* expected-error {{type name requires a specifier or qualifier}} expected-error {{expected expression}} */ #pragma clang loop vectorize_width(^)
215*67e74705SXin Li /* expected-error {{expected expression}} expected-error {{expected expression}} */ #pragma clang loop interleave_count(/)
216*67e74705SXin Li /* expected-error {{expected expression}} expected-error {{expected expression}} */ #pragma clang loop unroll_count(==)
217*67e74705SXin Li   while (i-8 < Length) {
218*67e74705SXin Li     List[i] = i;
219*67e74705SXin Li   }
220*67e74705SXin Li 
221*67e74705SXin Li #pragma clang loop vectorize(enable)
222*67e74705SXin Li /* expected-error {{expected a for, while, or do-while loop to follow '#pragma clang loop'}} */ int j = Length;
223*67e74705SXin Li   List[0] = List[1];
224*67e74705SXin Li 
225*67e74705SXin Li   while (j-1 < Length) {
226*67e74705SXin Li     List[j] = j;
227*67e74705SXin Li   }
228*67e74705SXin Li 
229*67e74705SXin Li // FIXME: A bug in ParsedAttributes causes the order of the attributes to be
230*67e74705SXin Li // processed in reverse. Consequently, the errors occur on the first of pragma
231*67e74705SXin Li // of the next three tests rather than the last, and the order of the kinds
232*67e74705SXin Li // is also reversed.
233*67e74705SXin Li 
234*67e74705SXin Li /* expected-error {{incompatible directives 'vectorize(disable)' and 'vectorize_width(4)'}} */ #pragma clang loop vectorize_width(4)
235*67e74705SXin Li #pragma clang loop vectorize(disable)
236*67e74705SXin Li /* expected-error {{incompatible directives 'interleave(disable)' and 'interleave_count(4)'}} */ #pragma clang loop interleave_count(4)
237*67e74705SXin Li #pragma clang loop interleave(disable)
238*67e74705SXin Li /* expected-error {{incompatible directives 'unroll(disable)' and 'unroll_count(4)'}} */ #pragma clang loop unroll_count(4)
239*67e74705SXin Li #pragma clang loop unroll(disable)
240*67e74705SXin Li   while (i-8 < Length) {
241*67e74705SXin Li     List[i] = i;
242*67e74705SXin Li   }
243*67e74705SXin Li 
244*67e74705SXin Li /* expected-error {{duplicate directives 'vectorize(disable)' and 'vectorize(enable)'}} */ #pragma clang loop vectorize(enable)
245*67e74705SXin Li #pragma clang loop vectorize(disable)
246*67e74705SXin Li /* expected-error {{duplicate directives 'interleave(disable)' and 'interleave(enable)'}} */ #pragma clang loop interleave(enable)
247*67e74705SXin Li #pragma clang loop interleave(disable)
248*67e74705SXin Li /* expected-error {{duplicate directives 'unroll(disable)' and 'unroll(full)'}} */ #pragma clang loop unroll(full)
249*67e74705SXin Li #pragma clang loop unroll(disable)
250*67e74705SXin Li /* expected-error {{duplicate directives 'distribute(disable)' and 'distribute(enable)'}} */ #pragma clang loop distribute(enable)
251*67e74705SXin Li #pragma clang loop distribute(disable)
252*67e74705SXin Li   while (i-9 < Length) {
253*67e74705SXin Li     List[i] = i;
254*67e74705SXin Li   }
255*67e74705SXin Li 
256*67e74705SXin Li /* expected-error {{incompatible directives 'vectorize(disable)' and 'vectorize_width(4)'}} */ #pragma clang loop vectorize(disable)
257*67e74705SXin Li #pragma clang loop vectorize_width(4)
258*67e74705SXin Li /* expected-error {{incompatible directives 'interleave(disable)' and 'interleave_count(4)'}} */ #pragma clang loop interleave(disable)
259*67e74705SXin Li #pragma clang loop interleave_count(4)
260*67e74705SXin Li /* expected-error {{incompatible directives 'unroll(disable)' and 'unroll_count(4)'}} */ #pragma clang loop unroll(disable)
261*67e74705SXin Li #pragma clang loop unroll_count(4)
262*67e74705SXin Li   while (i-10 < Length) {
263*67e74705SXin Li     List[i] = i;
264*67e74705SXin Li   }
265*67e74705SXin Li 
266*67e74705SXin Li /* expected-error {{duplicate directives 'vectorize_width(4)' and 'vectorize_width(8)'}} */ #pragma clang loop vectorize_width(8)
267*67e74705SXin Li #pragma clang loop vectorize_width(4)
268*67e74705SXin Li /* expected-error {{duplicate directives 'interleave_count(4)' and 'interleave_count(8)'}} */ #pragma clang loop interleave_count(8)
269*67e74705SXin Li #pragma clang loop interleave_count(4)
270*67e74705SXin Li /* expected-error {{duplicate directives 'unroll_count(4)' and 'unroll_count(8)'}} */ #pragma clang loop unroll_count(8)
271*67e74705SXin Li #pragma clang loop unroll_count(4)
272*67e74705SXin Li   while (i-11 < Length) {
273*67e74705SXin Li     List[i] = i;
274*67e74705SXin Li   }
275*67e74705SXin Li 
276*67e74705SXin Li 
277*67e74705SXin Li /* expected-error {{incompatible directives 'unroll(full)' and 'unroll_count(4)'}} */ #pragma clang loop unroll(full)
278*67e74705SXin Li #pragma clang loop unroll_count(4)
279*67e74705SXin Li   while (i-11 < Length) {
280*67e74705SXin Li     List[i] = i;
281*67e74705SXin Li   }
282*67e74705SXin Li 
283*67e74705SXin Li #pragma clang loop interleave(enable)
284*67e74705SXin Li /* expected-error {{expected statement}} */ }
285