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