Skip to content
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

[flang][OpenMP] Added semantic checks for target update #71270

Merged
merged 1 commit into from
Nov 4, 2023

Conversation

shraiysh
Copy link
Member

@shraiysh shraiysh commented Nov 4, 2023

This patch adds the following semantic check for target update

At least one motion-clause must be specified.

A motion clause is either a to or a from clause.

This patch also adds a test for the following semantic check which was already supported.

At most one nowait clause can appear on the directive.

This patch adds the following semantic check for target update

```
At least one motion-clause must be specified.
```

A motion clause is either a `to` or a `from` clause.

This patch also adds a test for the following semantic check which was
already supported.

```
At most one nowait clause can appear on the directive.
```
@llvmbot
Copy link
Collaborator

llvmbot commented Nov 4, 2023

@llvm/pr-subscribers-flang-parser

@llvm/pr-subscribers-flang-semantics

Author: Shraiysh (shraiysh)

Changes

This patch adds the following semantic check for target update

At least one motion-clause must be specified.

A motion clause is either a to or a from clause.

This patch also adds a test for the following semantic check which was already supported.

At most one nowait clause can appear on the directive.

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

5 Files Affected:

  • (modified) flang/lib/Semantics/check-omp-structure.cpp (+14-2)
  • (modified) flang/lib/Semantics/check-omp-structure.h (+1)
  • (modified) flang/test/Parser/OpenMP/if-clause.f90 (+2-2)
  • (modified) flang/test/Semantics/OpenMP/if-clause.f90 (+4-4)
  • (added) flang/test/Semantics/OpenMP/target-update01.f90 (+16)
diff --git a/flang/lib/Semantics/check-omp-structure.cpp b/flang/lib/Semantics/check-omp-structure.cpp
index 12f54fbd51e1c2f..a328424e0c098d0 100644
--- a/flang/lib/Semantics/check-omp-structure.cpp
+++ b/flang/lib/Semantics/check-omp-structure.cpp
@@ -10,7 +10,6 @@
 #include "definable.h"
 #include "flang/Parser/parse-tree.h"
 #include "flang/Semantics/tools.h"
-#include <algorithm>
 
 namespace Fortran::semantics {
 
@@ -1394,6 +1393,16 @@ void OmpStructureChecker::CheckOrderedDependClause(
   }
 }
 
