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.
4void 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 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.
20void 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
31enum struct Tuner : short { Interleave = 4, Unroll = 8 };
32
33// Verify for loop is recognized after sequence of pragma clang loop directives.
34void 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 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.
46void 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 label {{.*}}, !llvm.loop ![[LOOP_4:.*]]
52    List[i] = i;
53  }
54}
55
56// Verify disable pragma clang loop directive generates correct metadata
57void 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 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
70void 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 label {{.*}}, !llvm.loop ![[LOOP_6:.*]]
75    List[i] = i * Value;
76  }
77}
78
79// Verify constant expressions are handled correctly.
80void 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 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 label {{.*}}, !llvm.loop ![[LOOP_8:.*]]
90    List[i] += i;
91  }
92}
93
94// Verify metadata is generated when template is used.
95template <typename A>
96void 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 label {{.*}}, !llvm.loop ![[LOOP_9:.*]]
100    List[i] = i * Value;
101  }
102}
103
104// Verify define is resolved correctly when template is used.
105template <typename A, typename T>
106void 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 label {{.*}}, !llvm.loop ![[LOOP_10:.*]]
114    List[i] = i * Value;
115  }
116}
117
118// Verify templates and constant expressions are handled correctly.
119template <typename A, int V, int I, int U>
120void 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 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 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 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 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.
152void 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]], ![[WIDTH_4:.*]], ![[INTERLEAVE_4:.*]], ![[INTENABLE_1:.*]], ![[UNROLL_FULL:.*]]}
161// CHECK: ![[WIDTH_4]] = !{!"llvm.loop.vectorize.width", i32 4}
162// CHECK: ![[INTERLEAVE_4]] = !{!"llvm.loop.interleave.count", i32 4}
163// CHECK: ![[INTENABLE_1]] = !{!"llvm.loop.vectorize.enable", i1 true}
164// CHECK: ![[UNROLL_FULL]] = !{!"llvm.loop.unroll.full"}
165// CHECK: ![[LOOP_2]] = distinct !{![[LOOP_2:.*]], ![[WIDTH_8:.*]], ![[INTERLEAVE_4:.*]], ![[UNROLL_DISABLE:.*]]}
166// CHECK: ![[WIDTH_8]] = !{!"llvm.loop.vectorize.width", i32 8}
167// CHECK: ![[UNROLL_DISABLE]] = !{!"llvm.loop.unroll.disable"}
168// CHECK: ![[LOOP_3]] = distinct !{![[LOOP_3]], ![[INTERLEAVE_4:.*]], ![[UNROLL_8:.*]], ![[INTENABLE_1:.*]]}
169// CHECK: ![[UNROLL_8]] = !{!"llvm.loop.unroll.count", i32 8}
170// CHECK: ![[LOOP_4]] = distinct !{![[LOOP_4]], ![[WIDTH_2:.*]], ![[INTERLEAVE_2:.*]]}
171// CHECK: ![[WIDTH_2]] = !{!"llvm.loop.vectorize.width", i32 2}
172// CHECK: ![[INTERLEAVE_2]] = !{!"llvm.loop.interleave.count", i32 2}
173// CHECK: ![[LOOP_5]] = distinct !{![[LOOP_5]], ![[WIDTH_1:.*]], ![[UNROLL_DISABLE:.*]]}
174// CHECK: ![[WIDTH_1]] = !{!"llvm.loop.vectorize.width", i32 1}
175// CHECK: ![[LOOP_6]] = distinct !{![[LOOP_6]], ![[WIDTH_2:.*]], ![[INTERLEAVE_2:.*]], ![[UNROLL_8:.*]]}
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]], ![[WIDTH_8:.*]], ![[INTERLEAVE_8:.*]], ![[UNROLL_8:.*]]}
180// CHECK: ![[INTERLEAVE_8]] = !{!"llvm.loop.interleave.count", i32 8}
181// CHECK: ![[LOOP_10]] = distinct !{![[LOOP_10]], ![[WIDTH_2:.*]], ![[INTERLEAVE_2:.*]], ![[UNROLL_8:.*]]}
182// CHECK: ![[LOOP_11]] = distinct !{![[LOOP_11]], ![[WIDTH_2:.*]], ![[INTERLEAVE_4:.*]], ![[UNROLL_8:.*]]}
183// CHECK: ![[LOOP_12]] = distinct !{![[LOOP_12]], ![[WIDTH_6:.*]], ![[INTERLEAVE_10:.*]], ![[UNROLL_24:.*]]}
184// CHECK: ![[WIDTH_6]] = !{!"llvm.loop.vectorize.width", i32 6}
185// CHECK: ![[INTERLEAVE_10]] = !{!"llvm.loop.interleave.count", i32 10}
186// CHECK: ![[UNROLL_24]] = !{!"llvm.loop.unroll.count", i32 24}
187// CHECK: ![[LOOP_13]] = distinct !{![[LOOP_13]], ![[WIDTH_8:.*]], ![[INTERLEAVE_16:.*]], ![[UNROLL_32:.*]]}
188// CHECK: ![[INTERLEAVE_16]] = !{!"llvm.loop.interleave.count", i32 16}
189// CHECK: ![[UNROLL_32]] = !{!"llvm.loop.unroll.count", i32 32}
190// CHECK: ![[LOOP_14]] = distinct !{![[LOOP_14]], ![[WIDTH_10:.*]]}
191// CHECK: ![[WIDTH_10]] = !{!"llvm.loop.vectorize.width", i32 10}
192