Skip to content

[flang][openacc] Lower update directive #528

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 80 additions & 1 deletion flang/lib/Lower/OpenACC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,85 @@ genACCExitDataOp(Fortran::lower::AbstractConverter &converter,
exitDataOp.finalizeAttr(firOpBuilder.getUnitAttr());
}

static void
genACCUpdateOp(Fortran::lower::AbstractConverter &converter,
const Fortran::parser::AccClauseList &accClauseList) {
mlir::Value ifCond, async, waitDevnum;
SmallVector<Value, 2> hostOperands, deviceOperands, waitOperands;

// Async and wait clause have optional values but can be present with
// no value as well. When there is no value, the op has an attribute to
// represent the clause.
bool addAsyncAttr = false;
bool addWaitAttr = false;
bool addIfPresentAttr = false;

auto &firOpBuilder = converter.getFirOpBuilder();
auto currentLocation = converter.getCurrentLocation();

// 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) {
if (const auto *asyncClause =
std::get_if<Fortran::parser::AccClause::Async>(&clause.u)) {
const auto &asyncClauseValue = asyncClause->v;
if (asyncClauseValue) { // async has a value.
async = fir::getBase(converter.genExprValue(
*Fortran::semantics::GetExpr(*asyncClauseValue)));
} else {
addAsyncAttr = true;
}
} else if (const auto *waitClause =
std::get_if<Fortran::parser::AccClause::Wait>(&clause.u)) {
const auto &waitClauseValue = waitClause->v;
if (waitClauseValue) { // wait has a value.
const Fortran::parser::AccWaitArgument &waitArg = *waitClauseValue;
const std::list<Fortran::parser::ScalarIntExpr> &waitList =
std::get<std::list<Fortran::parser::ScalarIntExpr>>(waitArg.t);
for (const Fortran::parser::ScalarIntExpr &value : waitList) {
mlir::Value v = fir::getBase(
converter.genExprValue(*Fortran::semantics::GetExpr(value)));
waitOperands.push_back(v);
}

const std::optional<Fortran::parser::ScalarIntExpr> &waitDevnumValue =
std::get<std::optional<Fortran::parser::ScalarIntExpr>>(waitArg.t);
if (waitDevnumValue)
waitDevnum = fir::getBase(converter.genExprValue(
*Fortran::semantics::GetExpr(*waitDevnumValue)));
} else {
addWaitAttr = true;
}
} else if (const auto *hostClause =
std::get_if<Fortran::parser::AccClause::Host>(&clause.u)) {
genObjectList(hostClause->v, converter, hostOperands);
} else if (const auto *deviceClause =
std::get_if<Fortran::parser::AccClause::Device>(&clause.u)) {
genObjectList(deviceClause->v, converter, deviceOperands);
}
}

// Prepare the operand segement size attribute and the operands value range.
SmallVector<mlir::Value, 10> operands;
SmallVector<int32_t, 5> operandSegments;
addOperand(operands, operandSegments, async);
addOperand(operands, operandSegments, waitDevnum);
addOperands(operands, operandSegments, waitOperands);
addOperands(operands, operandSegments, hostOperands);
addOperands(operands, operandSegments, deviceOperands);

auto updateOp = createSimpleOp<mlir::acc::UpdateOp>(
firOpBuilder, currentLocation, operands, operandSegments);

if (addAsyncAttr)
updateOp.asyncAttr(firOpBuilder.getUnitAttr());
if (addWaitAttr)
updateOp.waitAttr(firOpBuilder.getUnitAttr());
if (addIfPresentAttr)
updateOp.ifPresentAttr(firOpBuilder.getUnitAttr());
}

