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.printRegion(body, /*printEntryBlockArgs=*/false, 999 /*printBlockTerminators=*/false); 1000 } 1001 1002 static mlir::LogicalResult verify(fir::DispatchTableOp &op) { 1003 for (auto &op : op.getBlock()) 1004 if (!(isa<fir::DTEntryOp>(op) || isa<fir::FirEndOp>(op))) 1005 return op.emitOpError("dispatch table must contain dt_entry"); 1006 return mlir::success(); 1007 } 1008 1009 //===----------------------------------------------------------------------===// 1010 // EmboxOp 1011 //===----------------------------------------------------------------------===// 1012 1013 static mlir::LogicalResult verify(fir::EmboxOp op) { 1014 auto eleTy = fir::dyn_cast_ptrEleTy(op.memref().getType()); 1015 bool isArray = false; 1016 if (auto seqTy = eleTy.dyn_cast<fir::SequenceType>()) { 1017 eleTy = seqTy.getEleTy(); 1018 isArray = true; 1019 } 1020 if (op.hasLenParams()) { 1021 auto lenPs = op.numLenParams(); 1022 if (auto rt = eleTy.dyn_cast<fir::RecordType>()) { 1023 if (lenPs != rt.getNumLenParams()) 1024 return op.emitOpError("number of LEN params does not correspond" 1025 " to the !fir.type type"); 1026 } else if (auto strTy = eleTy.dyn_cast<fir::CharacterType>()) { 1027 if (strTy.getLen() != fir::CharacterType::unknownLen()) 1028 return op.emitOpError("CHARACTER already has static LEN"); 1029 } else { 1030 return op.emitOpError("LEN parameters require CHARACTER or derived type"); 1031 } 1032 for (auto lp : op.typeparams()) 1033 if (!fir::isa_integer(lp.getType())) 1034 return op.emitOpError("LEN parameters must be integral type"); 1035 } 1036 if (op.getShape() && !isArray) 1037 return op.emitOpError("shape must not be provided for a scalar"); 1038 if (op.getSlice() && !isArray) 1039 return op.emitOpError("slice must not be provided for a scalar"); 1040 return mlir::success(); 1041 } 1042 1043 //===----------------------------------------------------------------------===// 1044 // EmboxCharOp 1045 //===----------------------------------------------------------------------===// 1046 1047 static mlir::LogicalResult verify(fir::EmboxCharOp &op) { 1048 auto eleTy = fir::dyn_cast_ptrEleTy(op.memref().getType()); 1049 if (!eleTy.dyn_cast_or_null<CharacterType>()) 1050 return mlir::failure(); 1051 return mlir::success(); 1052 } 1053 1054 //===----------------------------------------------------------------------===// 1055 // EmboxProcOp 1056 //===----------------------------------------------------------------------===// 1057 1058 static mlir::ParseResult parseEmboxProcOp(mlir::OpAsmParser &parser, 1059 mlir::OperationState &result) { 1060 mlir::SymbolRefAttr procRef; 1061 if (parser.parseAttribute(procRef, "funcname", result.attributes)) 1062 return mlir::failure(); 1063 bool hasTuple = false; 1064 mlir::OpAsmParser::OperandType tupleRef; 1065 if (!parser.parseOptionalComma()) { 1066 if (parser.parseOperand(tupleRef)) 1067 return mlir::failure(); 1068 hasTuple = true; 1069 } 1070 mlir::FunctionType type; 1071 if (parser.parseColon() || parser.parseLParen() || parser.parseType(type)) 1072 return mlir::failure(); 1073 result.addAttribute("functype", mlir::TypeAttr::get(type)); 1074 if (hasTuple) { 1075 mlir::Type tupleType; 1076 if (parser.parseComma() || parser.parseType(tupleType) || 1077 parser.resolveOperand(tupleRef, tupleType, result.operands)) 1078 return mlir::failure(); 1079 } 1080 mlir::Type boxType; 1081 if (parser.parseRParen() || parser.parseArrow() || 1082 parser.parseType(boxType) || parser.addTypesToList(boxType, result.types)) 1083 return mlir::failure(); 1084 return mlir::success(); 1085 } 1086 1087 static void print(mlir::OpAsmPrinter &p, fir::EmboxProcOp &op) { 1088 p << ' ' << op.getOperation()->getAttr("funcname"); 1089 auto h = op.host(); 1090 if (h) { 1091 p << ", "; 1092 p.printOperand(h); 1093 } 1094 p << " : (" << op.getOperation()->getAttr("functype"); 1095 if (h) 1096 p << ", " << h.getType(); 1097 p << ") -> " << op.getType(); 1098 } 1099 1100 static mlir::LogicalResult verify(fir::EmboxProcOp &op) { 1101 // host bindings (optional) must be a reference to a tuple 1102 if (auto h = op.host()) { 1103 if (auto r = h.getType().dyn_cast<ReferenceType>()) { 1104 if (!r.getEleTy().dyn_cast<mlir::TupleType>()) 1105 return mlir::failure(); 1106 } else { 1107 return mlir::failure(); 1108 } 1109 } 1110 return mlir::success(); 1111 } 1112 1113 //===----------------------------------------------------------------------===// 1114 // GenTypeDescOp 1115 //===----------------------------------------------------------------------===// 1116 1117 void fir::GenTypeDescOp::build(OpBuilder &, OperationState &result, 1118 mlir::TypeAttr inty) { 1119 result.addAttribute("in_type", inty); 1120 result.addTypes(TypeDescType::get(inty.getValue())); 1121 } 1122 1123 static mlir::ParseResult parseGenTypeDescOp(mlir::OpAsmParser &parser, 1124 mlir::OperationState &result) { 1125 mlir::Type intype; 1126 if (parser.parseType(intype)) 1127 return mlir::failure(); 1128 result.addAttribute("in_type", mlir::TypeAttr::get(intype)); 1129 mlir::Type restype = TypeDescType::get(intype); 1130 if (parser.addTypeToList(restype, result.types)) 1131 return mlir::failure(); 1132 return mlir::success(); 1133 } 1134 1135 static void print(mlir::OpAsmPrinter &p, fir::GenTypeDescOp &op) { 1136 p << ' ' << op.getOperation()->getAttr("in_type"); 1137 p.printOptionalAttrDict(op.getOperation()->getAttrs(), {"in_type"}); 1138 } 1139 1140 static mlir::LogicalResult verify(fir::GenTypeDescOp &op) { 1141 mlir::Type resultTy = op.getType(); 1142 if (auto tdesc = resultTy.dyn_cast<TypeDescType>()) { 1143 if (tdesc.getOfTy() != op.getInType()) 1144 return op.emitOpError("wrapped type mismatched"); 1145 } else { 1146 return op.emitOpError("must be !fir.tdesc type"); 1147 } 1148 return mlir::success(); 1149 } 1150 1151 //===----------------------------------------------------------------------===// 1152 // GlobalOp 1153 //===----------------------------------------------------------------------===// 1154 1155 mlir::Type fir::GlobalOp::resultType() { 1156 return wrapAllocaResultType(getType()); 1157 } 1158 1159 static ParseResult parseGlobalOp(OpAsmParser &parser, OperationState &result) { 1160 // Parse the optional linkage 1161 llvm::StringRef linkage; 1162 auto &builder = parser.getBuilder(); 1163 if (mlir::succeeded(parser.parseOptionalKeyword(&linkage))) { 1164 if (fir::GlobalOp::verifyValidLinkage(linkage)) 1165 return mlir::failure(); 1166 mlir::StringAttr linkAttr = builder.getStringAttr(linkage); 1167 result.addAttribute(fir::GlobalOp::linkageAttrName(), linkAttr); 1168 } 1169 1170 // Parse the name as a symbol reference attribute. 1171 mlir::SymbolRefAttr nameAttr; 1172 if (parser.parseAttribute(nameAttr, fir::GlobalOp::symbolAttrNameStr(), 1173 result.attributes)) 1174 return mlir::failure(); 1175 result.addAttribute(mlir::SymbolTable::getSymbolAttrName(), 1176 nameAttr.getRootReference()); 1177 1178 bool simpleInitializer = false; 1179 if (mlir::succeeded(parser.parseOptionalLParen())) { 1180 Attribute attr; 1181 if (parser.parseAttribute(attr, "initVal", result.attributes) || 1182 parser.parseRParen()) 1183 return mlir::failure(); 1184 simpleInitializer = true; 1185 } 1186 1187 if (succeeded(parser.parseOptionalKeyword("constant"))) { 1188 // if "constant" keyword then mark this as a constant, not a variable 1189 result.addAttribute("constant", builder.getUnitAttr()); 1190 } 1191 1192 mlir::Type globalType; 1193 if (parser.parseColonType(globalType)) 1194 return mlir::failure(); 1195 1196 result.addAttribute(fir::GlobalOp::typeAttrName(result.name), 1197 mlir::TypeAttr::get(globalType)); 1198 1199 if (simpleInitializer) { 1200 result.addRegion(); 1201 } else { 1202 // Parse the optional initializer body. 1203 auto parseResult = parser.parseOptionalRegion( 1204 *result.addRegion(), /*arguments=*/llvm::None, /*argTypes=*/llvm::None); 1205 if (parseResult.hasValue() && mlir::failed(*parseResult)) 1206 return mlir::failure(); 1207 } 1208 1209 return mlir::success(); 1210 } 1211 1212 static void print(mlir::OpAsmPrinter &p, fir::GlobalOp &op) { 1213 if (op.linkName().hasValue()) 1214 p << ' ' << op.linkName().getValue(); 1215 p << ' '; 1216 p.printAttributeWithoutType(op.getSymrefAttr()); 1217 if (auto val = op.getValueOrNull()) 1218 p << '(' << val << ')'; 1219 if (op.getOperation()->getAttr(fir::GlobalOp::getConstantAttrNameStr())) 1220 p << " constant"; 1221 p << " : "; 1222 p.printType(op.getType()); 1223 if (op.hasInitializationBody()) 1224 p.printRegion(op.getOperation()->getRegion(0), 1225 /*printEntryBlockArgs=*/false, 1226 /*printBlockTerminators=*/true); 1227 } 1228 1229 void fir::GlobalOp::appendInitialValue(mlir::Operation *op) { 1230 getBlock().getOperations().push_back(op); 1231 } 1232 1233 void fir::GlobalOp::build(mlir::OpBuilder &builder, OperationState &result, 1234 StringRef name, bool isConstant, Type type, 1235 Attribute initialVal, StringAttr linkage, 1236 ArrayRef<NamedAttribute> attrs) { 1237 result.addRegion(); 1238 result.addAttribute(typeAttrName(result.name), mlir::TypeAttr::get(type)); 1239 result.addAttribute(mlir::SymbolTable::getSymbolAttrName(), 1240 builder.getStringAttr(name)); 1241 result.addAttribute(symbolAttrNameStr(), 1242 SymbolRefAttr::get(builder.getContext(), name)); 1243 if (isConstant) 1244 result.addAttribute(constantAttrName(result.name), builder.getUnitAttr()); 1245 if (initialVal) 1246 result.addAttribute(initValAttrName(result.name), initialVal); 1247 if (linkage) 1248 result.addAttribute(linkageAttrName(), linkage); 1249 result.attributes.append(attrs.begin(), attrs.end()); 1250 } 1251 1252 void fir::GlobalOp::build(mlir::OpBuilder &builder, OperationState &result, 1253 StringRef name, Type type, Attribute initialVal, 1254 StringAttr linkage, ArrayRef<NamedAttribute> attrs) { 1255 build(builder, result, name, /*isConstant=*/false, type, {}, linkage, attrs); 1256 } 1257 1258 void fir::GlobalOp::build(mlir::OpBuilder &builder, OperationState &result, 1259 StringRef name, bool isConstant, Type type, 1260 StringAttr linkage, ArrayRef<NamedAttribute> attrs) { 1261 build(builder, result, name, isConstant, type, {}, linkage, attrs); 1262 } 1263 1264 void fir::GlobalOp::build(mlir::OpBuilder &builder, OperationState &result, 1265 StringRef name, Type type, StringAttr linkage, 1266 ArrayRef<NamedAttribute> attrs) { 1267 build(builder, result, name, /*isConstant=*/false, type, {}, linkage, attrs); 1268 } 1269 1270 void fir::GlobalOp::build(mlir::OpBuilder &builder, OperationState &result, 1271 StringRef name, bool isConstant, Type type, 1272 ArrayRef<NamedAttribute> attrs) { 1273 build(builder, result, name, isConstant, type, StringAttr{}, attrs); 1274 } 1275 1276 void fir::GlobalOp::build(mlir::OpBuilder &builder, OperationState &result, 1277 StringRef name, Type type, 1278 ArrayRef<NamedAttribute> attrs) { 1279 build(builder, result, name, /*isConstant=*/false, type, attrs); 1280 } 1281 1282 mlir::ParseResult fir::GlobalOp::verifyValidLinkage(StringRef linkage) { 1283 // Supporting only a subset of the LLVM linkage types for now 1284 static const char *validNames[] = {"common", "internal", "linkonce", "weak"}; 1285 return mlir::success(llvm::is_contained(validNames, linkage)); 1286 } 1287 1288 //===----------------------------------------------------------------------===// 1289 // GlobalLenOp 1290 //===----------------------------------------------------------------------===// 1291 1292 static mlir::ParseResult parseGlobalLenOp(mlir::OpAsmParser &parser, 1293 mlir::OperationState &result) { 1294 llvm::StringRef fieldName; 1295 if (failed(parser.parseOptionalKeyword(&fieldName))) { 1296 mlir::StringAttr fieldAttr; 1297 if (parser.parseAttribute(fieldAttr, fir::GlobalLenOp::lenParamAttrName(), 1298 result.attributes)) 1299 return mlir::failure(); 1300 } else { 1301 result.addAttribute(fir::GlobalLenOp::lenParamAttrName(), 1302 parser.getBuilder().getStringAttr(fieldName)); 1303 } 1304 mlir::IntegerAttr constant; 1305 if (parser.parseComma() || 1306 parser.parseAttribute(constant, fir::GlobalLenOp::intAttrName(), 1307 result.attributes)) 1308 return mlir::failure(); 1309 return mlir::success(); 1310 } 1311 1312 static void print(mlir::OpAsmPrinter &p, fir::GlobalLenOp &op) { 1313 p << ' ' << op.getOperation()->getAttr(fir::GlobalLenOp::lenParamAttrName()) 1314 << ", " << op.getOperation()->getAttr(fir::GlobalLenOp::intAttrName()); 1315 } 1316 1317 //===----------------------------------------------------------------------===// 1318 // FieldIndexOp 1319 //===----------------------------------------------------------------------===// 1320 1321 static mlir::ParseResult parseFieldIndexOp(mlir::OpAsmParser &parser, 1322 mlir::OperationState &result) { 1323 llvm::StringRef fieldName; 1324 auto &builder = parser.getBuilder(); 1325 mlir::Type recty; 1326 if (parser.parseOptionalKeyword(&fieldName) || parser.parseComma() || 1327 parser.parseType(recty)) 1328 return mlir::failure(); 1329 result.addAttribute(fir::FieldIndexOp::fieldAttrName(), 1330 builder.getStringAttr(fieldName)); 1331 if (!recty.dyn_cast<RecordType>()) 1332 return mlir::failure(); 1333 result.addAttribute(fir::FieldIndexOp::typeAttrName(), 1334 mlir::TypeAttr::get(recty)); 1335 if (!parser.parseOptionalLParen()) { 1336 llvm::SmallVector<mlir::OpAsmParser::OperandType> operands; 1337 llvm::SmallVector<mlir::Type> types; 1338 auto loc = parser.getNameLoc(); 1339 if (parser.parseOperandList(operands, mlir::OpAsmParser::Delimiter::None) || 1340 parser.parseColonTypeList(types) || parser.parseRParen() || 1341 parser.resolveOperands(operands, types, loc, result.operands)) 1342 return mlir::failure(); 1343 } 1344 mlir::Type fieldType = fir::FieldType::get(builder.getContext()); 1345 if (parser.addTypeToList(fieldType, result.types)) 1346 return mlir::failure(); 1347 return mlir::success(); 1348 } 1349 1350 static void print(mlir::OpAsmPrinter &p, fir::FieldIndexOp &op) { 1351 p << ' ' 1352 << op.getOperation() 1353 ->getAttrOfType<mlir::StringAttr>(fir::FieldIndexOp::fieldAttrName()) 1354 .getValue() 1355 << ", " << op.getOperation()->getAttr(fir::FieldIndexOp::typeAttrName()); 1356 if (op.getNumOperands()) { 1357 p << '('; 1358 p.printOperands(op.typeparams()); 1359 const auto *sep = ") : "; 1360 for (auto op : op.typeparams()) { 1361 p << sep; 1362 if (op) 1363 p.printType(op.getType()); 1364 else 1365 p << "()"; 1366 sep = ", "; 1367 } 1368 } 1369 } 1370 1371 void fir::FieldIndexOp::build(mlir::OpBuilder &builder, 1372 mlir::OperationState &result, 1373 llvm::StringRef fieldName, mlir::Type recTy, 1374 mlir::ValueRange operands) { 1375 result.addAttribute(fieldAttrName(), builder.getStringAttr(fieldName)); 1376 result.addAttribute(typeAttrName(), TypeAttr::get(recTy)); 1377 result.addOperands(operands); 1378 } 1379 1380 //===----------------------------------------------------------------------===// 1381 // InsertOnRangeOp 1382 //===----------------------------------------------------------------------===// 1383 1384 static ParseResult 1385 parseCustomRangeSubscript(mlir::OpAsmParser &parser, 1386 mlir::DenseIntElementsAttr &coord) { 1387 llvm::SmallVector<int64_t> lbounds; 1388 llvm::SmallVector<int64_t> ubounds; 1389 if (parser.parseKeyword("from") || 1390 parser.parseCommaSeparatedList( 1391 AsmParser::Delimiter::Paren, 1392 [&] { return parser.parseInteger(lbounds.emplace_back(0)); }) || 1393 parser.parseKeyword("to") || 1394 parser.parseCommaSeparatedList(AsmParser::Delimiter::Paren, [&] { 1395 return parser.parseInteger(ubounds.emplace_back(0)); 1396 })) 1397 return failure(); 1398 llvm::SmallVector<int64_t> zippedBounds; 1399 for (auto zip : llvm::zip(lbounds, ubounds)) { 1400 zippedBounds.push_back(std::get<0>(zip)); 1401 zippedBounds.push_back(std::get<1>(zip)); 1402 } 1403 coord = mlir::Builder(parser.getContext()).getIndexTensorAttr(zippedBounds); 1404 return success(); 1405 } 1406 1407 void printCustomRangeSubscript(mlir::OpAsmPrinter &printer, InsertOnRangeOp op, 1408 mlir::DenseIntElementsAttr coord) { 1409 printer << "from ("; 1410 auto enumerate = llvm::enumerate(coord.getValues<int64_t>()); 1411 // Even entries are the lower bounds. 1412 llvm::interleaveComma( 1413 make_filter_range( 1414 enumerate, 1415 [](auto indexed_value) { return indexed_value.index() % 2 == 0; }), 1416 printer, [&](auto indexed_value) { printer << indexed_value.value(); }); 1417 printer << ") to ("; 1418 // Odd entries are the upper bounds. 1419 llvm::interleaveComma( 1420 make_filter_range( 1421 enumerate, 1422 [](auto indexed_value) { return indexed_value.index() % 2 != 0; }), 1423 printer, [&](auto indexed_value) { printer << indexed_value.value(); }); 1424 printer << ")"; 1425 } 1426 1427 /// Range bounds must be nonnegative, and the range must not be empty. 1428 static mlir::LogicalResult verify(fir::InsertOnRangeOp op) { 1429 if (fir::hasDynamicSize(op.seq().getType())) 1430 return op.emitOpError("must have constant shape and size"); 1431 mlir::DenseIntElementsAttr coor = op.coor(); 1432 if (coor.size() < 2 || coor.size() % 2 != 0) 1433 return op.emitOpError("has uneven number of values in ranges"); 1434 bool rangeIsKnownToBeNonempty = false; 1435 for (auto i = coor.getValues<int64_t>().end(), 1436 b = coor.getValues<int64_t>().begin(); 1437 i != b;) { 1438 int64_t ub = (*--i); 1439 int64_t lb = (*--i); 1440 if (lb < 0 || ub < 0) 1441 return op.emitOpError("negative range bound"); 1442 if (rangeIsKnownToBeNonempty) 1443 continue; 1444 if (lb > ub) 1445 return op.emitOpError("empty range"); 1446 rangeIsKnownToBeNonempty = lb < ub; 1447 } 1448 return mlir::success(); 1449 } 1450 1451 //===----------------------------------------------------------------------===// 1452 // InsertValueOp 1453 //===----------------------------------------------------------------------===// 1454 1455 static bool checkIsIntegerConstant(mlir::Attribute attr, int64_t conVal) { 1456 if (auto iattr = attr.dyn_cast<mlir::IntegerAttr>()) 1457 return iattr.getInt() == conVal; 1458 return false; 1459 } 1460 static bool isZero(mlir::Attribute a) { return checkIsIntegerConstant(a, 0); } 1461 static bool isOne(mlir::Attribute a) { return checkIsIntegerConstant(a, 1); } 1462 1463 // Undo some complex patterns created in the front-end and turn them back into 1464 // complex ops. 1465 template <typename FltOp, typename CpxOp> 1466 struct UndoComplexPattern : public mlir::RewritePattern { 1467 UndoComplexPattern(mlir::MLIRContext *ctx) 1468 : mlir::RewritePattern("fir.insert_value", 2, ctx) {} 1469 1470 mlir::LogicalResult 1471 matchAndRewrite(mlir::Operation *op, 1472 mlir::PatternRewriter &rewriter) const override { 1473 auto insval = dyn_cast_or_null<fir::InsertValueOp>(op); 1474 if (!insval || !insval.getType().isa<fir::ComplexType>()) 1475 return mlir::failure(); 1476 auto insval2 = 1477 dyn_cast_or_null<fir::InsertValueOp>(insval.adt().getDefiningOp()); 1478 if (!insval2 || !isa<fir::UndefOp>(insval2.adt().getDefiningOp())) 1479 return mlir::failure(); 1480 auto binf = dyn_cast_or_null<FltOp>(insval.val().getDefiningOp()); 1481 auto binf2 = dyn_cast_or_null<FltOp>(insval2.val().getDefiningOp()); 1482 if (!binf || !binf2 || insval.coor().size() != 1 || 1483 !isOne(insval.coor()[0]) || insval2.coor().size() != 1 || 1484 !isZero(insval2.coor()[0])) 1485 return mlir::failure(); 1486 auto eai = 1487 dyn_cast_or_null<fir::ExtractValueOp>(binf.getLhs().getDefiningOp()); 1488 auto ebi = 1489 dyn_cast_or_null<fir::ExtractValueOp>(binf.getRhs().getDefiningOp()); 1490 auto ear = 1491 dyn_cast_or_null<fir::ExtractValueOp>(binf2.getLhs().getDefiningOp()); 1492 auto ebr = 1493 dyn_cast_or_null<fir::ExtractValueOp>(binf2.getRhs().getDefiningOp()); 1494 if (!eai || !ebi || !ear || !ebr || ear.adt() != eai.adt() || 1495 ebr.adt() != ebi.adt() || eai.coor().size() != 1 || 1496 !isOne(eai.coor()[0]) || ebi.coor().size() != 1 || 1497 !isOne(ebi.coor()[0]) || ear.coor().size() != 1 || 1498 !isZero(ear.coor()[0]) || ebr.coor().size() != 1 || 1499 !isZero(ebr.coor()[0])) 1500 return mlir::failure(); 1501 rewriter.replaceOpWithNewOp<CpxOp>(op, ear.adt(), ebr.adt()); 1502 return mlir::success(); 1503 } 1504 }; 1505 1506 void fir::InsertValueOp::getCanonicalizationPatterns( 1507 mlir::OwningRewritePatternList &results, mlir::MLIRContext *context) { 1508 results.insert<UndoComplexPattern<mlir::arith::AddFOp, fir::AddcOp>, 1509 UndoComplexPattern<mlir::arith::SubFOp, fir::SubcOp>>(context); 1510 } 1511 1512 //===----------------------------------------------------------------------===// 1513 // IterWhileOp 1514 //===----------------------------------------------------------------------===// 1515 1516 void fir::IterWhileOp::build(mlir::OpBuilder &builder, 1517 mlir::OperationState &result, mlir::Value lb, 1518 mlir::Value ub, mlir::Value step, 1519 mlir::Value iterate, bool finalCountValue, 1520 mlir::ValueRange iterArgs, 1521 llvm::ArrayRef<mlir::NamedAttribute> attributes) { 1522 result.addOperands({lb, ub, step, iterate}); 1523 if (finalCountValue) { 1524 result.addTypes(builder.getIndexType()); 1525 result.addAttribute(getFinalValueAttrNameStr(), builder.getUnitAttr()); 1526 } 1527 result.addTypes(iterate.getType()); 1528 result.addOperands(iterArgs); 1529 for (auto v : iterArgs) 1530 result.addTypes(v.getType()); 1531 mlir::Region *bodyRegion = result.addRegion(); 1532 bodyRegion->push_back(new Block{}); 1533 bodyRegion->front().addArgument(builder.getIndexType()); 1534 bodyRegion->front().addArgument(iterate.getType()); 1535 bodyRegion->front().addArguments(iterArgs.getTypes()); 1536 result.addAttributes(attributes); 1537 } 1538 1539 static mlir::ParseResult parseIterWhileOp(mlir::OpAsmParser &parser, 1540 mlir::OperationState &result) { 1541 auto &builder = parser.getBuilder(); 1542 mlir::OpAsmParser::OperandType inductionVariable, lb, ub, step; 1543 if (parser.parseLParen() || parser.parseRegionArgument(inductionVariable) || 1544 parser.parseEqual()) 1545 return mlir::failure(); 1546 1547 // Parse loop bounds. 1548 auto indexType = builder.getIndexType(); 1549 auto i1Type = builder.getIntegerType(1); 1550 if (parser.parseOperand(lb) || 1551 parser.resolveOperand(lb, indexType, result.operands) || 1552 parser.parseKeyword("to") || parser.parseOperand(ub) || 1553 parser.resolveOperand(ub, indexType, result.operands) || 1554 parser.parseKeyword("step") || parser.parseOperand(step) || 1555 parser.parseRParen() || 1556 parser.resolveOperand(step, indexType, result.operands)) 1557 return mlir::failure(); 1558 1559 mlir::OpAsmParser::OperandType iterateVar, iterateInput; 1560 if (parser.parseKeyword("and") || parser.parseLParen() || 1561 parser.parseRegionArgument(iterateVar) || parser.parseEqual() || 1562 parser.parseOperand(iterateInput) || parser.parseRParen() || 1563 parser.resolveOperand(iterateInput, i1Type, result.operands)) 1564 return mlir::failure(); 1565 1566 // Parse the initial iteration arguments. 1567 llvm::SmallVector<mlir::OpAsmParser::OperandType> regionArgs; 1568 auto prependCount = false; 1569 1570 // Induction variable. 1571 regionArgs.push_back(inductionVariable); 1572 regionArgs.push_back(iterateVar); 1573 1574 if (succeeded(parser.parseOptionalKeyword("iter_args"))) { 1575 llvm::SmallVector<mlir::OpAsmParser::OperandType> operands; 1576 llvm::SmallVector<mlir::Type> regionTypes; 1577 // Parse assignment list and results type list. 1578 if (parser.parseAssignmentList(regionArgs, operands) || 1579 parser.parseArrowTypeList(regionTypes)) 1580 return failure(); 1581 if (regionTypes.size() == operands.size() + 2) 1582 prependCount = true; 1583 llvm::ArrayRef<mlir::Type> resTypes = regionTypes; 1584 resTypes = prependCount ? resTypes.drop_front(2) : resTypes; 1585 // Resolve input operands. 1586 for (auto operandType : llvm::zip(operands, resTypes)) 1587 if (parser.resolveOperand(std::get<0>(operandType), 1588 std::get<1>(operandType), result.operands)) 1589 return failure(); 1590 if (prependCount) { 1591 result.addTypes(regionTypes); 1592 } else { 1593 result.addTypes(i1Type); 1594 result.addTypes(resTypes); 1595 } 1596 } else if (succeeded(parser.parseOptionalArrow())) { 1597 llvm::SmallVector<mlir::Type> typeList; 1598 if (parser.parseLParen() || parser.parseTypeList(typeList) || 1599 parser.parseRParen()) 1600 return failure(); 1601 // Type list must be "(index, i1)". 1602 if (typeList.size() != 2 || !typeList[0].isa<mlir::IndexType>() || 1603 !typeList[1].isSignlessInteger(1)) 1604 return failure(); 1605 result.addTypes(typeList); 1606 prependCount = true; 1607 } else { 1608 result.addTypes(i1Type); 1609 } 1610 1611 if (parser.parseOptionalAttrDictWithKeyword(result.attributes)) 1612 return mlir::failure(); 1613 1614 llvm::SmallVector<mlir::Type> argTypes; 1615 // Induction variable (hidden) 1616 if (prependCount) 1617 result.addAttribute(IterWhileOp::getFinalValueAttrNameStr(), 1618 builder.getUnitAttr()); 1619 else 1620 argTypes.push_back(indexType); 1621 // Loop carried variables (including iterate) 1622 argTypes.append(result.types.begin(), result.types.end()); 1623 // Parse the body region. 1624 auto *body = result.addRegion(); 1625 if (regionArgs.size() != argTypes.size()) 1626 return parser.emitError( 1627 parser.getNameLoc(), 1628 "mismatch in number of loop-carried values and defined values"); 1629 1630 if (parser.parseRegion(*body, regionArgs, argTypes)) 1631 return failure(); 1632 1633 fir::IterWhileOp::ensureTerminator(*body, builder, result.location); 1634 1635 return mlir::success(); 1636 } 1637 1638 static mlir::LogicalResult verify(fir::IterWhileOp op) { 1639 // Check that the body defines as single block argument for the induction 1640 // variable. 1641 auto *body = op.getBody(); 1642 if (!body->getArgument(1).getType().isInteger(1)) 1643 return op.emitOpError( 1644 "expected body second argument to be an index argument for " 1645 "the induction variable"); 1646 if (!body->getArgument(0).getType().isIndex()) 1647 return op.emitOpError( 1648 "expected body first argument to be an index argument for " 1649 "the induction variable"); 1650 1651 auto opNumResults = op.getNumResults(); 1652 if (op.finalValue()) { 1653 // Result type must be "(index, i1, ...)". 1654 if (!op.getResult(0).getType().isa<mlir::IndexType>()) 1655 return op.emitOpError("result #0 expected to be index"); 1656 if (!op.getResult(1).getType().isSignlessInteger(1)) 1657 return op.emitOpError("result #1 expected to be i1"); 1658 opNumResults--; 1659 } else { 1660 // iterate_while always returns the early exit induction value. 1661 // Result type must be "(i1, ...)" 1662 if (!op.getResult(0).getType().isSignlessInteger(1)) 1663 return op.emitOpError("result #0 expected to be i1"); 1664 } 1665 if (opNumResults == 0) 1666 return mlir::failure(); 1667 if (op.getNumIterOperands() != opNumResults) 1668 return op.emitOpError( 1669 "mismatch in number of loop-carried values and defined values"); 1670 if (op.getNumRegionIterArgs() != opNumResults) 1671 return op.emitOpError( 1672 "mismatch in number of basic block args and defined values"); 1673 auto iterOperands = op.getIterOperands(); 1674 auto iterArgs = op.getRegionIterArgs(); 1675 auto opResults = 1676 op.finalValue() ? op.getResults().drop_front() : op.getResults(); 1677 unsigned i = 0; 1678 for (auto e : llvm::zip(iterOperands, iterArgs, opResults)) { 1679 if (std::get<0>(e).getType() != std::get<2>(e).getType()) 1680 return op.emitOpError() << "types mismatch between " << i 1681 << "th iter operand and defined value"; 1682 if (std::get<1>(e).getType() != std::get<2>(e).getType()) 1683 return op.emitOpError() << "types mismatch between " << i 1684 << "th iter region arg and defined value"; 1685 1686 i++; 1687 } 1688 return mlir::success(); 1689 } 1690 1691 static void print(mlir::OpAsmPrinter &p, fir::IterWhileOp op) { 1692 p << " (" << op.getInductionVar() << " = " << op.lowerBound() << " to " 1693 << op.upperBound() << " step " << op.step() << ") and ("; 1694 assert(op.hasIterOperands()); 1695 auto regionArgs = op.getRegionIterArgs(); 1696 auto operands = op.getIterOperands(); 1697 p << regionArgs.front() << " = " << *operands.begin() << ")"; 1698 if (regionArgs.size() > 1) { 1699 p << " iter_args("; 1700 llvm::interleaveComma( 1701 llvm::zip(regionArgs.drop_front(), operands.drop_front()), p, 1702 [&](auto it) { p << std::get<0>(it) << " = " << std::get<1>(it); }); 1703 p << ") -> ("; 1704 llvm::interleaveComma( 1705 llvm::drop_begin(op.getResultTypes(), op.finalValue() ? 0 : 1), p); 1706 p << ")"; 1707 } else if (op.finalValue()) { 1708 p << " -> (" << op.getResultTypes() << ')'; 1709 } 1710 p.printOptionalAttrDictWithKeyword(op->getAttrs(), 1711 {op.getFinalValueAttrNameStr()}); 1712 p.printRegion(op.region(), /*printEntryBlockArgs=*/false, 1713 /*printBlockTerminators=*/true); 1714 } 1715 1716 mlir::Region &fir::IterWhileOp::getLoopBody() { return region(); } 1717 1718 bool fir::IterWhileOp::isDefinedOutsideOfLoop(mlir::Value value) { 1719 return !region().isAncestor(value.getParentRegion()); 1720 } 1721 1722 mlir::LogicalResult 1723 fir::IterWhileOp::moveOutOfLoop(llvm::ArrayRef<mlir::Operation *> ops) { 1724 for (auto *op : ops) 1725 op->moveBefore(*this); 1726 return success(); 1727 } 1728 1729 mlir::BlockArgument fir::IterWhileOp::iterArgToBlockArg(mlir::Value iterArg) { 1730 for (auto i : llvm::enumerate(initArgs())) 1731 if (iterArg == i.value()) 1732 return region().front().getArgument(i.index() + 1); 1733 return {}; 1734 } 1735 1736 void fir::IterWhileOp::resultToSourceOps( 1737 llvm::SmallVectorImpl<mlir::Value> &results, unsigned resultNum) { 1738 auto oper = finalValue() ? resultNum + 1 : resultNum; 1739 auto *term = region().front().getTerminator(); 1740 if (oper < term->getNumOperands()) 1741 results.push_back(term->getOperand(oper)); 1742 } 1743 1744 mlir::Value fir::IterWhileOp::blockArgToSourceOp(unsigned blockArgNum) { 1745 if (blockArgNum > 0 && blockArgNum <= initArgs().size()) 1746 return initArgs()[blockArgNum - 1]; 1747 return {}; 1748 } 1749 1750 //===----------------------------------------------------------------------===// 1751 // LenParamIndexOp 1752 //===----------------------------------------------------------------------===// 1753 1754 static mlir::ParseResult parseLenParamIndexOp(mlir::OpAsmParser &parser, 1755 mlir::OperationState &result) { 1756 llvm::StringRef fieldName; 1757 auto &builder = parser.getBuilder(); 1758 mlir::Type recty; 1759 if (parser.parseOptionalKeyword(&fieldName) || parser.parseComma() || 1760 parser.parseType(recty)) 1761 return mlir::failure(); 1762 result.addAttribute(fir::LenParamIndexOp::fieldAttrName(), 1763 builder.getStringAttr(fieldName)); 1764 if (!recty.dyn_cast<RecordType>()) 1765 return mlir::failure(); 1766 result.addAttribute(fir::LenParamIndexOp::typeAttrName(), 1767 mlir::TypeAttr::get(recty)); 1768 mlir::Type lenType = fir::LenType::get(builder.getContext()); 1769 if (parser.addTypeToList(lenType, result.types)) 1770 return mlir::failure(); 1771 return mlir::success(); 1772 } 1773 1774 static void print(mlir::OpAsmPrinter &p, fir::LenParamIndexOp &op) { 1775 p << ' ' 1776 << op.getOperation() 1777 ->getAttrOfType<mlir::StringAttr>( 1778 fir::LenParamIndexOp::fieldAttrName()) 1779 .getValue() 1780 << ", " << op.getOperation()->getAttr(fir::LenParamIndexOp::typeAttrName()); 1781 } 1782 1783 //===----------------------------------------------------------------------===// 1784 // LoadOp 1785 //===----------------------------------------------------------------------===// 1786 1787 void fir::LoadOp::build(mlir::OpBuilder &builder, mlir::OperationState &result, 1788 mlir::Value refVal) { 1789 if (!refVal) { 1790 mlir::emitError(result.location, "LoadOp has null argument"); 1791 return; 1792 } 1793 auto eleTy = fir::dyn_cast_ptrEleTy(refVal.getType()); 1794 if (!eleTy) { 1795 mlir::emitError(result.location, "not a memory reference type"); 1796 return; 1797 } 1798 result.addOperands(refVal); 1799 result.addTypes(eleTy); 1800 } 1801 1802 mlir::ParseResult fir::LoadOp::getElementOf(mlir::Type &ele, mlir::Type ref) { 1803 if ((ele = fir::dyn_cast_ptrEleTy(ref))) 1804 return mlir::success(); 1805 return mlir::failure(); 1806 } 1807 1808 static mlir::ParseResult parseLoadOp(mlir::OpAsmParser &parser, 1809 mlir::OperationState &result) { 1810 mlir::Type type; 1811 mlir::OpAsmParser::OperandType oper; 1812 if (parser.parseOperand(oper) || 1813 parser.parseOptionalAttrDict(result.attributes) || 1814 parser.parseColonType(type) || 1815 parser.resolveOperand(oper, type, result.operands)) 1816 return mlir::failure(); 1817 mlir::Type eleTy; 1818 if (fir::LoadOp::getElementOf(eleTy, type) || 1819 parser.addTypeToList(eleTy, result.types)) 1820 return mlir::failure(); 1821 return mlir::success(); 1822 } 1823 1824 static void print(mlir::OpAsmPrinter &p, fir::LoadOp &op) { 1825 p << ' '; 1826 p.printOperand(op.memref()); 1827 p.printOptionalAttrDict(op.getOperation()->getAttrs(), {}); 1828 p << " : " << op.memref().getType(); 1829 } 1830 1831 //===----------------------------------------------------------------------===// 1832 // DoLoopOp 1833 //===----------------------------------------------------------------------===// 1834 1835 void fir::DoLoopOp::build(mlir::OpBuilder &builder, 1836 mlir::OperationState &result, mlir::Value lb, 1837 mlir::Value ub, mlir::Value step, bool unordered, 1838 bool finalCountValue, mlir::ValueRange iterArgs, 1839 llvm::ArrayRef<mlir::NamedAttribute> attributes) { 1840 result.addOperands({lb, ub, step}); 1841 result.addOperands(iterArgs); 1842 if (finalCountValue) { 1843 result.addTypes(builder.getIndexType()); 1844 result.addAttribute(finalValueAttrName(result.name), builder.getUnitAttr()); 1845 } 1846 for (auto v : iterArgs) 1847 result.addTypes(v.getType()); 1848 mlir::Region *bodyRegion = result.addRegion(); 1849 bodyRegion->push_back(new Block{}); 1850 if (iterArgs.empty() && !finalCountValue) 1851 DoLoopOp::ensureTerminator(*bodyRegion, builder, result.location); 1852 bodyRegion->front().addArgument(builder.getIndexType()); 1853 bodyRegion->front().addArguments(iterArgs.getTypes()); 1854 if (unordered) 1855 result.addAttribute(unorderedAttrName(result.name), builder.getUnitAttr()); 1856 result.addAttributes(attributes); 1857 } 1858 1859 static mlir::ParseResult parseDoLoopOp(mlir::OpAsmParser &parser, 1860 mlir::OperationState &result) { 1861 auto &builder = parser.getBuilder(); 1862 mlir::OpAsmParser::OperandType inductionVariable, lb, ub, step; 1863 // Parse the induction variable followed by '='. 1864 if (parser.parseRegionArgument(inductionVariable) || parser.parseEqual()) 1865 return mlir::failure(); 1866 1867 // Parse loop bounds. 1868 auto indexType = builder.getIndexType(); 1869 if (parser.parseOperand(lb) || 1870 parser.resolveOperand(lb, indexType, result.operands) || 1871 parser.parseKeyword("to") || parser.parseOperand(ub) || 1872 parser.resolveOperand(ub, indexType, result.operands) || 1873 parser.parseKeyword("step") || parser.parseOperand(step) || 1874 parser.resolveOperand(step, indexType, result.operands)) 1875 return failure(); 1876 1877 if (mlir::succeeded(parser.parseOptionalKeyword("unordered"))) 1878 result.addAttribute("unordered", builder.getUnitAttr()); 1879 1880 // Parse the optional initial iteration arguments. 1881 llvm::SmallVector<mlir::OpAsmParser::OperandType> regionArgs, operands; 1882 llvm::SmallVector<mlir::Type> argTypes; 1883 auto prependCount = false; 1884 regionArgs.push_back(inductionVariable); 1885 1886 if (succeeded(parser.parseOptionalKeyword("iter_args"))) { 1887 // Parse assignment list and results type list. 1888 if (parser.parseAssignmentList(regionArgs, operands) || 1889 parser.parseArrowTypeList(result.types)) 1890 return failure(); 1891 if (result.types.size() == operands.size() + 1) 1892 prependCount = true; 1893 // Resolve input operands. 1894 llvm::ArrayRef<mlir::Type> resTypes = result.types; 1895 for (auto operand_type : 1896 llvm::zip(operands, prependCount ? resTypes.drop_front() : resTypes)) 1897 if (parser.resolveOperand(std::get<0>(operand_type), 1898 std::get<1>(operand_type), result.operands)) 1899 return failure(); 1900 } else if (succeeded(parser.parseOptionalArrow())) { 1901 if (parser.parseKeyword("index")) 1902 return failure(); 1903 result.types.push_back(indexType); 1904 prependCount = true; 1905 } 1906 1907 if (parser.parseOptionalAttrDictWithKeyword(result.attributes)) 1908 return mlir::failure(); 1909 1910 // Induction variable. 1911 if (prependCount) 1912 result.addAttribute(DoLoopOp::finalValueAttrName(result.name), 1913 builder.getUnitAttr()); 1914 else 1915 argTypes.push_back(indexType); 1916 // Loop carried variables 1917 argTypes.append(result.types.begin(), result.types.end()); 1918 // Parse the body region. 1919 auto *body = result.addRegion(); 1920 if (regionArgs.size() != argTypes.size()) 1921 return parser.emitError( 1922 parser.getNameLoc(), 1923 "mismatch in number of loop-carried values and defined values"); 1924 1925 if (parser.parseRegion(*body, regionArgs, argTypes)) 1926 return failure(); 1927 1928 DoLoopOp::ensureTerminator(*body, builder, result.location); 1929 1930 return mlir::success(); 1931 } 1932 1933 fir::DoLoopOp fir::getForInductionVarOwner(mlir::Value val) { 1934 auto ivArg = val.dyn_cast<mlir::BlockArgument>(); 1935 if (!ivArg) 1936 return {}; 1937 assert(ivArg.getOwner() && "unlinked block argument"); 1938 auto *containingInst = ivArg.getOwner()->getParentOp(); 1939 return dyn_cast_or_null<fir::DoLoopOp>(containingInst); 1940 } 1941 1942 // Lifted from loop.loop 1943 static mlir::LogicalResult verify(fir::DoLoopOp op) { 1944 // Check that the body defines as single block argument for the induction 1945 // variable. 1946 auto *body = op.getBody(); 1947 if (!body->getArgument(0).getType().isIndex()) 1948 return op.emitOpError( 1949 "expected body first argument to be an index argument for " 1950 "the induction variable"); 1951 1952 auto opNumResults = op.getNumResults(); 1953 if (opNumResults == 0) 1954 return success(); 1955 1956 if (op.finalValue()) { 1957 if (op.unordered()) 1958 return op.emitOpError("unordered loop has no final value"); 1959 opNumResults--; 1960 } 1961 if (op.getNumIterOperands() != opNumResults) 1962 return op.emitOpError( 1963 "mismatch in number of loop-carried values and defined values"); 1964 if (op.getNumRegionIterArgs() != opNumResults) 1965 return op.emitOpError( 1966 "mismatch in number of basic block args and defined values"); 1967 auto iterOperands = op.getIterOperands(); 1968 auto iterArgs = op.getRegionIterArgs(); 1969 auto opResults = 1970 op.finalValue() ? op.getResults().drop_front() : op.getResults(); 1971 unsigned i = 0; 1972 for (auto e : llvm::zip(iterOperands, iterArgs, opResults)) { 1973 if (std::get<0>(e).getType() != std::get<2>(e).getType()) 1974 return op.emitOpError() << "types mismatch between " << i 1975 << "th iter operand and defined value"; 1976 if (std::get<1>(e).getType() != std::get<2>(e).getType()) 1977 return op.emitOpError() << "types mismatch between " << i 1978 << "th iter region arg and defined value"; 1979 1980 i++; 1981 } 1982 return success(); 1983 } 1984 1985 static void print(mlir::OpAsmPrinter &p, fir::DoLoopOp op) { 1986 bool printBlockTerminators = false; 1987 p << ' ' << op.getInductionVar() << " = " << op.lowerBound() << " to " 1988 << op.upperBound() << " step " << op.step(); 1989 if (op.unordered()) 1990 p << " unordered"; 1991 if (op.hasIterOperands()) { 1992 p << " iter_args("; 1993 auto regionArgs = op.getRegionIterArgs(); 1994 auto operands = op.getIterOperands(); 1995 llvm::interleaveComma(llvm::zip(regionArgs, operands), p, [&](auto it) { 1996 p << std::get<0>(it) << " = " << std::get<1>(it); 1997 }); 1998 p << ") -> (" << op.getResultTypes() << ')'; 1999 printBlockTerminators = true; 2000 } else if (op.finalValue()) { 2001 p << " -> " << op.getResultTypes(); 2002 printBlockTerminators = true; 2003 } 2004 p.printOptionalAttrDictWithKeyword(op->getAttrs(), 2005 {"unordered", "finalValue"}); 2006 p.printRegion(op.region(), /*printEntryBlockArgs=*/false, 2007 printBlockTerminators); 2008 } 2009 2010 mlir::Region &fir::DoLoopOp::getLoopBody() { return region(); } 2011 2012 bool fir::DoLoopOp::isDefinedOutsideOfLoop(mlir::Value value) { 2013 return !region().isAncestor(value.getParentRegion()); 2014 } 2015 2016 mlir::LogicalResult 2017 fir::DoLoopOp::moveOutOfLoop(llvm::ArrayRef<mlir::Operation *> ops) { 2018 for (auto op : ops) 2019 op->moveBefore(*this); 2020 return success(); 2021 } 2022 2023 /// Translate a value passed as an iter_arg to the corresponding block 2024 /// argument in the body of the loop. 2025 mlir::BlockArgument fir::DoLoopOp::iterArgToBlockArg(mlir::Value iterArg) { 2026 for (auto i : llvm::enumerate(initArgs())) 2027 if (iterArg == i.value()) 2028 return region().front().getArgument(i.index() + 1); 2029 return {}; 2030 } 2031 2032 /// Translate the result vector (by index number) to the corresponding value 2033 /// to the `fir.result` Op. 2034 void fir::DoLoopOp::resultToSourceOps( 2035 llvm::SmallVectorImpl<mlir::Value> &results, unsigned resultNum) { 2036 auto oper = finalValue() ? resultNum + 1 : resultNum; 2037 auto *term = region().front().getTerminator(); 2038 if (oper < term->getNumOperands()) 2039 results.push_back(term->getOperand(oper)); 2040 } 2041 2042 /// Translate the block argument (by index number) to the corresponding value 2043 /// passed as an iter_arg to the parent DoLoopOp. 2044 mlir::Value fir::DoLoopOp::blockArgToSourceOp(unsigned blockArgNum) { 2045 if (blockArgNum > 0 && blockArgNum <= initArgs().size()) 2046 return initArgs()[blockArgNum - 1]; 2047 return {}; 2048 } 2049 2050 //===----------------------------------------------------------------------===// 2051 // DTEntryOp 2052 //===----------------------------------------------------------------------===// 2053 2054 static mlir::ParseResult parseDTEntryOp(mlir::OpAsmParser &parser, 2055 mlir::OperationState &result) { 2056 llvm::StringRef methodName; 2057 // allow `methodName` or `"methodName"` 2058 if (failed(parser.parseOptionalKeyword(&methodName))) { 2059 mlir::StringAttr methodAttr; 2060 if (parser.parseAttribute(methodAttr, 2061 fir::DTEntryOp::getMethodAttrNameStr(), 2062 result.attributes)) 2063 return mlir::failure(); 2064 } else { 2065 result.addAttribute(fir::DTEntryOp::getMethodAttrNameStr(), 2066 parser.getBuilder().getStringAttr(methodName)); 2067 } 2068 mlir::SymbolRefAttr calleeAttr; 2069 if (parser.parseComma() || 2070 parser.parseAttribute(calleeAttr, fir::DTEntryOp::getProcAttrNameStr(), 2071 result.attributes)) 2072 return mlir::failure(); 2073 return mlir::success(); 2074 } 2075 2076 static void print(mlir::OpAsmPrinter &p, fir::DTEntryOp &op) { 2077 p << ' ' << op.getMethodAttr() << ", " << op.getProcAttr(); 2078 } 2079 2080 //===----------------------------------------------------------------------===// 2081 // ReboxOp 2082 //===----------------------------------------------------------------------===// 2083 2084 /// Get the scalar type related to a fir.box type. 2085 /// Example: return f32 for !fir.box<!fir.heap<!fir.array<?x?xf32>>. 2086 static mlir::Type getBoxScalarEleTy(mlir::Type boxTy) { 2087 auto eleTy = fir::dyn_cast_ptrOrBoxEleTy(boxTy); 2088 if (auto seqTy = eleTy.dyn_cast<fir::SequenceType>()) 2089 return seqTy.getEleTy(); 2090 return eleTy; 2091 } 2092 2093 /// Get the rank from a !fir.box type 2094 static unsigned getBoxRank(mlir::Type boxTy) { 2095 auto eleTy = fir::dyn_cast_ptrOrBoxEleTy(boxTy); 2096 if (auto seqTy = eleTy.dyn_cast<fir::SequenceType>()) 2097 return seqTy.getDimension(); 2098 return 0; 2099 } 2100 2101 static mlir::LogicalResult verify(fir::ReboxOp op) { 2102 auto inputBoxTy = op.box().getType(); 2103 if (fir::isa_unknown_size_box(inputBoxTy)) 2104 return op.emitOpError("box operand must not have unknown rank or type"); 2105 auto outBoxTy = op.getType(); 2106 if (fir::isa_unknown_size_box(outBoxTy)) 2107 return op.emitOpError("result type must not have unknown rank or type"); 2108 auto inputRank = getBoxRank(inputBoxTy); 2109 auto inputEleTy = getBoxScalarEleTy(inputBoxTy); 2110 auto outRank = getBoxRank(outBoxTy); 2111 auto outEleTy = getBoxScalarEleTy(outBoxTy); 2112 2113 if (auto slice = op.slice()) { 2114 // Slicing case 2115 if (slice.getType().cast<fir::SliceType>().getRank() != inputRank) 2116 return op.emitOpError("slice operand rank must match box operand rank"); 2117 if (auto shape = op.shape()) { 2118 if (auto shiftTy = shape.getType().dyn_cast<fir::ShiftType>()) { 2119 if (shiftTy.getRank() != inputRank) 2120 return op.emitOpError("shape operand and input box ranks must match " 2121 "when there is a slice"); 2122 } else { 2123 return op.emitOpError("shape operand must absent or be a fir.shift " 2124 "when there is a slice"); 2125 } 2126 } 2127 if (auto sliceOp = slice.getDefiningOp()) { 2128 auto slicedRank = mlir::cast<fir::SliceOp>(sliceOp).getOutRank(); 2129 if (slicedRank != outRank) 2130 return op.emitOpError("result type rank and rank after applying slice " 2131 "operand must match"); 2132 } 2133 } else { 2134 // Reshaping case 2135 unsigned shapeRank = inputRank; 2136 if (auto shape = op.shape()) { 2137 auto ty = shape.getType(); 2138 if (auto shapeTy = ty.dyn_cast<fir::ShapeType>()) { 2139 shapeRank = shapeTy.getRank(); 2140 } else if (auto shapeShiftTy = ty.dyn_cast<fir::ShapeShiftType>()) { 2141 shapeRank = shapeShiftTy.getRank(); 2142 } else { 2143 auto shiftTy = ty.cast<fir::ShiftType>(); 2144 shapeRank = shiftTy.getRank(); 2145 if (shapeRank != inputRank) 2146 return op.emitOpError("shape operand and input box ranks must match " 2147 "when the shape is a fir.shift"); 2148 } 2149 } 2150 if (shapeRank != outRank) 2151 return op.emitOpError("result type and shape operand ranks must match"); 2152 } 2153 2154 if (inputEleTy != outEleTy) 2155 // TODO: check that outBoxTy is a parent type of inputBoxTy for derived 2156 // types. 2157 if (!inputEleTy.isa<fir::RecordType>()) 2158 return op.emitOpError( 2159 "op input and output element types must match for intrinsic types"); 2160 return mlir::success(); 2161 } 2162 2163 //===----------------------------------------------------------------------===// 2164 // ResultOp 2165 //===----------------------------------------------------------------------===// 2166 2167 static mlir::LogicalResult verify(fir::ResultOp op) { 2168 auto *parentOp = op->getParentOp(); 2169 auto results = parentOp->getResults(); 2170 auto operands = op->getOperands(); 2171 2172 if (parentOp->getNumResults() != op.getNumOperands()) 2173 return op.emitOpError() << "parent of result must have same arity"; 2174 for (auto e : llvm::zip(results, operands)) 2175 if (std::get<0>(e).getType() != std::get<1>(e).getType()) 2176 return op.emitOpError() 2177 << "types mismatch between result op and its parent"; 2178 return success(); 2179 } 2180 2181 //===----------------------------------------------------------------------===// 2182 // SaveResultOp 2183 //===----------------------------------------------------------------------===// 2184 2185 static mlir::LogicalResult verify(fir::SaveResultOp op) { 2186 auto resultType = op.value().getType(); 2187 if (resultType != fir::dyn_cast_ptrEleTy(op.memref().getType())) 2188 return op.emitOpError("value type must match memory reference type"); 2189 if (fir::isa_unknown_size_box(resultType)) 2190 return op.emitOpError("cannot save !fir.box of unknown rank or type"); 2191 2192 if (resultType.isa<fir::BoxType>()) { 2193 if (op.shape() || !op.typeparams().empty()) 2194 return op.emitOpError( 2195 "must not have shape or length operands if the value is a fir.box"); 2196 return mlir::success(); 2197 } 2198 2199 // fir.record or fir.array case. 2200 unsigned shapeTyRank = 0; 2201 if (auto shapeOp = op.shape()) { 2202 auto shapeTy = shapeOp.getType(); 2203 if (auto s = shapeTy.dyn_cast<fir::ShapeType>()) 2204 shapeTyRank = s.getRank(); 2205 else 2206 shapeTyRank = shapeTy.cast<fir::ShapeShiftType>().getRank(); 2207 } 2208 2209 auto eleTy = resultType; 2210 if (auto seqTy = resultType.dyn_cast<fir::SequenceType>()) { 2211 if (seqTy.getDimension() != shapeTyRank) 2212 op.emitOpError("shape operand must be provided and have the value rank " 2213 "when the value is a fir.array"); 2214 eleTy = seqTy.getEleTy(); 2215 } else { 2216 if (shapeTyRank != 0) 2217 op.emitOpError( 2218 "shape operand should only be provided if the value is a fir.array"); 2219 } 2220 2221 if (auto recTy = eleTy.dyn_cast<fir::RecordType>()) { 2222 if (recTy.getNumLenParams() != op.typeparams().size()) 2223 op.emitOpError("length parameters number must match with the value type " 2224 "length parameters"); 2225 } else if (auto charTy = eleTy.dyn_cast<fir::CharacterType>()) { 2226 if (op.typeparams().size() > 1) 2227 op.emitOpError("no more than one length parameter must be provided for " 2228 "character value"); 2229 } else { 2230 if (!op.typeparams().empty()) 2231 op.emitOpError( 2232 "length parameters must not be provided for this value type"); 2233 } 2234 2235 return mlir::success(); 2236 } 2237 2238 //===----------------------------------------------------------------------===// 2239 // SelectOp 2240 //===----------------------------------------------------------------------===// 2241 2242 static constexpr llvm::StringRef getCompareOffsetAttr() { 2243 return "compare_operand_offsets"; 2244 } 2245 2246 static constexpr llvm::StringRef getTargetOffsetAttr() { 2247 return "target_operand_offsets"; 2248 } 2249 2250 template <typename A, typename... AdditionalArgs> 2251 static A getSubOperands(unsigned pos, A allArgs, 2252 mlir::DenseIntElementsAttr ranges, 2253 AdditionalArgs &&...additionalArgs) { 2254 unsigned start = 0; 2255 for (unsigned i = 0; i < pos; ++i) 2256 start += (*(ranges.begin() + i)).getZExtValue(); 2257 return allArgs.slice(start, (*(ranges.begin() + pos)).getZExtValue(), 2258 std::forward<AdditionalArgs>(additionalArgs)...); 2259 } 2260 2261 static mlir::MutableOperandRange 2262 getMutableSuccessorOperands(unsigned pos, mlir::MutableOperandRange operands, 2263 StringRef offsetAttr) { 2264 Operation *owner = operands.getOwner(); 2265 NamedAttribute targetOffsetAttr = 2266 *owner->getAttrDictionary().getNamed(offsetAttr); 2267 return getSubOperands( 2268 pos, operands, targetOffsetAttr.getValue().cast<DenseIntElementsAttr>(), 2269 mlir::MutableOperandRange::OperandSegment(pos, targetOffsetAttr)); 2270 } 2271 2272 static unsigned denseElementsSize(mlir::DenseIntElementsAttr attr) { 2273 return attr.getNumElements(); 2274 } 2275 2276 llvm::Optional<mlir::OperandRange> fir::SelectOp::getCompareOperands(unsigned) { 2277 return {}; 2278 } 2279 2280 llvm::Optional<llvm::ArrayRef<mlir::Value>> 2281 fir::SelectOp::getCompareOperands(llvm::ArrayRef<mlir::Value>, unsigned) { 2282 return {}; 2283 } 2284 2285 llvm::Optional<mlir::MutableOperandRange> 2286 fir::SelectOp::getMutableSuccessorOperands(unsigned oper) { 2287 return ::getMutableSuccessorOperands(oper, targetArgsMutable(), 2288 getTargetOffsetAttr()); 2289 } 2290 2291 llvm::Optional<llvm::ArrayRef<mlir::Value>> 2292 fir::SelectOp::getSuccessorOperands(llvm::ArrayRef<mlir::Value> operands, 2293 unsigned oper) { 2294 auto a = 2295 (*this)->getAttrOfType<mlir::DenseIntElementsAttr>(getTargetOffsetAttr()); 2296 auto segments = (*this)->getAttrOfType<mlir::DenseIntElementsAttr>( 2297 getOperandSegmentSizeAttr()); 2298 return {getSubOperands(oper, getSubOperands(2, operands, segments), a)}; 2299 } 2300 2301 llvm::Optional<mlir::ValueRange> 2302 fir::SelectOp::getSuccessorOperands(mlir::ValueRange operands, unsigned oper) { 2303 auto a = 2304 (*this)->getAttrOfType<mlir::DenseIntElementsAttr>(getTargetOffsetAttr()); 2305 auto segments = (*this)->getAttrOfType<mlir::DenseIntElementsAttr>( 2306 getOperandSegmentSizeAttr()); 2307 return {getSubOperands(oper, getSubOperands(2, operands, segments), a)}; 2308 } 2309 2310 unsigned fir::SelectOp::targetOffsetSize() { 2311 return denseElementsSize((*this)->getAttrOfType<mlir::DenseIntElementsAttr>( 2312 getTargetOffsetAttr())); 2313 } 2314 2315 //===----------------------------------------------------------------------===// 2316 // SelectCaseOp 2317 //===----------------------------------------------------------------------===// 2318 2319 llvm::Optional<mlir::OperandRange> 2320 fir::SelectCaseOp::getCompareOperands(unsigned cond) { 2321 auto a = (*this)->getAttrOfType<mlir::DenseIntElementsAttr>( 2322 getCompareOffsetAttr()); 2323 return {getSubOperands(cond, compareArgs(), a)}; 2324 } 2325 2326 llvm::Optional<llvm::ArrayRef<mlir::Value>> 2327 fir::SelectCaseOp::getCompareOperands(llvm::ArrayRef<mlir::Value> operands, 2328 unsigned cond) { 2329 auto a = (*this)->getAttrOfType<mlir::DenseIntElementsAttr>( 2330 getCompareOffsetAttr()); 2331 auto segments = (*this)->getAttrOfType<mlir::DenseIntElementsAttr>( 2332 getOperandSegmentSizeAttr()); 2333 return {getSubOperands(cond, getSubOperands(1, operands, segments), a)}; 2334 } 2335 2336 llvm::Optional<mlir::ValueRange> 2337 fir::SelectCaseOp::getCompareOperands(mlir::ValueRange 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::MutableOperandRange> 2347 fir::SelectCaseOp::getMutableSuccessorOperands(unsigned oper) { 2348 return ::getMutableSuccessorOperands(oper, targetArgsMutable(), 2349 getTargetOffsetAttr()); 2350 } 2351 2352 llvm::Optional<llvm::ArrayRef<mlir::Value>> 2353 fir::SelectCaseOp::getSuccessorOperands(llvm::ArrayRef<mlir::Value> operands, 2354 unsigned oper) { 2355 auto a = 2356 (*this)->getAttrOfType<mlir::DenseIntElementsAttr>(getTargetOffsetAttr()); 2357 auto segments = (*this)->getAttrOfType<mlir::DenseIntElementsAttr>( 2358 getOperandSegmentSizeAttr()); 2359 return {getSubOperands(oper, getSubOperands(2, operands, segments), a)}; 2360 } 2361 2362 llvm::Optional<mlir::ValueRange> 2363 fir::SelectCaseOp::getSuccessorOperands(mlir::ValueRange 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 // parser for fir.select_case Op 2373 static mlir::ParseResult parseSelectCase(mlir::OpAsmParser &parser, 2374 mlir::OperationState &result) { 2375 mlir::OpAsmParser::OperandType selector; 2376 mlir::Type type; 2377 if (parseSelector(parser, result, selector, type)) 2378 return mlir::failure(); 2379 2380 llvm::SmallVector<mlir::Attribute> attrs; 2381 llvm::SmallVector<mlir::OpAsmParser::OperandType> opers; 2382 llvm::SmallVector<mlir::Block *> dests; 2383 llvm::SmallVector<llvm::SmallVector<mlir::Value>> destArgs; 2384 llvm::SmallVector<int32_t> argOffs; 2385 int32_t offSize = 0; 2386 while (true) { 2387 mlir::Attribute attr; 2388 mlir::Block *dest; 2389 llvm::SmallVector<mlir::Value> destArg; 2390 mlir::NamedAttrList temp; 2391 if (parser.parseAttribute(attr, "a", temp) || isValidCaseAttr(attr) || 2392 parser.parseComma()) 2393 return mlir::failure(); 2394 attrs.push_back(attr); 2395 if (attr.dyn_cast_or_null<mlir::UnitAttr>()) { 2396 argOffs.push_back(0); 2397 } else if (attr.dyn_cast_or_null<fir::ClosedIntervalAttr>()) { 2398 mlir::OpAsmParser::OperandType oper1; 2399 mlir::OpAsmParser::OperandType oper2; 2400 if (parser.parseOperand(oper1) || parser.parseComma() || 2401 parser.parseOperand(oper2) || parser.parseComma()) 2402 return mlir::failure(); 2403 opers.push_back(oper1); 2404 opers.push_back(oper2); 2405 argOffs.push_back(2); 2406 offSize += 2; 2407 } else { 2408 mlir::OpAsmParser::OperandType oper; 2409 if (parser.parseOperand(oper) || parser.parseComma()) 2410 return mlir::failure(); 2411 opers.push_back(oper); 2412 argOffs.push_back(1); 2413 ++offSize; 2414 } 2415 if (parser.parseSuccessorAndUseList(dest, destArg)) 2416 return mlir::failure(); 2417 dests.push_back(dest); 2418 destArgs.push_back(destArg); 2419 if (mlir::succeeded(parser.parseOptionalRSquare())) 2420 break; 2421 if (parser.parseComma()) 2422 return mlir::failure(); 2423 } 2424 result.addAttribute(fir::SelectCaseOp::getCasesAttr(), 2425 parser.getBuilder().getArrayAttr(attrs)); 2426 if (parser.resolveOperands(opers, type, result.operands)) 2427 return mlir::failure(); 2428 llvm::SmallVector<int32_t> targOffs; 2429 int32_t toffSize = 0; 2430 const auto count = dests.size(); 2431 for (std::remove_const_t<decltype(count)> i = 0; i != count; ++i) { 2432 result.addSuccessors(dests[i]); 2433 result.addOperands(destArgs[i]); 2434 auto argSize = destArgs[i].size(); 2435 targOffs.push_back(argSize); 2436 toffSize += argSize; 2437 } 2438 auto &bld = parser.getBuilder(); 2439 result.addAttribute(fir::SelectCaseOp::getOperandSegmentSizeAttr(), 2440 bld.getI32VectorAttr({1, offSize, toffSize})); 2441 result.addAttribute(getCompareOffsetAttr(), bld.getI32VectorAttr(argOffs)); 2442 result.addAttribute(getTargetOffsetAttr(), bld.getI32VectorAttr(targOffs)); 2443 return mlir::success(); 2444 } 2445 2446 static void print(mlir::OpAsmPrinter &p, fir::SelectCaseOp &op) { 2447 p << ' '; 2448 p.printOperand(op.getSelector()); 2449 p << " : " << op.getSelector().getType() << " ["; 2450 auto cases = op.getOperation() 2451 ->getAttrOfType<mlir::ArrayAttr>(op.getCasesAttr()) 2452 .getValue(); 2453 auto count = op.getNumConditions(); 2454 for (decltype(count) i = 0; i != count; ++i) { 2455 if (i) 2456 p << ", "; 2457 p << cases[i] << ", "; 2458 if (!cases[i].isa<mlir::UnitAttr>()) { 2459 auto caseArgs = *op.getCompareOperands(i); 2460 p.printOperand(*caseArgs.begin()); 2461 p << ", "; 2462 if (cases[i].isa<fir::ClosedIntervalAttr>()) { 2463 p.printOperand(*(++caseArgs.begin())); 2464 p << ", "; 2465 } 2466 } 2467 op.printSuccessorAtIndex(p, i); 2468 } 2469 p << ']'; 2470 p.printOptionalAttrDict(op.getOperation()->getAttrs(), 2471 {op.getCasesAttr(), getCompareOffsetAttr(), 2472 getTargetOffsetAttr(), 2473 op.getOperandSegmentSizeAttr()}); 2474 } 2475 2476 unsigned fir::SelectCaseOp::compareOffsetSize() { 2477 return denseElementsSize((*this)->getAttrOfType<mlir::DenseIntElementsAttr>( 2478 getCompareOffsetAttr())); 2479 } 2480 2481 unsigned fir::SelectCaseOp::targetOffsetSize() { 2482 return denseElementsSize((*this)->getAttrOfType<mlir::DenseIntElementsAttr>( 2483 getTargetOffsetAttr())); 2484 } 2485 2486 void fir::SelectCaseOp::build(mlir::OpBuilder &builder, 2487 mlir::OperationState &result, 2488 mlir::Value selector, 2489 llvm::ArrayRef<mlir::Attribute> compareAttrs, 2490 llvm::ArrayRef<mlir::ValueRange> cmpOperands, 2491 llvm::ArrayRef<mlir::Block *> destinations, 2492 llvm::ArrayRef<mlir::ValueRange> destOperands, 2493 llvm::ArrayRef<mlir::NamedAttribute> attributes) { 2494 result.addOperands(selector); 2495 result.addAttribute(getCasesAttr(), builder.getArrayAttr(compareAttrs)); 2496 llvm::SmallVector<int32_t> operOffs; 2497 int32_t operSize = 0; 2498 for (auto attr : compareAttrs) { 2499 if (attr.isa<fir::ClosedIntervalAttr>()) { 2500 operOffs.push_back(2); 2501 operSize += 2; 2502 } else if (attr.isa<mlir::UnitAttr>()) { 2503 operOffs.push_back(0); 2504 } else { 2505 operOffs.push_back(1); 2506 ++operSize; 2507 } 2508 } 2509 for (auto ops : cmpOperands) 2510 result.addOperands(ops); 2511 result.addAttribute(getCompareOffsetAttr(), 2512 builder.getI32VectorAttr(operOffs)); 2513 const auto count = destinations.size(); 2514 for (auto d : destinations) 2515 result.addSuccessors(d); 2516 const auto opCount = destOperands.size(); 2517 llvm::SmallVector<int32_t> argOffs; 2518 int32_t sumArgs = 0; 2519 for (std::remove_const_t<decltype(count)> i = 0; i != count; ++i) { 2520 if (i < opCount) { 2521 result.addOperands(destOperands[i]); 2522 const auto argSz = destOperands[i].size(); 2523 argOffs.push_back(argSz); 2524 sumArgs += argSz; 2525 } else { 2526 argOffs.push_back(0); 2527 } 2528 } 2529 result.addAttribute(getOperandSegmentSizeAttr(), 2530 builder.getI32VectorAttr({1, operSize, sumArgs})); 2531 result.addAttribute(getTargetOffsetAttr(), builder.getI32VectorAttr(argOffs)); 2532 result.addAttributes(attributes); 2533 } 2534 2535 /// This builder has a slightly simplified interface in that the list of 2536 /// operands need not be partitioned by the builder. Instead the operands are 2537 /// partitioned here, before being passed to the default builder. This 2538 /// partitioning is unchecked, so can go awry on bad input. 2539 void fir::SelectCaseOp::build(mlir::OpBuilder &builder, 2540 mlir::OperationState &result, 2541 mlir::Value selector, 2542 llvm::ArrayRef<mlir::Attribute> compareAttrs, 2543 llvm::ArrayRef<mlir::Value> cmpOpList, 2544 llvm::ArrayRef<mlir::Block *> destinations, 2545 llvm::ArrayRef<mlir::ValueRange> destOperands, 2546 llvm::ArrayRef<mlir::NamedAttribute> attributes) { 2547 llvm::SmallVector<mlir::ValueRange> cmpOpers; 2548 auto iter = cmpOpList.begin(); 2549 for (auto &attr : compareAttrs) { 2550 if (attr.isa<fir::ClosedIntervalAttr>()) { 2551 cmpOpers.push_back(mlir::ValueRange({iter, iter + 2})); 2552 iter += 2; 2553 } else if (attr.isa<UnitAttr>()) { 2554 cmpOpers.push_back(mlir::ValueRange{}); 2555 } else { 2556 cmpOpers.push_back(mlir::ValueRange({iter, iter + 1})); 2557 ++iter; 2558 } 2559 } 2560 build(builder, result, selector, compareAttrs, cmpOpers, destinations, 2561 destOperands, attributes); 2562 } 2563 2564 static mlir::LogicalResult verify(fir::SelectCaseOp &op) { 2565 if (!(op.getSelector().getType().isa<mlir::IntegerType>() || 2566 op.getSelector().getType().isa<mlir::IndexType>() || 2567 op.getSelector().getType().isa<fir::IntegerType>() || 2568 op.getSelector().getType().isa<fir::LogicalType>() || 2569 op.getSelector().getType().isa<fir::CharacterType>())) 2570 return op.emitOpError("must be an integer, character, or logical"); 2571 auto cases = op.getOperation() 2572 ->getAttrOfType<mlir::ArrayAttr>(op.getCasesAttr()) 2573 .getValue(); 2574 auto count = op.getNumDest(); 2575 if (count == 0) 2576 return op.emitOpError("must have at least one successor"); 2577 if (op.getNumConditions() != count) 2578 return op.emitOpError("number of conditions and successors don't match"); 2579 if (op.compareOffsetSize() != count) 2580 return op.emitOpError("incorrect number of compare operand groups"); 2581 if (op.targetOffsetSize() != count) 2582 return op.emitOpError("incorrect number of successor operand groups"); 2583 for (decltype(count) i = 0; i != count; ++i) { 2584 auto &attr = cases[i]; 2585 if (!(attr.isa<fir::PointIntervalAttr>() || 2586 attr.isa<fir::LowerBoundAttr>() || attr.isa<fir::UpperBoundAttr>() || 2587 attr.isa<fir::ClosedIntervalAttr>() || attr.isa<mlir::UnitAttr>())) 2588 return op.emitOpError("incorrect select case attribute type"); 2589 } 2590 return mlir::success(); 2591 } 2592 2593 //===----------------------------------------------------------------------===// 2594 // SelectRankOp 2595 //===----------------------------------------------------------------------===// 2596 2597 llvm::Optional<mlir::OperandRange> 2598 fir::SelectRankOp::getCompareOperands(unsigned) { 2599 return {}; 2600 } 2601 2602 llvm::Optional<llvm::ArrayRef<mlir::Value>> 2603 fir::SelectRankOp::getCompareOperands(llvm::ArrayRef<mlir::Value>, unsigned) { 2604 return {}; 2605 } 2606 2607 llvm::Optional<mlir::MutableOperandRange> 2608 fir::SelectRankOp::getMutableSuccessorOperands(unsigned oper) { 2609 return ::getMutableSuccessorOperands(oper, targetArgsMutable(), 2610 getTargetOffsetAttr()); 2611 } 2612 2613 llvm::Optional<llvm::ArrayRef<mlir::Value>> 2614 fir::SelectRankOp::getSuccessorOperands(llvm::ArrayRef<mlir::Value> operands, 2615 unsigned oper) { 2616 auto a = 2617 (*this)->getAttrOfType<mlir::DenseIntElementsAttr>(getTargetOffsetAttr()); 2618 auto segments = (*this)->getAttrOfType<mlir::DenseIntElementsAttr>( 2619 getOperandSegmentSizeAttr()); 2620 return {getSubOperands(oper, getSubOperands(2, operands, segments), a)}; 2621 } 2622 2623 llvm::Optional<mlir::ValueRange> 2624 fir::SelectRankOp::getSuccessorOperands(mlir::ValueRange 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 unsigned fir::SelectRankOp::targetOffsetSize() { 2634 return denseElementsSize((*this)->getAttrOfType<mlir::DenseIntElementsAttr>( 2635 getTargetOffsetAttr())); 2636 } 2637 2638 //===----------------------------------------------------------------------===// 2639 // SelectTypeOp 2640 //===----------------------------------------------------------------------===// 2641 2642 llvm::Optional<mlir::OperandRange> 2643 fir::SelectTypeOp::getCompareOperands(unsigned) { 2644 return {}; 2645 } 2646 2647 llvm::Optional<llvm::ArrayRef<mlir::Value>> 2648 fir::SelectTypeOp::getCompareOperands(llvm::ArrayRef<mlir::Value>, unsigned) { 2649 return {}; 2650 } 2651 2652 llvm::Optional<mlir::MutableOperandRange> 2653 fir::SelectTypeOp::getMutableSuccessorOperands(unsigned oper) { 2654 return ::getMutableSuccessorOperands(oper, targetArgsMutable(), 2655 getTargetOffsetAttr()); 2656 } 2657 2658 llvm::Optional<llvm::ArrayRef<mlir::Value>> 2659 fir::SelectTypeOp::getSuccessorOperands(llvm::ArrayRef<mlir::Value> operands, 2660 unsigned oper) { 2661 auto a = 2662 (*this)->getAttrOfType<mlir::DenseIntElementsAttr>(getTargetOffsetAttr()); 2663 auto segments = (*this)->getAttrOfType<mlir::DenseIntElementsAttr>( 2664 getOperandSegmentSizeAttr()); 2665 return {getSubOperands(oper, getSubOperands(2, operands, segments), a)}; 2666 } 2667 2668 static ParseResult parseSelectType(OpAsmParser &parser, 2669 OperationState &result) { 2670 mlir::OpAsmParser::OperandType selector; 2671 mlir::Type type; 2672 if (parseSelector(parser, result, selector, type)) 2673 return mlir::failure(); 2674 2675 llvm::SmallVector<mlir::Attribute> attrs; 2676 llvm::SmallVector<mlir::Block *> dests; 2677 llvm::SmallVector<llvm::SmallVector<mlir::Value>> destArgs; 2678 while (true) { 2679 mlir::Attribute attr; 2680 mlir::Block *dest; 2681 llvm::SmallVector<mlir::Value> destArg; 2682 mlir::NamedAttrList temp; 2683 if (parser.parseAttribute(attr, "a", temp) || parser.parseComma() || 2684 parser.parseSuccessorAndUseList(dest, destArg)) 2685 return mlir::failure(); 2686 attrs.push_back(attr); 2687 dests.push_back(dest); 2688 destArgs.push_back(destArg); 2689 if (mlir::succeeded(parser.parseOptionalRSquare())) 2690 break; 2691 if (parser.parseComma()) 2692 return mlir::failure(); 2693 } 2694 auto &bld = parser.getBuilder(); 2695 result.addAttribute(fir::SelectTypeOp::getCasesAttr(), 2696 bld.getArrayAttr(attrs)); 2697 llvm::SmallVector<int32_t> argOffs; 2698 int32_t offSize = 0; 2699 const auto count = dests.size(); 2700 for (std::remove_const_t<decltype(count)> i = 0; i != count; ++i) { 2701 result.addSuccessors(dests[i]); 2702 result.addOperands(destArgs[i]); 2703 auto argSize = destArgs[i].size(); 2704 argOffs.push_back(argSize); 2705 offSize += argSize; 2706 } 2707 result.addAttribute(fir::SelectTypeOp::getOperandSegmentSizeAttr(), 2708 bld.getI32VectorAttr({1, 0, offSize})); 2709 result.addAttribute(getTargetOffsetAttr(), bld.getI32VectorAttr(argOffs)); 2710 return mlir::success(); 2711 } 2712 2713 unsigned fir::SelectTypeOp::targetOffsetSize() { 2714 return denseElementsSize((*this)->getAttrOfType<mlir::DenseIntElementsAttr>( 2715 getTargetOffsetAttr())); 2716 } 2717 2718 static void print(mlir::OpAsmPrinter &p, fir::SelectTypeOp &op) { 2719 p << ' '; 2720 p.printOperand(op.getSelector()); 2721 p << " : " << op.getSelector().getType() << " ["; 2722 auto cases = op.getOperation() 2723 ->getAttrOfType<mlir::ArrayAttr>(op.getCasesAttr()) 2724 .getValue(); 2725 auto count = op.getNumConditions(); 2726 for (decltype(count) i = 0; i != count; ++i) { 2727 if (i) 2728 p << ", "; 2729 p << cases[i] << ", "; 2730 op.printSuccessorAtIndex(p, i); 2731 } 2732 p << ']'; 2733 p.printOptionalAttrDict(op.getOperation()->getAttrs(), 2734 {op.getCasesAttr(), getCompareOffsetAttr(), 2735 getTargetOffsetAttr(), 2736 fir::SelectTypeOp::getOperandSegmentSizeAttr()}); 2737 } 2738 2739 static mlir::LogicalResult verify(fir::SelectTypeOp &op) { 2740 if (!(op.getSelector().getType().isa<fir::BoxType>())) 2741 return op.emitOpError("must be a boxed type"); 2742 auto cases = op.getOperation() 2743 ->getAttrOfType<mlir::ArrayAttr>(op.getCasesAttr()) 2744 .getValue(); 2745 auto count = op.getNumDest(); 2746 if (count == 0) 2747 return op.emitOpError("must have at least one successor"); 2748 if (op.getNumConditions() != count) 2749 return op.emitOpError("number of conditions and successors don't match"); 2750 if (op.targetOffsetSize() != count) 2751 return op.emitOpError("incorrect number of successor operand groups"); 2752 for (decltype(count) i = 0; i != count; ++i) { 2753 auto &attr = cases[i]; 2754 if (!(attr.isa<fir::ExactTypeAttr>() || attr.isa<fir::SubclassAttr>() || 2755 attr.isa<mlir::UnitAttr>())) 2756 return op.emitOpError("invalid type-case alternative"); 2757 } 2758 return mlir::success(); 2759 } 2760 2761 void fir::SelectTypeOp::build(mlir::OpBuilder &builder, 2762 mlir::OperationState &result, 2763 mlir::Value selector, 2764 llvm::ArrayRef<mlir::Attribute> typeOperands, 2765 llvm::ArrayRef<mlir::Block *> destinations, 2766 llvm::ArrayRef<mlir::ValueRange> destOperands, 2767 llvm::ArrayRef<mlir::NamedAttribute> attributes) { 2768 result.addOperands(selector); 2769 result.addAttribute(getCasesAttr(), builder.getArrayAttr(typeOperands)); 2770 const auto count = destinations.size(); 2771 for (mlir::Block *dest : destinations) 2772 result.addSuccessors(dest); 2773 const auto opCount = destOperands.size(); 2774 llvm::SmallVector<int32_t> argOffs; 2775 int32_t sumArgs = 0; 2776 for (std::remove_const_t<decltype(count)> i = 0; i != count; ++i) { 2777 if (i < opCount) { 2778 result.addOperands(destOperands[i]); 2779 const auto argSz = destOperands[i].size(); 2780 argOffs.push_back(argSz); 2781 sumArgs += argSz; 2782 } else { 2783 argOffs.push_back(0); 2784 } 2785 } 2786 result.addAttribute(getOperandSegmentSizeAttr(), 2787 builder.getI32VectorAttr({1, 0, sumArgs})); 2788 result.addAttribute(getTargetOffsetAttr(), builder.getI32VectorAttr(argOffs)); 2789 result.addAttributes(attributes); 2790 } 2791 2792 //===----------------------------------------------------------------------===// 2793 // ShapeOp 2794 //===----------------------------------------------------------------------===// 2795 2796 static mlir::LogicalResult verify(fir::ShapeOp &op) { 2797 auto size = op.extents().size(); 2798 auto shapeTy = op.getType().dyn_cast<fir::ShapeType>(); 2799 assert(shapeTy && "must be a shape type"); 2800 if (shapeTy.getRank() != size) 2801 return op.emitOpError("shape type rank mismatch"); 2802 return mlir::success(); 2803 } 2804 2805 //===----------------------------------------------------------------------===// 2806 // ShapeShiftOp 2807 //===----------------------------------------------------------------------===// 2808 2809 static mlir::LogicalResult verify(fir::ShapeShiftOp &op) { 2810 auto size = op.pairs().size(); 2811 if (size < 2 || size > 16 * 2) 2812 return op.emitOpError("incorrect number of args"); 2813 if (size % 2 != 0) 2814 return op.emitOpError("requires a multiple of 2 args"); 2815 auto shapeTy = op.getType().dyn_cast<fir::ShapeShiftType>(); 2816 assert(shapeTy && "must be a shape shift type"); 2817 if (shapeTy.getRank() * 2 != size) 2818 return op.emitOpError("shape type rank mismatch"); 2819 return mlir::success(); 2820 } 2821 2822 //===----------------------------------------------------------------------===// 2823 // ShiftOp 2824 //===----------------------------------------------------------------------===// 2825 2826 static mlir::LogicalResult verify(fir::ShiftOp &op) { 2827 auto size = op.origins().size(); 2828 auto shiftTy = op.getType().dyn_cast<fir::ShiftType>(); 2829 assert(shiftTy && "must be a shift type"); 2830 if (shiftTy.getRank() != size) 2831 return op.emitOpError("shift type rank mismatch"); 2832 return mlir::success(); 2833 } 2834 2835 //===----------------------------------------------------------------------===// 2836 // SliceOp 2837 //===----------------------------------------------------------------------===// 2838 2839 void fir::SliceOp::build(mlir::OpBuilder &builder, mlir::OperationState &result, 2840 mlir::ValueRange trips, mlir::ValueRange path, 2841 mlir::ValueRange substr) { 2842 const auto rank = trips.size() / 3; 2843 auto sliceTy = fir::SliceType::get(builder.getContext(), rank); 2844 build(builder, result, sliceTy, trips, path, substr); 2845 } 2846 2847 /// Return the output rank of a slice op. The output rank must be between 1 and 2848 /// the rank of the array being sliced (inclusive). 2849 unsigned fir::SliceOp::getOutputRank(mlir::ValueRange triples) { 2850 unsigned rank = 0; 2851 if (!triples.empty()) { 2852 for (unsigned i = 1, end = triples.size(); i < end; i += 3) { 2853 auto *op = triples[i].getDefiningOp(); 2854 if (!mlir::isa_and_nonnull<fir::UndefOp>(op)) 2855 ++rank; 2856 } 2857 assert(rank > 0); 2858 } 2859 return rank; 2860 } 2861 2862 static mlir::LogicalResult verify(fir::SliceOp &op) { 2863 auto size = op.triples().size(); 2864 if (size < 3 || size > 16 * 3) 2865 return op.emitOpError("incorrect number of args for triple"); 2866 if (size % 3 != 0) 2867 return op.emitOpError("requires a multiple of 3 args"); 2868 auto sliceTy = op.getType().dyn_cast<fir::SliceType>(); 2869 assert(sliceTy && "must be a slice type"); 2870 if (sliceTy.getRank() * 3 != size) 2871 return op.emitOpError("slice type rank mismatch"); 2872 return mlir::success(); 2873 } 2874 2875 //===----------------------------------------------------------------------===// 2876 // StoreOp 2877 //===----------------------------------------------------------------------===// 2878 2879 mlir::Type fir::StoreOp::elementType(mlir::Type refType) { 2880 return fir::dyn_cast_ptrEleTy(refType); 2881 } 2882 2883 static mlir::ParseResult parseStoreOp(mlir::OpAsmParser &parser, 2884 mlir::OperationState &result) { 2885 mlir::Type type; 2886 mlir::OpAsmParser::OperandType oper; 2887 mlir::OpAsmParser::OperandType store; 2888 if (parser.parseOperand(oper) || parser.parseKeyword("to") || 2889 parser.parseOperand(store) || 2890 parser.parseOptionalAttrDict(result.attributes) || 2891 parser.parseColonType(type) || 2892 parser.resolveOperand(oper, fir::StoreOp::elementType(type), 2893 result.operands) || 2894 parser.resolveOperand(store, type, result.operands)) 2895 return mlir::failure(); 2896 return mlir::success(); 2897 } 2898 2899 static void print(mlir::OpAsmPrinter &p, fir::StoreOp &op) { 2900 p << ' '; 2901 p.printOperand(op.value()); 2902 p << " to "; 2903 p.printOperand(op.memref()); 2904 p.printOptionalAttrDict(op.getOperation()->getAttrs(), {}); 2905 p << " : " << op.memref().getType(); 2906 } 2907 2908 static mlir::LogicalResult verify(fir::StoreOp &op) { 2909 if (op.value().getType() != fir::dyn_cast_ptrEleTy(op.memref().getType())) 2910 return op.emitOpError("store value type must match memory reference type"); 2911 if (fir::isa_unknown_size_box(op.value().getType())) 2912 return op.emitOpError("cannot store !fir.box of unknown rank or type"); 2913 return mlir::success(); 2914 } 2915 2916 //===----------------------------------------------------------------------===// 2917 // StringLitOp 2918 //===----------------------------------------------------------------------===// 2919 2920 bool fir::StringLitOp::isWideValue() { 2921 auto eleTy = getType().cast<fir::SequenceType>().getEleTy(); 2922 return eleTy.cast<fir::CharacterType>().getFKind() != 1; 2923 } 2924 2925 static mlir::NamedAttribute 2926 mkNamedIntegerAttr(mlir::OpBuilder &builder, llvm::StringRef name, int64_t v) { 2927 assert(v > 0); 2928 return builder.getNamedAttr( 2929 name, builder.getIntegerAttr(builder.getIntegerType(64), v)); 2930 } 2931 2932 void fir::StringLitOp::build(mlir::OpBuilder &builder, OperationState &result, 2933 fir::CharacterType inType, llvm::StringRef val, 2934 llvm::Optional<int64_t> len) { 2935 auto valAttr = builder.getNamedAttr(value(), builder.getStringAttr(val)); 2936 int64_t length = len.hasValue() ? len.getValue() : inType.getLen(); 2937 auto lenAttr = mkNamedIntegerAttr(builder, size(), length); 2938 result.addAttributes({valAttr, lenAttr}); 2939 result.addTypes(inType); 2940 } 2941 2942 template <typename C> 2943 static mlir::ArrayAttr convertToArrayAttr(mlir::OpBuilder &builder, 2944 llvm::ArrayRef<C> xlist) { 2945 llvm::SmallVector<mlir::Attribute> attrs; 2946 auto ty = builder.getIntegerType(8 * sizeof(C)); 2947 for (auto ch : xlist) 2948 attrs.push_back(builder.getIntegerAttr(ty, ch)); 2949 return builder.getArrayAttr(attrs); 2950 } 2951 2952 void fir::StringLitOp::build(mlir::OpBuilder &builder, OperationState &result, 2953 fir::CharacterType inType, 2954 llvm::ArrayRef<char> vlist, 2955 llvm::Optional<int64_t> len) { 2956 auto valAttr = 2957 builder.getNamedAttr(xlist(), convertToArrayAttr(builder, vlist)); 2958 std::int64_t length = len.hasValue() ? len.getValue() : inType.getLen(); 2959 auto lenAttr = mkNamedIntegerAttr(builder, size(), length); 2960 result.addAttributes({valAttr, lenAttr}); 2961 result.addTypes(inType); 2962 } 2963 2964 void fir::StringLitOp::build(mlir::OpBuilder &builder, OperationState &result, 2965 fir::CharacterType inType, 2966 llvm::ArrayRef<char16_t> vlist, 2967 llvm::Optional<int64_t> len) { 2968 auto valAttr = 2969 builder.getNamedAttr(xlist(), convertToArrayAttr(builder, vlist)); 2970 std::int64_t length = len.hasValue() ? len.getValue() : inType.getLen(); 2971 auto lenAttr = mkNamedIntegerAttr(builder, size(), length); 2972 result.addAttributes({valAttr, lenAttr}); 2973 result.addTypes(inType); 2974 } 2975 2976 void fir::StringLitOp::build(mlir::OpBuilder &builder, OperationState &result, 2977 fir::CharacterType inType, 2978 llvm::ArrayRef<char32_t> vlist, 2979 llvm::Optional<int64_t> len) { 2980 auto valAttr = 2981 builder.getNamedAttr(xlist(), convertToArrayAttr(builder, vlist)); 2982 std::int64_t length = len.hasValue() ? len.getValue() : inType.getLen(); 2983 auto lenAttr = mkNamedIntegerAttr(builder, size(), length); 2984 result.addAttributes({valAttr, lenAttr}); 2985 result.addTypes(inType); 2986 } 2987 2988 static mlir::ParseResult parseStringLitOp(mlir::OpAsmParser &parser, 2989 mlir::OperationState &result) { 2990 auto &builder = parser.getBuilder(); 2991 mlir::Attribute val; 2992 mlir::NamedAttrList attrs; 2993 llvm::SMLoc trailingTypeLoc; 2994 if (parser.parseAttribute(val, "fake", attrs)) 2995 return mlir::failure(); 2996 if (auto v = val.dyn_cast<mlir::StringAttr>()) 2997 result.attributes.push_back( 2998 builder.getNamedAttr(fir::StringLitOp::value(), v)); 2999 else if (auto v = val.dyn_cast<mlir::ArrayAttr>()) 3000 result.attributes.push_back( 3001 builder.getNamedAttr(fir::StringLitOp::xlist(), v)); 3002 else 3003 return parser.emitError(parser.getCurrentLocation(), 3004 "found an invalid constant"); 3005 mlir::IntegerAttr sz; 3006 mlir::Type type; 3007 if (parser.parseLParen() || 3008 parser.parseAttribute(sz, fir::StringLitOp::size(), result.attributes) || 3009 parser.parseRParen() || parser.getCurrentLocation(&trailingTypeLoc) || 3010 parser.parseColonType(type)) 3011 return mlir::failure(); 3012 auto charTy = type.dyn_cast<fir::CharacterType>(); 3013 if (!charTy) 3014 return parser.emitError(trailingTypeLoc, "must have character type"); 3015 type = fir::CharacterType::get(builder.getContext(), charTy.getFKind(), 3016 sz.getInt()); 3017 if (!type || parser.addTypesToList(type, result.types)) 3018 return mlir::failure(); 3019 return mlir::success(); 3020 } 3021 3022 static void print(mlir::OpAsmPrinter &p, fir::StringLitOp &op) { 3023 p << ' ' << op.getValue() << '('; 3024 p << op.getSize().cast<mlir::IntegerAttr>().getValue() << ") : "; 3025 p.printType(op.getType()); 3026 } 3027 3028 static mlir::LogicalResult verify(fir::StringLitOp &op) { 3029 if (op.getSize().cast<mlir::IntegerAttr>().getValue().isNegative()) 3030 return op.emitOpError("size must be non-negative"); 3031 if (auto xl = op.getOperation()->getAttr(fir::StringLitOp::xlist())) { 3032 auto xList = xl.cast<mlir::ArrayAttr>(); 3033 for (auto a : xList) 3034 if (!a.isa<mlir::IntegerAttr>()) 3035 return op.emitOpError("values in list must be integers"); 3036 } 3037 return mlir::success(); 3038 } 3039 3040 //===----------------------------------------------------------------------===// 3041 // UnboxProcOp 3042 //===----------------------------------------------------------------------===// 3043 3044 static mlir::LogicalResult verify(fir::UnboxProcOp &op) { 3045 if (auto eleTy = fir::dyn_cast_ptrEleTy(op.refTuple().getType())) 3046 if (eleTy.isa<mlir::TupleType>()) 3047 return mlir::success(); 3048 return op.emitOpError("second output argument has bad type"); 3049 } 3050 3051 //===----------------------------------------------------------------------===// 3052 // IfOp 3053 //===----------------------------------------------------------------------===// 3054 3055 void fir::IfOp::build(mlir::OpBuilder &builder, OperationState &result, 3056 mlir::Value cond, bool withElseRegion) { 3057 build(builder, result, llvm::None, cond, withElseRegion); 3058 } 3059 3060 void fir::IfOp::build(mlir::OpBuilder &builder, OperationState &result, 3061 mlir::TypeRange resultTypes, mlir::Value cond, 3062 bool withElseRegion) { 3063 result.addOperands(cond); 3064 result.addTypes(resultTypes); 3065 3066 mlir::Region *thenRegion = result.addRegion(); 3067 thenRegion->push_back(new mlir::Block()); 3068 if (resultTypes.empty()) 3069 IfOp::ensureTerminator(*thenRegion, builder, result.location); 3070 3071 mlir::Region *elseRegion = result.addRegion(); 3072 if (withElseRegion) { 3073 elseRegion->push_back(new mlir::Block()); 3074 if (resultTypes.empty()) 3075 IfOp::ensureTerminator(*elseRegion, builder, result.location); 3076 } 3077 } 3078 3079 static mlir::ParseResult parseIfOp(OpAsmParser &parser, 3080 OperationState &result) { 3081 result.regions.reserve(2); 3082 mlir::Region *thenRegion = result.addRegion(); 3083 mlir::Region *elseRegion = result.addRegion(); 3084 3085 auto &builder = parser.getBuilder(); 3086 OpAsmParser::OperandType cond; 3087 mlir::Type i1Type = builder.getIntegerType(1); 3088 if (parser.parseOperand(cond) || 3089 parser.resolveOperand(cond, i1Type, result.operands)) 3090 return mlir::failure(); 3091 3092 if (parser.parseOptionalArrowTypeList(result.types)) 3093 return mlir::failure(); 3094 3095 if (parser.parseRegion(*thenRegion, {}, {})) 3096 return mlir::failure(); 3097 IfOp::ensureTerminator(*thenRegion, parser.getBuilder(), result.location); 3098 3099 if (mlir::succeeded(parser.parseOptionalKeyword("else"))) { 3100 if (parser.parseRegion(*elseRegion, {}, {})) 3101 return mlir::failure(); 3102 IfOp::ensureTerminator(*elseRegion, parser.getBuilder(), result.location); 3103 } 3104 3105 // Parse the optional attribute list. 3106 if (parser.parseOptionalAttrDict(result.attributes)) 3107 return mlir::failure(); 3108 return mlir::success(); 3109 } 3110 3111 static LogicalResult verify(fir::IfOp op) { 3112 if (op.getNumResults() != 0 && op.elseRegion().empty()) 3113 return op.emitOpError("must have an else block if defining values"); 3114 3115 return mlir::success(); 3116 } 3117 3118 static void print(mlir::OpAsmPrinter &p, fir::IfOp op) { 3119 bool printBlockTerminators = false; 3120 p << ' ' << op.condition(); 3121 if (!op.results().empty()) { 3122 p << " -> (" << op.getResultTypes() << ')'; 3123 printBlockTerminators = true; 3124 } 3125 p.printRegion(op.thenRegion(), /*printEntryBlockArgs=*/false, 3126 printBlockTerminators); 3127 3128 // Print the 'else' regions if it exists and has a block. 3129 auto &otherReg = op.elseRegion(); 3130 if (!otherReg.empty()) { 3131 p << " else"; 3132 p.printRegion(otherReg, /*printEntryBlockArgs=*/false, 3133 printBlockTerminators); 3134 } 3135 p.printOptionalAttrDict(op->getAttrs()); 3136 } 3137 3138 void fir::IfOp::resultToSourceOps(llvm::SmallVectorImpl<mlir::Value> &results, 3139 unsigned resultNum) { 3140 auto *term = thenRegion().front().getTerminator(); 3141 if (resultNum < term->getNumOperands()) 3142 results.push_back(term->getOperand(resultNum)); 3143 term = elseRegion().front().getTerminator(); 3144 if (resultNum < term->getNumOperands()) 3145 results.push_back(term->getOperand(resultNum)); 3146 } 3147 3148 //===----------------------------------------------------------------------===// 3149 3150 mlir::ParseResult fir::isValidCaseAttr(mlir::Attribute attr) { 3151 if (attr.dyn_cast_or_null<mlir::UnitAttr>() || 3152 attr.dyn_cast_or_null<ClosedIntervalAttr>() || 3153 attr.dyn_cast_or_null<PointIntervalAttr>() || 3154 attr.dyn_cast_or_null<LowerBoundAttr>() || 3155 attr.dyn_cast_or_null<UpperBoundAttr>()) 3156 return mlir::success(); 3157 return mlir::failure(); 3158 } 3159 3160 unsigned fir::getCaseArgumentOffset(llvm::ArrayRef<mlir::Attribute> cases, 3161 unsigned dest) { 3162 unsigned o = 0; 3163 for (unsigned i = 0; i < dest; ++i) { 3164 auto &attr = cases[i]; 3165 if (!attr.dyn_cast_or_null<mlir::UnitAttr>()) { 3166 ++o; 3167 if (attr.dyn_cast_or_null<ClosedIntervalAttr>()) 3168 ++o; 3169 } 3170 } 3171 return o; 3172 } 3173 3174 mlir::ParseResult fir::parseSelector(mlir::OpAsmParser &parser, 3175 mlir::OperationState &result, 3176 mlir::OpAsmParser::OperandType &selector, 3177 mlir::Type &type) { 3178 if (parser.parseOperand(selector) || parser.parseColonType(type) || 3179 parser.resolveOperand(selector, type, result.operands) || 3180 parser.parseLSquare()) 3181 return mlir::failure(); 3182 return mlir::success(); 3183 } 3184 3185 /// Generic pretty-printer of a binary operation 3186 static void printBinaryOp(Operation *op, OpAsmPrinter &p) { 3187 assert(op->getNumOperands() == 2 && "binary op must have two operands"); 3188 assert(op->getNumResults() == 1 && "binary op must have one result"); 3189 3190 p << ' ' << op->getOperand(0) << ", " << op->getOperand(1); 3191 p.printOptionalAttrDict(op->getAttrs()); 3192 p << " : " << op->getResult(0).getType(); 3193 } 3194 3195 /// Generic pretty-printer of an unary operation 3196 static void printUnaryOp(Operation *op, OpAsmPrinter &p) { 3197 assert(op->getNumOperands() == 1 && "unary op must have one operand"); 3198 assert(op->getNumResults() == 1 && "unary op must have one result"); 3199 3200 p << ' ' << op->getOperand(0); 3201 p.printOptionalAttrDict(op->getAttrs()); 3202 p << " : " << op->getResult(0).getType(); 3203 } 3204 3205 bool fir::isReferenceLike(mlir::Type type) { 3206 return type.isa<fir::ReferenceType>() || type.isa<fir::HeapType>() || 3207 type.isa<fir::PointerType>(); 3208 } 3209 3210 mlir::FuncOp fir::createFuncOp(mlir::Location loc, mlir::ModuleOp module, 3211 StringRef name, mlir::FunctionType type, 3212 llvm::ArrayRef<mlir::NamedAttribute> attrs) { 3213 if (auto f = module.lookupSymbol<mlir::FuncOp>(name)) 3214 return f; 3215 mlir::OpBuilder modBuilder(module.getBodyRegion()); 3216 modBuilder.setInsertionPointToEnd(module.getBody()); 3217 auto result = modBuilder.create<mlir::FuncOp>(loc, name, type, attrs); 3218 result.setVisibility(mlir::SymbolTable::Visibility::Private); 3219 return result; 3220 } 3221 3222 fir::GlobalOp fir::createGlobalOp(mlir::Location loc, mlir::ModuleOp module, 3223 StringRef name, mlir::Type type, 3224 llvm::ArrayRef<mlir::NamedAttribute> attrs) { 3225 if (auto g = module.lookupSymbol<fir::GlobalOp>(name)) 3226 return g; 3227 mlir::OpBuilder modBuilder(module.getBodyRegion()); 3228 auto result = modBuilder.create<fir::GlobalOp>(loc, name, type, attrs); 3229 result.setVisibility(mlir::SymbolTable::Visibility::Private); 3230 return result; 3231 } 3232 3233 bool fir::valueHasFirAttribute(mlir::Value value, 3234 llvm::StringRef attributeName) { 3235 // If this is a fir.box that was loaded, the fir attributes will be on the 3236 // related fir.ref<fir.box> creation. 3237 if (value.getType().isa<fir::BoxType>()) 3238 if (auto definingOp = value.getDefiningOp()) 3239 if (auto loadOp = mlir::dyn_cast<fir::LoadOp>(definingOp)) 3240 value = loadOp.memref(); 3241 // If this is a function argument, look in the argument attributes. 3242 if (auto blockArg = value.dyn_cast<mlir::BlockArgument>()) { 3243 if (blockArg.getOwner() && blockArg.getOwner()->isEntryBlock()) 3244 if (auto funcOp = 3245 mlir::dyn_cast<mlir::FuncOp>(blockArg.getOwner()->getParentOp())) 3246 if (funcOp.getArgAttr(blockArg.getArgNumber(), attributeName)) 3247 return true; 3248 return false; 3249 } 3250 3251 if (auto definingOp = value.getDefiningOp()) { 3252 // If this is an allocated value, look at the allocation attributes. 3253 if (mlir::isa<fir::AllocMemOp>(definingOp) || 3254 mlir::isa<AllocaOp>(definingOp)) 3255 return definingOp->hasAttr(attributeName); 3256 // If this is an imported global, look at AddrOfOp and GlobalOp attributes. 3257 // Both operations are looked at because use/host associated variable (the 3258 // AddrOfOp) can have ASYNCHRONOUS/VOLATILE attributes even if the ultimate 3259 // entity (the globalOp) does not have them. 3260 if (auto addressOfOp = mlir::dyn_cast<fir::AddrOfOp>(definingOp)) { 3261 if (addressOfOp->hasAttr(attributeName)) 3262 return true; 3263 if (auto module = definingOp->getParentOfType<mlir::ModuleOp>()) 3264 if (auto globalOp = 3265 module.lookupSymbol<fir::GlobalOp>(addressOfOp.symbol())) 3266 return globalOp->hasAttr(attributeName); 3267 } 3268 } 3269 // TODO: Construct associated entities attributes. Decide where the fir 3270 // attributes must be placed/looked for in this case. 3271 return false; 3272 } 3273 3274 mlir::Type fir::applyPathToType(mlir::Type eleTy, mlir::ValueRange path) { 3275 for (auto i = path.begin(), end = path.end(); eleTy && i < end;) { 3276 eleTy = llvm::TypeSwitch<mlir::Type, mlir::Type>(eleTy) 3277 .Case<fir::RecordType>([&](fir::RecordType ty) { 3278 if (auto *op = (*i++).getDefiningOp()) { 3279 if (auto off = mlir::dyn_cast<fir::FieldIndexOp>(op)) 3280 return ty.getType(off.getFieldName()); 3281 if (auto off = mlir::dyn_cast<mlir::arith::ConstantOp>(op)) 3282 return ty.getType(fir::toInt(off)); 3283 } 3284 return mlir::Type{}; 3285 }) 3286 .Case<fir::SequenceType>([&](fir::SequenceType ty) { 3287 bool valid = true; 3288 const auto rank = ty.getDimension(); 3289 for (std::remove_const_t<decltype(rank)> ii = 0; 3290 valid && ii < rank; ++ii) 3291 valid = i < end && fir::isa_integer((*i++).getType()); 3292 return valid ? ty.getEleTy() : mlir::Type{}; 3293 }) 3294 .Case<mlir::TupleType>([&](mlir::TupleType ty) { 3295 if (auto *op = (*i++).getDefiningOp()) 3296 if (auto off = mlir::dyn_cast<mlir::arith::ConstantOp>(op)) 3297 return ty.getType(fir::toInt(off)); 3298 return mlir::Type{}; 3299 }) 3300 .Case<fir::ComplexType>([&](fir::ComplexType ty) { 3301 if (fir::isa_integer((*i++).getType())) 3302 return ty.getElementType(); 3303 return mlir::Type{}; 3304 }) 3305 .Case<mlir::ComplexType>([&](mlir::ComplexType ty) { 3306 if (fir::isa_integer((*i++).getType())) 3307 return ty.getElementType(); 3308 return mlir::Type{}; 3309 }) 3310 .Default([&](const auto &) { return mlir::Type{}; }); 3311 } 3312 return eleTy; 3313 } 3314 3315 // Tablegen operators 3316 3317 #define GET_OP_CLASSES 3318 #include "flang/Optimizer/Dialect/FIROps.cpp.inc" 3319