Skip to content

[Flang][OpenMP][taskloop] Adding missing semantic checks in Taskloop #128431

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 17, 2025

Conversation

sharangkaushik
Copy link
Contributor

Below semantic checks for Taskloop clause mentioned in OpenMP [5.2] specification were missing, this patch contains the semantic checks, corresponding error messages and test cases:
OpenMP standard [5.2]:
[12.6] Taskloop Construct
[Restrictions]
Restrictions to the taskloop construct are as follows:
• The reduction-modifier must be default.
• The conditional lastprivate-modifier must not be specified.

Below semantic checks for Taskloop clause mentioned in OpenMP [5.2] specification were
missing, this patch contains the semantic checks, corresponding error
messages and test cases:
OpenMP standard [5.2]:
[12.6] Taskloop Construct
[Restrictions]
Restrictions to the taskloop construct are as follows:
• The reduction-modifier must be default.
• The conditional lastprivate-modifier must not be specified.
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added flang Flang issues not falling into any other category flang:openmp flang:semantics labels Feb 23, 2025
@llvmbot
Copy link
Member

llvmbot commented Feb 23, 2025

@llvm/pr-subscribers-flang-semantics

Author: sharang.12492 (sharangkaushik)

Changes

Below semantic checks for Taskloop clause mentioned in OpenMP [5.2] specification were missing, this patch contains the semantic checks, corresponding error messages and test cases:
OpenMP standard [5.2]:
[12.6] Taskloop Construct
[Restrictions]
Restrictions to the taskloop construct are as follows:
• The reduction-modifier must be default.
• The conditional lastprivate-modifier must not be specified.


Full diff: https://github.com/llvm/llvm-project/pull/128431.diff

4 Files Affected:

  • (modified) flang/lib/Semantics/check-omp-structure.cpp (+31-4)
  • (modified) flang/lib/Semantics/check-omp-structure.h (+1)
  • (added) flang/test/Semantics/OpenMP/test_taskloop_lastprivate_semantic_restrictions.f90 (+12)
  • (added) flang/test/Semantics/OpenMP/test_taskloop_reduction_semantic_restrictions.f90 (+24)
diff --git a/flang/lib/Semantics/check-omp-structure.cpp b/flang/lib/Semantics/check-omp-structure.cpp
index ef7204dcd9196..471cefbe4875d 100644
--- a/flang/lib/Semantics/check-omp-structure.cpp
+++ b/flang/lib/Semantics/check-omp-structure.cpp
@@ -3369,13 +3369,18 @@ void OmpStructureChecker::CheckReductionModifier(
     return;
   }
   const DirectiveContext &dirCtx{GetContext()};
