diff --git a/clang/lib/CodeGen/CodeGenFunction.cpp b/clang/lib/CodeGen/CodeGenFunction.cpp index d232728fd3d7..108398c5a98e 100644 --- a/clang/lib/CodeGen/CodeGenFunction.cpp +++ b/clang/lib/CodeGen/CodeGenFunction.cpp @@ -394,7 +394,8 @@ void CodeGenFunction::FinishFunction(SourceLocation EndLoc) { PopCleanupBlocks(PrologueCleanupDepth, {}, CompilingCilk); SyncEmitted = true; - } else if (CompilingCilk && Builder.GetInsertBlock()) { + } else if (CompilingCilk && Builder.GetInsertBlock() && + ReturnBlock.getBlock()->use_empty()) { // If we're compiling Cilk, emit an implicit sync for the function. EmitImplicitSyncCleanup(); SyncEmitted = true; diff --git a/clang/test/Cilk/early-return-while.c b/clang/test/Cilk/early-return-while.c new file mode 100644 index 000000000000..7b639f1be596 --- /dev/null +++ b/clang/test/Cilk/early-return-while.c @@ -0,0 +1,43 @@ +// RUN: %clang_cc1 %s -triple x86_64-unknown-linux-gnu -fcilkplus -verify -ftapir=none -S -emit-llvm -o - | FileCheck %s +// expected-no-diagnostics + +void bar(); +int baz(int); + +void foo(int p) { + while (p) { + if (baz(p)) + return; + _Cilk_spawn bar(); + --p; + } + bar(); +} + +// CHECK-LABEL: define {{.*}}void @foo( + +// CHECK: br i1 %{{.+}}, label %[[WHILE_BODY:.+]], label %[[WHILE_END:.+]] + +// CHECK: [[WHILE_BODY]]: +// CHECK: br i1 %{{.+}}, label %[[THEN:.+]], label %[[END:.+]] + +// CHECK: [[THEN]]: +// CHECK-NEXT: br label %[[RETURN:.+]] + +// CHECK: detach within %[[SYNCREG:.+]], label %[[DETACHED:.+]], label %[[CONTINUE:.+]] + +// CHECK: [[DETACHED]]: +// CHECK: call void {{.*}}@bar() +// CHECK: reattach within %[[SYNCREG]], label %[[CONTINUE]] + +// CHECK: [[CONTINUE]]: +// CHECK: br + +// CHECK: [[WHILE_END]]: +// CHECK-NEXT: call void {{.*}}@bar() +// CHECK-NEXT: br label %[[RETURN]] + +// CHECK: [[RETURN]]: +// CHECK-NEXT: sync within %[[SYNCREG]] + +// CHECK: ret void diff --git a/clang/test/Cilk/early-return.c b/clang/test/Cilk/early-return.c index a0ef073af84b..ba20ceb1314c 100644 --- a/clang/test/Cilk/early-return.c +++ b/clang/test/Cilk/early-return.c @@ -25,7 +25,8 @@ void foo(int p) { // CHECK: [[END]]: // CHECK-NEXT: call void {{.*}}@bar() -// CHECK-NEXT: sync within %[[SYNCREG]] + +// CHECK: sync within %[[SYNCREG]] // CHECK: ret void