[flang-commits] [flang] [llvm] [Flang][OpenMP] Lowering Order clause to MLIR (PR #96730)

via flang-commits flang-commits at lists.llvm.org
Wed Jun 26 06:52:23 PDT 2024


https://github.com/harishch4 updated https://github.com/llvm/llvm-project/pull/96730

>From f41c39ec0a5c56b33dcfebf81c69271b3b3f5000 Mon Sep 17 00:00:00 2001
From: Harish Chambeti <harishcse44 at gmail.com>
Date: Wed, 26 Jun 2024 10:08:38 +0530
Subject: [PATCH 1/3] [Flang][OpenMP] Lowering Order clause to MLIR

---
 flang/lib/Lower/OpenMP/ClauseProcessor.cpp | 23 ++++++++
 flang/lib/Lower/OpenMP/ClauseProcessor.h   |  1 +
 flang/lib/Lower/OpenMP/OpenMP.cpp          | 12 +++--
 flang/test/Lower/OpenMP/order-clause.f90   | 62 ++++++++++++++++++++++
 4 files changed, 93 insertions(+), 5 deletions(-)
 create mode 100644 flang/test/Lower/OpenMP/order-clause.f90

diff --git a/flang/lib/Lower/OpenMP/ClauseProcessor.cpp b/flang/lib/Lower/OpenMP/ClauseProcessor.cpp
index 9efff0523d972..c021733378aa9 100644
--- a/flang/lib/Lower/OpenMP/ClauseProcessor.cpp
+++ b/flang/lib/Lower/OpenMP/ClauseProcessor.cpp
@@ -394,6 +394,29 @@ bool ClauseProcessor::processNumThreads(
   return false;
 }
 
+bool ClauseProcessor::processOrder(mlir::omp::OrderClauseOps &result) const {
+  if (auto *clause = findUniqueClause<omp::clause::Order>()) {
+    fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder();
+    result.orderAttr = mlir::omp::ClauseOrderKindAttr::get(
+        firOpBuilder.getContext(), mlir::omp::ClauseOrderKind::Concurrent);
+    using Order = omp::clause::Order;
+    const auto &modifier =
+        std::get<std::optional<Order::OrderModifier>>(clause->t);
+    if (modifier &&
+        *modifier == omp::clause::Order::OrderModifier::Unconstrained) {
+      result.orderModAttr = mlir::omp::OrderModifierAttr::get(
+          firOpBuilder.getContext(), mlir::omp::OrderModifier::unconstrained);
+    } else {
+      // "If order-modifier is not unconstrained, the behavior is as if the
+      // reproducible modifier is present."
+      result.orderModAttr = mlir::omp::OrderModifierAttr::get(
+          firOpBuilder.getContext(), mlir::omp::OrderModifier::reproducible);
+    }
+    return true;
+  }
+  return false;
+}
+
 bool ClauseProcessor::processOrdered(
     mlir::omp::OrderedClauseOps &result) const {
   if (auto *clause = findUniqueClause<omp::clause::Ordered>()) {
diff --git a/flang/lib/Lower/OpenMP/ClauseProcessor.h b/flang/lib/Lower/OpenMP/ClauseProcessor.h
index 5c9ab8baf82dd..53571ae5abc20 100644
--- a/flang/lib/Lower/OpenMP/ClauseProcessor.h
+++ b/flang/lib/Lower/OpenMP/ClauseProcessor.h
@@ -77,6 +77,7 @@ class ClauseProcessor {
                        mlir::omp::NumTeamsClauseOps &result) const;
   bool processNumThreads(lower::StatementContext &stmtCtx,
                          mlir::omp::NumThreadsClauseOps &result) const;
+  bool processOrder(mlir::omp::OrderClauseOps &result) const;
   bool processOrdered(mlir::omp::OrderedClauseOps &result) const;
   bool processPriority(lower::StatementContext &stmtCtx,
                        mlir::omp::PriorityClauseOps &result) const;
diff --git a/flang/lib/Lower/OpenMP/OpenMP.cpp b/flang/lib/Lower/OpenMP/OpenMP.cpp
index 23f27496091a5..25ef7dd448379 100644
--- a/flang/lib/Lower/OpenMP/OpenMP.cpp
+++ b/flang/lib/Lower/OpenMP/OpenMP.cpp
@@ -1109,13 +1109,14 @@ static void genSimdClauses(lower::AbstractConverter &converter,
   ClauseProcessor cp(converter, semaCtx, clauses);
   cp.processAligned(clauseOps);
   cp.processIf(llvm::omp::Directive::OMPD_simd, clauseOps);
+  cp.processOrder(clauseOps);
   cp.processReduction(loc, clauseOps);
   cp.processSafelen(clauseOps);
   cp.processSimdlen(clauseOps);
 
   // TODO Support delayed privatization.
-  cp.processTODO<clause::Allocate, clause::Linear, clause::Nontemporal,
-                 clause::Order>(loc, llvm::omp::Directive::OMPD_simd);
+  cp.processTODO<clause::Allocate, clause::Linear, clause::Nontemporal>(
+      loc, llvm::omp::Directive::OMPD_simd);
 }
 
 static void genSingleClauses(lower::AbstractConverter &converter,
@@ -1280,12 +1281,13 @@ static void genWsloopClauses(
     llvm::SmallVectorImpl<const semantics::Symbol *> &reductionSyms) {
   ClauseProcessor cp(converter, semaCtx, clauses);
   cp.processNowait(clauseOps);
+  cp.processOrder(clauseOps);
   cp.processOrdered(clauseOps);
   cp.processReduction(loc, clauseOps, &reductionTypes, &reductionSyms);
   cp.processSchedule(stmtCtx, clauseOps);
   // TODO Support delayed privatization.
 
-  cp.processTODO<clause::Allocate, clause::Linear, clause::Order>(
+  cp.processTODO<clause::Allocate, clause::Linear>(
       loc, llvm::omp::Directive::OMPD_do);
 }
 
@@ -2023,8 +2025,8 @@ static void genCompositeDoSimd(lower::AbstractConverter &converter,
                                ConstructQueue::iterator item) {
   ClauseProcessor cp(converter, semaCtx, item->clauses);
   cp.processTODO<clause::Aligned, clause::Allocate, clause::Linear,
-                 clause::Order, clause::Safelen, clause::Simdlen>(
-      loc, llvm::omp::OMPD_do_simd);
+                 clause::Safelen, clause::Simdlen>(loc,
+                                                   llvm::omp::OMPD_do_simd);
   // TODO: Add support for vectorization - add vectorization hints inside loop
   // body.
   // OpenMP standard does not specify the length of vector instructions.
diff --git a/flang/test/Lower/OpenMP/order-clause.f90 b/flang/test/Lower/OpenMP/order-clause.f90
new file mode 100644
index 0000000000000..6635fa659b427
--- /dev/null
+++ b/flang/test/Lower/OpenMP/order-clause.f90
@@ -0,0 +1,62 @@
+! This test checks lowering of OpenMP order clause.
+
+!RUN: %flang_fc1 -emit-hlfir -fopenmp -fopenmp-version=50 %s -o - | FileCheck %s
+
+!CHECK-LABEL:   func.func @_QPsimd_order() {
+subroutine simd_order
+   !CHECK: omp.simd order(reproducible:concurrent) {
+   !$omp simd order(concurrent)
+   do i = 1, 10
+   end do
+   !CHECK: omp.simd order(reproducible:concurrent) {
+   !$omp simd order(reproducible:concurrent)
+   do i = 1, 10
+   end do
+   !CHECK: omp.simd order(unconstrained:concurrent) {
+   !$omp simd order(unconstrained:concurrent)
+   do i = 1, 10
+   end do
+end subroutine simd_order
+
+!CHECK-LABEL:   func.func @_QPdo_order() {
+subroutine do_order
+   !CHECK: omp.wsloop order(reproducible:concurrent) {
+   !$omp do order(concurrent)
+   do i = 1, 10
+   end do
+   !CHECK: omp.wsloop order(reproducible:concurrent) {
+   !$omp do order(reproducible:concurrent)
+   do i = 1, 10
+   end do
+   !CHECK: omp.wsloop order(unconstrained:concurrent) {
+   !$omp do order(unconstrained:concurrent)
+   do i = 1, 10
+   end do
+end subroutine do_order
+
+!CHECK-LABEL:   func.func @_QPdo_simd_order() {
+subroutine do_simd_order
+   !CHECK: omp.wsloop order(reproducible:concurrent) {
+   !$omp do simd order(concurrent)
+   do i = 1, 10
+   end do
+   !CHECK: omp.wsloop order(reproducible:concurrent) {
+   !$omp do simd order(reproducible:concurrent)
+   do i = 1, 10
+   end do
+   !CHECK: omp.wsloop order(unconstrained:concurrent) {
+   !$omp do simd order(unconstrained:concurrent)
+   do i = 1, 10
+   end do
+end subroutine do_simd_order
+
+!CHECK-LABEL:   func.func @_QPdo_simd_order_parallel() {
+subroutine do_simd_order_parallel
+   !CHECK: omp.parallel {
+   !CHECK: omp.wsloop order(reproducible:concurrent) {
+   !$omp parallel
+   !$omp do simd order(reproducible:concurrent)
+   do i = 1, 10
+   end do
+   !$omp end parallel
+end subroutine do_simd_order_parallel

>From 52f9e74063a97f9f68463f2f06238dd0fb4c6081 Mon Sep 17 00:00:00 2001
From: Harish Chambeti <harishcse44 at gmail.com>
Date: Wed, 26 Jun 2024 18:16:49 +0530
Subject: [PATCH 2/3] Address review comments

---
 flang/lib/Lower/OpenMP/ClauseProcessor.cpp |  7 +++----
 flang/lib/Lower/OpenMP/OpenMP.cpp          |  1 +
 flang/test/Lower/OpenMP/order-clause.f90   | 20 +++++++++++++++++---
 llvm/include/llvm/Frontend/OpenMP/OMP.td   |  3 +++
 4 files changed, 24 insertions(+), 7 deletions(-)

diff --git a/flang/lib/Lower/OpenMP/ClauseProcessor.cpp b/flang/lib/Lower/OpenMP/ClauseProcessor.cpp
index c021733378aa9..0b4ecaac9d73c 100644
--- a/flang/lib/Lower/OpenMP/ClauseProcessor.cpp
+++ b/flang/lib/Lower/OpenMP/ClauseProcessor.cpp
@@ -395,15 +395,14 @@ bool ClauseProcessor::processNumThreads(
 }
 
 bool ClauseProcessor::processOrder(mlir::omp::OrderClauseOps &result) const {
-  if (auto *clause = findUniqueClause<omp::clause::Order>()) {
+  using Order = omp::clause::Order;
+  if (auto *clause = findUniqueClause<Order>()) {
     fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder();
     result.orderAttr = mlir::omp::ClauseOrderKindAttr::get(
         firOpBuilder.getContext(), mlir::omp::ClauseOrderKind::Concurrent);
-    using Order = omp::clause::Order;
     const auto &modifier =
         std::get<std::optional<Order::OrderModifier>>(clause->t);
-    if (modifier &&
-        *modifier == omp::clause::Order::OrderModifier::Unconstrained) {
+    if (modifier && *modifier == Order::OrderModifier::Unconstrained) {
       result.orderModAttr = mlir::omp::OrderModifierAttr::get(
           firOpBuilder.getContext(), mlir::omp::OrderModifier::unconstrained);
     } else {
diff --git a/flang/lib/Lower/OpenMP/OpenMP.cpp b/flang/lib/Lower/OpenMP/OpenMP.cpp
index 25ef7dd448379..0b33c2a62d388 100644
--- a/flang/lib/Lower/OpenMP/OpenMP.cpp
+++ b/flang/lib/Lower/OpenMP/OpenMP.cpp
@@ -1037,6 +1037,7 @@ static void genDistributeClauses(lower::AbstractConverter &converter,
   ClauseProcessor cp(converter, semaCtx, clauses);
   cp.processAllocate(clauseOps);
   cp.processDistSchedule(stmtCtx, clauseOps);
+  cp.processOrder(clauseOps);
   // TODO Support delayed privatization.
 }
 
diff --git a/flang/test/Lower/OpenMP/order-clause.f90 b/flang/test/Lower/OpenMP/order-clause.f90
index 6635fa659b427..3da6108fb4f07 100644
--- a/flang/test/Lower/OpenMP/order-clause.f90
+++ b/flang/test/Lower/OpenMP/order-clause.f90
@@ -54,9 +54,23 @@ end subroutine do_simd_order
 subroutine do_simd_order_parallel
    !CHECK: omp.parallel {
    !CHECK: omp.wsloop order(reproducible:concurrent) {
-   !$omp parallel
-   !$omp do simd order(reproducible:concurrent)
+   !$omp parallel do simd order(reproducible:concurrent)
    do i = 1, 10
    end do
-   !$omp end parallel
 end subroutine do_simd_order_parallel
+
+
+subroutine distribute_order
+   !CHECK: omp.distribute order(reproducible:concurrent) {
+   !$omp teams distribute order(concurrent)
+   do i=1,10
+   end do
+   !CHECK: omp.distribute order(reproducible:concurrent) {
+   !$omp teams distribute order(reproducible:concurrent)
+   do i=1,10
+   end do
+   !CHECK: omp.distribute order(unconstrained:concurrent) {
+   !$omp teams distribute order(unconstrained:concurrent)
+   do i = 1, 10
+   end do
+  end subroutine
\ No newline at end of file
diff --git a/llvm/include/llvm/Frontend/OpenMP/OMP.td b/llvm/include/llvm/Frontend/OpenMP/OMP.td
index c818448d3d190..005c678302b27 100644
--- a/llvm/include/llvm/Frontend/OpenMP/OMP.td
+++ b/llvm/include/llvm/Frontend/OpenMP/OMP.td
@@ -665,6 +665,7 @@ def OMP_Distribute : Directive<"distribute"> {
   let allowedOnceClauses = [
     VersionedClause<OMPC_Collapse>,
     VersionedClause<OMPC_DistSchedule>,
+    VersionedClause<OMPC_Order, 50>,
   ];
   let association = AS_Loop;
   let category = CA_Executable;
@@ -2057,6 +2058,7 @@ def OMP_TargetTeamsDistribute : Directive<"target teams distribute"> {
     VersionedClause<OMPC_NoWait>,
     VersionedClause<OMPC_NumTeams>,
     VersionedClause<OMPC_OMPX_DynCGroupMem>,
+    VersionedClause<OMPC_Order, 50>,
     VersionedClause<OMPC_ThreadLimit>,
   ];
   let leafConstructs = [OMP_Target, OMP_Teams, OMP_Distribute];
@@ -2330,6 +2332,7 @@ def OMP_TeamsDistribute : Directive<"teams distribute"> {
   ];
   let allowedOnceClauses = [
     VersionedClause<OMPC_If>,
+    VersionedClause<OMPC_Order, 50>,
   ];
   let leafConstructs = [OMP_Teams, OMP_Distribute];
   let category = CA_Executable;

>From dae55361c14e361209d104d7c2bdecca1cf4c492 Mon Sep 17 00:00:00 2001
From: harishch4 <harishcse44 at gmail.com>
Date: Wed, 26 Jun 2024 19:22:14 +0530
Subject: [PATCH 3/3] Update order-clause.f90

---
 flang/test/Lower/OpenMP/order-clause.f90 | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/flang/test/Lower/OpenMP/order-clause.f90 b/flang/test/Lower/OpenMP/order-clause.f90
index 3da6108fb4f07..717d9740c56f8 100644
--- a/flang/test/Lower/OpenMP/order-clause.f90
+++ b/flang/test/Lower/OpenMP/order-clause.f90
@@ -73,4 +73,4 @@ subroutine distribute_order
    !$omp teams distribute order(unconstrained:concurrent)
    do i = 1, 10
    end do
-  end subroutine
\ No newline at end of file
+end subroutine



More information about the flang-commits mailing list