17#ifndef FORTRAN_OPTIMIZER_BUILDER_DIRECTIVESCOMMON_H_
18#define FORTRAN_OPTIMIZER_BUILDER_DIRECTIVESCOMMON_H_
21#include "FIRBuilder.h"
22#include "flang/Optimizer/Builder/BoxValue.h"
23#include "flang/Optimizer/Builder/FIRBuilder.h"
24#include "flang/Optimizer/Builder/Todo.h"
25#include "flang/Optimizer/HLFIR/HLFIROps.h"
26#include "mlir/Dialect/OpenACC/OpenACC.h"
27#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
36 : addr(addr), rawInput(rawInput) {}
38 mlir::Value isPresent)
39 : addr(addr), rawInput(rawInput), isPresent(isPresent) {}
41 mlir::Value isPresent, mlir::Type boxType)
42 : addr(addr), rawInput(rawInput), isPresent(isPresent), boxType(boxType) {
44 mlir::Value addr =
nullptr;
45 mlir::Value rawInput =
nullptr;
46 mlir::Value isPresent =
nullptr;
47 mlir::Type boxType =
nullptr;
48 void dump(llvm::raw_ostream &os) {
49 os <<
"AddrAndBoundsInfo addr: " << addr <<
"\n";
50 os <<
"AddrAndBoundsInfo rawInput: " << rawInput <<
"\n";
51 os <<
"AddrAndBoundsInfo isPresent: " << isPresent <<
"\n";
52 os <<
"AddrAndBoundsInfo boxType: " << boxType <<
"\n";
60 bool unwrapFirBox =
true) {
61 mlir::Value rawInput = symAddr;
63 mlir::dyn_cast_or_null<hlfir::DeclareOp>(symAddr.getDefiningOp())) {
64 symAddr = declareOp.getResults()[0];
65 rawInput = declareOp.getResults()[1];
69 llvm::report_fatal_error(
"could not retrieve symbol address");
71 mlir::Value isPresent;
74 fir::IsPresentOp::create(builder, loc, builder.getI1Type(), rawInput);
76 if (
auto boxTy = mlir::dyn_cast<fir::BaseBoxType>(
77 fir::unwrapRefType(symAddr.getType()))) {
83 if (unwrapFirBox && mlir::isa<fir::ReferenceType>(symAddr.getType()) &&
85 mlir::Value addr = fir::LoadOp::create(builder, loc, symAddr);
86 return AddrAndBoundsInfo(addr, rawInput, isPresent, boxTy);
89 return AddrAndBoundsInfo(symAddr, rawInput, isPresent, boxTy);
94 if (
auto boxCharType = mlir::dyn_cast<fir::BoxCharType>(
95 fir::unwrapRefType((symAddr.getType())))) {
96 if (!isOptional && mlir::isa<fir::ReferenceType>(symAddr.getType())) {
97 mlir::Value boxChar = fir::LoadOp::create(builder, loc, symAddr);
98 return AddrAndBoundsInfo(boxChar, rawInput, isPresent);
101 return AddrAndBoundsInfo(symAddr, rawInput, isPresent);
104template <
typename BoundsOp,
typename BoundsType>
108 bool collectValuesOnly =
false) {
109 assert(box &&
"box must exist");
111 mlir::Value byteStride;
112 mlir::Type idxTy = builder.getIndexType();
113 mlir::Type boundTy = builder.getType<BoundsType>();
115 for (
unsigned dim = 0; dim < dataExv.rank(); ++dim) {
120 fir::BoxDimsOp::create(builder, loc, idxTy, idxTy, idxTy, box, d);
123 mlir::arith::SubIOp::create(builder, loc, dimInfo.getExtent(), one);
125 byteStride = dimInfo.getByteStride();
126 if (collectValuesOnly) {
127 values.push_back(lb);
128 values.push_back(ub);
129 values.push_back(dimInfo.getExtent());
130 values.push_back(byteStride);
131 values.push_back(baseLb);
134 BoundsOp::create(builder, loc, boundTy, lb, ub, dimInfo.getExtent(),
135 byteStride,
true, baseLb);
136 values.push_back(bound);
139 byteStride = mlir::arith::MulIOp::create(builder, loc, byteStride,
140 dimInfo.getExtent());
144template <
typename BoundsOp,
typename BoundsType>
149 if (!mlir::isa<fir::BoxCharType>(fir::unwrapRefType(info.addr.getType())))
150 return mlir::Value{};
152 mlir::Type idxTy = builder.getIndexType();
156 using ExtentAndStride = std::tuple<mlir::Value, mlir::Value>;
157 auto [extent, stride] = [&]() -> ExtentAndStride {
158 if (info.isPresent) {
160 mlir::Operation::result_range ifRes =
162 .
genIfOp(loc, resTypes, info.isPresent,
true)
164 mlir::Value boxChar =
166 ? fir::LoadOp::create(builder, loc, info.addr)
168 fir::BoxCharType boxCharType =
169 mlir::cast<fir::BoxCharType>(boxChar.getType());
170 mlir::Type refType = builder.
getRefType(boxCharType.getEleTy());
171 auto unboxed = fir::UnboxCharOp::create(builder, loc, refType,
173 mlir::SmallVector<mlir::Value> results = {unboxed.getResult(1),
175 fir::ResultOp::create(builder, loc, results);
178 mlir::SmallVector<mlir::Value> results = {zero, zero};
179 fir::ResultOp::create(builder, loc, results);
182 return {ifRes[0], ifRes[1]};
187 ? fir::LoadOp::create(builder, loc, info.addr)
189 fir::BoxCharType boxCharType =
190 mlir::cast<fir::BoxCharType>(boxChar.getType());
191 mlir::Type refType = builder.
getRefType(boxCharType.getEleTy());
193 fir::UnboxCharOp::create(builder, loc, refType, lenType, boxChar);
194 return {unboxed.getResult(1), one};
197 mlir::Value ub = mlir::arith::SubIOp::create(builder, loc, extent, one);
198 mlir::Type boundTy = builder.getType<BoundsType>();
199 return BoundsOp::create(builder, loc, boundTy,
209template <
typename BoundsOp,
typename BoundsType>
214 mlir::Type idxTy = builder.getIndexType();
215 mlir::Type boundTy = builder.getType<BoundsType>();
217 assert(mlir::isa<fir::BaseBoxType>(info.boxType) &&
218 "expect fir.box or fir.class");
219 assert(fir::unwrapRefType(info.addr.getType()) == info.boxType &&
220 "expected box type consistency");
222 if (info.isPresent) {
224 constexpr unsigned nbValuesPerBound = 5;
225 for (
unsigned dim = 0; dim < dataExv.rank() * nbValuesPerBound; ++dim)
226 resTypes.push_back(idxTy);
228 mlir::Operation::result_range ifRes =
229 builder.
genIfOp(loc, resTypes, info.isPresent,
true)
234 : fir::LoadOp::create(builder, loc, info.addr);
236 gatherBoundsOrBoundValues<BoundsOp, BoundsType>(
237 builder, loc, dataExv, box,
239 fir::ResultOp::create(builder, loc, boundValues);
246 for (
unsigned dim = 0; dim < dataExv.rank(); ++dim) {
247 boundValues.push_back(zero);
248 boundValues.push_back(mOne);
249 boundValues.push_back(zero);
250 boundValues.push_back(zero);
251 boundValues.push_back(zero);
253 fir::ResultOp::create(builder, loc, boundValues);
258 for (
unsigned i = 0; i < ifRes.size(); i += nbValuesPerBound) {
260 BoundsOp::create(builder, loc, boundTy, ifRes[i], ifRes[i + 1],
261 ifRes[i + 2], ifRes[i + 3],
true, ifRes[i + 4]);
262 bounds.push_back(bound);
267 : fir::LoadOp::create(builder, loc, info.addr);
268 bounds = gatherBoundsOrBoundValues<BoundsOp, BoundsType>(builder, loc,
276template <
typename BoundsOp,
typename BoundsType>
280 bool strideIncludeLowerExtent =
false) {
281 mlir::Type idxTy = builder.getIndexType();
282 mlir::Type boundTy = builder.getType<BoundsType>();
285 if (dataExv.rank() == 0)
289 const unsigned rank = dataExv.rank();
290 mlir::Value cumulativeExtent = one;
291 for (
unsigned dim = 0; dim < rank; ++dim) {
296 mlir::Value lb = zero;
298 if (isAssumedSize && dim + 1 == rank) {
303 ub = mlir::arith::SubIOp::create(builder, loc, extent, one);
305 mlir::Value stride = one;
306 if (strideIncludeLowerExtent) {
307 stride = cumulativeExtent;
308 cumulativeExtent = builder.createOrFold<mlir::arith::MulIOp>(
309 loc, cumulativeExtent, extent);
312 mlir::Value bound = BoundsOp::create(builder, loc, boundTy, lb, ub, extent,
313 stride,
false, baseLb);
314 bounds.push_back(bound);
322 if (
auto declareOp = mlir::dyn_cast_or_null<hlfir::DeclareOp>(op))
323 if (declareOp.getFortranAttrs() &&
324 bitEnumContainsAny(*declareOp.getFortranAttrs(),
325 fir::FortranVariableFlagsEnum::optional))
330template <
typename BoundsOp,
typename BoundsType>
334 mlir::Location loc) {
337 mlir::Value baseOp = info.rawInput;
338 if (mlir::isa<fir::BaseBoxType>(fir::unwrapRefType(baseOp.getType())))
340 genBoundsOpsFromBox<BoundsOp, BoundsType>(builder, loc, dataExv, info);
341 if (mlir::isa<fir::SequenceType>(fir::unwrapRefType(baseOp.getType()))) {
342 bounds = genBaseBoundsOps<BoundsOp, BoundsType>(builder, loc, dataExv,
343 dataExvIsAssumedSize);
346 mlir::isa<fir::BoxCharType>(fir::unwrapRefType(info.addr.getType()))) {
347 bounds = {genBoundsOpFromBoxChar<BoundsOp, BoundsType>(builder, loc,
Definition: BoxValue.h:478
Definition: FIRBuilder.h:55
IfBuilder genIfOp(mlir::Location loc, mlir::TypeRange results, mlir::Value cdt, bool withElseRegion)
Definition: FIRBuilder.h:531
mlir::Type getCharacterLengthType()
Get character length type.
Definition: FIRBuilder.h:159
mlir::Type getRefType(mlir::Type eleTy, bool isVolatile=false)
Safely create a reference type to the type eleTy.
Definition: FIRBuilder.cpp:108
mlir::Value createMinusOneInteger(mlir::Location loc, mlir::Type integerType)
Definition: FIRBuilder.h:193
mlir::Value createIntegerConstant(mlir::Location loc, mlir::Type integerType, std::int64_t i)
Definition: FIRBuilder.cpp:144
Definition: BoxValue.h:445
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 isOptionalArgument(mlir::Operation *op)
Definition: DirectivesCommon.h:321
bool isa_ref_type(mlir::Type t)
Is t a FIR dialect type that implies a memory (de)reference?
Definition: FIRType.h:118
bool isBoxAddress(mlir::Type t)
Is t an address to fir.box or class type?
Definition: FIRType.h:506
bool characterWithDynamicLen(mlir::Type t)
Returns true iff t is a fir.char type and has an unknown length.
Definition: FIRType.h:238
Definition: DirectivesCommon.h:33