[flang-commits] [flang] 3d0b760 - [flang][OpenMP] upstream OpenMP lowering

Sourabh Singh Tomar via flang-commits flang-commits at lists.llvm.org
Tue Jul 14 05:31:55 PDT 2020


Author: Sourabh Singh Tomar
Date: 2020-07-14T18:01:15+05:30
New Revision: 3d0b76022df60a8ceaa2198012d67eab3ec3a2d9

URL: https://github.com/llvm/llvm-project/commit/3d0b76022df60a8ceaa2198012d67eab3ec3a2d9
DIFF: https://github.com/llvm/llvm-project/commit/3d0b76022df60a8ceaa2198012d67eab3ec3a2d9.diff

LOG: [flang][OpenMP] upstream OpenMP lowering

Summary:
This patch implements lowering of OpenMP barrier construct from
pft to OpenMPDialect.

Patch is carved out of following merged PR's from fir-dev branch
of https://github.com/flang-compiler/f18-llvm-project/

PR's:
https://github.com/flang-compiler/f18-llvm-project/pull/248
https://github.com/flang-compiler/f18-llvm-project/pull/251

Unfortunately primary tool `bbc` for functional validation is not
yet upstreamed. So this patch includes a unittest for lowering
`!OMP barrier` construct.

Some part of the these PR's still remains downstream(functional test
and dialect registration to legalizer) for obvious reasons.
Will upstream them when the dependencies are upstreamed.

Reviewed By: schweitz, kiranchandramohan

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

Added: 
    flang/unittests/Lower/CMakeLists.txt
    flang/unittests/Lower/OpenMPLoweringTest.cpp

Modified: 
    flang/include/flang/Lower/OpenMP.h
    flang/include/flang/Optimizer/Dialect/FIRDialect.h
    flang/lib/Lower/OpenMP.cpp
    flang/unittests/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/flang/include/flang/Lower/OpenMP.h b/flang/include/flang/Lower/OpenMP.h
index 0b273a6aa734..13dd43b60fde 100644
--- a/flang/include/flang/Lower/OpenMP.h
+++ b/flang/include/flang/Lower/OpenMP.h
@@ -5,6 +5,10 @@
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
 //===----------------------------------------------------------------------===//
+//
+// Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/
+//
+//===----------------------------------------------------------------------===//
 
 #ifndef FORTRAN_LOWER_OPENMP_H
 #define FORTRAN_LOWER_OPENMP_H

