15#ifndef FORTRAN_OPTIMIZER_BUILDER_FACTORY_H
16#define FORTRAN_OPTIMIZER_BUILDER_FACTORY_H
18#include "flang/Optimizer/Dialect/FIROps.h"
19#include "flang/Optimizer/Dialect/FIRType.h"
20#include "mlir/Dialect/Func/IR/FuncOps.h"
21#include "llvm/ADT/iterator_range.h"
28namespace fir::factory {
30constexpr llvm::StringRef attrFortranArrayOffsets() {
31 return "Fortran.offsets";
43void genCharacterCopy(mlir::Value src, mlir::Value srcLen, mlir::Value dst,
44 mlir::Value dstLen, B &builder, mlir::Location loc) {
49 if (!srcLen && !dstLen && srcTy.getFKind() == dstTy.getFKind() &&
50 srcTy.getLen() == dstTy.getLen()) {
52 auto load = builder.template create<fir::LoadOp>(loc, src);
53 builder.template create<fir::StoreOp>(loc, load, dst);
56 auto zero = builder.template create<mlir::arith::ConstantIndexOp>(loc, 0);
57 auto one = builder.template create<mlir::arith::ConstantIndexOp>(loc, 1);
58 auto toArrayTy = [&](fir::CharacterType ty) {
59 return fir::ReferenceType::get(fir::SequenceType::get(
60 fir::SequenceType::ShapeRef{fir::SequenceType::getUnknownExtent()},
61 fir::CharacterType::getSingleton(ty.getContext(), ty.getFKind())));
63 auto toEleTy = [&](fir::ReferenceType ty) {
64 auto seqTy = mlir::cast<fir::SequenceType>(ty.getEleTy());
65 return mlir::cast<fir::CharacterType>(seqTy.getEleTy());
67 auto toCoorTy = [&](fir::ReferenceType ty) {
68 return fir::ReferenceType::get(toEleTy(ty));
70 if (!srcLen && !dstLen && srcTy.getLen() >= dstTy.getLen()) {
71 auto upper = builder.template create<mlir::arith::ConstantIndexOp>(
72 loc, dstTy.getLen() - 1);
73 auto loop = builder.template create<fir::DoLoopOp>(loc, zero, upper, one);
74 auto insPt = builder.saveInsertionPoint();
75 builder.setInsertionPointToStart(loop.getBody());
76 auto csrcTy = toArrayTy(srcTy);
77 auto csrc = builder.template create<fir::ConvertOp>(loc, csrcTy, src);
78 auto in = builder.template create<fir::CoordinateOp>(
79 loc, toCoorTy(csrcTy), csrc, loop.getInductionVar());
80 auto load = builder.template create<fir::LoadOp>(loc, in);
81 auto cdstTy = toArrayTy(dstTy);
82 auto cdst = builder.template create<fir::ConvertOp>(loc, cdstTy, dst);
83 auto out = builder.template create<fir::CoordinateOp>(
84 loc, toCoorTy(cdstTy), cdst, loop.getInductionVar());
86 srcTy.getFKind() == dstTy.getFKind()
89 .template create<fir::ConvertOp>(loc, toEleTy(cdstTy), load)
91 builder.template create<fir::StoreOp>(loc, cast, out);
92 builder.restoreInsertionPoint(insPt);
95 auto minusOne = [&](mlir::Value v) -> mlir::Value {
96 return builder.template create<mlir::arith::SubIOp>(
97 loc, builder.template create<fir::ConvertOp>(loc, one.getType(), v),
100 mlir::Value len = dstLen ? minusOne(dstLen)
102 .template create<
mlir::arith::ConstantIndexOp>(
105 auto loop = builder.template create<fir::DoLoopOp>(loc, zero, len, one);
106 auto insPt = builder.saveInsertionPoint();
107 builder.setInsertionPointToStart(loop.getBody());
110 ? builder.template create<fir::ConvertOp>(loc, one.getType(), srcLen)
113 .template create<mlir::arith::ConstantIndexOp>(loc,
116 auto cond = builder.template create<mlir::arith::CmpIOp>(
117 loc, mlir::arith::CmpIPredicate::slt, loop.getInductionVar(), slen);
118 auto ifOp = builder.template create<fir::IfOp>(loc, cond,
true);
119 builder.setInsertionPointToStart(&ifOp.getThenRegion().front());
120 auto csrcTy = toArrayTy(srcTy);
121 auto csrc = builder.template create<fir::ConvertOp>(loc, csrcTy, src);
122 auto in = builder.template create<fir::CoordinateOp>(
123 loc, toCoorTy(csrcTy), csrc, loop.getInductionVar());
124 auto load = builder.template create<fir::LoadOp>(loc, in);
125 auto cdstTy = toArrayTy(dstTy);
126 auto cdst = builder.template create<fir::ConvertOp>(loc, cdstTy, dst);
127 auto out = builder.template create<fir::CoordinateOp>(
128 loc, toCoorTy(cdstTy), cdst, loop.getInductionVar());
130 srcTy.getFKind() == dstTy.getFKind()
132 : builder.template create<fir::ConvertOp>(loc, toEleTy(cdstTy), load)
134 builder.template create<fir::StoreOp>(loc, cast, out);
135 builder.setInsertionPointToStart(&ifOp.getElseRegion().front());
136 auto space = builder.template create<fir::StringLitOp>(
138 auto cdst2 = builder.template create<fir::ConvertOp>(loc, cdstTy, dst);
139 auto out2 = builder.template create<fir::CoordinateOp>(
140 loc, toCoorTy(cdstTy), cdst2, loop.getInductionVar());
141 builder.template create<fir::StoreOp>(loc, space, out2);
142 builder.restoreInsertionPoint(insPt);
149 if (
auto *shapeOp = shapeVal.getDefiningOp()) {
150 if (
auto shOp = mlir::dyn_cast<fir::ShapeOp>(shapeOp)) {
151 auto operands = shOp.getExtents();
152 return {operands.begin(), operands.end()};
154 if (
auto shOp = mlir::dyn_cast<fir::ShapeShiftOp>(shapeOp)) {
155 auto operands = shOp.getExtents();
156 return {operands.begin(), operands.end()};
166 if (
auto *shapeOp = shapeVal.getDefiningOp()) {
167 if (
auto shOp = mlir::dyn_cast<fir::ShapeShiftOp>(shapeOp)) {
168 auto operands = shOp.getOrigins();
169 return {operands.begin(), operands.end()};
171 if (
auto shOp = mlir::dyn_cast<fir::ShiftOp>(shapeOp)) {
172 auto operands = shOp.getOrigins();
173 return {operands.begin(), operands.end()};
186originateIndices(mlir::Location loc, B &builder, mlir::Type memTy,
187 mlir::Value shapeVal, mlir::ValueRange indices) {
189 auto origins = getOrigins(shapeVal);
190 if (origins.empty()) {
191 assert(!shapeVal || mlir::isa<fir::ShapeOp>(shapeVal.getDefiningOp()));
193 assert(ty && mlir::isa<fir::SequenceType>(ty));
194 auto seqTy = mlir::cast<fir::SequenceType>(ty);
195 auto one = builder.template create<mlir::arith::ConstantIndexOp>(loc, 1);
196 const auto dimension = seqTy.getDimension();
198 assert(dimension == mlir::cast<fir::ShapeOp>(shapeVal.getDefiningOp())
202 for (
auto i : llvm::enumerate(indices)) {
203 if (i.index() < dimension) {
206 builder.template create<mlir::arith::AddIOp>(loc, i.value(), one));
208 result.push_back(i.value());
213 const auto dimension = origins.size();
214 unsigned origOff = 0;
215 for (
auto i : llvm::enumerate(indices)) {
216 if (i.index() < dimension)
217 result.push_back(builder.template create<mlir::arith::AddIOp>(
218 loc, i.value(), origins[origOff++]));
220 result.push_back(i.value());
mlir::Value getLen(const ExtendedValue &exv)
Definition: BoxValue.cpp:26
mlir::Type dyn_cast_ptrEleTy(mlir::Type t)
Definition: FIRType.cpp:216
mlir::Type dyn_cast_ptrOrBoxEleTy(mlir::Type t)
Definition: FIRType.cpp:223
bool isa_integer(mlir::Type t)
Is t an integral type?
Definition: FIRType.h:166
@ Value
Lower argument to a value. Mainly intended for scalar arguments.
Definition: AbstractConverter.h:27