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