[clang] [OpenMP] Parse and Sema support for declare target in local scope (PR #83223)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Feb 27 21:26:49 PST 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Sandeep Kosuri (sandeepkosuri)
<details>
<summary>Changes</summary>
- adds Parse and Sema support for the `declare target` directive inside a function scope.
---
Full diff: https://github.com/llvm/llvm-project/pull/83223.diff
5 Files Affected:
- (modified) clang/include/clang/Basic/DiagnosticSemaKinds.td (+3)
- (modified) clang/lib/Parse/ParseOpenMP.cpp (+22-1)
- (modified) clang/lib/Sema/SemaOpenMP.cpp (+9)
- (modified) clang/test/OpenMP/declare_target_ast_print.cpp (+19)
- (modified) clang/test/OpenMP/declare_target_messages.cpp (+9)
``````````diff
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index a7f2858477bee6..faa7d1872ae3f1 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -11326,6 +11326,9 @@ def err_omp_device_type_mismatch : Error<
def err_omp_wrong_device_function_call : Error<
"function with 'device_type(%0)' is not available on %select{device|host}1">;
def note_omp_marked_device_type_here : Note<"marked as 'device_type(%0)' here">;
+def warn_omp_declare_target_has_local_vars : Warning<
+ "local variable '%0' ignored in 'declare target' directive; ">,
+ InGroup<OpenMPTarget>;
def warn_omp_declare_target_after_first_use : Warning<
"declaration marked as declare target after first use, it may lead to incorrect results">,
InGroup<OpenMPTarget>;
diff --git a/clang/lib/Parse/ParseOpenMP.cpp b/clang/lib/Parse/ParseOpenMP.cpp
index bfc31f2653c237..814126e321d3bc 100644
--- a/clang/lib/Parse/ParseOpenMP.cpp
+++ b/clang/lib/Parse/ParseOpenMP.cpp
@@ -2984,8 +2984,29 @@ StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective(
OMPDirectiveScope.Exit();
break;
}
+ case OMPD_declare_target: {
+ SourceLocation DTLoc = ConsumeAnyToken();
+ bool HasClauses = Tok.isNot(tok::annot_pragma_openmp_end);
+ Sema::DeclareTargetContextInfo DTCI(DKind, DTLoc);
+ if (HasClauses)
+ ParseOMPDeclareTargetClauses(DTCI);
+ bool HasImplicitMappings =
+ !HasClauses || (DTCI.ExplicitlyMapped.empty() && DTCI.Indirect);
+
+ if (HasImplicitMappings) {
+ Diag(Tok, diag::err_omp_unexpected_directive)
+ << 1 << getOpenMPDirectiveName(DKind);
+ SkipUntil(tok::annot_pragma_openmp_end);
+ break;
+ }
+
+ // Skip the last annot_pragma_openmp_end.
+ ConsumeAnyToken();
+
+ Actions.ActOnFinishedOpenMPDeclareTargetContext(DTCI);
+ break;
+ }
case OMPD_declare_simd:
- case OMPD_declare_target:
case OMPD_begin_declare_target:
case OMPD_end_declare_target:
case OMPD_requires:
diff --git a/clang/lib/Sema/SemaOpenMP.cpp b/clang/lib/Sema/SemaOpenMP.cpp
index 7f75cfc5b54f35..0cd8ff065a3419 100644
--- a/clang/lib/Sema/SemaOpenMP.cpp
+++ b/clang/lib/Sema/SemaOpenMP.cpp
@@ -23352,6 +23352,15 @@ void Sema::ActOnOpenMPDeclareTargetName(NamedDecl *ND, SourceLocation Loc,
isa<FunctionTemplateDecl>(ND)) &&
"Expected variable, function or function template.");
+ if (auto *VD = dyn_cast<VarDecl>(ND)) {
+ // Only global variables can be marked as declare target.
+ if (!VD->isFileVarDecl() && !VD->isStaticLocal() &&
+ !VD->isStaticDataMember()) {
+ Diag(Loc, diag::warn_omp_declare_target_has_local_vars)
+ << VD->getNameAsString();
+ return;
+ }
+ }
// Diagnose marking after use as it may lead to incorrect diagnosis and
// codegen.
if (LangOpts.OpenMP >= 50 &&
diff --git a/clang/test/OpenMP/declare_target_ast_print.cpp b/clang/test/OpenMP/declare_target_ast_print.cpp
index 40c5dd299abd96..43cccf763e97c3 100644
--- a/clang/test/OpenMP/declare_target_ast_print.cpp
+++ b/clang/test/OpenMP/declare_target_ast_print.cpp
@@ -360,6 +360,17 @@ int inner_link;
// CHECK-NEXT: int inner_link;
// CHECK-NEXT: #pragma omp end declare target
+void foo2() { return ;}
+// CHECK: #pragma omp declare target
+// CHECK-NEXT: void foo2() {
+// CHECK-NEXT: return;
+// CHECK-NEXT: }
+
+int x;
+// CHECK: #pragma omp declare target link
+// CHECK-NEXT: int x;
+// CHECK-NEXT: #pragma omp end declare target
+
int main (int argc, char **argv) {
foo();
foo_c();
@@ -367,6 +378,14 @@ int main (int argc, char **argv) {
test1();
baz<float>();
baz<int>();
+
+#if _OPENMP == 202111
+#pragma omp declare target enter(foo2)
+#else
+#pragma omp declare target to (foo2)
+#endif
+
+ #pragma omp declare target link(x)
return (0);
}
diff --git a/clang/test/OpenMP/declare_target_messages.cpp b/clang/test/OpenMP/declare_target_messages.cpp
index cf034aca7c9136..39616bc47b2ccb 100644
--- a/clang/test/OpenMP/declare_target_messages.cpp
+++ b/clang/test/OpenMP/declare_target_messages.cpp
@@ -182,11 +182,20 @@ struct S {
#pragma omp end declare target
};
+void foo3() {
+ return;
+}
+
+int *y;
+int **w = &y;
int main (int argc, char **argv) {
+ int a = 2;
#pragma omp declare target // expected-error {{unexpected OpenMP directive '#pragma omp declare target'}}
int v;
#pragma omp end declare target // expected-error {{unexpected OpenMP directive '#pragma omp end declare target'}}
foo(v);
+#pragma omp declare target to(foo3) link(w) // omp52-error {{unexpected 'to' clause, use 'enter' instead}} omp52-error {{expected at least one 'enter', 'link' or 'indirect' clause}}
+#pragma omp declare target to(a) //omp45-warning {{local variable 'a' ignored in 'declare target' directive}} omp5-warning {{local variable 'a' ignored in 'declare target' directive}} omp51-warning {{local variable 'a' ignored in 'declare target' directive}} omp52-error {{unexpected 'to' clause, use 'enter' instead}} omp52-error {{expected at least one 'enter', 'link' or 'indirect' clause}}
return (0);
}
``````````
</details>
https://github.com/llvm/llvm-project/pull/83223
More information about the cfe-commits
mailing list