diff --git a/flang/lib/Lower/OpenACC.cpp b/flang/lib/Lower/OpenACC.cpp --- a/flang/lib/Lower/OpenACC.cpp +++ b/flang/lib/Lower/OpenACC.cpp @@ -911,6 +911,81 @@ } } +static void genACC(Fortran::lower::AbstractConverter &converter, + Fortran::lower::pft::Evaluation &eval, + const Fortran::parser::OpenACCWaitConstruct &waitConstruct) { + + const auto &waitArgument = + std::get>( + waitConstruct.t); + const auto &accClauseList = + std::get(waitConstruct.t); + + mlir::Value ifCond, asyncOperand, waitDevnum, async; + SmallVector waitOperands; + + // Async 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; + + auto &firOpBuilder = converter.getFirOpBuilder(); + auto currentLocation = converter.getCurrentLocation(); + + if (waitArgument) { // wait has a value. + const Fortran::parser::AccWaitArgument &waitArg = *waitArgument; + const std::list &waitList = + std::get>(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 &waitDevnumValue = + std::get>(waitArg.t); + if (waitDevnumValue) + waitDevnum = fir::getBase(converter.genExprValue( + *Fortran::semantics::GetExpr(*waitDevnumValue))); + } + + // 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 *ifClause = + std::get_if(&clause.u)) { + mlir::Value cond = fir::getBase( + converter.genExprValue(*Fortran::semantics::GetExpr(ifClause->v))); + ifCond = firOpBuilder.createConvert(currentLocation, + firOpBuilder.getI1Type(), cond); + } else if (const auto *asyncClause = + std::get_if(&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; + } + } + } + + // Prepare the operand segement size attribute and the operands value range. + SmallVector operands; + SmallVector operandSegments; + addOperands(operands, operandSegments, waitOperands); + addOperand(operands, operandSegments, async); + addOperand(operands, operandSegments, waitDevnum); + addOperand(operands, operandSegments, ifCond); + + auto waitOp = createSimpleOp(firOpBuilder, currentLocation, + operands, operandSegments); + + if (addAsyncAttr) + waitOp.asyncAttr(firOpBuilder.getUnitAttr()); +} + void Fortran::lower::genOpenACCConstruct( Fortran::lower::AbstractConverter &converter, Fortran::lower::pft::Evaluation &eval, @@ -940,7 +1015,7 @@ TODO("OpenACC Cache construct not lowered yet!"); }, [&](const Fortran::parser::OpenACCWaitConstruct &waitConstruct) { - TODO("OpenACC Wait construct not lowered yet!"); + genACC(converter, eval, waitConstruct); }, [&](const Fortran::parser::OpenACCAtomicConstruct &atomicConstruct) { TODO("OpenACC Atomic construct not lowered yet!");