[clang] 668cd1c - [OpenACC] Implement 'return' branch-out of Compute Construct (#82814)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Feb 26 06:04:42 PST 2024
Author: Erich Keane
Date: 2024-02-26T06:04:38-08:00
New Revision: 668cd1ca15a8b9c60a87e5244db9c97b3ba2e624
URL: https://github.com/llvm/llvm-project/commit/668cd1ca15a8b9c60a87e5244db9c97b3ba2e624
DIFF: https://github.com/llvm/llvm-project/commit/668cd1ca15a8b9c60a87e5244db9c97b3ba2e624.diff
LOG: [OpenACC] Implement 'return' branch-out of Compute Construct (#82814)
Like with 'break'/'continue', returning out of a compute construct is
ill-formed, so this implements the diagnostic. However, unlike the
OpenMP implementation of this same diagnostic, OpenACC doesn't have a
concept of 'capture region', so this is implemented as just checking the
'scope'.
Added:
clang/test/SemaOpenACC/no-branch-in-out.cpp
Modified:
clang/include/clang/Basic/DiagnosticSemaKinds.td
clang/include/clang/Sema/Scope.h
clang/lib/Sema/SemaStmt.cpp
clang/test/SemaOpenACC/no-branch-in-out.c
Removed:
################################################################################
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index dad1764ad4f861..57784a4ba2e388 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -12210,6 +12210,7 @@ def warn_acc_clause_unimplemented
def err_acc_construct_appertainment
: Error<"OpenACC construct '%0' cannot be used here; it can only "
"be used in a statement context">;
-def err_acc_branch_in_out
- : Error<"invalid branch %select{out of|into}0 OpenACC Compute Construct">;
+def err_acc_branch_in_out_compute_construct
+ : Error<"invalid %select{branch|return}0 %select{out of|into}1 OpenACC "
+ "Compute Construct">;
} // end of sema component.
diff --git a/clang/include/clang/Sema/Scope.h b/clang/include/clang/Sema/Scope.h
index e7f166fe3461fd..b6b5a1f3479a25 100644
--- a/clang/include/clang/Sema/Scope.h
+++ b/clang/include/clang/Sema/Scope.h
@@ -521,6 +521,19 @@ class Scope {
return getFlags() & Scope::OpenACCComputeConstructScope;
}
+ bool isInOpenACCComputeConstructScope() const {
+ for (const Scope *S = this; S; S = S->getParent()) {
+ if (S->getFlags() & Scope::OpenACCComputeConstructScope)
+ return true;
+ else if (S->getFlags() &
+ (Scope::FnScope | Scope::ClassScope | Scope::BlockScope |
+ Scope::TemplateParamScope | Scope::FunctionPrototypeScope |
+ Scope::AtCatchScope | Scope::ObjCMethodScope))
+ return false;
+ }
+ return false;
+ }
+
/// Determine whether this scope is a while/do/for statement, which can have
/// continue statements embedded into it.
bool isContinueScope() const {
diff --git a/clang/lib/Sema/SemaStmt.cpp b/clang/lib/Sema/SemaStmt.cpp
index fcad09a63662ba..0a5c2b23a90c8e 100644
--- a/clang/lib/Sema/SemaStmt.cpp
+++ b/clang/lib/Sema/SemaStmt.cpp
@@ -3361,8 +3361,9 @@ Sema::ActOnContinueStmt(SourceLocation ContinueLoc, Scope *CurScope) {
// of a compute construct counts as 'branching out of' the compute construct,
// so diagnose here.
if (S->isOpenACCComputeConstructScope())
- return StmtError(Diag(ContinueLoc, diag::err_acc_branch_in_out)
- << /*out of */ 0);
+ return StmtError(
+ Diag(ContinueLoc, diag::err_acc_branch_in_out_compute_construct)
+ << /*branch*/ 0 << /*out of */ 0);
CheckJumpOutOfSEHFinally(*this, ContinueLoc, *S);
@@ -3390,8 +3391,9 @@ Sema::ActOnBreakStmt(SourceLocation BreakLoc, Scope *CurScope) {
if (S->isOpenACCComputeConstructScope() ||
(S->isLoopScope() && S->getParent() &&
S->getParent()->isOpenACCComputeConstructScope()))
- return StmtError(Diag(BreakLoc, diag::err_acc_branch_in_out)
- << /*out of */ 0);
+ return StmtError(
+ Diag(BreakLoc, diag::err_acc_branch_in_out_compute_construct)
+ << /*branch*/ 0 << /*out of */ 0);
CheckJumpOutOfSEHFinally(*this, BreakLoc, *S);
@@ -3947,6 +3949,12 @@ Sema::ActOnReturnStmt(SourceLocation ReturnLoc, Expr *RetValExp,
RetValExp, nullptr, /*RecoverUncorrectedTypos=*/true);
if (RetVal.isInvalid())
return StmtError();
+
+ if (getCurScope()->isInOpenACCComputeConstructScope())
+ return StmtError(
+ Diag(ReturnLoc, diag::err_acc_branch_in_out_compute_construct)
+ << /*return*/ 1 << /*out of */ 0);
+
StmtResult R =
BuildReturnStmt(ReturnLoc, RetVal.get(), /*AllowRecovery=*/true);
if (R.isInvalid() || ExprEvalContexts.back().isDiscardedStatementContext())
diff --git a/clang/test/SemaOpenACC/no-branch-in-out.c b/clang/test/SemaOpenACC/no-branch-in-out.c
index 33a171f1b68d51..f8fb40a1ca8f72 100644
--- a/clang/test/SemaOpenACC/no-branch-in-out.c
+++ b/clang/test/SemaOpenACC/no-branch-in-out.c
@@ -93,3 +93,23 @@ void BreakContinue() {
}
+void Return() {
+#pragma acc parallel
+ {
+ return;// expected-error{{invalid return out of OpenACC Compute Construct}}
+ }
+
+#pragma acc parallel
+ {
+ {
+ return;// expected-error{{invalid return out of OpenACC Compute Construct}}
+ }
+ }
+
+#pragma acc parallel
+ {
+ for (int i = 0; i < 5; ++i) {
+ return;// expected-error{{invalid return out of OpenACC Compute Construct}}
+ }
+ }
+}
diff --git a/clang/test/SemaOpenACC/no-branch-in-out.cpp b/clang/test/SemaOpenACC/no-branch-in-out.cpp
new file mode 100644
index 00000000000000..232e372cedd357
--- /dev/null
+++ b/clang/test/SemaOpenACC/no-branch-in-out.cpp
@@ -0,0 +1,17 @@
+// RUN: %clang_cc1 %s -verify -fopenacc -fcxx-exceptions
+
+
+void ReturnTest() {
+#pragma acc parallel
+ {
+ (void)[]() { return; };
+ }
+
+#pragma acc parallel
+ {
+ try {}
+ catch(...){
+ return; // expected-error{{invalid return out of OpenACC Compute Construct}}
+ }
+ }
+}
More information about the cfe-commits
mailing list