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