Skip to content

Commit

Permalink
[CodeGenFunction] Fix emission of implicit syncs before returns for r…
Browse files Browse the repository at this point in the history
…eturn statements processed early in a function.
  • Loading branch information
neboat committed Mar 5, 2021
1 parent 3241289 commit 21c13db
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
3 changes: 2 additions & 1 deletion clang/lib/CodeGen/CodeGenFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
43 changes: 43 additions & 0 deletions clang/test/Cilk/early-return-while.c
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion clang/test/Cilk/early-return.c
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 21c13db

Please sign in to comment.