Skip to content

Commit

Permalink
[flang[OpenACC] Lower data directive
Browse files Browse the repository at this point in the history
This patch adds lowering for the `!$acc data`
from the PFT to OpenACC dialect.

This patch is part of the upstreaming effort from fir-dev branch.

Depends on D122384

Reviewed By: PeteSteinfeld

Differential Revision: https://reviews.llvm.org/D122398
  • Loading branch information
clementval committed Mar 24, 2022
1 parent 5dbb53b commit 5ee88e0
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 10 deletions.
17 changes: 7 additions & 10 deletions flang/lib/Lower/OpenACC.cpp
Expand Up @@ -514,24 +514,21 @@ genACCParallelOp(Fortran::lower::AbstractConverter &converter,
static void genACCDataOp(Fortran::lower::AbstractConverter &converter,
const Fortran::parser::AccClauseList &accClauseList) {
mlir::Value ifCond;
SmallVector<Value, 2> copyOperands, copyinOperands, copyinReadonlyOperands,
SmallVector<mlir::Value> copyOperands, copyinOperands, copyinReadonlyOperands,
copyoutOperands, copyoutZeroOperands, createOperands, createZeroOperands,
noCreateOperands, presentOperands, deviceptrOperands, attachOperands;

auto &firOpBuilder = converter.getFirOpBuilder();
auto currentLocation = converter.getCurrentLocation();
fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder();
mlir::Location currentLocation = converter.getCurrentLocation();
Fortran::lower::StatementContext stmtCtx;

// Lower clauses values mapped to operands.
// Keep track of each group of operands separatly as clauses can appear
// more than once.
for (const auto &clause : accClauseList.v) {
for (const Fortran::parser::AccClause &clause : accClauseList.v) {
if (const auto *ifClause =
std::get_if<Fortran::parser::AccClause::If>(&clause.u)) {
Value cond = fir::getBase(converter.genExprValue(
*Fortran::semantics::GetExpr(ifClause->v), stmtCtx));
ifCond = firOpBuilder.createConvert(currentLocation,
firOpBuilder.getI1Type(), cond);
genIfClause(converter, ifClause, ifCond, stmtCtx);
} else if (const auto *copyClause =
std::get_if<Fortran::parser::AccClause::Copy>(&clause.u)) {
genObjectList(copyClause->v, converter, copyOperands);
Expand Down Expand Up @@ -573,8 +570,8 @@ static void genACCDataOp(Fortran::lower::AbstractConverter &converter,
}

// Prepare the operand segement size attribute and the operands value range.
SmallVector<Value, 8> operands;
SmallVector<int32_t, 8> operandSegments;
SmallVector<mlir::Value> operands;
SmallVector<int32_t> operandSegments;
addOperand(operands, operandSegments, ifCond);
addOperands(operands, operandSegments, copyOperands);
addOperands(operands, operandSegments, copyinOperands);
Expand Down
97 changes: 97 additions & 0 deletions flang/test/Lower/OpenACC/acc-data.f90
@@ -0,0 +1,97 @@
! This test checks lowering of OpenACC data directive.

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

subroutine acc_data
real, dimension(10, 10) :: a, b, c
real, pointer :: d, e
logical :: ifCondition = .TRUE.

!CHECK: [[A:%.*]] = fir.alloca !fir.array<10x10xf32> {{{.*}}uniq_name = "{{.*}}Ea"}
!CHECK: [[B:%.*]] = fir.alloca !fir.array<10x10xf32> {{{.*}}uniq_name = "{{.*}}Eb"}
!CHECK: [[C:%.*]] = fir.alloca !fir.array<10x10xf32> {{{.*}}uniq_name = "{{.*}}Ec"}
!CHECK: [[D:%.*]] = fir.alloca !fir.box<!fir.ptr<f32>> {bindc_name = "d", uniq_name = "{{.*}}Ed"}
!CHECK: [[E:%.*]] = fir.alloca !fir.box<!fir.ptr<f32>> {bindc_name = "e", uniq_name = "{{.*}}Ee"}

!$acc data if(.TRUE.) copy(a)
!$acc end data

!CHECK: [[IF1:%.*]] = arith.constant true
!CHECK: acc.data if([[IF1]]) copy([[A]] : !fir.ref<!fir.array<10x10xf32>>) {
!CHECK: acc.terminator
!CHECK-NEXT: }{{$}}

!$acc data copy(a) if(ifCondition)
!$acc end data

!CHECK: [[IFCOND:%.*]] = fir.load %{{.*}} : !fir.ref<!fir.logical<4>>
!CHECK: [[IF2:%.*]] = fir.convert [[IFCOND]] : (!fir.logical<4>) -> i1
!CHECK: acc.data if([[IF2]]) copy([[A]] : !fir.ref<!fir.array<10x10xf32>>) {
!CHECK: acc.terminator
!CHECK-NEXT: }{{$}}

!$acc data copy(a, b, c)
!$acc end data

!CHECK: acc.data copy([[A]], [[B]], [[C]] : !fir.ref<!fir.array<10x10xf32>>, !fir.ref<!fir.array<10x10xf32>>, !fir.ref<!fir.array<10x10xf32>>) {
!CHECK: acc.terminator
!CHECK-NEXT: }{{$}}

!$acc data copy(a) copy(b) copy(c)
!$acc end data

!CHECK: acc.data copy([[A]], [[B]], [[C]] : !fir.ref<!fir.array<10x10xf32>>, !fir.ref<!fir.array<10x10xf32>>, !fir.ref<!fir.array<10x10xf32>>) {
!CHECK: acc.terminator
!CHECK-NEXT: }{{$}}

!$acc data copyin(a) copyin(readonly: b, c)
!$acc end data

!CHECK: acc.data copyin([[A]] : !fir.ref<!fir.array<10x10xf32>>) copyin_readonly([[B]], [[C]] : !fir.ref<!fir.array<10x10xf32>>, !fir.ref<!fir.array<10x10xf32>>) {
!CHECK: acc.terminator
!CHECK-NEXT: }{{$}}

!$acc data copyout(a) copyout(zero: b) copyout(c)
!$acc end data

!CHECK: acc.data copyout([[A]], [[C]] : !fir.ref<!fir.array<10x10xf32>>, !fir.ref<!fir.array<10x10xf32>>) copyout_zero([[B]] : !fir.ref<!fir.array<10x10xf32>>) {
!CHECK: acc.terminator
!CHECK-NEXT: }{{$}}

!$acc data create(a, b) create(zero: c)
!$acc end data

!CHECK: acc.data create([[A]], [[B]] : !fir.ref<!fir.array<10x10xf32>>, !fir.ref<!fir.array<10x10xf32>>) create_zero([[C]] : !fir.ref<!fir.array<10x10xf32>>) {
!CHECK: acc.terminator
!CHECK-NEXT: }{{$}}

!$acc data no_create(a, b) create(zero: c)
!$acc end data

!CHECK: acc.data create_zero([[C]] : !fir.ref<!fir.array<10x10xf32>>) no_create([[A]], [[B]] : !fir.ref<!fir.array<10x10xf32>>, !fir.ref<!fir.array<10x10xf32>>) {
!CHECK: acc.terminator
!CHECK-NEXT: }{{$}}

!$acc data present(a, b, c)
!$acc end data

!CHECK: acc.data present([[A]], [[B]], [[C]] : !fir.ref<!fir.array<10x10xf32>>, !fir.ref<!fir.array<10x10xf32>>, !fir.ref<!fir.array<10x10xf32>>) {
!CHECK: acc.terminator
!CHECK-NEXT: }{{$}}

!$acc data deviceptr(b, c)
!$acc end data

!CHECK: acc.data deviceptr([[B]], [[C]] : !fir.ref<!fir.array<10x10xf32>>, !fir.ref<!fir.array<10x10xf32>>) {
!CHECK: acc.terminator
!CHECK-NEXT: }{{$}}

!$acc data attach(d, e)
!$acc end data

!CHECK: acc.data attach([[D]], [[E]] : !fir.ref<!fir.box<!fir.ptr<f32>>>, !fir.ref<!fir.box<!fir.ptr<f32>>>) {
!CHECK: acc.terminator
!CHECK-NEXT: }{{$}}

end subroutine acc_data

0 comments on commit 5ee88e0

Please sign in to comment.