18#ifndef FORTRAN_LOWER_DIRECTIVES_COMMON_H
19#define FORTRAN_LOWER_DIRECTIVES_COMMON_H
21#include "flang/Evaluate/tools.h"
22#include "flang/Lower/AbstractConverter.h"
23#include "flang/Lower/Bridge.h"
24#include "flang/Lower/ConvertVariable.h"
25#include "flang/Lower/OpenACC.h"
26#include "flang/Lower/OpenMP.h"
27#include "flang/Lower/PFTBuilder.h"
28#include "flang/Lower/StatementContext.h"
29#include "flang/Lower/Support/Utils.h"
30#include "flang/Optimizer/Builder/DirectivesCommon.h"
31#include "flang/Optimizer/Builder/HLFIRTools.h"
32#include "flang/Optimizer/Dialect/FIRType.h"
33#include "flang/Parser/parse-tree.h"
34#include "flang/Semantics/tools.h"
35#include "mlir/IR/Value.h"
44template <
typename... TerminatorOps>
47 std::list<Fortran::lower::pft::Evaluation> &evaluationList) {
48 mlir::Region *region = &builder.
getRegion();
51 if (eval.block->empty()) {
53 eval.block = builder.createBlock(region);
55 [[maybe_unused]] mlir::Operation &terminatorOp = eval.block->back();
56 assert(mlir::isa<TerminatorOps...>(terminatorOp) &&
57 "expected terminator op");
60 if (!eval.isDirective() && eval.hasNestedEvaluations())
62 eval.getNestedEvaluations());
69 Fortran::lower::SymbolRef sym, mlir::Location loc,
70 bool unwrapFirBox =
true) {
71 return fir::factory::getDataOperandBaseAddr(
73 Fortran::semantics::IsOptional(sym), loc, unwrapFirBox);
78static T &&AsRvalueRef(T &&t) {
82static T AsRvalueRef(T &t) {
86static T AsRvalueRef(
const T &t) {
97 template <Fortran::common::TypeCategory Category,
int Kind>
98 static Fortran::semantics::MaybeExpr visit_with_category(
101 return Fortran::common::visit(
102 [](
auto &&s) {
return visit_with_category<Category, Kind>(s); },
105 template <Fortran::common::TypeCategory Category,
int Kind>
106 static Fortran::semantics::MaybeExpr visit_with_category(
109 return AsGenericExpr(AsRvalueRef(expr.left()));
111 template <Fortran::common::TypeCategory Category,
int Kind,
typename T>
112 static Fortran::semantics::MaybeExpr visit_with_category(
const T &) {
115 template <Fortran::common::TypeCategory Category,
typename T>
116 static Fortran::semantics::MaybeExpr visit_with_category(
const T &) {
120 template <Fortran::common::TypeCategory Category>
121 static Fortran::semantics::MaybeExpr
124 return Fortran::common::visit(
125 [](
auto &&s) {
return visit_with_category<Category>(s); }, expr.u);
127 static Fortran::semantics::MaybeExpr
129 return Fortran::common::visit([](
auto &&s) {
return visit(s); }, expr.u);
131 template <
typename T>
132 static Fortran::semantics::MaybeExpr visit(
const T &) {
137static inline Fortran::semantics::SomeExpr
138peelOuterConvert(Fortran::semantics::SomeExpr &expr) {
139 if (
auto peeled = PeelConvert::visit(expr))
147template <
typename BoundsOp,
typename BoundsType>
152 const std::vector<Fortran::evaluate::Subscript> &subscripts,
155 bool treatIndexAsSection =
false,
156 bool strideIncludeLowerExtent =
false) {
158 mlir::Type idxTy = builder.getIndexType();
159 mlir::Type boundTy = builder.getType<BoundsType>();
164 const int dataExvRank =
static_cast<int>(dataExv.rank());
165 mlir::Value cumulativeExtent = one;
166 for (
const auto &subscript : subscripts) {
167 const auto *triplet{std::get_if<Fortran::evaluate::Triplet>(&subscript.u)};
168 if (triplet || treatIndexAsSection) {
171 mlir::Value lbound, ubound, extent;
172 std::optional<std::int64_t> lval, uval;
175 bool defaultLb = baseLb == one;
176 mlir::Value stride = one;
177 bool strideInBytes =
false;
179 auto genSourceExtent = [&]() -> mlir::Value {
180 if (info.isPresent && mlir::isa<fir::BaseBoxType>(
181 fir::unwrapRefType(info.addr.getType()))) {
183 .
genIfOp(loc, idxTy, info.isPresent,
true)
187 fir::ResultOp::create(builder, loc, ext);
191 fir::ResultOp::create(builder, loc, zero);
198 if (mlir::isa<fir::BaseBoxType>(
199 fir::unwrapRefType(info.addr.getType()))) {
200 if (info.isPresent) {
203 .
genIfOp(loc, idxTy, info.isPresent,
true)
208 : fir::LoadOp::create(builder, loc, info.addr);
211 auto dimInfo = fir::BoxDimsOp::create(builder, loc, idxTy,
212 idxTy, idxTy, box, d);
213 fir::ResultOp::create(builder, loc,
214 dimInfo.getByteStride());
219 fir::ResultOp::create(builder, loc, zero);
225 : fir::LoadOp::create(builder, loc, info.addr);
228 fir::BoxDimsOp::create(builder, loc, idxTy, idxTy, idxTy, box, d);
229 stride = dimInfo.getByteStride();
231 strideInBytes =
true;
234 Fortran::semantics::MaybeExpr
lower;
236 lower = Fortran::evaluate::AsGenericExpr(triplet->lower());
239 using IndirectSubscriptIntegerExpr =
240 Fortran::evaluate::IndirectSubscriptIntegerExpr;
241 using SubscriptInteger = Fortran::evaluate::SubscriptInteger;
243 std::get<IndirectSubscriptIntegerExpr>(subscript.u).value();
244 lower = Fortran::evaluate::AsGenericExpr(std::move(oneInt));
245 if (
lower->Rank() > 0) {
247 loc,
"vector subscript cannot be used for an array section");
252 lval = Fortran::evaluate::ToInt64(*
lower);
258 lbound = mlir::arith::SubIOp::create(builder, loc, lb, baseLb);
265 lbound = mlir::arith::SubIOp::create(builder, loc, lb, baseLb);
266 asFortran << detail::peelOuterConvert(*lower).AsFortran();
282 Fortran::semantics::MaybeExpr upper =
283 Fortran::evaluate::AsGenericExpr(triplet->upper());
286 uval = Fortran::evaluate::ToInt64(*upper);
292 ubound = mlir::arith::SubIOp::create(builder, loc, ub, baseLb);
299 ubound = mlir::arith::SubIOp::create(builder, loc, ub, baseLb);
300 asFortran << detail::peelOuterConvert(*upper).AsFortran();
303 if (
lower && upper) {
304 if (lval && uval && *uval < *lval) {
305 mlir::emitError(loc,
"zero sized array section");
309 auto val = Fortran::evaluate::ToInt64(triplet->GetStride());
310 if (!val || *val != 1) {
311 mlir::emitError(loc,
"stride cannot be specified on "
318 extent = genSourceExtent();
320 if (dataExvIsAssumedSize && dimension + 1 == dataExvRank) {
322 if (ubound && lbound) {
324 mlir::arith::SubIOp::create(builder, loc, ubound, lbound);
325 extent = mlir::arith::AddIOp::create(builder, loc, diff, one);
333 ubound = mlir::arith::SubIOp::create(builder, loc, extent, one);
339 if (strideIncludeLowerExtent && !strideInBytes) {
340 stride = cumulativeExtent;
341 mlir::Value strideExtent = extent;
342 if (!triplet && dimension + 1 < dataExvRank)
343 strideExtent = genSourceExtent();
344 cumulativeExtent = builder.createOrFold<mlir::arith::MulIOp>(
345 loc, cumulativeExtent, strideExtent);
349 BoundsOp::create(builder, loc, boundTy, lbound, ubound, extent,
350 stride, strideInBytes, baseLb);
351 bounds.push_back(bound);
359template <
typename Ref,
typename Expr>
360std::optional<Ref> getRef(Expr &&expr) {
361 if constexpr (std::is_same_v<llvm::remove_cvref_t<Expr>,
363 if (
auto *ref = std::get_if<Ref>(&expr.u))
367 auto maybeRef = Fortran::evaluate::ExtractDataRef(expr);
368 if (!maybeRef || !std::holds_alternative<Ref>(maybeRef->u))
370 return std::get<Ref>(maybeRef->u);
375template <
typename BoundsOp,
typename BoundsType>
376fir::factory::AddrAndBoundsInfo gatherDataOperandAddrAndBounds(
377 Fortran::lower::AbstractConverter &converter, fir::FirOpBuilder &builder,
378 semantics::SemanticsContext &semaCtx,
379 Fortran::lower::StatementContext &stmtCtx,
380 Fortran::semantics::SymbolRef symbol,
381 const Fortran::semantics::MaybeExpr &maybeDesignator,
382 mlir::Location operandLocation, std::stringstream &asFortran,
383 llvm::SmallVector<mlir::Value> &bounds,
bool treatIndexAsSection =
false,
384 bool unwrapFirBox =
true,
bool genDefaultBounds =
true,
385 bool strideIncludeLowerExtent =
false,
386 bool loadAllocatableAndPointerComponent =
true) {
387 using namespace Fortran;
389 fir::factory::AddrAndBoundsInfo info;
391 if (!maybeDesignator) {
392 info = getDataOperandBaseAddr(converter, builder, symbol, operandLocation,
394 asFortran << symbol->name().ToString();
398 semantics::SomeExpr designator = *maybeDesignator;
400 if ((designator.Rank() > 0 || treatIndexAsSection) &&
401 IsArrayElement(designator)) {
402 auto arrayRef = detail::getRef<evaluate::ArrayRef>(designator);
404 assert(arrayRef &&
"Expecting ArrayRef");
406 fir::ExtendedValue dataExv;
407 bool dataExvIsAssumedSize =
false;
409 auto toMaybeExpr = [&](
auto &&base) {
410 using BaseType = llvm::remove_cvref_t<
decltype(base)>;
411 evaluate::ExpressionAnalyzer ea{semaCtx};
413 if constexpr (std::is_same_v<evaluate::NamedEntity, BaseType>) {
414 if (
auto *ref = base.UnwrapSymbolRef())
415 return ea.Designate(evaluate::DataRef{*ref});
416 if (
auto *ref = base.UnwrapComponent())
417 return ea.Designate(evaluate::DataRef{*ref});
418 llvm_unreachable(
"Unexpected NamedEntity");
420 static_assert(std::is_same_v<semantics::SymbolRef, BaseType>);
421 return ea.Designate(evaluate::DataRef{base});
425 auto arrayBase = toMaybeExpr(arrayRef->base());
428 if (
auto comp = detail::getRef<evaluate::Component>(*arrayBase)) {
429 if (!loadAllocatableAndPointerComponent &&
430 semantics::IsAllocatableOrPointer(comp->symbol()))
433 dataExv = converter.
genExprAddr(operandLocation, *arrayBase, stmtCtx);
435 info.rawInput = info.addr;
436 asFortran << arrayBase->AsFortran();
438 const semantics::Symbol &sym = arrayRef->GetLastSymbol();
439 dataExvIsAssumedSize =
440 Fortran::semantics::IsAssumedSizeArray(sym.GetUltimate());
441 info = getDataOperandBaseAddr(converter, builder, sym, operandLocation,
443 dataExv = converter.getSymbolExtendedValue(sym);
444 asFortran << sym.name().ToString();
447 if (!arrayRef->subscript().empty()) {
450 builder, operandLocation, converter, stmtCtx, arrayRef->subscript(),
451 asFortran, dataExv, dataExvIsAssumedSize, info, treatIndexAsSection,
452 strideIncludeLowerExtent);
455 }
else if (
auto compRef = detail::getRef<evaluate::Component>(designator)) {
456 fir::ExtendedValue compExv;
457 if (!loadAllocatableAndPointerComponent &&
458 semantics::IsAllocatableOrPointer(compRef->symbol()))
461 compExv = converter.
genExprAddr(operandLocation, designator, stmtCtx);
463 info.rawInput = info.addr;
464 if (genDefaultBounds &&
465 mlir::isa<fir::SequenceType>(fir::unwrapRefType(info.addr.getType())))
467 builder, operandLocation, compExv,
468 false, strideIncludeLowerExtent);
469 asFortran << designator.AsFortran();
471 if (semantics::IsOptional(compRef->GetLastSymbol())) {
472 info.isPresent = fir::IsPresentOp::create(
473 builder, operandLocation, builder.getI1Type(), info.rawInput);
478 mlir::dyn_cast_or_null<fir::LoadOp>(info.addr.getDefiningOp())) {
481 info.boxType = info.addr.getType();
483 fir::BoxAddrOp::create(builder, operandLocation, info.addr);
485 info.rawInput = info.addr;
494 mlir::dyn_cast_or_null<fir::BoxAddrOp>(info.addr.getDefiningOp())) {
495 info.addr = boxAddrOp.getVal();
496 info.boxType = info.addr.getType();
497 info.rawInput = info.addr;
498 if (genDefaultBounds)
500 builder, operandLocation, compExv, info);
503 if (detail::getRef<evaluate::ArrayRef>(designator)) {
504 fir::ExtendedValue compExv =
505 converter.
genExprAddr(operandLocation, designator, stmtCtx);
507 info.rawInput = info.addr;
508 asFortran << designator.AsFortran();
509 }
else if (
auto symRef = detail::getRef<semantics::SymbolRef>(designator)) {
511 fir::ExtendedValue dataExv = converter.getSymbolExtendedValue(*symRef);
512 info = getDataOperandBaseAddr(converter, builder, *symRef,
513 operandLocation, unwrapFirBox);
514 if (genDefaultBounds && mlir::isa<fir::BaseBoxType>(
515 fir::unwrapRefType(info.addr.getType()))) {
516 info.boxType = fir::unwrapRefType(info.addr.getType());
518 builder, operandLocation, dataExv, info);
520 bool dataExvIsAssumedSize =
521 Fortran::semantics::IsAssumedSizeArray(symRef->get().GetUltimate());
522 if (genDefaultBounds && mlir::isa<fir::SequenceType>(
523 fir::unwrapRefType(info.addr.getType()))) {
525 builder, operandLocation, dataExv, dataExvIsAssumedSize,
526 strideIncludeLowerExtent);
529 fir::unwrapRefType(info.addr.getType()))) ||
530 mlir::isa<fir::BoxCharType>(
531 fir::unwrapRefType(info.addr.getType()))) {
532 bounds = {fir::factory::genBoundsOpFromBoxChar<BoundsOp, BoundsType>(
533 builder, operandLocation, dataExv, info)};
535 asFortran << symRef->get().name().ToString();
537 llvm::report_fatal_error(
"Unsupported type of OpenACC operand");
Definition AbstractConverter.h:87
virtual fir::MutableBoxValue genExprMutableBox(mlir::Location loc, const SomeExpr &expr)=0
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:475
Definition FIRBuilder.h:59
mlir::Value createConvert(mlir::Location loc, mlir::Type toTy, mlir::Value val)
Lazy creation of fir.convert op.
Definition FIRBuilder.cpp:623
IfBuilder genIfOp(mlir::Location loc, mlir::TypeRange results, mlir::Value cdt, bool withElseRegion)
Definition FIRBuilder.h:550
mlir::Region & getRegion()
Get the current Region of the insertion point.
Definition FIRBuilder.h:113
mlir::Value createIntegerConstant(mlir::Location loc, mlir::Type integerType, std::int64_t i)
Definition FIRBuilder.cpp:148
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:149
void createEmptyRegionBlocks(fir::FirOpBuilder &builder, std::list< Fortran::lower::pft::Evaluation > &evaluationList)
Definition DirectivesCommon.h:45
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:276
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:209
mlir::Value readLowerBound(fir::FirOpBuilder &builder, mlir::Location loc, const fir::ExtendedValue &box, unsigned dim, mlir::Value defaultValue)
Definition FIRBuilder.cpp:1031
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:1000
bool isBoxAddress(mlir::Type t)
Is t an address to fir.box or class type?
Definition FIRType.h:528
mlir::Value getBase(const ExtendedValue &exv)
Definition BoxValue.cpp:21
bool isPointerType(mlir::Type ty)
Definition FIRType.cpp:315
bool characterWithDynamicLen(mlir::Type t)
Returns true iff t is a fir.char type and has an unknown length.
Definition FIRType.h:256
bool isAllocatableType(mlir::Type ty)
Return true iff ty is the type of an ALLOCATABLE entity or value.
Definition FIRType.cpp:323
Definition expression.h:210
Definition variable.h:288
Definition DirectivesCommon.h:96
Definition PFTBuilder.h:221
Definition DirectivesCommon.h:31