]> granicus.if.org Git - clang/commitdiff
[OPENMP] Fix PR35156: Get correct thread id with windows exceptions.
authorAlexey Bataev <a.bataev@hotmail.com>
Thu, 2 Nov 2017 14:25:34 +0000 (14:25 +0000)
committerAlexey Bataev <a.bataev@hotmail.com>
Thu, 2 Nov 2017 14:25:34 +0000 (14:25 +0000)
If the thread id is requested in windows mode within funclets, we may
generate incorrect function call that could lead to broken codegen.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@317208 91177308-0d34-0410-b5e6-96231b3b80d8

12 files changed:
lib/CodeGen/CGOpenMPRuntime.cpp
test/OpenMP/critical_codegen.cpp
test/OpenMP/for_codegen.cpp
test/OpenMP/for_simd_codegen.cpp
test/OpenMP/master_codegen.cpp
test/OpenMP/openmp_win_codegen.cpp [new file with mode: 0644]
test/OpenMP/parallel_for_codegen.cpp
test/OpenMP/parallel_for_simd_codegen.cpp
test/OpenMP/parallel_sections_codegen.cpp
test/OpenMP/sections_codegen.cpp
test/OpenMP/single_codegen.cpp
test/OpenMP/taskgroup_codegen.cpp

index 35929af95e5783e72dc234e009b73ff807663411..779617adc21adc8636c81b511520e2e52a7177ac 100644 (file)
@@ -1451,7 +1451,8 @@ llvm::Value *CGOpenMPRuntime::getThreadID(CodeGenFunction &CGF,
       return ThreadID;
   }
   // If exceptions are enabled, do not use parameter to avoid possible crash.
