1 //===-- FIROps.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/Dialect/FIROps.h" 14 #include "flang/Optimizer/Dialect/FIRAttr.h" 15 #include "flang/Optimizer/Dialect/FIROpsSupport.h" 16 #include "flang/Optimizer/Dialect/FIRType.h" 17 #include "flang/Optimizer/Support/FIRContext.h" 18 #include "flang/Optimizer/Support/KindMapping.h" 19 #include "flang/Optimizer/Support/Utils.h" 20 #include "mlir/Dialect/CommonFolders.h" 21 #include "mlir/Dialect/Func/IR/FuncOps.h" 22 #include "mlir/IR/BuiltinAttributes.h" 23 #include "mlir/IR/BuiltinOps.h" 24 #include "mlir/IR/Diagnostics.h" 25 #include "mlir/IR/Matchers.h" 26 #include "mlir/IR/OpDefinition.h" 27 #include "mlir/IR/PatternMatch.h" 28 #include "llvm/ADT/STLExtras.h" 29 #include "llvm/ADT/SmallVector.h" 30 #include "llvm/ADT/StringSwitch.h" 31 #include "llvm/ADT/TypeSwitch.h" 32 33 namespace { 34 #include "flang/Optimizer/Dialect/CanonicalizationPatterns.inc" 35 } // namespace 36 37 /// Return true if a sequence type is of some incomplete size or a record type 38 /// is malformed or contains an incomplete sequence type. An incomplete sequence 39 /// type is one with more unknown extents in the type than have been provided 40 /// via `dynamicExtents`. Sequence types with an unknown rank are incomplete by 41 /// definition. 42 static bool verifyInType(mlir::Type inType, 43 llvm::SmallVectorImpl<llvm::StringRef> &visited, 44 unsigned dynamicExtents = 0) { 45 if (auto st = inType.dyn_cast<fir::SequenceType>()) { 46 auto shape = st.getShape(); 47 if (shape.size() == 0) 48 return true; 49 for (std::size_t i = 0, end = shape.size(); i < end; ++i) { 50 if (shape[i] != fir::SequenceType::getUnknownExtent()) 51 continue; 52 if (dynamicExtents-- == 0) 53 return true; 54 } 55 } else if (auto rt = inType.dyn_cast<fir::RecordType>()) { 56 // don't recurse if we're already visiting this one 57 if (llvm::is_contained(visited, rt.getName())) 58 return false; 59 // keep track of record types currently being visited 60 visited.push_back(rt.getName()); 61 for (auto &field : rt.getTypeList()) 62 if (verifyInType(field.second, visited)) 63 return true; 64 visited.pop_back(); 65 } 66 return false; 67 } 68 69 static bool verifyTypeParamCount(mlir::Type inType, unsigned numParams) { 70 auto ty = fir::unwrapSequenceType(inType); 71 if (numParams > 0) { 72 if (auto recTy = ty.dyn_cast<fir::RecordType>()) 73 return numParams != recTy.getNumLenParams(); 74 if (auto chrTy = ty.dyn_cast<fir::CharacterType>()) 75 return !(numParams == 1 && chrTy.hasDynamicLen()); 76 return true; 77 } 78 if (auto chrTy = ty.dyn_cast<fir::CharacterType>()) 79 return !chrTy.hasConstantLen(); 80 return false; 81 } 82 83 /// Parser shared by Alloca and Allocmem 84 /// 85 /// operation ::= %res = (`fir.alloca` | `fir.allocmem`) $in_type 86 /// ( `(` $typeparams `)` )? ( `,` $shape )? 87 /// attr-dict-without-keyword 88 template <typename FN> 89 static mlir::ParseResult parseAllocatableOp(FN wrapResultType, 90 mlir::OpAsmParser &parser, 91 mlir::OperationState &result) { 92 mlir::Type intype; 93 if (parser.parseType(intype)) 94 return mlir::failure(); 95 auto &builder = parser.getBuilder(); 96 result.addAttribute("in_type", mlir::TypeAttr::get(intype)); 97 llvm::SmallVector<mlir::OpAsmParser::UnresolvedOperand> operands; 98 llvm::SmallVector<mlir::Type> typeVec; 99 bool hasOperands = false; 100 std::int32_t typeparamsSize = 0; 101 if (!parser.parseOptionalLParen()) { 102 // parse the LEN params of the derived type. (<params> : <types>) 103 if (parser.parseOperandList(operands, mlir::OpAsmParser::Delimiter::None) || 104 parser.parseColonTypeList(typeVec) || parser.parseRParen()) 105 return mlir::failure(); 106 typeparamsSize = operands.size(); 107 hasOperands = true; 108 } 109 std::int32_t shapeSize = 0; 110 if (!parser.parseOptionalComma()) { 111 // parse size to scale by, vector of n dimensions of type index 112 if (parser.parseOperandList(operands, mlir::OpAsmParser::Delimiter::None)) 113 return mlir::failure(); 114 shapeSize = operands.size() - typeparamsSize; 115 auto idxTy = builder.getIndexType(); 116 for (std::int32_t i = typeparamsSize, end = operands.size(); i != end; ++i) 117 typeVec.push_back(idxTy); 118 hasOperands = true; 119 } 120 if (hasOperands && 121 parser.resolveOperands(operands, typeVec, parser.getNameLoc(), 122 result.operands)) 123 return mlir::failure(); 124 mlir::Type restype = wrapResultType(intype); 125 if (!restype) { 126 parser.emitError(parser.getNameLoc(), "invalid allocate type: ") << intype; 127 return mlir::failure(); 128 } 129 result.addAttribute("operand_segment_sizes", 130 builder.getI32VectorAttr({typeparamsSize, shapeSize})); 131 if (parser.parseOptionalAttrDict(result.attributes) || 132 parser.addTypeToList(restype, result.types)) 133 return mlir::failure(); 134 return mlir::success(); 135 } 136 137 template <typename OP> 138 static void printAllocatableOp(mlir::OpAsmPrinter &p, OP &op) { 139 p << ' ' << op.getInType(); 140 if (!op.getTypeparams().empty()) { 141 p << '(' << op.getTypeparams() << " : " << op.getTypeparams().getTypes() 142 << ')'; 143 } 144 // print the shape of the allocation (if any); all must be index type 145 for (auto sh : op.getShape()) { 146 p << ", "; 147 p.printOperand(sh); 148 } 149 p.printOptionalAttrDict(op->getAttrs(), {"in_type", "operand_segment_sizes"}); 150 } 151 152 //===----------------------------------------------------------------------===// 153 // AllocaOp 154 //===----------------------------------------------------------------------===// 155 156 /// Create a legal memory reference as return type 157 static mlir::Type wrapAllocaResultType(mlir::Type intype) { 158 // FIR semantics: memory references to memory references are disallowed 159 if (intype.isa<fir::ReferenceType>()) 160 return {}; 161 return fir::ReferenceType::get(intype); 162 } 163 164 mlir::Type fir::AllocaOp::getAllocatedType() { 165 return getType().cast<fir::ReferenceType>().getEleTy(); 166 } 167 168 mlir::Type fir::AllocaOp::getRefTy(mlir::Type ty) { 169 return fir::ReferenceType::get(ty); 170 } 171 172 void fir::AllocaOp::build(mlir::OpBuilder &builder, 173 mlir::OperationState &result, mlir::Type inType, 174 llvm::StringRef uniqName, mlir::ValueRange typeparams, 175 mlir::ValueRange shape, 176 llvm::ArrayRef<mlir::NamedAttribute> attributes) { 177 auto nameAttr = builder.getStringAttr(uniqName); 178 build(builder, result, wrapAllocaResultType(inType), inType, nameAttr, {}, 179 /*pinned=*/false, typeparams, shape); 180 result.addAttributes(attributes); 181 } 182 183 void fir::AllocaOp::build(mlir::OpBuilder &builder, 184 mlir::OperationState &result, mlir::Type inType, 185 llvm::StringRef uniqName, bool pinned, 186 mlir::ValueRange typeparams, mlir::ValueRange shape, 187 llvm::ArrayRef<mlir::NamedAttribute> attributes) { 188 auto nameAttr = builder.getStringAttr(uniqName); 189 build(builder, result, wrapAllocaResultType(inType), inType, nameAttr, {}, 190 pinned, typeparams, shape); 191 result.addAttributes(attributes); 192 } 193 194 void fir::AllocaOp::build(mlir::OpBuilder &builder, 195 mlir::OperationState &result, mlir::Type inType, 196 llvm::StringRef uniqName, llvm::StringRef bindcName, 197 mlir::ValueRange typeparams, mlir::ValueRange shape, 198 llvm::ArrayRef<mlir::NamedAttribute> attributes) { 199 auto nameAttr = 200 uniqName.empty() ? mlir::StringAttr{} : builder.getStringAttr(uniqName); 201 auto bindcAttr = 202 bindcName.empty() ? mlir::StringAttr{} : builder.getStringAttr(bindcName); 203 build(builder, result, wrapAllocaResultType(inType), inType, nameAttr, 204 bindcAttr, /*pinned=*/false, typeparams, shape); 205 result.addAttributes(attributes); 206 } 207 208 void fir::AllocaOp::build(mlir::OpBuilder &builder, 209 mlir::OperationState &result, mlir::Type inType, 210 llvm::StringRef uniqName, llvm::StringRef bindcName, 211 bool pinned, mlir::ValueRange typeparams, 212 mlir::ValueRange shape, 213 llvm::ArrayRef<mlir::NamedAttribute> attributes) { 214 auto nameAttr = 215 uniqName.empty() ? mlir::StringAttr{} : builder.getStringAttr(uniqName); 216 auto bindcAttr = 217 bindcName.empty() ? mlir::StringAttr{} : builder.getStringAttr(bindcName); 218 build(builder, result, wrapAllocaResultType(inType), inType, nameAttr, 219 bindcAttr, pinned, typeparams, shape); 220 result.addAttributes(attributes); 221 } 222 223 void fir::AllocaOp::build(mlir::OpBuilder &builder, 224 mlir::OperationState &result, mlir::Type inType, 225 mlir::ValueRange typeparams, mlir::ValueRange shape, 226 llvm::ArrayRef<mlir::NamedAttribute> attributes) { 227 build(builder, result, wrapAllocaResultType(inType), inType, {}, {}, 228 /*pinned=*/false, typeparams, shape); 229 result.addAttributes(attributes); 230 } 231 232 void fir::AllocaOp::build(mlir::OpBuilder &builder, 233 mlir::OperationState &result, mlir::Type inType, 234 bool pinned, mlir::ValueRange typeparams, 235 mlir::ValueRange shape, 236 llvm::ArrayRef<mlir::NamedAttribute> attributes) { 237 build(builder, result, wrapAllocaResultType(inType), inType, {}, {}, pinned, 238 typeparams, shape); 239 result.addAttributes(attributes); 240 } 241 242 mlir::ParseResult fir::AllocaOp::parse(mlir::OpAsmParser &parser, 243 mlir::OperationState &result) { 244 return parseAllocatableOp(wrapAllocaResultType, parser, result); 245 } 246 247 void fir::AllocaOp::print(mlir::OpAsmPrinter &p) { 248 printAllocatableOp(p, *this); 249 } 250 251 mlir::LogicalResult fir::AllocaOp::verify() { 252 llvm::SmallVector<llvm::StringRef> visited; 253 if (verifyInType(getInType(), visited, numShapeOperands())) 254 return emitOpError("invalid type for allocation"); 255 if (verifyTypeParamCount(getInType(), numLenParams())) 256 return emitOpError("LEN params do not correspond to type"); 257 mlir::Type outType = getType(); 258 if (!outType.isa<fir::ReferenceType>()) 259 return emitOpError("must be a !fir.ref type"); 260 if (fir::isa_unknown_size_box(fir::dyn_cast_ptrEleTy(outType))) 261 return emitOpError("cannot allocate !fir.box of unknown rank or type"); 262 return mlir::success(); 263 } 264 265 //===----------------------------------------------------------------------===// 266 // AllocMemOp 267 //===----------------------------------------------------------------------===// 268 269 /// Create a legal heap reference as return type 270 static mlir::Type wrapAllocMemResultType(mlir::Type intype) { 271 // Fortran semantics: C852 an entity cannot be both ALLOCATABLE and POINTER 272 // 8.5.3 note 1 prohibits ALLOCATABLE procedures as well 273 // FIR semantics: one may not allocate a memory reference value 274 if (intype.isa<fir::ReferenceType, fir::HeapType, fir::PointerType, 275 mlir::FunctionType>()) 276 return {}; 277 return fir::HeapType::get(intype); 278 } 279 280 mlir::Type fir::AllocMemOp::getAllocatedType() { 281 return getType().cast<fir::HeapType>().getEleTy(); 282 } 283 284 mlir::Type fir::AllocMemOp::getRefTy(mlir::Type ty) { 285 return fir::HeapType::get(ty); 286 } 287 288 void fir::AllocMemOp::build(mlir::OpBuilder &builder, 289 mlir::OperationState &result, mlir::Type inType, 290 llvm::StringRef uniqName, 291 mlir::ValueRange typeparams, mlir::ValueRange shape, 292 llvm::ArrayRef<mlir::NamedAttribute> attributes) { 293 auto nameAttr = builder.getStringAttr(uniqName); 294 build(builder, result, wrapAllocMemResultType(inType), inType, nameAttr, {}, 295 typeparams, shape); 296 result.addAttributes(attributes); 297 } 298 299 void fir::AllocMemOp::build(mlir::OpBuilder &builder, 300 mlir::OperationState &result, mlir::Type inType, 301 llvm::StringRef uniqName, llvm::StringRef bindcName, 302 mlir::ValueRange typeparams, mlir::ValueRange shape, 303 llvm::ArrayRef<mlir::NamedAttribute> attributes) { 304 auto nameAttr = builder.getStringAttr(uniqName); 305 auto bindcAttr = builder.getStringAttr(bindcName); 306 build(builder, result, wrapAllocMemResultType(inType), inType, nameAttr, 307 bindcAttr, typeparams, shape); 308 result.addAttributes(attributes); 309 } 310 311 void fir::AllocMemOp::build(mlir::OpBuilder &builder, 312 mlir::OperationState &result, mlir::Type inType, 313 mlir::ValueRange typeparams, mlir::ValueRange shape, 314 llvm::ArrayRef<mlir::NamedAttribute> attributes) { 315 build(builder, result, wrapAllocMemResultType(inType), inType, {}, {}, 316 typeparams, shape); 317 result.addAttributes(attributes); 318 } 319 320 mlir::ParseResult fir::AllocMemOp::parse(mlir::OpAsmParser &parser, 321 mlir::OperationState &result) { 322 return parseAllocatableOp(wrapAllocMemResultType, parser, result); 323 } 324 325 void fir::AllocMemOp::print(mlir::OpAsmPrinter &p) { 326 printAllocatableOp(p, *this); 327 } 328 329 mlir::LogicalResult fir::AllocMemOp::verify() { 330 llvm::SmallVector<llvm::StringRef> visited; 331 if (verifyInType(getInType(), visited, numShapeOperands())) 332 return emitOpError("invalid type for allocation"); 333 if (verifyTypeParamCount(getInType(), numLenParams())) 334 return emitOpError("LEN params do not correspond to type"); 335 mlir::Type outType = getType(); 336 if (!outType.dyn_cast<fir::HeapType>()) 337 return emitOpError("must be a !fir.heap type"); 338 if (fir::isa_unknown_size_box(fir::dyn_cast_ptrEleTy(outType))) 339 return emitOpError("cannot allocate !fir.box of unknown rank or type"); 340 return mlir::success(); 341 } 342 343 //===----------------------------------------------------------------------===// 344 // ArrayCoorOp 345 //===----------------------------------------------------------------------===// 346 347 mlir::LogicalResult fir::ArrayCoorOp::verify() { 348 auto eleTy = fir::dyn_cast_ptrOrBoxEleTy(getMemref().getType()); 349 auto arrTy = eleTy.dyn_cast<fir::SequenceType>(); 350 if (!arrTy) 351 return emitOpError("must be a reference to an array"); 352 auto arrDim = arrTy.getDimension(); 353 354 if (auto shapeOp = getShape()) { 355 auto shapeTy = shapeOp.getType(); 356 unsigned shapeTyRank = 0; 357 if (auto s = shapeTy.dyn_cast<fir::ShapeType>()) { 358 shapeTyRank = s.getRank(); 359 } else if (auto ss = shapeTy.dyn_cast<fir::ShapeShiftType>()) { 360 shapeTyRank = ss.getRank(); 361 } else { 362 auto s = shapeTy.cast<fir::ShiftType>(); 363 shapeTyRank = s.getRank(); 364 if (!getMemref().getType().isa<fir::BoxType>()) 365 return emitOpError("shift can only be provided with fir.box memref"); 366 } 367 if (arrDim && arrDim != shapeTyRank) 368 return emitOpError("rank of dimension mismatched"); 369 if (shapeTyRank != getIndices().size()) 370 return emitOpError("number of indices do not match dim rank"); 371 } 372 373 if (auto sliceOp = getSlice()) { 374 if (auto sl = mlir::dyn_cast_or_null<fir::SliceOp>(sliceOp.getDefiningOp())) 375 if (!sl.getSubstr().empty()) 376 return emitOpError("array_coor cannot take a slice with substring"); 377 if (auto sliceTy = sliceOp.getType().dyn_cast<fir::SliceType>()) 378 if (sliceTy.getRank() != arrDim) 379 return emitOpError("rank of dimension in slice mismatched"); 380 } 381 382 return mlir::success(); 383 } 384 385 //===----------------------------------------------------------------------===// 386 // ArrayLoadOp 387 //===----------------------------------------------------------------------===// 388 389 static mlir::Type adjustedElementType(mlir::Type t) { 390 if (auto ty = t.dyn_cast<fir::ReferenceType>()) { 391 auto eleTy = ty.getEleTy(); 392 if (fir::isa_char(eleTy)) 393 return eleTy; 394 if (fir::isa_derived(eleTy)) 395 return eleTy; 396 if (eleTy.isa<fir::SequenceType>()) 397 return eleTy; 398 } 399 return t; 400 } 401 402 std::vector<mlir::Value> fir::ArrayLoadOp::getExtents() { 403 if (auto sh = getShape()) 404 if (auto *op = sh.getDefiningOp()) { 405 if (auto shOp = mlir::dyn_cast<fir::ShapeOp>(op)) { 406 auto extents = shOp.getExtents(); 407 return {extents.begin(), extents.end()}; 408 } 409 return mlir::cast<fir::ShapeShiftOp>(op).getExtents(); 410 } 411 return {}; 412 } 413 414 mlir::LogicalResult fir::ArrayLoadOp::verify() { 415 auto eleTy = fir::dyn_cast_ptrOrBoxEleTy(getMemref().getType()); 416 auto arrTy = eleTy.dyn_cast<fir::SequenceType>(); 417 if (!arrTy) 418 return emitOpError("must be a reference to an array"); 419 auto arrDim = arrTy.getDimension(); 420 421 if (auto shapeOp = getShape()) { 422 auto shapeTy = shapeOp.getType(); 423 unsigned shapeTyRank = 0u; 424 if (auto s = shapeTy.dyn_cast<fir::ShapeType>()) { 425 shapeTyRank = s.getRank(); 426 } else if (auto ss = shapeTy.dyn_cast<fir::ShapeShiftType>()) { 427 shapeTyRank = ss.getRank(); 428 } else { 429 auto s = shapeTy.cast<fir::ShiftType>(); 430 shapeTyRank = s.getRank(); 431 if (!getMemref().getType().isa<fir::BoxType>()) 432 return emitOpError("shift can only be provided with fir.box memref"); 433 } 434 if (arrDim && arrDim != shapeTyRank) 435 return emitOpError("rank of dimension mismatched"); 436 } 437 438 if (auto sliceOp = getSlice()) { 439 if (auto sl = mlir::dyn_cast_or_null<fir::SliceOp>(sliceOp.getDefiningOp())) 440 if (!sl.getSubstr().empty()) 441 return emitOpError("array_load cannot take a slice with substring"); 442 if (auto sliceTy = sliceOp.getType().dyn_cast<fir::SliceType>()) 443 if (sliceTy.getRank() != arrDim) 444 return emitOpError("rank of dimension in slice mismatched"); 445 } 446 447 return mlir::success(); 448 } 449 450 //===----------------------------------------------------------------------===// 451 // ArrayMergeStoreOp 452 //===----------------------------------------------------------------------===// 453 454 mlir::LogicalResult fir::ArrayMergeStoreOp::verify() { 455 if (!mlir::isa<fir::ArrayLoadOp>(getOriginal().getDefiningOp())) 456 return emitOpError("operand #0 must be result of a fir.array_load op"); 457 if (auto sl = getSlice()) { 458 if (auto sliceOp = 459 mlir::dyn_cast_or_null<fir::SliceOp>(sl.getDefiningOp())) { 460 if (!sliceOp.getSubstr().empty()) 461 return emitOpError( 462 "array_merge_store cannot take a slice with substring"); 463 if (!sliceOp.getFields().empty()) { 464 // This is an intra-object merge, where the slice is projecting the 465 // subfields that are to be overwritten by the merge operation. 466 auto eleTy = fir::dyn_cast_ptrOrBoxEleTy(getMemref().getType()); 467 if (auto seqTy = eleTy.dyn_cast<fir::SequenceType>()) { 468 auto projTy = 469 fir::applyPathToType(seqTy.getEleTy(), sliceOp.getFields()); 470 if (fir::unwrapSequenceType(getOriginal().getType()) != projTy) 471 return emitOpError( 472 "type of origin does not match sliced memref type"); 473 if (fir::unwrapSequenceType(getSequence().getType()) != projTy) 474 return emitOpError( 475 "type of sequence does not match sliced memref type"); 476 return mlir::success(); 477 } 478 return emitOpError("referenced type is not an array"); 479 } 480 } 481 return mlir::success(); 482 } 483 auto eleTy = fir::dyn_cast_ptrOrBoxEleTy(getMemref().getType()); 484 if (getOriginal().getType() != eleTy) 485 return emitOpError("type of origin does not match memref element type"); 486 if (getSequence().getType() != eleTy) 487 return emitOpError("type of sequence does not match memref element type"); 488 return mlir::success(); 489 } 490 491 //===----------------------------------------------------------------------===// 492 // ArrayFetchOp 493 //===----------------------------------------------------------------------===// 494 495 // Template function used for both array_fetch and array_update verification. 496 template <typename A> 497 mlir::Type validArraySubobject(A op) { 498 auto ty = op.getSequence().getType(); 499 return fir::applyPathToType(ty, op.getIndices()); 500 } 501 502 mlir::LogicalResult fir::ArrayFetchOp::verify() { 503 auto arrTy = getSequence().getType().cast<fir::SequenceType>(); 504 auto indSize = getIndices().size(); 505 if (indSize < arrTy.getDimension()) 506 return emitOpError("number of indices != dimension of array"); 507 if (indSize == arrTy.getDimension() && 508 ::adjustedElementType(getElement().getType()) != arrTy.getEleTy()) 509 return emitOpError("return type does not match array"); 510 auto ty = validArraySubobject(*this); 511 if (!ty || ty != ::adjustedElementType(getType())) 512 return emitOpError("return type and/or indices do not type check"); 513 if (!mlir::isa<fir::ArrayLoadOp>(getSequence().getDefiningOp())) 514 return emitOpError("argument #0 must be result of fir.array_load"); 515 return mlir::success(); 516 } 517 518 //===----------------------------------------------------------------------===// 519 // ArrayAccessOp 520 //===----------------------------------------------------------------------===// 521 522 mlir::LogicalResult fir::ArrayAccessOp::verify() { 523 auto arrTy = getSequence().getType().cast<fir::SequenceType>(); 524 std::size_t indSize = getIndices().size(); 525 if (indSize < arrTy.getDimension()) 526 return emitOpError("number of indices != dimension of array"); 527 if (indSize == arrTy.getDimension() && 528 getElement().getType() != fir::ReferenceType::get(arrTy.getEleTy())) 529 return emitOpError("return type does not match array"); 530 mlir::Type ty = validArraySubobject(*this); 531 if (!ty || fir::ReferenceType::get(ty) != getType()) 532 return emitOpError("return type and/or indices do not type check"); 533 return mlir::success(); 534 } 535 536 //===----------------------------------------------------------------------===// 537 // ArrayUpdateOp 538 //===----------------------------------------------------------------------===// 539 540 mlir::LogicalResult fir::ArrayUpdateOp::verify() { 541 if (fir::isa_ref_type(getMerge().getType())) 542 return emitOpError("does not support reference type for merge"); 543 auto arrTy = getSequence().getType().cast<fir::SequenceType>(); 544 auto indSize = getIndices().size(); 545 if (indSize < arrTy.getDimension()) 546 return emitOpError("number of indices != dimension of array"); 547 if (indSize == arrTy.getDimension() && 548 ::adjustedElementType(getMerge().getType()) != arrTy.getEleTy()) 549 return emitOpError("merged value does not have element type"); 550 auto ty = validArraySubobject(*this); 551 if (!ty || ty != ::adjustedElementType(getMerge().getType())) 552 return emitOpError("merged value and/or indices do not type check"); 553 return mlir::success(); 554 } 555 556 //===----------------------------------------------------------------------===// 557 // ArrayModifyOp 558 //===----------------------------------------------------------------------===// 559 560 mlir::LogicalResult fir::ArrayModifyOp::verify() { 561 auto arrTy = getSequence().getType().cast<fir::SequenceType>(); 562 auto indSize = getIndices().size(); 563 if (indSize < arrTy.getDimension()) 564 return emitOpError("number of indices must match array dimension"); 565 return mlir::success(); 566 } 567 568 //===----------------------------------------------------------------------===// 569 // BoxAddrOp 570 //===----------------------------------------------------------------------===// 571 572 mlir::OpFoldResult fir::BoxAddrOp::fold(llvm::ArrayRef<mlir::Attribute> opnds) { 573 if (auto *v = getVal().getDefiningOp()) { 574 if (auto box = mlir::dyn_cast<fir::EmboxOp>(v)) { 575 if (!box.getSlice()) // Fold only if not sliced 576 return box.getMemref(); 577 } 578 if (auto box = mlir::dyn_cast<fir::EmboxCharOp>(v)) 579 return box.getMemref(); 580 } 581 return {}; 582 } 583 584 //===----------------------------------------------------------------------===// 585 // BoxCharLenOp 586 //===----------------------------------------------------------------------===// 587 588 mlir::OpFoldResult 589 fir::BoxCharLenOp::fold(llvm::ArrayRef<mlir::Attribute> opnds) { 590 if (auto v = getVal().getDefiningOp()) { 591 if (auto box = mlir::dyn_cast<fir::EmboxCharOp>(v)) 592 return box.getLen(); 593 } 594 return {}; 595 } 596 597 //===----------------------------------------------------------------------===// 598 // BoxDimsOp 599 //===----------------------------------------------------------------------===// 600 601 /// Get the result types packed in a tuple tuple 602 mlir::Type fir::BoxDimsOp::getTupleType() { 603 // note: triple, but 4 is nearest power of 2 604 llvm::SmallVector<mlir::Type> triple{ 605 getResult(0).getType(), getResult(1).getType(), getResult(2).getType()}; 606 return mlir::TupleType::get(getContext(), triple); 607 } 608 609 //===----------------------------------------------------------------------===// 610 // CallOp 611 //===----------------------------------------------------------------------===// 612 613 mlir::FunctionType fir::CallOp::getFunctionType() { 614 return mlir::FunctionType::get(getContext(), getOperandTypes(), 615 getResultTypes()); 616 } 617 618 void fir::CallOp::print(mlir::OpAsmPrinter &p) { 619 bool isDirect = getCallee().hasValue(); 620 p << ' '; 621 if (isDirect) 622 p << getCallee().getValue(); 623 else 624 p << getOperand(0); 625 p << '(' << (*this)->getOperands().drop_front(isDirect ? 0 : 1) << ')'; 626 p.printOptionalAttrDict((*this)->getAttrs(), 627 {fir::CallOp::getCalleeAttrNameStr()}); 628 auto resultTypes{getResultTypes()}; 629 llvm::SmallVector<mlir::Type> argTypes( 630 llvm::drop_begin(getOperandTypes(), isDirect ? 0 : 1)); 631 p << " : " << mlir::FunctionType::get(getContext(), argTypes, resultTypes); 632 } 633 634 mlir::ParseResult fir::CallOp::parse(mlir::OpAsmParser &parser, 635 mlir::OperationState &result) { 636 llvm::SmallVector<mlir::OpAsmParser::UnresolvedOperand> operands; 637 if (parser.parseOperandList(operands)) 638 return mlir::failure(); 639 640 mlir::NamedAttrList attrs; 641 mlir::SymbolRefAttr funcAttr; 642 bool isDirect = operands.empty(); 643 if (isDirect) 644 if (parser.parseAttribute(funcAttr, fir::CallOp::getCalleeAttrNameStr(), 645 attrs)) 646 return mlir::failure(); 647 648 mlir::Type type; 649 if (parser.parseOperandList(operands, mlir::OpAsmParser::Delimiter::Paren) || 650 parser.parseOptionalAttrDict(attrs) || parser.parseColon() || 651 parser.parseType(type)) 652 return mlir::failure(); 653 654 auto funcType = type.dyn_cast<mlir::FunctionType>(); 655 if (!funcType) 656 return parser.emitError(parser.getNameLoc(), "expected function type"); 657 if (isDirect) { 658 if (parser.resolveOperands(operands, funcType.getInputs(), 659 parser.getNameLoc(), result.operands)) 660 return mlir::failure(); 661 } else { 662 auto funcArgs = 663 llvm::ArrayRef<mlir::OpAsmParser::UnresolvedOperand>(operands) 664 .drop_front(); 665 if (parser.resolveOperand(operands[0], funcType, result.operands) || 666 parser.resolveOperands(funcArgs, funcType.getInputs(), 667 parser.getNameLoc(), result.operands)) 668 return mlir::failure(); 669 } 670 result.addTypes(funcType.getResults()); 671 result.attributes = attrs; 672 return mlir::success(); 673 } 674 675 void fir::CallOp::build(mlir::OpBuilder &builder, mlir::OperationState &result, 676 mlir::func::FuncOp callee, mlir::ValueRange operands) { 677 result.addOperands(operands); 678 result.addAttribute(getCalleeAttrNameStr(), mlir::SymbolRefAttr::get(callee)); 679 result.addTypes(callee.getFunctionType().getResults()); 680 } 681 682 void fir::CallOp::build(mlir::OpBuilder &builder, mlir::OperationState &result, 683 mlir::SymbolRefAttr callee, 684 llvm::ArrayRef<mlir::Type> results, 685 mlir::ValueRange operands) { 686 result.addOperands(operands); 687 if (callee) 688 result.addAttribute(getCalleeAttrNameStr(), callee); 689 result.addTypes(results); 690 } 691 692 //===----------------------------------------------------------------------===// 693 // CmpOp 694 //===----------------------------------------------------------------------===// 695 696 template <typename OPTY> 697 static void printCmpOp(mlir::OpAsmPrinter &p, OPTY op) { 698 p << ' '; 699 auto predSym = mlir::arith::symbolizeCmpFPredicate( 700 op->template getAttrOfType<mlir::IntegerAttr>( 701 OPTY::getPredicateAttrName()) 702 .getInt()); 703 assert(predSym.hasValue() && "invalid symbol value for predicate"); 704 p << '"' << mlir::arith::stringifyCmpFPredicate(predSym.getValue()) << '"' 705 << ", "; 706 p.printOperand(op.getLhs()); 707 p << ", "; 708 p.printOperand(op.getRhs()); 709 p.printOptionalAttrDict(op->getAttrs(), 710 /*elidedAttrs=*/{OPTY::getPredicateAttrName()}); 711 p << " : " << op.getLhs().getType(); 712 } 713 714 template <typename OPTY> 715 static mlir::ParseResult parseCmpOp(mlir::OpAsmParser &parser, 716 mlir::OperationState &result) { 717 llvm::SmallVector<mlir::OpAsmParser::UnresolvedOperand> ops; 718 mlir::NamedAttrList attrs; 719 mlir::Attribute predicateNameAttr; 720 mlir::Type type; 721 if (parser.parseAttribute(predicateNameAttr, OPTY::getPredicateAttrName(), 722 attrs) || 723 parser.parseComma() || parser.parseOperandList(ops, 2) || 724 parser.parseOptionalAttrDict(attrs) || parser.parseColonType(type) || 725 parser.resolveOperands(ops, type, result.operands)) 726 return mlir::failure(); 727 728 if (!predicateNameAttr.isa<mlir::StringAttr>()) 729 return parser.emitError(parser.getNameLoc(), 730 "expected string comparison predicate attribute"); 731 732 // Rewrite string attribute to an enum value. 733 llvm::StringRef predicateName = 734 predicateNameAttr.cast<mlir::StringAttr>().getValue(); 735 auto predicate = fir::CmpcOp::getPredicateByName(predicateName); 736 auto builder = parser.getBuilder(); 737 mlir::Type i1Type = builder.getI1Type(); 738 attrs.set(OPTY::getPredicateAttrName(), 739 builder.getI64IntegerAttr(static_cast<std::int64_t>(predicate))); 740 result.attributes = attrs; 741 result.addTypes({i1Type}); 742 return mlir::success(); 743 } 744 745 //===----------------------------------------------------------------------===// 746 // CharConvertOp 747 //===----------------------------------------------------------------------===// 748 749 mlir::LogicalResult fir::CharConvertOp::verify() { 750 auto unwrap = [&](mlir::Type t) { 751 t = fir::unwrapSequenceType(fir::dyn_cast_ptrEleTy(t)); 752 return t.dyn_cast<fir::CharacterType>(); 753 }; 754 auto inTy = unwrap(getFrom().getType()); 755 auto outTy = unwrap(getTo().getType()); 756 if (!(inTy && outTy)) 757 return emitOpError("not a reference to a character"); 758 if (inTy.getFKind() == outTy.getFKind()) 759 return emitOpError("buffers must have different KIND values"); 760 return mlir::success(); 761 } 762 763 //===----------------------------------------------------------------------===// 764 // CmpcOp 765 //===----------------------------------------------------------------------===// 766 767 void fir::buildCmpCOp(mlir::OpBuilder &builder, mlir::OperationState &result, 768 mlir::arith::CmpFPredicate predicate, mlir::Value lhs, 769 mlir::Value rhs) { 770 result.addOperands({lhs, rhs}); 771 result.types.push_back(builder.getI1Type()); 772 result.addAttribute( 773 fir::CmpcOp::getPredicateAttrName(), 774 builder.getI64IntegerAttr(static_cast<std::int64_t>(predicate))); 775 } 776 777 mlir::arith::CmpFPredicate 778 fir::CmpcOp::getPredicateByName(llvm::StringRef name) { 779 auto pred = mlir::arith::symbolizeCmpFPredicate(name); 780 assert(pred.hasValue() && "invalid predicate name"); 781 return pred.getValue(); 782 } 783 784 void fir::CmpcOp::print(mlir::OpAsmPrinter &p) { printCmpOp(p, *this); } 785 786 mlir::ParseResult fir::CmpcOp::parse(mlir::OpAsmParser &parser, 787 mlir::OperationState &result) { 788 return parseCmpOp<fir::CmpcOp>(parser, result); 789 } 790 791 //===----------------------------------------------------------------------===// 792 // ConstcOp 793 //===----------------------------------------------------------------------===// 794 795 mlir::ParseResult fir::ConstcOp::parse(mlir::OpAsmParser &parser, 796 mlir::OperationState &result) { 797 fir::RealAttr realp; 798 fir::RealAttr imagp; 799 mlir::Type type; 800 if (parser.parseLParen() || 801 parser.parseAttribute(realp, fir::ConstcOp::getRealAttrName(), 802 result.attributes) || 803 parser.parseComma() || 804 parser.parseAttribute(imagp, fir::ConstcOp::getImagAttrName(), 805 result.attributes) || 806 parser.parseRParen() || parser.parseColonType(type) || 807 parser.addTypesToList(type, result.types)) 808 return mlir::failure(); 809 return mlir::success(); 810 } 811 812 void fir::ConstcOp::print(mlir::OpAsmPrinter &p) { 813 p << '('; 814 p << getOperation()->getAttr(fir::ConstcOp::getRealAttrName()) << ", "; 815 p << getOperation()->getAttr(fir::ConstcOp::getImagAttrName()) << ") : "; 816 p.printType(getType()); 817 } 818 819 mlir::LogicalResult fir::ConstcOp::verify() { 820 if (!getType().isa<fir::ComplexType>()) 821 return emitOpError("must be a !fir.complex type"); 822 return mlir::success(); 823 } 824 825 //===----------------------------------------------------------------------===// 826 // ConvertOp 827 //===----------------------------------------------------------------------===// 828 829 void fir::ConvertOp::getCanonicalizationPatterns( 830 mlir::RewritePatternSet &results, mlir::MLIRContext *context) { 831 results.insert<ConvertConvertOptPattern, ConvertAscendingIndexOptPattern, 832 ConvertDescendingIndexOptPattern, RedundantConvertOptPattern, 833 CombineConvertOptPattern, CombineConvertTruncOptPattern, 834 ForwardConstantConvertPattern>(context); 835 } 836 837 mlir::OpFoldResult fir::ConvertOp::fold(llvm::ArrayRef<mlir::Attribute> opnds) { 838 if (getValue().getType() == getType()) 839 return getValue(); 840 if (matchPattern(getValue(), mlir::m_Op<fir::ConvertOp>())) { 841 auto inner = mlir::cast<fir::ConvertOp>(getValue().getDefiningOp()); 842 // (convert (convert 'a : logical -> i1) : i1 -> logical) ==> forward 'a 843 if (auto toTy = getType().dyn_cast<fir::LogicalType>()) 844 if (auto fromTy = inner.getValue().getType().dyn_cast<fir::LogicalType>()) 845 if (inner.getType().isa<mlir::IntegerType>() && (toTy == fromTy)) 846 return inner.getValue(); 847 // (convert (convert 'a : i1 -> logical) : logical -> i1) ==> forward 'a 848 if (auto toTy = getType().dyn_cast<mlir::IntegerType>()) 849 if (auto fromTy = 850 inner.getValue().getType().dyn_cast<mlir::IntegerType>()) 851 if (inner.getType().isa<fir::LogicalType>() && (toTy == fromTy) && 852 (fromTy.getWidth() == 1)) 853 return inner.getValue(); 854 } 855 return {}; 856 } 857 858 bool fir::ConvertOp::isIntegerCompatible(mlir::Type ty) { 859 return ty.isa<mlir::IntegerType, mlir::IndexType, fir::IntegerType, 860 fir::LogicalType>(); 861 } 862 863 bool fir::ConvertOp::isFloatCompatible(mlir::Type ty) { 864 return ty.isa<mlir::FloatType, fir::RealType>(); 865 } 866 867 bool fir::ConvertOp::isPointerCompatible(mlir::Type ty) { 868 return ty.isa<fir::ReferenceType, fir::PointerType, fir::HeapType, 869 fir::LLVMPointerType, mlir::MemRefType, mlir::FunctionType, 870 fir::TypeDescType>(); 871 } 872 873 mlir::LogicalResult fir::ConvertOp::verify() { 874 auto inType = getValue().getType(); 875 auto outType = getType(); 876 if (inType == outType) 877 return mlir::success(); 878 if ((isPointerCompatible(inType) && isPointerCompatible(outType)) || 879 (isIntegerCompatible(inType) && isIntegerCompatible(outType)) || 880 (isIntegerCompatible(inType) && isFloatCompatible(outType)) || 881 (isFloatCompatible(inType) && isIntegerCompatible(outType)) || 882 (isFloatCompatible(inType) && isFloatCompatible(outType)) || 883 (isIntegerCompatible(inType) && isPointerCompatible(outType)) || 884 (isPointerCompatible(inType) && isIntegerCompatible(outType)) || 885 (inType.isa<fir::BoxType>() && outType.isa<fir::BoxType>()) || 886 (inType.isa<fir::BoxProcType>() && outType.isa<fir::BoxProcType>()) || 887 (fir::isa_complex(inType) && fir::isa_complex(outType))) 888 return mlir::success(); 889 return emitOpError("invalid type conversion"); 890 } 891 892 //===----------------------------------------------------------------------===// 893 // CoordinateOp 894 //===----------------------------------------------------------------------===// 895 896 void fir::CoordinateOp::print(mlir::OpAsmPrinter &p) { 897 p << ' ' << getRef() << ", " << getCoor(); 898 p.printOptionalAttrDict((*this)->getAttrs(), /*elideAttrs=*/{"baseType"}); 899 p << " : "; 900 p.printFunctionalType(getOperandTypes(), (*this)->getResultTypes()); 901 } 902 903 mlir::ParseResult fir::CoordinateOp::parse(mlir::OpAsmParser &parser, 904 mlir::OperationState &result) { 905 mlir::OpAsmParser::UnresolvedOperand memref; 906 if (parser.parseOperand(memref) || parser.parseComma()) 907 return mlir::failure(); 908 llvm::SmallVector<mlir::OpAsmParser::UnresolvedOperand> coorOperands; 909 if (parser.parseOperandList(coorOperands)) 910 return mlir::failure(); 911 llvm::SmallVector<mlir::OpAsmParser::UnresolvedOperand> allOperands; 912 allOperands.push_back(memref); 913 allOperands.append(coorOperands.begin(), coorOperands.end()); 914 mlir::FunctionType funcTy; 915 auto loc = parser.getCurrentLocation(); 916 if (parser.parseOptionalAttrDict(result.attributes) || 917 parser.parseColonType(funcTy) || 918 parser.resolveOperands(allOperands, funcTy.getInputs(), loc, 919 result.operands) || 920 parser.addTypesToList(funcTy.getResults(), result.types)) 921 return mlir::failure(); 922 result.addAttribute("baseType", mlir::TypeAttr::get(funcTy.getInput(0))); 923 return mlir::success(); 924 } 925 926 mlir::LogicalResult fir::CoordinateOp::verify() { 927 const mlir::Type refTy = getRef().getType(); 928 if (fir::isa_ref_type(refTy)) { 929 auto eleTy = fir::dyn_cast_ptrEleTy(refTy); 930 if (auto arrTy = eleTy.dyn_cast<fir::SequenceType>()) { 931 if (arrTy.hasUnknownShape()) 932 return emitOpError("cannot find coordinate in unknown shape"); 933 if (arrTy.getConstantRows() < arrTy.getDimension() - 1) 934 return emitOpError("cannot find coordinate with unknown extents"); 935 } 936 if (!(fir::isa_aggregate(eleTy) || fir::isa_complex(eleTy) || 937 fir::isa_char_string(eleTy))) 938 return emitOpError("cannot apply to this element type"); 939 } 940 auto eleTy = fir::dyn_cast_ptrOrBoxEleTy(refTy); 941 unsigned dimension = 0; 942 const unsigned numCoors = getCoor().size(); 943 for (auto coorOperand : llvm::enumerate(getCoor())) { 944 auto co = coorOperand.value(); 945 if (dimension == 0 && eleTy.isa<fir::SequenceType>()) { 946 dimension = eleTy.cast<fir::SequenceType>().getDimension(); 947 if (dimension == 0) 948 return emitOpError("cannot apply to array of unknown rank"); 949 } 950 if (auto *defOp = co.getDefiningOp()) { 951 if (auto index = mlir::dyn_cast<fir::LenParamIndexOp>(defOp)) { 952 // Recovering a LEN type parameter only makes sense from a boxed 953 // value. For a bare reference, the LEN type parameters must be 954 // passed as additional arguments to `index`. 955 if (refTy.isa<fir::BoxType>()) { 956 if (coorOperand.index() != numCoors - 1) 957 return emitOpError("len_param_index must be last argument"); 958 if (getNumOperands() != 2) 959 return emitOpError("too many operands for len_param_index case"); 960 } 961 if (eleTy != index.getOnType()) 962 emitOpError( 963 "len_param_index type not compatible with reference type"); 964 return mlir::success(); 965 } else if (auto index = mlir::dyn_cast<fir::FieldIndexOp>(defOp)) { 966 if (eleTy != index.getOnType()) 967 emitOpError("field_index type not compatible with reference type"); 968 if (auto recTy = eleTy.dyn_cast<fir::RecordType>()) { 969 eleTy = recTy.getType(index.getFieldName()); 970 continue; 971 } 972 return emitOpError("field_index not applied to !fir.type"); 973 } 974 } 975 if (dimension) { 976 if (--dimension == 0) 977 eleTy = eleTy.cast<fir::SequenceType>().getEleTy(); 978 } else { 979 if (auto t = eleTy.dyn_cast<mlir::TupleType>()) { 980 // FIXME: Generally, we don't know which field of the tuple is being 981 // referred to unless the operand is a constant. Just assume everything 982 // is good in the tuple case for now. 983 return mlir::success(); 984 } else if (auto t = eleTy.dyn_cast<fir::RecordType>()) { 985 // FIXME: This is the same as the tuple case. 986 return mlir::success(); 987 } else if (auto t = eleTy.dyn_cast<fir::ComplexType>()) { 988 eleTy = t.getElementType(); 989 } else if (auto t = eleTy.dyn_cast<mlir::ComplexType>()) { 990 eleTy = t.getElementType(); 991 } else if (auto t = eleTy.dyn_cast<fir::CharacterType>()) { 992 if (t.getLen() == fir::CharacterType::singleton()) 993 return emitOpError("cannot apply to character singleton"); 994 eleTy = fir::CharacterType::getSingleton(t.getContext(), t.getFKind()); 995 if (fir::unwrapRefType(getType()) != eleTy) 996 return emitOpError("character type mismatch"); 997 } else { 998 return emitOpError("invalid parameters (too many)"); 999 } 1000 } 1001 } 1002 return mlir::success(); 1003 } 1004 1005 //===----------------------------------------------------------------------===// 1006 // DispatchOp 1007 //===----------------------------------------------------------------------===// 1008 1009 mlir::FunctionType fir::DispatchOp::getFunctionType() { 1010 return mlir::FunctionType::get(getContext(), getOperandTypes(), 1011 getResultTypes()); 1012 } 1013 1014 mlir::ParseResult fir::DispatchOp::parse(mlir::OpAsmParser &parser, 1015 mlir::OperationState &result) { 1016 mlir::FunctionType calleeType; 1017 llvm::SmallVector<mlir::OpAsmParser::UnresolvedOperand> operands; 1018 auto calleeLoc = parser.getNameLoc(); 1019 llvm::StringRef calleeName; 1020 if (failed(parser.parseOptionalKeyword(&calleeName))) { 1021 mlir::StringAttr calleeAttr; 1022 if (parser.parseAttribute(calleeAttr, 1023 fir::DispatchOp::getMethodAttrNameStr(), 1024 result.attributes)) 1025 return mlir::failure(); 1026 } else { 1027 result.addAttribute(fir::DispatchOp::getMethodAttrNameStr(), 1028 parser.getBuilder().getStringAttr(calleeName)); 1029 } 1030 if (parser.parseOperandList(operands, mlir::OpAsmParser::Delimiter::Paren) || 1031 parser.parseOptionalAttrDict(result.attributes) || 1032 parser.parseColonType(calleeType) || 1033 parser.addTypesToList(calleeType.getResults(), result.types) || 1034 parser.resolveOperands(operands, calleeType.getInputs(), calleeLoc, 1035 result.operands)) 1036 return mlir::failure(); 1037 return mlir::success(); 1038 } 1039 1040 void fir::DispatchOp::print(mlir::OpAsmPrinter &p) { 1041 p << ' ' << getMethodAttr() << '('; 1042 p.printOperand(getObject()); 1043 if (!getArgs().empty()) { 1044 p << ", "; 1045 p.printOperands(getArgs()); 1046 } 1047 p << ") : "; 1048 p.printFunctionalType(getOperation()->getOperandTypes(), 1049 getOperation()->getResultTypes()); 1050 } 1051 1052 //===----------------------------------------------------------------------===// 1053 // DispatchTableOp 1054 //===----------------------------------------------------------------------===// 1055 1056 void fir::DispatchTableOp::appendTableEntry(mlir::Operation *op) { 1057 assert(mlir::isa<fir::DTEntryOp>(*op) && "operation must be a DTEntryOp"); 1058 auto &block = getBlock(); 1059 block.getOperations().insert(block.end(), op); 1060 } 1061 1062 mlir::ParseResult fir::DispatchTableOp::parse(mlir::OpAsmParser &parser, 1063 mlir::OperationState &result) { 1064 // Parse the name as a symbol reference attribute. 1065 mlir::SymbolRefAttr nameAttr; 1066 if (parser.parseAttribute(nameAttr, mlir::SymbolTable::getSymbolAttrName(), 1067 result.attributes)) 1068 return mlir::failure(); 1069 1070 // Convert the parsed name attr into a string attr. 1071 result.attributes.set(mlir::SymbolTable::getSymbolAttrName(), 1072 nameAttr.getRootReference()); 1073 1074 // Parse the optional table body. 1075 mlir::Region *body = result.addRegion(); 1076 mlir::OptionalParseResult parseResult = parser.parseOptionalRegion(*body); 1077 if (parseResult.hasValue() && failed(*parseResult)) 1078 return mlir::failure(); 1079 1080 fir::DispatchTableOp::ensureTerminator(*body, parser.getBuilder(), 1081 result.location); 1082 return mlir::success(); 1083 } 1084 1085 void fir::DispatchTableOp::print(mlir::OpAsmPrinter &p) { 1086 auto tableName = getOperation() 1087 ->getAttrOfType<mlir::StringAttr>( 1088 mlir::SymbolTable::getSymbolAttrName()) 1089 .getValue(); 1090 p << " @" << tableName; 1091 1092 mlir::Region &body = getOperation()->getRegion(0); 1093 if (!body.empty()) { 1094 p << ' '; 1095 p.printRegion(body, /*printEntryBlockArgs=*/false, 1096 /*printBlockTerminators=*/false); 1097 } 1098 } 1099 1100 mlir::LogicalResult fir::DispatchTableOp::verify() { 1101 for (auto &op : getBlock()) 1102 if (!mlir::isa<fir::DTEntryOp, fir::FirEndOp>(op)) 1103 return op.emitOpError("dispatch table must contain dt_entry"); 1104 return mlir::success(); 1105 } 1106 1107 //===----------------------------------------------------------------------===// 1108 // EmboxOp 1109 //===----------------------------------------------------------------------===// 1110 1111 mlir::LogicalResult fir::EmboxOp::verify() { 1112 auto eleTy = fir::dyn_cast_ptrEleTy(getMemref().getType()); 1113 bool isArray = false; 1114 if (auto seqTy = eleTy.dyn_cast<fir::SequenceType>()) { 1115 eleTy = seqTy.getEleTy(); 1116 isArray = true; 1117 } 1118 if (hasLenParams()) { 1119 auto lenPs = numLenParams(); 1120 if (auto rt = eleTy.dyn_cast<fir::RecordType>()) { 1121 if (lenPs != rt.getNumLenParams()) 1122 return emitOpError("number of LEN params does not correspond" 1123 " to the !fir.type type"); 1124 } else if (auto strTy = eleTy.dyn_cast<fir::CharacterType>()) { 1125 if (strTy.getLen() != fir::CharacterType::unknownLen()) 1126 return emitOpError("CHARACTER already has static LEN"); 1127 } else { 1128 return emitOpError("LEN parameters require CHARACTER or derived type"); 1129 } 1130 for (auto lp : getTypeparams()) 1131 if (!fir::isa_integer(lp.getType())) 1132 return emitOpError("LEN parameters must be integral type"); 1133 } 1134 if (getShape() && !isArray) 1135 return emitOpError("shape must not be provided for a scalar"); 1136 if (getSlice() && !isArray) 1137 return emitOpError("slice must not be provided for a scalar"); 1138 return mlir::success(); 1139 } 1140 1141 //===----------------------------------------------------------------------===// 1142 // EmboxCharOp 1143 //===----------------------------------------------------------------------===// 1144 1145 mlir::LogicalResult fir::EmboxCharOp::verify() { 1146 auto eleTy = fir::dyn_cast_ptrEleTy(getMemref().getType()); 1147 if (!eleTy.dyn_cast_or_null<fir::CharacterType>()) 1148 return mlir::failure(); 1149 return mlir::success(); 1150 } 1151 1152 //===----------------------------------------------------------------------===// 1153 // EmboxProcOp 1154 //===----------------------------------------------------------------------===// 1155 1156 mlir::LogicalResult fir::EmboxProcOp::verify() { 1157 // host bindings (optional) must be a reference to a tuple 1158 if (auto h = getHost()) { 1159 if (auto r = h.getType().dyn_cast<fir::ReferenceType>()) 1160 if (r.getEleTy().isa<mlir::TupleType>()) 1161 return mlir::success(); 1162 return mlir::failure(); 1163 } 1164 return mlir::success(); 1165 } 1166 1167 //===----------------------------------------------------------------------===// 1168 // GenTypeDescOp 1169 //===----------------------------------------------------------------------===// 1170 1171 void fir::GenTypeDescOp::build(mlir::OpBuilder &, mlir::OperationState &result, 1172 mlir::TypeAttr inty) { 1173 result.addAttribute("in_type", inty); 1174 result.addTypes(TypeDescType::get(inty.getValue())); 1175 } 1176 1177 mlir::ParseResult fir::GenTypeDescOp::parse(mlir::OpAsmParser &parser, 1178 mlir::OperationState &result) { 1179 mlir::Type intype; 1180 if (parser.parseType(intype)) 1181 return mlir::failure(); 1182 result.addAttribute("in_type", mlir::TypeAttr::get(intype)); 1183 mlir::Type restype = fir::TypeDescType::get(intype); 1184 if (parser.addTypeToList(restype, result.types)) 1185 return mlir::failure(); 1186 return mlir::success(); 1187 } 1188 1189 void fir::GenTypeDescOp::print(mlir::OpAsmPrinter &p) { 1190 p << ' ' << getOperation()->getAttr("in_type"); 1191 p.printOptionalAttrDict(getOperation()->getAttrs(), {"in_type"}); 1192 } 1193 1194 mlir::LogicalResult fir::GenTypeDescOp::verify() { 1195 mlir::Type resultTy = getType(); 1196 if (auto tdesc = resultTy.dyn_cast<fir::TypeDescType>()) { 1197 if (tdesc.getOfTy() != getInType()) 1198 return emitOpError("wrapped type mismatched"); 1199 return mlir::success(); 1200 } 1201 return emitOpError("must be !fir.tdesc type"); 1202 } 1203 1204 //===----------------------------------------------------------------------===// 1205 // GlobalOp 1206 //===----------------------------------------------------------------------===// 1207 1208 mlir::Type fir::GlobalOp::resultType() { 1209 return wrapAllocaResultType(getType()); 1210 } 1211 1212 mlir::ParseResult fir::GlobalOp::parse(mlir::OpAsmParser &parser, 1213 mlir::OperationState &result) { 1214 // Parse the optional linkage 1215 llvm::StringRef linkage; 1216 auto &builder = parser.getBuilder(); 1217 if (mlir::succeeded(parser.parseOptionalKeyword(&linkage))) { 1218 if (fir::GlobalOp::verifyValidLinkage(linkage)) 1219 return mlir::failure(); 1220 mlir::StringAttr linkAttr = builder.getStringAttr(linkage); 1221 result.addAttribute(fir::GlobalOp::getLinkageAttrNameStr(), linkAttr); 1222 } 1223 1224 // Parse the name as a symbol reference attribute. 1225 mlir::SymbolRefAttr nameAttr; 1226 if (parser.parseAttribute(nameAttr, fir::GlobalOp::getSymbolAttrNameStr(), 1227 result.attributes)) 1228 return mlir::failure(); 1229 result.addAttribute(mlir::SymbolTable::getSymbolAttrName(), 1230 nameAttr.getRootReference()); 1231 1232 bool simpleInitializer = false; 1233 if (mlir::succeeded(parser.parseOptionalLParen())) { 1234 mlir::Attribute attr; 1235 if (parser.parseAttribute(attr, "initVal", result.attributes) || 1236 parser.parseRParen()) 1237 return mlir::failure(); 1238 simpleInitializer = true; 1239 } 1240 1241 if (succeeded(parser.parseOptionalKeyword("constant"))) { 1242 // if "constant" keyword then mark this as a constant, not a variable 1243 result.addAttribute("constant", builder.getUnitAttr()); 1244 } 1245 1246 mlir::Type globalType; 1247 if (parser.parseColonType(globalType)) 1248 return mlir::failure(); 1249 1250 result.addAttribute(fir::GlobalOp::getTypeAttrName(result.name), 1251 mlir::TypeAttr::get(globalType)); 1252 1253 if (simpleInitializer) { 1254 result.addRegion(); 1255 } else { 1256 // Parse the optional initializer body. 1257 auto parseResult = 1258 parser.parseOptionalRegion(*result.addRegion(), /*arguments=*/{}); 1259 if (parseResult.hasValue() && mlir::failed(*parseResult)) 1260 return mlir::failure(); 1261 } 1262 return mlir::success(); 1263 } 1264 1265 void fir::GlobalOp::print(mlir::OpAsmPrinter &p) { 1266 if (getLinkName().hasValue()) 1267 p << ' ' << getLinkName().getValue(); 1268 p << ' '; 1269 p.printAttributeWithoutType(getSymrefAttr()); 1270 if (auto val = getValueOrNull()) 1271 p << '(' << val << ')'; 1272 if (getOperation()->getAttr(fir::GlobalOp::getConstantAttrNameStr())) 1273 p << " constant"; 1274 p << " : "; 1275 p.printType(getType()); 1276 if (hasInitializationBody()) { 1277 p << ' '; 1278 p.printRegion(getOperation()->getRegion(0), 1279 /*printEntryBlockArgs=*/false, 1280 /*printBlockTerminators=*/true); 1281 } 1282 } 1283 1284 void fir::GlobalOp::appendInitialValue(mlir::Operation *op) { 1285 getBlock().getOperations().push_back(op); 1286 } 1287 1288 void fir::GlobalOp::build(mlir::OpBuilder &builder, 1289 mlir::OperationState &result, llvm::StringRef name, 1290 bool isConstant, mlir::Type type, 1291 mlir::Attribute initialVal, mlir::StringAttr linkage, 1292 llvm::ArrayRef<mlir::NamedAttribute> attrs) { 1293 result.addRegion(); 1294 result.addAttribute(getTypeAttrName(result.name), mlir::TypeAttr::get(type)); 1295 result.addAttribute(mlir::SymbolTable::getSymbolAttrName(), 1296 builder.getStringAttr(name)); 1297 result.addAttribute(getSymbolAttrNameStr(), 1298 mlir::SymbolRefAttr::get(builder.getContext(), name)); 1299 if (isConstant) 1300 result.addAttribute(getConstantAttrName(result.name), 1301 builder.getUnitAttr()); 1302 if (initialVal) 1303 result.addAttribute(getInitValAttrName(result.name), initialVal); 1304 if (linkage) 1305 result.addAttribute(getLinkageAttrNameStr(), linkage); 1306 result.attributes.append(attrs.begin(), attrs.end()); 1307 } 1308 1309 void fir::GlobalOp::build(mlir::OpBuilder &builder, 1310 mlir::OperationState &result, llvm::StringRef name, 1311 mlir::Type type, mlir::Attribute initialVal, 1312 mlir::StringAttr linkage, 1313 llvm::ArrayRef<mlir::NamedAttribute> attrs) { 1314 build(builder, result, name, /*isConstant=*/false, type, {}, linkage, attrs); 1315 } 1316 1317 void fir::GlobalOp::build(mlir::OpBuilder &builder, 1318 mlir::OperationState &result, llvm::StringRef name, 1319 bool isConstant, mlir::Type type, 1320 mlir::StringAttr linkage, 1321 llvm::ArrayRef<mlir::NamedAttribute> attrs) { 1322 build(builder, result, name, isConstant, type, {}, linkage, attrs); 1323 } 1324 1325 void fir::GlobalOp::build(mlir::OpBuilder &builder, 1326 mlir::OperationState &result, llvm::StringRef name, 1327 mlir::Type type, mlir::StringAttr linkage, 1328 llvm::ArrayRef<mlir::NamedAttribute> attrs) { 1329 build(builder, result, name, /*isConstant=*/false, type, {}, linkage, attrs); 1330 } 1331 1332 void fir::GlobalOp::build(mlir::OpBuilder &builder, 1333 mlir::OperationState &result, llvm::StringRef name, 1334 bool isConstant, mlir::Type type, 1335 llvm::ArrayRef<mlir::NamedAttribute> attrs) { 1336 build(builder, result, name, isConstant, type, mlir::StringAttr{}, attrs); 1337 } 1338 1339 void fir::GlobalOp::build(mlir::OpBuilder &builder, 1340 mlir::OperationState &result, llvm::StringRef name, 1341 mlir::Type type, 1342 llvm::ArrayRef<mlir::NamedAttribute> attrs) { 1343 build(builder, result, name, /*isConstant=*/false, type, attrs); 1344 } 1345 1346 mlir::ParseResult fir::GlobalOp::verifyValidLinkage(llvm::StringRef linkage) { 1347 // Supporting only a subset of the LLVM linkage types for now 1348 static const char *validNames[] = {"common", "internal", "linkonce", 1349 "linkonce_odr", "weak"}; 1350 return mlir::success(llvm::is_contained(validNames, linkage)); 1351 } 1352 1353 //===----------------------------------------------------------------------===// 1354 // GlobalLenOp 1355 //===----------------------------------------------------------------------===// 1356 1357 mlir::ParseResult fir::GlobalLenOp::parse(mlir::OpAsmParser &parser, 1358 mlir::OperationState &result) { 1359 llvm::StringRef fieldName; 1360 if (failed(parser.parseOptionalKeyword(&fieldName))) { 1361 mlir::StringAttr fieldAttr; 1362 if (parser.parseAttribute(fieldAttr, 1363 fir::GlobalLenOp::getLenParamAttrName(), 1364 result.attributes)) 1365 return mlir::failure(); 1366 } else { 1367 result.addAttribute(fir::GlobalLenOp::getLenParamAttrName(), 1368 parser.getBuilder().getStringAttr(fieldName)); 1369 } 1370 mlir::IntegerAttr constant; 1371 if (parser.parseComma() || 1372 parser.parseAttribute(constant, fir::GlobalLenOp::getIntAttrName(), 1373 result.attributes)) 1374 return mlir::failure(); 1375 return mlir::success(); 1376 } 1377 1378 void fir::GlobalLenOp::print(mlir::OpAsmPrinter &p) { 1379 p << ' ' << getOperation()->getAttr(fir::GlobalLenOp::getLenParamAttrName()) 1380 << ", " << getOperation()->getAttr(fir::GlobalLenOp::getIntAttrName()); 1381 } 1382 1383 //===----------------------------------------------------------------------===// 1384 // FieldIndexOp 1385 //===----------------------------------------------------------------------===// 1386 1387 template <typename TY> 1388 mlir::ParseResult parseFieldLikeOp(mlir::OpAsmParser &parser, 1389 mlir::OperationState &result) { 1390 llvm::StringRef fieldName; 1391 auto &builder = parser.getBuilder(); 1392 mlir::Type recty; 1393 if (parser.parseOptionalKeyword(&fieldName) || parser.parseComma() || 1394 parser.parseType(recty)) 1395 return mlir::failure(); 1396 result.addAttribute(fir::FieldIndexOp::getFieldAttrName(), 1397 builder.getStringAttr(fieldName)); 1398 if (!recty.dyn_cast<fir::RecordType>()) 1399 return mlir::failure(); 1400 result.addAttribute(fir::FieldIndexOp::getTypeAttrName(), 1401 mlir::TypeAttr::get(recty)); 1402 if (!parser.parseOptionalLParen()) { 1403 llvm::SmallVector<mlir::OpAsmParser::UnresolvedOperand> operands; 1404 llvm::SmallVector<mlir::Type> types; 1405 auto loc = parser.getNameLoc(); 1406 if (parser.parseOperandList(operands, mlir::OpAsmParser::Delimiter::None) || 1407 parser.parseColonTypeList(types) || parser.parseRParen() || 1408 parser.resolveOperands(operands, types, loc, result.operands)) 1409 return mlir::failure(); 1410 } 1411 mlir::Type fieldType = TY::get(builder.getContext()); 1412 if (parser.addTypeToList(fieldType, result.types)) 1413 return mlir::failure(); 1414 return mlir::success(); 1415 } 1416 1417 mlir::ParseResult fir::FieldIndexOp::parse(mlir::OpAsmParser &parser, 1418 mlir::OperationState &result) { 1419 return parseFieldLikeOp<fir::FieldType>(parser, result); 1420 } 1421 1422 template <typename OP> 1423 void printFieldLikeOp(mlir::OpAsmPrinter &p, OP &op) { 1424 p << ' ' 1425 << op.getOperation() 1426 ->template getAttrOfType<mlir::StringAttr>( 1427 fir::FieldIndexOp::getFieldAttrName()) 1428 .getValue() 1429 << ", " << op.getOperation()->getAttr(fir::FieldIndexOp::getTypeAttrName()); 1430 if (op.getNumOperands()) { 1431 p << '('; 1432 p.printOperands(op.getTypeparams()); 1433 auto sep = ") : "; 1434 for (auto op : op.getTypeparams()) { 1435 p << sep; 1436 if (op) 1437 p.printType(op.getType()); 1438 else 1439 p << "()"; 1440 sep = ", "; 1441 } 1442 } 1443 } 1444 1445 void fir::FieldIndexOp::print(mlir::OpAsmPrinter &p) { 1446 printFieldLikeOp(p, *this); 1447 } 1448 1449 void fir::FieldIndexOp::build(mlir::OpBuilder &builder, 1450 mlir::OperationState &result, 1451 llvm::StringRef fieldName, mlir::Type recTy, 1452 mlir::ValueRange operands) { 1453 result.addAttribute(getFieldAttrName(), builder.getStringAttr(fieldName)); 1454 result.addAttribute(getTypeAttrName(), mlir::TypeAttr::get(recTy)); 1455 result.addOperands(operands); 1456 } 1457 1458 llvm::SmallVector<mlir::Attribute> fir::FieldIndexOp::getAttributes() { 1459 llvm::SmallVector<mlir::Attribute> attrs; 1460 attrs.push_back(getFieldIdAttr()); 1461 attrs.push_back(getOnTypeAttr()); 1462 return attrs; 1463 } 1464 1465 //===----------------------------------------------------------------------===// 1466 // InsertOnRangeOp 1467 //===----------------------------------------------------------------------===// 1468 1469 static mlir::ParseResult 1470 parseCustomRangeSubscript(mlir::OpAsmParser &parser, 1471 mlir::DenseIntElementsAttr &coord) { 1472 llvm::SmallVector<std::int64_t> lbounds; 1473 llvm::SmallVector<std::int64_t> ubounds; 1474 if (parser.parseKeyword("from") || 1475 parser.parseCommaSeparatedList( 1476 mlir::AsmParser::Delimiter::Paren, 1477 [&] { return parser.parseInteger(lbounds.emplace_back(0)); }) || 1478 parser.parseKeyword("to") || 1479 parser.parseCommaSeparatedList(mlir::AsmParser::Delimiter::Paren, [&] { 1480 return parser.parseInteger(ubounds.emplace_back(0)); 1481 })) 1482 return mlir::failure(); 1483 llvm::SmallVector<std::int64_t> zippedBounds; 1484 for (auto zip : llvm::zip(lbounds, ubounds)) { 1485 zippedBounds.push_back(std::get<0>(zip)); 1486 zippedBounds.push_back(std::get<1>(zip)); 1487 } 1488 coord = mlir::Builder(parser.getContext()).getIndexTensorAttr(zippedBounds); 1489 return mlir::success(); 1490 } 1491 1492 static void printCustomRangeSubscript(mlir::OpAsmPrinter &printer, 1493 fir::InsertOnRangeOp op, 1494 mlir::DenseIntElementsAttr coord) { 1495 printer << "from ("; 1496 auto enumerate = llvm::enumerate(coord.getValues<std::int64_t>()); 1497 // Even entries are the lower bounds. 1498 llvm::interleaveComma( 1499 make_filter_range( 1500 enumerate, 1501 [](auto indexed_value) { return indexed_value.index() % 2 == 0; }), 1502 printer, [&](auto indexed_value) { printer << indexed_value.value(); }); 1503 printer << ") to ("; 1504 // Odd entries are the upper bounds. 1505 llvm::interleaveComma( 1506 make_filter_range( 1507 enumerate, 1508 [](auto indexed_value) { return indexed_value.index() % 2 != 0; }), 1509 printer, [&](auto indexed_value) { printer << indexed_value.value(); }); 1510 printer << ")"; 1511 } 1512 1513 /// Range bounds must be nonnegative, and the range must not be empty. 1514 mlir::LogicalResult fir::InsertOnRangeOp::verify() { 1515 if (fir::hasDynamicSize(getSeq().getType())) 1516 return emitOpError("must have constant shape and size"); 1517 mlir::DenseIntElementsAttr coorAttr = getCoor(); 1518 if (coorAttr.size() < 2 || coorAttr.size() % 2 != 0) 1519 return emitOpError("has uneven number of values in ranges"); 1520 bool rangeIsKnownToBeNonempty = false; 1521 for (auto i = coorAttr.getValues<std::int64_t>().end(), 1522 b = coorAttr.getValues<std::int64_t>().begin(); 1523 i != b;) { 1524 int64_t ub = (*--i); 1525 int64_t lb = (*--i); 1526 if (lb < 0 || ub < 0) 1527 return emitOpError("negative range bound"); 1528 if (rangeIsKnownToBeNonempty) 1529 continue; 1530 if (lb > ub) 1531 return emitOpError("empty range"); 1532 rangeIsKnownToBeNonempty = lb < ub; 1533 } 1534 return mlir::success(); 1535 } 1536 1537 //===----------------------------------------------------------------------===// 1538 // InsertValueOp 1539 //===----------------------------------------------------------------------===// 1540 1541 static bool checkIsIntegerConstant(mlir::Attribute attr, std::int64_t conVal) { 1542 if (auto iattr = attr.dyn_cast<mlir::IntegerAttr>()) 1543 return iattr.getInt() == conVal; 1544 return false; 1545 } 1546 1547 static bool isZero(mlir::Attribute a) { return checkIsIntegerConstant(a, 0); } 1548 static bool isOne(mlir::Attribute a) { return checkIsIntegerConstant(a, 1); } 1549 1550 // Undo some complex patterns created in the front-end and turn them back into 1551 // complex ops. 1552 template <typename FltOp, typename CpxOp> 1553 struct UndoComplexPattern : public mlir::RewritePattern { 1554 UndoComplexPattern(mlir::MLIRContext *ctx) 1555 : mlir::RewritePattern("fir.insert_value", 2, ctx) {} 1556 1557 mlir::LogicalResult 1558 matchAndRewrite(mlir::Operation *op, 1559 mlir::PatternRewriter &rewriter) const override { 1560 auto insval = mlir::dyn_cast_or_null<fir::InsertValueOp>(op); 1561 if (!insval || !insval.getType().isa<fir::ComplexType>()) 1562 return mlir::failure(); 1563 auto insval2 = mlir::dyn_cast_or_null<fir::InsertValueOp>( 1564 insval.getAdt().getDefiningOp()); 1565 if (!insval2) 1566 return mlir::failure(); 1567 auto binf = mlir::dyn_cast_or_null<FltOp>(insval.getVal().getDefiningOp()); 1568 auto binf2 = 1569 mlir::dyn_cast_or_null<FltOp>(insval2.getVal().getDefiningOp()); 1570 if (!binf || !binf2 || insval.getCoor().size() != 1 || 1571 !isOne(insval.getCoor()[0]) || insval2.getCoor().size() != 1 || 1572 !isZero(insval2.getCoor()[0])) 1573 return mlir::failure(); 1574 auto eai = mlir::dyn_cast_or_null<fir::ExtractValueOp>( 1575 binf.getLhs().getDefiningOp()); 1576 auto ebi = mlir::dyn_cast_or_null<fir::ExtractValueOp>( 1577 binf.getRhs().getDefiningOp()); 1578 auto ear = mlir::dyn_cast_or_null<fir::ExtractValueOp>( 1579 binf2.getLhs().getDefiningOp()); 1580 auto ebr = mlir::dyn_cast_or_null<fir::ExtractValueOp>( 1581 binf2.getRhs().getDefiningOp()); 1582 if (!eai || !ebi || !ear || !ebr || ear.getAdt() != eai.getAdt() || 1583 ebr.getAdt() != ebi.getAdt() || eai.getCoor().size() != 1 || 1584 !isOne(eai.getCoor()[0]) || ebi.getCoor().size() != 1 || 1585 !isOne(ebi.getCoor()[0]) || ear.getCoor().size() != 1 || 1586 !isZero(ear.getCoor()[0]) || ebr.getCoor().size() != 1 || 1587 !isZero(ebr.getCoor()[0])) 1588 return mlir::failure(); 1589 rewriter.replaceOpWithNewOp<CpxOp>(op, ear.getAdt(), ebr.getAdt()); 1590 return mlir::success(); 1591 } 1592 }; 1593 1594 void fir::InsertValueOp::getCanonicalizationPatterns( 1595 mlir::RewritePatternSet &results, mlir::MLIRContext *context) { 1596 results.insert<UndoComplexPattern<mlir::arith::AddFOp, fir::AddcOp>, 1597 UndoComplexPattern<mlir::arith::SubFOp, fir::SubcOp>>(context); 1598 } 1599 1600 //===----------------------------------------------------------------------===// 1601 // IterWhileOp 1602 //===----------------------------------------------------------------------===// 1603 1604 void fir::IterWhileOp::build(mlir::OpBuilder &builder, 1605 mlir::OperationState &result, mlir::Value lb, 1606 mlir::Value ub, mlir::Value step, 1607 mlir::Value iterate, bool finalCountValue, 1608 mlir::ValueRange iterArgs, 1609 llvm::ArrayRef<mlir::NamedAttribute> attributes) { 1610 result.addOperands({lb, ub, step, iterate}); 1611 if (finalCountValue) { 1612 result.addTypes(builder.getIndexType()); 1613 result.addAttribute(getFinalValueAttrNameStr(), builder.getUnitAttr()); 1614 } 1615 result.addTypes(iterate.getType()); 1616 result.addOperands(iterArgs); 1617 for (auto v : iterArgs) 1618 result.addTypes(v.getType()); 1619 mlir::Region *bodyRegion = result.addRegion(); 1620 bodyRegion->push_back(new mlir::Block{}); 1621 bodyRegion->front().addArgument(builder.getIndexType(), result.location); 1622 bodyRegion->front().addArgument(iterate.getType(), result.location); 1623 bodyRegion->front().addArguments( 1624 iterArgs.getTypes(), 1625 llvm::SmallVector<mlir::Location>(iterArgs.size(), result.location)); 1626 result.addAttributes(attributes); 1627 } 1628 1629 mlir::ParseResult fir::IterWhileOp::parse(mlir::OpAsmParser &parser, 1630 mlir::OperationState &result) { 1631 auto &builder = parser.getBuilder(); 1632 mlir::OpAsmParser::Argument inductionVariable, iterateVar; 1633 mlir::OpAsmParser::UnresolvedOperand lb, ub, step, iterateInput; 1634 if (parser.parseLParen() || parser.parseArgument(inductionVariable) || 1635 parser.parseEqual()) 1636 return mlir::failure(); 1637 1638 // Parse loop bounds. 1639 auto indexType = builder.getIndexType(); 1640 auto i1Type = builder.getIntegerType(1); 1641 if (parser.parseOperand(lb) || 1642 parser.resolveOperand(lb, indexType, result.operands) || 1643 parser.parseKeyword("to") || parser.parseOperand(ub) || 1644 parser.resolveOperand(ub, indexType, result.operands) || 1645 parser.parseKeyword("step") || parser.parseOperand(step) || 1646 parser.parseRParen() || 1647 parser.resolveOperand(step, indexType, result.operands) || 1648 parser.parseKeyword("and") || parser.parseLParen() || 1649 parser.parseArgument(iterateVar) || parser.parseEqual() || 1650 parser.parseOperand(iterateInput) || parser.parseRParen() || 1651 parser.resolveOperand(iterateInput, i1Type, result.operands)) 1652 return mlir::failure(); 1653 1654 // Parse the initial iteration arguments. 1655 auto prependCount = false; 1656 1657 // Induction variable. 1658 llvm::SmallVector<mlir::OpAsmParser::Argument> regionArgs; 1659 regionArgs.push_back(inductionVariable); 1660 regionArgs.push_back(iterateVar); 1661 1662 if (succeeded(parser.parseOptionalKeyword("iter_args"))) { 1663 llvm::SmallVector<mlir::OpAsmParser::UnresolvedOperand> operands; 1664 llvm::SmallVector<mlir::Type> regionTypes; 1665 // Parse assignment list and results type list. 1666 if (parser.parseAssignmentList(regionArgs, operands) || 1667 parser.parseArrowTypeList(regionTypes)) 1668 return mlir::failure(); 1669 if (regionTypes.size() == operands.size() + 2) 1670 prependCount = true; 1671 llvm::ArrayRef<mlir::Type> resTypes = regionTypes; 1672 resTypes = prependCount ? resTypes.drop_front(2) : resTypes; 1673 // Resolve input operands. 1674 for (auto operandType : llvm::zip(operands, resTypes)) 1675 if (parser.resolveOperand(std::get<0>(operandType), 1676 std::get<1>(operandType), result.operands)) 1677 return mlir::failure(); 1678 if (prependCount) { 1679 result.addTypes(regionTypes); 1680 } else { 1681 result.addTypes(i1Type); 1682 result.addTypes(resTypes); 1683 } 1684 } else if (succeeded(parser.parseOptionalArrow())) { 1685 llvm::SmallVector<mlir::Type> typeList; 1686 if (parser.parseLParen() || parser.parseTypeList(typeList) || 1687 parser.parseRParen()) 1688 return mlir::failure(); 1689 // Type list must be "(index, i1)". 1690 if (typeList.size() != 2 || !typeList[0].isa<mlir::IndexType>() || 1691 !typeList[1].isSignlessInteger(1)) 1692 return mlir::failure(); 1693 result.addTypes(typeList); 1694 prependCount = true; 1695 } else { 1696 result.addTypes(i1Type); 1697 } 1698 1699 if (parser.parseOptionalAttrDictWithKeyword(result.attributes)) 1700 return mlir::failure(); 1701 1702 llvm::SmallVector<mlir::Type> argTypes; 1703 // Induction variable (hidden) 1704 if (prependCount) 1705 result.addAttribute(IterWhileOp::getFinalValueAttrNameStr(), 1706 builder.getUnitAttr()); 1707 else 1708 argTypes.push_back(indexType); 1709 // Loop carried variables (including iterate) 1710 argTypes.append(result.types.begin(), result.types.end()); 1711 // Parse the body region. 1712 auto *body = result.addRegion(); 1713 if (regionArgs.size() != argTypes.size()) 1714 return parser.emitError( 1715 parser.getNameLoc(), 1716 "mismatch in number of loop-carried values and defined values"); 1717 1718 for (size_t i = 0, e = regionArgs.size(); i != e; ++i) 1719 regionArgs[i].type = argTypes[i]; 1720 1721 if (parser.parseRegion(*body, regionArgs)) 1722 return mlir::failure(); 1723 1724 fir::IterWhileOp::ensureTerminator(*body, builder, result.location); 1725 return mlir::success(); 1726 } 1727 1728 mlir::LogicalResult fir::IterWhileOp::verify() { 1729 // Check that the body defines as single block argument for the induction 1730 // variable. 1731 auto *body = getBody(); 1732 if (!body->getArgument(1).getType().isInteger(1)) 1733 return emitOpError( 1734 "expected body second argument to be an index argument for " 1735 "the induction variable"); 1736 if (!body->getArgument(0).getType().isIndex()) 1737 return emitOpError( 1738 "expected body first argument to be an index argument for " 1739 "the induction variable"); 1740 1741 auto opNumResults = getNumResults(); 1742 if (getFinalValue()) { 1743 // Result type must be "(index, i1, ...)". 1744 if (!getResult(0).getType().isa<mlir::IndexType>()) 1745 return emitOpError("result #0 expected to be index"); 1746 if (!getResult(1).getType().isSignlessInteger(1)) 1747 return emitOpError("result #1 expected to be i1"); 1748 opNumResults--; 1749 } else { 1750 // iterate_while always returns the early exit induction value. 1751 // Result type must be "(i1, ...)" 1752 if (!getResult(0).getType().isSignlessInteger(1)) 1753 return emitOpError("result #0 expected to be i1"); 1754 } 1755 if (opNumResults == 0) 1756 return mlir::failure(); 1757 if (getNumIterOperands() != opNumResults) 1758 return emitOpError( 1759 "mismatch in number of loop-carried values and defined values"); 1760 if (getNumRegionIterArgs() != opNumResults) 1761 return emitOpError( 1762 "mismatch in number of basic block args and defined values"); 1763 auto iterOperands = getIterOperands(); 1764 auto iterArgs = getRegionIterArgs(); 1765 auto opResults = getFinalValue() ? getResults().drop_front() : getResults(); 1766 unsigned i = 0u; 1767 for (auto e : llvm::zip(iterOperands, iterArgs, opResults)) { 1768 if (std::get<0>(e).getType() != std::get<2>(e).getType()) 1769 return emitOpError() << "types mismatch between " << i 1770 << "th iter operand and defined value"; 1771 if (std::get<1>(e).getType() != std::get<2>(e).getType()) 1772 return emitOpError() << "types mismatch between " << i 1773 << "th iter region arg and defined value"; 1774 1775 i++; 1776 } 1777 return mlir::success(); 1778 } 1779 1780 void fir::IterWhileOp::print(mlir::OpAsmPrinter &p) { 1781 p << " (" << getInductionVar() << " = " << getLowerBound() << " to " 1782 << getUpperBound() << " step " << getStep() << ") and ("; 1783 assert(hasIterOperands()); 1784 auto regionArgs = getRegionIterArgs(); 1785 auto operands = getIterOperands(); 1786 p << regionArgs.front() << " = " << *operands.begin() << ")"; 1787 if (regionArgs.size() > 1) { 1788 p << " iter_args("; 1789 llvm::interleaveComma( 1790 llvm::zip(regionArgs.drop_front(), operands.drop_front()), p, 1791 [&](auto it) { p << std::get<0>(it) << " = " << std::get<1>(it); }); 1792 p << ") -> ("; 1793 llvm::interleaveComma( 1794 llvm::drop_begin(getResultTypes(), getFinalValue() ? 0 : 1), p); 1795 p << ")"; 1796 } else if (getFinalValue()) { 1797 p << " -> (" << getResultTypes() << ')'; 1798 } 1799 p.printOptionalAttrDictWithKeyword((*this)->getAttrs(), 1800 {getFinalValueAttrNameStr()}); 1801 p << ' '; 1802 p.printRegion(getRegion(), /*printEntryBlockArgs=*/false, 1803 /*printBlockTerminators=*/true); 1804 } 1805 1806 mlir::Region &fir::IterWhileOp::getLoopBody() { return getRegion(); } 1807 1808 mlir::BlockArgument fir::IterWhileOp::iterArgToBlockArg(mlir::Value iterArg) { 1809 for (auto i : llvm::enumerate(getInitArgs())) 1810 if (iterArg == i.value()) 1811 return getRegion().front().getArgument(i.index() + 1); 1812 return {}; 1813 } 1814 1815 void fir::IterWhileOp::resultToSourceOps( 1816 llvm::SmallVectorImpl<mlir::Value> &results, unsigned resultNum) { 1817 auto oper = getFinalValue() ? resultNum + 1 : resultNum; 1818 auto *term = getRegion().front().getTerminator(); 1819 if (oper < term->getNumOperands()) 1820 results.push_back(term->getOperand(oper)); 1821 } 1822 1823 mlir::Value fir::IterWhileOp::blockArgToSourceOp(unsigned blockArgNum) { 1824 if (blockArgNum > 0 && blockArgNum <= getInitArgs().size()) 1825 return getInitArgs()[blockArgNum - 1]; 1826 return {}; 1827 } 1828 1829 //===----------------------------------------------------------------------===// 1830 // LenParamIndexOp 1831 //===----------------------------------------------------------------------===// 1832 1833 mlir::ParseResult fir::LenParamIndexOp::parse(mlir::OpAsmParser &parser, 1834 mlir::OperationState &result) { 1835 return parseFieldLikeOp<fir::LenType>(parser, result); 1836 } 1837 1838 void fir::LenParamIndexOp::print(mlir::OpAsmPrinter &p) { 1839 printFieldLikeOp(p, *this); 1840 } 1841 1842 void fir::LenParamIndexOp::build(mlir::OpBuilder &builder, 1843 mlir::OperationState &result, 1844 llvm::StringRef fieldName, mlir::Type recTy, 1845 mlir::ValueRange operands) { 1846 result.addAttribute(getFieldAttrName(), builder.getStringAttr(fieldName)); 1847 result.addAttribute(getTypeAttrName(), mlir::TypeAttr::get(recTy)); 1848 result.addOperands(operands); 1849 } 1850 1851 llvm::SmallVector<mlir::Attribute> fir::LenParamIndexOp::getAttributes() { 1852 llvm::SmallVector<mlir::Attribute> attrs; 1853 attrs.push_back(getFieldIdAttr()); 1854 attrs.push_back(getOnTypeAttr()); 1855 return attrs; 1856 } 1857 1858 //===----------------------------------------------------------------------===// 1859 // LoadOp 1860 //===----------------------------------------------------------------------===// 1861 1862 void fir::LoadOp::build(mlir::OpBuilder &builder, mlir::OperationState &result, 1863 mlir::Value refVal) { 1864 if (!refVal) { 1865 mlir::emitError(result.location, "LoadOp has null argument"); 1866 return; 1867 } 1868 auto eleTy = fir::dyn_cast_ptrEleTy(refVal.getType()); 1869 if (!eleTy) { 1870 mlir::emitError(result.location, "not a memory reference type"); 1871 return; 1872 } 1873 result.addOperands(refVal); 1874 result.addTypes(eleTy); 1875 } 1876 1877 mlir::ParseResult fir::LoadOp::getElementOf(mlir::Type &ele, mlir::Type ref) { 1878 if ((ele = fir::dyn_cast_ptrEleTy(ref))) 1879 return mlir::success(); 1880 return mlir::failure(); 1881 } 1882 1883 mlir::ParseResult fir::LoadOp::parse(mlir::OpAsmParser &parser, 1884 mlir::OperationState &result) { 1885 mlir::Type type; 1886 mlir::OpAsmParser::UnresolvedOperand oper; 1887 if (parser.parseOperand(oper) || 1888 parser.parseOptionalAttrDict(result.attributes) || 1889 parser.parseColonType(type) || 1890 parser.resolveOperand(oper, type, result.operands)) 1891 return mlir::failure(); 1892 mlir::Type eleTy; 1893 if (fir::LoadOp::getElementOf(eleTy, type) || 1894 parser.addTypeToList(eleTy, result.types)) 1895 return mlir::failure(); 1896 return mlir::success(); 1897 } 1898 1899 void fir::LoadOp::print(mlir::OpAsmPrinter &p) { 1900 p << ' '; 1901 p.printOperand(getMemref()); 1902 p.printOptionalAttrDict(getOperation()->getAttrs(), {}); 1903 p << " : " << getMemref().getType(); 1904 } 1905 1906 //===----------------------------------------------------------------------===// 1907 // DoLoopOp 1908 //===----------------------------------------------------------------------===// 1909 1910 void fir::DoLoopOp::build(mlir::OpBuilder &builder, 1911 mlir::OperationState &result, mlir::Value lb, 1912 mlir::Value ub, mlir::Value step, bool unordered, 1913 bool finalCountValue, mlir::ValueRange iterArgs, 1914 llvm::ArrayRef<mlir::NamedAttribute> attributes) { 1915 result.addOperands({lb, ub, step}); 1916 result.addOperands(iterArgs); 1917 if (finalCountValue) { 1918 result.addTypes(builder.getIndexType()); 1919 result.addAttribute(getFinalValueAttrName(result.name), 1920 builder.getUnitAttr()); 1921 } 1922 for (auto v : iterArgs) 1923 result.addTypes(v.getType()); 1924 mlir::Region *bodyRegion = result.addRegion(); 1925 bodyRegion->push_back(new mlir::Block{}); 1926 if (iterArgs.empty() && !finalCountValue) 1927 fir::DoLoopOp::ensureTerminator(*bodyRegion, builder, result.location); 1928 bodyRegion->front().addArgument(builder.getIndexType(), result.location); 1929 bodyRegion->front().addArguments( 1930 iterArgs.getTypes(), 1931 llvm::SmallVector<mlir::Location>(iterArgs.size(), result.location)); 1932 if (unordered) 1933 result.addAttribute(getUnorderedAttrName(result.name), 1934 builder.getUnitAttr()); 1935 result.addAttributes(attributes); 1936 } 1937 1938 mlir::ParseResult fir::DoLoopOp::parse(mlir::OpAsmParser &parser, 1939 mlir::OperationState &result) { 1940 auto &builder = parser.getBuilder(); 1941 mlir::OpAsmParser::Argument inductionVariable; 1942 mlir::OpAsmParser::UnresolvedOperand lb, ub, step; 1943 // Parse the induction variable followed by '='. 1944 if (parser.parseArgument(inductionVariable) || parser.parseEqual()) 1945 return mlir::failure(); 1946 1947 // Parse loop bounds. 1948 auto indexType = builder.getIndexType(); 1949 if (parser.parseOperand(lb) || 1950 parser.resolveOperand(lb, indexType, result.operands) || 1951 parser.parseKeyword("to") || parser.parseOperand(ub) || 1952 parser.resolveOperand(ub, indexType, result.operands) || 1953 parser.parseKeyword("step") || parser.parseOperand(step) || 1954 parser.resolveOperand(step, indexType, result.operands)) 1955 return mlir::failure(); 1956 1957 if (mlir::succeeded(parser.parseOptionalKeyword("unordered"))) 1958 result.addAttribute("unordered", builder.getUnitAttr()); 1959 1960 // Parse the optional initial iteration arguments. 1961 llvm::SmallVector<mlir::OpAsmParser::Argument> regionArgs; 1962 llvm::SmallVector<mlir::OpAsmParser::UnresolvedOperand> operands; 1963 llvm::SmallVector<mlir::Type> argTypes; 1964 bool prependCount = false; 1965 regionArgs.push_back(inductionVariable); 1966 1967 if (succeeded(parser.parseOptionalKeyword("iter_args"))) { 1968 // Parse assignment list and results type list. 1969 if (parser.parseAssignmentList(regionArgs, operands) || 1970 parser.parseArrowTypeList(result.types)) 1971 return mlir::failure(); 1972 if (result.types.size() == operands.size() + 1) 1973 prependCount = true; 1974 // Resolve input operands. 1975 llvm::ArrayRef<mlir::Type> resTypes = result.types; 1976 for (auto operand_type : 1977 llvm::zip(operands, prependCount ? resTypes.drop_front() : resTypes)) 1978 if (parser.resolveOperand(std::get<0>(operand_type), 1979 std::get<1>(operand_type), result.operands)) 1980 return mlir::failure(); 1981 } else if (succeeded(parser.parseOptionalArrow())) { 1982 if (parser.parseKeyword("index")) 1983 return mlir::failure(); 1984 result.types.push_back(indexType); 1985 prependCount = true; 1986 } 1987 1988 if (parser.parseOptionalAttrDictWithKeyword(result.attributes)) 1989 return mlir::failure(); 1990 1991 // Induction variable. 1992 if (prependCount) 1993 result.addAttribute(DoLoopOp::getFinalValueAttrName(result.name), 1994 builder.getUnitAttr()); 1995 else 1996 argTypes.push_back(indexType); 1997 // Loop carried variables 1998 argTypes.append(result.types.begin(), result.types.end()); 1999 // Parse the body region. 2000 auto *body = result.addRegion(); 2001 if (regionArgs.size() != argTypes.size()) 2002 return parser.emitError( 2003 parser.getNameLoc(), 2004 "mismatch in number of loop-carried values and defined values"); 2005 for (size_t i = 0, e = regionArgs.size(); i != e; ++i) 2006 regionArgs[i].type = argTypes[i]; 2007 2008 if (parser.parseRegion(*body, regionArgs)) 2009 return mlir::failure(); 2010 2011 DoLoopOp::ensureTerminator(*body, builder, result.location); 2012 2013 return mlir::success(); 2014 } 2015 2016 fir::DoLoopOp fir::getForInductionVarOwner(mlir::Value val) { 2017 auto ivArg = val.dyn_cast<mlir::BlockArgument>(); 2018 if (!ivArg) 2019 return {}; 2020 assert(ivArg.getOwner() && "unlinked block argument"); 2021 auto *containingInst = ivArg.getOwner()->getParentOp(); 2022 return mlir::dyn_cast_or_null<fir::DoLoopOp>(containingInst); 2023 } 2024 2025 // Lifted from loop.loop 2026 mlir::LogicalResult fir::DoLoopOp::verify() { 2027 // Check that the body defines as single block argument for the induction 2028 // variable. 2029 auto *body = getBody(); 2030 if (!body->getArgument(0).getType().isIndex()) 2031 return emitOpError( 2032 "expected body first argument to be an index argument for " 2033 "the induction variable"); 2034 2035 auto opNumResults = getNumResults(); 2036 if (opNumResults == 0) 2037 return mlir::success(); 2038 2039 if (getFinalValue()) { 2040 if (getUnordered()) 2041 return emitOpError("unordered loop has no final value"); 2042 opNumResults--; 2043 } 2044 if (getNumIterOperands() != opNumResults) 2045 return emitOpError( 2046 "mismatch in number of loop-carried values and defined values"); 2047 if (getNumRegionIterArgs() != opNumResults) 2048 return emitOpError( 2049 "mismatch in number of basic block args and defined values"); 2050 auto iterOperands = getIterOperands(); 2051 auto iterArgs = getRegionIterArgs(); 2052 auto opResults = getFinalValue() ? getResults().drop_front() : getResults(); 2053 unsigned i = 0u; 2054 for (auto e : llvm::zip(iterOperands, iterArgs, opResults)) { 2055 if (std::get<0>(e).getType() != std::get<2>(e).getType()) 2056 return emitOpError() << "types mismatch between " << i 2057 << "th iter operand and defined value"; 2058 if (std::get<1>(e).getType() != std::get<2>(e).getType()) 2059 return emitOpError() << "types mismatch between " << i 2060 << "th iter region arg and defined value"; 2061 2062 i++; 2063 } 2064 return mlir::success(); 2065 } 2066 2067 void fir::DoLoopOp::print(mlir::OpAsmPrinter &p) { 2068 bool printBlockTerminators = false; 2069 p << ' ' << getInductionVar() << " = " << getLowerBound() << " to " 2070 << getUpperBound() << " step " << getStep(); 2071 if (getUnordered()) 2072 p << " unordered"; 2073 if (hasIterOperands()) { 2074 p << " iter_args("; 2075 auto regionArgs = getRegionIterArgs(); 2076 auto operands = getIterOperands(); 2077 llvm::interleaveComma(llvm::zip(regionArgs, operands), p, [&](auto it) { 2078 p << std::get<0>(it) << " = " << std::get<1>(it); 2079 }); 2080 p << ") -> (" << getResultTypes() << ')'; 2081 printBlockTerminators = true; 2082 } else if (getFinalValue()) { 2083 p << " -> " << getResultTypes(); 2084 printBlockTerminators = true; 2085 } 2086 p.printOptionalAttrDictWithKeyword((*this)->getAttrs(), 2087 {"unordered", "finalValue"}); 2088 p << ' '; 2089 p.printRegion(getRegion(), /*printEntryBlockArgs=*/false, 2090 printBlockTerminators); 2091 } 2092 2093 mlir::Region &fir::DoLoopOp::getLoopBody() { return getRegion(); } 2094 2095 /// Translate a value passed as an iter_arg to the corresponding block 2096 /// argument in the body of the loop. 2097 mlir::BlockArgument fir::DoLoopOp::iterArgToBlockArg(mlir::Value iterArg) { 2098 for (auto i : llvm::enumerate(getInitArgs())) 2099 if (iterArg == i.value()) 2100 return getRegion().front().getArgument(i.index() + 1); 2101 return {}; 2102 } 2103 2104 /// Translate the result vector (by index number) to the corresponding value 2105 /// to the `fir.result` Op. 2106 void fir::DoLoopOp::resultToSourceOps( 2107 llvm::SmallVectorImpl<mlir::Value> &results, unsigned resultNum) { 2108 auto oper = getFinalValue() ? resultNum + 1 : resultNum; 2109 auto *term = getRegion().front().getTerminator(); 2110 if (oper < term->getNumOperands()) 2111 results.push_back(term->getOperand(oper)); 2112 } 2113 2114 /// Translate the block argument (by index number) to the corresponding value 2115 /// passed as an iter_arg to the parent DoLoopOp. 2116 mlir::Value fir::DoLoopOp::blockArgToSourceOp(unsigned blockArgNum) { 2117 if (blockArgNum > 0 && blockArgNum <= getInitArgs().size()) 2118 return getInitArgs()[blockArgNum - 1]; 2119 return {}; 2120 } 2121 2122 //===----------------------------------------------------------------------===// 2123 // DTEntryOp 2124 //===----------------------------------------------------------------------===// 2125 2126 mlir::ParseResult fir::DTEntryOp::parse(mlir::OpAsmParser &parser, 2127 mlir::OperationState &result) { 2128 llvm::StringRef methodName; 2129 // allow `methodName` or `"methodName"` 2130 if (failed(parser.parseOptionalKeyword(&methodName))) { 2131 mlir::StringAttr methodAttr; 2132 if (parser.parseAttribute(methodAttr, 2133 fir::DTEntryOp::getMethodAttrNameStr(), 2134 result.attributes)) 2135 return mlir::failure(); 2136 } else { 2137 result.addAttribute(fir::DTEntryOp::getMethodAttrNameStr(), 2138 parser.getBuilder().getStringAttr(methodName)); 2139 } 2140 mlir::SymbolRefAttr calleeAttr; 2141 if (parser.parseComma() || 2142 parser.parseAttribute(calleeAttr, fir::DTEntryOp::getProcAttrNameStr(), 2143 result.attributes)) 2144 return mlir::failure(); 2145 return mlir::success(); 2146 } 2147 2148 void fir::DTEntryOp::print(mlir::OpAsmPrinter &p) { 2149 p << ' ' << getMethodAttr() << ", " << getProcAttr(); 2150 } 2151 2152 //===----------------------------------------------------------------------===// 2153 // ReboxOp 2154 //===----------------------------------------------------------------------===// 2155 2156 /// Get the scalar type related to a fir.box type. 2157 /// Example: return f32 for !fir.box<!fir.heap<!fir.array<?x?xf32>>. 2158 static mlir::Type getBoxScalarEleTy(mlir::Type boxTy) { 2159 auto eleTy = fir::dyn_cast_ptrOrBoxEleTy(boxTy); 2160 if (auto seqTy = eleTy.dyn_cast<fir::SequenceType>()) 2161 return seqTy.getEleTy(); 2162 return eleTy; 2163 } 2164 2165 /// Get the rank from a !fir.box type 2166 static unsigned getBoxRank(mlir::Type boxTy) { 2167 auto eleTy = fir::dyn_cast_ptrOrBoxEleTy(boxTy); 2168 if (auto seqTy = eleTy.dyn_cast<fir::SequenceType>()) 2169 return seqTy.getDimension(); 2170 return 0; 2171 } 2172 2173 /// Test if \p t1 and \p t2 are compatible character types (if they can 2174 /// represent the same type at runtime). 2175 static bool areCompatibleCharacterTypes(mlir::Type t1, mlir::Type t2) { 2176 auto c1 = t1.dyn_cast<fir::CharacterType>(); 2177 auto c2 = t2.dyn_cast<fir::CharacterType>(); 2178 if (!c1 || !c2) 2179 return false; 2180 if (c1.hasDynamicLen() || c2.hasDynamicLen()) 2181 return true; 2182 return c1.getLen() == c2.getLen(); 2183 } 2184 2185 mlir::LogicalResult fir::ReboxOp::verify() { 2186 auto inputBoxTy = getBox().getType(); 2187 if (fir::isa_unknown_size_box(inputBoxTy)) 2188 return emitOpError("box operand must not have unknown rank or type"); 2189 auto outBoxTy = getType(); 2190 if (fir::isa_unknown_size_box(outBoxTy)) 2191 return emitOpError("result type must not have unknown rank or type"); 2192 auto inputRank = getBoxRank(inputBoxTy); 2193 auto inputEleTy = getBoxScalarEleTy(inputBoxTy); 2194 auto outRank = getBoxRank(outBoxTy); 2195 auto outEleTy = getBoxScalarEleTy(outBoxTy); 2196 2197 if (auto sliceVal = getSlice()) { 2198 // Slicing case 2199 if (sliceVal.getType().cast<fir::SliceType>().getRank() != inputRank) 2200 return emitOpError("slice operand rank must match box operand rank"); 2201 if (auto shapeVal = getShape()) { 2202 if (auto shiftTy = shapeVal.getType().dyn_cast<fir::ShiftType>()) { 2203 if (shiftTy.getRank() != inputRank) 2204 return emitOpError("shape operand and input box ranks must match " 2205 "when there is a slice"); 2206 } else { 2207 return emitOpError("shape operand must absent or be a fir.shift " 2208 "when there is a slice"); 2209 } 2210 } 2211 if (auto sliceOp = sliceVal.getDefiningOp()) { 2212 auto slicedRank = mlir::cast<fir::SliceOp>(sliceOp).getOutRank(); 2213 if (slicedRank != outRank) 2214 return emitOpError("result type rank and rank after applying slice " 2215 "operand must match"); 2216 } 2217 } else { 2218 // Reshaping case 2219 unsigned shapeRank = inputRank; 2220 if (auto shapeVal = getShape()) { 2221 auto ty = shapeVal.getType(); 2222 if (auto shapeTy = ty.dyn_cast<fir::ShapeType>()) { 2223 shapeRank = shapeTy.getRank(); 2224 } else if (auto shapeShiftTy = ty.dyn_cast<fir::ShapeShiftType>()) { 2225 shapeRank = shapeShiftTy.getRank(); 2226 } else { 2227 auto shiftTy = ty.cast<fir::ShiftType>(); 2228 shapeRank = shiftTy.getRank(); 2229 if (shapeRank != inputRank) 2230 return emitOpError("shape operand and input box ranks must match " 2231 "when the shape is a fir.shift"); 2232 } 2233 } 2234 if (shapeRank != outRank) 2235 return emitOpError("result type and shape operand ranks must match"); 2236 } 2237 2238 if (inputEleTy != outEleTy) { 2239 // TODO: check that outBoxTy is a parent type of inputBoxTy for derived 2240 // types. 2241 // Character input and output types with constant length may be different if 2242 // there is a substring in the slice, otherwise, they must match. If any of 2243 // the types is a character with dynamic length, the other type can be any 2244 // character type. 2245 const bool typeCanMismatch = 2246 inputEleTy.isa<fir::RecordType>() || 2247 (getSlice() && inputEleTy.isa<fir::CharacterType>()) || 2248 areCompatibleCharacterTypes(inputEleTy, outEleTy); 2249 if (!typeCanMismatch) 2250 return emitOpError( 2251 "op input and output element types must match for intrinsic types"); 2252 } 2253 return mlir::success(); 2254 } 2255 2256 //===----------------------------------------------------------------------===// 2257 // ResultOp 2258 //===----------------------------------------------------------------------===// 2259 2260 mlir::LogicalResult fir::ResultOp::verify() { 2261 auto *parentOp = (*this)->getParentOp(); 2262 auto results = parentOp->getResults(); 2263 auto operands = (*this)->getOperands(); 2264 2265 if (parentOp->getNumResults() != getNumOperands()) 2266 return emitOpError() << "parent of result must have same arity"; 2267 for (auto e : llvm::zip(results, operands)) 2268 if (std::get<0>(e).getType() != std::get<1>(e).getType()) 2269 return emitOpError() << "types mismatch between result op and its parent"; 2270 return mlir::success(); 2271 } 2272 2273 //===----------------------------------------------------------------------===// 2274 // SaveResultOp 2275 //===----------------------------------------------------------------------===// 2276 2277 mlir::LogicalResult fir::SaveResultOp::verify() { 2278 auto resultType = getValue().getType(); 2279 if (resultType != fir::dyn_cast_ptrEleTy(getMemref().getType())) 2280 return emitOpError("value type must match memory reference type"); 2281 if (fir::isa_unknown_size_box(resultType)) 2282 return emitOpError("cannot save !fir.box of unknown rank or type"); 2283 2284 if (resultType.isa<fir::BoxType>()) { 2285 if (getShape() || !getTypeparams().empty()) 2286 return emitOpError( 2287 "must not have shape or length operands if the value is a fir.box"); 2288 return mlir::success(); 2289 } 2290 2291 // fir.record or fir.array case. 2292 unsigned shapeTyRank = 0; 2293 if (auto shapeVal = getShape()) { 2294 auto shapeTy = shapeVal.getType(); 2295 if (auto s = shapeTy.dyn_cast<fir::ShapeType>()) 2296 shapeTyRank = s.getRank(); 2297 else 2298 shapeTyRank = shapeTy.cast<fir::ShapeShiftType>().getRank(); 2299 } 2300 2301 auto eleTy = resultType; 2302 if (auto seqTy = resultType.dyn_cast<fir::SequenceType>()) { 2303 if (seqTy.getDimension() != shapeTyRank) 2304 emitOpError("shape operand must be provided and have the value rank " 2305 "when the value is a fir.array"); 2306 eleTy = seqTy.getEleTy(); 2307 } else { 2308 if (shapeTyRank != 0) 2309 emitOpError( 2310 "shape operand should only be provided if the value is a fir.array"); 2311 } 2312 2313 if (auto recTy = eleTy.dyn_cast<fir::RecordType>()) { 2314 if (recTy.getNumLenParams() != getTypeparams().size()) 2315 emitOpError("length parameters number must match with the value type " 2316 "length parameters"); 2317 } else if (auto charTy = eleTy.dyn_cast<fir::CharacterType>()) { 2318 if (getTypeparams().size() > 1) 2319 emitOpError("no more than one length parameter must be provided for " 2320 "character value"); 2321 } else { 2322 if (!getTypeparams().empty()) 2323 emitOpError("length parameters must not be provided for this value type"); 2324 } 2325 2326 return mlir::success(); 2327 } 2328 2329 //===----------------------------------------------------------------------===// 2330 // IntegralSwitchTerminator 2331 //===----------------------------------------------------------------------===// 2332 static constexpr llvm::StringRef getCompareOffsetAttr() { 2333 return "compare_operand_offsets"; 2334 } 2335 2336 static constexpr llvm::StringRef getTargetOffsetAttr() { 2337 return "target_operand_offsets"; 2338 } 2339 2340 template <typename OpT> 2341 static mlir::LogicalResult verifyIntegralSwitchTerminator(OpT op) { 2342 if (!op.getSelector() 2343 .getType() 2344 .template isa<mlir::IntegerType, mlir::IndexType, 2345 fir::IntegerType>()) 2346 return op.emitOpError("must be an integer"); 2347 auto cases = 2348 op->template getAttrOfType<mlir::ArrayAttr>(op.getCasesAttr()).getValue(); 2349 auto count = op.getNumDest(); 2350 if (count == 0) 2351 return op.emitOpError("must have at least one successor"); 2352 if (op.getNumConditions() != count) 2353 return op.emitOpError("number of cases and targets don't match"); 2354 if (op.targetOffsetSize() != count) 2355 return op.emitOpError("incorrect number of successor operand groups"); 2356 for (decltype(count) i = 0; i != count; ++i) { 2357 if (!cases[i].template isa<mlir::IntegerAttr, mlir::UnitAttr>()) 2358 return op.emitOpError("invalid case alternative"); 2359 } 2360 return mlir::success(); 2361 } 2362 2363 static mlir::ParseResult parseIntegralSwitchTerminator( 2364 mlir::OpAsmParser &parser, mlir::OperationState &result, 2365 llvm::StringRef casesAttr, llvm::StringRef operandSegmentAttr) { 2366 mlir::OpAsmParser::UnresolvedOperand selector; 2367 mlir::Type type; 2368 if (fir::parseSelector(parser, result, selector, type)) 2369 return mlir::failure(); 2370 2371 llvm::SmallVector<mlir::Attribute> ivalues; 2372 llvm::SmallVector<mlir::Block *> dests; 2373 llvm::SmallVector<llvm::SmallVector<mlir::Value>> destArgs; 2374 while (true) { 2375 mlir::Attribute ivalue; // Integer or Unit 2376 mlir::Block *dest; 2377 llvm::SmallVector<mlir::Value> destArg; 2378 mlir::NamedAttrList temp; 2379 if (parser.parseAttribute(ivalue, "i", temp) || parser.parseComma() || 2380 parser.parseSuccessorAndUseList(dest, destArg)) 2381 return mlir::failure(); 2382 ivalues.push_back(ivalue); 2383 dests.push_back(dest); 2384 destArgs.push_back(destArg); 2385 if (!parser.parseOptionalRSquare()) 2386 break; 2387 if (parser.parseComma()) 2388 return mlir::failure(); 2389 } 2390 auto &bld = parser.getBuilder(); 2391 result.addAttribute(casesAttr, bld.getArrayAttr(ivalues)); 2392 llvm::SmallVector<int32_t> argOffs; 2393 int32_t sumArgs = 0; 2394 const auto count = dests.size(); 2395 for (std::remove_const_t<decltype(count)> i = 0; i != count; ++i) { 2396 result.addSuccessors(dests[i]); 2397 result.addOperands(destArgs[i]); 2398 auto argSize = destArgs[i].size(); 2399 argOffs.push_back(argSize); 2400 sumArgs += argSize; 2401 } 2402 result.addAttribute(operandSegmentAttr, 2403 bld.getI32VectorAttr({1, 0, sumArgs})); 2404 result.addAttribute(getTargetOffsetAttr(), bld.getI32VectorAttr(argOffs)); 2405 return mlir::success(); 2406 } 2407 2408 template <typename OpT> 2409 static void printIntegralSwitchTerminator(OpT op, mlir::OpAsmPrinter &p) { 2410 p << ' '; 2411 p.printOperand(op.getSelector()); 2412 p << " : " << op.getSelector().getType() << " ["; 2413 auto cases = 2414 op->template getAttrOfType<mlir::ArrayAttr>(op.getCasesAttr()).getValue(); 2415 auto count = op.getNumConditions(); 2416 for (decltype(count) i = 0; i != count; ++i) { 2417 if (i) 2418 p << ", "; 2419 auto &attr = cases[i]; 2420 if (auto intAttr = attr.template dyn_cast_or_null<mlir::IntegerAttr>()) 2421 p << intAttr.getValue(); 2422 else 2423 p.printAttribute(attr); 2424 p << ", "; 2425 op.printSuccessorAtIndex(p, i); 2426 } 2427 p << ']'; 2428 p.printOptionalAttrDict( 2429 op->getAttrs(), {op.getCasesAttr(), getCompareOffsetAttr(), 2430 getTargetOffsetAttr(), op.getOperandSegmentSizeAttr()}); 2431 } 2432 2433 //===----------------------------------------------------------------------===// 2434 // SelectOp 2435 //===----------------------------------------------------------------------===// 2436 2437 mlir::LogicalResult fir::SelectOp::verify() { 2438 return verifyIntegralSwitchTerminator(*this); 2439 } 2440 2441 mlir::ParseResult fir::SelectOp::parse(mlir::OpAsmParser &parser, 2442 mlir::OperationState &result) { 2443 return parseIntegralSwitchTerminator(parser, result, getCasesAttr(), 2444 getOperandSegmentSizeAttr()); 2445 } 2446 2447 void fir::SelectOp::print(mlir::OpAsmPrinter &p) { 2448 printIntegralSwitchTerminator(*this, p); 2449 } 2450 2451 template <typename A, typename... AdditionalArgs> 2452 static A getSubOperands(unsigned pos, A allArgs, 2453 mlir::DenseIntElementsAttr ranges, 2454 AdditionalArgs &&...additionalArgs) { 2455 unsigned start = 0; 2456 for (unsigned i = 0; i < pos; ++i) 2457 start += (*(ranges.begin() + i)).getZExtValue(); 2458 return allArgs.slice(start, (*(ranges.begin() + pos)).getZExtValue(), 2459 std::forward<AdditionalArgs>(additionalArgs)...); 2460 } 2461 2462 static mlir::MutableOperandRange 2463 getMutableSuccessorOperands(unsigned pos, mlir::MutableOperandRange operands, 2464 llvm::StringRef offsetAttr) { 2465 mlir::Operation *owner = operands.getOwner(); 2466 mlir::NamedAttribute targetOffsetAttr = 2467 *owner->getAttrDictionary().getNamed(offsetAttr); 2468 return getSubOperands( 2469 pos, operands, 2470 targetOffsetAttr.getValue().cast<mlir::DenseIntElementsAttr>(), 2471 mlir::MutableOperandRange::OperandSegment(pos, targetOffsetAttr)); 2472 } 2473 2474 static unsigned denseElementsSize(mlir::DenseIntElementsAttr attr) { 2475 return attr.getNumElements(); 2476 } 2477 2478 llvm::Optional<mlir::OperandRange> fir::SelectOp::getCompareOperands(unsigned) { 2479 return {}; 2480 } 2481 2482 llvm::Optional<llvm::ArrayRef<mlir::Value>> 2483 fir::SelectOp::getCompareOperands(llvm::ArrayRef<mlir::Value>, unsigned) { 2484 return {}; 2485 } 2486 2487 mlir::SuccessorOperands fir::SelectOp::getSuccessorOperands(unsigned oper) { 2488 return mlir::SuccessorOperands(::getMutableSuccessorOperands( 2489 oper, getTargetArgsMutable(), getTargetOffsetAttr())); 2490 } 2491 2492 llvm::Optional<llvm::ArrayRef<mlir::Value>> 2493 fir::SelectOp::getSuccessorOperands(llvm::ArrayRef<mlir::Value> operands, 2494 unsigned oper) { 2495 auto a = 2496 (*this)->getAttrOfType<mlir::DenseIntElementsAttr>(getTargetOffsetAttr()); 2497 auto segments = (*this)->getAttrOfType<mlir::DenseIntElementsAttr>( 2498 getOperandSegmentSizeAttr()); 2499 return {getSubOperands(oper, getSubOperands(2, operands, segments), a)}; 2500 } 2501 2502 llvm::Optional<mlir::ValueRange> 2503 fir::SelectOp::getSuccessorOperands(mlir::ValueRange operands, unsigned oper) { 2504 auto a = 2505 (*this)->getAttrOfType<mlir::DenseIntElementsAttr>(getTargetOffsetAttr()); 2506 auto segments = (*this)->getAttrOfType<mlir::DenseIntElementsAttr>( 2507 getOperandSegmentSizeAttr()); 2508 return {getSubOperands(oper, getSubOperands(2, operands, segments), a)}; 2509 } 2510 2511 unsigned fir::SelectOp::targetOffsetSize() { 2512 return denseElementsSize((*this)->getAttrOfType<mlir::DenseIntElementsAttr>( 2513 getTargetOffsetAttr())); 2514 } 2515 2516 //===----------------------------------------------------------------------===// 2517 // SelectCaseOp 2518 //===----------------------------------------------------------------------===// 2519 2520 llvm::Optional<mlir::OperandRange> 2521 fir::SelectCaseOp::getCompareOperands(unsigned cond) { 2522 auto a = (*this)->getAttrOfType<mlir::DenseIntElementsAttr>( 2523 getCompareOffsetAttr()); 2524 return {getSubOperands(cond, getCompareArgs(), a)}; 2525 } 2526 2527 llvm::Optional<llvm::ArrayRef<mlir::Value>> 2528 fir::SelectCaseOp::getCompareOperands(llvm::ArrayRef<mlir::Value> operands, 2529 unsigned cond) { 2530 auto a = (*this)->getAttrOfType<mlir::DenseIntElementsAttr>( 2531 getCompareOffsetAttr()); 2532 auto segments = (*this)->getAttrOfType<mlir::DenseIntElementsAttr>( 2533 getOperandSegmentSizeAttr()); 2534 return {getSubOperands(cond, getSubOperands(1, operands, segments), a)}; 2535 } 2536 2537 llvm::Optional<mlir::ValueRange> 2538 fir::SelectCaseOp::getCompareOperands(mlir::ValueRange operands, 2539 unsigned cond) { 2540 auto a = (*this)->getAttrOfType<mlir::DenseIntElementsAttr>( 2541 getCompareOffsetAttr()); 2542 auto segments = (*this)->getAttrOfType<mlir::DenseIntElementsAttr>( 2543 getOperandSegmentSizeAttr()); 2544 return {getSubOperands(cond, getSubOperands(1, operands, segments), a)}; 2545 } 2546 2547 mlir::SuccessorOperands fir::SelectCaseOp::getSuccessorOperands(unsigned oper) { 2548 return mlir::SuccessorOperands(::getMutableSuccessorOperands( 2549 oper, getTargetArgsMutable(), getTargetOffsetAttr())); 2550 } 2551 2552 llvm::Optional<llvm::ArrayRef<mlir::Value>> 2553 fir::SelectCaseOp::getSuccessorOperands(llvm::ArrayRef<mlir::Value> operands, 2554 unsigned oper) { 2555 auto a = 2556 (*this)->getAttrOfType<mlir::DenseIntElementsAttr>(getTargetOffsetAttr()); 2557 auto segments = (*this)->getAttrOfType<mlir::DenseIntElementsAttr>( 2558 getOperandSegmentSizeAttr()); 2559 return {getSubOperands(oper, getSubOperands(2, operands, segments), a)}; 2560 } 2561 2562 llvm::Optional<mlir::ValueRange> 2563 fir::SelectCaseOp::getSuccessorOperands(mlir::ValueRange operands, 2564 unsigned oper) { 2565 auto a = 2566 (*this)->getAttrOfType<mlir::DenseIntElementsAttr>(getTargetOffsetAttr()); 2567 auto segments = (*this)->getAttrOfType<mlir::DenseIntElementsAttr>( 2568 getOperandSegmentSizeAttr()); 2569 return {getSubOperands(oper, getSubOperands(2, operands, segments), a)}; 2570 } 2571 2572 // parser for fir.select_case Op 2573 mlir::ParseResult fir::SelectCaseOp::parse(mlir::OpAsmParser &parser, 2574 mlir::OperationState &result) { 2575 mlir::OpAsmParser::UnresolvedOperand selector; 2576 mlir::Type type; 2577 if (fir::parseSelector(parser, result, selector, type)) 2578 return mlir::failure(); 2579 2580 llvm::SmallVector<mlir::Attribute> attrs; 2581 llvm::SmallVector<mlir::OpAsmParser::UnresolvedOperand> opers; 2582 llvm::SmallVector<mlir::Block *> dests; 2583 llvm::SmallVector<llvm::SmallVector<mlir::Value>> destArgs; 2584 llvm::SmallVector<std::int32_t> argOffs; 2585 std::int32_t offSize = 0; 2586 while (true) { 2587 mlir::Attribute attr; 2588 mlir::Block *dest; 2589 llvm::SmallVector<mlir::Value> destArg; 2590 mlir::NamedAttrList temp; 2591 if (parser.parseAttribute(attr, "a", temp) || isValidCaseAttr(attr) || 2592 parser.parseComma()) 2593 return mlir::failure(); 2594 attrs.push_back(attr); 2595 if (attr.dyn_cast_or_null<mlir::UnitAttr>()) { 2596 argOffs.push_back(0); 2597 } else if (attr.dyn_cast_or_null<fir::ClosedIntervalAttr>()) { 2598 mlir::OpAsmParser::UnresolvedOperand oper1; 2599 mlir::OpAsmParser::UnresolvedOperand oper2; 2600 if (parser.parseOperand(oper1) || parser.parseComma() || 2601 parser.parseOperand(oper2) || parser.parseComma()) 2602 return mlir::failure(); 2603 opers.push_back(oper1); 2604 opers.push_back(oper2); 2605 argOffs.push_back(2); 2606 offSize += 2; 2607 } else { 2608 mlir::OpAsmParser::UnresolvedOperand oper; 2609 if (parser.parseOperand(oper) || parser.parseComma()) 2610 return mlir::failure(); 2611 opers.push_back(oper); 2612 argOffs.push_back(1); 2613 ++offSize; 2614 } 2615 if (parser.parseSuccessorAndUseList(dest, destArg)) 2616 return mlir::failure(); 2617 dests.push_back(dest); 2618 destArgs.push_back(destArg); 2619 if (mlir::succeeded(parser.parseOptionalRSquare())) 2620 break; 2621 if (parser.parseComma()) 2622 return mlir::failure(); 2623 } 2624 result.addAttribute(fir::SelectCaseOp::getCasesAttr(), 2625 parser.getBuilder().getArrayAttr(attrs)); 2626 if (parser.resolveOperands(opers, type, result.operands)) 2627 return mlir::failure(); 2628 llvm::SmallVector<int32_t> targOffs; 2629 int32_t toffSize = 0; 2630 const auto count = dests.size(); 2631 for (std::remove_const_t<decltype(count)> i = 0; i != count; ++i) { 2632 result.addSuccessors(dests[i]); 2633 result.addOperands(destArgs[i]); 2634 auto argSize = destArgs[i].size(); 2635 targOffs.push_back(argSize); 2636 toffSize += argSize; 2637 } 2638 auto &bld = parser.getBuilder(); 2639 result.addAttribute(fir::SelectCaseOp::getOperandSegmentSizeAttr(), 2640 bld.getI32VectorAttr({1, offSize, toffSize})); 2641 result.addAttribute(getCompareOffsetAttr(), bld.getI32VectorAttr(argOffs)); 2642 result.addAttribute(getTargetOffsetAttr(), bld.getI32VectorAttr(targOffs)); 2643 return mlir::success(); 2644 } 2645 2646 void fir::SelectCaseOp::print(mlir::OpAsmPrinter &p) { 2647 p << ' '; 2648 p.printOperand(getSelector()); 2649 p << " : " << getSelector().getType() << " ["; 2650 auto cases = 2651 getOperation()->getAttrOfType<mlir::ArrayAttr>(getCasesAttr()).getValue(); 2652 auto count = getNumConditions(); 2653 for (decltype(count) i = 0; i != count; ++i) { 2654 if (i) 2655 p << ", "; 2656 p << cases[i] << ", "; 2657 if (!cases[i].isa<mlir::UnitAttr>()) { 2658 auto caseArgs = *getCompareOperands(i); 2659 p.printOperand(*caseArgs.begin()); 2660 p << ", "; 2661 if (cases[i].isa<fir::ClosedIntervalAttr>()) { 2662 p.printOperand(*(++caseArgs.begin())); 2663 p << ", "; 2664 } 2665 } 2666 printSuccessorAtIndex(p, i); 2667 } 2668 p << ']'; 2669 p.printOptionalAttrDict(getOperation()->getAttrs(), 2670 {getCasesAttr(), getCompareOffsetAttr(), 2671 getTargetOffsetAttr(), getOperandSegmentSizeAttr()}); 2672 } 2673 2674 unsigned fir::SelectCaseOp::compareOffsetSize() { 2675 return denseElementsSize((*this)->getAttrOfType<mlir::DenseIntElementsAttr>( 2676 getCompareOffsetAttr())); 2677 } 2678 2679 unsigned fir::SelectCaseOp::targetOffsetSize() { 2680 return denseElementsSize((*this)->getAttrOfType<mlir::DenseIntElementsAttr>( 2681 getTargetOffsetAttr())); 2682 } 2683 2684 void fir::SelectCaseOp::build(mlir::OpBuilder &builder, 2685 mlir::OperationState &result, 2686 mlir::Value selector, 2687 llvm::ArrayRef<mlir::Attribute> compareAttrs, 2688 llvm::ArrayRef<mlir::ValueRange> cmpOperands, 2689 llvm::ArrayRef<mlir::Block *> destinations, 2690 llvm::ArrayRef<mlir::ValueRange> destOperands, 2691 llvm::ArrayRef<mlir::NamedAttribute> attributes) { 2692 result.addOperands(selector); 2693 result.addAttribute(getCasesAttr(), builder.getArrayAttr(compareAttrs)); 2694 llvm::SmallVector<int32_t> operOffs; 2695 int32_t operSize = 0; 2696 for (auto attr : compareAttrs) { 2697 if (attr.isa<fir::ClosedIntervalAttr>()) { 2698 operOffs.push_back(2); 2699 operSize += 2; 2700 } else if (attr.isa<mlir::UnitAttr>()) { 2701 operOffs.push_back(0); 2702 } else { 2703 operOffs.push_back(1); 2704 ++operSize; 2705 } 2706 } 2707 for (auto ops : cmpOperands) 2708 result.addOperands(ops); 2709 result.addAttribute(getCompareOffsetAttr(), 2710 builder.getI32VectorAttr(operOffs)); 2711 const auto count = destinations.size(); 2712 for (auto d : destinations) 2713 result.addSuccessors(d); 2714 const auto opCount = destOperands.size(); 2715 llvm::SmallVector<std::int32_t> argOffs; 2716 std::int32_t sumArgs = 0; 2717 for (std::remove_const_t<decltype(count)> i = 0; i != count; ++i) { 2718 if (i < opCount) { 2719 result.addOperands(destOperands[i]); 2720 const auto argSz = destOperands[i].size(); 2721 argOffs.push_back(argSz); 2722 sumArgs += argSz; 2723 } else { 2724 argOffs.push_back(0); 2725 } 2726 } 2727 result.addAttribute(getOperandSegmentSizeAttr(), 2728 builder.getI32VectorAttr({1, operSize, sumArgs})); 2729 result.addAttribute(getTargetOffsetAttr(), builder.getI32VectorAttr(argOffs)); 2730 result.addAttributes(attributes); 2731 } 2732 2733 /// This builder has a slightly simplified interface in that the list of 2734 /// operands need not be partitioned by the builder. Instead the operands are 2735 /// partitioned here, before being passed to the default builder. This 2736 /// partitioning is unchecked, so can go awry on bad input. 2737 void fir::SelectCaseOp::build(mlir::OpBuilder &builder, 2738 mlir::OperationState &result, 2739 mlir::Value selector, 2740 llvm::ArrayRef<mlir::Attribute> compareAttrs, 2741 llvm::ArrayRef<mlir::Value> cmpOpList, 2742 llvm::ArrayRef<mlir::Block *> destinations, 2743 llvm::ArrayRef<mlir::ValueRange> destOperands, 2744 llvm::ArrayRef<mlir::NamedAttribute> attributes) { 2745 llvm::SmallVector<mlir::ValueRange> cmpOpers; 2746 auto iter = cmpOpList.begin(); 2747 for (auto &attr : compareAttrs) { 2748 if (attr.isa<fir::ClosedIntervalAttr>()) { 2749 cmpOpers.push_back(mlir::ValueRange({iter, iter + 2})); 2750 iter += 2; 2751 } else if (attr.isa<mlir::UnitAttr>()) { 2752 cmpOpers.push_back(mlir::ValueRange{}); 2753 } else { 2754 cmpOpers.push_back(mlir::ValueRange({iter, iter + 1})); 2755 ++iter; 2756 } 2757 } 2758 build(builder, result, selector, compareAttrs, cmpOpers, destinations, 2759 destOperands, attributes); 2760 } 2761 2762 mlir::LogicalResult fir::SelectCaseOp::verify() { 2763 if (!getSelector() 2764 .getType() 2765 .isa<mlir::IntegerType, mlir::IndexType, fir::IntegerType, 2766 fir::LogicalType, fir::CharacterType>()) 2767 return emitOpError("must be an integer, character, or logical"); 2768 auto cases = 2769 getOperation()->getAttrOfType<mlir::ArrayAttr>(getCasesAttr()).getValue(); 2770 auto count = getNumDest(); 2771 if (count == 0) 2772 return emitOpError("must have at least one successor"); 2773 if (getNumConditions() != count) 2774 return emitOpError("number of conditions and successors don't match"); 2775 if (compareOffsetSize() != count) 2776 return emitOpError("incorrect number of compare operand groups"); 2777 if (targetOffsetSize() != count) 2778 return emitOpError("incorrect number of successor operand groups"); 2779 for (decltype(count) i = 0; i != count; ++i) { 2780 auto &attr = cases[i]; 2781 if (!(attr.isa<fir::PointIntervalAttr>() || 2782 attr.isa<fir::LowerBoundAttr>() || attr.isa<fir::UpperBoundAttr>() || 2783 attr.isa<fir::ClosedIntervalAttr>() || attr.isa<mlir::UnitAttr>())) 2784 return emitOpError("incorrect select case attribute type"); 2785 } 2786 return mlir::success(); 2787 } 2788 2789 //===----------------------------------------------------------------------===// 2790 // SelectRankOp 2791 //===----------------------------------------------------------------------===// 2792 2793 mlir::LogicalResult fir::SelectRankOp::verify() { 2794 return verifyIntegralSwitchTerminator(*this); 2795 } 2796 2797 mlir::ParseResult fir::SelectRankOp::parse(mlir::OpAsmParser &parser, 2798 mlir::OperationState &result) { 2799 return parseIntegralSwitchTerminator(parser, result, getCasesAttr(), 2800 getOperandSegmentSizeAttr()); 2801 } 2802 2803 void fir::SelectRankOp::print(mlir::OpAsmPrinter &p) { 2804 printIntegralSwitchTerminator(*this, p); 2805 } 2806 2807 llvm::Optional<mlir::OperandRange> 2808 fir::SelectRankOp::getCompareOperands(unsigned) { 2809 return {}; 2810 } 2811 2812 llvm::Optional<llvm::ArrayRef<mlir::Value>> 2813 fir::SelectRankOp::getCompareOperands(llvm::ArrayRef<mlir::Value>, unsigned) { 2814 return {}; 2815 } 2816 2817 mlir::SuccessorOperands fir::SelectRankOp::getSuccessorOperands(unsigned oper) { 2818 return mlir::SuccessorOperands(::getMutableSuccessorOperands( 2819 oper, getTargetArgsMutable(), getTargetOffsetAttr())); 2820 } 2821 2822 llvm::Optional<llvm::ArrayRef<mlir::Value>> 2823 fir::SelectRankOp::getSuccessorOperands(llvm::ArrayRef<mlir::Value> operands, 2824 unsigned oper) { 2825 auto a = 2826 (*this)->getAttrOfType<mlir::DenseIntElementsAttr>(getTargetOffsetAttr()); 2827 auto segments = (*this)->getAttrOfType<mlir::DenseIntElementsAttr>( 2828 getOperandSegmentSizeAttr()); 2829 return {getSubOperands(oper, getSubOperands(2, operands, segments), a)}; 2830 } 2831 2832 llvm::Optional<mlir::ValueRange> 2833 fir::SelectRankOp::getSuccessorOperands(mlir::ValueRange operands, 2834 unsigned oper) { 2835 auto a = 2836 (*this)->getAttrOfType<mlir::DenseIntElementsAttr>(getTargetOffsetAttr()); 2837 auto segments = (*this)->getAttrOfType<mlir::DenseIntElementsAttr>( 2838 getOperandSegmentSizeAttr()); 2839 return {getSubOperands(oper, getSubOperands(2, operands, segments), a)}; 2840 } 2841 2842 unsigned fir::SelectRankOp::targetOffsetSize() { 2843 return denseElementsSize((*this)->getAttrOfType<mlir::DenseIntElementsAttr>( 2844 getTargetOffsetAttr())); 2845 } 2846 2847 //===----------------------------------------------------------------------===// 2848 // SelectTypeOp 2849 //===----------------------------------------------------------------------===// 2850 2851 llvm::Optional<mlir::OperandRange> 2852 fir::SelectTypeOp::getCompareOperands(unsigned) { 2853 return {}; 2854 } 2855 2856 llvm::Optional<llvm::ArrayRef<mlir::Value>> 2857 fir::SelectTypeOp::getCompareOperands(llvm::ArrayRef<mlir::Value>, unsigned) { 2858 return {}; 2859 } 2860 2861 mlir::SuccessorOperands fir::SelectTypeOp::getSuccessorOperands(unsigned oper) { 2862 return mlir::SuccessorOperands(::getMutableSuccessorOperands( 2863 oper, getTargetArgsMutable(), getTargetOffsetAttr())); 2864 } 2865 2866 llvm::Optional<llvm::ArrayRef<mlir::Value>> 2867 fir::SelectTypeOp::getSuccessorOperands(llvm::ArrayRef<mlir::Value> operands, 2868 unsigned oper) { 2869 auto a = 2870 (*this)->getAttrOfType<mlir::DenseIntElementsAttr>(getTargetOffsetAttr()); 2871 auto segments = (*this)->getAttrOfType<mlir::DenseIntElementsAttr>( 2872 getOperandSegmentSizeAttr()); 2873 return {getSubOperands(oper, getSubOperands(2, operands, segments), a)}; 2874 } 2875 2876 mlir::ParseResult fir::SelectTypeOp::parse(mlir::OpAsmParser &parser, 2877 mlir::OperationState &result) { 2878 mlir::OpAsmParser::UnresolvedOperand selector; 2879 mlir::Type type; 2880 if (fir::parseSelector(parser, result, selector, type)) 2881 return mlir::failure(); 2882 2883 llvm::SmallVector<mlir::Attribute> attrs; 2884 llvm::SmallVector<mlir::Block *> dests; 2885 llvm::SmallVector<llvm::SmallVector<mlir::Value>> destArgs; 2886 while (true) { 2887 mlir::Attribute attr; 2888 mlir::Block *dest; 2889 llvm::SmallVector<mlir::Value> destArg; 2890 mlir::NamedAttrList temp; 2891 if (parser.parseAttribute(attr, "a", temp) || parser.parseComma() || 2892 parser.parseSuccessorAndUseList(dest, destArg)) 2893 return mlir::failure(); 2894 attrs.push_back(attr); 2895 dests.push_back(dest); 2896 destArgs.push_back(destArg); 2897 if (mlir::succeeded(parser.parseOptionalRSquare())) 2898 break; 2899 if (parser.parseComma()) 2900 return mlir::failure(); 2901 } 2902 auto &bld = parser.getBuilder(); 2903 result.addAttribute(fir::SelectTypeOp::getCasesAttr(), 2904 bld.getArrayAttr(attrs)); 2905 llvm::SmallVector<int32_t> argOffs; 2906 int32_t offSize = 0; 2907 const auto count = dests.size(); 2908 for (std::remove_const_t<decltype(count)> i = 0; i != count; ++i) { 2909 result.addSuccessors(dests[i]); 2910 result.addOperands(destArgs[i]); 2911 auto argSize = destArgs[i].size(); 2912 argOffs.push_back(argSize); 2913 offSize += argSize; 2914 } 2915 result.addAttribute(fir::SelectTypeOp::getOperandSegmentSizeAttr(), 2916 bld.getI32VectorAttr({1, 0, offSize})); 2917 result.addAttribute(getTargetOffsetAttr(), bld.getI32VectorAttr(argOffs)); 2918 return mlir::success(); 2919 } 2920 2921 unsigned fir::SelectTypeOp::targetOffsetSize() { 2922 return denseElementsSize((*this)->getAttrOfType<mlir::DenseIntElementsAttr>( 2923 getTargetOffsetAttr())); 2924 } 2925 2926 void fir::SelectTypeOp::print(mlir::OpAsmPrinter &p) { 2927 p << ' '; 2928 p.printOperand(getSelector()); 2929 p << " : " << getSelector().getType() << " ["; 2930 auto cases = 2931 getOperation()->getAttrOfType<mlir::ArrayAttr>(getCasesAttr()).getValue(); 2932 auto count = getNumConditions(); 2933 for (decltype(count) i = 0; i != count; ++i) { 2934 if (i) 2935 p << ", "; 2936 p << cases[i] << ", "; 2937 printSuccessorAtIndex(p, i); 2938 } 2939 p << ']'; 2940 p.printOptionalAttrDict(getOperation()->getAttrs(), 2941 {getCasesAttr(), getCompareOffsetAttr(), 2942 getTargetOffsetAttr(), 2943 fir::SelectTypeOp::getOperandSegmentSizeAttr()}); 2944 } 2945 2946 mlir::LogicalResult fir::SelectTypeOp::verify() { 2947 if (!(getSelector().getType().isa<fir::BoxType>())) 2948 return emitOpError("must be a boxed type"); 2949 auto cases = 2950 getOperation()->getAttrOfType<mlir::ArrayAttr>(getCasesAttr()).getValue(); 2951 auto count = getNumDest(); 2952 if (count == 0) 2953 return emitOpError("must have at least one successor"); 2954 if (getNumConditions() != count) 2955 return emitOpError("number of conditions and successors don't match"); 2956 if (targetOffsetSize() != count) 2957 return emitOpError("incorrect number of successor operand groups"); 2958 for (decltype(count) i = 0; i != count; ++i) { 2959 auto &attr = cases[i]; 2960 if (!(attr.isa<fir::ExactTypeAttr>() || attr.isa<fir::SubclassAttr>() || 2961 attr.isa<mlir::UnitAttr>())) 2962 return emitOpError("invalid type-case alternative"); 2963 } 2964 return mlir::success(); 2965 } 2966 2967 void fir::SelectTypeOp::build(mlir::OpBuilder &builder, 2968 mlir::OperationState &result, 2969 mlir::Value selector, 2970 llvm::ArrayRef<mlir::Attribute> typeOperands, 2971 llvm::ArrayRef<mlir::Block *> destinations, 2972 llvm::ArrayRef<mlir::ValueRange> destOperands, 2973 llvm::ArrayRef<mlir::NamedAttribute> attributes) { 2974 result.addOperands(selector); 2975 result.addAttribute(getCasesAttr(), builder.getArrayAttr(typeOperands)); 2976 const auto count = destinations.size(); 2977 for (mlir::Block *dest : destinations) 2978 result.addSuccessors(dest); 2979 const auto opCount = destOperands.size(); 2980 llvm::SmallVector<int32_t> argOffs; 2981 int32_t sumArgs = 0; 2982 for (std::remove_const_t<decltype(count)> i = 0; i != count; ++i) { 2983 if (i < opCount) { 2984 result.addOperands(destOperands[i]); 2985 const auto argSz = destOperands[i].size(); 2986 argOffs.push_back(argSz); 2987 sumArgs += argSz; 2988 } else { 2989 argOffs.push_back(0); 2990 } 2991 } 2992 result.addAttribute(getOperandSegmentSizeAttr(), 2993 builder.getI32VectorAttr({1, 0, sumArgs})); 2994 result.addAttribute(getTargetOffsetAttr(), builder.getI32VectorAttr(argOffs)); 2995 result.addAttributes(attributes); 2996 } 2997 2998 //===----------------------------------------------------------------------===// 2999 // ShapeOp 3000 //===----------------------------------------------------------------------===// 3001 3002 mlir::LogicalResult fir::ShapeOp::verify() { 3003 auto size = getExtents().size(); 3004 auto shapeTy = getType().dyn_cast<fir::ShapeType>(); 3005 assert(shapeTy && "must be a shape type"); 3006 if (shapeTy.getRank() != size) 3007 return emitOpError("shape type rank mismatch"); 3008 return mlir::success(); 3009 } 3010 3011 //===----------------------------------------------------------------------===// 3012 // ShapeShiftOp 3013 //===----------------------------------------------------------------------===// 3014 3015 mlir::LogicalResult fir::ShapeShiftOp::verify() { 3016 auto size = getPairs().size(); 3017 if (size < 2 || size > 16 * 2) 3018 return emitOpError("incorrect number of args"); 3019 if (size % 2 != 0) 3020 return emitOpError("requires a multiple of 2 args"); 3021 auto shapeTy = getType().dyn_cast<fir::ShapeShiftType>(); 3022 assert(shapeTy && "must be a shape shift type"); 3023 if (shapeTy.getRank() * 2 != size) 3024 return emitOpError("shape type rank mismatch"); 3025 return mlir::success(); 3026 } 3027 3028 //===----------------------------------------------------------------------===// 3029 // ShiftOp 3030 //===----------------------------------------------------------------------===// 3031 3032 mlir::LogicalResult fir::ShiftOp::verify() { 3033 auto size = getOrigins().size(); 3034 auto shiftTy = getType().dyn_cast<fir::ShiftType>(); 3035 assert(shiftTy && "must be a shift type"); 3036 if (shiftTy.getRank() != size) 3037 return emitOpError("shift type rank mismatch"); 3038 return mlir::success(); 3039 } 3040 3041 //===----------------------------------------------------------------------===// 3042 // SliceOp 3043 //===----------------------------------------------------------------------===// 3044 3045 void fir::SliceOp::build(mlir::OpBuilder &builder, mlir::OperationState &result, 3046 mlir::ValueRange trips, mlir::ValueRange path, 3047 mlir::ValueRange substr) { 3048 const auto rank = trips.size() / 3; 3049 auto sliceTy = fir::SliceType::get(builder.getContext(), rank); 3050 build(builder, result, sliceTy, trips, path, substr); 3051 } 3052 3053 /// Return the output rank of a slice op. The output rank must be between 1 and 3054 /// the rank of the array being sliced (inclusive). 3055 unsigned fir::SliceOp::getOutputRank(mlir::ValueRange triples) { 3056 unsigned rank = 0; 3057 if (!triples.empty()) { 3058 for (unsigned i = 1, end = triples.size(); i < end; i += 3) { 3059 auto *op = triples[i].getDefiningOp(); 3060 if (!mlir::isa_and_nonnull<fir::UndefOp>(op)) 3061 ++rank; 3062 } 3063 assert(rank > 0); 3064 } 3065 return rank; 3066 } 3067 3068 mlir::LogicalResult fir::SliceOp::verify() { 3069 auto size = getTriples().size(); 3070 if (size < 3 || size > 16 * 3) 3071 return emitOpError("incorrect number of args for triple"); 3072 if (size % 3 != 0) 3073 return emitOpError("requires a multiple of 3 args"); 3074 auto sliceTy = getType().dyn_cast<fir::SliceType>(); 3075 assert(sliceTy && "must be a slice type"); 3076 if (sliceTy.getRank() * 3 != size) 3077 return emitOpError("slice type rank mismatch"); 3078 return mlir::success(); 3079 } 3080 3081 //===----------------------------------------------------------------------===// 3082 // StoreOp 3083 //===----------------------------------------------------------------------===// 3084 3085 mlir::Type fir::StoreOp::elementType(mlir::Type refType) { 3086 return fir::dyn_cast_ptrEleTy(refType); 3087 } 3088 3089 mlir::ParseResult fir::StoreOp::parse(mlir::OpAsmParser &parser, 3090 mlir::OperationState &result) { 3091 mlir::Type type; 3092 mlir::OpAsmParser::UnresolvedOperand oper; 3093 mlir::OpAsmParser::UnresolvedOperand store; 3094 if (parser.parseOperand(oper) || parser.parseKeyword("to") || 3095 parser.parseOperand(store) || 3096 parser.parseOptionalAttrDict(result.attributes) || 3097 parser.parseColonType(type) || 3098 parser.resolveOperand(oper, fir::StoreOp::elementType(type), 3099 result.operands) || 3100 parser.resolveOperand(store, type, result.operands)) 3101 return mlir::failure(); 3102 return mlir::success(); 3103 } 3104 3105 void fir::StoreOp::print(mlir::OpAsmPrinter &p) { 3106 p << ' '; 3107 p.printOperand(getValue()); 3108 p << " to "; 3109 p.printOperand(getMemref()); 3110 p.printOptionalAttrDict(getOperation()->getAttrs(), {}); 3111 p << " : " << getMemref().getType(); 3112 } 3113 3114 mlir::LogicalResult fir::StoreOp::verify() { 3115 if (getValue().getType() != fir::dyn_cast_ptrEleTy(getMemref().getType())) 3116 return emitOpError("store value type must match memory reference type"); 3117 if (fir::isa_unknown_size_box(getValue().getType())) 3118 return emitOpError("cannot store !fir.box of unknown rank or type"); 3119 return mlir::success(); 3120 } 3121 3122 //===----------------------------------------------------------------------===// 3123 // StringLitOp 3124 //===----------------------------------------------------------------------===// 3125 3126 inline fir::CharacterType::KindTy stringLitOpGetKind(fir::StringLitOp op) { 3127 auto eleTy = op.getType().cast<fir::SequenceType>().getEleTy(); 3128 return eleTy.cast<fir::CharacterType>().getFKind(); 3129 } 3130 3131 bool fir::StringLitOp::isWideValue() { return stringLitOpGetKind(*this) != 1; } 3132 3133 static mlir::NamedAttribute 3134 mkNamedIntegerAttr(mlir::OpBuilder &builder, llvm::StringRef name, int64_t v) { 3135 assert(v > 0); 3136 return builder.getNamedAttr( 3137 name, builder.getIntegerAttr(builder.getIntegerType(64), v)); 3138 } 3139 3140 void fir::StringLitOp::build(mlir::OpBuilder &builder, 3141 mlir::OperationState &result, 3142 fir::CharacterType inType, llvm::StringRef val, 3143 llvm::Optional<int64_t> len) { 3144 auto valAttr = builder.getNamedAttr(value(), builder.getStringAttr(val)); 3145 int64_t length = len ? *len : inType.getLen(); 3146 auto lenAttr = mkNamedIntegerAttr(builder, size(), length); 3147 result.addAttributes({valAttr, lenAttr}); 3148 result.addTypes(inType); 3149 } 3150 3151 template <typename C> 3152 static mlir::ArrayAttr convertToArrayAttr(mlir::OpBuilder &builder, 3153 llvm::ArrayRef<C> xlist) { 3154 llvm::SmallVector<mlir::Attribute> attrs; 3155 auto ty = builder.getIntegerType(8 * sizeof(C)); 3156 for (auto ch : xlist) 3157 attrs.push_back(builder.getIntegerAttr(ty, ch)); 3158 return builder.getArrayAttr(attrs); 3159 } 3160 3161 void fir::StringLitOp::build(mlir::OpBuilder &builder, 3162 mlir::OperationState &result, 3163 fir::CharacterType inType, 3164 llvm::ArrayRef<char> vlist, 3165 llvm::Optional<std::int64_t> len) { 3166 auto valAttr = 3167 builder.getNamedAttr(xlist(), convertToArrayAttr(builder, vlist)); 3168 std::int64_t length = len ? *len : inType.getLen(); 3169 auto lenAttr = mkNamedIntegerAttr(builder, size(), length); 3170 result.addAttributes({valAttr, lenAttr}); 3171 result.addTypes(inType); 3172 } 3173 3174 void fir::StringLitOp::build(mlir::OpBuilder &builder, 3175 mlir::OperationState &result, 3176 fir::CharacterType inType, 3177 llvm::ArrayRef<char16_t> vlist, 3178 llvm::Optional<std::int64_t> len) { 3179 auto valAttr = 3180 builder.getNamedAttr(xlist(), convertToArrayAttr(builder, vlist)); 3181 std::int64_t length = len ? *len : inType.getLen(); 3182 auto lenAttr = mkNamedIntegerAttr(builder, size(), length); 3183 result.addAttributes({valAttr, lenAttr}); 3184 result.addTypes(inType); 3185 } 3186 3187 void fir::StringLitOp::build(mlir::OpBuilder &builder, 3188 mlir::OperationState &result, 3189 fir::CharacterType inType, 3190 llvm::ArrayRef<char32_t> vlist, 3191 llvm::Optional<std::int64_t> len) { 3192 auto valAttr = 3193 builder.getNamedAttr(xlist(), convertToArrayAttr(builder, vlist)); 3194 std::int64_t length = len ? *len : inType.getLen(); 3195 auto lenAttr = mkNamedIntegerAttr(builder, size(), length); 3196 result.addAttributes({valAttr, lenAttr}); 3197 result.addTypes(inType); 3198 } 3199 3200 mlir::ParseResult fir::StringLitOp::parse(mlir::OpAsmParser &parser, 3201 mlir::OperationState &result) { 3202 auto &builder = parser.getBuilder(); 3203 mlir::Attribute val; 3204 mlir::NamedAttrList attrs; 3205 llvm::SMLoc trailingTypeLoc; 3206 if (parser.parseAttribute(val, "fake", attrs)) 3207 return mlir::failure(); 3208 if (auto v = val.dyn_cast<mlir::StringAttr>()) 3209 result.attributes.push_back( 3210 builder.getNamedAttr(fir::StringLitOp::value(), v)); 3211 else if (auto v = val.dyn_cast<mlir::DenseElementsAttr>()) 3212 result.attributes.push_back( 3213 builder.getNamedAttr(fir::StringLitOp::xlist(), v)); 3214 else if (auto v = val.dyn_cast<mlir::ArrayAttr>()) 3215 result.attributes.push_back( 3216 builder.getNamedAttr(fir::StringLitOp::xlist(), v)); 3217 else 3218 return parser.emitError(parser.getCurrentLocation(), 3219 "found an invalid constant"); 3220 mlir::IntegerAttr sz; 3221 mlir::Type type; 3222 if (parser.parseLParen() || 3223 parser.parseAttribute(sz, fir::StringLitOp::size(), result.attributes) || 3224 parser.parseRParen() || parser.getCurrentLocation(&trailingTypeLoc) || 3225 parser.parseColonType(type)) 3226 return mlir::failure(); 3227 auto charTy = type.dyn_cast<fir::CharacterType>(); 3228 if (!charTy) 3229 return parser.emitError(trailingTypeLoc, "must have character type"); 3230 type = fir::CharacterType::get(builder.getContext(), charTy.getFKind(), 3231 sz.getInt()); 3232 if (!type || parser.addTypesToList(type, result.types)) 3233 return mlir::failure(); 3234 return mlir::success(); 3235 } 3236 3237 void fir::StringLitOp::print(mlir::OpAsmPrinter &p) { 3238 p << ' ' << getValue() << '('; 3239 p << getSize().cast<mlir::IntegerAttr>().getValue() << ") : "; 3240 p.printType(getType()); 3241 } 3242 3243 mlir::LogicalResult fir::StringLitOp::verify() { 3244 if (getSize().cast<mlir::IntegerAttr>().getValue().isNegative()) 3245 return emitOpError("size must be non-negative"); 3246 if (auto xl = getOperation()->getAttr(fir::StringLitOp::xlist())) { 3247 if (auto xList = xl.dyn_cast<mlir::ArrayAttr>()) { 3248 for (auto a : xList) 3249 if (!a.isa<mlir::IntegerAttr>()) 3250 return emitOpError("values in initializer must be integers"); 3251 } else if (xl.isa<mlir::DenseElementsAttr>()) { 3252 // do nothing 3253 } else { 3254 return emitOpError("has unexpected attribute"); 3255 } 3256 } 3257 return mlir::success(); 3258 } 3259 3260 //===----------------------------------------------------------------------===// 3261 // UnboxProcOp 3262 //===----------------------------------------------------------------------===// 3263 3264 mlir::LogicalResult fir::UnboxProcOp::verify() { 3265 if (auto eleTy = fir::dyn_cast_ptrEleTy(getRefTuple().getType())) 3266 if (eleTy.isa<mlir::TupleType>()) 3267 return mlir::success(); 3268 return emitOpError("second output argument has bad type"); 3269 } 3270 3271 //===----------------------------------------------------------------------===// 3272 // IfOp 3273 //===----------------------------------------------------------------------===// 3274 3275 void fir::IfOp::build(mlir::OpBuilder &builder, mlir::OperationState &result, 3276 mlir::Value cond, bool withElseRegion) { 3277 build(builder, result, llvm::None, cond, withElseRegion); 3278 } 3279 3280 void fir::IfOp::build(mlir::OpBuilder &builder, mlir::OperationState &result, 3281 mlir::TypeRange resultTypes, mlir::Value cond, 3282 bool withElseRegion) { 3283 result.addOperands(cond); 3284 result.addTypes(resultTypes); 3285 3286 mlir::Region *thenRegion = result.addRegion(); 3287 thenRegion->push_back(new mlir::Block()); 3288 if (resultTypes.empty()) 3289 IfOp::ensureTerminator(*thenRegion, builder, result.location); 3290 3291 mlir::Region *elseRegion = result.addRegion(); 3292 if (withElseRegion) { 3293 elseRegion->push_back(new mlir::Block()); 3294 if (resultTypes.empty()) 3295 IfOp::ensureTerminator(*elseRegion, builder, result.location); 3296 } 3297 } 3298 3299 mlir::ParseResult fir::IfOp::parse(mlir::OpAsmParser &parser, 3300 mlir::OperationState &result) { 3301 result.regions.reserve(2); 3302 mlir::Region *thenRegion = result.addRegion(); 3303 mlir::Region *elseRegion = result.addRegion(); 3304 3305 auto &builder = parser.getBuilder(); 3306 mlir::OpAsmParser::UnresolvedOperand cond; 3307 mlir::Type i1Type = builder.getIntegerType(1); 3308 if (parser.parseOperand(cond) || 3309 parser.resolveOperand(cond, i1Type, result.operands)) 3310 return mlir::failure(); 3311 3312 if (parser.parseOptionalArrowTypeList(result.types)) 3313 return mlir::failure(); 3314 3315 if (parser.parseRegion(*thenRegion, {}, {})) 3316 return mlir::failure(); 3317 fir::IfOp::ensureTerminator(*thenRegion, parser.getBuilder(), 3318 result.location); 3319 3320 if (mlir::succeeded(parser.parseOptionalKeyword("else"))) { 3321 if (parser.parseRegion(*elseRegion, {}, {})) 3322 return mlir::failure(); 3323 fir::IfOp::ensureTerminator(*elseRegion, parser.getBuilder(), 3324 result.location); 3325 } 3326 3327 // Parse the optional attribute list. 3328 if (parser.parseOptionalAttrDict(result.attributes)) 3329 return mlir::failure(); 3330 return mlir::success(); 3331 } 3332 3333 mlir::LogicalResult fir::IfOp::verify() { 3334 if (getNumResults() != 0 && getElseRegion().empty()) 3335 return emitOpError("must have an else block if defining values"); 3336 3337 return mlir::success(); 3338 } 3339 3340 void fir::IfOp::print(mlir::OpAsmPrinter &p) { 3341 bool printBlockTerminators = false; 3342 p << ' ' << getCondition(); 3343 if (!getResults().empty()) { 3344 p << " -> (" << getResultTypes() << ')'; 3345 printBlockTerminators = true; 3346 } 3347 p << ' '; 3348 p.printRegion(getThenRegion(), /*printEntryBlockArgs=*/false, 3349 printBlockTerminators); 3350 3351 // Print the 'else' regions if it exists and has a block. 3352 auto &otherReg = getElseRegion(); 3353 if (!otherReg.empty()) { 3354 p << " else "; 3355 p.printRegion(otherReg, /*printEntryBlockArgs=*/false, 3356 printBlockTerminators); 3357 } 3358 p.printOptionalAttrDict((*this)->getAttrs()); 3359 } 3360 3361 void fir::IfOp::resultToSourceOps(llvm::SmallVectorImpl<mlir::Value> &results, 3362 unsigned resultNum) { 3363 auto *term = getThenRegion().front().getTerminator(); 3364 if (resultNum < term->getNumOperands()) 3365 results.push_back(term->getOperand(resultNum)); 3366 term = getElseRegion().front().getTerminator(); 3367 if (resultNum < term->getNumOperands()) 3368 results.push_back(term->getOperand(resultNum)); 3369 } 3370 3371 //===----------------------------------------------------------------------===// 3372 3373 mlir::ParseResult fir::isValidCaseAttr(mlir::Attribute attr) { 3374 if (attr.isa<mlir::UnitAttr, fir::ClosedIntervalAttr, fir::PointIntervalAttr, 3375 fir::LowerBoundAttr, fir::UpperBoundAttr>()) 3376 return mlir::success(); 3377 return mlir::failure(); 3378 } 3379 3380 unsigned fir::getCaseArgumentOffset(llvm::ArrayRef<mlir::Attribute> cases, 3381 unsigned dest) { 3382 unsigned o = 0; 3383 for (unsigned i = 0; i < dest; ++i) { 3384 auto &attr = cases[i]; 3385 if (!attr.dyn_cast_or_null<mlir::UnitAttr>()) { 3386 ++o; 3387 if (attr.dyn_cast_or_null<fir::ClosedIntervalAttr>()) 3388 ++o; 3389 } 3390 } 3391 return o; 3392 } 3393 3394 mlir::ParseResult 3395 fir::parseSelector(mlir::OpAsmParser &parser, mlir::OperationState &result, 3396 mlir::OpAsmParser::UnresolvedOperand &selector, 3397 mlir::Type &type) { 3398 if (parser.parseOperand(selector) || parser.parseColonType(type) || 3399 parser.resolveOperand(selector, type, result.operands) || 3400 parser.parseLSquare()) 3401 return mlir::failure(); 3402 return mlir::success(); 3403 } 3404 3405 mlir::func::FuncOp 3406 fir::createFuncOp(mlir::Location loc, mlir::ModuleOp module, 3407 llvm::StringRef name, mlir::FunctionType type, 3408 llvm::ArrayRef<mlir::NamedAttribute> attrs) { 3409 if (auto f = module.lookupSymbol<mlir::func::FuncOp>(name)) 3410 return f; 3411 mlir::OpBuilder modBuilder(module.getBodyRegion()); 3412 modBuilder.setInsertionPointToEnd(module.getBody()); 3413 auto result = modBuilder.create<mlir::func::FuncOp>(loc, name, type, attrs); 3414 result.setVisibility(mlir::SymbolTable::Visibility::Private); 3415 return result; 3416 } 3417 3418 fir::GlobalOp fir::createGlobalOp(mlir::Location loc, mlir::ModuleOp module, 3419 llvm::StringRef name, mlir::Type type, 3420 llvm::ArrayRef<mlir::NamedAttribute> attrs) { 3421 if (auto g = module.lookupSymbol<fir::GlobalOp>(name)) 3422 return g; 3423 mlir::OpBuilder modBuilder(module.getBodyRegion()); 3424 auto result = modBuilder.create<fir::GlobalOp>(loc, name, type, attrs); 3425 result.setVisibility(mlir::SymbolTable::Visibility::Private); 3426 return result; 3427 } 3428 3429 bool fir::hasHostAssociationArgument(mlir::func::FuncOp func) { 3430 if (auto allArgAttrs = func.getAllArgAttrs()) 3431 for (auto attr : allArgAttrs) 3432 if (auto dict = attr.template dyn_cast_or_null<mlir::DictionaryAttr>()) 3433 if (dict.get(fir::getHostAssocAttrName())) 3434 return true; 3435 return false; 3436 } 3437 3438 bool fir::valueHasFirAttribute(mlir::Value value, 3439 llvm::StringRef attributeName) { 3440 // If this is a fir.box that was loaded, the fir attributes will be on the 3441 // related fir.ref<fir.box> creation. 3442 if (value.getType().isa<fir::BoxType>()) 3443 if (auto definingOp = value.getDefiningOp()) 3444 if (auto loadOp = mlir::dyn_cast<fir::LoadOp>(definingOp)) 3445 value = loadOp.getMemref(); 3446 // If this is a function argument, look in the argument attributes. 3447 if (auto blockArg = value.dyn_cast<mlir::BlockArgument>()) { 3448 if (blockArg.getOwner() && blockArg.getOwner()->isEntryBlock()) 3449 if (auto funcOp = mlir::dyn_cast<mlir::func::FuncOp>( 3450 blockArg.getOwner()->getParentOp())) 3451 if (funcOp.getArgAttr(blockArg.getArgNumber(), attributeName)) 3452 return true; 3453 return false; 3454 } 3455 3456 if (auto definingOp = value.getDefiningOp()) { 3457 // If this is an allocated value, look at the allocation attributes. 3458 if (mlir::isa<fir::AllocMemOp>(definingOp) || 3459 mlir::isa<AllocaOp>(definingOp)) 3460 return definingOp->hasAttr(attributeName); 3461 // If this is an imported global, look at AddrOfOp and GlobalOp attributes. 3462 // Both operations are looked at because use/host associated variable (the 3463 // AddrOfOp) can have ASYNCHRONOUS/VOLATILE attributes even if the ultimate 3464 // entity (the globalOp) does not have them. 3465 if (auto addressOfOp = mlir::dyn_cast<fir::AddrOfOp>(definingOp)) { 3466 if (addressOfOp->hasAttr(attributeName)) 3467 return true; 3468 if (auto module = definingOp->getParentOfType<mlir::ModuleOp>()) 3469 if (auto globalOp = 3470 module.lookupSymbol<fir::GlobalOp>(addressOfOp.getSymbol())) 3471 return globalOp->hasAttr(attributeName); 3472 } 3473 } 3474 // TODO: Construct associated entities attributes. Decide where the fir 3475 // attributes must be placed/looked for in this case. 3476 return false; 3477 } 3478 3479 bool fir::anyFuncArgsHaveAttr(mlir::func::FuncOp func, llvm::StringRef attr) { 3480 for (unsigned i = 0, end = func.getNumArguments(); i < end; ++i) 3481 if (func.getArgAttr(i, attr)) 3482 return true; 3483 return false; 3484 } 3485 3486 mlir::Type fir::applyPathToType(mlir::Type eleTy, mlir::ValueRange path) { 3487 for (auto i = path.begin(), end = path.end(); eleTy && i < end;) { 3488 eleTy = llvm::TypeSwitch<mlir::Type, mlir::Type>(eleTy) 3489 .Case<fir::RecordType>([&](fir::RecordType ty) { 3490 if (auto *op = (*i++).getDefiningOp()) { 3491 if (auto off = mlir::dyn_cast<fir::FieldIndexOp>(op)) 3492 return ty.getType(off.getFieldName()); 3493 if (auto off = mlir::dyn_cast<mlir::arith::ConstantOp>(op)) 3494 return ty.getType(fir::toInt(off)); 3495 } 3496 return mlir::Type{}; 3497 }) 3498 .Case<fir::SequenceType>([&](fir::SequenceType ty) { 3499 bool valid = true; 3500 const auto rank = ty.getDimension(); 3501 for (std::remove_const_t<decltype(rank)> ii = 0; 3502 valid && ii < rank; ++ii) 3503 valid = i < end && fir::isa_integer((*i++).getType()); 3504 return valid ? ty.getEleTy() : mlir::Type{}; 3505 }) 3506 .Case<mlir::TupleType>([&](mlir::TupleType ty) { 3507 if (auto *op = (*i++).getDefiningOp()) 3508 if (auto off = mlir::dyn_cast<mlir::arith::ConstantOp>(op)) 3509 return ty.getType(fir::toInt(off)); 3510 return mlir::Type{}; 3511 }) 3512 .Case<fir::ComplexType>([&](fir::ComplexType ty) { 3513 auto x = *i; 3514 if (auto *op = (*i++).getDefiningOp()) 3515 if (fir::isa_integer(x.getType())) 3516 return ty.getEleType(fir::getKindMapping( 3517 op->getParentOfType<mlir::ModuleOp>())); 3518 return mlir::Type{}; 3519 }) 3520 .Case<mlir::ComplexType>([&](mlir::ComplexType ty) { 3521 if (fir::isa_integer((*i++).getType())) 3522 return ty.getElementType(); 3523 return mlir::Type{}; 3524 }) 3525 .Default([&](const auto &) { return mlir::Type{}; }); 3526 } 3527 return eleTy; 3528 } 3529 3530 // Tablegen operators 3531 3532 #define GET_OP_CLASSES 3533 #include "flang/Optimizer/Dialect/FIROps.cpp.inc" 3534