[llvm] 6a3c4a4 - [flang][OpenMP] Added parser support for in_reduction clause

Nimish Mishra via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 6 02:24:19 PDT 2022


Author: Nimish Mishra
Date: 2022-06-06T14:55:27+05:30
New Revision: 6a3c4a40f4ba6943c5902660858d0c3df2830330

URL: https://github.com/llvm/llvm-project/commit/6a3c4a40f4ba6943c5902660858d0c3df2830330
DIFF: https://github.com/llvm/llvm-project/commit/6a3c4a40f4ba6943c5902660858d0c3df2830330.diff

LOG: [flang][OpenMP] Added parser support for in_reduction clause

OpenMP 5.0 adds a new clause `in_reduction` on OpenMP directives.
This patch adds parser support for the same.

Reviewed By: kiranchandramohan

Differential Revision: https://reviews.llvm.org/D124156

Added: 
    flang/test/Examples/omp-in-reduction-clause.f90
    flang/test/Parser/omp-in-reduction-clause.f90

Modified: 
    flang/include/flang/Parser/dump-parse-tree.h
    flang/include/flang/Parser/parse-tree.h
    flang/lib/Parser/openmp-parsers.cpp
    flang/lib/Parser/unparse.cpp
    llvm/include/llvm/Frontend/OpenMP/OMP.td

Removed: 
    


