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