1// RUN: %clang_cc1 -verify -fopenmp -x c++ -triple x86_64-unknown-unknown -emit-llvm %s -o - | FileCheck %s
2// RUN: %clang_cc1 -fopenmp -x c++ -std=c++11 -triple x86_64-unknown-unknown -emit-pch -o %t %s
3// RUN: %clang_cc1 -fopenmp -x c++ -triple x86_64-unknown-unknown -std=c++11 -include-pch %t -verify %s -emit-llvm -o - | FileCheck %s
4// RUN: %clang_cc1 -verify -fopenmp -x c++ -std=c++11 -DLAMBDA -triple %itanium_abi_triple -emit-llvm %s -o - | FileCheck -check-prefix=LAMBDA %s
5// RUN: %clang_cc1 -verify -fopenmp -x c++ -fblocks -DBLOCKS -triple %itanium_abi_triple -emit-llvm %s -o - | FileCheck -check-prefix=BLOCKS %s
6// expected-no-diagnostics
7// REQUIRES: x86-registered-target
8#ifndef HEADER
9#define HEADER
10template <class T>
11struct S {
12  T f;
13  S(T a) : f(a) {}
14  S() : f() {}
15  operator T() { return T(); }
16  ~S() {}
17};
18
19volatile int g __attribute__((aligned(128))) = 1212;
20
21// CHECK: [[S_FLOAT_TY:%.+]] = type { float }
22// CHECK: [[S_INT_TY:%.+]] = type { i{{[0-9]+}} }
23template <typename T>
24T tmain() {
25  S<T> test;
26  T t_var __attribute__((aligned(128))) = T();
27  T vec[] __attribute__((aligned(128))) = {1, 2};
28  S<T> s_arr[] __attribute__((aligned(128))) = {1, 2};
29  S<T> var __attribute__((aligned(128))) (3);
30#pragma omp parallel private(t_var, vec, s_arr, var)
31  {
32    vec[0] = t_var;
33    s_arr[0] = var;
34  }
35  return T();
36}
37
38int main() {
39  static int sivar;
40#ifdef LAMBDA
41  // LAMBDA: [[G:@.+]] = global i{{[0-9]+}} 1212,
42  // LAMBDA-LABEL: @main
43  // LAMBDA: call{{.*}} void [[OUTER_LAMBDA:@.+]](
44  [&]() {
45  // LAMBDA: define{{.*}} internal{{.*}} void [[OUTER_LAMBDA]](
46  // LAMBDA-NOT: = getelementptr inbounds %{{.+}},
47  // LAMBDA: call{{.*}} void {{.+}} @__kmpc_fork_call({{.+}}, i32 0, {{.+}}* [[OMP_REGION:@.+]] to {{.+}})
48#pragma omp parallel private(g, sivar)
49  {
50    // LAMBDA: define{{.*}} internal{{.*}} void [[OMP_REGION]](i32* noalias %{{.+}}, i32* noalias %{{.+}})
51    // LAMBDA: [[G_PRIVATE_ADDR:%.+]] = alloca i{{[0-9]+}},
52    // LAMBDA: [[SIVAR_PRIVATE_ADDR:%.+]] = alloca i{{[0-9]+}},
53    g = 1;
54    sivar = 2;
55    // LAMBDA: store i{{[0-9]+}} 1, i{{[0-9]+}}* [[G_PRIVATE_ADDR]],
56    // LAMBDA: store i{{[0-9]+}} 2, i{{[0-9]+}}* [[SIVAR_PRIVATE_ADDR]],
57    // LAMBDA: [[G_PRIVATE_ADDR_REF:%.+]] = getelementptr inbounds %{{.+}}, %{{.+}}* [[ARG:%.+]], i{{[0-9]+}} 0, i{{[0-9]+}} 0
58    // LAMBDA: store i{{[0-9]+}}* [[G_PRIVATE_ADDR]], i{{[0-9]+}}** [[G_PRIVATE_ADDR_REF]]
59
60    // LAMBDA: [[SIVAR_PRIVATE_ADDR_REF:%.+]] = getelementptr inbounds %{{.+}}, %{{.+}}* [[ARG:%.+]], i{{[0-9]+}} 0, i{{[0-9]+}} 1
61    // LAMBDA: store i{{[0-9]+}}* [[SIVAR_PRIVATE_ADDR]], i{{[0-9]+}}** [[SIVAR_PRIVATE_ADDR_REF]]
62
63    // LAMBDA: call{{.*}} void [[INNER_LAMBDA:@.+]](%{{.+}}* [[ARG]])
64    [&]() {
65      // LAMBDA: define {{.+}} void [[INNER_LAMBDA]](%{{.+}}* [[ARG_PTR:%.+]])
66      // LAMBDA: store %{{.+}}* [[ARG_PTR]], %{{.+}}** [[ARG_PTR_REF:%.+]],
67      g = 2;
68      sivar = 4;
69      // LAMBDA: [[ARG_PTR:%.+]] = load %{{.+}}*, %{{.+}}** [[ARG_PTR_REF]]
70      // LAMBDA: [[G_PTR_REF:%.+]] = getelementptr inbounds %{{.+}}, %{{.+}}* [[ARG_PTR]], i{{[0-9]+}} 0, i{{[0-9]+}} 0
71      // LAMBDA: [[G_REF:%.+]] = load i{{[0-9]+}}*, i{{[0-9]+}}** [[G_PTR_REF]]
72      // LAMBDA: store i{{[0-9]+}} 2, i{{[0-9]+}}* [[G_REF]]
73      // LAMBDA: [[SIVAR_PTR_REF:%.+]] = getelementptr inbounds %{{.+}}, %{{.+}}* [[ARG_PTR]], i{{[0-9]+}} 0, i{{[0-9]+}} 1
74      // LAMBDA: [[SIVAR_REF:%.+]] = load i{{[0-9]+}}*, i{{[0-9]+}}** [[SIVAR_PTR_REF]]
75      // LAMBDA: store i{{[0-9]+}} 4, i{{[0-9]+}}* [[SIVAR_REF]]
76    }();
77  }
78  }();
79  return 0;
80#elif defined(BLOCKS)
81  // BLOCKS: [[G:@.+]] = global i{{[0-9]+}} 1212,
82  // BLOCKS-LABEL: @main
83  // BLOCKS: call{{.*}} void {{%.+}}(i8
84  ^{
85  // BLOCKS: define{{.*}} internal{{.*}} void {{.+}}(i8*
86  // BLOCKS-NOT: = getelementptr inbounds %{{.+}},
87  // BLOCKS: call{{.*}} void {{.+}} @__kmpc_fork_call({{.+}}, i32 0, {{.+}}* [[OMP_REGION:@.+]] to {{.+}})
88#pragma omp parallel private(g, sivar)
89  {
90    // BLOCKS: define{{.*}} internal{{.*}} void [[OMP_REGION]](i32* noalias %{{.+}}, i32* noalias %{{.+}})
91    // BLOCKS: [[G_PRIVATE_ADDR:%.+]] = alloca i{{[0-9]+}},
92    // BLOCKS: [[SIVAR_PRIVATE_ADDR:%.+]] = alloca i{{[0-9]+}},
93    g = 1;
94    sivar = 20;
95    // BLOCKS: store i{{[0-9]+}} 1, i{{[0-9]+}}* [[G_PRIVATE_ADDR]],
96    // BLOCKS: store i{{[0-9]+}} 20, i{{[0-9]+}}* [[SIVAR_PRIVATE_ADDR]],
97    // BLOCKS-NOT: [[G]]{{[[^:word:]]}}
98    // BLOCKS: i{{[0-9]+}}* [[G_PRIVATE_ADDR]]
99    // BLOCKS-NOT: [[G]]{{[[^:word:]]}}
100    // BLOCKS-NOT: [[SIVAR]]{{[[^:word:]]}}
101    // BLOCKS: i{{[0-9]+}}* [[SIVAR_PRIVATE_ADDR]]
102    // BLOCKS-NOT: [[SIVAR]]{{[[^:word:]]}}
103    // BLOCKS: call{{.*}} void {{%.+}}(i8
104    ^{
105      // BLOCKS: define {{.+}} void {{@.+}}(i8*
106      g = 2;
107      sivar = 40;
108      // BLOCKS-NOT: [[G]]{{[[^:word:]]}}
109      // BLOCKS: store i{{[0-9]+}} 2, i{{[0-9]+}}*
110      // BLOCKS-NOT: [[G]]{{[[^:word:]]}}
111      // BLOCKS-NOT: [[SIVAR]]{{[[^:word:]]}}
112      // BLOCKS: store i{{[0-9]+}} 40, i{{[0-9]+}}*
113      // BLOCKS-NOT: [[SIVAR]]{{[[^:word:]]}}
114      // BLOCKS: ret
115    }();
116  }
117  }();
118  return 0;
119#else
120  S<float> test;
121  int t_var = 0;
122  int vec[] = {1, 2};
123  S<float> s_arr[] = {1, 2};
124  S<float> var(3);
125#pragma omp parallel private(t_var, vec, s_arr, var, sivar)
126  {
127    vec[0] = t_var;
128    s_arr[0] = var;
129    sivar = 3;
130  }
131  return tmain<int>();
132#endif
133}
134
135// CHECK: define i{{[0-9]+}} @main()
136// CHECK: [[TEST:%.+]] = alloca [[S_FLOAT_TY]],
137// CHECK: call {{.*}} [[S_FLOAT_TY_DEF_CONSTR:@.+]]([[S_FLOAT_TY]]* [[TEST]])
138// CHECK: call void (%{{.+}}*, i{{[0-9]+}}, void (i{{[0-9]+}}*, i{{[0-9]+}}*, ...)*, ...) @__kmpc_fork_call(%{{.+}}* @{{.+}}, i{{[0-9]+}} 0, void (i{{[0-9]+}}*, i{{[0-9]+}}*, ...)* bitcast (void (i{{[0-9]+}}*, i{{[0-9]+}}*)* [[MAIN_MICROTASK:@.+]] to void
139// CHECK: = call i{{.+}} [[TMAIN_INT:@.+]]()
140// CHECK: call void [[S_FLOAT_TY_DESTR:@.+]]([[S_FLOAT_TY]]*
141// CHECK: ret
142//
143// CHECK: define internal void [[MAIN_MICROTASK]](i{{[0-9]+}}* noalias [[GTID_ADDR:%.+]], i{{[0-9]+}}* noalias %{{.+}})
144// CHECK: [[T_VAR_PRIV:%.+]] = alloca i{{[0-9]+}},
145// CHECK: [[VEC_PRIV:%.+]] = alloca [2 x i{{[0-9]+}}],
146// CHECK: [[S_ARR_PRIV:%.+]] = alloca [2 x [[S_FLOAT_TY]]],
147// CHECK: [[VAR_PRIV:%.+]] = alloca [[S_FLOAT_TY]],
148// CHECK: [[SIVAR_PRIV:%.+]] = alloca i{{[0-9]+}},
149// CHECK: store i{{[0-9]+}}* [[GTID_ADDR]], i{{[0-9]+}}** [[GTID_ADDR_REF:%.+]]
150// CHECK-NOT: [[T_VAR_PRIV]]
151// CHECK-NOT: [[VEC_PRIV]]
152// CHECK: {{.+}}:
153// CHECK: [[S_ARR_PRIV_ITEM:%.+]] = phi [[S_FLOAT_TY]]*
154// CHECK: call {{.*}} [[S_FLOAT_TY_DEF_CONSTR]]([[S_FLOAT_TY]]* [[S_ARR_PRIV_ITEM]])
155// CHECK-NOT: [[T_VAR_PRIV]]
156// CHECK-NOT: [[VEC_PRIV]]
157// CHECK: call {{.*}} [[S_FLOAT_TY_DEF_CONSTR]]([[S_FLOAT_TY]]* [[VAR_PRIV]])
158// CHECK-DAG: call void [[S_FLOAT_TY_DESTR]]([[S_FLOAT_TY]]* [[VAR_PRIV]])
159// CHECK-DAG: call void [[S_FLOAT_TY_DESTR]]([[S_FLOAT_TY]]*
160// CHECK: ret void
161
162// CHECK: define {{.*}} i{{[0-9]+}} [[TMAIN_INT]]()
163// CHECK: [[TEST:%.+]] = alloca [[S_INT_TY]],
164// CHECK: call {{.*}} [[S_INT_TY_DEF_CONSTR:@.+]]([[S_INT_TY]]* [[TEST]])
165// CHECK: call void (%{{.+}}*, i{{[0-9]+}}, void (i{{[0-9]+}}*, i{{[0-9]+}}*, ...)*, ...) @__kmpc_fork_call(%{{.+}}* @{{.+}}, i{{[0-9]+}} 0, void (i{{[0-9]+}}*, i{{[0-9]+}}*, ...)* bitcast (void (i{{[0-9]+}}*, i{{[0-9]+}}*)* [[TMAIN_MICROTASK:@.+]] to void
166// CHECK: call void [[S_INT_TY_DESTR:@.+]]([[S_INT_TY]]*
167// CHECK: ret
168//
169// CHECK: define internal void [[TMAIN_MICROTASK]](i{{[0-9]+}}* noalias [[GTID_ADDR:%.+]], i{{[0-9]+}}* noalias %{{.+}})
170// CHECK: [[T_VAR_PRIV:%.+]] = alloca i{{[0-9]+}}, align 128
171// CHECK: [[VEC_PRIV:%.+]] = alloca [2 x i{{[0-9]+}}], align 128
172// CHECK: [[S_ARR_PRIV:%.+]] = alloca [2 x [[S_INT_TY]]], align 128
173// CHECK: [[VAR_PRIV:%.+]] = alloca [[S_INT_TY]], align 128
174// CHECK: store i{{[0-9]+}}* [[GTID_ADDR]], i{{[0-9]+}}** [[GTID_ADDR_REF:%.+]]
175// CHECK-NOT: [[T_VAR_PRIV]]
176// CHECK-NOT: [[VEC_PRIV]]
177// CHECK-NOT: [[SIVAR_PRIV]]
178// CHECK: {{.+}}:
179// CHECK: [[S_ARR_PRIV_ITEM:%.+]] = phi [[S_INT_TY]]*
180// CHECK: call {{.*}} [[S_INT_TY_DEF_CONSTR]]([[S_INT_TY]]* [[S_ARR_PRIV_ITEM]])
181// CHECK-NOT: [[T_VAR_PRIV]]
182// CHECK-NOT: [[VEC_PRIV]]
183// CHECK: call {{.*}} [[S_INT_TY_DEF_CONSTR]]([[S_INT_TY]]* [[VAR_PRIV]])
184// CHECK-DAG: call void [[S_INT_TY_DESTR]]([[S_INT_TY]]* [[VAR_PRIV]])
185// CHECK-DAG: call void [[S_INT_TY_DESTR]]([[S_INT_TY]]*
186// CHECK: ret void
187#endif
188
189