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.
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.
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.
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.
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
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
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.
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>
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>
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>
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.
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 // CHECK: ![[LOOP_1]] = distinct !{![[LOOP_1]], ![[WIDTH_4:.*]], ![[INTERLEAVE_4:.*]], ![[INTENABLE_1:.*]], ![[UNROLL_FULL:.*]], ![[DISTRIBUTE_ENABLE:.*]]}
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: ![[UNROLL_FULL]] = !{!"llvm.loop.unroll.full"}
166 // CHECK: ![[DISTRIBUTE_ENABLE]] = !{!"llvm.loop.distribute.enable", i1 true}
167 // CHECK: ![[LOOP_2]] = distinct !{![[LOOP_2:.*]], ![[WIDTH_8:.*]], ![[INTERLEAVE_4:.*]], ![[UNROLL_DISABLE:.*]], ![[DISTRIBUTE_DISABLE:.*]]}
168 // CHECK: ![[WIDTH_8]] = !{!"llvm.loop.vectorize.width", i32 8}
169 // CHECK: ![[UNROLL_DISABLE]] = !{!"llvm.loop.unroll.disable"}
170 // CHECK: ![[DISTRIBUTE_DISABLE]] = !{!"llvm.loop.distribute.enable", i1 false}
171 // CHECK: ![[LOOP_3]] = distinct !{![[LOOP_3]], ![[INTERLEAVE_4:.*]], ![[UNROLL_8:.*]], ![[INTENABLE_1:.*]]}
172 // CHECK: ![[UNROLL_8]] = !{!"llvm.loop.unroll.count", i32 8}
173 // CHECK: ![[LOOP_4]] = distinct !{![[LOOP_4]], ![[WIDTH_2:.*]], ![[INTERLEAVE_2:.*]]}
174 // CHECK: ![[WIDTH_2]] = !{!"llvm.loop.vectorize.width", i32 2}
175 // CHECK: ![[INTERLEAVE_2]] = !{!"llvm.loop.interleave.count", i32 2}
176 // CHECK: ![[LOOP_5]] = distinct !{![[LOOP_5]], ![[WIDTH_1:.*]], ![[UNROLL_DISABLE:.*]], ![[DISTRIBUTE_DISABLE:.*]]}
177 // CHECK: ![[WIDTH_1]] = !{!"llvm.loop.vectorize.width", i32 1}
178 // CHECK: ![[LOOP_6]] = distinct !{![[LOOP_6]], ![[WIDTH_2:.*]], ![[INTERLEAVE_2:.*]], ![[UNROLL_8:.*]]}
179 // CHECK: ![[LOOP_7]] = distinct !{![[LOOP_7]], ![[WIDTH_5:.*]]}
180 // CHECK: ![[WIDTH_5]] = !{!"llvm.loop.vectorize.width", i32 5}
181 // CHECK: ![[LOOP_8]] = distinct !{![[LOOP_8]], ![[WIDTH_5:.*]]}
182 // CHECK: ![[LOOP_9]] = distinct !{![[LOOP_9]], ![[WIDTH_8:.*]], ![[INTERLEAVE_8:.*]], ![[UNROLL_8:.*]]}
183 // CHECK: ![[INTERLEAVE_8]] = !{!"llvm.loop.interleave.count", i32 8}
184 // CHECK: ![[LOOP_10]] = distinct !{![[LOOP_10]], ![[WIDTH_2:.*]], ![[INTERLEAVE_2:.*]], ![[UNROLL_8:.*]]}
185 // CHECK: ![[LOOP_11]] = distinct !{![[LOOP_11]], ![[WIDTH_2:.*]], ![[INTERLEAVE_4:.*]], ![[UNROLL_8:.*]]}
186 // CHECK: ![[LOOP_12]] = distinct !{![[LOOP_12]], ![[WIDTH_6:.*]], ![[INTERLEAVE_10:.*]], ![[UNROLL_24:.*]]}
187 // CHECK: ![[WIDTH_6]] = !{!"llvm.loop.vectorize.width", i32 6}
188 // CHECK: ![[INTERLEAVE_10]] = !{!"llvm.loop.interleave.count", i32 10}
189 // CHECK: ![[UNROLL_24]] = !{!"llvm.loop.unroll.count", i32 24}
190 // CHECK: ![[LOOP_13]] = distinct !{![[LOOP_13]], ![[WIDTH_8:.*]], ![[INTERLEAVE_16:.*]], ![[UNROLL_32:.*]]}
191 // CHECK: ![[INTERLEAVE_16]] = !{!"llvm.loop.interleave.count", i32 16}
192 // CHECK: ![[UNROLL_32]] = !{!"llvm.loop.unroll.count", i32 32}
193 // CHECK: ![[LOOP_14]] = distinct !{![[LOOP_14]], ![[WIDTH_10:.*]]}
194 // CHECK: ![[WIDTH_10]] = !{!"llvm.loop.vectorize.width", i32 10}
195