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][openacc] Accept scalar integer expression in the if clause #69381

Merged
merged 1 commit into from
Oct 17, 2023

Conversation

clementval
Copy link
Contributor

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.

@llvmbot llvmbot added flang Flang issues not falling into any other category flang:fir-hlfir openacc flang:semantics labels Oct 17, 2023
@llvmbot
Copy link
Member

llvmbot commented Oct 17, 2023

@llvm/pr-subscribers-flang-semantics
@llvm/pr-subscribers-flang-fir-hlfir

@llvm/pr-subscribers-openacc

Author: Valentin Clement (バレンタイン クレメン) (clementval)

Changes

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.


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

6 Files Affected:

  • (modified) flang/docs/OpenACC.md (+2)
  • (modified) flang/lib/Semantics/check-acc-structure.cpp (+14-1)
  • (modified) flang/test/Lower/OpenACC/acc-init.f90 (+6)
  • (modified) flang/test/Semantics/OpenACC/acc-init-validity.f90 (+6)
  • (modified) llvm/include/llvm/Frontend/OpenACC/ACC.td (+1-1)
  • (modified) llvm/utils/TableGen/DirectiveEmitter.cpp (+1)
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));

@clementval clementval merged commit cf670d5 into llvm:main Oct 17, 2023
6 checks passed
@clementval clementval deleted the acc_if_clause branch October 17, 2023 21:41
Guzhu-AMD pushed a commit to GPUOpen-Drivers/llvm-project that referenced this pull request Oct 20, 2023
Local branch amd-gfx 917468d Merged main:122064a6303e into amd-gfx:ba5461f13b4c
Remote branch main cf670d5 [flang][openacc] Accept scalar integer expression in the if clause (llvm#69381)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:fir-hlfir flang:semantics flang Flang issues not falling into any other category openacc
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants