Skip to content

Commit 5e9e085

Browse files
committed
[flang][OpenMP] Added support for lowering OpenMP barrier construct
This patch adds lowering support for OpenMP barrier construct to OpenMP Dialect operations. In order to support lowering, this patch registers OpenMPDialect and also adds OpenMPDialect to the legalizer.
1 parent c8f3f05 commit 5e9e085

File tree

4 files changed

+57
-3
lines changed

4 files changed

+57
-3
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ inline void registerFIR() {
4646
mlir::registerDialect<mlir::StandardOpsDialect>();
4747
mlir::registerDialect<mlir::vector::VectorDialect>();
4848
mlir::registerDialect<FIROpsDialect>();
49+
mlir::registerDialect<mlir::omp::OpenMPDialect>();
4950
return true;
5051
}();
5152
}

flang/lib/Lower/OpenMP.cpp

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,43 @@
88

99
#include "flang/Lower/OpenMP.h"
1010
#include "flang/Lower/Bridge.h"
11+
#include "flang/Lower/FIRBuilder.h"
1112
#include "flang/Lower/PFTBuilder.h"
1213
#include "flang/Parser/parse-tree.h"
14+
#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
15+
#include "mlir/IR/Builders.h"
16+
#include "mlir/IR/Module.h"
17+
#include "llvm/Support/CommandLine.h"
18+
#include "llvm/Support/ErrorHandling.h"
19+
#include "llvm/Support/MD5.h"
1320

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

1623
void Fortran::lower::genOpenMPConstruct(
17-
Fortran::lower::AbstractConverter &, Fortran::lower::pft::Evaluation &,
18-
const Fortran::parser::OpenMPConstruct &) {
19-
TODO();
24+
Fortran::lower::AbstractConverter &ABSConv,
25+
Fortran::lower::pft::Evaluation &Eval,
26+
const Fortran::parser::OpenMPConstruct &OMPConstruct) {
27+
if (auto StandaloneConstruct =
28+
std::get_if<Fortran::parser::OpenMPStandaloneConstruct>(
29+
&OMPConstruct.u)) {
30+
31+
if (auto SimpleStandaloneConstruct =
32+
std::get_if<Fortran::parser::OpenMPSimpleStandaloneConstruct>(
33+
&StandaloneConstruct->u)) {
34+
const auto &Directive{
35+
std::get<Fortran::parser::OmpSimpleStandaloneDirective>(
36+
SimpleStandaloneConstruct->t)};
37+
switch (Directive.v) {
38+
default:
39+
TODO();
40+
case parser::OmpSimpleStandaloneDirective::Directive::Barrier: {
41+
ABSConv.getFirOpBuilder().create<mlir::omp::BarrierOp>(
42+
ABSConv.getCurrentLocation());
43+
break;
44+
}
45+
}
46+
}
47+
}
2048
}
2149

2250
void Fortran::lower::genOpenMPEndLoop(

flang/lib/Optimizer/CodeGen/CodeGen.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2697,6 +2697,7 @@ struct FIRToLLVMLoweringPass
26972697
mlir::populateStdToLLVMConversionPatterns(typeConverter, pattern);
26982698
mlir::ConversionTarget target{*context};
26992699
target.addLegalDialect<mlir::LLVM::LLVMDialect>();
2700+
target.addLegalDialect<mlir::omp::OpenMPDialect>();
27002701

27012702
// required NOPs for applying a full conversion
27022703
target.addLegalOp<mlir::ModuleOp, mlir::ModuleTerminatorOp>();

flang/test/Lower/omp-barrier.f90

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
! This test checks lowering of OpenMP Barrier Directive.
2+
3+
! RUN: bbc -fopenmp -emit-fir %s -o - | \
4+
! RUN: FileCheck %s --check-prefix=FIRDialect
5+
! RUN: bbc -fopenmp -emit-llvm %s -o - | \
6+
! RUN: FileCheck %s --check-prefix=LLVMIRDialect
7+
! RUN: bbc -fopenmp -emit-llvm %s -o - | \
8+
! RUN: mlir-translate -mlir-to-llvmir - | FileCheck %s --check-prefix=LLVMIR
9+
10+
program barrier
11+
12+
integer :: a,b,c
13+
14+
!$OMP BARRIER
15+
!FIRDialect: omp.barrier
16+
!LLVMIRDialect: omp.barrier
17+
!LLVMIR: call void @__kmpc_barrier(%struct.ident_t* @1, i32 %omp_global_thread_num)
18+
c = a + b
19+
!$OMP BARRIER
20+
!FIRDialect: omp.barrier
21+
!LLVMIRDialect: omp.barrier
22+
!LLVMIR: call void @__kmpc_barrier(%struct.ident_t* @1, i32 %omp_global_thread_num1)
23+
24+
end program

0 commit comments

Comments
 (0)