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();
155 mlir::cast<fir::BoxCharType>(fir::unwrapRefType(info.addr.getType()));
156 unsigned codeUnitBytes =
159 mlir::Value byteStride =
163 using ExtentAndStride = std::tuple<mlir::Value, mlir::Value>;
164 auto [extent, stride] = [&]() -> ExtentAndStride {
165 if (info.isPresent) {
166 llvm::SmallVector<mlir::Type> resTypes = {idxTy, idxTy};
167 mlir::Operation::result_range ifRes =
169 .
genIfOp(loc, resTypes, info.isPresent,
true)
171 mlir::Value boxChar =
173 ? fir::LoadOp::create(builder, loc, info.addr)
175 fir::BoxCharType boxCharType =
176 mlir::cast<fir::BoxCharType>(boxChar.getType());
177 mlir::Type refType = builder.
getRefType(boxCharType.getEleTy());
178 auto unboxed = fir::UnboxCharOp::create(builder, loc, refType,
180 mlir::SmallVector<mlir::Value> results = {unboxed.getResult(1),
182 fir::ResultOp::create(builder, loc, results);
185 mlir::SmallVector<mlir::Value> results = {zero, zero};
186 fir::ResultOp::create(builder, loc, results);
189 return {ifRes[0], ifRes[1]};
194 ? fir::LoadOp::create(builder, loc, info.addr)
196 fir::BoxCharType boxCharType =
197 mlir::cast<fir::BoxCharType>(boxChar.getType());
198 mlir::Type refType = builder.
getRefType(boxCharType.getEleTy());
200 fir::UnboxCharOp::create(builder, loc, refType, lenType, boxChar);
201 return {unboxed.getResult(1), byteStride};
204 mlir::Value ub = mlir::arith::SubIOp::create(builder, loc, extent, one);
205 mlir::Type boundTy = builder.getType<BoundsType>();
206 return BoundsOp::create(builder, loc, boundTy,
216template <
typename BoundsOp,
typename BoundsType>
217llvm::SmallVector<mlir::Value>
221 mlir::Type idxTy = builder.getIndexType();
222 mlir::Type boundTy = builder.getType<BoundsType>();
224 assert(mlir::isa<fir::BaseBoxType>(info.boxType) &&
225 "expect fir.box or fir.class");
226 assert(fir::unwrapRefType(info.addr.getType()) == info.boxType &&
227 "expected box type consistency");
229 if (info.isPresent) {
231 constexpr unsigned nbValuesPerBound = 5;
232 for (
unsigned dim = 0; dim < dataExv.rank() * nbValuesPerBound; ++dim)
233 resTypes.push_back(idxTy);
235 mlir::Operation::result_range ifRes =
236 builder.
genIfOp(loc, resTypes, info.isPresent,
true)
241 : fir::LoadOp::create(builder, loc, info.addr);
243 gatherBoundsOrBoundValues<BoundsOp, BoundsType>(
244 builder, loc, dataExv, box,
246 fir::ResultOp::create(builder, loc, boundValues);
253 for (
unsigned dim = 0; dim < dataExv.rank(); ++dim) {
254 boundValues.push_back(zero);
255 boundValues.push_back(mOne);
256 boundValues.push_back(zero);
257 boundValues.push_back(zero);
258 boundValues.push_back(zero);
260 fir::ResultOp::create(builder, loc, boundValues);
265 for (
unsigned i = 0; i < ifRes.size(); i += nbValuesPerBound) {
267 BoundsOp::create(builder, loc, boundTy, ifRes[i], ifRes[i + 1],
268 ifRes[i + 2], ifRes[i + 3],
true, ifRes[i + 4]);
269 bounds.push_back(bound);
274 : fir::LoadOp::create(builder, loc, info.addr);
275 bounds = gatherBoundsOrBoundValues<BoundsOp, BoundsType>(builder, loc,
283template <
typename BoundsOp,
typename BoundsType>
287 bool strideIncludeLowerExtent =
false) {
288 mlir::Type idxTy = builder.getIndexType();
289 mlir::Type boundTy = builder.getType<BoundsType>();
292 if (dataExv.rank() == 0)
296 const unsigned rank = dataExv.rank();
297 mlir::Value cumulativeExtent = one;
298 for (
unsigned dim = 0; dim < rank; ++dim) {
303 mlir::Value lb = zero;
305 if (isAssumedSize && dim + 1 == rank) {
310 ub = mlir::arith::SubIOp::create(builder, loc, extent, one);
312 mlir::Value stride = one;
313 if (strideIncludeLowerExtent) {
314 stride = cumulativeExtent;
315 cumulativeExtent = builder.createOrFold<mlir::arith::MulIOp>(
316 loc, cumulativeExtent, extent);
319 mlir::Value bound = BoundsOp::create(builder, loc, boundTy, lb, ub, extent,
320 stride,
false, baseLb);
321 bounds.push_back(bound);
329 if (
auto declareOp = mlir::dyn_cast_or_null<hlfir::DeclareOp>(op))
330 if (declareOp.getFortranAttrs() &&
331 bitEnumContainsAny(*declareOp.getFortranAttrs(),
332 fir::FortranVariableFlagsEnum::optional))
337template <
typename BoundsOp,
typename BoundsType>
341 mlir::Location loc) {
344 mlir::Value baseOp = info.rawInput;
345 if (mlir::isa<fir::BaseBoxType>(fir::unwrapRefType(baseOp.getType())))
348 if (mlir::isa<fir::SequenceType>(fir::unwrapRefType(baseOp.getType()))) {
350 dataExvIsAssumedSize);
353 mlir::isa<fir::BoxCharType>(fir::unwrapRefType(info.addr.getType()))) {
354 bounds = {genBoundsOpFromBoxChar<BoundsOp, BoundsType>(builder, loc,
Definition BoxValue.h:469
Definition FIRBuilder.h:66
IfBuilder genIfOp(mlir::Location loc, mlir::TypeRange results, mlir::Value cdt, bool withElseRegion)
Definition FIRBuilder.h:589
mlir::Type getCharacterLengthType()
Get character length type.
Definition FIRBuilder.h:174
const fir::KindMapping & getKindMap()
Get a reference to the kind map.
Definition FIRBuilder.h:133
mlir::Type getRefType(mlir::Type eleTy, bool isVolatile=false)
Safely create a reference type to the type eleTy.
Definition FIRBuilder.cpp:115
mlir::Value createMinusOneInteger(mlir::Location loc, mlir::Type integerType)
Definition FIRBuilder.h:215
mlir::Value createIntegerConstant(mlir::Location loc, mlir::Type integerType, std::int64_t i)
Definition FIRBuilder.cpp:151
Bitsize getCharacterBitsize(KindTy kind) const
Get the size in bits of !fir.char<kind>
Definition KindMapping.cpp:278
Definition BoxValue.h:446
llvm::SmallVector< mlir::Value > genBaseBoundsOps(fir::FirOpBuilder &builder, mlir::Location loc, fir::ExtendedValue dataExv, bool isAssumedSize, bool strideIncludeLowerExtent=false)
Definition DirectivesCommon.h:285
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:218
mlir::Value readLowerBound(fir::FirOpBuilder &builder, mlir::Location loc, const fir::ExtendedValue &box, unsigned dim, mlir::Value defaultValue)
Definition FIRBuilder.cpp:1079
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:1048
bool isOptionalArgument(mlir::Operation *op)
Definition DirectivesCommon.h:328
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:535
bool characterWithDynamicLen(mlir::Type t)
Returns true iff t is a fir.char type and has an unknown length.
Definition FIRType.h:263
Definition DirectivesCommon.h:31