18#ifndef FORTRAN_LOWER_DIRECTIVES_COMMON_H
19#define FORTRAN_LOWER_DIRECTIVES_COMMON_H
21#include "flang/Common/idioms.h"
22#include "flang/Evaluate/tools.h"
23#include "flang/Lower/AbstractConverter.h"
24#include "flang/Lower/Bridge.h"
25#include "flang/Lower/ConvertExpr.h"
26#include "flang/Lower/ConvertVariable.h"
27#include "flang/Lower/OpenACC.h"
28#include "flang/Lower/OpenMP.h"
29#include "flang/Lower/PFTBuilder.h"
30#include "flang/Lower/StatementContext.h"
31#include "flang/Lower/Support/Utils.h"
32#include "flang/Optimizer/Builder/DirectivesCommon.h"
33#include "flang/Optimizer/Builder/HLFIRTools.h"
34#include "flang/Optimizer/Dialect/FIRType.h"
35#include "flang/Parser/parse-tree.h"
36#include "flang/Semantics/openmp-directive-sets.h"
37#include "flang/Semantics/tools.h"
38#include "mlir/Dialect/OpenACC/OpenACC.h"
39#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
40#include "mlir/Dialect/SCF/IR/SCF.h"
41#include "mlir/IR/Value.h"
42#include "llvm/Frontend/OpenMP/OMPConstants.h"
51template <
typename... TerminatorOps>
54 std::list<Fortran::lower::pft::Evaluation> &evaluationList) {
55 mlir::Region *region = &builder.
getRegion();
58 if (eval.block->empty()) {
60 eval.block = builder.createBlock(region);
62 [[maybe_unused]] mlir::Operation &terminatorOp = eval.block->back();
63 assert(mlir::isa<TerminatorOps...>(terminatorOp) &&
64 "expected terminator op");
67 if (!eval.isDirective() && eval.hasNestedEvaluations())
69 eval.getNestedEvaluations());
76 Fortran::lower::SymbolRef sym, mlir::Location loc,
77 bool unwrapFirBox =
true) {
78 return fir::factory::getDataOperandBaseAddr(
80 Fortran::semantics::IsOptional(sym), loc, unwrapFirBox);
85static T &&AsRvalueRef(T &&t) {
89static T AsRvalueRef(T &t) {
93static T AsRvalueRef(
const T &t) {
104 template <Fortran::common::TypeCategory Category,
int Kind>
105 static Fortran::semantics::MaybeExpr visit_with_category(
108 return Fortran::common::visit(
109 [](
auto &&s) {
return visit_with_category<Category, Kind>(s); },
112 template <Fortran::common::TypeCategory Category,
int Kind>
113 static Fortran::semantics::MaybeExpr visit_with_category(
116 return AsGenericExpr(AsRvalueRef(expr.left()));
118 template <Fortran::common::TypeCategory Category,
int Kind,
typename T>
119 static Fortran::semantics::MaybeExpr visit_with_category(
const T &) {
122 template <Fortran::common::TypeCategory Category,
typename T>
123 static Fortran::semantics::MaybeExpr visit_with_category(
const T &) {
127 template <Fortran::common::TypeCategory Category>
128 static Fortran::semantics::MaybeExpr
131 return Fortran::common::visit(
132 [](
auto &&s) {
return visit_with_category<Category>(s); }, expr.u);
134 static Fortran::semantics::MaybeExpr
136 return Fortran::common::visit([](
auto &&s) {
return visit(s); }, expr.u);
138 template <
typename T>
139 static Fortran::semantics::MaybeExpr visit(
const T &) {
144static inline Fortran::semantics::SomeExpr
145peelOuterConvert(Fortran::semantics::SomeExpr &expr) {
146 if (
auto peeled = PeelConvert::visit(expr))
154template <
typename BoundsOp,
typename BoundsType>
159 const std::vector<Fortran::evaluate::Subscript> &subscripts,
162 bool treatIndexAsSection =
false,
163 bool strideIncludeLowerExtent =
false) {
165 mlir::Type idxTy = builder.getIndexType();
166 mlir::Type boundTy = builder.getType<BoundsType>();
171 const int dataExvRank =
static_cast<int>(dataExv.rank());
172 mlir::Value cumulativeExtent = one;
173 for (
const auto &subscript : subscripts) {
174 const auto *triplet{std::get_if<Fortran::evaluate::Triplet>(&subscript.u)};
175 if (triplet || treatIndexAsSection) {
178 mlir::Value lbound, ubound, extent;
179 std::optional<std::int64_t> lval, uval;
182 bool defaultLb = baseLb == one;
183 mlir::Value stride = one;
184 bool strideInBytes =
false;
186 if (mlir::isa<fir::BaseBoxType>(
187 fir::unwrapRefType(info.addr.getType()))) {
188 if (info.isPresent) {
191 .
genIfOp(loc, idxTy, info.isPresent,
true)
196 : fir::LoadOp::create(builder, loc, info.addr);
199 auto dimInfo = fir::BoxDimsOp::create(builder, loc, idxTy,
200 idxTy, idxTy, box, d);
201 fir::ResultOp::create(builder, loc,
202 dimInfo.getByteStride());
207 fir::ResultOp::create(builder, loc, zero);
213 : fir::LoadOp::create(builder, loc, info.addr);
216 fir::BoxDimsOp::create(builder, loc, idxTy, idxTy, idxTy, box, d);
217 stride = dimInfo.getByteStride();
219 strideInBytes =
true;
222 Fortran::semantics::MaybeExpr
lower;
224 lower = Fortran::evaluate::AsGenericExpr(triplet->lower());
227 using IndirectSubscriptIntegerExpr =
228 Fortran::evaluate::IndirectSubscriptIntegerExpr;
229 using SubscriptInteger = Fortran::evaluate::SubscriptInteger;
231 std::get<IndirectSubscriptIntegerExpr>(subscript.u).value();
232 lower = Fortran::evaluate::AsGenericExpr(std::move(oneInt));
233 if (
lower->Rank() > 0) {
235 loc,
"vector subscript cannot be used for an array section");
240 lval = Fortran::evaluate::ToInt64(*
lower);
246 lbound = mlir::arith::SubIOp::create(builder, loc, lb, baseLb);
253 lbound = mlir::arith::SubIOp::create(builder, loc, lb, baseLb);
254 asFortran << detail::peelOuterConvert(*lower).AsFortran();
270 Fortran::semantics::MaybeExpr upper =
271 Fortran::evaluate::AsGenericExpr(triplet->upper());
274 uval = Fortran::evaluate::ToInt64(*upper);
280 ubound = mlir::arith::SubIOp::create(builder, loc, ub, baseLb);
287 ubound = mlir::arith::SubIOp::create(builder, loc, ub, baseLb);
288 asFortran << detail::peelOuterConvert(*upper).AsFortran();
291 if (
lower && upper) {
292 if (lval && uval && *uval < *lval) {
293 mlir::emitError(loc,
"zero sized array section");
297 auto val = Fortran::evaluate::ToInt64(triplet->GetStride());
298 if (!val || *val != 1) {
299 mlir::emitError(loc,
"stride cannot be specified on "
306 if (info.isPresent && mlir::isa<fir::BaseBoxType>(
307 fir::unwrapRefType(info.addr.getType()))) {
310 .
genIfOp(loc, idxTy, info.isPresent,
true)
313 builder, loc, dataExv, dimension);
314 fir::ResultOp::create(builder, loc, ext);
319 fir::ResultOp::create(builder, loc, zero);
326 if (dataExvIsAssumedSize && dimension + 1 == dataExvRank) {
328 if (ubound && lbound) {
330 mlir::arith::SubIOp::create(builder, loc, ubound, lbound);
331 extent = mlir::arith::AddIOp::create(builder, loc, diff, one);
339 ubound = mlir::arith::SubIOp::create(builder, loc, extent, one);
345 if (strideIncludeLowerExtent && !strideInBytes) {
346 stride = cumulativeExtent;
347 cumulativeExtent = builder.createOrFold<mlir::arith::MulIOp>(
348 loc, cumulativeExtent, extent);
352 BoundsOp::create(builder, loc, boundTy, lbound, ubound, extent,
353 stride, strideInBytes, baseLb);
354 bounds.push_back(bound);
362template <
typename Ref,
typename Expr>
363std::optional<Ref> getRef(Expr &&expr) {
364 if constexpr (std::is_same_v<llvm::remove_cvref_t<Expr>,
366 if (
auto *ref = std::get_if<Ref>(&expr.u))
370 auto maybeRef = Fortran::evaluate::ExtractDataRef(expr);
371 if (!maybeRef || !std::holds_alternative<Ref>(maybeRef->u))
373 return std::get<Ref>(maybeRef->u);
378template <
typename BoundsOp,
typename BoundsType>
379fir::factory::AddrAndBoundsInfo gatherDataOperandAddrAndBounds(
380 Fortran::lower::AbstractConverter &converter, fir::FirOpBuilder &builder,
381 semantics::SemanticsContext &semaCtx,
382 Fortran::lower::StatementContext &stmtCtx,
383 Fortran::semantics::SymbolRef symbol,
384 const Fortran::semantics::MaybeExpr &maybeDesignator,
385 mlir::Location operandLocation, std::stringstream &asFortran,
386 llvm::SmallVector<mlir::Value> &bounds,
bool treatIndexAsSection =
false,
387 bool unwrapFirBox =
true,
bool genDefaultBounds =
true,
388 bool strideIncludeLowerExtent =
false) {
389 using namespace Fortran;
391 fir::factory::AddrAndBoundsInfo info;
393 if (!maybeDesignator) {
394 info = getDataOperandBaseAddr(converter, builder, symbol, operandLocation,
396 asFortran << symbol->name().ToString();
400 semantics::SomeExpr designator = *maybeDesignator;
402 if ((designator.Rank() > 0 || treatIndexAsSection) &&
403 IsArrayElement(designator)) {
404 auto arrayRef = detail::getRef<evaluate::ArrayRef>(designator);
406 assert(arrayRef &&
"Expecting ArrayRef");
408 fir::ExtendedValue dataExv;
409 bool dataExvIsAssumedSize =
false;
411 auto toMaybeExpr = [&](
auto &&base) {
412 using BaseType = llvm::remove_cvref_t<
decltype(base)>;
413 evaluate::ExpressionAnalyzer ea{semaCtx};
415 if constexpr (std::is_same_v<evaluate::NamedEntity, BaseType>) {
416 if (
auto *ref = base.UnwrapSymbolRef())
417 return ea.Designate(evaluate::DataRef{*ref});
418 if (
auto *ref = base.UnwrapComponent())
419 return ea.Designate(evaluate::DataRef{*ref});
420 llvm_unreachable(
"Unexpected NamedEntity");
422 static_assert(std::is_same_v<semantics::SymbolRef, BaseType>);
423 return ea.Designate(evaluate::DataRef{base});
427 auto arrayBase = toMaybeExpr(arrayRef->base());
430 if (detail::getRef<evaluate::Component>(*arrayBase)) {
431 dataExv = converter.
genExprAddr(operandLocation, *arrayBase, stmtCtx);
433 info.rawInput = info.addr;
434 asFortran << arrayBase->AsFortran();
436 const semantics::Symbol &sym = arrayRef->GetLastSymbol();
437 dataExvIsAssumedSize =
438 Fortran::semantics::IsAssumedSizeArray(sym.GetUltimate());
439 info = getDataOperandBaseAddr(converter, builder, sym, operandLocation,
441 dataExv = converter.getSymbolExtendedValue(sym);
442 asFortran << sym.name().ToString();
445 if (!arrayRef->subscript().empty()) {
448 builder, operandLocation, converter, stmtCtx, arrayRef->subscript(),
449 asFortran, dataExv, dataExvIsAssumedSize, info, treatIndexAsSection,
450 strideIncludeLowerExtent);
453 }
else if (
auto compRef = detail::getRef<evaluate::Component>(designator)) {
454 fir::ExtendedValue compExv =
455 converter.
genExprAddr(operandLocation, designator, stmtCtx);
457 info.rawInput = info.addr;
458 if (genDefaultBounds &&
459 mlir::isa<fir::SequenceType>(fir::unwrapRefType(info.addr.getType())))
461 builder, operandLocation, compExv,
462 false, strideIncludeLowerExtent);
463 asFortran << designator.AsFortran();
465 if (semantics::IsOptional(compRef->GetLastSymbol())) {
466 info.isPresent = fir::IsPresentOp::create(
467 builder, operandLocation, builder.getI1Type(), info.rawInput);
472 mlir::dyn_cast_or_null<fir::LoadOp>(info.addr.getDefiningOp())) {
475 info.boxType = info.addr.getType();
477 fir::BoxAddrOp::create(builder, operandLocation, info.addr);
479 info.rawInput = info.addr;
488 mlir::dyn_cast_or_null<fir::BoxAddrOp>(info.addr.getDefiningOp())) {
489 info.addr = boxAddrOp.getVal();
490 info.boxType = info.addr.getType();
491 info.rawInput = info.addr;
492 if (genDefaultBounds)
494 builder, operandLocation, compExv, info);
497 if (detail::getRef<evaluate::ArrayRef>(designator)) {
498 fir::ExtendedValue compExv =
499 converter.
genExprAddr(operandLocation, designator, stmtCtx);
501 info.rawInput = info.addr;
502 asFortran << designator.AsFortran();
503 }
else if (
auto symRef = detail::getRef<semantics::SymbolRef>(designator)) {
505 fir::ExtendedValue dataExv = converter.getSymbolExtendedValue(*symRef);
506 info = getDataOperandBaseAddr(converter, builder, *symRef,
507 operandLocation, unwrapFirBox);
508 if (genDefaultBounds && mlir::isa<fir::BaseBoxType>(
509 fir::unwrapRefType(info.addr.getType()))) {
510 info.boxType = fir::unwrapRefType(info.addr.getType());
512 builder, operandLocation, dataExv, info);
514 bool dataExvIsAssumedSize =
515 Fortran::semantics::IsAssumedSizeArray(symRef->get().GetUltimate());
516 if (genDefaultBounds &&
517 mlir::isa<fir::SequenceType>(fir::unwrapRefType(info.addr.getType())))
519 builder, operandLocation, dataExv, dataExvIsAssumedSize,
520 strideIncludeLowerExtent);
521 asFortran << symRef->get().name().ToString();
523 llvm::report_fatal_error(
"Unsupported type of OpenACC operand");
Definition AbstractConverter.h:85
virtual mlir::Value getSymbolAddress(SymbolRef sym)=0
Get the mlir instance of a symbol.
virtual fir::ExtendedValue genExprValue(const SomeExpr &expr, StatementContext &context, mlir::Location *locPtr=nullptr)=0
Generate the computations of the expression to produce a value.
virtual fir::ExtendedValue genExprAddr(const SomeExpr &expr, StatementContext &context, mlir::Location *locPtr=nullptr)=0
Definition StatementContext.h:46
Definition BoxValue.h:478
Definition FIRBuilder.h:55
mlir::Value createConvert(mlir::Location loc, mlir::Type toTy, mlir::Value val)
Lazy creation of fir.convert op.
Definition FIRBuilder.cpp:611
IfBuilder genIfOp(mlir::Location loc, mlir::TypeRange results, mlir::Value cdt, bool withElseRegion)
Definition FIRBuilder.h:538
mlir::Region & getRegion()
Get the current Region of the insertion point.
Definition FIRBuilder.h:109
mlir::Value createIntegerConstant(mlir::Location loc, mlir::Type integerType, std::int64_t i)
Definition FIRBuilder.cpp:144
Definition ParserActions.h:24
llvm::SmallVector< mlir::Value > genBoundsOps(fir::FirOpBuilder &builder, mlir::Location loc, Fortran::lower::AbstractConverter &converter, Fortran::lower::StatementContext &stmtCtx, const std::vector< Fortran::evaluate::Subscript > &subscripts, std::stringstream &asFortran, fir::ExtendedValue &dataExv, bool dataExvIsAssumedSize, fir::factory::AddrAndBoundsInfo &info, bool treatIndexAsSection=false, bool strideIncludeLowerExtent=false)
Definition DirectivesCommon.h:156
void createEmptyRegionBlocks(fir::FirOpBuilder &builder, std::list< Fortran::lower::pft::Evaluation > &evaluationList)
Definition DirectivesCommon.h:52
Definition bit-population-count.h:20
llvm::SmallVector< mlir::Value > genBaseBoundsOps(fir::FirOpBuilder &builder, mlir::Location loc, fir::ExtendedValue dataExv, bool isAssumedSize, bool strideIncludeLowerExtent=false)
Definition DirectivesCommon.h:278
llvm::SmallVector< mlir::Value > genBoundsOpsFromBox(fir::FirOpBuilder &builder, mlir::Location loc, fir::ExtendedValue dataExv, AddrAndBoundsInfo &info)
Generate the bounds operation from the descriptor information.
Definition DirectivesCommon.h:211
mlir::Value readLowerBound(fir::FirOpBuilder &builder, mlir::Location loc, const fir::ExtendedValue &box, unsigned dim, mlir::Value defaultValue)
Definition FIRBuilder.cpp:1008
mlir::Value readExtent(fir::FirOpBuilder &builder, mlir::Location loc, const fir::ExtendedValue &box, unsigned dim)
Read or get the extent in dimension dim of the array described by box.
Definition FIRBuilder.cpp:977
bool isBoxAddress(mlir::Type t)
Is t an address to fir.box or class type?
Definition FIRType.h:506
mlir::Value getBase(const ExtendedValue &exv)
Definition BoxValue.cpp:21
bool isPointerType(mlir::Type ty)
Definition FIRType.cpp:305
bool isAllocatableType(mlir::Type ty)
Return true iff ty is the type of an ALLOCATABLE entity or value.
Definition FIRType.cpp:313
Definition expression.h:211
Definition variable.h:284
Definition DirectivesCommon.h:103
Definition PFTBuilder.h:221
Definition DirectivesCommon.h:33