static void
genACC(Fortran::lower::AbstractConverter &converter,
Fortran::lower::pft::Evaluation &eval,
Expand All @@ -646,7 +725,7 @@ genACC(Fortran::lower::AbstractConverter &converter,
} else if (standaloneDirective.v == llvm::acc::Directive::ACCD_set) {
TODO("OpenACC set directive not lowered yet!");
} else if (standaloneDirective.v == llvm::acc::Directive::ACCD_update) {
TODO("OpenACC update directive not lowered yet!");
genACCUpdateOp(converter, accClauseList);
}
}

Expand Down
55 changes: 55 additions & 0 deletions flang/test/Lower/OpenACC/acc-update.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
! This test checks lowering of OpenACC update directive.

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

subroutine acc_update
integer :: async = 1
real, dimension(10, 10) :: a, b, c
logical :: ifCondition = .TRUE.

!CHECK: [[A:%.*]] = fir.alloca !fir.array<10x10xf32> {name = "{{.*}}Ea"}
!CHECK: [[B:%.*]] = fir.alloca !fir.array<10x10xf32> {name = "{{.*}}Eb"}
!CHECK: [[C:%.*]] = fir.alloca !fir.array<10x10xf32> {name = "{{.*}}Ec"}

!$acc update host(a)
!CHECK: acc.update host([[A]] : !fir.ref<!fir.array<10x10xf32>>){{$}}

!$acc update host(a) host(b) host(c)
!CHECK: acc.update host([[A]], [[B]], [[C]] : !fir.ref<!fir.array<10x10xf32>>, !fir.ref<!fir.array<10x10xf32>>, !fir.ref<!fir.array<10x10xf32>>){{$}}

!$acc update host(a) host(b) device(c)
!CHECK: acc.update host([[A]], [[B]] : !fir.ref<!fir.array<10x10xf32>>, !fir.ref<!fir.array<10x10xf32>>) device([[C]] : !fir.ref<!fir.array<10x10xf32>>){{$}}

!$acc update host(a) async
!CHECK: acc.update host([[A]] : !fir.ref<!fir.array<10x10xf32>>) attributes {async}

!$acc update host(a) wait
!CHECK: acc.update host([[A]] : !fir.ref<!fir.array<10x10xf32>>) attributes {wait}

!$acc update host(a) async wait
!CHECK: acc.update host([[A]] : !fir.ref<!fir.array<10x10xf32>>) attributes {async, wait}

!$acc update host(a) async(1)
!CHECK: [[ASYNC1:%.*]] = constant 1 : i32
!CHECK: acc.update async([[ASYNC1]] : i32) host([[A]] : !fir.ref<!fir.array<10x10xf32>>)

!$acc update host(a) async(async)
!CHECK: [[ASYNC2:%.*]] = fir.load %{{.*}} : !fir.ref<i32>
!CHECK: acc.update async([[ASYNC2]] : i32) host([[A]] : !fir.ref<!fir.array<10x10xf32>>)

!$acc update host(a) wait(1)
!CHECK: [[WAIT1:%.*]] = constant 1 : i32
!CHECK: acc.update wait([[WAIT1]] : i32) host([[A]] : !fir.ref<!fir.array<10x10xf32>>)

!$acc update host(a) wait(queues: 1, 2)
!CHECK: [[WAIT2:%.*]] = constant 1 : i32
!CHECK: [[WAIT3:%.*]] = constant 2 : i32
!CHECK: acc.update wait([[WAIT2]], [[WAIT3]] : i32, i32) host([[A]] : !fir.ref<!fir.array<10x10xf32>>)

!$acc update host(a) wait(devnum: 1: queues: 1, 2)
!CHECK: [[WAIT4:%.*]] = constant 1 : i32
!CHECK: [[WAIT5:%.*]] = constant 2 : i32
!CHECK: [[WAIT6:%.*]] = constant 1 : i32
!CHECK: acc.update wait_devnum([[WAIT6]] : i32) wait([[WAIT4]], [[WAIT5]] : i32, i32) host([[A]] : !fir.ref<!fir.array<10x10xf32>>)

end subroutine acc_update