Skip to content

Commit

Permalink
[Cilk] Emit a diagnostic message, rather than fail an assert, when co…
Browse files Browse the repository at this point in the history
…degen fails to emit a spawn.

Addresses issue #5
  • Loading branch information
neboat committed Jun 30, 2020
1 parent 7049520 commit 41bde2b
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 29 deletions.
13 changes: 3 additions & 10 deletions clang/lib/CodeGen/CGCilk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ void CodeGenFunction::DetachScope::StartDetach() {
}

void CodeGenFunction::DetachScope::CleanupDetach() {
if (DetachCleanedUp)
if (!DetachStarted || DetachCleanedUp)
return;

// Pop the sync region for the detached task.
Expand Down Expand Up @@ -380,13 +380,6 @@ static const Stmt *IgnoreImplicitAndCleanups(const Stmt *S) {
return Current;
}

static void FailedSpawnWarning(CodeGenFunction &CGF, SourceLocation SLoc) {
DiagnosticsEngine &Diags = CGF.CGM.getDiags();
unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Warning,
"Failed to produce spawn");
Diags.Report(SLoc, DiagID);
}

void CodeGenFunction::EmitCilkSpawnStmt(const CilkSpawnStmt &S) {
// Handle spawning of calls in a special manner, to evaluate
// arguments before spawn.
Expand All @@ -403,7 +396,7 @@ void CodeGenFunction::EmitCilkSpawnStmt(const CilkSpawnStmt &S) {
// Finish the detach.
if (IsSpawned) {
if (!CurDetachScope->IsDetachStarted())
FailedSpawnWarning(*this, S.getBeginLoc());
FailedSpawnWarning(S.getBeginLoc());
IsSpawned = false;
PopDetachScope();
}
Expand Down Expand Up @@ -441,7 +434,7 @@ LValue CodeGenFunction::EmitCilkSpawnExprLValue(const CilkSpawnExpr *E) {
// Finish the detach.
if (IsSpawned) {
if (!CurDetachScope->IsDetachStarted())
FailedSpawnWarning(*this, E->getExprLoc());
FailedSpawnWarning(E->getExprLoc());
IsSpawned = false;
PopDetachScope();
}
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/CodeGen/CGExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4646,8 +4646,8 @@ LValue CodeGenFunction::EmitBinaryOperatorLValue(const BinaryOperator *E) {
EmitStoreThroughLValue(RV, LV);

// Finish the detach.
assert(CurDetachScope && CurDetachScope->IsDetachStarted() &&
"Processing _Cilk_spawn of expression did not produce a detach.");
if (!(CurDetachScope && CurDetachScope->IsDetachStarted()))
FailedSpawnWarning(E->getRHS()->getExprLoc());
PopDetachScope();
IsSpawned = false;

Expand Down
12 changes: 6 additions & 6 deletions clang/lib/CodeGen/CGExprAgg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -890,8 +890,8 @@ void AggExprEmitter::VisitCilkSpawnExpr(CilkSpawnExpr *E) {
Visit(E->getSpawnedExpr());

// Pop the detach scope
assert(CGF.IsSpawned && CGF.CurDetachScope->IsDetachStarted() &&
"Processing _Cilk_spawn of expression did not produce a detach.");
if (!(CGF.IsSpawned && CGF.CurDetachScope->IsDetachStarted()))
CGF.FailedSpawnWarning(E->getExprLoc());
CGF.IsSpawned = false;
CGF.PopDetachScope();
}
Expand Down Expand Up @@ -1192,8 +1192,8 @@ void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) {
Visit(E->getRHS());
CGF.EmitAtomicStore(Dest.asRValue(), LHS, /*isInit*/ false);
if (CGF.IsSpawned) {
assert(CGF.CurDetachScope && CGF.CurDetachScope->IsDetachStarted() &&
"Processing _Cilk_spawn of expression did not produce a detach.");
if (!(CGF.CurDetachScope && CGF.CurDetachScope->IsDetachStarted()))
CGF.FailedSpawnWarning(E->getRHS()->getExprLoc());
CGF.IsSpawned = false;
CGF.PopDetachScope();
}
Expand All @@ -1217,8 +1217,8 @@ void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) {
EmitFinalDestCopy(E->getType(), LHS);

if (CGF.IsSpawned) {
assert(CGF.CurDetachScope && CGF.CurDetachScope->IsDetachStarted() &&
"Processing _Cilk_spawn of expression did not produce a detach.");
if (!(CGF.CurDetachScope && CGF.CurDetachScope->IsDetachStarted()))
CGF.FailedSpawnWarning(E->getRHS()->getExprLoc());
CGF.IsSpawned = false;
CGF.PopDetachScope();
}
Expand Down
8 changes: 4 additions & 4 deletions clang/lib/CodeGen/CGExprComplex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ class ComplexExprEmitter
CGF.PushDetachScope();
ComplexPairTy C = Visit(CSE->getSpawnedExpr());
if (DoSpawnedInit) {
assert(CGF.CurDetachScope && CGF.CurDetachScope->IsDetachStarted() &&
"Processing _Cilk_spawn of expression did not produce detach.");
if (!(CGF.CurDetachScope && CGF.CurDetachScope->IsDetachStarted()))
CGF.FailedSpawnWarning(CSE->getExprLoc());
LValue LV = LValueToSpawnInit;
EmitStoreOfComplex(C, LV, /*init*/ true);

Expand Down Expand Up @@ -1022,8 +1022,8 @@ LValue ComplexExprEmitter::EmitBinAssignLValue(const BinaryOperator *E,
EmitStoreOfComplex(Val, LHS, /*isInit*/ false);

// Finish the detach.
assert(CGF.CurDetachScope && CGF.CurDetachScope->IsDetachStarted() &&
"Processing _Cilk_spawn of expression did not produce detach.");
if (!(CGF.CurDetachScope && CGF.CurDetachScope->IsDetachStarted()))
CGF.FailedSpawnWarning(E->getRHS()->getExprLoc());
CGF.IsSpawned = false;
CGF.PopDetachScope();

Expand Down
8 changes: 4 additions & 4 deletions clang/lib/CodeGen/CGExprScalar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -464,9 +464,9 @@ class ScalarExprEmitter
CGF.IsSpawned = true;
CGF.PushDetachScope();
Value *V = Visit(CSE->getSpawnedExpr());
if (!(CGF.CurDetachScope && CGF.CurDetachScope->IsDetachStarted()))
CGF.FailedSpawnWarning(CSE->getExprLoc());
if (DoSpawnedInit) {
assert(CGF.CurDetachScope && CGF.CurDetachScope->IsDetachStarted() &&
"Processing _Cilk_spawn of expression did not produce detach.");
LValue LV = LValueToSpawnInit;
CGF.EmitNullabilityCheck(LV, V, CSE->getExprLoc());
CGF.EmitStoreThroughLValue(RValue::get(V), LV, true);
Expand Down Expand Up @@ -3974,8 +3974,8 @@ Value *ScalarExprEmitter::VisitBinAssign(const BinaryOperator *E) {
CGF.EmitStoreThroughLValue(RValue::get(RHS), LHS);

// Finish the detach.
assert(CGF.CurDetachScope && CGF.CurDetachScope->IsDetachStarted() &&
"Processing _Cilk_spawn of expression did not produce detach.");
if (!(CGF.CurDetachScope && CGF.CurDetachScope->IsDetachStarted()))
CGF.FailedSpawnWarning(E->getRHS()->getExprLoc());
CGF.IsSpawned = false;
CGF.PopDetachScope();

Expand Down
4 changes: 2 additions & 2 deletions clang/lib/CodeGen/CGStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1145,9 +1145,9 @@ void CodeGenFunction::EmitReturnStmt(const ReturnStmt &S) {
CurDetachScope->CleanupDetach();
cleanupScope.ForceCleanup();
if (IsSpawned) {
if (!(CurDetachScope && CurDetachScope->IsDetachStarted()))
FailedSpawnWarning(RV->getExprLoc());
// Pop the detach scope
assert(IsSpawned && CurDetachScope->IsDetachStarted() &&
"Processing _Cilk_spawn of expression did not produce a detach.");
IsSpawned = false;
PopDetachScope();
}
Expand Down
10 changes: 9 additions & 1 deletion clang/lib/CodeGen/CodeGenFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -1382,6 +1382,14 @@ class CodeGenFunction : public CodeGenTypeCache {
delete CurDetachScope;
}

/// Produce a warning that we failed to emit a spawn.
void FailedSpawnWarning(SourceLocation SLoc) {
DiagnosticsEngine &Diags = CGM.getDiags();
unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Warning,
"Failed to emit spawn");
Diags.Report(SLoc, DiagID);
}

// RAII for automatically popping detach scopes at the end of code-generating
// an expression.
class DetachScopeRAII {
Expand All @@ -1396,7 +1404,7 @@ class CodeGenFunction : public CodeGenTypeCache {
return;
CGF.PopDetachScope();
assert(CGF.CurDetachScope == StartingDetachScope &&
"Unexpected detach scope after processing AtomicExpr");
"Unexpected detach scope");
CGF.IsSpawned = false;
}
};
Expand Down
15 changes: 15 additions & 0 deletions clang/test/Cilk/spawn-expr.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Check the spawning of builtins.
//
// RUN: %clang_cc1 %s -triple x86_64-unknown-linux-gnu -fopencilk -verify -ftapir=none -S -emit-llvm -o - | FileCheck %s

int g(int);

int f() {
int x = _Cilk_spawn 0; // expected-warning {{Failed to emit spawn}}
g(_Cilk_spawn 7); // expected-warning {{Failed to emit spawn}}
return _Cilk_spawn 1; // expected-warning {{no parallelism from a '_Cilk_spawn' in a return statement}} expected-warning {{Failed to emit spawn}}
}

// CHECK-LABEL: define {{.*}}i32 @f(
// CHECK-NOT: detach

0 comments on commit 41bde2b

Please sign in to comment.