+void OmpStructureChecker::CheckTargetUpdate() {
+  const parser::OmpClause *toClause = FindClause(llvm::omp::Clause::OMPC_to);
+  const parser::OmpClause *fromClause =
+      FindClause(llvm::omp::Clause::OMPC_from);
+  if (!toClause && !fromClause) {
+    context_.Say(GetContext().directiveSource,
+        "At least one motion-clause (TO/FROM) must be specified on TARGET UPDATE construct."_err_en_US);
+  }
+}
+
 void OmpStructureChecker::Enter(
     const parser::OpenMPSimpleStandaloneConstruct &x) {
   const auto &dir{std::get<parser::OmpSimpleStandaloneDirective>(x.t)};
@@ -1402,12 +1411,15 @@ void OmpStructureChecker::Enter(
 }
 
 void OmpStructureChecker::Leave(
-    const parser::OpenMPSimpleStandaloneConstruct &) {
+    const parser::OpenMPSimpleStandaloneConstruct &x) {
   switch (GetContext().directive) {
   case llvm::omp::Directive::OMPD_ordered:
     // [5.1] 2.19.9 Ordered Construct Restriction
     ChecksOnOrderedAsStandalone();
     break;
+  case llvm::omp::Directive::OMPD_target_update:
+    CheckTargetUpdate();
+    break;
   default:
     break;
   }
diff --git a/flang/lib/Semantics/check-omp-structure.h b/flang/lib/Semantics/check-omp-structure.h
index 78892d7bd282e65..0adfa7b5d83874d 100644
--- a/flang/lib/Semantics/check-omp-structure.h
+++ b/flang/lib/Semantics/check-omp-structure.h
@@ -182,6 +182,7 @@ class OmpStructureChecker
   void CheckDistLinear(const parser::OpenMPLoopConstruct &x);
   void CheckSIMDNest(const parser::OpenMPConstruct &x);
   void CheckTargetNest(const parser::OpenMPConstruct &x);
+  void CheckTargetUpdate();
   void CheckCancellationNest(
       const parser::CharBlock &source, const parser::OmpCancelType::Type &type);
   std::int64_t GetOrdCollapseLevel(const parser::OpenMPLoopConstruct &x);
diff --git a/flang/test/Parser/OpenMP/if-clause.f90 b/flang/test/Parser/OpenMP/if-clause.f90
index ca62f6969eee1c0..6d69e16e7cc731f 100644
--- a/flang/test/Parser/OpenMP/if-clause.f90
+++ b/flang/test/Parser/OpenMP/if-clause.f90
@@ -7,12 +7,12 @@ program openmp_parse_if
   ! CHECK: OmpSimpleStandaloneDirective -> llvm::omp::Directive = target update
   ! CHECK-NEXT: OmpClause -> If -> OmpIfClause
   ! CHECK-NOT: DirectiveNameModifier
-  !$omp target update if(cond)
+  !$omp target update if(cond) to(i)
 
   ! CHECK: OmpSimpleStandaloneDirective -> llvm::omp::Directive = target update
   ! CHECK-NEXT: OmpClause -> If -> OmpIfClause
   ! CHECK-NEXT: DirectiveNameModifier = TargetUpdate
-  !$omp target update if(target update: cond)
+  !$omp target update if(target update: cond) to(i)
 
   ! CHECK: OmpSimpleStandaloneDirective -> llvm::omp::Directive = target enter data
   ! CHECK: OmpClause -> If -> OmpIfClause
diff --git a/flang/test/Semantics/OpenMP/if-clause.f90 b/flang/test/Semantics/OpenMP/if-clause.f90
index dc9aa5ef7f1d15f..493c6c873bfbf29 100644
--- a/flang/test/Semantics/OpenMP/if-clause.f90
+++ b/flang/test/Semantics/OpenMP/if-clause.f90
@@ -451,15 +451,15 @@ program main
   ! ----------------------------------------------------------------------------
   ! TARGET UPDATE
   ! ----------------------------------------------------------------------------
-  !$omp target update if(.true.)
+  !$omp target update to(i) if(.true.)
   
-  !$omp target update if(target update: .true.)
+  !$omp target update to(i) if(target update: .true.)
 
   !ERROR: Unmatched directive name modifier TARGET on the IF clause
-  !$omp target update if(target: .true.)
+  !$omp target update to(i) if(target: .true.)
 
   !ERROR: At most one IF clause can appear on the TARGET UPDATE directive
-  !$omp target update if(.true.) if(target update: .false.)
+  !$omp target update to(i) if(.true.) if(target update: .false.)
 
   ! ----------------------------------------------------------------------------
   ! TASK
diff --git a/flang/test/Semantics/OpenMP/target-update01.f90 b/flang/test/Semantics/OpenMP/target-update01.f90
new file mode 100644
index 000000000000000..a0728e52ba3d230
--- /dev/null
+++ b/flang/test/Semantics/OpenMP/target-update01.f90
@@ -0,0 +1,16 @@
+! RUN: %python %S/../test_errors.py %s %flang_fc1 -fopenmp
+
+subroutine foo(x)
+  integer :: x
+  !ERROR: At least one motion-clause (TO/FROM) must be specified on TARGET UPDATE construct.
+  !$omp target update
+
+  !ERROR: At least one motion-clause (TO/FROM) must be specified on TARGET UPDATE construct.
+  !$omp target update nowait
+
+  !$omp target update to(x) nowait
+
+  !ERROR: At most one NOWAIT clause can appear on the TARGET UPDATE directive
+  !$omp target update to(x) nowait nowait
+
+end subroutine

@llvmbot
Copy link
Collaborator

llvmbot commented Nov 4, 2023

@llvm/pr-subscribers-flang-openmp

Author: Shraiysh (shraiysh)

Changes

This patch adds the following semantic check for target update

At least one motion-clause must be specified.

A motion clause is either a to or a from clause.

This patch also adds a test for the following semantic check which was already supported.

At most one nowait clause can appear on the directive.

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

5 Files Affected:

  • (modified) flang/lib/Semantics/check-omp-structure.cpp (+14-2)
  • (modified) flang/lib/Semantics/check-omp-structure.h (+1)
  • (modified) flang/test/Parser/OpenMP/if-clause.f90 (+2-2)
  • (modified) flang/test/Semantics/OpenMP/if-clause.f90 (+4-4)
  • (added) flang/test/Semantics/OpenMP/target-update01.f90 (+16)
diff --git a/flang/lib/Semantics/check-omp-structure.cpp b/flang/lib/Semantics/check-omp-structure.cpp
index 12f54fbd51e1c2f..a328424e0c098d0 100644
--- a/flang/lib/Semantics/check-omp-structure.cpp
+++ b/flang/lib/Semantics/check-omp-structure.cpp
@@ -10,7 +10,6 @@
 #include "definable.h"
 #include "flang/Parser/parse-tree.h"
 #include "flang/Semantics/tools.h"
-#include <algorithm>
 
 namespace Fortran::semantics {
 
@@ -1394,6 +1393,16 @@ void OmpStructureChecker::CheckOrderedDependClause(
   }
 }
 
+void OmpStructureChecker::CheckTargetUpdate() {
+  const parser::OmpClause *toClause = FindClause(llvm::omp::Clause::OMPC_to);
+  const parser::OmpClause *fromClause =
+      FindClause(llvm::omp::Clause::OMPC_from);
+  if (!toClause && !fromClause) {
+    context_.Say(GetContext().directiveSource,
+        "At least one motion-clause (TO/FROM) must be specified on TARGET UPDATE construct."_err_en_US);
+  }
+}
+
 void OmpStructureChecker::Enter(
     const parser::OpenMPSimpleStandaloneConstruct &x) {
   const auto &dir{std::get<parser::OmpSimpleStandaloneDirective>(x.t)};
@@ -1402,12 +1411,15 @@ void OmpStructureChecker::Enter(
 }
 
 void OmpStructureChecker::Leave(
-    const parser::OpenMPSimpleStandaloneConstruct &) {
+    const parser::OpenMPSimpleStandaloneConstruct &x) {
   switch (GetContext().directive) {
   case llvm::omp::Directive::OMPD_ordered:
     // [5.1] 2.19.9 Ordered Construct Restriction
     ChecksOnOrderedAsStandalone();
     break;
+  case llvm::omp::Directive::OMPD_target_update:
+    CheckTargetUpdate();
+    break;
   default:
     break;
   }
diff --git a/flang/lib/Semantics/check-omp-structure.h b/flang/lib/Semantics/check-omp-structure.h
index 78892d7bd282e65..0adfa7b5d83874d 100644
--- a/flang/lib/Semantics/check-omp-structure.h
+++ b/flang/lib/Semantics/check-omp-structure.h
@@ -182,6 +182,7 @@ class OmpStructureChecker
   void CheckDistLinear(const parser::OpenMPLoopConstruct &x);
   void CheckSIMDNest(const parser::OpenMPConstruct &x);
   void CheckTargetNest(const parser::OpenMPConstruct &x);
+  void CheckTargetUpdate();
   void CheckCancellationNest(
       const parser::CharBlock &source, const parser::OmpCancelType::Type &type);
   std::int64_t GetOrdCollapseLevel(const parser::OpenMPLoopConstruct &x);
diff --git a/flang/test/Parser/OpenMP/if-clause.f90 b/flang/test/Parser/OpenMP/if-clause.f90
index ca62f6969eee1c0..6d69e16e7cc731f 100644
--- a/flang/test/Parser/OpenMP/if-clause.f90
+++ b/flang/test/Parser/OpenMP/if-clause.f90
@@ -7,12 +7,12 @@ program openmp_parse_if
   ! CHECK: OmpSimpleStandaloneDirective -> llvm::omp::Directive = target update
   ! CHECK-NEXT: OmpClause -> If -> OmpIfClause
   ! CHECK-NOT: DirectiveNameModifier
-  !$omp target update if(cond)
+  !$omp target update if(cond) to(i)
 
   ! CHECK: OmpSimpleStandaloneDirective -> llvm::omp::Directive = target update
   ! CHECK-NEXT: OmpClause -> If -> OmpIfClause
   ! CHECK-NEXT: DirectiveNameModifier = TargetUpdate
-  !$omp target update if(target update: cond)
+  !$omp target update if(target update: cond) to(i)
 
   ! CHECK: OmpSimpleStandaloneDirective -> llvm::omp::Directive = target enter data
   ! CHECK: OmpClause -> If -> OmpIfClause
diff --git a/flang/test/Semantics/OpenMP/if-clause.f90 b/flang/test/Semantics/OpenMP/if-clause.f90
index dc9aa5ef7f1d15f..493c6c873bfbf29 100644
--- a/flang/test/Semantics/OpenMP/if-clause.f90
+++ b/flang/test/Semantics/OpenMP/if-clause.f90
@@ -451,15 +451,15 @@ program main
   ! ----------------------------------------------------------------------------
   ! TARGET UPDATE
   ! ----------------------------------------------------------------------------
-  !$omp target update if(.true.)
+  !$omp target update to(i) if(.true.)
   
-  !$omp target update if(target update: .true.)
+  !$omp target update to(i) if(target update: .true.)
 
   !ERROR: Unmatched directive name modifier TARGET on the IF clause
-  !$omp target update if(target: .true.)
+  !$omp target update to(i) if(target: .true.)
 
   !ERROR: At most one IF clause can appear on the TARGET UPDATE directive
-  !$omp target update if(.true.) if(target update: .false.)
+  !$omp target update to(i) if(.true.) if(target update: .false.)
 
   ! ----------------------------------------------------------------------------
   ! TASK
diff --git a/flang/test/Semantics/OpenMP/target-update01.f90 b/flang/test/Semantics/OpenMP/target-update01.f90
new file mode 100644
index 000000000000000..a0728e52ba3d230
--- /dev/null
+++ b/flang/test/Semantics/OpenMP/target-update01.f90
@@ -0,0 +1,16 @@
+! RUN: %python %S/../test_errors.py %s %flang_fc1 -fopenmp
+
+subroutine foo(x)
+  integer :: x
+  !ERROR: At least one motion-clause (TO/FROM) must be specified on TARGET UPDATE construct.
+  !$omp target update
+
+  !ERROR: At least one motion-clause (TO/FROM) must be specified on TARGET UPDATE construct.
+  !$omp target update nowait
+
+  !$omp target update to(x) nowait
+
+  !ERROR: At most one NOWAIT clause can appear on the TARGET UPDATE directive
+  !$omp target update to(x) nowait nowait
+
+end subroutine

Copy link
Contributor

@raghavendhra raghavendhra left a comment

Choose a reason for hiding this comment

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

LGTM

@shraiysh shraiysh merged commit 97c9c94 into llvm:main Nov 4, 2023
7 checks passed
@shraiysh shraiysh deleted the target_update_semantics1 branch November 4, 2023 20:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:openmp flang:parser flang:semantics flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants