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"
31struct AddrAndBoundsInfo {
32 explicit AddrAndBoundsInfo() {}
33 explicit AddrAndBoundsInfo(mlir::Value addr, mlir::Value rawInput)
34 : addr(addr), rawInput(rawInput) {}
35 explicit AddrAndBoundsInfo(mlir::Value addr, mlir::Value rawInput,
36 mlir::Value isPresent)
37 : addr(addr), rawInput(rawInput), isPresent(isPresent) {}
38 explicit AddrAndBoundsInfo(mlir::Value addr, mlir::Value rawInput,
39 mlir::Value isPresent, mlir::Type boxType)
40 : addr(addr), rawInput(rawInput), isPresent(isPresent), boxType(boxType) {
42 mlir::Value addr =
nullptr;
43 mlir::Value rawInput =
nullptr;
44 mlir::Value isPresent =
nullptr;
45 mlir::Type boxType =
nullptr;
46 void dump(llvm::raw_ostream &os) {
47 os <<
"AddrAndBoundsInfo addr: " << addr <<
"\n";
48 os <<
"AddrAndBoundsInfo rawInput: " << rawInput <<
"\n";
49 os <<
"AddrAndBoundsInfo isPresent: " << isPresent <<
"\n";
50 os <<
"AddrAndBoundsInfo boxType: " << boxType <<
"\n";
58 bool unwrapFirBox =
true) {
59 mlir::Value rawInput = symAddr;
61 mlir::dyn_cast_or_null<hlfir::DeclareOp>(symAddr.getDefiningOp())) {
62 symAddr = declareOp.getResults()[0];
63 rawInput = declareOp.getResults()[1];
67 llvm::report_fatal_error(
"could not retrieve symbol address");
69 mlir::Value isPresent;
72 fir::IsPresentOp::create(builder, loc, builder.getI1Type(), rawInput);
74 if (
auto boxTy = mlir::dyn_cast<fir::BaseBoxType>(
75 fir::unwrapRefType(symAddr.getType()))) {
81 if (unwrapFirBox && mlir::isa<fir::ReferenceType>(symAddr.getType()) &&
83 mlir::Value addr = fir::LoadOp::create(builder, loc, symAddr);
84 return AddrAndBoundsInfo(addr, rawInput, isPresent, boxTy);
87 return AddrAndBoundsInfo(symAddr, rawInput, isPresent, boxTy);
92 if (
auto boxCharType = mlir::dyn_cast<fir::BoxCharType>(
93 fir::unwrapRefType((symAddr.getType())))) {
94 if (!isOptional && mlir::isa<fir::ReferenceType>(symAddr.getType())) {
95 mlir::Value boxChar = fir::LoadOp::create(builder, loc, symAddr);
102template <
typename BoundsOp,
typename BoundsType>
103llvm::SmallVector<mlir::Value>
104gatherBoundsOrBoundValues(fir::FirOpBuilder &builder, mlir::Location loc,
105 fir::ExtendedValue dataExv, mlir::Value box,
106 bool collectValuesOnly =
false) {
107 assert(box &&
"box must exist");
108 llvm::SmallVector<mlir::Value> values;
109 mlir::Value byteStride;
110 mlir::Type idxTy = builder.getIndexType();
111 mlir::Type boundTy = builder.getType<BoundsType>();
113 for (
unsigned dim = 0; dim < dataExv.rank(); ++dim) {
118 fir::BoxDimsOp::create(builder, loc, idxTy, idxTy, idxTy, box, d);
121 mlir::arith::SubIOp::create(builder, loc, dimInfo.getExtent(), one);
123 byteStride = dimInfo.getByteStride();
124 if (collectValuesOnly) {
125 values.push_back(lb);
126 values.push_back(ub);
127 values.push_back(dimInfo.getExtent());
128 values.push_back(byteStride);
129 values.push_back(baseLb);
132 BoundsOp::create(builder, loc, boundTy, lb, ub, dimInfo.getExtent(),
133 byteStride,
true, baseLb);
134 values.push_back(bound);
137 byteStride = mlir::arith::MulIOp::create(builder, loc, byteStride,
138 dimInfo.getExtent());
142template <
typename BoundsOp,
typename BoundsType>
144genBoundsOpFromBoxChar(fir::FirOpBuilder &builder, mlir::Location loc,
147 if (!mlir::isa<fir::BoxCharType>(fir::unwrapRefType(info.addr.getType())))
148 return mlir::Value{};
150 mlir::Type idxTy = builder.getIndexType();
154 using ExtentAndStride = std::tuple<mlir::Value, mlir::Value>;
155 auto [extent, stride] = [&]() -> ExtentAndStride {
156 if (info.isPresent) {
157 llvm::SmallVector<mlir::Type> resTypes = {idxTy, idxTy};
158 mlir::Operation::result_range ifRes =
160 .
genIfOp(loc, resTypes, info.isPresent,
true)
162 mlir::Value boxChar =
164 ? fir::LoadOp::create(builder, loc, info.addr)
166 fir::BoxCharType boxCharType =
167 mlir::cast<fir::BoxCharType>(boxChar.getType());
168 mlir::Type refType = builder.
getRefType(boxCharType.getEleTy());
169 auto unboxed = fir::UnboxCharOp::create(builder, loc, refType,
171 mlir::SmallVector<mlir::Value> results = {unboxed.getResult(1),
173 fir::ResultOp::create(builder, loc, results);
176 mlir::SmallVector<mlir::Value> results = {zero, zero};
177 fir::ResultOp::create(builder, loc, results);
180 return {ifRes[0], ifRes[1]};
185 ? fir::LoadOp::create(builder, loc, info.addr)
187 fir::BoxCharType boxCharType =
188 mlir::cast<fir::BoxCharType>(boxChar.getType());
189 mlir::Type refType = builder.
getRefType(boxCharType.getEleTy());
191 fir::UnboxCharOp::create(builder, loc, refType, lenType, boxChar);
192 return {unboxed.getResult(1), one};
195 mlir::Value ub = mlir::arith::SubIOp::create(builder, loc, extent, one);
196 mlir::Type boundTy = builder.getType<BoundsType>();
197 return BoundsOp::create(builder, loc, boundTy,
207template <
typename BoundsOp,
typename BoundsType>
208llvm::SmallVector<mlir::Value>
212 mlir::Type idxTy = builder.getIndexType();
213 mlir::Type boundTy = builder.getType<BoundsType>();
215 assert(mlir::isa<fir::BaseBoxType>(info.boxType) &&
216 "expect fir.box or fir.class");
217 assert(fir::unwrapRefType(info.addr.getType()) == info.boxType &&
218 "expected box type consistency");
220 if (info.isPresent) {
222 constexpr unsigned nbValuesPerBound = 5;
223 for (
unsigned dim = 0; dim < dataExv.rank() * nbValuesPerBound; ++dim)
224 resTypes.push_back(idxTy);
226 mlir::Operation::result_range ifRes =
227 builder.
genIfOp(loc, resTypes, info.isPresent,
true)
232 : fir::LoadOp::create(builder, loc, info.addr);
234 gatherBoundsOrBoundValues<BoundsOp, BoundsType>(
235 builder, loc, dataExv, box,
237 fir::ResultOp::create(builder, loc, boundValues);
244 for (
unsigned dim = 0; dim < dataExv.rank(); ++dim) {
245 boundValues.push_back(zero);
246 boundValues.push_back(mOne);
247 boundValues.push_back(zero);
248 boundValues.push_back(zero);
249 boundValues.push_back(zero);
251 fir::ResultOp::create(builder, loc, boundValues);
256 for (
unsigned i = 0; i < ifRes.size(); i += nbValuesPerBound) {
258 BoundsOp::create(builder, loc, boundTy, ifRes[i], ifRes[i + 1],
259 ifRes[i + 2], ifRes[i + 3],
true, ifRes[i + 4]);
260 bounds.push_back(bound);
265 : fir::LoadOp::create(builder, loc, info.addr);
266 bounds = gatherBoundsOrBoundValues<BoundsOp, BoundsType>(builder, loc,
274template <
typename BoundsOp,
typename BoundsType>
278 bool strideIncludeLowerExtent =
false) {
279 mlir::Type idxTy = builder.getIndexType();
280 mlir::Type boundTy = builder.getType<BoundsType>();
283 if (dataExv.rank() == 0)
287 const unsigned rank = dataExv.rank();
288 mlir::Value cumulativeExtent = one;
289 for (
unsigned dim = 0; dim < rank; ++dim) {
294 mlir::Value lb = zero;
296 if (isAssumedSize && dim + 1 == rank) {
301 ub = mlir::arith::SubIOp::create(builder, loc, extent, one);
303 mlir::Value stride = one;
304 if (strideIncludeLowerExtent) {
305 stride = cumulativeExtent;
306 cumulativeExtent = builder.createOrFold<mlir::arith::MulIOp>(
307 loc, cumulativeExtent, extent);
310 mlir::Value bound = BoundsOp::create(builder, loc, boundTy, lb, ub, extent,
311 stride,
false, baseLb);
312 bounds.push_back(bound);
320 if (
auto declareOp = mlir::dyn_cast_or_null<hlfir::DeclareOp>(op))
321 if (declareOp.getFortranAttrs() &&
322 bitEnumContainsAny(*declareOp.getFortranAttrs(),
323 fir::FortranVariableFlagsEnum::optional))
328template <
typename BoundsOp,
typename BoundsType>
332 mlir::Location loc) {
335 mlir::Value baseOp = info.rawInput;
336 if (mlir::isa<fir::BaseBoxType>(fir::unwrapRefType(baseOp.getType())))
339 if (mlir::isa<fir::SequenceType>(fir::unwrapRefType(baseOp.getType()))) {
341 dataExvIsAssumedSize);
344 mlir::isa<fir::BoxCharType>(fir::unwrapRefType(info.addr.getType()))) {
345 bounds = {genBoundsOpFromBoxChar<BoundsOp, BoundsType>(builder, loc,
Definition BoxValue.h:480
Definition FIRBuilder.h:59
IfBuilder genIfOp(mlir::Location loc, mlir::TypeRange results, mlir::Value cdt, bool withElseRegion)
Definition FIRBuilder.h:550
mlir::Type getCharacterLengthType()
Get character length type.
Definition FIRBuilder.h:163
mlir::Type getRefType(mlir::Type eleTy, bool isVolatile=false)
Safely create a reference type to the type eleTy.
Definition FIRBuilder.cpp:109
mlir::Value createMinusOneInteger(mlir::Location loc, mlir::Type integerType)
Definition FIRBuilder.h:197
mlir::Value createIntegerConstant(mlir::Location loc, mlir::Type integerType, std::int64_t i)
Definition FIRBuilder.cpp:145
Definition BoxValue.h:447
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:1028
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:997
bool isOptionalArgument(mlir::Operation *op)
Definition DirectivesCommon.h:319
bool isa_ref_type(mlir::Type t)
Is t a FIR dialect type that implies a memory (de)reference?
Definition FIRType.h:135
bool isBoxAddress(mlir::Type t)
Is t an address to fir.box or class type?
Definition FIRType.h:528
bool characterWithDynamicLen(mlir::Type t)
Returns true iff t is a fir.char type and has an unknown length.
Definition FIRType.h:256
Definition DirectivesCommon.h:31