Skip to content

Commit

Permalink
[CGCilk] Fix code generation of normal cleanup destination slot aroun…
Browse files Browse the repository at this point in the history
…d unassociated taskframes.
  • Loading branch information
neboat committed Nov 23, 2024
1 parent 8074d9d commit a3f980d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
14 changes: 6 additions & 8 deletions clang/lib/CodeGen/CGCilk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,10 @@ CodeGenFunction::TaskFrameScope::TaskFrameScope(CodeGenFunction &CGF)
CGF.CGM.getIntrinsic(llvm::Intrinsic::taskframe_create);
TaskFrame = CGF.Builder.CreateCall(TaskFrameCreate);

// Make sure the normal cleanup dest slot is allocated before changing the
// alloca insertion point.
CGF.getNormalCleanupDestSlot();

// Create a new alloca insertion point within the task frame.
OldAllocaInsertPt = CGF.AllocaInsertPt;
llvm::Value *Undef = llvm::UndefValue::get(CGF.Int32Ty);
Expand All @@ -360,8 +364,6 @@ CodeGenFunction::TaskFrameScope::TaskFrameScope(CodeGenFunction &CGF)
CGF.ExceptionSlot = nullptr;
OldEHSelectorSlot = CGF.EHSelectorSlot;
CGF.EHSelectorSlot = nullptr;
OldNormalCleanupDest = CGF.NormalCleanupDest;
CGF.NormalCleanupDest = Address::invalid();

CGF.pushFullExprCleanup<EndUnassocTaskFrame>(
static_cast<CleanupKind>(NormalAndEHCleanup | LifetimeMarker | TaskExit),
Expand All @@ -375,7 +377,7 @@ CodeGenFunction::TaskFrameScope::~TaskFrameScope() {
return;

// Pop the taskframe.
CGF.PopCleanupBlock();
CGF.PopCleanupBlock(true);

// Restore the alloca insertion point.
{
Expand All @@ -389,7 +391,6 @@ CodeGenFunction::TaskFrameScope::~TaskFrameScope() {
CGF.EHResumeBlock = OldEHResumeBlock;
CGF.ExceptionSlot = OldExceptionSlot;
CGF.EHSelectorSlot = OldEHSelectorSlot;
CGF.NormalCleanupDest = OldNormalCleanupDest;

if (TempInvokeDest) {
if (llvm::BasicBlock *InvokeDest = CGF.getInvokeDest()) {
Expand Down Expand Up @@ -475,16 +476,13 @@ void CodeGenFunction::EmitCilkScopeStmt(const CilkScopeStmt &S) {
TapirRTStart);
}
// Create a nested synced scope.
SyncedScopeRAII SyncedScp(*this);
PushSyncRegion()->addImplicitSync();
SyncRegionRAII SyncReg(*this);
bool BodyIsCompoundStmt = isa<CompoundStmt>(S.getBody());
if (BodyIsCompoundStmt)
ScopeIsSynced = true;

// Emit the spawned statement.
EmitStmt(S.getBody());

PopSyncRegion();
}

// If this _Cilk_scope is outermost in the function, mark that CodeGen is no
Expand Down
1 change: 0 additions & 1 deletion clang/lib/CodeGen/CodeGenFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -1638,7 +1638,6 @@ class CodeGenFunction : public CodeGenTypeCache {
llvm::BasicBlock *OldEHResumeBlock = nullptr;
llvm::Value *OldExceptionSlot = nullptr;
llvm::AllocaInst *OldEHSelectorSlot = nullptr;
Address OldNormalCleanupDest = Address::invalid();

// Taskframe created separately from detach.
llvm::Value *TaskFrame = nullptr;
Expand Down
29 changes: 29 additions & 0 deletions clang/test/Cilk/cilkscope-try-normal-cleanup.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// RUN: %clang_cc1 %s -x c++ -O1 -fcxx-exceptions -fexceptions -fopencilk -mllvm -use-opencilk-runtime-bc=false -mllvm -debug-abi-calls=true -verify -S -emit-llvm -o - | FileCheck %s
// expected-no-diagnostics
extern int maybe_throws(int);

// CHECK-LABEL: _Z9function4v
int function4()
{
_Cilk_scope {
try {
// CHECK: i32 @_Z12maybe_throwsi
// CHECK-NEXT: to label %[[NORMAL:.+]] unwind label %[[LPAD:.+]]
maybe_throws(1);
// CHECK: [[LPAD]]:
// CHECK: invoke
// CHECK-NEXT: to label %[[NORMAL]] unwind label %[[LPAD1:.+]]
return 1;
} catch (...) {
// CHECK: [[NORMAL]]:
// CHECK-NEXT: %[[RETVAL:.+]] = phi i32
// CHECK-DAG: [ 2, %[[LPAD]] ]
// CHECK-DAG: [ 1, %entry ]
// CHECK: ret i32 %[[RETVAL]]
return 2;
}
}
// This return is unreachable.
// CHECK-NOT: ret i32 3
return 3;
}

0 comments on commit a3f980d

Please sign in to comment.