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] Add semantic check for device clause #72789

Merged
merged 1 commit into from
Nov 21, 2023

Conversation

shraiysh
Copy link
Member

This patch adds the following semantic check:

The ancestor device-modifier must not appear on the device clause on any
directive other than the target construct.

@llvmbot
Copy link
Collaborator

llvmbot commented Nov 19, 2023

@llvm/pr-subscribers-flang-openmp

@llvm/pr-subscribers-flang-semantics

Author: Shraiysh (shraiysh)

Changes

This patch adds the following semantic check:

The ancestor device-modifier must not appear on the device clause on any
directive other than the target construct.

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

2 Files Affected:

  • (modified) flang/lib/Semantics/check-omp-structure.cpp (+12)
  • (added) flang/test/Semantics/OpenMP/device-clause01.f90 (+31)
diff --git a/flang/lib/Semantics/check-omp-structure.cpp b/flang/lib/Semantics/check-omp-structure.cpp
index 0b1a581bf298196..a79ff19915476e8 100644
--- a/flang/lib/Semantics/check-omp-structure.cpp
+++ b/flang/lib/Semantics/check-omp-structure.cpp
@@ -8,8 +8,10 @@
 
 #include "check-omp-structure.h"
 #include "definable.h"
+#include "flang/Parser/characters.h"
 #include "flang/Parser/parse-tree.h"
 #include "flang/Semantics/tools.h"
+#include "llvm/Frontend/OpenMP/OMP.h.inc"
 
 namespace Fortran::semantics {
 
@@ -2748,6 +2750,16 @@ void OmpStructureChecker::Enter(const parser::OmpClause::Device &x) {
   const auto &device{std::get<1>(deviceClause.t)};
   RequiresPositiveParameter(
       llvm::omp::Clause::OMPC_device, device, "device expression");
+  auto modifier = std::get<0>(x.v.t);
+  if (modifier.has_value() &&
+      modifier.value() == parser::OmpDeviceClause::DeviceModifier::Ancestor) {
+    if(GetContext().directive != llvm::omp::OMPD_target) {
+      context_.Say(GetContext().clauseSource,
+          "The ANCESTOR device-modifier must not appear on the DEVICE clause on"
+          " any directive other than the TARGET construct. Found on %s construct."_err_en_US,
+          parser::ToUpperCaseLetters(getDirectiveName(GetContext().directive)));
+    }
+  }
 }
 
 void OmpStructureChecker::Enter(const parser::OmpClause::Depend &x) {
diff --git a/flang/test/Semantics/OpenMP/device-clause01.f90 b/flang/test/Semantics/OpenMP/device-clause01.f90
new file mode 100644
index 000000000000000..6f95d162790d575
--- /dev/null
+++ b/flang/test/Semantics/OpenMP/device-clause01.f90
@@ -0,0 +1,31 @@
+! RUN: %python %S/../test_errors.py %s %flang -fopenmp
+! OpenMP Version 5.2
+! 13.2 Device clause
+
+subroutine foo
+
+  integer :: a
+
+  !$omp target device(ancestor:0)
+  !$omp end target
+  !$omp target device(device_num:0)
+  !$omp end target
+  
+  !ERROR: The ANCESTOR device-modifier must not appear on the DEVICE clause on any directive other than the TARGET construct. Found on TARGET DATA construct.
+  !$omp target data device(ancestor:0) map(tofrom:a)
+  !$omp end target data
+  !$omp target data device(device_num:0) map(tofrom:a)
+  !$omp end target data
+
+  
+  !ERROR: The ANCESTOR device-modifier must not appear on the DEVICE clause on any directive other than the TARGET construct. Found on TARGET ENTER DATA construct.
+  !$omp target enter data device(ancestor:0) map(to:a)
+  !$omp target exit data map(from:a)
+  !$omp target enter data device(device_num:0) map(to:a)
+  !$omp target exit data map(from:a)
+
+  !ERROR: The ANCESTOR device-modifier must not appear on the DEVICE clause on any directive other than the TARGET construct. Found on TARGET UPDATE construct.
+  !$omp target update device(ancestor:0) to(a)
+  !$omp target update device(device_num:0) to(a)
+
+end subroutine foo

Copy link
Contributor

@kiranchandramohan kiranchandramohan left a comment

Choose a reason for hiding this comment

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

LG with Nit comments.

@@ -2748,6 +2748,16 @@ void OmpStructureChecker::Enter(const parser::OmpClause::Device &x) {
const auto &device{std::get<1>(deviceClause.t)};
RequiresPositiveParameter(
llvm::omp::Clause::OMPC_device, device, "device expression");
auto modifier = std::get<0>(x.v.t);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
auto modifier = std::get<0>(x.v.t);
std::optional<parser::OmpDeviceClause::DeviceModifier> modifier = std::get<0>(deviceClause.t);

Comment on lines 2752 to 2753
if (modifier.has_value() &&
modifier.value() == parser::OmpDeviceClause::DeviceModifier::Ancestor) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (modifier.has_value() &&
modifier.value() == parser::OmpDeviceClause::DeviceModifier::Ancestor) {
if (modifier &&
*modifier == parser::OmpDeviceClause::DeviceModifier::Ancestor) {

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

Copy link
Contributor

@agozillon agozillon left a comment

Choose a reason for hiding this comment

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

LGTM, thank you for the patch!

This patch adds the following semantic check:

```
The ancestor device-modifier must not appear on the device clause on any
directive other than the target construct.
```
@shraiysh shraiysh merged commit 525396a into llvm:main Nov 21, 2023
2 of 3 checks passed
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