| 1 |
//===-- HLFIROps.cpp ------------------------------------------------------===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
// |
| 9 |
// Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/ |
| 10 |
// |
| 11 |
//===----------------------------------------------------------------------===// |
| 12 |
|
| 13 |
#include "flang/Optimizer/HLFIR/HLFIROps.h" |
| 14 |
#include "flang/Optimizer/Dialect/FIROpsSupport.h" |
| 15 |
#include "mlir/IR/Builders.h" |
| 16 |
#include "mlir/IR/BuiltinTypes.h" |
| 17 |
#include "mlir/IR/DialectImplementation.h" |
| 18 |
#include "mlir/IR/Matchers.h" |
| 19 |
#include "mlir/IR/OpImplementation.h" |
| 20 |
#include "llvm/ADT/TypeSwitch.h" |
| 21 |
#include <optional> |
| 22 |
#include <tuple> |
| 23 |
|
| 24 |
//===----------------------------------------------------------------------===// |
| 25 |
// DeclareOp |
| 26 |
//===----------------------------------------------------------------------===// |
| 27 |
|
| 28 |
/// Given a FIR memory type, and information about non default lower bounds, get |
| 29 |
/// the related HLFIR variable type. |
| 30 |
mlir::Type hlfir::DeclareOp::getHLFIRVariableType(mlir::Type inputType, |
| 31 |
bool hasExplicitLowerBounds) { |
| 32 |
mlir::Type type = fir::unwrapRefType(inputType); |
| 33 |
if (type.isa<fir::BaseBoxType>()) |
| 34 |
return inputType; |
| 35 |
if (auto charType = type.dyn_cast<fir::CharacterType>()) |
| 36 |
if (charType.hasDynamicLen()) |
| 37 |
return fir::BoxCharType::get(charType.getContext(), charType.getFKind()); |
| 38 |
|
| 39 |
auto seqType = type.dyn_cast<fir::SequenceType>(); |
| 40 |
bool hasDynamicExtents = |
| 41 |
seqType && fir::sequenceWithNonConstantShape(seqType); |
| 42 |
mlir::Type eleType = seqType ? seqType.getEleTy() : type; |
| 43 |
bool hasDynamicLengthParams = fir::characterWithDynamicLen(eleType) || |
| 44 |
fir::isRecordWithTypeParameters(eleType); |
| 45 |
if (hasExplicitLowerBounds || hasDynamicExtents || hasDynamicLengthParams) |
| 46 |
return fir::BoxType::get(type); |
| 47 |
return inputType; |
| 48 |
} |
| 49 |
|
| 50 |
static bool hasExplicitLowerBounds(mlir::Value shape) { |
| 51 |
return shape && shape.getType().isa<fir::ShapeShiftType, fir::ShiftType>(); |
| 52 |
} |
| 53 |
|
| 54 |
void hlfir::DeclareOp::build(mlir::OpBuilder &builder, |
| 55 |
mlir::OperationState &result, mlir::Value memref, |
| 56 |
llvm::StringRef uniq_name, mlir::Value shape, |
| 57 |
mlir::ValueRange typeparams, |
| 58 |
fir::FortranVariableFlagsAttr fortran_attrs) { |
| 59 |
auto nameAttr = builder.getStringAttr(uniq_name); |
| 60 |
mlir::Type inputType = memref.getType(); |
| 61 |
bool hasExplicitLbs = hasExplicitLowerBounds(shape); |
| 62 |
mlir::Type hlfirVariableType = |
| 63 |
getHLFIRVariableType(inputType, hasExplicitLbs); |
| 64 |
build(builder, result, {hlfirVariableType, inputType}, memref, shape, |
| 65 |
typeparams, nameAttr, fortran_attrs); |
| 66 |
} |
| 67 |
|
| 68 |
mlir::LogicalResult hlfir::DeclareOp::verify() { |
| 69 |
if (getMemref().getType() != getResult(1).getType()) |
| 70 |
return emitOpError("second result type must match input memref type"); |
| 71 |
mlir::Type hlfirVariableType = getHLFIRVariableType( |
| 72 |
getMemref().getType(), hasExplicitLowerBounds(getShape())); |
| 73 |
if (hlfirVariableType != getResult(0).getType()) |
| 74 |
return emitOpError("first result type is inconsistent with variable " |
| 75 |
"properties: expected ") |
| 76 |
<< hlfirVariableType; |
| 77 |
// The rest of the argument verification is done by the |
| 78 |
// FortranVariableInterface verifier. |
| 79 |
auto fortranVar = |
| 80 |
mlir::cast<fir::FortranVariableOpInterface>(this->getOperation()); |
| 81 |
return fortranVar.verifyDeclareLikeOpImpl(getMemref()); |
| 82 |
} |
| 83 |
|
| 84 |
//===----------------------------------------------------------------------===// |
| 85 |
// DesignateOp |
| 86 |
//===----------------------------------------------------------------------===// |
| 87 |
|
| 88 |
void hlfir::DesignateOp::build( |
| 89 |
mlir::OpBuilder &builder, mlir::OperationState &result, |
| 90 |
mlir::Type result_type, mlir::Value memref, llvm::StringRef component, |
| 91 |
mlir::Value component_shape, llvm::ArrayRef<Subscript> subscripts, |
| 92 |
mlir::ValueRange substring, std::optional<bool> complex_part, |
| 93 |
mlir::Value shape, mlir::ValueRange typeparams, |
| 94 |
fir::FortranVariableFlagsAttr fortran_attrs) { |
| 95 |
auto componentAttr = |
| 96 |
component.empty() ? mlir::StringAttr{} : builder.getStringAttr(component); |
| 97 |
llvm::SmallVector<mlir::Value> indices; |
| 98 |
llvm::SmallVector<bool> isTriplet; |
| 99 |
for (auto subscript : subscripts) { |
| 100 |
if (auto *triplet = std::get_if<Triplet>(&subscript)) { |
| 101 |
isTriplet.push_back(true); |
| 102 |
indices.push_back(std::get<0>(*triplet)); |
| 103 |
indices.push_back(std::get<1>(*triplet)); |
| 104 |
indices.push_back(std::get<2>(*triplet)); |
| 105 |
} else { |
| 106 |
isTriplet.push_back(false); |
| 107 |
indices.push_back(std::get<mlir::Value>(subscript)); |
| 108 |
} |
| 109 |
} |
| 110 |
auto isTripletAttr = |
| 111 |
mlir::DenseBoolArrayAttr::get(builder.getContext(), isTriplet); |
| 112 |
auto complexPartAttr = |
| 113 |
complex_part.has_value() |
| 114 |
? mlir::BoolAttr::get(builder.getContext(), *complex_part) |
| 115 |
: mlir::BoolAttr{}; |
| 116 |
build(builder, result, result_type, memref, componentAttr, component_shape, |
| 117 |
indices, isTripletAttr, substring, complexPartAttr, shape, typeparams, |
| 118 |
fortran_attrs); |
| 119 |
} |
| 120 |
|
| 121 |
void hlfir::DesignateOp::build(mlir::OpBuilder &builder, |
| 122 |
mlir::OperationState &result, |
| 123 |
mlir::Type result_type, mlir::Value memref, |
| 124 |
mlir::ValueRange indices, |
| 125 |
mlir::ValueRange typeparams, |
| 126 |
fir::FortranVariableFlagsAttr fortran_attrs) { |
| 127 |
llvm::SmallVector<bool> isTriplet(indices.size(), false); |
| 128 |
auto isTripletAttr = |
| 129 |
mlir::DenseBoolArrayAttr::get(builder.getContext(), isTriplet); |
| 130 |
build(builder, result, result_type, memref, |
| 131 |
/*componentAttr=*/mlir::StringAttr{}, /*component_shape=*/mlir::Value{}, |
| 132 |
indices, isTripletAttr, /*substring*/ mlir::ValueRange{}, |
| 133 |
/*complexPartAttr=*/mlir::BoolAttr{}, /*shape=*/mlir::Value{}, |
| 134 |
typeparams, fortran_attrs); |
| 135 |
} |
| 136 |
|
| 137 |
static mlir::ParseResult parseDesignatorIndices( |
| 138 |
mlir::OpAsmParser &parser, |
| 139 |
llvm::SmallVectorImpl<mlir::OpAsmParser::UnresolvedOperand> &indices, |
| 140 |
mlir::DenseBoolArrayAttr &isTripletAttr) { |
| 141 |
llvm::SmallVector<bool> isTriplet; |
| 142 |
if (mlir::succeeded(parser.parseOptionalLParen())) { |
| 143 |
do { |
| 144 |
mlir::OpAsmParser::UnresolvedOperand i1, i2, i3; |
| 145 |
if (parser.parseOperand(i1)) |
| 146 |
return mlir::failure(); |
| 147 |
indices.push_back(i1); |
| 148 |
if (mlir::succeeded(parser.parseOptionalColon())) { |
| 149 |
if (parser.parseOperand(i2) || parser.parseColon() || |
| 150 |
parser.parseOperand(i3)) |
| 151 |
return mlir::failure(); |
| 152 |
indices.push_back(i2); |
| 153 |
indices.push_back(i3); |
| 154 |
isTriplet.push_back(true); |
| 155 |
} else { |
| 156 |
isTriplet.push_back(false); |
| 157 |
} |
| 158 |
} while (mlir::succeeded(parser.parseOptionalComma())); |
| 159 |
if (parser.parseRParen()) |
| 160 |
return mlir::failure(); |
| 161 |
} |
| 162 |
isTripletAttr = mlir::DenseBoolArrayAttr::get(parser.getContext(), isTriplet); |
| 163 |
return mlir::success(); |
| 164 |
} |
| 165 |
|
| 166 |
static void |
| 167 |
printDesignatorIndices(mlir::OpAsmPrinter &p, hlfir::DesignateOp designateOp, |
| 168 |
mlir::OperandRange indices, |
| 169 |
const mlir::DenseBoolArrayAttr &isTripletAttr) { |
| 170 |
if (!indices.empty()) { |
| 171 |
p << '('; |
| 172 |
unsigned i = 0; |
| 173 |
for (auto isTriplet : isTripletAttr.asArrayRef()) { |
| 174 |
if (isTriplet) { |
| 175 |
assert(i + 2 < indices.size() && "ill-formed indices"); |
| 176 |
p << indices[i] << ":" << indices[i + 1] << ":" << indices[i + 2]; |
| 177 |
i += 3; |
| 178 |
} else { |
| 179 |
p << indices[i++]; |
| 180 |
} |
| 181 |
if (i != indices.size()) |
| 182 |
p << ", "; |
| 183 |
} |
| 184 |
p << ')'; |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
static mlir::ParseResult |
| 189 |
parseDesignatorComplexPart(mlir::OpAsmParser &parser, |
| 190 |
mlir::BoolAttr &complexPart) { |
| 191 |
if (mlir::succeeded(parser.parseOptionalKeyword("imag"))) |
| 192 |
complexPart = mlir::BoolAttr::get(parser.getContext(), true); |
| 193 |
else if (mlir::succeeded(parser.parseOptionalKeyword("real"))) |
| 194 |
complexPart = mlir::BoolAttr::get(parser.getContext(), false); |
| 195 |
return mlir::success(); |
| 196 |
} |
| 197 |
|
| 198 |
static void printDesignatorComplexPart(mlir::OpAsmPrinter &p, |
| 199 |
hlfir::DesignateOp designateOp, |
| 200 |
mlir::BoolAttr complexPartAttr) { |
| 201 |
if (complexPartAttr) { |
| 202 |
if (complexPartAttr.getValue()) |
| 203 |
p << "imag"; |
| 204 |
else |
| 205 |
p << "real"; |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
mlir::LogicalResult hlfir::DesignateOp::verify() { |
| 210 |
mlir::Type memrefType = getMemref().getType(); |
| 211 |
mlir::Type baseType = getFortranElementOrSequenceType(memrefType); |
| 212 |
mlir::Type baseElementType = fir::unwrapSequenceType(baseType); |
| 213 |
unsigned numSubscripts = getIsTriplet().size(); |
| 214 |
unsigned subscriptsRank = |
| 215 |
llvm::count_if(getIsTriplet(), [](bool isTriplet) { return isTriplet; }); |
| 216 |
unsigned outputRank = 0; |
| 217 |
mlir::Type outputElementType; |
| 218 |
bool hasBoxComponent; |
| 219 |
if (getComponent()) { |
| 220 |
auto component = getComponent().value(); |
| 221 |
auto recType = baseElementType.dyn_cast<fir::RecordType>(); |
| 222 |
if (!recType) |
| 223 |
return emitOpError( |
| 224 |
"component must be provided only when the memref is a derived type"); |
| 225 |
unsigned fieldIdx = recType.getFieldIndex(component); |
| 226 |
if (fieldIdx > recType.getNumFields()) { |
| 227 |
return emitOpError("component ") |
| 228 |
<< component << " is not a component of memref element type " |
| 229 |
<< recType; |
| 230 |
} |
| 231 |
mlir::Type fieldType = recType.getType(fieldIdx); |
| 232 |
mlir::Type componentBaseType = getFortranElementOrSequenceType(fieldType); |
| 233 |
hasBoxComponent = fieldType.isa<fir::BaseBoxType>(); |
| 234 |
if (componentBaseType.isa<fir::SequenceType>() && |
| 235 |
baseType.isa<fir::SequenceType>() && |
| 236 |
(numSubscripts == 0 || subscriptsRank > 0)) |
| 237 |
return emitOpError("indices must be provided and must not contain " |
| 238 |
"triplets when both memref and component are arrays"); |
| 239 |
if (numSubscripts != 0) { |
| 240 |
if (!componentBaseType.isa<fir::SequenceType>()) |
| 241 |
return emitOpError("indices must not be provided if component appears " |
| 242 |
"and is not an array component"); |
| 243 |
if (!getComponentShape()) |
| 244 |
return emitOpError( |
| 245 |
"component_shape must be provided when indexing a component"); |
| 246 |
mlir::Type compShapeType = getComponentShape().getType(); |
| 247 |
unsigned componentRank = |
| 248 |
componentBaseType.cast<fir::SequenceType>().getDimension(); |
| 249 |
auto shapeType = compShapeType.dyn_cast<fir::ShapeType>(); |
| 250 |
auto shapeShiftType = compShapeType.dyn_cast<fir::ShapeShiftType>(); |
| 251 |
if (!((shapeType && shapeType.getRank() == componentRank) || |
| 252 |
(shapeShiftType && shapeShiftType.getRank() == componentRank))) |
| 253 |
return emitOpError("component_shape must be a fir.shape or " |
| 254 |
"fir.shapeshift with the rank of the component"); |
| 255 |
if (numSubscripts > componentRank) |
| 256 |
return emitOpError("indices number must match array component rank"); |
| 257 |
} |
| 258 |
if (auto baseSeqType = baseType.dyn_cast<fir::SequenceType>()) |
| 259 |
// This case must come first to cover "array%array_comp(i, j)" that has |
| 260 |
// subscripts for the component but whose rank come from the base. |
| 261 |
outputRank = baseSeqType.getDimension(); |
| 262 |
else if (numSubscripts != 0) |
| 263 |
outputRank = subscriptsRank; |
| 264 |
else if (auto componentSeqType = |
| 265 |
componentBaseType.dyn_cast<fir::SequenceType>()) |
| 266 |
outputRank = componentSeqType.getDimension(); |
| 267 |
outputElementType = fir::unwrapSequenceType(componentBaseType); |
| 268 |
} else { |
| 269 |
outputElementType = baseElementType; |
| 270 |
unsigned baseTypeRank = |
| 271 |
baseType.isa<fir::SequenceType>() |
| 272 |
? baseType.cast<fir::SequenceType>().getDimension() |
| 273 |
: 0; |
| 274 |
if (numSubscripts != 0) { |
| 275 |
if (baseTypeRank != numSubscripts) |
| 276 |
return emitOpError("indices number must match memref rank"); |
| 277 |
outputRank = subscriptsRank; |
| 278 |
} else if (auto baseSeqType = baseType.dyn_cast<fir::SequenceType>()) { |
| 279 |
outputRank = baseSeqType.getDimension(); |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
if (!getSubstring().empty()) { |
| 284 |
if (!outputElementType.isa<fir::CharacterType>()) |
| 285 |
return emitOpError("memref or component must have character type if " |
| 286 |
"substring indices are provided"); |
| 287 |
if (getSubstring().size() != 2) |
| 288 |
return emitOpError("substring must contain 2 indices when provided"); |
| 289 |
} |
| 290 |
if (getComplexPart()) { |
| 291 |
if (!fir::isa_complex(outputElementType)) |
| 292 |
return emitOpError("memref or component must have complex type if " |
| 293 |
"complex_part is provided"); |
| 294 |
if (auto firCplx = outputElementType.dyn_cast<fir::ComplexType>()) |
| 295 |
outputElementType = firCplx.getElementType(); |
| 296 |
else |
| 297 |
outputElementType = |
| 298 |
outputElementType.cast<mlir::ComplexType>().getElementType(); |
| 299 |
} |
| 300 |
mlir::Type resultBaseType = |
| 301 |
getFortranElementOrSequenceType(getResult().getType()); |
| 302 |
unsigned resultRank = 0; |
| 303 |
if (auto resultSeqType = resultBaseType.dyn_cast<fir::SequenceType>()) |
| 304 |
resultRank = resultSeqType.getDimension(); |
| 305 |
if (resultRank != outputRank) |
| 306 |
return emitOpError("result type rank is not consistent with operands, " |
| 307 |
"expected rank ") |
| 308 |
<< outputRank; |
| 309 |
mlir::Type resultElementType = fir::unwrapSequenceType(resultBaseType); |
| 310 |
// result type must match the one that was inferred here, except the character |
| 311 |
// length may differ because of substrings. |
| 312 |
if (resultElementType != outputElementType && |
| 313 |
!(resultElementType.isa<fir::CharacterType>() && |
| 314 |
outputElementType.isa<fir::CharacterType>())) |
| 315 |
return emitOpError( |
| 316 |
"result element type is not consistent with operands, expected ") |
| 317 |
<< outputElementType; |
| 318 |
|
| 319 |
if (isBoxAddressType(getResult().getType())) { |
| 320 |
if (!hasBoxComponent || numSubscripts != 0 || !getSubstring().empty() || |
| 321 |
getComplexPart()) |
| 322 |
return emitOpError( |
| 323 |
"result type must only be a box address type if it designates a " |
| 324 |
"component that is a fir.box or fir.class and if there are no " |
| 325 |
"indices, substrings, and complex part"); |
| 326 |
|
| 327 |
} else { |
| 328 |
if ((resultRank == 0) != !getShape()) |
| 329 |
return emitOpError("shape must be provided if and only if the result is " |
| 330 |
"an array that is not a box address"); |
| 331 |
if (resultRank != 0) { |
| 332 |
auto shapeType = getShape().getType().dyn_cast<fir::ShapeType>(); |
| 333 |
auto shapeShiftType = |
| 334 |
getShape().getType().dyn_cast<fir::ShapeShiftType>(); |
| 335 |
if (!((shapeType && shapeType.getRank() == resultRank) || |
| 336 |
(shapeShiftType && shapeShiftType.getRank() == resultRank))) |
| 337 |
return emitOpError("shape must be a fir.shape or fir.shapeshift with " |
| 338 |
"the rank of the result"); |
| 339 |
} |
| 340 |
auto numLenParam = getTypeparams().size(); |
| 341 |
if (outputElementType.isa<fir::CharacterType>()) { |
| 342 |
if (numLenParam != 1) |
| 343 |
return emitOpError("must be provided one length parameter when the " |
| 344 |
"result is a character"); |
| 345 |
} else if (fir::isRecordWithTypeParameters(outputElementType)) { |
| 346 |
if (numLenParam != |
| 347 |
outputElementType.cast<fir::RecordType>().getNumLenParams()) |
| 348 |
return emitOpError("must be provided the same number of length " |
| 349 |
"parameters as in the result derived type"); |
| 350 |
} else if (numLenParam != 0) { |
| 351 |
return emitOpError("must not be provided length parameters if the result " |
| 352 |
"type does not have length parameters"); |
| 353 |
} |
| 354 |
} |
| 355 |
return mlir::success(); |
| 356 |
} |
| 357 |
|
| 358 |
//===----------------------------------------------------------------------===// |
| 359 |
// ConcatOp |
| 360 |
//===----------------------------------------------------------------------===// |
| 361 |
|
| 362 |
static unsigned getCharacterKind(mlir::Type t) { |
| 363 |
return hlfir::getFortranElementType(t).cast<fir::CharacterType>().getFKind(); |
| 364 |
} |
| 365 |
|
| 366 |
static std::optional<fir::CharacterType::LenType> |
| 367 |
getCharacterLengthIfStatic(mlir::Type t) { |
| 368 |
if (auto charType = |
| 369 |
hlfir::getFortranElementType(t).dyn_cast<fir::CharacterType>()) |
| 370 |
if (charType.hasConstantLen()) |
| 371 |
return charType.getLen(); |
| 372 |
return std::nullopt; |
| 373 |
} |
| 374 |
|
| 375 |
mlir::LogicalResult hlfir::ConcatOp::verify() { |
| 376 |
if (getStrings().size() < 2) |
| 377 |
return emitOpError("must be provided at least two string operands"); |
| 378 |
unsigned kind = getCharacterKind(getResult().getType()); |
| 379 |
for (auto string : getStrings()) |
| 380 |
if (kind != getCharacterKind(string.getType())) |
| 381 |
return emitOpError("strings must have the same KIND as the result type"); |
| 382 |
return mlir::success(); |
| 383 |
} |
| 384 |
|
| 385 |
void hlfir::ConcatOp::build(mlir::OpBuilder &builder, |
| 386 |
mlir::OperationState &result, |
| 387 |
mlir::ValueRange strings, mlir::Value len) { |
| 388 |
fir::CharacterType::LenType resultTypeLen = 0; |
| 389 |
assert(!strings.empty() && "must contain operands"); |
| 390 |
unsigned kind = getCharacterKind(strings[0].getType()); |
| 391 |
for (auto string : strings) |
| 392 |
if (auto cstLen = getCharacterLengthIfStatic(string.getType())) { |
| 393 |
resultTypeLen += *cstLen; |
| 394 |
} else { |
| 395 |
resultTypeLen = fir::CharacterType::unknownLen(); |
| 396 |
break; |
| 397 |
} |
| 398 |
auto resultType = hlfir::ExprType::get( |
| 399 |
builder.getContext(), hlfir::ExprType::Shape{}, |
| 400 |
fir::CharacterType::get(builder.getContext(), kind, resultTypeLen), |
| 401 |
false); |
| 402 |
build(builder, result, resultType, strings, len); |
| 403 |
} |
| 404 |
|
| 405 |
//===----------------------------------------------------------------------===// |
| 406 |
// SetLengthOp |
| 407 |
//===----------------------------------------------------------------------===// |
| 408 |
|
| 409 |
void hlfir::SetLengthOp::build(mlir::OpBuilder &builder, |
| 410 |
mlir::OperationState &result, mlir::Value string, |
| 411 |
mlir::Value len) { |
| 412 |
fir::CharacterType::LenType resultTypeLen = fir::CharacterType::unknownLen(); |
| 413 |
if (auto cstLen = fir::getIntIfConstant(len)) |
| 414 |
resultTypeLen = *cstLen; |
| 415 |
unsigned kind = getCharacterKind(string.getType()); |
| 416 |
auto resultType = hlfir::ExprType::get( |
| 417 |
builder.getContext(), hlfir::ExprType::Shape{}, |
| 418 |
fir::CharacterType::get(builder.getContext(), kind, resultTypeLen), |
| 419 |
false); |
| 420 |
build(builder, result, resultType, string, len); |
| 421 |
} |
| 422 |
|
| 423 |
//===----------------------------------------------------------------------===// |
| 424 |
// AssociateOp |
| 425 |
//===----------------------------------------------------------------------===// |
| 426 |
|
| 427 |
void hlfir::AssociateOp::build(mlir::OpBuilder &builder, |
| 428 |
mlir::OperationState &result, mlir::Value source, |
| 429 |
llvm::StringRef uniq_name, mlir::Value shape, |
| 430 |
mlir::ValueRange typeparams, |
| 431 |
fir::FortranVariableFlagsAttr fortran_attrs) { |
| 432 |
auto nameAttr = builder.getStringAttr(uniq_name); |
| 433 |
// TODO: preserve polymorphism of polymorphic expr. |
| 434 |
mlir::Type firVarType = fir::ReferenceType::get( |
| 435 |
getFortranElementOrSequenceType(source.getType())); |
| 436 |
mlir::Type hlfirVariableType = |
| 437 |
DeclareOp::getHLFIRVariableType(firVarType, /*hasExplicitLbs=*/false); |
| 438 |
mlir::Type i1Type = builder.getI1Type(); |
| 439 |
build(builder, result, {hlfirVariableType, firVarType, i1Type}, source, shape, |
| 440 |
typeparams, nameAttr, fortran_attrs); |
| 441 |
} |
| 442 |
|
| 443 |
//===----------------------------------------------------------------------===// |
| 444 |
// EndAssociateOp |
| 445 |
//===----------------------------------------------------------------------===// |
| 446 |
|
| 447 |
void hlfir::EndAssociateOp::build(mlir::OpBuilder &builder, |
| 448 |
mlir::OperationState &result, |
| 449 |
hlfir::AssociateOp associate) { |
| 450 |
return build(builder, result, associate.getFirBase(), |
| 451 |
associate.getMustFreeStrorageFlag()); |
| 452 |
} |
| 453 |
|
| 454 |
//===----------------------------------------------------------------------===// |
| 455 |
// AsExprOp |
| 456 |
//===----------------------------------------------------------------------===// |
| 457 |
|
| 458 |
void hlfir::AsExprOp::build(mlir::OpBuilder &builder, |
| 459 |
mlir::OperationState &result, mlir::Value var, |
| 460 |
mlir::Value mustFree) { |
| 461 |
hlfir::ExprType::Shape typeShape; |
| 462 |
mlir::Type type = getFortranElementOrSequenceType(var.getType()); |
| 463 |
if (auto seqType = type.dyn_cast<fir::SequenceType>()) { |
| 464 |
typeShape.append(seqType.getShape().begin(), seqType.getShape().end()); |
| 465 |
type = seqType.getEleTy(); |
| 466 |
} |
| 467 |
|
| 468 |
auto resultType = hlfir::ExprType::get(builder.getContext(), typeShape, type, |
| 469 |
/*isPolymorphic: TODO*/ false); |
| 470 |
return build(builder, result, resultType, var, mustFree); |
| 471 |
} |
| 472 |
|
| 473 |
//===----------------------------------------------------------------------===// |
| 474 |
// ElementalOp |
| 475 |
//===----------------------------------------------------------------------===// |
| 476 |
|
| 477 |
void hlfir::ElementalOp::build(mlir::OpBuilder &builder, |
| 478 |
mlir::OperationState &odsState, |
| 479 |
mlir::Type resultType, mlir::Value shape, |
| 480 |
mlir::ValueRange typeparams) { |
| 481 |
odsState.addOperands(shape); |
| 482 |
odsState.addOperands(typeparams); |
| 483 |
odsState.addTypes(resultType); |
| 484 |
mlir::Region *bodyRegion = odsState.addRegion(); |
| 485 |
bodyRegion->push_back(new mlir::Block{}); |
| 486 |
if (auto exprType = resultType.dyn_cast<hlfir::ExprType>()) { |
| 487 |
unsigned dim = exprType.getRank(); |
| 488 |
mlir::Type indexType = builder.getIndexType(); |
| 489 |
for (unsigned d = 0; d < dim; ++d) |
| 490 |
bodyRegion->front().addArgument(indexType, odsState.location); |
| 491 |
} |
| 492 |
} |
| 493 |
|
| 494 |
//===----------------------------------------------------------------------===// |
| 495 |
// ApplyOp |
| 496 |
//===----------------------------------------------------------------------===// |
| 497 |
|
| 498 |
void hlfir::ApplyOp::build(mlir::OpBuilder &builder, |
| 499 |
mlir::OperationState &odsState, mlir::Value expr, |
| 500 |
mlir::ValueRange indices, |
| 501 |
mlir::ValueRange typeparams) { |
| 502 |
mlir::Type resultType = expr.getType(); |
| 503 |
if (auto exprType = resultType.dyn_cast<hlfir::ExprType>()) |
| 504 |
resultType = exprType.getElementExprType(); |
| 505 |
build(builder, odsState, resultType, expr, indices, typeparams); |
| 506 |
} |
| 507 |
|
| 508 |
//===----------------------------------------------------------------------===// |
| 509 |
// NullOp |
| 510 |
//===----------------------------------------------------------------------===// |
| 511 |
|
| 512 |
void hlfir::NullOp::build(mlir::OpBuilder &builder, |
| 513 |
mlir::OperationState &odsState) { |
| 514 |
return build(builder, odsState, |
| 515 |
fir::ReferenceType::get(builder.getNoneType())); |
| 516 |
} |
| 517 |
|
| 518 |
#define GET_OP_CLASSES |
| 519 |
#include "flang/Optimizer/HLFIR/HLFIROps.cpp.inc" |