Skip to content

Commit 101b84a

Browse files
committed
Sync before exiting a cilk_scope with a non-compound statement body
1 parent d752b94 commit 101b84a

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

clang/lib/CodeGen/CGCilk.cpp

+12-10
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ void CodeGenFunction::EmitCilkSyncStmt(const CilkSyncStmt &S) {
456456
void CodeGenFunction::EmitCilkScopeStmt(const CilkScopeStmt &S) {
457457
LexicalScope CilkScope(*this, S.getSourceRange());
458458

459-
// If this _Cilk_scope is outermost in the function, emit
459+
// If this cilk_scope is outermost in the function, emit
460460
// tapir_runtime_{start,end} intrinsics around the scope.
461461
bool ThisScopeIsOutermost = false;
462462
if (!WithinCilkScope) {
@@ -471,22 +471,24 @@ void CodeGenFunction::EmitCilkScopeStmt(const CilkScopeStmt &S) {
471471
if (ThisScopeIsOutermost && !CurSyncRegion) {
472472
llvm::Instruction *TapirRTStart = Builder.CreateCall(
473473
CGM.getIntrinsic(llvm::Intrinsic::tapir_runtime_start));
474-
// Mark the end of the _Cilk_scope with tapir_runtime_end.
474+
// Mark the end of the cilk_scope with tapir_runtime_end.
475475
EHStack.pushCleanup<TapirRuntimeEndCleanup>(NormalAndEHCleanup,
476476
TapirRTStart);
477477
}
478478
// Create a nested synced scope.
479-
SyncRegionRAII SyncReg(*this);
480-
bool BodyIsCompoundStmt = isa<CompoundStmt>(S.getBody());
481-
if (BodyIsCompoundStmt)
479+
if (isa<CompoundStmt>(S.getBody())) {
480+
SyncRegionRAII SyncReg(*this);
482481
ScopeIsSynced = true;
483-
484-
// Emit the spawned statement.
485-
EmitStmt(S.getBody());
482+
EmitStmt(S.getBody());
483+
} else {
484+
PushSyncRegion()->addImplicitSync();
485+
EmitStmt(S.getBody());
486+
PopSyncRegion();
487+
}
486488
}
487489

488-
// If this _Cilk_scope is outermost in the function, mark that CodeGen is no
489-
// longer emitting within a _Cilk_scope.
490+
// If this cilk_scope is outermost in the function, mark that CodeGen is no
491+
// longer emitting within a cilk_scope.
490492
if (ThisScopeIsOutermost)
491493
WithinCilkScope = false;
492494
}

clang/test/Cilk/scope-nobrace.c

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// RUN: %clang_cc1 -emit-llvm -fopencilk -disable-llvm-passes -O1 -x c %s -o - | FileCheck %s
2+
3+
extern void bbb(void);
4+
5+
// CHECK-LABEL: @aaa
6+
int aaa(int argc, char *argv[])
7+
{
8+
// CHECK: call token @llvm.tapir.runtime.start()
9+
cilk_scope
10+
for (int i = 0; i < argc; ++i)
11+
// CHECK: detach within
12+
// CHECK: call void @bbb
13+
// CHECK-NEXT: reattach within
14+
// sync must precede runtime end
15+
// CHECK: sync within
16+
// CHECK: call void @llvm.tapir.runtime.end
17+
cilk_spawn { bbb(); }
18+
return 0;
19+
}

0 commit comments

Comments
 (0)