1 // RUN: %clang_cc1 -verify -fopenmp -x c++ -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 -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 
6 // RUN: %clang_cc1 -verify -fopenmp-simd -x c++ -emit-llvm %s -fexceptions -fcxx-exceptions -o - | FileCheck --check-prefix SIMD-ONLY0 %s
7 // RUN: %clang_cc1 -fopenmp-simd -x c++ -std=c++11 -triple x86_64-unknown-unknown -fexceptions -fcxx-exceptions -emit-pch -o %t %s
8 // RUN: %clang_cc1 -fopenmp-simd -x c++ -triple x86_64-unknown-unknown -fexceptions -fcxx-exceptions -std=c++11 -include-pch %t -verify %s -emit-llvm -o - | FileCheck --check-prefix SIMD-ONLY0 %s
9 // RUN: %clang_cc1 -verify -triple x86_64-apple-darwin10 -fopenmp-simd -fexceptions -fcxx-exceptions -debug-info-kind=line-tables-only -x c++ -emit-llvm %s -o - | FileCheck --check-prefix SIMD-ONLY0 %s
10 // SIMD-ONLY0-NOT: {{__kmpc|__tgt}}
11 // expected-no-diagnostics
12 #ifndef HEADER
13 #define HEADER
14 
15 // CHECK:       [[IDENT_T_TY:%.+]] = type { i32, i32, i32, i32, i8* }
16 // CHECK:       [[UNNAMED_LOCK:@.+]] = common global [8 x i32] zeroinitializer
17 // CHECK:       [[THE_NAME_LOCK:@.+]] = common global [8 x i32] zeroinitializer
18 // CHECK:       [[THE_NAME_LOCK1:@.+]] = common global [8 x i32] zeroinitializer
19 
20 // CHECK:       define {{.*}}void [[FOO:@.+]]()
21 
foo()22 void foo() {}
23 
24 // CHECK-LABEL: @main
25 // TERM_DEBUG-LABEL: @main
main()26 int main() {
27 // CHECK:       [[A_ADDR:%.+]] = alloca i8
28   char a;
29 
30 // CHECK:       [[GTID:%.+]] = call {{.*}}i32 @__kmpc_global_thread_num([[IDENT_T_TY]]* [[DEFAULT_LOC:@.+]])
31 // CHECK:       call {{.*}}void @__kmpc_critical([[IDENT_T_TY]]* [[DEFAULT_LOC]], i32 [[GTID]], [8 x i32]* [[UNNAMED_LOCK]])
32 // CHECK-NEXT:  store i8 2, i8* [[A_ADDR]]
33 // CHECK-NEXT:  call {{.*}}void @__kmpc_end_critical([[IDENT_T_TY]]* [[DEFAULT_LOC]], i32 [[GTID]], [8 x i32]* [[UNNAMED_LOCK]])
34 #pragma omp critical
35   a = 2;
36 // CHECK:       call {{.*}}void @__kmpc_critical([[IDENT_T_TY]]* [[DEFAULT_LOC]], i32 [[GTID]], [8 x i32]* [[THE_NAME_LOCK]])
37 // CHECK-NEXT:  invoke {{.*}}void [[FOO]]()
38 // CHECK:       call {{.*}}void @__kmpc_end_critical([[IDENT_T_TY]]* [[DEFAULT_LOC]], i32 [[GTID]], [8 x i32]* [[THE_NAME_LOCK]])
39 #pragma omp critical(the_name)
40   foo();
41 // CHECK:       call {{.*}}void @__kmpc_critical_with_hint([[IDENT_T_TY]]* [[DEFAULT_LOC]], i32 [[GTID]], [8 x i32]* [[THE_NAME_LOCK1]], i{{64|32}} 23)
42 // CHECK-NEXT:  invoke {{.*}}void [[FOO]]()
43 // CHECK:       call {{.*}}void @__kmpc_end_critical([[IDENT_T_TY]]* [[DEFAULT_LOC]], i32 [[GTID]], [8 x i32]* [[THE_NAME_LOCK1]])
44 #pragma omp critical(the_name1) hint(23)
45   foo();
46 // CHECK:       call {{.*}}void @__kmpc_critical([[IDENT_T_TY]]* [[DEFAULT_LOC]], i32 [[GTID]], [8 x i32]* [[THE_NAME_LOCK]])
47 // CHECK:       br label
48 // CHECK-NOT:   call {{.*}}void @__kmpc_end_critical(
49 // CHECK:       br label
50 // CHECK-NOT:   call {{.*}}void @__kmpc_end_critical(
51 // CHECK:       br label
52   if (a)
53 #pragma omp critical(the_name)
54     while (1)
55       ;
56 // CHECK:  call {{.*}}void [[FOO]]()
57   foo();
58 // CHECK-NOT:   call void @__kmpc_critical
59 // CHECK-NOT:   call void @__kmpc_end_critical
60   return a;
61 }
62 
63 struct S {
64   int a;
65 };
66 // CHECK-LABEL: critical_ref
critical_ref(S & s)67 void critical_ref(S &s) {
68   // CHECK: [[S_ADDR:%.+]] = alloca %struct.S*,
69   // CHECK: [[S_REF:%.+]] = load %struct.S*, %struct.S** [[S_ADDR]],
70   // CHECK: [[S_A_REF:%.+]] = getelementptr inbounds %struct.S, %struct.S* [[S_REF]], i32 0, i32 0
71   ++s.a;
72   // CHECK: [[S_REF:%.+]] = load %struct.S*, %struct.S** [[S_ADDR]],
73   // CHECK: store %struct.S* [[S_REF]], %struct.S** [[S_ADDR:%.+]],
74   // CHECK: call void @__kmpc_critical(
75 #pragma omp critical
76   // CHECK: [[S_REF:%.+]] = load %struct.S*, %struct.S** [[S_ADDR]],
77   // CHECK: [[S_A_REF:%.+]] = getelementptr inbounds %struct.S, %struct.S* [[S_REF]], i32 0, i32 0
78   ++s.a;
79   // CHECK: call void @__kmpc_end_critical(
80 }
81 
82 // CHECK-LABEL:      parallel_critical
83 // TERM_DEBUG-LABEL: parallel_critical
parallel_critical()84 void parallel_critical() {
85 #pragma omp parallel
86 #pragma omp critical
87   // TERM_DEBUG-NOT: __kmpc_global_thread_num
88   // TERM_DEBUG:     call void @__kmpc_critical({{.+}}), !dbg [[DBG_LOC_START:![0-9]+]]
89   // TERM_DEBUG:     invoke void {{.*}}foo{{.*}}()
90   // TERM_DEBUG:     unwind label %[[TERM_LPAD:.+]],
91   // TERM_DEBUG-NOT: __kmpc_global_thread_num
92   // TERM_DEBUG:     call void @__kmpc_end_critical({{.+}}), !dbg [[DBG_LOC_END:![0-9]+]]
93   // TERM_DEBUG:     [[TERM_LPAD]]
94   // TERM_DEBUG:     call void @__clang_call_terminate
95   // TERM_DEBUG:     unreachable
96   foo();
97 }
98 // TERM_DEBUG-DAG: [[DBG_LOC_START]] = !DILocation(line: [[@LINE-12]],
99 // TERM_DEBUG-DAG: [[DBG_LOC_END]] = !DILocation(line: [[@LINE-3]],
100 #endif
101