-  if (!CGF.getInvokeDest()) {
+  if (!CGF.getInvokeDest() ||
+      CGF.Builder.GetInsertBlock() == CGF.AllocaInsertPt->getParent()) {
     if (auto *OMPRegionInfo =
             dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo)) {
       if (OMPRegionInfo->getThreadIDVariable()) {
@@ -1475,12 +1476,13 @@ llvm::Value *CGOpenMPRuntime::getThreadID(CodeGenFunction &CGF,
   // function.
   CGBuilderTy::InsertPointGuard IPG(CGF.Builder);
   CGF.Builder.SetInsertPoint(CGF.AllocaInsertPt);
-  ThreadID =
-      CGF.EmitRuntimeCall(createRuntimeFunction(OMPRTL__kmpc_global_thread_num),
-                          emitUpdateLocation(CGF, Loc));
+  auto *Call = CGF.Builder.CreateCall(
+      createRuntimeFunction(OMPRTL__kmpc_global_thread_num),
+      emitUpdateLocation(CGF, Loc));
+  Call->setCallingConv(CGF.getRuntimeCC());
   auto &Elem = OpenMPLocThreadIDMap.FindAndConstruct(CGF.CurFn);
-  Elem.second.ThreadID = ThreadID;
-  return ThreadID;
+  Elem.second.ThreadID = Call;
+  return Call;
 }
 
 void CGOpenMPRuntime::functionFinished(CodeGenFunction &CGF) {
index 964c91f45ae857a89d0630cdfc3f73edc145f037..f4e449a22370fad9809957a66f2596221886d3f4 100644 (file)
@@ -78,7 +78,7 @@ void critical_ref(S &s) {
 void parallel_critical() {
 #pragma omp parallel
 #pragma omp critical
-  // TERM_DEBUG:     __kmpc_global_thread_num
+  // TERM_DEBUG-NOT: __kmpc_global_thread_num
   // TERM_DEBUG:     call void @__kmpc_critical({{.+}}), !dbg [[DBG_LOC_START:![0-9]+]]
   // TERM_DEBUG:     invoke void {{.*}}foo{{.*}}()
   // TERM_DEBUG:     unwind label %[[TERM_LPAD:.+]],
index 0d5972f7d0dba16a68de616eed20b6441e1ea40d..9ea167fb1fd7a872741e7e813bc38653944b1d58 100644 (file)
@@ -355,7 +355,7 @@ int foo() {return 0;};
 void parallel_for(float *a) {
 #pragma omp parallel
 #pragma omp for schedule(static, 5)
-  // TERM_DEBUG:     __kmpc_global_thread_num
+  // TERM_DEBUG-NOT: __kmpc_global_thread_num
   // TERM_DEBUG:     call void @__kmpc_for_static_init_4u({{.+}}), !dbg [[DBG_LOC_START:![0-9]+]]
   // TERM_DEBUG:     invoke i32 {{.*}}foo{{.*}}()
   // TERM_DEBUG:     unwind label %[[TERM_LPAD:.+]],
index e33bfe4a575d3c73ef8921b087da45c02478d355..89f779db33cd79ac457fd6654785273614444351 100644 (file)
@@ -673,7 +673,7 @@ int bar() {return 0;};
 void parallel_simd(float *a) {
 #pragma omp parallel
 #pragma omp for simd
-  // TERM_DEBUG:     __kmpc_global_thread_num
+  // TERM_DEBUG-NOT: __kmpc_global_thread_num
   // TERM_DEBUG:     invoke i32 {{.*}}bar{{.*}}()
   // TERM_DEBUG:     unwind label %[[TERM_LPAD:.+]],
   // TERM_DEBUG-NOT: __kmpc_global_thread_num
index d61b4767560cb5f278de48f479148e1b52bc2751..ad92a13e14cfecc02f205440888695e5ff3bb370 100644 (file)
@@ -49,7 +49,7 @@ int main() {
 void parallel_master() {
 #pragma omp parallel
 #pragma omp master
-  // TERM_DEBUG:     __kmpc_global_thread_num
+  // TERM_DEBUG-NOT: __kmpc_global_thread_num
   // TERM_DEBUG:     call i32 @__kmpc_master({{.+}}), !dbg [[DBG_LOC_START:![0-9]+]]
   // TERM_DEBUG:     invoke void {{.*}}foo{{.*}}()
   // TERM_DEBUG:     unwind label %[[TERM_LPAD:.+]],
diff --git a/test/OpenMP/openmp_win_codegen.cpp b/test/OpenMP/openmp_win_codegen.cpp
new file mode 100644 (file)
index 0000000..cdad7e2
--- /dev/null
@@ -0,0 +1,38 @@
+// RUN: %clang_cc1 -verify -fopenmp -x c++ -triple x86_64-pc-windows-msvc18.0.0 -std=c++11 -fms-compatibility-version=18 -fms-extensions -emit-llvm %s -fexceptions -fcxx-exceptions -o - | FileCheck %s
+// REQUIRES: x86-registered-target
+// expected-no-diagnostics
+
+void foo();
+void bar();
+
+// CHECK-LABEL: @main
+int main() {
+  // CHECK: call void (%ident_t*, i32, void (i32*, i32*, ...)*, ...) @__kmpc_fork_call(%ident_t* @0, i32 0, void (i32*, i32*, ...)* bitcast (void (i32*, i32*)* [[OUTLINED:@.+]] to void (i32*, i32*, ...)*))
+#pragma omp parallel
+  {
+    try {
+      foo();
+    } catch (int t) {
+#pragma omp critical
+      {
+        bar();
+      };
+    }
+  };
+  // CHECK: ret i32 0
+  return 0;
+}
+
+// CHECK: define internal void [[OUTLINED]](
+// CHECK: [[GID:%.+]] = call i32 @__kmpc_global_thread_num(%ident_t* @0)
+// CHECK: invoke void @{{.+}}foo
+// CHECK: catchswitch within
+// CHECK: catchpad within
+// CHECK: call void @__kmpc_critical(%ident_t* @0, i32 [[GID]],
+// CHECK: invoke void @{{.+}}bar
+// CHECK: call void @__kmpc_end_critical(%ident_t* @0, i32 [[GID]],
+// CHECK: catchret from
+// CHECK: cleanuppad within
+// CHECK: call void @__kmpc_end_critical(%ident_t* @0, i32 [[GID]],
+// CHECK: cleanupret from
+
index bc04532bd17da1ce1bc347229470aef533e6c9b3..1773619bce20951efa4ae49491a474408061d909 100644 (file)
@@ -348,7 +348,7 @@ int foo() {return 0;};
 void parallel_for(float *a, int n) {
   float arr[n];
 #pragma omp parallel for schedule(static, 5) private(arr)
-  // TERM_DEBUG:     __kmpc_global_thread_num
+  // TERM_DEBUG-NOT: __kmpc_global_thread_num
   // TERM_DEBUG:     call void @__kmpc_for_static_init_4u({{.+}}), !dbg [[DBG_LOC_START:![0-9]+]]
   // TERM_DEBUG:     invoke i32 {{.*}}foo{{.*}}()
   // TERM_DEBUG:     unwind label %[[TERM_LPAD:.+]],
index 91126358551c04ab467457077d2b1ec3a9be051b..369ea17844e9cbd935293d480d57907b52631651 100644 (file)
@@ -668,7 +668,7 @@ int bar() {return 0;};
 // TERM_DEBUG-LABEL: parallel_simd
 void parallel_simd(float *a) {
 #pragma omp parallel for simd
-  // TERM_DEBUG:     __kmpc_global_thread_num
+  // TERM_DEBUG-NOT: __kmpc_global_thread_num
   // TERM_DEBUG:     invoke i32 {{.*}}bar{{.*}}()
   // TERM_DEBUG:     unwind label %[[TERM_LPAD:.+]],
   // TERM_DEBUG-NOT: __kmpc_global_thread_num
index afbc6e4d5ef946283338b63d7574276021ccc94e..a26147303611dc7de6fc2f450cdecdf5d367f170 100644 (file)
@@ -74,7 +74,7 @@ int main() {
 
 // CHECK-LABEL: tmain
 // CHECK:       call void {{.*}} @__kmpc_fork_call(
-// CHECK:       __kmpc_global_thread_num
+// CHECK-NOT:   __kmpc_global_thread_num
 // CHECK:       call void @__kmpc_for_static_init_4(
 // CHECK:       invoke void @{{.*}}foo{{.*}}()
 // CHECK-NEXT:  unwind label %[[TERM_LPAD:.+]]
index 0ed87e4035d935cc606652d604020759388d183c..94ded37db95d76f7ec3fe24fc17152dd173a606c 100644 (file)
@@ -84,7 +84,7 @@ int main() {
 
 // CHECK-LABEL: tmain
 // CHECK:       call void {{.*}} @__kmpc_fork_call(
-// CHECK:       __kmpc_global_thread_num
+// CHECK-NOT:   __kmpc_global_thread_num
 // CHECK:       call void @__kmpc_for_static_init_4(
 // CHECK:       invoke void @{{.*}}foo{{.*}}()
 // CHECK-NEXT:  unwind label %[[TERM_LPAD:.+]]
index 4feb3bdac3ed5381ffc63c8f30ffcb14026285e0..892e160324171b43eb4ba16078d6bcee465019c5 100644 (file)
@@ -190,7 +190,7 @@ int main() {
 void parallel_single() {
 #pragma omp parallel
 #pragma omp single
-  // TERM_DEBUG:     __kmpc_global_thread_num
+  // TERM_DEBUG-NOT: __kmpc_global_thread_num
   // TERM_DEBUG:     call i32 @__kmpc_single({{.+}}), !dbg [[DBG_LOC_START:![0-9]+]]
   // TERM_DEBUG:     invoke void {{.*}}foo{{.*}}()
   // TERM_DEBUG:     unwind label %[[TERM_LPAD:.+]],
index 3dd41a1f82afe5c9ad99983f5ce843b6b7c8761e..4b7d89e70391f6da395548958648e30ab5957930 100644 (file)
@@ -40,7 +40,7 @@ int main() {
 void parallel_taskgroup() {
 #pragma omp parallel
 #pragma omp taskgroup
-  // TERM_DEBUG:     __kmpc_global_thread_num
+  // TERM_DEBUG-NOT: __kmpc_global_thread_num
   // TERM_DEBUG:     call void @__kmpc_taskgroup({{.+}}), !dbg [[DBG_LOC_START:![0-9]+]]
   // TERM_DEBUG:     invoke void {{.*}}foo{{.*}}()
   // TERM_DEBUG:     unwind label %[[TERM_LPAD:.+]],