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