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