[flang-commits] [flang] [Flang][OpenMP]Lowering support for Order clause (PR #93819)

via flang-commits flang-commits at lists.llvm.org
Tue Jun 25 08:13:57 PDT 2024


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

>From 372c19c83b39e0c4610920d6275b92e1171ffea2 Mon Sep 17 00:00:00 2001
From: Harish Chambeti <harishcse44 at gmail.com>
Date: Thu, 30 May 2024 18:59:58 +0530
Subject: [PATCH] [Flang][OpenMP]Lowering support for Order clause

---
 flang/lib/Lower/OpenMP/ClauseProcessor.cpp | 23 ++++++++
 flang/lib/Lower/OpenMP/ClauseProcessor.h   |  1 +
 flang/lib/Lower/OpenMP/OpenMP.cpp          | 13 +++--
 flang/test/Lower/OpenMP/order-clause.f90   | 62 ++++++++++++++++++++++
 4 files changed, 92 insertions(+), 7 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 68619f699ebb2..c6a9d7f20835f 100644
--- a/flang/lib/Lower/OpenMP/ClauseProcessor.cpp
+++ b/flang/lib/Lower/OpenMP/ClauseProcessor.cpp
@@ -999,6 +999,29 @@ bool ClauseProcessor::processUseDevicePtr(
       });
 }
 
+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;
+}
+
 } // namespace omp
 } // namespace lower
 } // namespace Fortran
diff --git a/flang/lib/Lower/OpenMP/ClauseProcessor.h b/flang/lib/Lower/OpenMP/ClauseProcessor.h
index 4d3d4448e8f03..2d7d53453f48f 100644
--- a/flang/lib/Lower/OpenMP/ClauseProcessor.h
+++ b/flang/lib/Lower/OpenMP/ClauseProcessor.h
@@ -139,6 +139,7 @@ class ClauseProcessor {
   bool processMotionClauses(lower::StatementContext &stmtCtx,
                             mlir::omp::MapClauseOps &result);
 
+  bool processOrder(mlir::omp::OrderClauseOps &result) const;
   // Call this method for these clauses that should be supported but are not
   // implemented yet. It triggers a compilation error if any of the given
   // clauses is found.
diff --git a/flang/lib/Lower/OpenMP/OpenMP.cpp b/flang/lib/Lower/OpenMP/OpenMP.cpp
index 9598457d123cf..1975846c83613 100644
--- a/flang/lib/Lower/OpenMP/OpenMP.cpp
+++ b/flang/lib/Lower/OpenMP/OpenMP.cpp
@@ -1007,10 +1007,9 @@ static void genSimdClauses(lower::AbstractConverter &converter,
   cp.processSafelen(clauseOps);
   cp.processSimdlen(clauseOps);
   // TODO Support delayed privatization.
-
+  cp.processOrder(clauseOps);
   cp.processTODO<clause::Aligned, clause::Allocate, clause::Linear,
-                 clause::Nontemporal, clause::Order>(
-      loc, llvm::omp::Directive::OMPD_simd);
+                 clause::Nontemporal>(loc, llvm::omp::Directive::OMPD_simd);
 }
 
 static void genSingleClauses(lower::AbstractConverter &converter,
@@ -1176,8 +1175,8 @@ static void genWsloopClauses(
   cp.processReduction(loc, clauseOps, &reductionTypes, &reductionSyms);
   cp.processSchedule(stmtCtx, clauseOps);
   // TODO Support delayed privatization.
-
-  cp.processTODO<clause::Allocate, clause::Linear, clause::Order>(
+  cp.processOrder(clauseOps);
+  cp.processTODO<clause::Allocate, clause::Linear>(
       loc, llvm::omp::Directive::OMPD_do);
 }
 
@@ -1855,8 +1854,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



More information about the flang-commits mailing list