Skip to content

Commit

Permalink
Sync before exiting a cilk_scope with a non-compound statement body
Browse files Browse the repository at this point in the history
  • Loading branch information
VoxSciurorum committed Feb 28, 2025
1 parent d752b94 commit 101b84a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
22 changes: 12 additions & 10 deletions clang/lib/CodeGen/CGCilk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ void CodeGenFunction::EmitCilkSyncStmt(const CilkSyncStmt &S) {
void CodeGenFunction::EmitCilkScopeStmt(const CilkScopeStmt &S) {
LexicalScope CilkScope(*this, S.getSourceRange());

// If this _Cilk_scope is outermost in the function, emit
// If this cilk_scope is outermost in the function, emit
// tapir_runtime_{start,end} intrinsics around the scope.
bool ThisScopeIsOutermost = false;
if (!WithinCilkScope) {
Expand All @@ -471,22 +471,24 @@ void CodeGenFunction::EmitCilkScopeStmt(const CilkScopeStmt &S) {
if (ThisScopeIsOutermost && !CurSyncRegion) {
llvm::Instruction *TapirRTStart = Builder.CreateCall(
CGM.getIntrinsic(llvm::Intrinsic::tapir_runtime_start));
// Mark the end of the _Cilk_scope with tapir_runtime_end.
// Mark the end of the cilk_scope with tapir_runtime_end.
EHStack.pushCleanup<TapirRuntimeEndCleanup>(NormalAndEHCleanup,
TapirRTStart);
}
// Create a nested synced scope.
SyncRegionRAII SyncReg(*this);
bool BodyIsCompoundStmt = isa<CompoundStmt>(S.getBody());
if (BodyIsCompoundStmt)
if (isa<CompoundStmt>(S.getBody())) {
SyncRegionRAII SyncReg(*this);
ScopeIsSynced = true;

// Emit the spawned statement.
EmitStmt(S.getBody());
EmitStmt(S.getBody());
} else {
PushSyncRegion()->addImplicitSync();
EmitStmt(S.getBody());
PopSyncRegion();
}
}

// If this _Cilk_scope is outermost in the function, mark that CodeGen is no
// longer emitting within a _Cilk_scope.
// If this cilk_scope is outermost in the function, mark that CodeGen is no
// longer emitting within a cilk_scope.
if (ThisScopeIsOutermost)
WithinCilkScope = false;
}
Expand Down
19 changes: 19 additions & 0 deletions clang/test/Cilk/scope-nobrace.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// RUN: %clang_cc1 -emit-llvm -fopencilk -disable-llvm-passes -O1 -x c %s -o - | FileCheck %s

extern void bbb(void);

// CHECK-LABEL: @aaa
int aaa(int argc, char *argv[])
{
// CHECK: call token @llvm.tapir.runtime.start()
cilk_scope
for (int i = 0; i < argc; ++i)
// CHECK: detach within
// CHECK: call void @bbb
// CHECK-NEXT: reattach within
// sync must precede runtime end
// CHECK: sync within
// CHECK: call void @llvm.tapir.runtime.end
cilk_spawn { bbb(); }
return 0;
}

0 comments on commit 101b84a

Please sign in to comment.