Skip to content

Commit 1f734b0

Browse files
committed
[flang][OpenMP] Handle private/firstprivate clauses on sections construct
This patch adds private/firstprivate support for sections construct. For a source like the following: ``` !$omp sections private(x) firstprivate(y) !$omp section <block of code> !$omp section <block of code> !$omp end sections ``` ...privatization proceeds to privatize `x` and `y` accordingly inside each of the generated `omp.section` operations. Reviewed By: peixin Differential Revision: https://reviews.llvm.org/D131463
1 parent 95db373 commit 1f734b0

File tree

2 files changed

+56
-29
lines changed

2 files changed

+56
-29
lines changed

flang/lib/Lower/OpenMP.cpp

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,10 @@ static bool privatizeVars(Op &op, Fortran::lower::AbstractConverter &converter,
201201
}
202202

203203
bool needBarrier = false;
204-
firOpBuilder.setInsertionPointToStart(firOpBuilder.getAllocaBlock());
204+
if (mlir::isa<mlir::omp::SectionOp>(op))
205+
firOpBuilder.setInsertionPointToStart(&op.getRegion().back());
206+
else
207+
firOpBuilder.setInsertionPointToStart(firOpBuilder.getAllocaBlock());
205208
for (auto sym : privatizedSymbols) {
206209
privatizeSymbol(converter, sym, lastPrivBlock);
207210
if (sym->test(Fortran::semantics::Symbol::Flag::OmpFirstPrivate) &&
@@ -1326,12 +1329,28 @@ genOMP(Fortran::lower::AbstractConverter &converter,
13261329

13271330
auto &firOpBuilder = converter.getFirOpBuilder();
13281331
auto currentLocation = converter.getCurrentLocation();
1332+
const Fortran::parser::OpenMPConstruct *parentOmpConstruct =
1333+
eval.parentConstruct->getIf<Fortran::parser::OpenMPConstruct>();
1334+
assert(parentOmpConstruct &&
1335+
"No enclosing parent OpenMPConstruct on SECTION construct");
1336+
const Fortran::parser::OpenMPSectionsConstruct *sectionsConstruct =
1337+
std::get_if<Fortran::parser::OpenMPSectionsConstruct>(
1338+
&parentOmpConstruct->u);
1339+
assert(sectionsConstruct && "SECTION construct must have parent"
1340+
"SECTIONS construct");
1341+
const Fortran::parser::OmpClauseList &sectionsClauseList =
1342+
std::get<Fortran::parser::OmpClauseList>(
1343+
std::get<Fortran::parser::OmpBeginSectionsDirective>(
1344+
sectionsConstruct->t)
1345+
.t);
1346+
// Currently only private/firstprivate clause is handled, and
1347+
// all privatization is done within `omp.section` operations.
13291348
mlir::omp::SectionOp sectionOp =
13301349
firOpBuilder.create<mlir::omp::SectionOp>(currentLocation);
1331-
createBodyOfOp<omp::SectionOp>(sectionOp, converter, currentLocation, eval);
1350+
createBodyOfOp<omp::SectionOp>(sectionOp, converter, currentLocation, eval,
1351+
&sectionsClauseList);
13321352
}
13331353

1334-
// TODO: Add support for reduction
13351354
static void
13361355
genOMP(Fortran::lower::AbstractConverter &converter,
13371356
Fortran::lower::pft::Evaluation &eval,

flang/test/Lower/OpenMP/sections.f90

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,49 @@
44

55
!CHECK: func @_QQmain() {
66
!CHECK: %[[COUNT:.*]] = fir.address_of(@_QFEcount) : !fir.ref<i32>
7-
!CHECK: %[[DOUBLE_COUNT:.*]] = fir.address_of(@_QFEdouble_count) : !fir.ref<i32>
87
!CHECK: %[[ETA:.*]] = fir.alloca f32 {bindc_name = "eta", uniq_name = "_QFEeta"}
98
!CHECK: %[[CONST_1:.*]] = arith.constant 1 : i32
109
!CHECK: omp.sections allocate(%[[CONST_1]] : i32 -> %0 : !fir.ref<i32>) {
1110
!CHECK: omp.section {
12-
!CHECK: {{.*}} = arith.constant 5 : i32
13-
!CHECK: fir.store {{.*}} to {{.*}} : !fir.ref<i32>
14-
!CHECK: {{.*}} = fir.load %[[COUNT]] : !fir.ref<i32>
15-
!CHECK: {{.*}} = fir.load %[[DOUBLE_COUNT]] : !fir.ref<i32>
16-
!CHECK: {{.*}} = arith.muli {{.*}}, {{.*}} : i32
17-
!CHECK: {{.*}} = fir.convert {{.*}} : (i32) -> f32
18-
!CHECK: fir.store {{.*}} to %[[ETA]] : !fir.ref<f32>
11+
!CHECK: %[[PRIVATE_ETA:.*]] = fir.alloca f32 {bindc_name = "eta", pinned, uniq_name = "_QFEeta"}
12+
!CHECK: %[[PRIVATE_DOUBLE_COUNT:.*]] = fir.alloca i32 {bindc_name = "double_count", pinned, uniq_name = "_QFEdouble_count"}
13+
!CHECK: %[[const:.*]] = arith.constant 5 : i32
14+
!CHECK: fir.store %[[const]] to %[[COUNT]] : !fir.ref<i32>
15+
!CHECK: %[[temp_count:.*]] = fir.load %[[COUNT]] : !fir.ref<i32>
16+
!CHECK: %[[temp_double_count:.*]] = fir.load %[[PRIVATE_DOUBLE_COUNT]] : !fir.ref<i32>
17+
!CHECK: %[[result:.*]] = arith.muli %[[temp_count]], %[[temp_double_count]] : i32
18+
!CHECK: {{.*}} = fir.convert %[[result]] : (i32) -> f32
19+
!CHECK: fir.store {{.*}} to %[[PRIVATE_ETA]] : !fir.ref<f32>
1920
!CHECK: omp.terminator
2021
!CHECK: }
2122
!CHECK: omp.section {
22-
!CHECK: {{.*}} = fir.load %[[DOUBLE_COUNT]] : !fir.ref<i32>
23-
!CHECK: {{.*}} = arith.constant 1 : i32
24-
!CHECK: {{.*}} = arith.addi {{.*}} : i32
25-
!CHECK: fir.store {{.*}} to %[[DOUBLE_COUNT]] : !fir.ref<i32>
23+
!CHECK: %[[PRIVATE_ETA:.*]] = fir.alloca f32 {bindc_name = "eta", pinned, uniq_name = "_QFEeta"}
24+
!CHECK: %[[PRIVATE_DOUBLE_COUNT:.*]] = fir.alloca i32 {bindc_name = "double_count", pinned, uniq_name = "_QFEdouble_count"}
25+
!CHECK: %[[temp:.*]] = fir.load %[[PRIVATE_DOUBLE_COUNT]] : !fir.ref<i32>
26+
!CHECK: %[[const:.*]] = arith.constant 1 : i32
27+
!CHECK: %[[result:.*]] = arith.addi %[[temp]], %[[const]] : i32
28+
!CHECK: fir.store %[[result]] to %[[PRIVATE_DOUBLE_COUNT]] : !fir.ref<i32>
2629
!CHECK: omp.terminator
2730
!CHECK: }
2831
!CHECK: omp.section {
29-
!CHECK: {{.*}} = fir.load %[[ETA]] : !fir.ref<f32>
30-
!CHECK: {{.*}} = arith.constant 7.000000e+00 : f32
31-
!CHECK: {{.*}} = arith.subf {{.*}} : f32
32-
!CHECK: fir.store {{.*}} to %[[ETA]] : !fir.ref<f32>
32+
!CHECK: %[[PRIVATE_ETA:.*]] = fir.alloca f32 {bindc_name = "eta", pinned, uniq_name = "_QFEeta"}
33+
!CHECK: %[[PRIVATE_DOUBLE_COUNT:.*]] = fir.alloca i32 {bindc_name = "double_count", pinned, uniq_name = "_QFEdouble_count"}
34+
!CHECK: %[[temp:.*]] = fir.load %[[PRIVATE_ETA]] : !fir.ref<f32>
35+
!CHECK: %[[const:.*]] = arith.constant 7.000000e+00 : f32
36+
!CHECK: %[[result:.*]] = arith.subf %[[temp]], %[[const]] : f32
37+
!CHECK: fir.store %[[result]] to %[[PRIVATE_ETA]] : !fir.ref<f32>
3338
!CHECK: {{.*}} = fir.load %[[COUNT]] : !fir.ref<i32>
34-
!CHECK: {{.*}} = fir.convert {{.*}} : (i32) -> f32
35-
!CHECK: {{.*}} = fir.load %[[ETA]] : !fir.ref<f32>
36-
!CHECK: {{.*}} = arith.mulf {{.*}}, {{.*}} : f32
37-
!CHECK: {{.*}} = fir.convert {{.*}} : (f32) -> i32
38-
!CHECK: fir.store {{.*}} to %[[COUNT]] : !fir.ref<i32>
39+
!CHECK: %[[temp_count:.*]] = fir.convert {{.*}} : (i32) -> f32
40+
!CHECK: %[[temp_eta:.*]] = fir.load %[[PRIVATE_ETA]] : !fir.ref<f32>
41+
!CHECK: {{.*}} = arith.mulf %[[temp_count]], %[[temp_eta]] : f32
42+
!CHECK: %[[result:.*]] = fir.convert {{.*}} : (f32) -> i32
43+
!CHECK: fir.store %[[result]] to %[[COUNT]] : !fir.ref<i32>
3944
!CHECK: {{.*}} = fir.load %[[COUNT]] : !fir.ref<i32>
40-
!CHECK: {{.*}} = fir.convert {{.*}} : (i32) -> f32
41-
!CHECK: {{.*}} = fir.load %[[ETA]] : !fir.ref<f32>
42-
!CHECK: {{.*}} = arith.subf {{.*}}, {{.*}} : f32
43-
!CHECK: {{.*}} = fir.convert {{.*}} : (f32) -> i32
44-
!CHECK: fir.store {{.*}} to %[[DOUBLE_COUNT]] : !fir.ref<i32>
45+
!CHECK: %[[temp_count:.*]] = fir.convert {{.*}} : (i32) -> f32
46+
!CHECK: %[[temp_eta:.*]] = fir.load %[[PRIVATE_ETA]] : !fir.ref<f32>
47+
!CHECK: {{.*}} = arith.subf %[[temp_count]], %[[temp_eta]] : f32
48+
!CHECK: %[[result:.*]] = fir.convert {{.*}} : (f32) -> i32
49+
!CHECK: fir.store %[[result]] to %[[PRIVATE_DOUBLE_COUNT]] : !fir.ref<i32>
4550
!CHECK: omp.terminator
4651
!CHECK: }
4752
!CHECK: omp.terminator
@@ -74,6 +79,9 @@ end program sample
7479
!CHECK: func @_QPfirstprivate(%[[ARG:.*]]: !fir.ref<f32> {fir.bindc_name = "alpha"}) {
7580
!CHECK: omp.sections {
7681
!CHECK: omp.section {
82+
!CHECK: %[[PRIVATE_ALPHA:.*]] = fir.alloca f32 {bindc_name = "alpha", pinned, uniq_name = "_QFfirstprivateEalpha"}
83+
!CHECK: %[[temp:.*]] = fir.load %[[ARG]] : !fir.ref<f32>
84+
!CHECK: fir.store %[[temp]] to %[[PRIVATE_ALPHA]] : !fir.ref<f32>
7785
!CHECK: omp.terminator
7886
!CHECK: }
7987
!CHECK: omp.terminator

0 commit comments

Comments
 (0)