xref: /minix3/external/bsd/llvm/dist/clang/test/CodeGen/pragma-loop.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc // RUN: %clang_cc1 -triple x86_64-apple-darwin -std=c++11 -emit-llvm -o - %s | FileCheck %s
2*0a6a1f1dSLionel Sambuc 
3*0a6a1f1dSLionel Sambuc // Verify while loop is recognized after sequence of pragma clang loop directives.
while_test(int * List,int Length)4*0a6a1f1dSLionel Sambuc void while_test(int *List, int Length) {
5*0a6a1f1dSLionel Sambuc   // CHECK: define {{.*}} @_Z10while_test
6*0a6a1f1dSLionel Sambuc   int i = 0;
7*0a6a1f1dSLionel Sambuc 
8*0a6a1f1dSLionel Sambuc #pragma clang loop vectorize(enable)
9*0a6a1f1dSLionel Sambuc #pragma clang loop interleave_count(4)
10*0a6a1f1dSLionel Sambuc #pragma clang loop vectorize_width(4)
11*0a6a1f1dSLionel Sambuc #pragma clang loop unroll(full)
12*0a6a1f1dSLionel Sambuc   while (i < Length) {
13*0a6a1f1dSLionel Sambuc     // CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}, !llvm.loop ![[LOOP_1:.*]]
14*0a6a1f1dSLionel Sambuc     List[i] = i * 2;
15*0a6a1f1dSLionel Sambuc     i++;
16*0a6a1f1dSLionel Sambuc   }
17*0a6a1f1dSLionel Sambuc }
18*0a6a1f1dSLionel Sambuc 
19*0a6a1f1dSLionel Sambuc // Verify do loop is recognized after multi-option pragma clang loop directive.
do_test(int * List,int Length)20*0a6a1f1dSLionel Sambuc void do_test(int *List, int Length) {
21*0a6a1f1dSLionel Sambuc   int i = 0;
22*0a6a1f1dSLionel Sambuc 
23*0a6a1f1dSLionel Sambuc #pragma clang loop vectorize_width(8) interleave_count(4) unroll(disable)
24*0a6a1f1dSLionel Sambuc   do {
25*0a6a1f1dSLionel Sambuc     // CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}, !llvm.loop ![[LOOP_2:.*]]
26*0a6a1f1dSLionel Sambuc     List[i] = i * 2;
27*0a6a1f1dSLionel Sambuc     i++;
28*0a6a1f1dSLionel Sambuc   } while (i < Length);
29*0a6a1f1dSLionel Sambuc }
30*0a6a1f1dSLionel Sambuc 
31*0a6a1f1dSLionel Sambuc enum struct Tuner : short { Interleave = 4, Unroll = 8 };
32*0a6a1f1dSLionel Sambuc 
33*0a6a1f1dSLionel Sambuc // Verify for loop is recognized after sequence of pragma clang loop directives.
for_test(int * List,int Length)34*0a6a1f1dSLionel Sambuc void for_test(int *List, int Length) {
35*0a6a1f1dSLionel Sambuc #pragma clang loop interleave(enable)
36*0a6a1f1dSLionel Sambuc #pragma clang loop interleave_count(static_cast<int>(Tuner::Interleave))
37*0a6a1f1dSLionel Sambuc #pragma clang loop unroll_count(static_cast<int>(Tuner::Unroll))
38*0a6a1f1dSLionel Sambuc   for (int i = 0; i < Length; i++) {
39*0a6a1f1dSLionel Sambuc     // CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}, !llvm.loop ![[LOOP_3:.*]]
40*0a6a1f1dSLionel Sambuc     List[i] = i * 2;
41*0a6a1f1dSLionel Sambuc   }
42*0a6a1f1dSLionel Sambuc }
43*0a6a1f1dSLionel Sambuc 
44*0a6a1f1dSLionel Sambuc // Verify c++11 for range loop is recognized after
45*0a6a1f1dSLionel Sambuc // sequence of pragma clang loop directives.
for_range_test()46*0a6a1f1dSLionel Sambuc void for_range_test() {
47*0a6a1f1dSLionel Sambuc   double List[100];
48*0a6a1f1dSLionel Sambuc 
49*0a6a1f1dSLionel Sambuc #pragma clang loop vectorize_width(2) interleave_count(2)
50*0a6a1f1dSLionel Sambuc   for (int i : List) {
51*0a6a1f1dSLionel Sambuc     // CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}, !llvm.loop ![[LOOP_4:.*]]
52*0a6a1f1dSLionel Sambuc     List[i] = i;
53*0a6a1f1dSLionel Sambuc   }
54*0a6a1f1dSLionel Sambuc }
55*0a6a1f1dSLionel Sambuc 
56*0a6a1f1dSLionel Sambuc // Verify disable pragma clang loop directive generates correct metadata
disable_test(int * List,int Length)57*0a6a1f1dSLionel Sambuc void disable_test(int *List, int Length) {
58*0a6a1f1dSLionel Sambuc #pragma clang loop vectorize(disable) unroll(disable)
59*0a6a1f1dSLionel Sambuc   for (int i = 0; i < Length; i++) {
60*0a6a1f1dSLionel Sambuc     // CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}, !llvm.loop ![[LOOP_5:.*]]
61*0a6a1f1dSLionel Sambuc     List[i] = i * 2;
62*0a6a1f1dSLionel Sambuc   }
63*0a6a1f1dSLionel Sambuc }
64*0a6a1f1dSLionel Sambuc 
65*0a6a1f1dSLionel Sambuc #define VECWIDTH 2
66*0a6a1f1dSLionel Sambuc #define INTCOUNT 2
67*0a6a1f1dSLionel Sambuc #define UNROLLCOUNT 8
68*0a6a1f1dSLionel Sambuc 
69*0a6a1f1dSLionel Sambuc // Verify defines are correctly resolved in pragma clang loop directive
for_define_test(int * List,int Length,int Value)70*0a6a1f1dSLionel Sambuc void for_define_test(int *List, int Length, int Value) {
71*0a6a1f1dSLionel Sambuc #pragma clang loop vectorize_width(VECWIDTH) interleave_count(INTCOUNT)
72*0a6a1f1dSLionel Sambuc #pragma clang loop unroll_count(UNROLLCOUNT)
73*0a6a1f1dSLionel Sambuc   for (int i = 0; i < Length; i++) {
74*0a6a1f1dSLionel Sambuc     // CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}, !llvm.loop ![[LOOP_6:.*]]
75*0a6a1f1dSLionel Sambuc     List[i] = i * Value;
76*0a6a1f1dSLionel Sambuc   }
77*0a6a1f1dSLionel Sambuc }
78*0a6a1f1dSLionel Sambuc 
79*0a6a1f1dSLionel Sambuc // Verify constant expressions are handled correctly.
for_contant_expression_test(int * List,int Length)80*0a6a1f1dSLionel Sambuc void for_contant_expression_test(int *List, int Length) {
81*0a6a1f1dSLionel Sambuc #pragma clang loop vectorize_width(1 + 4)
82*0a6a1f1dSLionel Sambuc   for (int i = 0; i < Length; i++) {
83*0a6a1f1dSLionel Sambuc     // CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}, !llvm.loop ![[LOOP_7:.*]]
84*0a6a1f1dSLionel Sambuc     List[i] = i;
85*0a6a1f1dSLionel Sambuc   }
86*0a6a1f1dSLionel Sambuc 
87*0a6a1f1dSLionel Sambuc #pragma clang loop vectorize_width(3 + VECWIDTH)
88*0a6a1f1dSLionel Sambuc   for (int i = 0; i < Length; i++) {
89*0a6a1f1dSLionel Sambuc     // CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}, !llvm.loop ![[LOOP_8:.*]]
90*0a6a1f1dSLionel Sambuc     List[i] += i;
91*0a6a1f1dSLionel Sambuc   }
92*0a6a1f1dSLionel Sambuc }
93*0a6a1f1dSLionel Sambuc 
94*0a6a1f1dSLionel Sambuc // Verify metadata is generated when template is used.
95*0a6a1f1dSLionel Sambuc template <typename A>
for_template_test(A * List,int Length,A Value)96*0a6a1f1dSLionel Sambuc void for_template_test(A *List, int Length, A Value) {
97*0a6a1f1dSLionel Sambuc #pragma clang loop vectorize_width(8) interleave_count(8) unroll_count(8)
98*0a6a1f1dSLionel Sambuc   for (int i = 0; i < Length; i++) {
99*0a6a1f1dSLionel Sambuc     // CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}, !llvm.loop ![[LOOP_9:.*]]
100*0a6a1f1dSLionel Sambuc     List[i] = i * Value;
101*0a6a1f1dSLionel Sambuc   }
102*0a6a1f1dSLionel Sambuc }
103*0a6a1f1dSLionel Sambuc 
104*0a6a1f1dSLionel Sambuc // Verify define is resolved correctly when template is used.
105*0a6a1f1dSLionel Sambuc template <typename A, typename T>
for_template_define_test(A * List,int Length,A Value)106*0a6a1f1dSLionel Sambuc void for_template_define_test(A *List, int Length, A Value) {
107*0a6a1f1dSLionel Sambuc   const T VWidth = VECWIDTH;
108*0a6a1f1dSLionel Sambuc   const T ICount = INTCOUNT;
109*0a6a1f1dSLionel Sambuc   const T UCount = UNROLLCOUNT;
110*0a6a1f1dSLionel Sambuc #pragma clang loop vectorize_width(VWidth) interleave_count(ICount)
111*0a6a1f1dSLionel Sambuc #pragma clang loop unroll_count(UCount)
112*0a6a1f1dSLionel Sambuc   for (int i = 0; i < Length; i++) {
113*0a6a1f1dSLionel Sambuc     // CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}, !llvm.loop ![[LOOP_10:.*]]
114*0a6a1f1dSLionel Sambuc     List[i] = i * Value;
115*0a6a1f1dSLionel Sambuc   }
116*0a6a1f1dSLionel Sambuc }
117*0a6a1f1dSLionel Sambuc 
118*0a6a1f1dSLionel Sambuc // Verify templates and constant expressions are handled correctly.
119*0a6a1f1dSLionel Sambuc template <typename A, int V, int I, int U>
for_template_constant_expression_test(A * List,int Length)120*0a6a1f1dSLionel Sambuc void for_template_constant_expression_test(A *List, int Length) {
121*0a6a1f1dSLionel Sambuc #pragma clang loop vectorize_width(V) interleave_count(I) unroll_count(U)
122*0a6a1f1dSLionel Sambuc   for (int i = 0; i < Length; i++) {
123*0a6a1f1dSLionel Sambuc     // CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}, !llvm.loop ![[LOOP_11:.*]]
124*0a6a1f1dSLionel Sambuc     List[i] = i;
125*0a6a1f1dSLionel Sambuc   }
126*0a6a1f1dSLionel Sambuc 
127*0a6a1f1dSLionel Sambuc #pragma clang loop vectorize_width(V * 2 + VECWIDTH) interleave_count(I * 2 + INTCOUNT) unroll_count(U * 2 + UNROLLCOUNT)
128*0a6a1f1dSLionel Sambuc   for (int i = 0; i < Length; i++) {
129*0a6a1f1dSLionel Sambuc     // CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}, !llvm.loop ![[LOOP_12:.*]]
130*0a6a1f1dSLionel Sambuc     List[i] += i;
131*0a6a1f1dSLionel Sambuc   }
132*0a6a1f1dSLionel Sambuc 
133*0a6a1f1dSLionel Sambuc   const int Scale = 4;
134*0a6a1f1dSLionel Sambuc #pragma clang loop vectorize_width(Scale * V) interleave_count(Scale * I) unroll_count(Scale * U)
135*0a6a1f1dSLionel Sambuc   for (int i = 0; i < Length; i++) {
136*0a6a1f1dSLionel Sambuc     // CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}, !llvm.loop ![[LOOP_13:.*]]
137*0a6a1f1dSLionel Sambuc     List[i] += i;
138*0a6a1f1dSLionel Sambuc   }
139*0a6a1f1dSLionel Sambuc 
140*0a6a1f1dSLionel Sambuc #pragma clang loop vectorize_width((Scale * V) + 2)
141*0a6a1f1dSLionel Sambuc   for (int i = 0; i < Length; i++) {
142*0a6a1f1dSLionel Sambuc     // CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}, !llvm.loop ![[LOOP_14:.*]]
143*0a6a1f1dSLionel Sambuc     List[i] += i;
144*0a6a1f1dSLionel Sambuc   }
145*0a6a1f1dSLionel Sambuc }
146*0a6a1f1dSLionel Sambuc 
147*0a6a1f1dSLionel Sambuc #undef VECWIDTH
148*0a6a1f1dSLionel Sambuc #undef INTCOUNT
149*0a6a1f1dSLionel Sambuc #undef UNROLLCOUNT
150*0a6a1f1dSLionel Sambuc 
151*0a6a1f1dSLionel Sambuc // Use templates defined above. Test verifies metadata is generated correctly.
template_test(double * List,int Length)152*0a6a1f1dSLionel Sambuc void template_test(double *List, int Length) {
153*0a6a1f1dSLionel Sambuc   double Value = 10;
154*0a6a1f1dSLionel Sambuc 
155*0a6a1f1dSLionel Sambuc   for_template_test<double>(List, Length, Value);
156*0a6a1f1dSLionel Sambuc   for_template_define_test<double, int>(List, Length, Value);
157*0a6a1f1dSLionel Sambuc   for_template_constant_expression_test<double, 2, 4, 8>(List, Length);
158*0a6a1f1dSLionel Sambuc }
159*0a6a1f1dSLionel Sambuc 
160*0a6a1f1dSLionel Sambuc // CHECK: ![[LOOP_1]] = distinct !{![[LOOP_1]], ![[UNROLL_FULL:.*]], ![[WIDTH_4:.*]], ![[INTERLEAVE_4:.*]], ![[INTENABLE_1:.*]]}
161*0a6a1f1dSLionel Sambuc // CHECK: ![[UNROLL_FULL]] = !{!"llvm.loop.unroll.full"}
162*0a6a1f1dSLionel Sambuc // CHECK: ![[WIDTH_4]] = !{!"llvm.loop.vectorize.width", i32 4}
163*0a6a1f1dSLionel Sambuc // CHECK: ![[INTERLEAVE_4]] = !{!"llvm.loop.interleave.count", i32 4}
164*0a6a1f1dSLionel Sambuc // CHECK: ![[INTENABLE_1]] = !{!"llvm.loop.vectorize.enable", i1 true}
165*0a6a1f1dSLionel Sambuc // CHECK: ![[LOOP_2]] = distinct !{![[LOOP_2:.*]], ![[UNROLL_DISABLE:.*]], ![[INTERLEAVE_4:.*]], ![[WIDTH_8:.*]]}
166*0a6a1f1dSLionel Sambuc // CHECK: ![[UNROLL_DISABLE]] = !{!"llvm.loop.unroll.disable"}
167*0a6a1f1dSLionel Sambuc // CHECK: ![[WIDTH_8]] = !{!"llvm.loop.vectorize.width", i32 8}
168*0a6a1f1dSLionel Sambuc // CHECK: ![[LOOP_3]] = distinct !{![[LOOP_3]], ![[UNROLL_8:.*]], ![[INTERLEAVE_4:.*]], ![[ENABLE_1:.*]]}
169*0a6a1f1dSLionel Sambuc // CHECK: ![[UNROLL_8]] = !{!"llvm.loop.unroll.count", i32 8}
170*0a6a1f1dSLionel Sambuc // CHECK: ![[LOOP_4]] = distinct !{![[LOOP_4]], ![[INTERLEAVE_2:.*]], ![[WIDTH_2:.*]]}
171*0a6a1f1dSLionel Sambuc // CHECK: ![[INTERLEAVE_2]] = !{!"llvm.loop.interleave.count", i32 2}
172*0a6a1f1dSLionel Sambuc // CHECK: ![[WIDTH_2]] = !{!"llvm.loop.vectorize.width", i32 2}
173*0a6a1f1dSLionel Sambuc // CHECK: ![[LOOP_5]] = distinct !{![[LOOP_5]], ![[UNROLL_DISABLE:.*]], ![[WIDTH_1:.*]]}
174*0a6a1f1dSLionel Sambuc // CHECK: ![[WIDTH_1]] = !{!"llvm.loop.vectorize.width", i32 1}
175*0a6a1f1dSLionel Sambuc // CHECK: ![[LOOP_6]] = distinct !{![[LOOP_6]], ![[UNROLL_8:.*]], ![[INTERLEAVE_2:.*]], ![[WIDTH_2:.*]]}
176*0a6a1f1dSLionel Sambuc // CHECK: ![[LOOP_7]] = distinct !{![[LOOP_7]], ![[WIDTH_5:.*]]}
177*0a6a1f1dSLionel Sambuc // CHECK: ![[WIDTH_5]] = !{!"llvm.loop.vectorize.width", i32 5}
178*0a6a1f1dSLionel Sambuc // CHECK: ![[LOOP_8]] = distinct !{![[LOOP_8]], ![[WIDTH_5:.*]]}
179*0a6a1f1dSLionel Sambuc // CHECK: ![[LOOP_9]] = distinct !{![[LOOP_9]], ![[UNROLL_8:.*]], ![[INTERLEAVE_8:.*]], ![[WIDTH_8:.*]]}
180*0a6a1f1dSLionel Sambuc // CHECK: ![[INTERLEAVE_8]] = !{!"llvm.loop.interleave.count", i32 8}
181*0a6a1f1dSLionel Sambuc // CHECK: ![[LOOP_10]] = distinct !{![[LOOP_10]], ![[UNROLL_8:.*]], ![[INTERLEAVE_2:.*]], ![[WIDTH_2:.*]]}
182*0a6a1f1dSLionel Sambuc // CHECK: ![[LOOP_11]] = distinct !{![[LOOP_11]], ![[UNROLL_8:.*]], ![[INTERLEAVE_4:.*]], ![[WIDTH_2:.*]]}
183*0a6a1f1dSLionel Sambuc // CHECK: ![[LOOP_12]] = distinct !{![[LOOP_12]], ![[UNROLL_24:.*]], ![[INTERLEAVE_10:.*]], ![[WIDTH_6:.*]]}
184*0a6a1f1dSLionel Sambuc // CHECK: ![[UNROLL_24]] = !{!"llvm.loop.unroll.count", i32 24}
185*0a6a1f1dSLionel Sambuc // CHECK: ![[INTERLEAVE_10]] = !{!"llvm.loop.interleave.count", i32 10}
186*0a6a1f1dSLionel Sambuc // CHECK: ![[WIDTH_6]] = !{!"llvm.loop.vectorize.width", i32 6}
187*0a6a1f1dSLionel Sambuc // CHECK: ![[LOOP_13]] = distinct !{![[LOOP_13]], ![[UNROLL_32:.*]], ![[INTERLEAVE_16:.*]], ![[WIDTH_8:.*]]}
188*0a6a1f1dSLionel Sambuc // CHECK: ![[UNROLL_32]] = !{!"llvm.loop.unroll.count", i32 32}
189*0a6a1f1dSLionel Sambuc // CHECK: ![[INTERLEAVE_16]] = !{!"llvm.loop.interleave.count", i32 16}
190*0a6a1f1dSLionel Sambuc // CHECK: ![[LOOP_14]] = distinct !{![[LOOP_14]], ![[WIDTH_10:.*]]}
191*0a6a1f1dSLionel Sambuc // CHECK: ![[WIDTH_10]] = !{!"llvm.loop.vectorize.width", i32 10}
192