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 @@ -15,6 +15,7 @@ #include "flang/Lower/Bridge.h" #include "flang/Lower/FIRBuilder.h" #include "flang/Lower/PFTBuilder.h" +#include "flang/Lower/Support/BoxValue.h" #include "flang/Parser/parse-tree.h" #include "flang/Semantics/tools.h" #include "mlir/Dialect/OpenACC/OpenACC.h" @@ -48,6 +49,26 @@ } } +template +static void +genObjectListWithModifier(const Clause *x, + Fortran::lower::AbstractConverter &converter, + Fortran::parser::AccDataModifier::Modifier mod, + SmallVectorImpl &operandsWithModifier, + SmallVectorImpl &operands) { + const Fortran::parser::AccObjectListWithModifier &listWithModifier = x->v; + const Fortran::parser::AccObjectList &accObjectList = + std::get(listWithModifier.t); + const auto &modifier = + std::get>( + listWithModifier.t); + if (modifier && (*modifier).v == mod) { + genObjectList(accObjectList, converter, operandsWithModifier); + } else { + genObjectList(accObjectList, converter, operands); + } +} + static void addOperands(SmallVectorImpl &operands, SmallVectorImpl &operandSegments, const SmallVectorImpl &clauseOperands) { @@ -227,6 +248,102 @@ } } +static void genACCDataOp(Fortran::lower::AbstractConverter &converter, + const Fortran::parser::AccClauseList &accClauseList) { + mlir::Value ifCond; + SmallVector copyOperands, copyinOperands, copyinReadonlyOperands, + copyoutOperands, copyoutZeroOperands, createOperands, createZeroOperands, + noCreateOperands, presentOperands, deviceptrOperands, attachOperands; + + 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 *ifClause = + std::get_if(&clause.u)) { + Value cond = fir::getBase( + converter.genExprValue(*Fortran::semantics::GetExpr(ifClause->v))); + ifCond = firOpBuilder.createConvert(currentLocation, + firOpBuilder.getI1Type(), cond); + } else if (const auto *copyClause = + std::get_if(&clause.u)) { + genObjectList(copyClause->v, converter, copyOperands); + } else if (const auto *copyinClause = + std::get_if(&clause.u)) { + genObjectListWithModifier( + copyinClause, converter, + Fortran::parser::AccDataModifier::Modifier::ReadOnly, + copyinReadonlyOperands, copyinOperands); + } else if (const auto *copyoutClause = + std::get_if( + &clause.u)) { + genObjectListWithModifier( + copyoutClause, converter, + Fortran::parser::AccDataModifier::Modifier::Zero, copyoutZeroOperands, + copyoutOperands); + } else if (const auto *createClause = + std::get_if(&clause.u)) { + genObjectListWithModifier( + createClause, converter, + Fortran::parser::AccDataModifier::Modifier::Zero, createZeroOperands, + createOperands); + } else if (const auto *noCreateClause = + std::get_if( + &clause.u)) { + genObjectList(noCreateClause->v, converter, noCreateOperands); + } else if (const auto *presentClause = + std::get_if( + &clause.u)) { + genObjectList(presentClause->v, converter, presentOperands); + } else if (const auto *deviceptrClause = + std::get_if( + &clause.u)) { + genObjectList(deviceptrClause->v, converter, deviceptrOperands); + } else if (const auto *attachClause = + std::get_if(&clause.u)) { + genObjectList(attachClause->v, converter, attachOperands); + } + } + + // Prepare the operand segement size attribute and the operands value range. + SmallVector operands; + SmallVector operandSegments; + addOperand(operands, operandSegments, ifCond); + addOperands(operands, operandSegments, copyOperands); + addOperands(operands, operandSegments, copyinOperands); + addOperands(operands, operandSegments, copyinReadonlyOperands); + addOperands(operands, operandSegments, copyoutOperands); + addOperands(operands, operandSegments, copyoutZeroOperands); + addOperands(operands, operandSegments, createOperands); + addOperands(operands, operandSegments, createZeroOperands); + addOperands(operands, operandSegments, noCreateOperands); + addOperands(operands, operandSegments, presentOperands); + addOperands(operands, operandSegments, deviceptrOperands); + addOperands(operands, operandSegments, attachOperands); + + createRegionOp( + firOpBuilder, currentLocation, operands, operandSegments); +} + +static void +genACC(Fortran::lower::AbstractConverter &converter, + Fortran::lower::pft::Evaluation &eval, + const Fortran::parser::OpenACCBlockConstruct &blockConstruct) { + const auto &beginBlockDirective = + std::get(blockConstruct.t); + const auto &blockDirective = + std::get(beginBlockDirective.t); + const auto &accClauseList = + std::get(beginBlockDirective.t); + + if (blockDirective.v == llvm::acc::ACCD_data) { + genACCDataOp(converter, accClauseList); + } +} + void Fortran::lower::genOpenACCConstruct( Fortran::lower::AbstractConverter &converter, Fortran::lower::pft::Evaluation &eval, @@ -235,7 +352,7 @@ std::visit( common::visitors{ [&](const Fortran::parser::OpenACCBlockConstruct &blockConstruct) { - TODO(); + genACC(converter, eval, blockConstruct); }, [&](const Fortran::parser::OpenACCCombinedConstruct &combinedConstruct) { TODO(); },