[flang] [llvm] [Flang] [OpenMP] Add semantic checks for detach clause in task (PR #119172)

Tom Eccles via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 4 02:36:31 PDT 2025


================
@@ -2961,6 +2961,63 @@ void OmpStructureChecker::Leave(const parser::OmpClauseList &) {
   // clause
   CheckMultListItems();
 
+  if (GetContext().directive == llvm::omp::Directive::OMPD_task) {
+    if (auto *detachClause{FindClause(llvm::omp::Clause::OMPC_detach)}) {
+      unsigned version{context_.langOptions().OpenMPVersion};
+      if (version == 50 || version == 51) {
+        // OpenMP 5.0: 2.10.1 Task construct restrictions
+        CheckNotAllowedIfClause(llvm::omp::Clause::OMPC_detach,
+            {llvm::omp::Clause::OMPC_mergeable});
+      } else if (version >= 52) {
+        // OpenMP 5.2: 12.5.2 Detach construct restrictions
+        if (FindClause(llvm::omp::Clause::OMPC_final)) {
+          context_.Say(GetContext().clauseSource,
+              "If a DETACH clause appears on a directive, then the encountering task must not be a FINAL task"_err_en_US);
+        }
+
+        const auto &detach{
+            std::get<parser::OmpClause::Detach>(detachClause->u)};
+        if (const auto *name{parser::Unwrap<parser::Name>(detach.v.v)}) {
+          if (name->symbol) {
+            Symbol *eventHandleSym{name->symbol};
+            auto checkVarAppearsInDataEnvClause = [&](const parser::
+                                                          OmpObjectList &objs,
+                                                      std::string clause) {
+              for (const auto &obj : objs.v) {
+                if (const parser::Name *
+                    objName{parser::Unwrap<parser::Name>(obj)}) {
+                  if (&objName->symbol->GetUltimate() == eventHandleSym) {
+                    context_.Say(GetContext().clauseSource,
+                        "A variable: `%s` that appears in a DETACH clause cannot appear on %s clause on the same construct"_err_en_US,
+                        objName->source, clause);
+                  }
+                }
+              }
+            };
+            if (auto *dataEnvClause{
+                    FindClause(llvm::omp::Clause::OMPC_private)}) {
+              const auto &pClause{
+                  std::get<parser::OmpClause::Private>(dataEnvClause->u)};
+              checkVarAppearsInDataEnvClause(pClause.v, "PRIVATE");
+            } else if (auto *dataEnvClause{
+                           FindClause(llvm::omp::Clause::OMPC_firstprivate)}) {
+              const auto &fpClause{
+                  std::get<parser::OmpClause::Firstprivate>(dataEnvClause->u)};
+              checkVarAppearsInDataEnvClause(fpClause.v, "FIRSTPRIVATE");
+            } else if (auto *dataEnvClause{
+                           FindClause(llvm::omp::Clause::OMPC_in_reduction)}) {
+              const auto &irClause{
+                  std::get<parser::OmpClause::InReduction>(dataEnvClause->u)};
+              checkVarAppearsInDataEnvClause(
+                  std::get<parser::OmpObjectList>(irClause.v.t),
+                  "IN_REDUCTION");
+            }
----------------
tblah wrote:

what about SHARED?

https://github.com/llvm/llvm-project/pull/119172


More information about the llvm-commits mailing list