Skip to content

Commit 580c31f

Browse files
committed
[flang][openacc] Add initial support to lower private clause to the new design
This patch adds the initial infrastructure to lower the private clause to the new design introduced in D150622. The init region is not implemented yet and currently only yield the input argument (semantic is then similar with the previous design currently). Implementation will come in a follow up patch to keep patch. This patch also re-enable test commented out for `acc serial`, `acc serial loop`, `acc parallel` and `acc parallel loop` with private clause. Depends on D150972, D150973 Reviewed By: razvanlupusoru, jeanPerier Differential Revision: https://reviews.llvm.org/D150975
1 parent a7bbbc5 commit 580c31f

File tree

5 files changed

+120
-39
lines changed

5 files changed

+120
-39
lines changed

flang/lib/Lower/OpenACC.cpp

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,57 @@ static void genDataExitOperations(fir::FirOpBuilder &builder,
478478
}
479479
}
480480

481+
static mlir::acc::PrivateRecipeOp
482+
createBasePrivateRecipeOp(fir::FirOpBuilder &builder, mlir::Value input,
483+
llvm::StringRef recipeName, mlir::Location loc) {
484+
mlir::ModuleOp mod = builder.getModule();
485+
mlir::OpBuilder modBuilder(mod.getBodyRegion());
486+
mlir::Type ty = input.getType();
487+
auto recipe =
488+
modBuilder.create<mlir::acc::PrivateRecipeOp>(loc, recipeName, ty);
489+
builder.createBlock(&recipe.getInitRegion(), recipe.getInitRegion().end(),
490+
{ty}, {loc});
491+
builder.setInsertionPointToEnd(&recipe.getInitRegion().back());
492+
builder.create<mlir::acc::YieldOp>(
493+
loc, recipe.getInitRegion().front().getArgument(0));
494+
return recipe;
495+
}
496+
497+
static void
498+
genPrivatizations(const Fortran::parser::AccObjectList &objectList,
499+
Fortran::lower::AbstractConverter &converter,
500+
Fortran::semantics::SemanticsContext &semanticsContext,
501+
Fortran::lower::StatementContext &stmtCtx,
502+
llvm::SmallVectorImpl<mlir::Value> &dataOperands,
503+
llvm::SmallVector<mlir::Attribute> &privatizations) {
504+
fir::FirOpBuilder &builder = converter.getFirOpBuilder();
505+
mlir::ModuleOp mod = builder.getModule();
506+
for (const auto &accObject : objectList.v) {
507+
llvm::SmallVector<mlir::Value> bounds;
508+
std::stringstream asFortran;
509+
mlir::Location operandLocation = genOperandLocation(converter, accObject);
510+
mlir::Value baseAddr = gatherDataOperandAddrAndBounds(
511+
converter, builder, semanticsContext, stmtCtx, accObject,
512+
operandLocation, asFortran, bounds);
513+
514+
std::string recipeName = fir::getTypeAsString(
515+
baseAddr.getType(), converter.getKindMap(), "privatization");
516+
if (auto recipe =
517+
mod.lookupSymbol<mlir::acc::PrivateRecipeOp>(recipeName)) {
518+
privatizations.push_back(mlir::SymbolRefAttr::get(
519+
builder.getContext(), recipe.getSymName().str()));
520+
} else {
521+
auto crtPos = builder.saveInsertionPoint();
522+
mlir::acc::PrivateRecipeOp newRecipe = createBasePrivateRecipeOp(
523+
builder, baseAddr, recipeName, operandLocation);
524+
builder.restoreInsertionPoint(crtPos);
525+
privatizations.push_back(mlir::SymbolRefAttr::get(
526+
builder.getContext(), newRecipe.getSymName().str()));
527+
}
528+
dataOperands.push_back(baseAddr);
529+
}
530+
}
531+
481532
template <typename Clause>
482533
static void genObjectListWithModifier(
483534
const Clause *x, Fortran::lower::AbstractConverter &converter,
@@ -824,9 +875,9 @@ createComputeOp(Fortran::lower::AbstractConverter &converter,
824875
copyEntryOperands, copyoutEntryOperands, createEntryOperands,
825876
dataClauseOperands;
826877

827-
// TODO: need to more work/design.
828878
llvm::SmallVector<mlir::Value> reductionOperands, privateOperands,
829879
firstprivateOperands;
880+
llvm::SmallVector<mlir::Attribute> privatizations;
830881

831882
// Async, wait and self clause have optional values but can be present with
832883
// no value as well. When there is no value, the op has an attribute to
@@ -973,8 +1024,8 @@ createComputeOp(Fortran::lower::AbstractConverter &converter,
9731024
} else if (const auto *privateClause =
9741025
std::get_if<Fortran::parser::AccClause::Private>(
9751026
&clause.u)) {
976-
genObjectList(privateClause->v, converter, semanticsContext, stmtCtx,
977-
privateOperands);
1027+
genPrivatizations(privateClause->v, converter, semanticsContext, stmtCtx,
1028+
privateOperands, privatizations);
9781029
} else if (const auto *firstprivateClause =
9791030
std::get_if<Fortran::parser::AccClause::Firstprivate>(
9801031
&clause.u)) {
@@ -1019,6 +1070,12 @@ createComputeOp(Fortran::lower::AbstractConverter &converter,
10191070
if (addSelfAttr)
10201071
computeOp.setSelfAttrAttr(builder.getUnitAttr());
10211072

1073+
if constexpr (!std::is_same_v<Op, mlir::acc::KernelsOp>) {
1074+
if (!privatizations.empty())
1075+
computeOp.setPrivatizationsAttr(
1076+
mlir::ArrayAttr::get(builder.getContext(), privatizations));
1077+
}
1078+
10221079
auto insPt = builder.saveInsertionPoint();
10231080
builder.setInsertionPointAfter(computeOp);
10241081

flang/test/Lower/OpenACC/acc-parallel-loop.f90

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
! RUN: bbc -fopenacc -emit-fir %s -o - | FileCheck %s
44

5+
! CHECK-LABEL: acc.private.recipe @privatization_10xf32 : !fir.ref<!fir.array<10xf32>> init {
6+
! CHECK: ^bb0(%{{.*}}: !fir.ref<!fir.array<10xf32>>):
7+
! CHECK: acc.yield %{{.*}} : !fir.ref<!fir.array<10xf32>>
8+
! CHECK: }
9+
10+
! CHECK-LABEL: func.func @_QPacc_parallel_loop()
11+
512
subroutine acc_parallel_loop
613
integer :: i, j
714

@@ -442,19 +449,18 @@ subroutine acc_parallel_loop
442449
! CHECK: acc.yield
443450
! CHECK-NEXT: }{{$}}
444451

445-
! TODO: will be updated after lowering change in privatization to MLIR
446-
! !$acc parallel loop private(a) firstprivate(b)
447-
! DO i = 1, n
448-
! a(i) = b(i)
449-
! END DO
452+
!$acc parallel loop private(a) firstprivate(b)
453+
DO i = 1, n
454+
a(i) = b(i)
455+
END DO
450456

451-
! TODO: acc.parallel firstprivate(%[[B]] : !fir.ref<!fir.array<10xf32>>) private(%[[A]] : !fir.ref<!fir.array<10xf32>>) {
452-
! TODO: acc.loop private(%[[A]] : !fir.ref<!fir.array<10xf32>>) {
453-
! TODO: fir.do_loop
454-
! TODO: acc.yield
455-
! TODO-NEXT: }{{$}}
456-
! TODO: acc.yield
457-
! TODO-NEXT: }{{$}}
457+
! CHECK: acc.parallel firstprivate(%[[B]] : !fir.ref<!fir.array<10xf32>>) private(@privatization_10xf32 -> %[[A]] : !fir.ref<!fir.array<10xf32>>) {
458+
! CHECK: acc.loop private(%[[A]] : !fir.ref<!fir.array<10xf32>>) {
459+
! CHECK: fir.do_loop
460+
! CHECK: acc.yield
461+
! CHECK-NEXT: }{{$}}
462+
! CHECK: acc.yield
463+
! CHECK-NEXT: }{{$}}
458464

459465
!$acc parallel loop seq
460466
DO i = 1, n

flang/test/Lower/OpenACC/acc-parallel.f90

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
! RUN: bbc -fopenacc -emit-fir %s -o - | FileCheck %s
44

5+
! CHECK-LABEL: acc.private.recipe @privatization_10x10xf32 : !fir.ref<!fir.array<10x10xf32>> init {
6+
! CHECK: ^bb0(%{{.*}}: !fir.ref<!fir.array<10x10xf32>>):
7+
! CHECK: acc.yield %{{.*}} : !fir.ref<!fir.array<10x10xf32>>
8+
! CHECK: }
9+
10+
! CHECK-LABEL: func.func @_QPacc_parallel()
11+
512
subroutine acc_parallel
613
integer :: i, j
714

@@ -288,12 +295,11 @@ subroutine acc_parallel
288295
!CHECK: acc.detach accPtr(%[[ATTACH_D]] : !fir.ptr<f32>) {dataClause = 10 : i64, name = "d"}
289296
!CHECK: acc.detach accPtr(%[[ATTACH_E]] : !fir.ptr<f32>) {dataClause = 10 : i64, name = "e"}
290297

291-
! TODO: will be updated after lowering change in privatization to MLIR
292-
! !$acc parallel private(a) firstprivate(b) private(c)
293-
! !$acc end parallel
298+
!$acc parallel private(a) firstprivate(b) private(c)
299+
!$acc end parallel
294300

295-
!TODO: acc.parallel firstprivate(%[[B]] : !fir.ref<!fir.array<10x10xf32>>) private(%[[A]], %[[C]] : !fir.ref<!fir.array<10x10xf32>>, !fir.ref<!fir.array<10x10xf32>>) {
296-
!TODO: acc.yield
297-
!TODO-NEXT: }{{$}}
301+
! CHECK: acc.parallel firstprivate(%[[B]] : !fir.ref<!fir.array<10x10xf32>>) private(@privatization_10x10xf32 -> %[[A]] : !fir.ref<!fir.array<10x10xf32>>, @privatization_10x10xf32 -> %[[C]] : !fir.ref<!fir.array<10x10xf32>>) {
302+
! CHECK: acc.yield
303+
! CHECK-NEXT: }{{$}}
298304

299305
end subroutine acc_parallel

flang/test/Lower/OpenACC/acc-serial-loop.f90

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
! RUN: bbc -fopenacc -emit-fir %s -o - | FileCheck %s
44

5+
! CHECK-LABEL: acc.private.recipe @privatization_10xf32 : !fir.ref<!fir.array<10xf32>> init {
6+
! CHECK: ^bb0(%{{.*}}: !fir.ref<!fir.array<10xf32>>):
7+
! CHECK: acc.yield %{{.*}} : !fir.ref<!fir.array<10xf32>>
8+
! CHECK: }
9+
10+
! CHECK-LABEL: func.func @_QPacc_serial_loop()
11+
512
subroutine acc_serial_loop
613
integer :: i, j
714

@@ -358,19 +365,18 @@ subroutine acc_serial_loop
358365
! CHECK: acc.yield
359366
! CHECK-NEXT: }{{$}}
360367

361-
! TODO: update when lowering is updated to new private design
362-
! !$acc serial loop private(a) firstprivate(b)
363-
! DO i = 1, n
364-
! a(i) = b(i)
365-
! END DO
368+
!$acc serial loop private(a) firstprivate(b)
369+
DO i = 1, n
370+
a(i) = b(i)
371+
END DO
366372

367-
! TODO: acc.serial firstprivate(%[[B]] : !fir.ref<!fir.array<10xf32>>) private(%[[A]] : !fir.ref<!fir.array<10xf32>>) {
368-
! TODO: acc.loop private(%[[A]] : !fir.ref<!fir.array<10xf32>>) {
369-
! TODO: fir.do_loop
370-
! TODO: acc.yield
371-
! TODO-NEXT: }{{$}}
372-
! TODO: acc.yield
373-
! TODO-NEXT: }{{$}}
373+
! CHECK: acc.serial firstprivate(%[[B]] : !fir.ref<!fir.array<10xf32>>) private(@privatization_10xf32 -> %[[A]] : !fir.ref<!fir.array<10xf32>>) {
374+
! CHECK: acc.loop private(%[[A]] : !fir.ref<!fir.array<10xf32>>) {
375+
! CHECK: fir.do_loop
376+
! CHECK: acc.yield
377+
! CHECK-NEXT: }{{$}}
378+
! CHECK: acc.yield
379+
! CHECK-NEXT: }{{$}}
374380

375381
!$acc serial loop seq
376382
DO i = 1, n

flang/test/Lower/OpenACC/acc-serial.f90

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
! RUN: bbc -fopenacc -emit-fir %s -o - | FileCheck %s
44

5+
! CHECK-LABEL: acc.private.recipe @privatization_10x10xf32 : !fir.ref<!fir.array<10x10xf32>> init {
6+
! CHECK: ^bb0(%{{.*}}: !fir.ref<!fir.array<10x10xf32>>):
7+
! CHECK: acc.yield %{{.*}} : !fir.ref<!fir.array<10x10xf32>>
8+
! CHECK: }
9+
10+
! CHECK-LABEL: func.func @_QPacc_serial()
11+
512
subroutine acc_serial
613
integer :: i, j
714

@@ -231,12 +238,11 @@ subroutine acc_serial
231238
! CHECK: acc.detach accPtr(%[[ATTACH_D]] : !fir.ptr<f32>) {dataClause = 10 : i64, name = "d"}
232239
! CHECK: acc.detach accPtr(%[[ATTACH_E]] : !fir.ptr<f32>) {dataClause = 10 : i64, name = "e"}
233240

234-
! TODO: update when lowering is updated to new private design
235-
! !$acc serial private(a) firstprivate(b) private(c)
236-
! !$acc end serial
241+
!$acc serial private(a) firstprivate(b) private(c)
242+
!$acc end serial
237243

238-
! TODO: acc.serial firstprivate(%[[B]] : !fir.ref<!fir.array<10x10xf32>>) private(%[[A]], %[[C]] : !fir.ref<!fir.array<10x10xf32>>, !fir.ref<!fir.array<10x10xf32>>) {
239-
! TODO: acc.yield
240-
! TODO-NEXT: }{{$}}
244+
! CHECK: acc.serial firstprivate(%[[B]] : !fir.ref<!fir.array<10x10xf32>>) private(@privatization_10x10xf32 -> %[[A]] : !fir.ref<!fir.array<10x10xf32>>, @privatization_10x10xf32 -> %[[C]] : !fir.ref<!fir.array<10x10xf32>>) {
245+
! CHECK: acc.yield
246+
! CHECK-NEXT: }{{$}}
241247

242248
end subroutine

0 commit comments

Comments
 (0)