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