-  if (dirCtx.directive == llvm::omp::Directive::OMPD_loop) {
+  if (dirCtx.directive == llvm::omp::Directive::OMPD_loop ||
+      dirCtx.directive == llvm::omp::Directive::OMPD_taskloop) {
     // [5.2:257:33-34]
     // If a reduction-modifier is specified in a reduction clause that
     // appears on the directive, then the reduction modifier must be
     // default.
+    // [5.2:268:16]
+    // The reduction-modifier must be default.
     context_.Say(GetContext().clauseSource,
-        "REDUCTION modifier on LOOP directive must be DEFAULT"_err_en_US);
+        "REDUCTION modifier on %s directive must be DEFAULT"_err_en_US,
+        parser::ToUpperCaseLetters(GetContext().directiveSource.ToString()));
+    return;
   }
   if (modifier.v == ReductionModifier::Value::Task) {
     // "Task" is only allowed on worksharing or "parallel" directive.
@@ -4274,8 +4279,30 @@ void OmpStructureChecker::Enter(const parser::OmpClause::Lastprivate &x) {
   CheckPrivateSymbolsInOuterCxt(
       currSymbols, dirClauseTriple, llvm::omp::Clause::OMPC_lastprivate);
 
-  OmpVerifyModifiers(
-      x.v, llvm::omp::OMPC_lastprivate, GetContext().clauseSource, context_);
+  if (OmpVerifyModifiers(x.v, llvm::omp::OMPC_lastprivate,
+          GetContext().clauseSource, context_)) {
+    auto &modifiers{OmpGetModifiers(x.v)};
+    using LastprivateModifier = parser::OmpLastprivateModifier;
+    if (auto *modifier{OmpGetUniqueModifier<LastprivateModifier>(modifiers)}) {
+      CheckLastprivateModifier(*modifier);
+    }
+  }
+}
+
+// Add any restrictions related to Modifiers/Directives with
+// Lastprivate clause here:
+void OmpStructureChecker::CheckLastprivateModifier(
+    const parser::OmpLastprivateModifier &modifier) {
+  using LastprivateModifier = parser::OmpLastprivateModifier;
+  const DirectiveContext &dirCtx{GetContext()};
+  if (modifier.v == LastprivateModifier::Value::Conditional &&
+      dirCtx.directive == llvm::omp::Directive::OMPD_taskloop) {
+    // [5.2:268:17]
+    // The conditional lastprivate-modifier must not be specified.
+    context_.Say(GetContext().clauseSource,
+        "'CONDITIONAL' modifier on lastprivate clause with TASKLOOP "
+        "directive is not allowed"_err_en_US);
+  }
 }
 
 void OmpStructureChecker::Enter(const parser::OmpClause::Copyin &x) {
diff --git a/flang/lib/Semantics/check-omp-structure.h b/flang/lib/Semantics/check-omp-structure.h
index a9ac93a9149d4..fa74c67e51186 100644
--- a/flang/lib/Semantics/check-omp-structure.h
+++ b/flang/lib/Semantics/check-omp-structure.h
@@ -279,6 +279,7 @@ class OmpStructureChecker
   void CheckReductionObjectTypes(const parser::OmpObjectList &objects,
       const parser::OmpReductionIdentifier &ident);
   void CheckReductionModifier(const parser::OmpReductionModifier &);
+  void CheckLastprivateModifier(const parser::OmpLastprivateModifier &);
   void CheckMasterNesting(const parser::OpenMPBlockConstruct &x);
   void ChecksOnOrderedAsBlock();
   void CheckBarrierNesting(const parser::OpenMPSimpleStandaloneConstruct &x);
diff --git a/flang/test/Semantics/OpenMP/test_taskloop_lastprivate_semantic_restrictions.f90 b/flang/test/Semantics/OpenMP/test_taskloop_lastprivate_semantic_restrictions.f90
new file mode 100644
index 0000000000000..521a9cd031fcf
--- /dev/null
+++ b/flang/test/Semantics/OpenMP/test_taskloop_lastprivate_semantic_restrictions.f90
@@ -0,0 +1,12 @@
+!RUN: %python %S/../test_errors.py %s %flang -fopenmp -fopenmp-version=52
+
+subroutine foo()
+  integer :: x, i
+  x = 1
+!ERROR: 'CONDITIONAL' modifier on lastprivate clause with TASKLOOP directive is not allowed
+  !$omp taskloop lastprivate(conditional: x)
+  do i = 1, 100
+    x = x + 1
+  enddo
+  !$omp end taskloop
+end
diff --git a/flang/test/Semantics/OpenMP/test_taskloop_reduction_semantic_restrictions.f90 b/flang/test/Semantics/OpenMP/test_taskloop_reduction_semantic_restrictions.f90
new file mode 100644
index 0000000000000..81c1fed7a910a
--- /dev/null
+++ b/flang/test/Semantics/OpenMP/test_taskloop_reduction_semantic_restrictions.f90
@@ -0,0 +1,24 @@
+!RUN: %python %S/../test_errors.py %s %flang -fopenmp -fopenmp-version=52
+
+subroutine bar()
+  integer :: x, i
+  x = 1
+!ERROR: REDUCTION modifier on TASKLOOP directive must be DEFAULT
+!ERROR: List item x must appear in EXCLUSIVE or INCLUSIVE clause of an enclosed SCAN directive
+!$omp taskloop reduction(inscan, +:x)
+  do i = 1, 100
+    x = x + 1
+  enddo
+!$omp end taskloop
+end
+
+subroutine baz()
+  integer :: x, i
+  x = 1
+!ERROR: REDUCTION modifier on TASKLOOP directive must be DEFAULT
+!$omp taskloop reduction(task, +:x)
+  do i = 1, 100
+    x = x + 1
+  enddo
+!$omp end taskloop
+end

@llvmbot
Copy link
Member

llvmbot commented Feb 23, 2025

@llvm/pr-subscribers-flang-openmp

Author: sharang.12492 (sharangkaushik)

Changes

Below semantic checks for Taskloop clause mentioned in OpenMP [5.2] specification were missing, this patch contains the semantic checks, corresponding error messages and test cases:
OpenMP standard [5.2]:
[12.6] Taskloop Construct
[Restrictions]
Restrictions to the taskloop construct are as follows:
• The reduction-modifier must be default.
• The conditional lastprivate-modifier must not be specified.


Full diff: https://github.com/llvm/llvm-project/pull/128431.diff

4 Files Affected:

  • (modified) flang/lib/Semantics/check-omp-structure.cpp (+31-4)
  • (modified) flang/lib/Semantics/check-omp-structure.h (+1)
  • (added) flang/test/Semantics/OpenMP/test_taskloop_lastprivate_semantic_restrictions.f90 (+12)
  • (added) flang/test/Semantics/OpenMP/test_taskloop_reduction_semantic_restrictions.f90 (+24)
diff --git a/flang/lib/Semantics/check-omp-structure.cpp b/flang/lib/Semantics/check-omp-structure.cpp
index ef7204dcd9196..471cefbe4875d 100644
--- a/flang/lib/Semantics/check-omp-structure.cpp
+++ b/flang/lib/Semantics/check-omp-structure.cpp
@@ -3369,13 +3369,18 @@ void OmpStructureChecker::CheckReductionModifier(
     return;
   }
   const DirectiveContext &dirCtx{GetContext()};
-  if (dirCtx.directive == llvm::omp::Directive::OMPD_loop) {
+  if (dirCtx.directive == llvm::omp::Directive::OMPD_loop ||
+      dirCtx.directive == llvm::omp::Directive::OMPD_taskloop) {
     // [5.2:257:33-34]
     // If a reduction-modifier is specified in a reduction clause that
     // appears on the directive, then the reduction modifier must be
     // default.
+    // [5.2:268:16]
+    // The reduction-modifier must be default.
     context_.Say(GetContext().clauseSource,
-        "REDUCTION modifier on LOOP directive must be DEFAULT"_err_en_US);
+        "REDUCTION modifier on %s directive must be DEFAULT"_err_en_US,
+        parser::ToUpperCaseLetters(GetContext().directiveSource.ToString()));
+    return;
   }
   if (modifier.v == ReductionModifier::Value::Task) {
     // "Task" is only allowed on worksharing or "parallel" directive.
@@ -4274,8 +4279,30 @@ void OmpStructureChecker::Enter(const parser::OmpClause::Lastprivate &x) {
   CheckPrivateSymbolsInOuterCxt(
       currSymbols, dirClauseTriple, llvm::omp::Clause::OMPC_lastprivate);
 
-  OmpVerifyModifiers(
-      x.v, llvm::omp::OMPC_lastprivate, GetContext().clauseSource, context_);
+  if (OmpVerifyModifiers(x.v, llvm::omp::OMPC_lastprivate,
+          GetContext().clauseSource, context_)) {
+    auto &modifiers{OmpGetModifiers(x.v)};
+    using LastprivateModifier = parser::OmpLastprivateModifier;
+    if (auto *modifier{OmpGetUniqueModifier<LastprivateModifier>(modifiers)}) {
+      CheckLastprivateModifier(*modifier);
+    }
+  }
+}
+
+// Add any restrictions related to Modifiers/Directives with
+// Lastprivate clause here:
+void OmpStructureChecker::CheckLastprivateModifier(
+    const parser::OmpLastprivateModifier &modifier) {
+  using LastprivateModifier = parser::OmpLastprivateModifier;
+  const DirectiveContext &dirCtx{GetContext()};
+  if (modifier.v == LastprivateModifier::Value::Conditional &&
+      dirCtx.directive == llvm::omp::Directive::OMPD_taskloop) {
+    // [5.2:268:17]
+    // The conditional lastprivate-modifier must not be specified.
+    context_.Say(GetContext().clauseSource,
+        "'CONDITIONAL' modifier on lastprivate clause with TASKLOOP "
+        "directive is not allowed"_err_en_US);
+  }
 }
 
 void OmpStructureChecker::Enter(const parser::OmpClause::Copyin &x) {
diff --git a/flang/lib/Semantics/check-omp-structure.h b/flang/lib/Semantics/check-omp-structure.h
index a9ac93a9149d4..fa74c67e51186 100644
--- a/flang/lib/Semantics/check-omp-structure.h
+++ b/flang/lib/Semantics/check-omp-structure.h
@@ -279,6 +279,7 @@ class OmpStructureChecker
   void CheckReductionObjectTypes(const parser::OmpObjectList &objects,
       const parser::OmpReductionIdentifier &ident);
   void CheckReductionModifier(const parser::OmpReductionModifier &);
+  void CheckLastprivateModifier(const parser::OmpLastprivateModifier &);
   void CheckMasterNesting(const parser::OpenMPBlockConstruct &x);
   void ChecksOnOrderedAsBlock();
   void CheckBarrierNesting(const parser::OpenMPSimpleStandaloneConstruct &x);
diff --git a/flang/test/Semantics/OpenMP/test_taskloop_lastprivate_semantic_restrictions.f90 b/flang/test/Semantics/OpenMP/test_taskloop_lastprivate_semantic_restrictions.f90
new file mode 100644
index 0000000000000..521a9cd031fcf
--- /dev/null
+++ b/flang/test/Semantics/OpenMP/test_taskloop_lastprivate_semantic_restrictions.f90
@@ -0,0 +1,12 @@
+!RUN: %python %S/../test_errors.py %s %flang -fopenmp -fopenmp-version=52
+
+subroutine foo()
+  integer :: x, i
+  x = 1
+!ERROR: 'CONDITIONAL' modifier on lastprivate clause with TASKLOOP directive is not allowed
+  !$omp taskloop lastprivate(conditional: x)
+  do i = 1, 100
+    x = x + 1
+  enddo
+  !$omp end taskloop
+end
diff --git a/flang/test/Semantics/OpenMP/test_taskloop_reduction_semantic_restrictions.f90 b/flang/test/Semantics/OpenMP/test_taskloop_reduction_semantic_restrictions.f90
new file mode 100644
index 0000000000000..81c1fed7a910a
--- /dev/null
+++ b/flang/test/Semantics/OpenMP/test_taskloop_reduction_semantic_restrictions.f90
@@ -0,0 +1,24 @@
+!RUN: %python %S/../test_errors.py %s %flang -fopenmp -fopenmp-version=52
+
+subroutine bar()
+  integer :: x, i
+  x = 1
+!ERROR: REDUCTION modifier on TASKLOOP directive must be DEFAULT
+!ERROR: List item x must appear in EXCLUSIVE or INCLUSIVE clause of an enclosed SCAN directive
+!$omp taskloop reduction(inscan, +:x)
+  do i = 1, 100
+    x = x + 1
+  enddo
+!$omp end taskloop
+end
+
+subroutine baz()
+  integer :: x, i
+  x = 1
+!ERROR: REDUCTION modifier on TASKLOOP directive must be DEFAULT
+!$omp taskloop reduction(task, +:x)
+  do i = 1, 100
+    x = x + 1
+  enddo
+!$omp end taskloop
+end

Copy link
Contributor

@tblah tblah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great to me. Welcome to the LLVM project!

@tblah
Copy link
Contributor

tblah commented Feb 27, 2025

I suspect you won't have permission to merge the PR. When you are ready ping me and I can merge it for you.

@kaviya2510 kaviya2510 requested a review from kiranktp March 17, 2025 05:41
@kiranktp kiranktp merged commit 7eb8b73 into llvm:main Mar 17, 2025
11 checks passed
Copy link

@sharangkaushik Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

@sharangkaushik sharangkaushik deleted the taskloop-restrictions-patch branch March 18, 2025 09:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:openmp flang:semantics flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants