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