1 // RUN: %clang_cc1 -verify -fopenmp -ast-print %s | FileCheck %s
2 // RUN: %clang_cc1 -fopenmp -x c++ -std=c++11 -emit-pch -o %t %s
3 // RUN: %clang_cc1 -fopenmp -std=c++11 -include-pch %t -fsyntax-only -verify %s -ast-print | FileCheck %s
4 // expected-no-diagnostics
5 
6 #ifndef HEADER
7 #define HEADER
8 
9 struct SS {
SSSS10   SS(): a(0) {}
SSSS11   SS(int v) : a(v) {}
12   int a;
13   typedef int type;
14 };
15 
16 template <typename T>
17 class S7 : public T {
18 protected:
19   T *a;
20   T b[2];
S7()21   S7() : a(0) {}
22 
23 public:
S7(typename T::type & v)24   S7(typename T::type &v) : a((T*)&v) {
25 #pragma omp simd aligned(a)
26     for (int k = 0; k < a->a; ++k)
27       ++this->a->a;
28   }
operator =(S7 & s)29   S7 &operator=(S7 &s) {
30 #pragma omp simd aligned(this->b : 8)
31     for (int k = 0; k < s.a->a; ++k)
32       ++s.a->a;
33     return *this;
34   }
35 };
36 
37 // CHECK: #pragma omp simd aligned(this->a)
38 // CHECK: #pragma omp simd aligned(this->a)
39 // CHECK: #pragma omp simd aligned(this->b: 8)
40 
41 class S8 : public S7<SS> {
S8()42   S8() {}
43 
44 public:
S8(int v)45   S8(int v) : S7<SS>(v){
46 #pragma omp simd aligned(S7<SS>::a)
47     for (int k = 0; k < a->a; ++k)
48       ++this->a->a;
49   }
operator =(S8 & s)50   S8 &operator=(S8 &s) {
51 #pragma omp simd aligned(this->b: 4)
52     for (int k = 0; k < s.a->a; ++k)
53       ++s.a->a;
54     return *this;
55   }
56 };
57 
58 // CHECK: #pragma omp simd aligned(this->S7<SS>::a)
59 // CHECK: #pragma omp simd aligned(this->b: 4)
60 
foo()61 void foo() {}
62 int g_ind = 1;
reduct(T * arr,N num)63 template<class T, class N> T reduct(T* arr, N num) {
64   N i;
65   N ind;
66   N myind;
67   N &ref = i;
68   T sum = (T)0;
69 // CHECK: T sum = (T)0;
70 #pragma omp simd private(myind, g_ind), linear(ind), aligned(arr), linear(uval(ref))
71 // CHECK-NEXT: #pragma omp simd private(myind,g_ind) linear(ind) aligned(arr) linear(uval(ref))
72   for (i = 0; i < num; ++i) {
73     myind = ind;
74     T cur = arr[myind];
75     ind += g_ind;
76     sum += cur;
77   }
78 }
79 
80 template<class T> struct S {
SS81   S(const T &a)
82     :m_a(a)
83   {}
resultS84   T result(T *v) const {
85     T res;
86     T val;
87     T lin = 0;
88     T &ref = res;
89 // CHECK: T res;
90 // CHECK: T val;
91 // CHECK: T lin = 0;
92 // CHECK: T &ref = res;
93     #pragma omp simd private(val)  safelen(7) linear(lin : -5) lastprivate(res) simdlen(5) linear(ref(ref))
94 // CHECK-NEXT: #pragma omp simd private(val) safelen(7) linear(lin: -5) lastprivate(res) simdlen(5) linear(ref(ref))
95     for (T i = 7; i < m_a; ++i) {
96       val = v[i-7] + m_a;
97       res = val;
98       lin -= 5;
99     }
100     const T clen = 3;
101 // CHECK: T clen = 3;
102     #pragma omp simd safelen(clen-1) simdlen(clen-1)
103 // CHECK-NEXT: #pragma omp simd safelen(clen - 1) simdlen(clen - 1)
104     for(T i = clen+2; i < 20; ++i) {
105 // CHECK-NEXT: for (T i = clen + 2; i < 20; ++i) {
106       v[i] = v[v-clen] + 1;
107 // CHECK-NEXT: v[i] = v[v - clen] + 1;
108     }
109 // CHECK-NEXT: }
110     return res;
111   }
~SS112   ~S()
113   {}
114   T m_a;
115 };
116 
117 template<int LEN> struct S2 {
funcS2118   static void func(int n, float *a, float *b, float *c) {
119     int k1 = 0, k2 = 0;
120 #pragma omp simd safelen(LEN) linear(k1,k2:LEN) aligned(a:LEN) simdlen(LEN)
121     for(int i = 0; i < n; i++) {
122       c[i] = a[i] + b[i];
123       c[k1] = a[k1] + b[k1];
124       c[k2] = a[k2] + b[k2];
125       k1 = k1 + LEN;
126       k2 = k2 + LEN;
127     }
128   }
129 };
130 
131 // S2<4>::func is called below in main.
132 // CHECK: template <int LEN = 4> struct S2 {
133 // CHECK-NEXT: static void func(int n, float *a, float *b, float *c)     {
134 // CHECK-NEXT:   int k1 = 0, k2 = 0;
135 // CHECK-NEXT: #pragma omp simd safelen(4) linear(k1,k2: 4) aligned(a: 4) simdlen(4)
136 // CHECK-NEXT:   for (int i = 0; i < n; i++) {
137 // CHECK-NEXT:     c[i] = a[i] + b[i];
138 // CHECK-NEXT:     c[k1] = a[k1] + b[k1];
139 // CHECK-NEXT:     c[k2] = a[k2] + b[k2];
140 // CHECK-NEXT:     k1 = k1 + 4;
141 // CHECK-NEXT:     k2 = k2 + 4;
142 // CHECK-NEXT:   }
143 // CHECK-NEXT: }
144 
main(int argc,char ** argv)145 int main (int argc, char **argv) {
146   int b = argc, c, d, e, f, g;
147   int k1=0,k2=0;
148   int &ref = b;
149   static int *a;
150 // CHECK: static int *a;
151 #pragma omp simd
152 // CHECK-NEXT: #pragma omp simd
153   for (int i=0; i < 2; ++i)*a=2;
154 // CHECK-NEXT: for (int i = 0; i < 2; ++i)
155 // CHECK-NEXT: *a = 2;
156 #pragma omp simd private(argc, b),lastprivate(d,f) collapse(2) aligned(a : 4)
157   for (int i = 0; i < 10; ++i)
158   for (int j = 0; j < 10; ++j) {foo(); k1 += 8; k2 += 8;}
159 // CHECK-NEXT: #pragma omp simd private(argc,b) lastprivate(d,f) collapse(2) aligned(a: 4)
160 // CHECK-NEXT: for (int i = 0; i < 10; ++i)
161 // CHECK-NEXT: for (int j = 0; j < 10; ++j) {
162 // CHECK-NEXT: foo();
163 // CHECK-NEXT: k1 += 8;
164 // CHECK-NEXT: k2 += 8;
165 // CHECK-NEXT: }
166   for (int i = 0; i < 10; ++i)foo();
167 // CHECK-NEXT: for (int i = 0; i < 10; ++i)
168 // CHECK-NEXT: foo();
169   const int CLEN = 4;
170 // CHECK-NEXT: const int CLEN = 4;
171   #pragma omp simd aligned(a:CLEN) linear(a:CLEN) safelen(CLEN) collapse( 1 ) simdlen(CLEN) linear(val(ref): CLEN)
172 // CHECK-NEXT: #pragma omp simd aligned(a: CLEN) linear(a: CLEN) safelen(CLEN) collapse(1) simdlen(CLEN) linear(val(ref): CLEN)
173   for (int i = 0; i < 10; ++i)foo();
174 // CHECK-NEXT: for (int i = 0; i < 10; ++i)
175 // CHECK-NEXT: foo();
176 
177   float arr[16];
178   S2<4>::func(0,arr,arr,arr);
179   return (0);
180 }
181 
182 #endif
183