Skip to content

Commit 3d0b760

Browse files
committed
[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: flang-compiler#248 flang-compiler#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
1 parent 1cd1c1d commit 3d0b760

File tree

6 files changed

+146
-3
lines changed

6 files changed

+146
-3
lines changed

flang/include/flang/Lower/OpenMP.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
8+
//
9+
// Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/
10+
//
11+
//===----------------------------------------------------------------------===//
812

913
#ifndef FORTRAN_LOWER_OPENMP_H
1014
#define FORTRAN_LOWER_OPENMP_H

flang/include/flang/Optimizer/Dialect/FIRDialect.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ inline void registerFIR() {
3838
[[maybe_unused]] static bool init_once = [] {
3939
mlir::registerDialect<mlir::AffineDialect>();
4040
mlir::registerDialect<mlir::LLVM::LLVMDialect>();
41+
mlir::registerDialect<mlir::omp::OpenMPDialect>();
4142
mlir::registerDialect<mlir::scf::SCFDialect>();
4243
mlir::registerDialect<mlir::StandardOpsDialect>();
4344
mlir::registerDialect<mlir::vector::VectorDialect>();

flang/lib/Lower/OpenMP.cpp

Lines changed: 83 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,98 @@
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
8+
//
9+
// Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/
10+
//
11+
//===----------------------------------------------------------------------===//
812

913
#include "flang/Lower/OpenMP.h"
1014
#include "flang/Lower/Bridge.h"
15+
#include "flang/Lower/FIRBuilder.h"
1116
#include "flang/Lower/PFTBuilder.h"
1217
#include "flang/Parser/parse-tree.h"
18+
#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
19+
#include "llvm/Frontend/OpenMP/OMPConstants.h"
1320

1421
#define TODO() llvm_unreachable("not yet implemented")
1522

23+
static void genOMP(Fortran::lower::AbstractConverter &absConv,
24+
Fortran::lower::pft::Evaluation &eval,
25+
const Fortran::parser::OpenMPSimpleStandaloneConstruct
26+
&simpleStandaloneConstruct) {
27+
const auto &directive =
28+
std::get<Fortran::parser::OmpSimpleStandaloneDirective>(
29+
simpleStandaloneConstruct.t);
30+
switch (directive.v) {
31+
default:
32+
break;
33+
case llvm::omp::Directive::OMPD_barrier:
34+
absConv.getFirOpBuilder().create<mlir::omp::BarrierOp>(
35+
absConv.getCurrentLocation());
36+
break;
37+
case llvm::omp::Directive::OMPD_taskwait:
38+
TODO();
39+
case llvm::omp::Directive::OMPD_taskyield:
40+
TODO();
41+
case llvm::omp::Directive::OMPD_target_enter_data:
42+
TODO();
43+
case llvm::omp::Directive::OMPD_target_exit_data:
44+
TODO();
45+
case llvm::omp::Directive::OMPD_target_update:
46+
TODO();
47+
case llvm::omp::Directive::OMPD_ordered:
48+
TODO();
49+
}
50+
}
51+
52+
static void
53+
genOMP(Fortran::lower::AbstractConverter &absConv,
54+
Fortran::lower::pft::Evaluation &eval,
55+
const Fortran::parser::OpenMPStandaloneConstruct &standaloneConstruct) {
56+
std::visit(
57+
Fortran::common::visitors{
58+
[&](const Fortran::parser::OpenMPSimpleStandaloneConstruct
59+
&simpleStandaloneConstruct) {
60+
genOMP(absConv, eval, simpleStandaloneConstruct);
61+
},
62+
[&](const Fortran::parser::OpenMPFlushConstruct &flushConstruct) {
63+
TODO();
64+
},
65+
[&](const Fortran::parser::OpenMPCancelConstruct &cancelConstruct) {
66+
TODO();
67+
},
68+
[&](const Fortran::parser::OpenMPCancellationPointConstruct
69+
&cancellationPointConstruct) { TODO(); },
70+
},
71+
standaloneConstruct.u);
72+
}
73+
1674
void Fortran::lower::genOpenMPConstruct(
17-
Fortran::lower::AbstractConverter &, Fortran::lower::pft::Evaluation &,
18-
const Fortran::parser::OpenMPConstruct &) {
19-
TODO();
75+
Fortran::lower::AbstractConverter &absConv,
76+
Fortran::lower::pft::Evaluation &eval,
77+
const Fortran::parser::OpenMPConstruct &ompConstruct) {
78+
79+
std::visit(
80+
common::visitors{
81+
[&](const Fortran::parser::OpenMPStandaloneConstruct
82+
&standaloneConstruct) {
83+
genOMP(absConv, eval, standaloneConstruct);
84+
},
85+
[&](const Fortran::parser::OpenMPSectionsConstruct
86+
&sectionsConstruct) { TODO(); },
87+
[&](const Fortran::parser::OpenMPLoopConstruct &loopConstruct) {
88+
TODO();
89+
},
90+
[&](const Fortran::parser::OpenMPBlockConstruct &blockConstruct) {
91+
TODO();
92+
},
93+
[&](const Fortran::parser::OpenMPAtomicConstruct &atomicConstruct) {
94+
TODO();
95+
},
96+
[&](const Fortran::parser::OpenMPCriticalConstruct
97+
&criticalConstruct) { TODO(); },
98+
},
99+
ompConstruct.u);
20100
}
21101

22102
void Fortran::lower::genOpenMPEndLoop(

flang/unittests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ add_subdirectory(Optimizer)
99
add_subdirectory(Decimal)
1010
add_subdirectory(Evaluate)
1111
add_subdirectory(Runtime)
12+
add_subdirectory(Lower)

flang/unittests/Lower/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS)
2+
3+
set(LIBS
4+
MLIRLLVMIR
5+
${dialect_libs}
6+
)
7+
8+
add_flang_unittest(FlangLoweringOpenMPTests
9+
OpenMPLoweringTest.cpp
10+
)
11+
target_link_libraries(FlangLoweringOpenMPTests
12+
PRIVATE
13+
${LIBS})
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//===- OpenMPLoweringTest.cpp -- OpenMPLowering unit tests ----------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "gtest/gtest.h"
10+
#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
11+
#include "mlir/IR/Builders.h"
12+
#include "flang/Parser/parse-tree.h"
13+
#include "llvm/Frontend/OpenMP/OMPConstants.h"
14+
15+
class OpenMPLoweringTest : public testing::Test {
16+
protected:
17+
void SetUp() override {
18+
mlir::registerDialect<mlir::omp::OpenMPDialect>();
19+
mlir::registerAllDialects(&ctx);
20+
mlirOpBuilder.reset(new mlir::OpBuilder(&ctx));
21+
}
22+
23+
void TearDown() override { mlirOpBuilder.reset(); }
24+
25+
mlir::MLIRContext ctx;
26+
std::unique_ptr<mlir::OpBuilder> mlirOpBuilder;
27+
};
28+
29+
TEST_F(OpenMPLoweringTest, Barrier) {
30+
// Construct a dummy parse tree node for `!OMP barrier`.
31+
struct Fortran::parser::OmpSimpleStandaloneDirective barrierDirective(
32+
llvm::omp::Directive::OMPD_barrier);
33+
34+
// Check and lower the `!OMP barrier` node to `BarrierOp` operation of
35+
// OpenMPDialect.
36+
EXPECT_EQ(barrierDirective.v, llvm::omp::Directive::OMPD_barrier);
37+
auto barrierOp = mlirOpBuilder->create<mlir::omp::BarrierOp>(
38+
mlirOpBuilder->getUnknownLoc());
39+
40+
EXPECT_EQ(barrierOp.getOperationName(), "omp.barrier");
41+
EXPECT_EQ(succeeded(barrierOp.verify()), true);
42+
}
43+
44+
// main() from gtest_main

0 commit comments

Comments
 (0)