################################################################################
diff  --git a/flang/include/flang/Parser/dump-parse-tree.h b/flang/include/flang/Parser/dump-parse-tree.h
index 3ed00dd5e913..a81125e19e6d 100644
--- a/flang/include/flang/Parser/dump-parse-tree.h
+++ b/flang/include/flang/Parser/dump-parse-tree.h
@@ -514,6 +514,7 @@ class ParseTreeDumper {
   NODE(parser, OmpProcBindClause)
   NODE_ENUM(OmpProcBindClause, Type)
   NODE(parser, OmpReductionClause)
+  NODE(parser, OmpInReductionClause)
   NODE(parser, OmpReductionCombiner)
   NODE(OmpReductionCombiner, FunctionCombiner)
   NODE(parser, OmpReductionInitializerClause)

diff  --git a/flang/include/flang/Parser/parse-tree.h b/flang/include/flang/Parser/parse-tree.h
index 4824e4a51a9b..e1521f00ff0f 100644
--- a/flang/include/flang/Parser/parse-tree.h
+++ b/flang/include/flang/Parser/parse-tree.h
@@ -3434,6 +3434,13 @@ struct OmpReductionClause {
   std::tuple<OmpReductionOperator, OmpObjectList> t;
 };
 
+// OMP 5.0 2.19.5.6 in_reduction-clause -> IN_REDUCTION (reduction-identifier:
+//                                         variable-name-list)
+struct OmpInReductionClause {
+  TUPLE_CLASS_BOILERPLATE(OmpInReductionClause);
+  std::tuple<OmpReductionOperator, OmpObjectList> t;
+};
+
 // OMP 5.0 2.11.4 allocate-clause -> ALLOCATE ([allocator:] variable-name-list)
 struct OmpAllocateClause {
   TUPLE_CLASS_BOILERPLATE(OmpAllocateClause);

diff  --git a/flang/lib/Parser/openmp-parsers.cpp b/flang/lib/Parser/openmp-parsers.cpp
index 8d298f0304f5..e3d6d5f90bd5 100644
--- a/flang/lib/Parser/openmp-parsers.cpp
+++ b/flang/lib/Parser/openmp-parsers.cpp
@@ -122,6 +122,10 @@ TYPE_PARSER(construct<OmpReductionOperator>(Parser<DefinedOperator>{}) ||
 TYPE_PARSER(construct<OmpReductionClause>(
     Parser<OmpReductionOperator>{} / ":", Parser<OmpObjectList>{}))
 
+// OMP 5.0 2.19.5.6 IN_REDUCTION (reduction-identifier: variable-name-list)
+TYPE_PARSER(construct<OmpInReductionClause>(
+    Parser<OmpReductionOperator>{} / ":", Parser<OmpObjectList>{}))
+
 // OMP 5.0 2.11.4  ALLOCATE ([allocator:] variable-name-list)
 TYPE_PARSER(construct<OmpAllocateClause>(
     maybe(construct<OmpAllocateClause::Allocator>(scalarIntExpr) / ":"),
@@ -242,6 +246,8 @@ TYPE_PARSER(
                        parenthesized(Parser<OmpProcBindClause>{}))) ||
     "REDUCTION" >> construct<OmpClause>(construct<OmpClause::Reduction>(
                        parenthesized(Parser<OmpReductionClause>{}))) ||
+    "IN_REDUCTION" >> construct<OmpClause>(construct<OmpClause::InReduction>(
+                          parenthesized(Parser<OmpInReductionClause>{}))) ||
     "TASK_REDUCTION" >>
         construct<OmpClause>(construct<OmpClause::TaskReduction>(
             parenthesized(Parser<OmpReductionClause>{}))) ||

diff  --git a/flang/lib/Parser/unparse.cpp b/flang/lib/Parser/unparse.cpp
index c52ad06eeace..880d8323ab32 100644
--- a/flang/lib/Parser/unparse.cpp
+++ b/flang/lib/Parser/unparse.cpp
@@ -2025,6 +2025,11 @@ class UnparseVisitor {
     Put(":");
     Walk(std::get<OmpObjectList>(x.t));
   }
+  void Unparse(const OmpInReductionClause &x) {
+    Walk(std::get<OmpReductionOperator>(x.t));
+    Put(":");
+    Walk(std::get<OmpObjectList>(x.t));
+  }
   void Unparse(const OmpAllocateClause &x) {
     Walk(std::get<std::optional<OmpAllocateClause::Allocator>>(x.t));
     Put(":");

diff  --git a/flang/test/Examples/omp-in-reduction-clause.f90 b/flang/test/Examples/omp-in-reduction-clause.f90
new file mode 100644
index 000000000000..cf06b6dc1a8a
--- /dev/null
+++ b/flang/test/Examples/omp-in-reduction-clause.f90
@@ -0,0 +1,63 @@
+! REQUIRES: plugins, examples, shell
+
+! RUN: %flang_fc1 -load %llvmshlibdir/flangOmpReport.so -plugin flang-omp-report -fopenmp  %s -o - | FileCheck %s
+
+! Check for IN_REDUCTION() clause on OpenMP constructs
+
+subroutine omp_in_reduction_taskgroup()
+    integer :: z, i
+    !$omp taskgroup task_reduction(+:z)
+    !$omp task in_reduction(+:z)
+        z = z + 5
+    !$omp end task
+
+    !$omp taskloop in_reduction(+:z)
+        do i=1,10
+            z = z * 5
+        end do
+    !$omp end taskloop 
+    !$omp end taskgroup
+end subroutine omp_in_reduction_taskgroup
+
+!CHECK: - file:         {{.*}}
+!CHECK:   line:         10
+!CHECK:   construct:    task
+!CHECK:   clauses:
+!CHECK:     - clause:   in_reduction
+!CHECK:       details:  '+:z'
+!CHECK: - file:         {{.*}}
+!CHECK:   line:         14
+!CHECK:   construct:    taskloop
+!CHECK:   clauses:
+!CHECK:     - clause:   in_reduction
+!CHECK:       details:  '+:z'
+!CHECK: - file:         {{.*}}
+!CHECK:   line:         9
+!CHECK:   construct:    taskgroup
+!CHECK:   clauses:
+!CHECK:      - clause:  task_reduction
+!CHECK:        details: '+:z'
+
+subroutine omp_in_reduction_parallel()
+    integer :: z
+    !$omp parallel reduction(+:z)
+        !$omp taskloop simd in_reduction(+:z)
+            do i=1,10
+                z = z * 5
+            end do
+        !$omp end taskloop simd
+    !$omp end parallel
+end subroutine omp_in_reduction_parallel
+
+!CHECK: - file:         {{.*}}
+!CHECK:   line:         44
+!CHECK:   construct:    taskloop simd
+!CHECK:   clauses:
+!CHECK:     - clause:   in_reduction
+!CHECK:       details:  '+:z'
+!CHECK:  - file:        {{.*}}
+!CHECK:    line:        43
+!CHECK:    construct:   parallel
+!CHECK:    clauses:
+!CHECK:      - clause:  reduction
+!CHECK:        details: '+:z'

diff  --git a/flang/test/Parser/omp-in-reduction-clause.f90 b/flang/test/Parser/omp-in-reduction-clause.f90
new file mode 100644
index 000000000000..16f045771d73
--- /dev/null
+++ b/flang/test/Parser/omp-in-reduction-clause.f90
@@ -0,0 +1,79 @@
+! RUN: %flang_fc1 -fdebug-unparse -fopenmp %s | FileCheck --ignore-case %s
+! RUN: %flang_fc1 -fdebug-dump-parse-tree -fopenmp %s | FileCheck --check-prefix="PARSE-TREE" %s
+
+! Check for IN_REDUCTION() clause on OpenMP constructs
+
+subroutine omp_in_reduction_taskgroup()
+    integer :: z, i
+    !CHECK: !$OMP TASKGROUP  TASK_REDUCTION(+:z)
+    !$omp taskgroup task_reduction(+:z)
+    !CHECK-NEXT: !$OMP TASK  IN_REDUCTION(+:z)
+        !$omp task in_reduction(+:z)
+    !CHECK-NEXT: z=z+5_4
+            z = z + 5
+    !CHECK-NEXT: !$OMP END TASK
+        !$omp end task
+
+    !CHECK-NEXT: !$OMP TASKLOOP  IN_REDUCTION(+:z)
+        !$omp taskloop in_reduction(+:z)
+    !CHECK-NEXT: DO i=1_4,10_4
+            do i=1,10
+    !CHECK-NEXT: z=5_4*z
+                z = z * 5
+    !CHECK-NEXT: END DO
+            end do
+    !CHECK-NEXT: !$OMP END TASKLOOP
+        !$omp end taskloop
+    !CHECK-NEXT: !$OMP END TASKGROUP
+    !$omp end taskgroup
+end subroutine omp_in_reduction_taskgroup
+
+!PARSE-TREE: OpenMPConstruct -> OpenMPBlockConstruct
+!PARSE-TREE-NEXT: OmpBeginBlockDirective
+!PARSE-TREE-NEXT: OmpBlockDirective -> llvm::omp::Directive = taskgroup
+!PARSE-TREE-NEXT: OmpClauseList -> OmpClause -> TaskReduction -> OmpReductionClause
+
+!PARSE-TREE: OpenMPConstruct -> OpenMPBlockConstruct
+!PARSE-TREE-NEXT: OmpBeginBlockDirective
+!PARSE-TREE-NEXT: OmpBlockDirective -> llvm::omp::Directive = task
+!PARSE-TREE-NEXT: OmpClauseList -> OmpClause -> InReduction -> OmpInReductionClause
+!PARSE-TREE-NEXT: OmpReductionOperator -> DefinedOperator -> IntrinsicOperator = Add
+!PARSE-TREE-NEXT: OmpObjectList -> OmpObject -> Designator -> DataRef -> Name = 'z'
+
+!PARSE-TREE: OpenMPConstruct -> OpenMPLoopConstruct
+!PARSE-TREE-NEXT: OmpBeginLoopDirective
+!PARSE-TREE-NEXT: OmpLoopDirective -> llvm::omp::Directive = taskloop
+!PARSE-TREE-NEXT: OmpClauseList -> OmpClause -> InReduction -> OmpInReductionClause
+!PARSE-TREE-NEXT: OmpReductionOperator -> DefinedOperator -> IntrinsicOperator = Add
+!PARSE-TREE-NEXT: OmpObjectList -> OmpObject -> Designator -> DataRef -> Name = 'z'
+
+subroutine omp_in_reduction_parallel()
+    integer :: z
+    !CHECK: !$OMP PARALLEL  REDUCTION(+:z)
+    !$omp parallel reduction(+:z)
+    !CHECK-NEXT: !$OMP TASKLOOP SIMD  IN_REDUCTION(+:z)
+        !$omp taskloop simd in_reduction(+:z)
+    !CHECK-NEXT: DO i=1_4,10_4
+            do i=1,10
+    !CHECK-NEXT: z=5_4*z
+                z = z * 5
+    !CHECK-NEXT: END DO
+            end do
+    !CHECK-NEXT: !$OMP END TASKLOOP SIMD
+        !$omp end taskloop simd
+    !CHECK-NEXT: !$OMP END PARALLEL
+    !$omp end parallel
+end subroutine omp_in_reduction_parallel
+
+!PARSE-TREE: OpenMPConstruct -> OpenMPBlockConstruct
+!PARSE-TREE-NEXT: OmpBeginBlockDirective
+!PARSE-TREE-NEXT: OmpBlockDirective -> llvm::omp::Directive = parallel
+!PARSE-TREE-NEXT: OmpClauseList -> OmpClause -> Reduction -> OmpReductionClause
+
+!PARSE-TREE: OpenMPConstruct -> OpenMPLoopConstruct
+!PARSE-TREE-NEXT: OmpBeginLoopDirective
+!PARSE-TREE-NEXT: OmpLoopDirective -> llvm::omp::Directive = taskloop simd
+!PARSE-TREE-NEXT: OmpClauseList -> OmpClause -> InReduction -> OmpInReductionClause
+!PARSE-TREE-NEXT: OmpReductionOperator -> DefinedOperator -> IntrinsicOperator = Add
+!PASRE-TREE-NEXT: OmpObjectList -> OmpObject -> Designator -> DataRef -> Name = 'z'
+

diff  --git a/llvm/include/llvm/Frontend/OpenMP/OMP.td b/llvm/include/llvm/Frontend/OpenMP/OMP.td
index 4f0cc29a8b20..ba33b6c95f7a 100644
--- a/llvm/include/llvm/Frontend/OpenMP/OMP.td
+++ b/llvm/include/llvm/Frontend/OpenMP/OMP.td
@@ -283,6 +283,7 @@ def OMPC_TaskReduction : Clause<"task_reduction"> {
 }
 def OMPC_InReduction : Clause<"in_reduction"> {
   let clangClass = "OMPInReductionClause";
+  let flangClass = "OmpInReductionClause";
 }
 def OMPC_UnifiedAddress : Clause<"unified_address"> {
   let clangClass = "OMPUnifiedAddressClause";


        


More information about the llvm-commits mailing list