diff  --git a/flang/include/flang/Optimizer/Dialect/FIRDialect.h b/flang/include/flang/Optimizer/Dialect/FIRDialect.h
index 963dad8a09c4..1c06fe5ab060 100644
--- a/flang/include/flang/Optimizer/Dialect/FIRDialect.h
+++ b/flang/include/flang/Optimizer/Dialect/FIRDialect.h
@@ -38,6 +38,7 @@ inline void registerFIR() {
   [[maybe_unused]] static bool init_once = [] {
     mlir::registerDialect<mlir::AffineDialect>();
     mlir::registerDialect<mlir::LLVM::LLVMDialect>();
+    mlir::registerDialect<mlir::omp::OpenMPDialect>();
     mlir::registerDialect<mlir::scf::SCFDialect>();
     mlir::registerDialect<mlir::StandardOpsDialect>();
     mlir::registerDialect<mlir::vector::VectorDialect>();

diff  --git a/flang/lib/Lower/OpenMP.cpp b/flang/lib/Lower/OpenMP.cpp
index 5eb6a1866d29..c476f1b0d7d3 100644
--- a/flang/lib/Lower/OpenMP.cpp
+++ b/flang/lib/Lower/OpenMP.cpp
@@ -5,18 +5,98 @@
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
 //===----------------------------------------------------------------------===//
+//
+// Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/
+//
+//===----------------------------------------------------------------------===//
 
 #include "flang/Lower/OpenMP.h"
 #include "flang/Lower/Bridge.h"
+#include "flang/Lower/FIRBuilder.h"
 #include "flang/Lower/PFTBuilder.h"
 #include "flang/Parser/parse-tree.h"
+#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
+#include "llvm/Frontend/OpenMP/OMPConstants.h"
 
 #define TODO() llvm_unreachable("not yet implemented")
 
+static void genOMP(Fortran::lower::AbstractConverter &absConv,
+                   Fortran::lower::pft::Evaluation &eval,
+                   const Fortran::parser::OpenMPSimpleStandaloneConstruct
+                       &simpleStandaloneConstruct) {
+  const auto &directive =
+      std::get<Fortran::parser::OmpSimpleStandaloneDirective>(
+          simpleStandaloneConstruct.t);
+  switch (directive.v) {
+  default:
+    break;
+  case llvm::omp::Directive::OMPD_barrier:
+    absConv.getFirOpBuilder().create<mlir::omp::BarrierOp>(
+        absConv.getCurrentLocation());
+    break;
+  case llvm::omp::Directive::OMPD_taskwait:
+    TODO();
+  case llvm::omp::Directive::OMPD_taskyield:
+    TODO();
+  case llvm::omp::Directive::OMPD_target_enter_data:
+    TODO();
+  case llvm::omp::Directive::OMPD_target_exit_data:
+    TODO();
+  case llvm::omp::Directive::OMPD_target_update:
+    TODO();
+  case llvm::omp::Directive::OMPD_ordered:
+    TODO();
+  }
+}
+
+static void
+genOMP(Fortran::lower::AbstractConverter &absConv,
+       Fortran::lower::pft::Evaluation &eval,
+       const Fortran::parser::OpenMPStandaloneConstruct &standaloneConstruct) {
+  std::visit(
+      Fortran::common::visitors{
+          [&](const Fortran::parser::OpenMPSimpleStandaloneConstruct
+                  &simpleStandaloneConstruct) {
+            genOMP(absConv, eval, simpleStandaloneConstruct);
+          },
+          [&](const Fortran::parser::OpenMPFlushConstruct &flushConstruct) {
+            TODO();
+          },
+          [&](const Fortran::parser::OpenMPCancelConstruct &cancelConstruct) {
+            TODO();
+          },
+          [&](const Fortran::parser::OpenMPCancellationPointConstruct
+                  &cancellationPointConstruct) { TODO(); },
+      },
+      standaloneConstruct.u);
+}
+
 void Fortran::lower::genOpenMPConstruct(
-    Fortran::lower::AbstractConverter &, Fortran::lower::pft::Evaluation &,
-    const Fortran::parser::OpenMPConstruct &) {
-  TODO();
+    Fortran::lower::AbstractConverter &absConv,
+    Fortran::lower::pft::Evaluation &eval,
+    const Fortran::parser::OpenMPConstruct &ompConstruct) {
+
+  std::visit(
+      common::visitors{
+          [&](const Fortran::parser::OpenMPStandaloneConstruct
+                  &standaloneConstruct) {
+            genOMP(absConv, eval, standaloneConstruct);
+          },
+          [&](const Fortran::parser::OpenMPSectionsConstruct
+                  &sectionsConstruct) { TODO(); },
+          [&](const Fortran::parser::OpenMPLoopConstruct &loopConstruct) {
+            TODO();
+          },
+          [&](const Fortran::parser::OpenMPBlockConstruct &blockConstruct) {
+            TODO();
+          },
+          [&](const Fortran::parser::OpenMPAtomicConstruct &atomicConstruct) {
+            TODO();
+          },
+          [&](const Fortran::parser::OpenMPCriticalConstruct
+                  &criticalConstruct) { TODO(); },
+      },
+      ompConstruct.u);
 }
 
 void Fortran::lower::genOpenMPEndLoop(

diff  --git a/flang/unittests/CMakeLists.txt b/flang/unittests/CMakeLists.txt
index 440ab4e0358d..d53d155f2f2b 100644
--- a/flang/unittests/CMakeLists.txt
+++ b/flang/unittests/CMakeLists.txt
@@ -9,3 +9,4 @@ add_subdirectory(Optimizer)
 add_subdirectory(Decimal)
 add_subdirectory(Evaluate)
 add_subdirectory(Runtime)
+add_subdirectory(Lower)

diff  --git a/flang/unittests/Lower/CMakeLists.txt b/flang/unittests/Lower/CMakeLists.txt
new file mode 100644
index 000000000000..19535e8f4534
--- /dev/null
+++ b/flang/unittests/Lower/CMakeLists.txt
@@ -0,0 +1,13 @@
+get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS)
+
+set(LIBS
+  MLIRLLVMIR
+  ${dialect_libs}
+)
+
+add_flang_unittest(FlangLoweringOpenMPTests
+	OpenMPLoweringTest.cpp
+)
+target_link_libraries(FlangLoweringOpenMPTests
+  PRIVATE
+  ${LIBS})

diff  --git a/flang/unittests/Lower/OpenMPLoweringTest.cpp b/flang/unittests/Lower/OpenMPLoweringTest.cpp
new file mode 100644
index 000000000000..0942b9deab98
--- /dev/null
+++ b/flang/unittests/Lower/OpenMPLoweringTest.cpp
@@ -0,0 +1,44 @@
+//===- OpenMPLoweringTest.cpp -- OpenMPLowering unit tests ----------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "gtest/gtest.h"
+#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
+#include "mlir/IR/Builders.h"
+#include "flang/Parser/parse-tree.h"
+#include "llvm/Frontend/OpenMP/OMPConstants.h"
+
+class OpenMPLoweringTest : public testing::Test {
+protected:
+  void SetUp() override {
+    mlir::registerDialect<mlir::omp::OpenMPDialect>();
+    mlir::registerAllDialects(&ctx);
+    mlirOpBuilder.reset(new mlir::OpBuilder(&ctx));
+  }
+
+  void TearDown() override { mlirOpBuilder.reset(); }
+
+  mlir::MLIRContext ctx;
+  std::unique_ptr<mlir::OpBuilder> mlirOpBuilder;
+};
+
+TEST_F(OpenMPLoweringTest, Barrier) {
+  // Construct a dummy parse tree node for `!OMP barrier`.
+  struct Fortran::parser::OmpSimpleStandaloneDirective barrierDirective(
+      llvm::omp::Directive::OMPD_barrier);
+
+  // Check and lower the `!OMP barrier` node to `BarrierOp` operation of
+  // OpenMPDialect.
+  EXPECT_EQ(barrierDirective.v, llvm::omp::Directive::OMPD_barrier);
+  auto barrierOp = mlirOpBuilder->create<mlir::omp::BarrierOp>(
+      mlirOpBuilder->getUnknownLoc());
+
+  EXPECT_EQ(barrierOp.getOperationName(), "omp.barrier");
+  EXPECT_EQ(succeeded(barrierOp.verify()), true);
+}
+
+// main() from gtest_main


        


More information about the flang-commits mailing list