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