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