Skip to content

Commit 7d3527d

Browse files
authored
Merge pull request #248 from SouraVX/barrier-lowering
[flang][OpenMP] Added support for lowering OpenMP barrier construct
2 parents b0a1260 + 8f30317 commit 7d3527d

File tree

5 files changed

+112
-3
lines changed

5 files changed

+112
-3
lines changed

flang/include/flang/Lower/OpenMP.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
namespace Fortran {
1717
namespace parser {
1818
struct OpenMPConstruct;
19+
struct OpenMPStandaloneConstruct;
20+
struct OpenMPSimpleStandaloneConstruct;
1921
struct OmpEndLoopDirective;
2022
} // namespace parser
2123

@@ -30,6 +32,12 @@ struct Evaluation;
3032
void genOpenMPConstruct(AbstractConverter &, pft::Evaluation &,
3133
const parser::OpenMPConstruct &);
3234

35+
void genOMP(AbstractConverter &, pft::Evaluation &,
36+
const parser::OpenMPStandaloneConstruct &);
37+
38+
void genOMP(AbstractConverter &, pft::Evaluation &,
39+
const parser::OpenMPSimpleStandaloneConstruct &);
40+
3341
void genOpenMPEndLoop(AbstractConverter &, pft::Evaluation &,
3442
const parser::OmpEndLoopDirective &);
3543

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: 78 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,90 @@
1212

1313
#include "flang/Lower/OpenMP.h"
1414
#include "flang/Lower/Bridge.h"
15+
#include "flang/Lower/FIRBuilder.h"
1516
#include "flang/Lower/PFTBuilder.h"
1617
#include "flang/Parser/parse-tree.h"
18+
#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
1719

1820
#define TODO() llvm_unreachable("not yet implemented")
1921

22+
void Fortran::lower::genOMP(
23+
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+
32+
case parser::OmpSimpleStandaloneDirective::Directive::Barrier:
33+
absConv.getFirOpBuilder().create<mlir::omp::BarrierOp>(
34+
absConv.getCurrentLocation());
35+
break;
36+
case parser::OmpSimpleStandaloneDirective::Directive::Taskwait:
37+
TODO();
38+
case parser::OmpSimpleStandaloneDirective::Directive::Taskyield:
39+
TODO();
40+
case parser::OmpSimpleStandaloneDirective::Directive::TargetEnterData:
41+
TODO();
42+
case parser::OmpSimpleStandaloneDirective::Directive::TargetExitData:
43+
TODO();
44+
case parser::OmpSimpleStandaloneDirective::Directive::TargetUpdate:
45+
TODO();
46+
case parser::OmpSimpleStandaloneDirective::Directive::Ordered:
47+
TODO();
48+
}
49+
}
50+
51+
void Fortran::lower::genOMP(
52+
Fortran::lower::AbstractConverter &absConv,
53+
Fortran::lower::pft::Evaluation &eval,
54+
const Fortran::parser::OpenMPStandaloneConstruct &standaloneConstruct) {
55+
std::visit(
56+
common::visitors{
57+
[&](const Fortran::parser::OpenMPSimpleStandaloneConstruct
58+
&simpleStandaloneConstruct) {
59+
genOMP(absConv, eval, simpleStandaloneConstruct);
60+
},
61+
[&](const Fortran::parser::OpenMPFlushConstruct &flushConstruct) {
62+
TODO();
63+
},
64+
[&](const Fortran::parser::OpenMPCancelConstruct &cancelConstruct) {
65+
TODO();
66+
},
67+
[&](const Fortran::parser::OpenMPCancellationPointConstruct
68+
&cancellationPointConstruct) { TODO(); },
69+
},
70+
standaloneConstruct.u);
71+
}
72+
2073
void Fortran::lower::genOpenMPConstruct(
21-
Fortran::lower::AbstractConverter &, Fortran::lower::pft::Evaluation &,
22-
const Fortran::parser::OpenMPConstruct &) {
23-
TODO();
74+
Fortran::lower::AbstractConverter &absConv,
75+
Fortran::lower::pft::Evaluation &eval,
76+
const Fortran::parser::OpenMPConstruct &ompConstruct) {
77+
78+
std::visit(
79+
common::visitors{
80+
[&](const Fortran::parser::OpenMPStandaloneConstruct
81+
&standaloneConstruct) {
82+
genOMP(absConv, eval, standaloneConstruct);
83+
},
84+
[&](const Fortran::parser::OpenMPSectionsConstruct
85+
&sectionsConstruct) { TODO(); },
86+
[&](const Fortran::parser::OpenMPLoopConstruct &loopConstruct) {
87+
TODO();
88+
},
89+
[&](const Fortran::parser::OpenMPBlockConstruct &blockConstruct) {
90+
TODO();
91+
},
92+
[&](const Fortran::parser::OpenMPAtomicConstruct &atomicConstruct) {
93+
TODO();
94+
},
95+
[&](const Fortran::parser::OpenMPCriticalConstruct
96+
&criticalConstruct) { TODO(); },
97+
},
98+
ompConstruct.u);
2499
}
25100

26101
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-fir %s -o - | \
8+
! RUN: tco | 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)