17#ifndef FORTRAN_OPTIMIZER_BUILDER_DIRECTIVESCOMMON_H_
18#define FORTRAN_OPTIMIZER_BUILDER_DIRECTIVESCOMMON_H_
20#include "flang/Optimizer/Builder/BoxValue.h"
21#include "flang/Optimizer/Builder/FIRBuilder.h"
22#include "flang/Optimizer/Builder/Todo.h"
23#include "flang/Optimizer/HLFIR/HLFIROps.h"
24#include "mlir/Dialect/OpenACC/OpenACC.h"
25#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
34 : addr(addr), rawInput(rawInput) {}
36 mlir::Value isPresent)
37 : addr(addr), rawInput(rawInput), isPresent(isPresent) {}
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 mlir::Value rawInput = symAddr;
60 mlir::dyn_cast_or_null<hlfir::DeclareOp>(symAddr.getDefiningOp())) {
61 symAddr = declareOp.getResults()[0];
62 rawInput = declareOp.getResults()[1];
66 llvm::report_fatal_error(
"could not retrieve symbol address");
68 mlir::Value isPresent;
71 builder.create<fir::IsPresentOp>(loc, builder.getI1Type(), rawInput);
73 if (
auto boxTy = mlir::dyn_cast<fir::BaseBoxType>(
74 fir::unwrapRefType(symAddr.getType()))) {
75 if (mlir::isa<fir::RecordType>(boxTy.getEleTy()))
76 TODO(loc,
"derived type");
83 if (mlir::isa<fir::ReferenceType>(symAddr.getType()) && !isOptional) {
84 mlir::Value addr = builder.create<fir::LoadOp>(loc, symAddr);
85 return AddrAndBoundsInfo(addr, rawInput, isPresent, boxTy);
88 return AddrAndBoundsInfo(symAddr, rawInput, isPresent, boxTy);
90 return AddrAndBoundsInfo(symAddr, rawInput, isPresent);
93template <
typename BoundsOp,
typename BoundsType>
97 bool collectValuesOnly =
false) {
98 assert(box &&
"box must exist");
100 mlir::Value byteStride;
101 mlir::Type idxTy = builder.getIndexType();
102 mlir::Type boundTy = builder.getType<BoundsType>();
104 for (
unsigned dim = 0; dim < dataExv.rank(); ++dim) {
109 builder.create<fir::BoxDimsOp>(loc, idxTy, idxTy, idxTy, box, d);
112 builder.create<mlir::arith::SubIOp>(loc, dimInfo.getExtent(), one);
114 byteStride = dimInfo.getByteStride();
115 if (collectValuesOnly) {
116 values.push_back(lb);
117 values.push_back(ub);
118 values.push_back(dimInfo.getExtent());
119 values.push_back(byteStride);
120 values.push_back(baseLb);
122 mlir::Value bound = builder.create<BoundsOp>(
123 loc, boundTy, lb, ub, dimInfo.getExtent(), byteStride,
true, baseLb);
124 values.push_back(bound);
127 byteStride = builder.create<mlir::arith::MulIOp>(loc, byteStride,
128 dimInfo.getExtent());
134template <
typename BoundsOp,
typename BoundsType>
139 mlir::Type idxTy = builder.getIndexType();
140 mlir::Type boundTy = builder.getType<BoundsType>();
142 assert(mlir::isa<fir::BaseBoxType>(info.boxType) &&
143 "expect fir.box or fir.class");
144 assert(fir::unwrapRefType(info.addr.getType()) == info.boxType &&
145 "expected box type consistency");
147 if (info.isPresent) {
149 constexpr unsigned nbValuesPerBound = 5;
150 for (
unsigned dim = 0; dim < dataExv.rank() * nbValuesPerBound; ++dim)
151 resTypes.push_back(idxTy);
153 mlir::Operation::result_range ifRes =
154 builder.
genIfOp(loc, resTypes, info.isPresent,
true)
159 : builder.create<fir::LoadOp>(loc, info.addr);
161 gatherBoundsOrBoundValues<BoundsOp, BoundsType>(
162 builder, loc, dataExv, box,
164 builder.create<fir::ResultOp>(loc, boundValues);
171 for (
unsigned dim = 0; dim < dataExv.rank(); ++dim) {
172 boundValues.push_back(zero);
173 boundValues.push_back(mOne);
174 boundValues.push_back(zero);
175 boundValues.push_back(zero);
176 boundValues.push_back(zero);
178 builder.create<fir::ResultOp>(loc, boundValues);
183 for (
unsigned i = 0; i < ifRes.size(); i += nbValuesPerBound) {
184 mlir::Value bound = builder.create<BoundsOp>(
185 loc, boundTy, ifRes[i], ifRes[i + 1], ifRes[i + 2], ifRes[i + 3],
187 bounds.push_back(bound);
192 : builder.create<fir::LoadOp>(loc, info.addr);
193 bounds = gatherBoundsOrBoundValues<BoundsOp, BoundsType>(builder, loc,
201template <
typename BoundsOp,
typename BoundsType>
205 mlir::Type idxTy = builder.getIndexType();
206 mlir::Type boundTy = builder.getType<BoundsType>();
209 if (dataExv.rank() == 0)
213 const unsigned rank = dataExv.rank();
214 for (
unsigned dim = 0; dim < rank; ++dim) {
219 mlir::Value lb = zero;
221 if (isAssumedSize && dim + 1 == rank) {
226 ub = builder.create<mlir::arith::SubIOp>(loc, ext, one);
230 builder.create<BoundsOp>(loc, boundTy, lb, ub, ext, one,
false, baseLb);
231 bounds.push_back(bound);
236template <
typename BoundsOp,
typename BoundsType>
240 mlir::Location loc) {
243 mlir::Value baseOp = info.rawInput;
244 if (mlir::isa<fir::BaseBoxType>(fir::unwrapRefType(baseOp.getType())))
246 genBoundsOpsFromBox<BoundsOp, BoundsType>(builder, loc, dataExv, info);
247 if (mlir::isa<fir::SequenceType>(fir::unwrapRefType(baseOp.getType()))) {
248 bounds = genBaseBoundsOps<BoundsOp, BoundsType>(builder, loc, dataExv,
249 dataExvIsAssumedSize);
Definition: BoxValue.h:478
Definition: FIRBuilder.h:55
IfBuilder genIfOp(mlir::Location loc, mlir::TypeRange results, mlir::Value cdt, bool withElseRegion)
Definition: FIRBuilder.h:463
mlir::Value createMinusOneInteger(mlir::Location loc, mlir::Type integerType)
Definition: FIRBuilder.h:187
mlir::Value createIntegerConstant(mlir::Location loc, mlir::Type integerType, std::int64_t i)
Definition: FIRBuilder.cpp:131
Definition: BoxValue.h:445
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:136
mlir::Value readLowerBound(fir::FirOpBuilder &builder, mlir::Location loc, const fir::ExtendedValue &box, unsigned dim, mlir::Value defaultValue)
Definition: FIRBuilder.cpp:905
llvm::SmallVector< mlir::Value > genBaseBoundsOps(fir::FirOpBuilder &builder, mlir::Location loc, fir::ExtendedValue dataExv, bool isAssumedSize)
Definition: DirectivesCommon.h:203
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:873
bool isBoxAddress(mlir::Type t)
Is t an address to fir.box or class type?
Definition: FIRType.h:475
Definition: DirectivesCommon.h:31