1 // RUN: %clang_cc1 -verify -fopenmp -x c++ -triple x86_64-unknown-unknown -emit-llvm %s -fexceptions -fcxx-exceptions -o - | FileCheck %s
2 // RUN: %clang_cc1 -fopenmp -x c++ -std=c++11 -triple x86_64-unknown-unknown -fexceptions -fcxx-exceptions -emit-pch -o %t %s
3 // RUN: %clang_cc1 -fopenmp -x c++ -triple x86_64-unknown-unknown -fexceptions -fcxx-exceptions -debug-info-kind=limited -std=c++11 -include-pch %t -verify %s -emit-llvm -o - | FileCheck %s
4 // RUN: %clang_cc1 -verify -triple x86_64-apple-darwin10 -fopenmp -fexceptions -fcxx-exceptions -debug-info-kind=line-tables-only -x c++ -emit-llvm %s -o - | FileCheck %s --check-prefix=TERM_DEBUG
5 // expected-no-diagnostics
6 #ifndef HEADER
7 #define HEADER
8 
9 long long get_val() { return 0; }
10 double *g_ptr;
11 
12 // CHECK-LABEL: define {{.*void}} @{{.*}}simple{{.*}}(float* {{.+}}, float* {{.+}}, float* {{.+}}, float* {{.+}})
13 void simple(float *a, float *b, float *c, float *d) {
14   // CHECK: store i32 3, i32* %
15   // CHECK: icmp slt i32 %{{.+}}, 32
16   // CHECK: fmul float
17   // CHECK: fmul float
18   // CHECK: add nsw i32 %{{.+}}, 5
19   #pragma omp target parallel for device((int)*a)
20   for (int i = 3; i < 32; i += 5) {
21     a[i] = b[i] * c[i] * d[i];
22   }
23 
24   // CHECK: call i{{.+}} @{{.+}}get_val{{.+}}()
25   // CHECK: store i32 10, i32* %
26   // CHECK: icmp sgt i32 %{{.+}}, 1
27   // CHECK: fadd float %{{.+}}, 1.000000e+00
28   // CHECK: add nsw {{.+}} %{{.+}}, 3
29   // CHECK: add nsw i32 %{{.+}}, -1
30   long long k = get_val();
31   #pragma omp target parallel for linear(k : 3) schedule(dynamic)
32   for (int i = 10; i > 1; i--) {
33     a[k]++;
34     k = k + 3;
35   }
36 
37   // CHECK: store i32 12, i32* %
38   // CHECK: store i{{.+}} 2000, i{{.+}}* %
39   // CHECK: icmp uge i{{.+}} %{{.+}}, 600
40   // CHECK: store double 0.000000e+00,
41   // CHECK: fadd float %{{.+}}, 1.000000e+00
42   // CHECK: sub i{{.+}} %{{.+}}, 400
43   int lin = 12;
44   #pragma omp target parallel for linear(lin : get_val()), linear(g_ptr)
45   for (unsigned long long it = 2000; it >= 600; it-=400) {
46     *g_ptr++ = 0.0;
47     a[it + lin]++;
48   }
49 
50   // CHECK: store i{{.+}} 6, i{{.+}}* %
51   // CHECK: icmp sle i{{.+}} %{{.+}}, 20
52   // CHECK: sub nsw i{{.+}} %{{.+}}, -4
53   #pragma omp target parallel for
54   for (short it = 6; it <= 20; it-=-4) {
55   }
56 
57   // CHECK: store i8 122, i8* %
58   // CHECK: icmp sge i32 %{{.+}}, 97
59   // CHECK: add nsw i32 %{{.+}}, -1
60   #pragma omp target parallel for
61   for (unsigned char it = 'z'; it >= 'a'; it+=-1) {
62   }
63 
64   // CHECK: store i32 100, i32* %
65   // CHECK: icmp ult i32 %{{.+}}, 10
66   // CHECK: add i32 %{{.+}}, 10
67   #pragma omp target parallel for
68   for (unsigned i=100; i<10; i+=10) {
69   }
70 
71   int A;
72   {
73   A = -1;
74   // CHECK: store i{{.+}} -10, i{{.+}}* %
75   // CHECK: icmp slt i{{.+}} %{{.+}}, 10
76   // CHECK: add nsw i{{.+}} %{{.+}}, 3
77   #pragma omp target parallel for lastprivate(A)
78   for (long long i = -10; i < 10; i += 3) {
79     A = i;
80   }
81   }
82   int R;
83   {
84   R = -1;
85   // CHECK: store i{{.+}} -10, i{{.+}}* %
86   // CHECK: icmp slt i{{.+}} %{{.+}}, 10
87   // CHECK: add nsw i{{.+}} %{{.+}}, 3
88   #pragma omp target parallel for reduction(*:R)
89   for (long long i = -10; i < 10; i += 3) {
90     R *= i;
91   }
92   }
93 }
94 
95 template <class T, unsigned K> T tfoo(T a) { return a + K; }
96 
97 template <typename T, unsigned N>
98 int templ1(T a, T *z) {
99   #pragma omp target parallel for collapse(N)
100   for (int i = 0; i < N * 2; i++) {
101     for (long long j = 0; j < (N + N + N + N); j += 2) {
102       z[i + j] = a + tfoo<T, N>(i + j);
103     }
104   }
105   return 0;
106 }
107 
108 // Instatiation templ1<float,2>
109 // CHECK-LABEL: define {{.*i32}} @{{.*}}templ1{{.*}}(float {{.+}}, float* {{.+}})
110 void inst_templ1() {
111   float a;
112   float z[100];
113   templ1<float,2> (a, z);
114 }
115 
116 
117 typedef int MyIdx;
118 
119 class IterDouble {
120   double *Ptr;
121 public:
122   IterDouble operator++ () const {
123     IterDouble n;
124     n.Ptr = Ptr + 1;
125     return n;
126   }
127   bool operator < (const IterDouble &that) const {
128     return Ptr < that.Ptr;
129   }
130   double & operator *() const {
131     return *Ptr;
132   }
133   MyIdx operator - (const IterDouble &that) const {
134     return (MyIdx) (Ptr - that.Ptr);
135   }
136   IterDouble operator + (int Delta) {
137     IterDouble re;
138     re.Ptr = Ptr + Delta;
139     return re;
140   }
141 
142   ///~IterDouble() {}
143 };
144 
145 // CHECK-LABEL: define {{.*void}} @{{.*}}iter_simple{{.*}}
146 void iter_simple(IterDouble ia, IterDouble ib, IterDouble ic) {
147 //
148 // Calculate number of iterations before the loop body.
149 // CHECK: invoke {{.*}}i32 @{{.*}}IterDouble{{.*}}
150   #pragma omp target parallel for
151   for (IterDouble i = ia; i < ib; ++i) {
152 // Call of operator+ (i, IV).
153 // CHECK: {{%.+}} = invoke {{.+}} @{{.*}}IterDouble{{.*}}
154 // ... loop body ...
155    *i = *ic * 0.5;
156 // Float multiply and save result.
157 // CHECK: [[MULR:%.+]] = fmul double {{%.+}}, 5.000000e-01
158 // CHECK-NEXT: invoke {{.+}} @{{.*}}IterDouble{{.*}}
159 // CHECK: store double [[MULR:%.+]], double* [[RESULT_ADDR:%.+]]
160    ++ic;
161   }
162 // CHECK: ret void
163 }
164 
165 
166 // CHECK-LABEL: define {{.*void}} @{{.*}}collapsed{{.*}}
167 void collapsed(float *a, float *b, float *c, float *d) {
168   int i; // outer loop counter
169   unsigned j; // middle loop couter, leads to unsigned icmp in loop header.
170   // k declared in the loop init below
171   short l; // inner loop counter
172 //
173   #pragma omp target parallel for collapse(4)
174   for (i = 1; i < 3; i++) // 2 iterations
175     for (j = 2u; j < 5u; j++) //3 iterations
176       for (int k = 3; k <= 6; k++) // 4 iterations
177         for (l = 4; l < 9; ++l) // 5 iterations
178         {
179 // ... loop body ...
180 // End of body: store into a[i]:
181 // CHECK: store float [[RESULT:%.+]], float* [[RESULT_ADDR:%.+]]
182     float res = b[j] * c[k];
183     a[i] = res * d[l];
184   }
185 // CHECK: ret void
186 }
187 
188 extern char foo();
189 extern double globalfloat;
190 
191 // CHECK-LABEL: define {{.*void}} @{{.*}}widened{{.*}}
192 void widened(float *a, float *b, float *c, float *d) {
193   int i; // outer loop counter
194   short j; // inner loop counter
195   globalfloat = 1.0;
196   int localint = 1;
197 // CHECK: store double {{.+}}, double* [[GLOBALFLOAT:@.+]],
198   #pragma omp target parallel for collapse(2) private(globalfloat, localint)
199   for (i = 1; i < 3; i++) // 2 iterations
200     for (j = 0; j < foo(); j++) // foo() iterations
201   {
202 // ... loop body ...
203 //
204 // Here we expect store into private double var, not global
205 // CHECK: store double {{.+}}, double* [[GLOBALFLOAT]]
206     globalfloat = (float)j/i;
207     float res = b[j] * c[j];
208 // Store into a[i]:
209 // CHECK: store float [[RESULT:%.+]], float* [[RESULT_ADDR:%.+]]
210     a[i] = res * d[i];
211 // Then there's a store into private var localint:
212 // CHECK: store i32 {{.+}}, i32* [[LOCALINT:%[^,]+]]
213     localint = (int)j;
214   }
215 //
216 // Here we expect store into original localint, not its privatized version.
217 // CHECK: store i32 {{.+}}, i32* [[LOCALINT]]
218   localint = (int)j;
219 // CHECK: ret void
220 }
221 
222 // TERM_DEBUG-LABEL: bar
223 int bar() {return 0;};
224 
225 // TERM_DEBUG-LABEL: parallel_simd
226 void parallel_simd(float *a) {
227 #pragma omp target parallel for
228   for (unsigned i = 131071; i <= 2147483647; i += 127)
229     a[i] += bar();
230 }
231 #endif // HEADER
232 
233