[flang-commits] [flang] [flang][openacc] Accept scalar integer expression in the if clause (PR #69381)

Valentin Clement バレンタイン クレメン via flang-commits flang-commits at lists.llvm.org
Tue Oct 17 13:37:37 PDT 2023


https://github.com/clementval created https://github.com/llvm/llvm-project/pull/69381

Relax the parser to accept scalar integer expression in addition to scalar logical expression. The parser now accepts scalar expression and the semantic checks its type. 

>From 8a3242e1f66ca0b998cf01b3eb82574478446414 Mon Sep 17 00:00:00 2001
From: Valentin Clement <clementval at gmail.com>
Date: Mon, 16 Oct 2023 14:15:34 -0700
Subject: [PATCH] [flang][openacc] Accept scalar integer in the if clause.

---
 flang/docs/OpenACC.md                             |  2 ++
 flang/lib/Semantics/check-acc-structure.cpp       | 15 ++++++++++++++-
 flang/test/Lower/OpenACC/acc-init.f90             |  6 ++++++
 .../test/Semantics/OpenACC/acc-init-validity.f90  |  6 ++++++
 llvm/include/llvm/Frontend/OpenACC/ACC.td         |  2 +-
 llvm/utils/TableGen/DirectiveEmitter.cpp          |  1 +
 6 files changed, 30 insertions(+), 2 deletions(-)

diff --git a/flang/docs/OpenACC.md b/flang/docs/OpenACC.md
index 80258041a627b93..e29c5f89f9b2482 100644
--- a/flang/docs/OpenACC.md
+++ b/flang/docs/OpenACC.md
@@ -21,3 +21,5 @@ local:
 * `!$acc end loop` does not trigger a parsing error and is just ignored.
 * The restriction on `!$acc data` required clauses is emitted as a portability
   warning instead of an error as other compiler accepts it.
+* The `if` clause accepts scalar integer expression in addition to scalar
+  logical expression.
diff --git a/flang/lib/Semantics/check-acc-structure.cpp b/flang/lib/Semantics/check-acc-structure.cpp
index 3c9e89940d23784..ce3525e3c335b97 100644
--- a/flang/lib/Semantics/check-acc-structure.cpp
+++ b/flang/lib/Semantics/check-acc-structure.cpp
@@ -376,7 +376,6 @@ CHECK_SIMPLE_CLAUSE(DeviceNum, ACCC_device_num)
 CHECK_SIMPLE_CLAUSE(Finalize, ACCC_finalize)
 CHECK_SIMPLE_CLAUSE(Firstprivate, ACCC_firstprivate)
 CHECK_SIMPLE_CLAUSE(Host, ACCC_host)
-CHECK_SIMPLE_CLAUSE(If, ACCC_if)
 CHECK_SIMPLE_CLAUSE(IfPresent, ACCC_if_present)
 CHECK_SIMPLE_CLAUSE(Independent, ACCC_independent)
 CHECK_SIMPLE_CLAUSE(NoCreate, ACCC_no_create)
@@ -660,6 +659,20 @@ void AccStructureChecker::Enter(const parser::AccClause::Link &x) {
   CheckMultipleOccurrenceInDeclare(x.v, llvm::acc::Clause::ACCC_link);
 }
 
+void AccStructureChecker::Enter(const parser::AccClause::If &x) {
+  CheckAllowed(llvm::acc::Clause::ACCC_if);
+  if (const auto *expr{GetExpr(x.v)}) {
+    if (auto type{expr->GetType()}) {
+      if (type->category() == TypeCategory::Integer ||
+          type->category() == TypeCategory::Logical) {
+        return; // LOGICAL and INTEGER type supported for the if clause.
+      }
+    }
+  }
+  context_.Say(
+      GetContext().clauseSource, "Must have LOGICAL or INTEGER type"_err_en_US);
+}
+
 void AccStructureChecker::Enter(const parser::Module &) {
   declareSymbols.clear();
 }
diff --git a/flang/test/Lower/OpenACC/acc-init.f90 b/flang/test/Lower/OpenACC/acc-init.f90
index 9132d41ccbdbdf2..de940426b6f1c0b 100644
--- a/flang/test/Lower/OpenACC/acc-init.f90
+++ b/flang/test/Lower/OpenACC/acc-init.f90
@@ -5,6 +5,7 @@
 
 subroutine acc_init
   logical :: ifCondition = .TRUE.
+  integer :: ifInt = 1
 
   !$acc init
 !CHECK: acc.init{{ *}}{{$}}
@@ -28,4 +29,9 @@ subroutine acc_init
 !CHECK: [[DEVTYPE2:%.*]] = arith.constant 2 : i32
 !CHECK: acc.init device_type([[DEVTYPE1]], [[DEVTYPE2]] : i32, i32) device_num([[DEVNUM]] : i32){{$}}
 
+  !$acc init if(ifInt)
+!CHECK: %[[IFINT:.*]] = fir.load %{{.*}} : !fir.ref<i32>
+!CHECK: %[[CONV:.*]] = fir.convert %[[IFINT]] : (i32) -> i1
+!CHECK: acc.init if(%[[CONV]])
+
 end subroutine acc_init
diff --git a/flang/test/Semantics/OpenACC/acc-init-validity.f90 b/flang/test/Semantics/OpenACC/acc-init-validity.f90
index 278211492c583ee..f54898f73fdce28 100644
--- a/flang/test/Semantics/OpenACC/acc-init-validity.f90
+++ b/flang/test/Semantics/OpenACC/acc-init-validity.f90
@@ -10,11 +10,14 @@ program openacc_init_validity
   integer :: i, j
   integer, parameter :: N = 256
   logical :: ifCondition = .TRUE.
+  integer :: ifInt
+  real :: ifReal
   real(8), dimension(N) :: a
 
   !$acc init
   !$acc init if(.TRUE.)
   !$acc init if(ifCondition)
+  !$acc init if(ifInt)
   !$acc init device_num(1)
   !$acc init device_num(i)
   !$acc init device_type(i)
@@ -93,4 +96,7 @@ program openacc_init_validity
   !ERROR: At most one DEVICE_TYPE clause can appear on the INIT directive
   !$acc init device_type(2) device_type(i, j)
 
+  !ERROR: Must have LOGICAL or INTEGER type
+  !$acc init if(ifReal)
+
 end program openacc_init_validity
diff --git a/llvm/include/llvm/Frontend/OpenACC/ACC.td b/llvm/include/llvm/Frontend/OpenACC/ACC.td
index 62b4113be6e27c1..692ddefb1b4cc2e 100644
--- a/llvm/include/llvm/Frontend/OpenACC/ACC.td
+++ b/llvm/include/llvm/Frontend/OpenACC/ACC.td
@@ -159,7 +159,7 @@ def ACCC_Host : Clause<"host"> {
 
 // 2.5.6
 def ACCC_If : Clause <"if"> {
-  let flangClass = "ScalarLogicalExpr";
+  let flangClass = "ScalarExpr";
 }
 
 // 2.14.4
diff --git a/llvm/utils/TableGen/DirectiveEmitter.cpp b/llvm/utils/TableGen/DirectiveEmitter.cpp
index 67033c6290ca0e6..b6aee665f8ee0bb 100644
--- a/llvm/utils/TableGen/DirectiveEmitter.cpp
+++ b/llvm/utils/TableGen/DirectiveEmitter.cpp
@@ -736,6 +736,7 @@ static void GenerateFlangClausesParser(const DirectiveLanguage &DirLang,
             .Case("Name", "name")
             .Case("ScalarIntConstantExpr", "scalarIntConstantExpr")
             .Case("ScalarIntExpr", "scalarIntExpr")
+            .Case("ScalarExpr", "scalarExpr")
             .Case("ScalarLogicalExpr", "scalarLogicalExpr")
             .Default(("Parser<" + Clause.getFlangClass() + ">{}")
                          .toStringRef(Scratch));



More information about the flang-commits mailing list