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 "mlir/Dialect/CommonFolders.h" 18 #include "mlir/Dialect/StandardOps/IR/Ops.h" 19 #include "mlir/IR/BuiltinOps.h" 20 #include "mlir/IR/Diagnostics.h" 21 #include "mlir/IR/Matchers.h" 22 #include "mlir/IR/PatternMatch.h" 23 #include "llvm/ADT/StringSwitch.h" 24 #include "llvm/ADT/TypeSwitch.h" 25 26 using namespace fir; 27 28 /// Return true if a sequence type is of some incomplete size or a record type 29 /// is malformed or contains an incomplete sequence type. An incomplete sequence 30 /// type is one with more unknown extents in the type than have been provided 31 /// via `dynamicExtents`. Sequence types with an unknown rank are incomplete by 32 /// definition. 33 static bool verifyInType(mlir::Type inType, 34 llvm::SmallVectorImpl<llvm::StringRef> &visited, 35 unsigned dynamicExtents = 0) { 36 if (auto st = inType.dyn_cast<fir::SequenceType>()) { 37 auto shape = st.getShape(); 38 if (shape.size() == 0) 39 return true; 40 for (std::size_t i = 0, end{shape.size()}; i < end; ++i) { 41 if (shape[i] != fir::SequenceType::getUnknownExtent()) 42 continue; 43 if (dynamicExtents-- == 0) 44 return true; 45 } 46 } else if (auto rt = inType.dyn_cast<fir::RecordType>()) { 47 // don't recurse if we're already visiting this one 48 if (llvm::is_contained(visited, rt.getName())) 49 return false; 50 // keep track of record types currently being visited 51 visited.push_back(rt.getName()); 52 for (auto &field : rt.getTypeList()) 53 if (verifyInType(field.second, visited)) 54 return true; 55 visited.pop_back(); 56 } else if (auto rt = inType.dyn_cast<fir::PointerType>()) { 57 return verifyInType(rt.getEleTy(), visited); 58 } 59 return false; 60 } 61 62 static bool verifyRecordLenParams(mlir::Type inType, unsigned numLenParams) { 63 if (numLenParams > 0) { 64 if (auto rt = inType.dyn_cast<fir::RecordType>()) 65 return numLenParams != rt.getNumLenParams(); 66 return true; 67 } 68 return false; 69 } 70 71 //===----------------------------------------------------------------------===// 72 // AllocaOp 73 //===----------------------------------------------------------------------===// 74 75 mlir::Type fir::AllocaOp::getAllocatedType() { 76 return getType().cast<ReferenceType>().getEleTy(); 77 } 78 79 /// Create a legal memory reference as return type 80 mlir::Type fir::AllocaOp::wrapResultType(mlir::Type intype) { 81 // FIR semantics: memory references to memory references are disallowed 82 if (intype.isa<ReferenceType>()) 83 return {}; 84 return ReferenceType::get(intype); 85 } 86 87 mlir::Type fir::AllocaOp::getRefTy(mlir::Type ty) { 88 return ReferenceType::get(ty); 89 } 90 91 //===----------------------------------------------------------------------===// 92 // AllocMemOp 93 //===----------------------------------------------------------------------===// 94 95 mlir::Type fir::AllocMemOp::getAllocatedType() { 96 return getType().cast<HeapType>().getEleTy(); 97 } 98 99 mlir::Type fir::AllocMemOp::getRefTy(mlir::Type ty) { 100 return HeapType::get(ty); 101 } 102 103 /// Create a legal heap reference as return type 104 mlir::Type fir::AllocMemOp::wrapResultType(mlir::Type intype) { 105 // Fortran semantics: C852 an entity cannot be both ALLOCATABLE and POINTER 106 // 8.5.3 note 1 prohibits ALLOCATABLE procedures as well 107 // FIR semantics: one may not allocate a memory reference value 108 if (intype.isa<ReferenceType>() || intype.isa<HeapType>() || 109 intype.isa<PointerType>() || intype.isa<FunctionType>()) 110 return {}; 111 return HeapType::get(intype); 112 } 113 114 //===----------------------------------------------------------------------===// 115 // ArrayCoorOp 116 //===----------------------------------------------------------------------===// 117 118 static mlir::LogicalResult verify(fir::ArrayCoorOp op) { 119 auto eleTy = fir::dyn_cast_ptrOrBoxEleTy(op.memref().getType()); 120 auto arrTy = eleTy.dyn_cast<fir::SequenceType>(); 121 if (!arrTy) 122 return op.emitOpError("must be a reference to an array"); 123 auto arrDim = arrTy.getDimension(); 124 125 if (auto shapeOp = op.shape()) { 126 auto shapeTy = shapeOp.getType(); 127 unsigned shapeTyRank = 0; 128 if (auto s = shapeTy.dyn_cast<fir::ShapeType>()) { 129 shapeTyRank = s.getRank(); 130 } else if (auto ss = shapeTy.dyn_cast<fir::ShapeShiftType>()) { 131 shapeTyRank = ss.getRank(); 132 } else { 133 auto s = shapeTy.cast<fir::ShiftType>(); 134 shapeTyRank = s.getRank(); 135 if (!op.memref().getType().isa<fir::BoxType>()) 136 return op.emitOpError("shift can only be provided with fir.box memref"); 137 } 138 if (arrDim && arrDim != shapeTyRank) 139 return op.emitOpError("rank of dimension mismatched"); 140 if (shapeTyRank != op.indices().size()) 141 return op.emitOpError("number of indices do not match dim rank"); 142 } 143 144 if (auto sliceOp = op.slice()) 145 if (auto sliceTy = sliceOp.getType().dyn_cast<fir::SliceType>()) 146 if (sliceTy.getRank() != arrDim) 147 return op.emitOpError("rank of dimension in slice mismatched"); 148 149 return mlir::success(); 150 } 151 152 //===----------------------------------------------------------------------===// 153 // ArrayLoadOp 154 //===----------------------------------------------------------------------===// 155 156 std::vector<mlir::Value> fir::ArrayLoadOp::getExtents() { 157 if (auto sh = shape()) 158 if (auto *op = sh.getDefiningOp()) { 159 if (auto shOp = dyn_cast<fir::ShapeOp>(op)) 160 return shOp.getExtents(); 161 return cast<fir::ShapeShiftOp>(op).getExtents(); 162 } 163 return {}; 164 } 165 166 static mlir::LogicalResult verify(fir::ArrayLoadOp op) { 167 auto eleTy = fir::dyn_cast_ptrOrBoxEleTy(op.memref().getType()); 168 auto arrTy = eleTy.dyn_cast<fir::SequenceType>(); 169 if (!arrTy) 170 return op.emitOpError("must be a reference to an array"); 171 auto arrDim = arrTy.getDimension(); 172 173 if (auto shapeOp = op.shape()) { 174 auto shapeTy = shapeOp.getType(); 175 unsigned shapeTyRank = 0; 176 if (auto s = shapeTy.dyn_cast<fir::ShapeType>()) { 177 shapeTyRank = s.getRank(); 178 } else if (auto ss = shapeTy.dyn_cast<fir::ShapeShiftType>()) { 179 shapeTyRank = ss.getRank(); 180 } else { 181 auto s = shapeTy.cast<fir::ShiftType>(); 182 shapeTyRank = s.getRank(); 183 if (!op.memref().getType().isa<fir::BoxType>()) 184 return op.emitOpError("shift can only be provided with fir.box memref"); 185 } 186 if (arrDim && arrDim != shapeTyRank) 187 return op.emitOpError("rank of dimension mismatched"); 188 } 189 190 if (auto sliceOp = op.slice()) 191 if (auto sliceTy = sliceOp.getType().dyn_cast<fir::SliceType>()) 192 if (sliceTy.getRank() != arrDim) 193 return op.emitOpError("rank of dimension in slice mismatched"); 194 195 return mlir::success(); 196 } 197 198 //===----------------------------------------------------------------------===// 199 // BoxAddrOp 200 //===----------------------------------------------------------------------===// 201 202 mlir::OpFoldResult fir::BoxAddrOp::fold(llvm::ArrayRef<mlir::Attribute> opnds) { 203 if (auto v = val().getDefiningOp()) { 204 if (auto box = dyn_cast<fir::EmboxOp>(v)) 205 return box.memref(); 206 if (auto box = dyn_cast<fir::EmboxCharOp>(v)) 207 return box.memref(); 208 } 209 return {}; 210 } 211 212 //===----------------------------------------------------------------------===// 213 // BoxCharLenOp 214 //===----------------------------------------------------------------------===// 215 216 mlir::OpFoldResult 217 fir::BoxCharLenOp::fold(llvm::ArrayRef<mlir::Attribute> opnds) { 218 if (auto v = val().getDefiningOp()) { 219 if (auto box = dyn_cast<fir::EmboxCharOp>(v)) 220 return box.len(); 221 } 222 return {}; 223 } 224 225 //===----------------------------------------------------------------------===// 226 // BoxDimsOp 227 //===----------------------------------------------------------------------===// 228 229 /// Get the result types packed in a tuple tuple 230 mlir::Type fir::BoxDimsOp::getTupleType() { 231 // note: triple, but 4 is nearest power of 2 232 llvm::SmallVector<mlir::Type, 4> triple{ 233 getResult(0).getType(), getResult(1).getType(), getResult(2).getType()}; 234 return mlir::TupleType::get(getContext(), triple); 235 } 236 237 //===----------------------------------------------------------------------===// 238 // CallOp 239 //===----------------------------------------------------------------------===// 240 241 mlir::FunctionType fir::CallOp::getFunctionType() { 242 return mlir::FunctionType::get(getContext(), getOperandTypes(), 243 getResultTypes()); 244 } 245 246 static void printCallOp(mlir::OpAsmPrinter &p, fir::CallOp &op) { 247 auto callee = op.callee(); 248 bool isDirect = callee.hasValue(); 249 p << ' '; 250 if (isDirect) 251 p << callee.getValue(); 252 else 253 p << op.getOperand(0); 254 p << '(' << op->getOperands().drop_front(isDirect ? 0 : 1) << ')'; 255 p.printOptionalAttrDict(op->getAttrs(), {"callee"}); 256 auto resultTypes{op.getResultTypes()}; 257 llvm::SmallVector<Type, 8> argTypes( 258 llvm::drop_begin(op.getOperandTypes(), isDirect ? 0 : 1)); 259 p << " : " << FunctionType::get(op.getContext(), argTypes, resultTypes); 260 } 261 262 static mlir::ParseResult parseCallOp(mlir::OpAsmParser &parser, 263 mlir::OperationState &result) { 264 llvm::SmallVector<mlir::OpAsmParser::OperandType, 8> operands; 265 if (parser.parseOperandList(operands)) 266 return mlir::failure(); 267 268 mlir::NamedAttrList attrs; 269 mlir::SymbolRefAttr funcAttr; 270 bool isDirect = operands.empty(); 271 if (isDirect) 272 if (parser.parseAttribute(funcAttr, "callee", attrs)) 273 return mlir::failure(); 274 275 Type type; 276 if (parser.parseOperandList(operands, mlir::OpAsmParser::Delimiter::Paren) || 277 parser.parseOptionalAttrDict(attrs) || parser.parseColon() || 278 parser.parseType(type)) 279 return mlir::failure(); 280 281 auto funcType = type.dyn_cast<mlir::FunctionType>(); 282 if (!funcType) 283 return parser.emitError(parser.getNameLoc(), "expected function type"); 284 if (isDirect) { 285 if (parser.resolveOperands(operands, funcType.getInputs(), 286 parser.getNameLoc(), result.operands)) 287 return mlir::failure(); 288 } else { 289 auto funcArgs = 290 llvm::ArrayRef<mlir::OpAsmParser::OperandType>(operands).drop_front(); 291 if (parser.resolveOperand(operands[0], funcType, result.operands) || 292 parser.resolveOperands(funcArgs, funcType.getInputs(), 293 parser.getNameLoc(), result.operands)) 294 return mlir::failure(); 295 } 296 result.addTypes(funcType.getResults()); 297 result.attributes = attrs; 298 return mlir::success(); 299 } 300 301 void fir::CallOp::build(mlir::OpBuilder &builder, mlir::OperationState &result, 302 mlir::FuncOp callee, mlir::ValueRange operands) { 303 result.addOperands(operands); 304 result.addAttribute(getCalleeAttrName(), SymbolRefAttr::get(callee)); 305 result.addTypes(callee.getType().getResults()); 306 } 307 308 void fir::CallOp::build(mlir::OpBuilder &builder, mlir::OperationState &result, 309 mlir::SymbolRefAttr callee, 310 llvm::ArrayRef<mlir::Type> results, 311 mlir::ValueRange operands) { 312 result.addOperands(operands); 313 result.addAttribute(getCalleeAttrName(), callee); 314 result.addTypes(results); 315 } 316 317 //===----------------------------------------------------------------------===// 318 // CmpOp 319 //===----------------------------------------------------------------------===// 320 321 template <typename OPTY> 322 static void printCmpOp(OpAsmPrinter &p, OPTY op) { 323 p << ' '; 324 auto predSym = mlir::symbolizeCmpFPredicate( 325 op->template getAttrOfType<mlir::IntegerAttr>( 326 OPTY::getPredicateAttrName()) 327 .getInt()); 328 assert(predSym.hasValue() && "invalid symbol value for predicate"); 329 p << '"' << mlir::stringifyCmpFPredicate(predSym.getValue()) << '"' << ", "; 330 p.printOperand(op.lhs()); 331 p << ", "; 332 p.printOperand(op.rhs()); 333 p.printOptionalAttrDict(op->getAttrs(), 334 /*elidedAttrs=*/{OPTY::getPredicateAttrName()}); 335 p << " : " << op.lhs().getType(); 336 } 337 338 template <typename OPTY> 339 static mlir::ParseResult parseCmpOp(mlir::OpAsmParser &parser, 340 mlir::OperationState &result) { 341 llvm::SmallVector<mlir::OpAsmParser::OperandType, 2> ops; 342 mlir::NamedAttrList attrs; 343 mlir::Attribute predicateNameAttr; 344 mlir::Type type; 345 if (parser.parseAttribute(predicateNameAttr, OPTY::getPredicateAttrName(), 346 attrs) || 347 parser.parseComma() || parser.parseOperandList(ops, 2) || 348 parser.parseOptionalAttrDict(attrs) || parser.parseColonType(type) || 349 parser.resolveOperands(ops, type, result.operands)) 350 return failure(); 351 352 if (!predicateNameAttr.isa<mlir::StringAttr>()) 353 return parser.emitError(parser.getNameLoc(), 354 "expected string comparison predicate attribute"); 355 356 // Rewrite string attribute to an enum value. 357 llvm::StringRef predicateName = 358 predicateNameAttr.cast<mlir::StringAttr>().getValue(); 359 auto predicate = fir::CmpcOp::getPredicateByName(predicateName); 360 auto builder = parser.getBuilder(); 361 mlir::Type i1Type = builder.getI1Type(); 362 attrs.set(OPTY::getPredicateAttrName(), 363 builder.getI64IntegerAttr(static_cast<int64_t>(predicate))); 364 result.attributes = attrs; 365 result.addTypes({i1Type}); 366 return success(); 367 } 368 369 //===----------------------------------------------------------------------===// 370 // CmpcOp 371 //===----------------------------------------------------------------------===// 372 373 void fir::buildCmpCOp(OpBuilder &builder, OperationState &result, 374 CmpFPredicate predicate, Value lhs, Value rhs) { 375 result.addOperands({lhs, rhs}); 376 result.types.push_back(builder.getI1Type()); 377 result.addAttribute( 378 fir::CmpcOp::getPredicateAttrName(), 379 builder.getI64IntegerAttr(static_cast<int64_t>(predicate))); 380 } 381 382 mlir::CmpFPredicate fir::CmpcOp::getPredicateByName(llvm::StringRef name) { 383 auto pred = mlir::symbolizeCmpFPredicate(name); 384 assert(pred.hasValue() && "invalid predicate name"); 385 return pred.getValue(); 386 } 387 388 static void printCmpcOp(OpAsmPrinter &p, fir::CmpcOp op) { printCmpOp(p, op); } 389 390 mlir::ParseResult fir::parseCmpcOp(mlir::OpAsmParser &parser, 391 mlir::OperationState &result) { 392 return parseCmpOp<fir::CmpcOp>(parser, result); 393 } 394 395 //===----------------------------------------------------------------------===// 396 // ConvertOp 397 //===----------------------------------------------------------------------===// 398 399 void fir::ConvertOp::getCanonicalizationPatterns( 400 OwningRewritePatternList &results, MLIRContext *context) {} 401 402 mlir::OpFoldResult fir::ConvertOp::fold(llvm::ArrayRef<mlir::Attribute> opnds) { 403 if (value().getType() == getType()) 404 return value(); 405 if (matchPattern(value(), m_Op<fir::ConvertOp>())) { 406 auto inner = cast<fir::ConvertOp>(value().getDefiningOp()); 407 // (convert (convert 'a : logical -> i1) : i1 -> logical) ==> forward 'a 408 if (auto toTy = getType().dyn_cast<fir::LogicalType>()) 409 if (auto fromTy = inner.value().getType().dyn_cast<fir::LogicalType>()) 410 if (inner.getType().isa<mlir::IntegerType>() && (toTy == fromTy)) 411 return inner.value(); 412 // (convert (convert 'a : i1 -> logical) : logical -> i1) ==> forward 'a 413 if (auto toTy = getType().dyn_cast<mlir::IntegerType>()) 414 if (auto fromTy = inner.value().getType().dyn_cast<mlir::IntegerType>()) 415 if (inner.getType().isa<fir::LogicalType>() && (toTy == fromTy) && 416 (fromTy.getWidth() == 1)) 417 return inner.value(); 418 } 419 return {}; 420 } 421 422 bool fir::ConvertOp::isIntegerCompatible(mlir::Type ty) { 423 return ty.isa<mlir::IntegerType>() || ty.isa<mlir::IndexType>() || 424 ty.isa<fir::IntegerType>() || ty.isa<fir::LogicalType>(); 425 } 426 427 bool fir::ConvertOp::isFloatCompatible(mlir::Type ty) { 428 return ty.isa<mlir::FloatType>() || ty.isa<fir::RealType>(); 429 } 430 431 bool fir::ConvertOp::isPointerCompatible(mlir::Type ty) { 432 return ty.isa<fir::ReferenceType>() || ty.isa<fir::PointerType>() || 433 ty.isa<fir::HeapType>() || ty.isa<mlir::MemRefType>() || 434 ty.isa<mlir::FunctionType>() || ty.isa<fir::TypeDescType>(); 435 } 436 437 //===----------------------------------------------------------------------===// 438 // CoordinateOp 439 //===----------------------------------------------------------------------===// 440 441 static void print(mlir::OpAsmPrinter &p, fir::CoordinateOp op) { 442 p << ' ' << op.ref() << ", " << op.coor(); 443 p.printOptionalAttrDict(op->getAttrs(), /*elideAttrs=*/{"baseType"}); 444 p << " : "; 445 p.printFunctionalType(op.getOperandTypes(), op->getResultTypes()); 446 } 447 448 static mlir::ParseResult parseCoordinateCustom(mlir::OpAsmParser &parser, 449 mlir::OperationState &result) { 450 mlir::OpAsmParser::OperandType memref; 451 if (parser.parseOperand(memref) || parser.parseComma()) 452 return mlir::failure(); 453 llvm::SmallVector<mlir::OpAsmParser::OperandType, 8> coorOperands; 454 if (parser.parseOperandList(coorOperands)) 455 return mlir::failure(); 456 llvm::SmallVector<mlir::OpAsmParser::OperandType, 16> allOperands; 457 allOperands.push_back(memref); 458 allOperands.append(coorOperands.begin(), coorOperands.end()); 459 mlir::FunctionType funcTy; 460 auto loc = parser.getCurrentLocation(); 461 if (parser.parseOptionalAttrDict(result.attributes) || 462 parser.parseColonType(funcTy) || 463 parser.resolveOperands(allOperands, funcTy.getInputs(), loc, 464 result.operands)) 465 return failure(); 466 parser.addTypesToList(funcTy.getResults(), result.types); 467 result.addAttribute("baseType", mlir::TypeAttr::get(funcTy.getInput(0))); 468 return mlir::success(); 469 } 470 471 static mlir::LogicalResult verify(fir::CoordinateOp op) { 472 auto refTy = op.ref().getType(); 473 if (fir::isa_ref_type(refTy)) { 474 auto eleTy = fir::dyn_cast_ptrEleTy(refTy); 475 if (auto arrTy = eleTy.dyn_cast<fir::SequenceType>()) { 476 if (arrTy.hasUnknownShape()) 477 return op.emitOpError("cannot find coordinate in unknown shape"); 478 if (arrTy.getConstantRows() < arrTy.getDimension() - 1) 479 return op.emitOpError("cannot find coordinate with unknown extents"); 480 } 481 if (!(fir::isa_aggregate(eleTy) || fir::isa_complex(eleTy) || 482 fir::isa_char_string(eleTy))) 483 return op.emitOpError("cannot apply coordinate_of to this type"); 484 } 485 // Recovering a LEN type parameter only makes sense from a boxed value. For a 486 // bare reference, the LEN type parameters must be passed as additional 487 // arguments to `op`. 488 for (auto co : op.coor()) 489 if (dyn_cast_or_null<fir::LenParamIndexOp>(co.getDefiningOp())) { 490 if (op.getNumOperands() != 2) 491 return op.emitOpError("len_param_index must be last argument"); 492 if (!op.ref().getType().isa<BoxType>()) 493 return op.emitOpError("len_param_index must be used on box type"); 494 } 495 return mlir::success(); 496 } 497 498 //===----------------------------------------------------------------------===// 499 // DispatchOp 500 //===----------------------------------------------------------------------===// 501 502 mlir::FunctionType fir::DispatchOp::getFunctionType() { 503 return mlir::FunctionType::get(getContext(), getOperandTypes(), 504 getResultTypes()); 505 } 506 507 //===----------------------------------------------------------------------===// 508 // DispatchTableOp 509 //===----------------------------------------------------------------------===// 510 511 void fir::DispatchTableOp::appendTableEntry(mlir::Operation *op) { 512 assert(mlir::isa<fir::DTEntryOp>(*op) && "operation must be a DTEntryOp"); 513 auto &block = getBlock(); 514 block.getOperations().insert(block.end(), op); 515 } 516 517 //===----------------------------------------------------------------------===// 518 // EmboxOp 519 //===----------------------------------------------------------------------===// 520 521 static mlir::LogicalResult verify(fir::EmboxOp op) { 522 auto eleTy = fir::dyn_cast_ptrEleTy(op.memref().getType()); 523 bool isArray = false; 524 if (auto seqTy = eleTy.dyn_cast<fir::SequenceType>()) { 525 eleTy = seqTy.getEleTy(); 526 isArray = true; 527 } 528 if (op.hasLenParams()) { 529 auto lenPs = op.numLenParams(); 530 if (auto rt = eleTy.dyn_cast<fir::RecordType>()) { 531 if (lenPs != rt.getNumLenParams()) 532 return op.emitOpError("number of LEN params does not correspond" 533 " to the !fir.type type"); 534 } else if (auto strTy = eleTy.dyn_cast<fir::CharacterType>()) { 535 if (strTy.getLen() != fir::CharacterType::unknownLen()) 536 return op.emitOpError("CHARACTER already has static LEN"); 537 } else { 538 return op.emitOpError("LEN parameters require CHARACTER or derived type"); 539 } 540 for (auto lp : op.typeparams()) 541 if (!fir::isa_integer(lp.getType())) 542 return op.emitOpError("LEN parameters must be integral type"); 543 } 544 if (op.getShape() && !isArray) 545 return op.emitOpError("shape must not be provided for a scalar"); 546 if (op.getSlice() && !isArray) 547 return op.emitOpError("slice must not be provided for a scalar"); 548 return mlir::success(); 549 } 550 551 //===----------------------------------------------------------------------===// 552 // GenTypeDescOp 553 //===----------------------------------------------------------------------===// 554 555 void fir::GenTypeDescOp::build(OpBuilder &, OperationState &result, 556 mlir::TypeAttr inty) { 557 result.addAttribute("in_type", inty); 558 result.addTypes(TypeDescType::get(inty.getValue())); 559 } 560 561 //===----------------------------------------------------------------------===// 562 // GlobalOp 563 //===----------------------------------------------------------------------===// 564 565 static ParseResult parseGlobalOp(OpAsmParser &parser, OperationState &result) { 566 // Parse the optional linkage 567 llvm::StringRef linkage; 568 auto &builder = parser.getBuilder(); 569 if (mlir::succeeded(parser.parseOptionalKeyword(&linkage))) { 570 if (fir::GlobalOp::verifyValidLinkage(linkage)) 571 return mlir::failure(); 572 mlir::StringAttr linkAttr = builder.getStringAttr(linkage); 573 result.addAttribute(fir::GlobalOp::linkageAttrName(), linkAttr); 574 } 575 576 // Parse the name as a symbol reference attribute. 577 mlir::SymbolRefAttr nameAttr; 578 if (parser.parseAttribute(nameAttr, fir::GlobalOp::symbolAttrName(), 579 result.attributes)) 580 return mlir::failure(); 581 result.addAttribute(mlir::SymbolTable::getSymbolAttrName(), 582 nameAttr.getRootReference()); 583 584 bool simpleInitializer = false; 585 if (mlir::succeeded(parser.parseOptionalLParen())) { 586 Attribute attr; 587 if (parser.parseAttribute(attr, "initVal", result.attributes) || 588 parser.parseRParen()) 589 return mlir::failure(); 590 simpleInitializer = true; 591 } 592 593 if (succeeded(parser.parseOptionalKeyword("constant"))) { 594 // if "constant" keyword then mark this as a constant, not a variable 595 result.addAttribute("constant", builder.getUnitAttr()); 596 } 597 598 mlir::Type globalType; 599 if (parser.parseColonType(globalType)) 600 return mlir::failure(); 601 602 result.addAttribute(fir::GlobalOp::typeAttrName(result.name), 603 mlir::TypeAttr::get(globalType)); 604 605 if (simpleInitializer) { 606 result.addRegion(); 607 } else { 608 // Parse the optional initializer body. 609 auto parseResult = parser.parseOptionalRegion( 610 *result.addRegion(), /*arguments=*/llvm::None, /*argTypes=*/llvm::None); 611 if (parseResult.hasValue() && mlir::failed(*parseResult)) 612 return mlir::failure(); 613 } 614 615 return mlir::success(); 616 } 617 618 void fir::GlobalOp::appendInitialValue(mlir::Operation *op) { 619 getBlock().getOperations().push_back(op); 620 } 621 622 void fir::GlobalOp::build(mlir::OpBuilder &builder, OperationState &result, 623 StringRef name, bool isConstant, Type type, 624 Attribute initialVal, StringAttr linkage, 625 ArrayRef<NamedAttribute> attrs) { 626 result.addRegion(); 627 result.addAttribute(typeAttrName(result.name), mlir::TypeAttr::get(type)); 628 result.addAttribute(mlir::SymbolTable::getSymbolAttrName(), 629 builder.getStringAttr(name)); 630 result.addAttribute(symbolAttrName(), 631 SymbolRefAttr::get(builder.getContext(), name)); 632 if (isConstant) 633 result.addAttribute(constantAttrName(result.name), builder.getUnitAttr()); 634 if (initialVal) 635 result.addAttribute(initValAttrName(result.name), initialVal); 636 if (linkage) 637 result.addAttribute(linkageAttrName(), linkage); 638 result.attributes.append(attrs.begin(), attrs.end()); 639 } 640 641 void fir::GlobalOp::build(mlir::OpBuilder &builder, OperationState &result, 642 StringRef name, Type type, Attribute initialVal, 643 StringAttr linkage, ArrayRef<NamedAttribute> attrs) { 644 build(builder, result, name, /*isConstant=*/false, type, {}, linkage, attrs); 645 } 646 647 void fir::GlobalOp::build(mlir::OpBuilder &builder, OperationState &result, 648 StringRef name, bool isConstant, Type type, 649 StringAttr linkage, ArrayRef<NamedAttribute> attrs) { 650 build(builder, result, name, isConstant, type, {}, linkage, attrs); 651 } 652 653 void fir::GlobalOp::build(mlir::OpBuilder &builder, OperationState &result, 654 StringRef name, Type type, StringAttr linkage, 655 ArrayRef<NamedAttribute> attrs) { 656 build(builder, result, name, /*isConstant=*/false, type, {}, linkage, attrs); 657 } 658 659 void fir::GlobalOp::build(mlir::OpBuilder &builder, OperationState &result, 660 StringRef name, bool isConstant, Type type, 661 ArrayRef<NamedAttribute> attrs) { 662 build(builder, result, name, isConstant, type, StringAttr{}, attrs); 663 } 664 665 void fir::GlobalOp::build(mlir::OpBuilder &builder, OperationState &result, 666 StringRef name, Type type, 667 ArrayRef<NamedAttribute> attrs) { 668 build(builder, result, name, /*isConstant=*/false, type, attrs); 669 } 670 671 mlir::ParseResult fir::GlobalOp::verifyValidLinkage(StringRef linkage) { 672 // Supporting only a subset of the LLVM linkage types for now 673 static const char *validNames[] = {"common", "internal", "linkonce", "weak"}; 674 return mlir::success(llvm::is_contained(validNames, linkage)); 675 } 676 677 template <bool AllowFields> 678 static void appendAsAttribute(llvm::SmallVectorImpl<mlir::Attribute> &attrs, 679 mlir::Value val) { 680 if (auto *op = val.getDefiningOp()) { 681 if (auto cop = mlir::dyn_cast<mlir::ConstantOp>(op)) { 682 // append the integer constant value 683 if (auto iattr = cop.getValue().dyn_cast<mlir::IntegerAttr>()) { 684 attrs.push_back(iattr); 685 return; 686 } 687 } else if (auto fld = mlir::dyn_cast<fir::FieldIndexOp>(op)) { 688 if constexpr (AllowFields) { 689 // append the field name and the record type 690 attrs.push_back(fld.field_idAttr()); 691 attrs.push_back(fld.on_typeAttr()); 692 return; 693 } 694 } 695 } 696 llvm::report_fatal_error("cannot build Op with these arguments"); 697 } 698 699 template <bool AllowFields = true> 700 static mlir::ArrayAttr collectAsAttributes(mlir::MLIRContext *ctxt, 701 OperationState &result, 702 llvm::ArrayRef<mlir::Value> inds) { 703 llvm::SmallVector<mlir::Attribute> attrs; 704 for (auto v : inds) 705 appendAsAttribute<AllowFields>(attrs, v); 706 assert(!attrs.empty()); 707 return mlir::ArrayAttr::get(ctxt, attrs); 708 } 709 710 //===----------------------------------------------------------------------===// 711 // InsertOnRangeOp 712 //===----------------------------------------------------------------------===// 713 714 void fir::InsertOnRangeOp::build(mlir::OpBuilder &builder, 715 OperationState &result, mlir::Type resTy, 716 mlir::Value aggVal, mlir::Value eleVal, 717 llvm::ArrayRef<mlir::Value> inds) { 718 auto aa = collectAsAttributes<false>(builder.getContext(), result, inds); 719 build(builder, result, resTy, aggVal, eleVal, aa); 720 } 721 722 /// Range bounds must be nonnegative, and the range must not be empty. 723 static mlir::LogicalResult verify(fir::InsertOnRangeOp op) { 724 if (op.coor().size() < 2 || op.coor().size() % 2 != 0) 725 return op.emitOpError("has uneven number of values in ranges"); 726 bool rangeIsKnownToBeNonempty = false; 727 for (auto i = op.coor().end(), b = op.coor().begin(); i != b;) { 728 int64_t ub = (*--i).cast<IntegerAttr>().getInt(); 729 int64_t lb = (*--i).cast<IntegerAttr>().getInt(); 730 if (lb < 0 || ub < 0) 731 return op.emitOpError("negative range bound"); 732 if (rangeIsKnownToBeNonempty) 733 continue; 734 if (lb > ub) 735 return op.emitOpError("empty range"); 736 rangeIsKnownToBeNonempty = lb < ub; 737 } 738 return mlir::success(); 739 } 740 741 //===----------------------------------------------------------------------===// 742 // InsertValueOp 743 //===----------------------------------------------------------------------===// 744 745 static bool checkIsIntegerConstant(mlir::Value v, int64_t conVal) { 746 if (auto c = dyn_cast_or_null<mlir::ConstantOp>(v.getDefiningOp())) { 747 auto attr = c.getValue(); 748 if (auto iattr = attr.dyn_cast<mlir::IntegerAttr>()) 749 return iattr.getInt() == conVal; 750 } 751 return false; 752 } 753 static bool isZero(mlir::Value v) { return checkIsIntegerConstant(v, 0); } 754 static bool isOne(mlir::Value v) { return checkIsIntegerConstant(v, 1); } 755 756 // Undo some complex patterns created in the front-end and turn them back into 757 // complex ops. 758 template <typename FltOp, typename CpxOp> 759 struct UndoComplexPattern : public mlir::RewritePattern { 760 UndoComplexPattern(mlir::MLIRContext *ctx) 761 : mlir::RewritePattern("fir.insert_value", 2, ctx) {} 762 763 mlir::LogicalResult 764 matchAndRewrite(mlir::Operation *op, 765 mlir::PatternRewriter &rewriter) const override { 766 auto insval = dyn_cast_or_null<fir::InsertValueOp>(op); 767 if (!insval || !insval.getType().isa<fir::ComplexType>()) 768 return mlir::failure(); 769 auto insval2 = 770 dyn_cast_or_null<fir::InsertValueOp>(insval.adt().getDefiningOp()); 771 if (!insval2 || !isa<fir::UndefOp>(insval2.adt().getDefiningOp())) 772 return mlir::failure(); 773 auto binf = dyn_cast_or_null<FltOp>(insval.val().getDefiningOp()); 774 auto binf2 = dyn_cast_or_null<FltOp>(insval2.val().getDefiningOp()); 775 if (!binf || !binf2 || insval.coor().size() != 1 || 776 !isOne(insval.coor()[0]) || insval2.coor().size() != 1 || 777 !isZero(insval2.coor()[0])) 778 return mlir::failure(); 779 auto eai = 780 dyn_cast_or_null<fir::ExtractValueOp>(binf.lhs().getDefiningOp()); 781 auto ebi = 782 dyn_cast_or_null<fir::ExtractValueOp>(binf.rhs().getDefiningOp()); 783 auto ear = 784 dyn_cast_or_null<fir::ExtractValueOp>(binf2.lhs().getDefiningOp()); 785 auto ebr = 786 dyn_cast_or_null<fir::ExtractValueOp>(binf2.rhs().getDefiningOp()); 787 if (!eai || !ebi || !ear || !ebr || ear.adt() != eai.adt() || 788 ebr.adt() != ebi.adt() || eai.coor().size() != 1 || 789 !isOne(eai.coor()[0]) || ebi.coor().size() != 1 || 790 !isOne(ebi.coor()[0]) || ear.coor().size() != 1 || 791 !isZero(ear.coor()[0]) || ebr.coor().size() != 1 || 792 !isZero(ebr.coor()[0])) 793 return mlir::failure(); 794 rewriter.replaceOpWithNewOp<CpxOp>(op, ear.adt(), ebr.adt()); 795 return mlir::success(); 796 } 797 }; 798 799 void fir::InsertValueOp::getCanonicalizationPatterns( 800 mlir::OwningRewritePatternList &results, mlir::MLIRContext *context) { 801 results.insert<UndoComplexPattern<mlir::AddFOp, fir::AddcOp>, 802 UndoComplexPattern<mlir::SubFOp, fir::SubcOp>>(context); 803 } 804 805 //===----------------------------------------------------------------------===// 806 // IterWhileOp 807 //===----------------------------------------------------------------------===// 808 809 void fir::IterWhileOp::build(mlir::OpBuilder &builder, 810 mlir::OperationState &result, mlir::Value lb, 811 mlir::Value ub, mlir::Value step, 812 mlir::Value iterate, bool finalCountValue, 813 mlir::ValueRange iterArgs, 814 llvm::ArrayRef<mlir::NamedAttribute> attributes) { 815 result.addOperands({lb, ub, step, iterate}); 816 if (finalCountValue) { 817 result.addTypes(builder.getIndexType()); 818 result.addAttribute(getFinalValueAttrName(), builder.getUnitAttr()); 819 } 820 result.addTypes(iterate.getType()); 821 result.addOperands(iterArgs); 822 for (auto v : iterArgs) 823 result.addTypes(v.getType()); 824 mlir::Region *bodyRegion = result.addRegion(); 825 bodyRegion->push_back(new Block{}); 826 bodyRegion->front().addArgument(builder.getIndexType()); 827 bodyRegion->front().addArgument(iterate.getType()); 828 bodyRegion->front().addArguments(iterArgs.getTypes()); 829 result.addAttributes(attributes); 830 } 831 832 static mlir::ParseResult parseIterWhileOp(mlir::OpAsmParser &parser, 833 mlir::OperationState &result) { 834 auto &builder = parser.getBuilder(); 835 mlir::OpAsmParser::OperandType inductionVariable, lb, ub, step; 836 if (parser.parseLParen() || parser.parseRegionArgument(inductionVariable) || 837 parser.parseEqual()) 838 return mlir::failure(); 839 840 // Parse loop bounds. 841 auto indexType = builder.getIndexType(); 842 auto i1Type = builder.getIntegerType(1); 843 if (parser.parseOperand(lb) || 844 parser.resolveOperand(lb, indexType, result.operands) || 845 parser.parseKeyword("to") || parser.parseOperand(ub) || 846 parser.resolveOperand(ub, indexType, result.operands) || 847 parser.parseKeyword("step") || parser.parseOperand(step) || 848 parser.parseRParen() || 849 parser.resolveOperand(step, indexType, result.operands)) 850 return mlir::failure(); 851 852 mlir::OpAsmParser::OperandType iterateVar, iterateInput; 853 if (parser.parseKeyword("and") || parser.parseLParen() || 854 parser.parseRegionArgument(iterateVar) || parser.parseEqual() || 855 parser.parseOperand(iterateInput) || parser.parseRParen() || 856 parser.resolveOperand(iterateInput, i1Type, result.operands)) 857 return mlir::failure(); 858 859 // Parse the initial iteration arguments. 860 llvm::SmallVector<mlir::OpAsmParser::OperandType> regionArgs; 861 auto prependCount = false; 862 863 // Induction variable. 864 regionArgs.push_back(inductionVariable); 865 regionArgs.push_back(iterateVar); 866 867 if (succeeded(parser.parseOptionalKeyword("iter_args"))) { 868 llvm::SmallVector<mlir::OpAsmParser::OperandType> operands; 869 llvm::SmallVector<mlir::Type> regionTypes; 870 // Parse assignment list and results type list. 871 if (parser.parseAssignmentList(regionArgs, operands) || 872 parser.parseArrowTypeList(regionTypes)) 873 return failure(); 874 if (regionTypes.size() == operands.size() + 2) 875 prependCount = true; 876 llvm::ArrayRef<mlir::Type> resTypes = regionTypes; 877 resTypes = prependCount ? resTypes.drop_front(2) : resTypes; 878 // Resolve input operands. 879 for (auto operandType : llvm::zip(operands, resTypes)) 880 if (parser.resolveOperand(std::get<0>(operandType), 881 std::get<1>(operandType), result.operands)) 882 return failure(); 883 if (prependCount) { 884 result.addTypes(regionTypes); 885 } else { 886 result.addTypes(i1Type); 887 result.addTypes(resTypes); 888 } 889 } else if (succeeded(parser.parseOptionalArrow())) { 890 llvm::SmallVector<mlir::Type> typeList; 891 if (parser.parseLParen() || parser.parseTypeList(typeList) || 892 parser.parseRParen()) 893 return failure(); 894 // Type list must be "(index, i1)". 895 if (typeList.size() != 2 || !typeList[0].isa<mlir::IndexType>() || 896 !typeList[1].isSignlessInteger(1)) 897 return failure(); 898 result.addTypes(typeList); 899 prependCount = true; 900 } else { 901 result.addTypes(i1Type); 902 } 903 904 if (parser.parseOptionalAttrDictWithKeyword(result.attributes)) 905 return mlir::failure(); 906 907 llvm::SmallVector<mlir::Type> argTypes; 908 // Induction variable (hidden) 909 if (prependCount) 910 result.addAttribute(IterWhileOp::getFinalValueAttrName(), 911 builder.getUnitAttr()); 912 else 913 argTypes.push_back(indexType); 914 // Loop carried variables (including iterate) 915 argTypes.append(result.types.begin(), result.types.end()); 916 // Parse the body region. 917 auto *body = result.addRegion(); 918 if (regionArgs.size() != argTypes.size()) 919 return parser.emitError( 920 parser.getNameLoc(), 921 "mismatch in number of loop-carried values and defined values"); 922 923 if (parser.parseRegion(*body, regionArgs, argTypes)) 924 return failure(); 925 926 fir::IterWhileOp::ensureTerminator(*body, builder, result.location); 927 928 return mlir::success(); 929 } 930 931 static mlir::LogicalResult verify(fir::IterWhileOp op) { 932 // Check that the body defines as single block argument for the induction 933 // variable. 934 auto *body = op.getBody(); 935 if (!body->getArgument(1).getType().isInteger(1)) 936 return op.emitOpError( 937 "expected body second argument to be an index argument for " 938 "the induction variable"); 939 if (!body->getArgument(0).getType().isIndex()) 940 return op.emitOpError( 941 "expected body first argument to be an index argument for " 942 "the induction variable"); 943 944 auto opNumResults = op.getNumResults(); 945 if (op.finalValue()) { 946 // Result type must be "(index, i1, ...)". 947 if (!op.getResult(0).getType().isa<mlir::IndexType>()) 948 return op.emitOpError("result #0 expected to be index"); 949 if (!op.getResult(1).getType().isSignlessInteger(1)) 950 return op.emitOpError("result #1 expected to be i1"); 951 opNumResults--; 952 } else { 953 // iterate_while always returns the early exit induction value. 954 // Result type must be "(i1, ...)" 955 if (!op.getResult(0).getType().isSignlessInteger(1)) 956 return op.emitOpError("result #0 expected to be i1"); 957 } 958 if (opNumResults == 0) 959 return mlir::failure(); 960 if (op.getNumIterOperands() != opNumResults) 961 return op.emitOpError( 962 "mismatch in number of loop-carried values and defined values"); 963 if (op.getNumRegionIterArgs() != opNumResults) 964 return op.emitOpError( 965 "mismatch in number of basic block args and defined values"); 966 auto iterOperands = op.getIterOperands(); 967 auto iterArgs = op.getRegionIterArgs(); 968 auto opResults = 969 op.finalValue() ? op.getResults().drop_front() : op.getResults(); 970 unsigned i = 0; 971 for (auto e : llvm::zip(iterOperands, iterArgs, opResults)) { 972 if (std::get<0>(e).getType() != std::get<2>(e).getType()) 973 return op.emitOpError() << "types mismatch between " << i 974 << "th iter operand and defined value"; 975 if (std::get<1>(e).getType() != std::get<2>(e).getType()) 976 return op.emitOpError() << "types mismatch between " << i 977 << "th iter region arg and defined value"; 978 979 i++; 980 } 981 return mlir::success(); 982 } 983 984 static void print(mlir::OpAsmPrinter &p, fir::IterWhileOp op) { 985 p << " (" << op.getInductionVar() << " = " << op.lowerBound() << " to " 986 << op.upperBound() << " step " << op.step() << ") and ("; 987 assert(op.hasIterOperands()); 988 auto regionArgs = op.getRegionIterArgs(); 989 auto operands = op.getIterOperands(); 990 p << regionArgs.front() << " = " << *operands.begin() << ")"; 991 if (regionArgs.size() > 1) { 992 p << " iter_args("; 993 llvm::interleaveComma( 994 llvm::zip(regionArgs.drop_front(), operands.drop_front()), p, 995 [&](auto it) { p << std::get<0>(it) << " = " << std::get<1>(it); }); 996 p << ") -> ("; 997 llvm::interleaveComma( 998 llvm::drop_begin(op.getResultTypes(), op.finalValue() ? 0 : 1), p); 999 p << ")"; 1000 } else if (op.finalValue()) { 1001 p << " -> (" << op.getResultTypes() << ')'; 1002 } 1003 p.printOptionalAttrDictWithKeyword(op->getAttrs(), 1004 {IterWhileOp::getFinalValueAttrName()}); 1005 p.printRegion(op.region(), /*printEntryBlockArgs=*/false, 1006 /*printBlockTerminators=*/true); 1007 } 1008 1009 mlir::Region &fir::IterWhileOp::getLoopBody() { return region(); } 1010 1011 bool fir::IterWhileOp::isDefinedOutsideOfLoop(mlir::Value value) { 1012 return !region().isAncestor(value.getParentRegion()); 1013 } 1014 1015 mlir::LogicalResult 1016 fir::IterWhileOp::moveOutOfLoop(llvm::ArrayRef<mlir::Operation *> ops) { 1017 for (auto *op : ops) 1018 op->moveBefore(*this); 1019 return success(); 1020 } 1021 1022 mlir::BlockArgument fir::IterWhileOp::iterArgToBlockArg(mlir::Value iterArg) { 1023 for (auto i : llvm::enumerate(initArgs())) 1024 if (iterArg == i.value()) 1025 return region().front().getArgument(i.index() + 1); 1026 return {}; 1027 } 1028 1029 void fir::IterWhileOp::resultToSourceOps( 1030 llvm::SmallVectorImpl<mlir::Value> &results, unsigned resultNum) { 1031 auto oper = finalValue() ? resultNum + 1 : resultNum; 1032 auto *term = region().front().getTerminator(); 1033 if (oper < term->getNumOperands()) 1034 results.push_back(term->getOperand(oper)); 1035 } 1036 1037 mlir::Value fir::IterWhileOp::blockArgToSourceOp(unsigned blockArgNum) { 1038 if (blockArgNum > 0 && blockArgNum <= initArgs().size()) 1039 return initArgs()[blockArgNum - 1]; 1040 return {}; 1041 } 1042 1043 //===----------------------------------------------------------------------===// 1044 // LoadOp 1045 //===----------------------------------------------------------------------===// 1046 1047 /// Get the element type of a reference like type; otherwise null 1048 static mlir::Type elementTypeOf(mlir::Type ref) { 1049 return llvm::TypeSwitch<mlir::Type, mlir::Type>(ref) 1050 .Case<ReferenceType, PointerType, HeapType>( 1051 [](auto type) { return type.getEleTy(); }) 1052 .Default([](mlir::Type) { return mlir::Type{}; }); 1053 } 1054 1055 mlir::ParseResult fir::LoadOp::getElementOf(mlir::Type &ele, mlir::Type ref) { 1056 if ((ele = elementTypeOf(ref))) 1057 return mlir::success(); 1058 return mlir::failure(); 1059 } 1060 1061 //===----------------------------------------------------------------------===// 1062 // DoLoopOp 1063 //===----------------------------------------------------------------------===// 1064 1065 void fir::DoLoopOp::build(mlir::OpBuilder &builder, 1066 mlir::OperationState &result, mlir::Value lb, 1067 mlir::Value ub, mlir::Value step, bool unordered, 1068 bool finalCountValue, mlir::ValueRange iterArgs, 1069 llvm::ArrayRef<mlir::NamedAttribute> attributes) { 1070 result.addOperands({lb, ub, step}); 1071 result.addOperands(iterArgs); 1072 if (finalCountValue) { 1073 result.addTypes(builder.getIndexType()); 1074 result.addAttribute(finalValueAttrName(result.name), builder.getUnitAttr()); 1075 } 1076 for (auto v : iterArgs) 1077 result.addTypes(v.getType()); 1078 mlir::Region *bodyRegion = result.addRegion(); 1079 bodyRegion->push_back(new Block{}); 1080 if (iterArgs.empty() && !finalCountValue) 1081 DoLoopOp::ensureTerminator(*bodyRegion, builder, result.location); 1082 bodyRegion->front().addArgument(builder.getIndexType()); 1083 bodyRegion->front().addArguments(iterArgs.getTypes()); 1084 if (unordered) 1085 result.addAttribute(unorderedAttrName(result.name), builder.getUnitAttr()); 1086 result.addAttributes(attributes); 1087 } 1088 1089 static mlir::ParseResult parseDoLoopOp(mlir::OpAsmParser &parser, 1090 mlir::OperationState &result) { 1091 auto &builder = parser.getBuilder(); 1092 mlir::OpAsmParser::OperandType inductionVariable, lb, ub, step; 1093 // Parse the induction variable followed by '='. 1094 if (parser.parseRegionArgument(inductionVariable) || parser.parseEqual()) 1095 return mlir::failure(); 1096 1097 // Parse loop bounds. 1098 auto indexType = builder.getIndexType(); 1099 if (parser.parseOperand(lb) || 1100 parser.resolveOperand(lb, indexType, result.operands) || 1101 parser.parseKeyword("to") || parser.parseOperand(ub) || 1102 parser.resolveOperand(ub, indexType, result.operands) || 1103 parser.parseKeyword("step") || parser.parseOperand(step) || 1104 parser.resolveOperand(step, indexType, result.operands)) 1105 return failure(); 1106 1107 if (mlir::succeeded(parser.parseOptionalKeyword("unordered"))) 1108 result.addAttribute("unordered", builder.getUnitAttr()); 1109 1110 // Parse the optional initial iteration arguments. 1111 llvm::SmallVector<mlir::OpAsmParser::OperandType, 4> regionArgs, operands; 1112 llvm::SmallVector<mlir::Type, 4> argTypes; 1113 auto prependCount = false; 1114 regionArgs.push_back(inductionVariable); 1115 1116 if (succeeded(parser.parseOptionalKeyword("iter_args"))) { 1117 // Parse assignment list and results type list. 1118 if (parser.parseAssignmentList(regionArgs, operands) || 1119 parser.parseArrowTypeList(result.types)) 1120 return failure(); 1121 if (result.types.size() == operands.size() + 1) 1122 prependCount = true; 1123 // Resolve input operands. 1124 llvm::ArrayRef<mlir::Type> resTypes = result.types; 1125 for (auto operand_type : 1126 llvm::zip(operands, prependCount ? resTypes.drop_front() : resTypes)) 1127 if (parser.resolveOperand(std::get<0>(operand_type), 1128 std::get<1>(operand_type), result.operands)) 1129 return failure(); 1130 } else if (succeeded(parser.parseOptionalArrow())) { 1131 if (parser.parseKeyword("index")) 1132 return failure(); 1133 result.types.push_back(indexType); 1134 prependCount = true; 1135 } 1136 1137 if (parser.parseOptionalAttrDictWithKeyword(result.attributes)) 1138 return mlir::failure(); 1139 1140 // Induction variable. 1141 if (prependCount) 1142 result.addAttribute(DoLoopOp::finalValueAttrName(result.name), 1143 builder.getUnitAttr()); 1144 else 1145 argTypes.push_back(indexType); 1146 // Loop carried variables 1147 argTypes.append(result.types.begin(), result.types.end()); 1148 // Parse the body region. 1149 auto *body = result.addRegion(); 1150 if (regionArgs.size() != argTypes.size()) 1151 return parser.emitError( 1152 parser.getNameLoc(), 1153 "mismatch in number of loop-carried values and defined values"); 1154 1155 if (parser.parseRegion(*body, regionArgs, argTypes)) 1156 return failure(); 1157 1158 DoLoopOp::ensureTerminator(*body, builder, result.location); 1159 1160 return mlir::success(); 1161 } 1162 1163 fir::DoLoopOp fir::getForInductionVarOwner(mlir::Value val) { 1164 auto ivArg = val.dyn_cast<mlir::BlockArgument>(); 1165 if (!ivArg) 1166 return {}; 1167 assert(ivArg.getOwner() && "unlinked block argument"); 1168 auto *containingInst = ivArg.getOwner()->getParentOp(); 1169 return dyn_cast_or_null<fir::DoLoopOp>(containingInst); 1170 } 1171 1172 // Lifted from loop.loop 1173 static mlir::LogicalResult verify(fir::DoLoopOp op) { 1174 // Check that the body defines as single block argument for the induction 1175 // variable. 1176 auto *body = op.getBody(); 1177 if (!body->getArgument(0).getType().isIndex()) 1178 return op.emitOpError( 1179 "expected body first argument to be an index argument for " 1180 "the induction variable"); 1181 1182 auto opNumResults = op.getNumResults(); 1183 if (opNumResults == 0) 1184 return success(); 1185 1186 if (op.finalValue()) { 1187 if (op.unordered()) 1188 return op.emitOpError("unordered loop has no final value"); 1189 opNumResults--; 1190 } 1191 if (op.getNumIterOperands() != opNumResults) 1192 return op.emitOpError( 1193 "mismatch in number of loop-carried values and defined values"); 1194 if (op.getNumRegionIterArgs() != opNumResults) 1195 return op.emitOpError( 1196 "mismatch in number of basic block args and defined values"); 1197 auto iterOperands = op.getIterOperands(); 1198 auto iterArgs = op.getRegionIterArgs(); 1199 auto opResults = 1200 op.finalValue() ? op.getResults().drop_front() : op.getResults(); 1201 unsigned i = 0; 1202 for (auto e : llvm::zip(iterOperands, iterArgs, opResults)) { 1203 if (std::get<0>(e).getType() != std::get<2>(e).getType()) 1204 return op.emitOpError() << "types mismatch between " << i 1205 << "th iter operand and defined value"; 1206 if (std::get<1>(e).getType() != std::get<2>(e).getType()) 1207 return op.emitOpError() << "types mismatch between " << i 1208 << "th iter region arg and defined value"; 1209 1210 i++; 1211 } 1212 return success(); 1213 } 1214 1215 static void print(mlir::OpAsmPrinter &p, fir::DoLoopOp op) { 1216 bool printBlockTerminators = false; 1217 p << ' ' << op.getInductionVar() << " = " << op.lowerBound() << " to " 1218 << op.upperBound() << " step " << op.step(); 1219 if (op.unordered()) 1220 p << " unordered"; 1221 if (op.hasIterOperands()) { 1222 p << " iter_args("; 1223 auto regionArgs = op.getRegionIterArgs(); 1224 auto operands = op.getIterOperands(); 1225 llvm::interleaveComma(llvm::zip(regionArgs, operands), p, [&](auto it) { 1226 p << std::get<0>(it) << " = " << std::get<1>(it); 1227 }); 1228 p << ") -> (" << op.getResultTypes() << ')'; 1229 printBlockTerminators = true; 1230 } else if (op.finalValue()) { 1231 p << " -> " << op.getResultTypes(); 1232 printBlockTerminators = true; 1233 } 1234 p.printOptionalAttrDictWithKeyword(op->getAttrs(), 1235 {"unordered", "finalValue"}); 1236 p.printRegion(op.region(), /*printEntryBlockArgs=*/false, 1237 printBlockTerminators); 1238 } 1239 1240 mlir::Region &fir::DoLoopOp::getLoopBody() { return region(); } 1241 1242 bool fir::DoLoopOp::isDefinedOutsideOfLoop(mlir::Value value) { 1243 return !region().isAncestor(value.getParentRegion()); 1244 } 1245 1246 mlir::LogicalResult 1247 fir::DoLoopOp::moveOutOfLoop(llvm::ArrayRef<mlir::Operation *> ops) { 1248 for (auto op : ops) 1249 op->moveBefore(*this); 1250 return success(); 1251 } 1252 1253 /// Translate a value passed as an iter_arg to the corresponding block 1254 /// argument in the body of the loop. 1255 mlir::BlockArgument fir::DoLoopOp::iterArgToBlockArg(mlir::Value iterArg) { 1256 for (auto i : llvm::enumerate(initArgs())) 1257 if (iterArg == i.value()) 1258 return region().front().getArgument(i.index() + 1); 1259 return {}; 1260 } 1261 1262 /// Translate the result vector (by index number) to the corresponding value 1263 /// to the `fir.result` Op. 1264 void fir::DoLoopOp::resultToSourceOps( 1265 llvm::SmallVectorImpl<mlir::Value> &results, unsigned resultNum) { 1266 auto oper = finalValue() ? resultNum + 1 : resultNum; 1267 auto *term = region().front().getTerminator(); 1268 if (oper < term->getNumOperands()) 1269 results.push_back(term->getOperand(oper)); 1270 } 1271 1272 /// Translate the block argument (by index number) to the corresponding value 1273 /// passed as an iter_arg to the parent DoLoopOp. 1274 mlir::Value fir::DoLoopOp::blockArgToSourceOp(unsigned blockArgNum) { 1275 if (blockArgNum > 0 && blockArgNum <= initArgs().size()) 1276 return initArgs()[blockArgNum - 1]; 1277 return {}; 1278 } 1279 1280 //===----------------------------------------------------------------------===// 1281 // ReboxOp 1282 //===----------------------------------------------------------------------===// 1283 1284 /// Get the scalar type related to a fir.box type. 1285 /// Example: return f32 for !fir.box<!fir.heap<!fir.array<?x?xf32>>. 1286 static mlir::Type getBoxScalarEleTy(mlir::Type boxTy) { 1287 auto eleTy = fir::dyn_cast_ptrOrBoxEleTy(boxTy); 1288 if (auto seqTy = eleTy.dyn_cast<fir::SequenceType>()) 1289 return seqTy.getEleTy(); 1290 return eleTy; 1291 } 1292 1293 /// Get the rank from a !fir.box type 1294 static unsigned getBoxRank(mlir::Type boxTy) { 1295 auto eleTy = fir::dyn_cast_ptrOrBoxEleTy(boxTy); 1296 if (auto seqTy = eleTy.dyn_cast<fir::SequenceType>()) 1297 return seqTy.getDimension(); 1298 return 0; 1299 } 1300 1301 static mlir::LogicalResult verify(fir::ReboxOp op) { 1302 auto inputBoxTy = op.box().getType(); 1303 if (fir::isa_unknown_size_box(inputBoxTy)) 1304 return op.emitOpError("box operand must not have unknown rank or type"); 1305 auto outBoxTy = op.getType(); 1306 if (fir::isa_unknown_size_box(outBoxTy)) 1307 return op.emitOpError("result type must not have unknown rank or type"); 1308 auto inputRank = getBoxRank(inputBoxTy); 1309 auto inputEleTy = getBoxScalarEleTy(inputBoxTy); 1310 auto outRank = getBoxRank(outBoxTy); 1311 auto outEleTy = getBoxScalarEleTy(outBoxTy); 1312 1313 if (auto slice = op.slice()) { 1314 // Slicing case 1315 if (slice.getType().cast<fir::SliceType>().getRank() != inputRank) 1316 return op.emitOpError("slice operand rank must match box operand rank"); 1317 if (auto shape = op.shape()) { 1318 if (auto shiftTy = shape.getType().dyn_cast<fir::ShiftType>()) { 1319 if (shiftTy.getRank() != inputRank) 1320 return op.emitOpError("shape operand and input box ranks must match " 1321 "when there is a slice"); 1322 } else { 1323 return op.emitOpError("shape operand must absent or be a fir.shift " 1324 "when there is a slice"); 1325 } 1326 } 1327 if (auto sliceOp = slice.getDefiningOp()) { 1328 auto slicedRank = mlir::cast<fir::SliceOp>(sliceOp).getOutRank(); 1329 if (slicedRank != outRank) 1330 return op.emitOpError("result type rank and rank after applying slice " 1331 "operand must match"); 1332 } 1333 } else { 1334 // Reshaping case 1335 unsigned shapeRank = inputRank; 1336 if (auto shape = op.shape()) { 1337 auto ty = shape.getType(); 1338 if (auto shapeTy = ty.dyn_cast<fir::ShapeType>()) { 1339 shapeRank = shapeTy.getRank(); 1340 } else if (auto shapeShiftTy = ty.dyn_cast<fir::ShapeShiftType>()) { 1341 shapeRank = shapeShiftTy.getRank(); 1342 } else { 1343 auto shiftTy = ty.cast<fir::ShiftType>(); 1344 shapeRank = shiftTy.getRank(); 1345 if (shapeRank != inputRank) 1346 return op.emitOpError("shape operand and input box ranks must match " 1347 "when the shape is a fir.shift"); 1348 } 1349 } 1350 if (shapeRank != outRank) 1351 return op.emitOpError("result type and shape operand ranks must match"); 1352 } 1353 1354 if (inputEleTy != outEleTy) 1355 // TODO: check that outBoxTy is a parent type of inputBoxTy for derived 1356 // types. 1357 if (!inputEleTy.isa<fir::RecordType>()) 1358 return op.emitOpError( 1359 "op input and output element types must match for intrinsic types"); 1360 return mlir::success(); 1361 } 1362 1363 //===----------------------------------------------------------------------===// 1364 // ResultOp 1365 //===----------------------------------------------------------------------===// 1366 1367 static mlir::LogicalResult verify(fir::ResultOp op) { 1368 auto *parentOp = op->getParentOp(); 1369 auto results = parentOp->getResults(); 1370 auto operands = op->getOperands(); 1371 1372 if (parentOp->getNumResults() != op.getNumOperands()) 1373 return op.emitOpError() << "parent of result must have same arity"; 1374 for (auto e : llvm::zip(results, operands)) 1375 if (std::get<0>(e).getType() != std::get<1>(e).getType()) 1376 return op.emitOpError() 1377 << "types mismatch between result op and its parent"; 1378 return success(); 1379 } 1380 1381 //===----------------------------------------------------------------------===// 1382 // SaveResultOp 1383 //===----------------------------------------------------------------------===// 1384 1385 static mlir::LogicalResult verify(fir::SaveResultOp op) { 1386 auto resultType = op.value().getType(); 1387 if (resultType != fir::dyn_cast_ptrEleTy(op.memref().getType())) 1388 return op.emitOpError("value type must match memory reference type"); 1389 if (fir::isa_unknown_size_box(resultType)) 1390 return op.emitOpError("cannot save !fir.box of unknown rank or type"); 1391 1392 if (resultType.isa<fir::BoxType>()) { 1393 if (op.shape() || !op.typeparams().empty()) 1394 return op.emitOpError( 1395 "must not have shape or length operands if the value is a fir.box"); 1396 return mlir::success(); 1397 } 1398 1399 // fir.record or fir.array case. 1400 unsigned shapeTyRank = 0; 1401 if (auto shapeOp = op.shape()) { 1402 auto shapeTy = shapeOp.getType(); 1403 if (auto s = shapeTy.dyn_cast<fir::ShapeType>()) 1404 shapeTyRank = s.getRank(); 1405 else 1406 shapeTyRank = shapeTy.cast<fir::ShapeShiftType>().getRank(); 1407 } 1408 1409 auto eleTy = resultType; 1410 if (auto seqTy = resultType.dyn_cast<fir::SequenceType>()) { 1411 if (seqTy.getDimension() != shapeTyRank) 1412 op.emitOpError("shape operand must be provided and have the value rank " 1413 "when the value is a fir.array"); 1414 eleTy = seqTy.getEleTy(); 1415 } else { 1416 if (shapeTyRank != 0) 1417 op.emitOpError( 1418 "shape operand should only be provided if the value is a fir.array"); 1419 } 1420 1421 if (auto recTy = eleTy.dyn_cast<fir::RecordType>()) { 1422 if (recTy.getNumLenParams() != op.typeparams().size()) 1423 op.emitOpError("length parameters number must match with the value type " 1424 "length parameters"); 1425 } else if (auto charTy = eleTy.dyn_cast<fir::CharacterType>()) { 1426 if (op.typeparams().size() > 1) 1427 op.emitOpError("no more than one length parameter must be provided for " 1428 "character value"); 1429 } else { 1430 if (!op.typeparams().empty()) 1431 op.emitOpError( 1432 "length parameters must not be provided for this value type"); 1433 } 1434 1435 return mlir::success(); 1436 } 1437 1438 //===----------------------------------------------------------------------===// 1439 // SelectOp 1440 //===----------------------------------------------------------------------===// 1441 1442 static constexpr llvm::StringRef getCompareOffsetAttr() { 1443 return "compare_operand_offsets"; 1444 } 1445 1446 static constexpr llvm::StringRef getTargetOffsetAttr() { 1447 return "target_operand_offsets"; 1448 } 1449 1450 template <typename A, typename... AdditionalArgs> 1451 static A getSubOperands(unsigned pos, A allArgs, 1452 mlir::DenseIntElementsAttr ranges, 1453 AdditionalArgs &&...additionalArgs) { 1454 unsigned start = 0; 1455 for (unsigned i = 0; i < pos; ++i) 1456 start += (*(ranges.begin() + i)).getZExtValue(); 1457 return allArgs.slice(start, (*(ranges.begin() + pos)).getZExtValue(), 1458 std::forward<AdditionalArgs>(additionalArgs)...); 1459 } 1460 1461 static mlir::MutableOperandRange 1462 getMutableSuccessorOperands(unsigned pos, mlir::MutableOperandRange operands, 1463 StringRef offsetAttr) { 1464 Operation *owner = operands.getOwner(); 1465 NamedAttribute targetOffsetAttr = 1466 *owner->getAttrDictionary().getNamed(offsetAttr); 1467 return getSubOperands( 1468 pos, operands, targetOffsetAttr.second.cast<DenseIntElementsAttr>(), 1469 mlir::MutableOperandRange::OperandSegment(pos, targetOffsetAttr)); 1470 } 1471 1472 static unsigned denseElementsSize(mlir::DenseIntElementsAttr attr) { 1473 return attr.getNumElements(); 1474 } 1475 1476 llvm::Optional<mlir::OperandRange> fir::SelectOp::getCompareOperands(unsigned) { 1477 return {}; 1478 } 1479 1480 llvm::Optional<llvm::ArrayRef<mlir::Value>> 1481 fir::SelectOp::getCompareOperands(llvm::ArrayRef<mlir::Value>, unsigned) { 1482 return {}; 1483 } 1484 1485 llvm::Optional<mlir::MutableOperandRange> 1486 fir::SelectOp::getMutableSuccessorOperands(unsigned oper) { 1487 return ::getMutableSuccessorOperands(oper, targetArgsMutable(), 1488 getTargetOffsetAttr()); 1489 } 1490 1491 llvm::Optional<llvm::ArrayRef<mlir::Value>> 1492 fir::SelectOp::getSuccessorOperands(llvm::ArrayRef<mlir::Value> operands, 1493 unsigned oper) { 1494 auto a = 1495 (*this)->getAttrOfType<mlir::DenseIntElementsAttr>(getTargetOffsetAttr()); 1496 auto segments = (*this)->getAttrOfType<mlir::DenseIntElementsAttr>( 1497 getOperandSegmentSizeAttr()); 1498 return {getSubOperands(oper, getSubOperands(2, operands, segments), a)}; 1499 } 1500 1501 unsigned fir::SelectOp::targetOffsetSize() { 1502 return denseElementsSize((*this)->getAttrOfType<mlir::DenseIntElementsAttr>( 1503 getTargetOffsetAttr())); 1504 } 1505 1506 //===----------------------------------------------------------------------===// 1507 // SelectCaseOp 1508 //===----------------------------------------------------------------------===// 1509 1510 llvm::Optional<mlir::OperandRange> 1511 fir::SelectCaseOp::getCompareOperands(unsigned cond) { 1512 auto a = (*this)->getAttrOfType<mlir::DenseIntElementsAttr>( 1513 getCompareOffsetAttr()); 1514 return {getSubOperands(cond, compareArgs(), a)}; 1515 } 1516 1517 llvm::Optional<llvm::ArrayRef<mlir::Value>> 1518 fir::SelectCaseOp::getCompareOperands(llvm::ArrayRef<mlir::Value> operands, 1519 unsigned cond) { 1520 auto a = (*this)->getAttrOfType<mlir::DenseIntElementsAttr>( 1521 getCompareOffsetAttr()); 1522 auto segments = (*this)->getAttrOfType<mlir::DenseIntElementsAttr>( 1523 getOperandSegmentSizeAttr()); 1524 return {getSubOperands(cond, getSubOperands(1, operands, segments), a)}; 1525 } 1526 1527 llvm::Optional<mlir::MutableOperandRange> 1528 fir::SelectCaseOp::getMutableSuccessorOperands(unsigned oper) { 1529 return ::getMutableSuccessorOperands(oper, targetArgsMutable(), 1530 getTargetOffsetAttr()); 1531 } 1532 1533 llvm::Optional<llvm::ArrayRef<mlir::Value>> 1534 fir::SelectCaseOp::getSuccessorOperands(llvm::ArrayRef<mlir::Value> operands, 1535 unsigned oper) { 1536 auto a = 1537 (*this)->getAttrOfType<mlir::DenseIntElementsAttr>(getTargetOffsetAttr()); 1538 auto segments = (*this)->getAttrOfType<mlir::DenseIntElementsAttr>( 1539 getOperandSegmentSizeAttr()); 1540 return {getSubOperands(oper, getSubOperands(2, operands, segments), a)}; 1541 } 1542 1543 // parser for fir.select_case Op 1544 static mlir::ParseResult parseSelectCase(mlir::OpAsmParser &parser, 1545 mlir::OperationState &result) { 1546 mlir::OpAsmParser::OperandType selector; 1547 mlir::Type type; 1548 if (parseSelector(parser, result, selector, type)) 1549 return mlir::failure(); 1550 1551 llvm::SmallVector<mlir::Attribute, 8> attrs; 1552 llvm::SmallVector<mlir::OpAsmParser::OperandType, 8> opers; 1553 llvm::SmallVector<mlir::Block *, 8> dests; 1554 llvm::SmallVector<llvm::SmallVector<mlir::Value, 8>, 8> destArgs; 1555 llvm::SmallVector<int32_t, 8> argOffs; 1556 int32_t offSize = 0; 1557 while (true) { 1558 mlir::Attribute attr; 1559 mlir::Block *dest; 1560 llvm::SmallVector<mlir::Value, 8> destArg; 1561 mlir::NamedAttrList temp; 1562 if (parser.parseAttribute(attr, "a", temp) || isValidCaseAttr(attr) || 1563 parser.parseComma()) 1564 return mlir::failure(); 1565 attrs.push_back(attr); 1566 if (attr.dyn_cast_or_null<mlir::UnitAttr>()) { 1567 argOffs.push_back(0); 1568 } else if (attr.dyn_cast_or_null<fir::ClosedIntervalAttr>()) { 1569 mlir::OpAsmParser::OperandType oper1; 1570 mlir::OpAsmParser::OperandType oper2; 1571 if (parser.parseOperand(oper1) || parser.parseComma() || 1572 parser.parseOperand(oper2) || parser.parseComma()) 1573 return mlir::failure(); 1574 opers.push_back(oper1); 1575 opers.push_back(oper2); 1576 argOffs.push_back(2); 1577 offSize += 2; 1578 } else { 1579 mlir::OpAsmParser::OperandType oper; 1580 if (parser.parseOperand(oper) || parser.parseComma()) 1581 return mlir::failure(); 1582 opers.push_back(oper); 1583 argOffs.push_back(1); 1584 ++offSize; 1585 } 1586 if (parser.parseSuccessorAndUseList(dest, destArg)) 1587 return mlir::failure(); 1588 dests.push_back(dest); 1589 destArgs.push_back(destArg); 1590 if (mlir::succeeded(parser.parseOptionalRSquare())) 1591 break; 1592 if (parser.parseComma()) 1593 return mlir::failure(); 1594 } 1595 result.addAttribute(fir::SelectCaseOp::getCasesAttr(), 1596 parser.getBuilder().getArrayAttr(attrs)); 1597 if (parser.resolveOperands(opers, type, result.operands)) 1598 return mlir::failure(); 1599 llvm::SmallVector<int32_t, 8> targOffs; 1600 int32_t toffSize = 0; 1601 const auto count = dests.size(); 1602 for (std::remove_const_t<decltype(count)> i = 0; i != count; ++i) { 1603 result.addSuccessors(dests[i]); 1604 result.addOperands(destArgs[i]); 1605 auto argSize = destArgs[i].size(); 1606 targOffs.push_back(argSize); 1607 toffSize += argSize; 1608 } 1609 auto &bld = parser.getBuilder(); 1610 result.addAttribute(fir::SelectCaseOp::getOperandSegmentSizeAttr(), 1611 bld.getI32VectorAttr({1, offSize, toffSize})); 1612 result.addAttribute(getCompareOffsetAttr(), bld.getI32VectorAttr(argOffs)); 1613 result.addAttribute(getTargetOffsetAttr(), bld.getI32VectorAttr(targOffs)); 1614 return mlir::success(); 1615 } 1616 1617 unsigned fir::SelectCaseOp::compareOffsetSize() { 1618 return denseElementsSize((*this)->getAttrOfType<mlir::DenseIntElementsAttr>( 1619 getCompareOffsetAttr())); 1620 } 1621 1622 unsigned fir::SelectCaseOp::targetOffsetSize() { 1623 return denseElementsSize((*this)->getAttrOfType<mlir::DenseIntElementsAttr>( 1624 getTargetOffsetAttr())); 1625 } 1626 1627 void fir::SelectCaseOp::build(mlir::OpBuilder &builder, 1628 mlir::OperationState &result, 1629 mlir::Value selector, 1630 llvm::ArrayRef<mlir::Attribute> compareAttrs, 1631 llvm::ArrayRef<mlir::ValueRange> cmpOperands, 1632 llvm::ArrayRef<mlir::Block *> destinations, 1633 llvm::ArrayRef<mlir::ValueRange> destOperands, 1634 llvm::ArrayRef<mlir::NamedAttribute> attributes) { 1635 result.addOperands(selector); 1636 result.addAttribute(getCasesAttr(), builder.getArrayAttr(compareAttrs)); 1637 llvm::SmallVector<int32_t, 8> operOffs; 1638 int32_t operSize = 0; 1639 for (auto attr : compareAttrs) { 1640 if (attr.isa<fir::ClosedIntervalAttr>()) { 1641 operOffs.push_back(2); 1642 operSize += 2; 1643 } else if (attr.isa<mlir::UnitAttr>()) { 1644 operOffs.push_back(0); 1645 } else { 1646 operOffs.push_back(1); 1647 ++operSize; 1648 } 1649 } 1650 for (auto ops : cmpOperands) 1651 result.addOperands(ops); 1652 result.addAttribute(getCompareOffsetAttr(), 1653 builder.getI32VectorAttr(operOffs)); 1654 const auto count = destinations.size(); 1655 for (auto d : destinations) 1656 result.addSuccessors(d); 1657 const auto opCount = destOperands.size(); 1658 llvm::SmallVector<int32_t, 8> argOffs; 1659 int32_t sumArgs = 0; 1660 for (std::remove_const_t<decltype(count)> i = 0; i != count; ++i) { 1661 if (i < opCount) { 1662 result.addOperands(destOperands[i]); 1663 const auto argSz = destOperands[i].size(); 1664 argOffs.push_back(argSz); 1665 sumArgs += argSz; 1666 } else { 1667 argOffs.push_back(0); 1668 } 1669 } 1670 result.addAttribute(getOperandSegmentSizeAttr(), 1671 builder.getI32VectorAttr({1, operSize, sumArgs})); 1672 result.addAttribute(getTargetOffsetAttr(), builder.getI32VectorAttr(argOffs)); 1673 result.addAttributes(attributes); 1674 } 1675 1676 /// This builder has a slightly simplified interface in that the list of 1677 /// operands need not be partitioned by the builder. Instead the operands are 1678 /// partitioned here, before being passed to the default builder. This 1679 /// partitioning is unchecked, so can go awry on bad input. 1680 void fir::SelectCaseOp::build(mlir::OpBuilder &builder, 1681 mlir::OperationState &result, 1682 mlir::Value selector, 1683 llvm::ArrayRef<mlir::Attribute> compareAttrs, 1684 llvm::ArrayRef<mlir::Value> cmpOpList, 1685 llvm::ArrayRef<mlir::Block *> destinations, 1686 llvm::ArrayRef<mlir::ValueRange> destOperands, 1687 llvm::ArrayRef<mlir::NamedAttribute> attributes) { 1688 llvm::SmallVector<mlir::ValueRange, 16> cmpOpers; 1689 auto iter = cmpOpList.begin(); 1690 for (auto &attr : compareAttrs) { 1691 if (attr.isa<fir::ClosedIntervalAttr>()) { 1692 cmpOpers.push_back(mlir::ValueRange({iter, iter + 2})); 1693 iter += 2; 1694 } else if (attr.isa<UnitAttr>()) { 1695 cmpOpers.push_back(mlir::ValueRange{}); 1696 } else { 1697 cmpOpers.push_back(mlir::ValueRange({iter, iter + 1})); 1698 ++iter; 1699 } 1700 } 1701 build(builder, result, selector, compareAttrs, cmpOpers, destinations, 1702 destOperands, attributes); 1703 } 1704 1705 //===----------------------------------------------------------------------===// 1706 // SelectRankOp 1707 //===----------------------------------------------------------------------===// 1708 1709 llvm::Optional<mlir::OperandRange> 1710 fir::SelectRankOp::getCompareOperands(unsigned) { 1711 return {}; 1712 } 1713 1714 llvm::Optional<llvm::ArrayRef<mlir::Value>> 1715 fir::SelectRankOp::getCompareOperands(llvm::ArrayRef<mlir::Value>, unsigned) { 1716 return {}; 1717 } 1718 1719 llvm::Optional<mlir::MutableOperandRange> 1720 fir::SelectRankOp::getMutableSuccessorOperands(unsigned oper) { 1721 return ::getMutableSuccessorOperands(oper, targetArgsMutable(), 1722 getTargetOffsetAttr()); 1723 } 1724 1725 llvm::Optional<llvm::ArrayRef<mlir::Value>> 1726 fir::SelectRankOp::getSuccessorOperands(llvm::ArrayRef<mlir::Value> operands, 1727 unsigned oper) { 1728 auto a = 1729 (*this)->getAttrOfType<mlir::DenseIntElementsAttr>(getTargetOffsetAttr()); 1730 auto segments = (*this)->getAttrOfType<mlir::DenseIntElementsAttr>( 1731 getOperandSegmentSizeAttr()); 1732 return {getSubOperands(oper, getSubOperands(2, operands, segments), a)}; 1733 } 1734 1735 unsigned fir::SelectRankOp::targetOffsetSize() { 1736 return denseElementsSize((*this)->getAttrOfType<mlir::DenseIntElementsAttr>( 1737 getTargetOffsetAttr())); 1738 } 1739 1740 //===----------------------------------------------------------------------===// 1741 // SelectTypeOp 1742 //===----------------------------------------------------------------------===// 1743 1744 llvm::Optional<mlir::OperandRange> 1745 fir::SelectTypeOp::getCompareOperands(unsigned) { 1746 return {}; 1747 } 1748 1749 llvm::Optional<llvm::ArrayRef<mlir::Value>> 1750 fir::SelectTypeOp::getCompareOperands(llvm::ArrayRef<mlir::Value>, unsigned) { 1751 return {}; 1752 } 1753 1754 llvm::Optional<mlir::MutableOperandRange> 1755 fir::SelectTypeOp::getMutableSuccessorOperands(unsigned oper) { 1756 return ::getMutableSuccessorOperands(oper, targetArgsMutable(), 1757 getTargetOffsetAttr()); 1758 } 1759 1760 llvm::Optional<llvm::ArrayRef<mlir::Value>> 1761 fir::SelectTypeOp::getSuccessorOperands(llvm::ArrayRef<mlir::Value> operands, 1762 unsigned oper) { 1763 auto a = 1764 (*this)->getAttrOfType<mlir::DenseIntElementsAttr>(getTargetOffsetAttr()); 1765 auto segments = (*this)->getAttrOfType<mlir::DenseIntElementsAttr>( 1766 getOperandSegmentSizeAttr()); 1767 return {getSubOperands(oper, getSubOperands(2, operands, segments), a)}; 1768 } 1769 1770 static ParseResult parseSelectType(OpAsmParser &parser, 1771 OperationState &result) { 1772 mlir::OpAsmParser::OperandType selector; 1773 mlir::Type type; 1774 if (parseSelector(parser, result, selector, type)) 1775 return mlir::failure(); 1776 1777 llvm::SmallVector<mlir::Attribute, 8> attrs; 1778 llvm::SmallVector<mlir::Block *, 8> dests; 1779 llvm::SmallVector<llvm::SmallVector<mlir::Value, 8>, 8> destArgs; 1780 while (true) { 1781 mlir::Attribute attr; 1782 mlir::Block *dest; 1783 llvm::SmallVector<mlir::Value, 8> destArg; 1784 mlir::NamedAttrList temp; 1785 if (parser.parseAttribute(attr, "a", temp) || parser.parseComma() || 1786 parser.parseSuccessorAndUseList(dest, destArg)) 1787 return mlir::failure(); 1788 attrs.push_back(attr); 1789 dests.push_back(dest); 1790 destArgs.push_back(destArg); 1791 if (mlir::succeeded(parser.parseOptionalRSquare())) 1792 break; 1793 if (parser.parseComma()) 1794 return mlir::failure(); 1795 } 1796 auto &bld = parser.getBuilder(); 1797 result.addAttribute(fir::SelectTypeOp::getCasesAttr(), 1798 bld.getArrayAttr(attrs)); 1799 llvm::SmallVector<int32_t, 8> argOffs; 1800 int32_t offSize = 0; 1801 const auto count = dests.size(); 1802 for (std::remove_const_t<decltype(count)> i = 0; i != count; ++i) { 1803 result.addSuccessors(dests[i]); 1804 result.addOperands(destArgs[i]); 1805 auto argSize = destArgs[i].size(); 1806 argOffs.push_back(argSize); 1807 offSize += argSize; 1808 } 1809 result.addAttribute(fir::SelectTypeOp::getOperandSegmentSizeAttr(), 1810 bld.getI32VectorAttr({1, 0, offSize})); 1811 result.addAttribute(getTargetOffsetAttr(), bld.getI32VectorAttr(argOffs)); 1812 return mlir::success(); 1813 } 1814 1815 unsigned fir::SelectTypeOp::targetOffsetSize() { 1816 return denseElementsSize((*this)->getAttrOfType<mlir::DenseIntElementsAttr>( 1817 getTargetOffsetAttr())); 1818 } 1819 1820 //===----------------------------------------------------------------------===// 1821 // SliceOp 1822 //===----------------------------------------------------------------------===// 1823 1824 /// Return the output rank of a slice op. The output rank must be between 1 and 1825 /// the rank of the array being sliced (inclusive). 1826 unsigned fir::SliceOp::getOutputRank(mlir::ValueRange triples) { 1827 unsigned rank = 0; 1828 if (!triples.empty()) { 1829 for (unsigned i = 1, end = triples.size(); i < end; i += 3) { 1830 auto op = triples[i].getDefiningOp(); 1831 if (!mlir::isa_and_nonnull<fir::UndefOp>(op)) 1832 ++rank; 1833 } 1834 assert(rank > 0); 1835 } 1836 return rank; 1837 } 1838 1839 //===----------------------------------------------------------------------===// 1840 // StoreOp 1841 //===----------------------------------------------------------------------===// 1842 1843 mlir::Type fir::StoreOp::elementType(mlir::Type refType) { 1844 if (auto ref = refType.dyn_cast<ReferenceType>()) 1845 return ref.getEleTy(); 1846 if (auto ref = refType.dyn_cast<PointerType>()) 1847 return ref.getEleTy(); 1848 if (auto ref = refType.dyn_cast<HeapType>()) 1849 return ref.getEleTy(); 1850 return {}; 1851 } 1852 1853 //===----------------------------------------------------------------------===// 1854 // StringLitOp 1855 //===----------------------------------------------------------------------===// 1856 1857 bool fir::StringLitOp::isWideValue() { 1858 auto eleTy = getType().cast<fir::SequenceType>().getEleTy(); 1859 return eleTy.cast<fir::CharacterType>().getFKind() != 1; 1860 } 1861 1862 static mlir::NamedAttribute 1863 mkNamedIntegerAttr(mlir::OpBuilder &builder, llvm::StringRef name, int64_t v) { 1864 assert(v > 0); 1865 return builder.getNamedAttr( 1866 name, builder.getIntegerAttr(builder.getIntegerType(64), v)); 1867 } 1868 1869 void fir::StringLitOp::build(mlir::OpBuilder &builder, OperationState &result, 1870 fir::CharacterType inType, llvm::StringRef val, 1871 llvm::Optional<int64_t> len) { 1872 auto valAttr = builder.getNamedAttr(value(), builder.getStringAttr(val)); 1873 int64_t length = len.hasValue() ? len.getValue() : inType.getLen(); 1874 auto lenAttr = mkNamedIntegerAttr(builder, size(), length); 1875 result.addAttributes({valAttr, lenAttr}); 1876 result.addTypes(inType); 1877 } 1878 1879 template <typename C> 1880 static mlir::ArrayAttr convertToArrayAttr(mlir::OpBuilder &builder, 1881 llvm::ArrayRef<C> xlist) { 1882 llvm::SmallVector<mlir::Attribute> attrs; 1883 auto ty = builder.getIntegerType(8 * sizeof(C)); 1884 for (auto ch : xlist) 1885 attrs.push_back(builder.getIntegerAttr(ty, ch)); 1886 return builder.getArrayAttr(attrs); 1887 } 1888 1889 void fir::StringLitOp::build(mlir::OpBuilder &builder, OperationState &result, 1890 fir::CharacterType inType, 1891 llvm::ArrayRef<char> vlist, 1892 llvm::Optional<int64_t> len) { 1893 auto valAttr = 1894 builder.getNamedAttr(xlist(), convertToArrayAttr(builder, vlist)); 1895 std::int64_t length = len.hasValue() ? len.getValue() : inType.getLen(); 1896 auto lenAttr = mkNamedIntegerAttr(builder, size(), length); 1897 result.addAttributes({valAttr, lenAttr}); 1898 result.addTypes(inType); 1899 } 1900 1901 void fir::StringLitOp::build(mlir::OpBuilder &builder, OperationState &result, 1902 fir::CharacterType inType, 1903 llvm::ArrayRef<char16_t> vlist, 1904 llvm::Optional<int64_t> len) { 1905 auto valAttr = 1906 builder.getNamedAttr(xlist(), convertToArrayAttr(builder, vlist)); 1907 std::int64_t length = len.hasValue() ? len.getValue() : inType.getLen(); 1908 auto lenAttr = mkNamedIntegerAttr(builder, size(), length); 1909 result.addAttributes({valAttr, lenAttr}); 1910 result.addTypes(inType); 1911 } 1912 1913 void fir::StringLitOp::build(mlir::OpBuilder &builder, OperationState &result, 1914 fir::CharacterType inType, 1915 llvm::ArrayRef<char32_t> vlist, 1916 llvm::Optional<int64_t> len) { 1917 auto valAttr = 1918 builder.getNamedAttr(xlist(), convertToArrayAttr(builder, vlist)); 1919 std::int64_t length = len.hasValue() ? len.getValue() : inType.getLen(); 1920 auto lenAttr = mkNamedIntegerAttr(builder, size(), length); 1921 result.addAttributes({valAttr, lenAttr}); 1922 result.addTypes(inType); 1923 } 1924 1925 static mlir::ParseResult parseStringLitOp(mlir::OpAsmParser &parser, 1926 mlir::OperationState &result) { 1927 auto &builder = parser.getBuilder(); 1928 mlir::Attribute val; 1929 mlir::NamedAttrList attrs; 1930 llvm::SMLoc trailingTypeLoc; 1931 if (parser.parseAttribute(val, "fake", attrs)) 1932 return mlir::failure(); 1933 if (auto v = val.dyn_cast<mlir::StringAttr>()) 1934 result.attributes.push_back( 1935 builder.getNamedAttr(fir::StringLitOp::value(), v)); 1936 else if (auto v = val.dyn_cast<mlir::ArrayAttr>()) 1937 result.attributes.push_back( 1938 builder.getNamedAttr(fir::StringLitOp::xlist(), v)); 1939 else 1940 return parser.emitError(parser.getCurrentLocation(), 1941 "found an invalid constant"); 1942 mlir::IntegerAttr sz; 1943 mlir::Type type; 1944 if (parser.parseLParen() || 1945 parser.parseAttribute(sz, fir::StringLitOp::size(), result.attributes) || 1946 parser.parseRParen() || parser.getCurrentLocation(&trailingTypeLoc) || 1947 parser.parseColonType(type)) 1948 return mlir::failure(); 1949 auto charTy = type.dyn_cast<fir::CharacterType>(); 1950 if (!charTy) 1951 return parser.emitError(trailingTypeLoc, "must have character type"); 1952 type = fir::CharacterType::get(builder.getContext(), charTy.getFKind(), 1953 sz.getInt()); 1954 if (!type || parser.addTypesToList(type, result.types)) 1955 return mlir::failure(); 1956 return mlir::success(); 1957 } 1958 1959 static void print(mlir::OpAsmPrinter &p, fir::StringLitOp &op) { 1960 p << ' ' << op.getValue() << '('; 1961 p << op.getSize().cast<mlir::IntegerAttr>().getValue() << ") : "; 1962 p.printType(op.getType()); 1963 } 1964 1965 static mlir::LogicalResult verify(fir::StringLitOp &op) { 1966 if (op.getSize().cast<mlir::IntegerAttr>().getValue().isNegative()) 1967 return op.emitOpError("size must be non-negative"); 1968 if (auto xl = op.getOperation()->getAttr(fir::StringLitOp::xlist())) { 1969 auto xList = xl.cast<mlir::ArrayAttr>(); 1970 for (auto a : xList) 1971 if (!a.isa<mlir::IntegerAttr>()) 1972 return op.emitOpError("values in list must be integers"); 1973 } 1974 return mlir::success(); 1975 } 1976 1977 //===----------------------------------------------------------------------===// 1978 // IfOp 1979 //===----------------------------------------------------------------------===// 1980 1981 void fir::IfOp::build(mlir::OpBuilder &builder, OperationState &result, 1982 mlir::Value cond, bool withElseRegion) { 1983 build(builder, result, llvm::None, cond, withElseRegion); 1984 } 1985 1986 void fir::IfOp::build(mlir::OpBuilder &builder, OperationState &result, 1987 mlir::TypeRange resultTypes, mlir::Value cond, 1988 bool withElseRegion) { 1989 result.addOperands(cond); 1990 result.addTypes(resultTypes); 1991 1992 mlir::Region *thenRegion = result.addRegion(); 1993 thenRegion->push_back(new mlir::Block()); 1994 if (resultTypes.empty()) 1995 IfOp::ensureTerminator(*thenRegion, builder, result.location); 1996 1997 mlir::Region *elseRegion = result.addRegion(); 1998 if (withElseRegion) { 1999 elseRegion->push_back(new mlir::Block()); 2000 if (resultTypes.empty()) 2001 IfOp::ensureTerminator(*elseRegion, builder, result.location); 2002 } 2003 } 2004 2005 static mlir::ParseResult parseIfOp(OpAsmParser &parser, 2006 OperationState &result) { 2007 result.regions.reserve(2); 2008 mlir::Region *thenRegion = result.addRegion(); 2009 mlir::Region *elseRegion = result.addRegion(); 2010 2011 auto &builder = parser.getBuilder(); 2012 OpAsmParser::OperandType cond; 2013 mlir::Type i1Type = builder.getIntegerType(1); 2014 if (parser.parseOperand(cond) || 2015 parser.resolveOperand(cond, i1Type, result.operands)) 2016 return mlir::failure(); 2017 2018 if (parser.parseOptionalArrowTypeList(result.types)) 2019 return mlir::failure(); 2020 2021 if (parser.parseRegion(*thenRegion, {}, {})) 2022 return mlir::failure(); 2023 IfOp::ensureTerminator(*thenRegion, parser.getBuilder(), result.location); 2024 2025 if (mlir::succeeded(parser.parseOptionalKeyword("else"))) { 2026 if (parser.parseRegion(*elseRegion, {}, {})) 2027 return mlir::failure(); 2028 IfOp::ensureTerminator(*elseRegion, parser.getBuilder(), result.location); 2029 } 2030 2031 // Parse the optional attribute list. 2032 if (parser.parseOptionalAttrDict(result.attributes)) 2033 return mlir::failure(); 2034 return mlir::success(); 2035 } 2036 2037 static LogicalResult verify(fir::IfOp op) { 2038 if (op.getNumResults() != 0 && op.elseRegion().empty()) 2039 return op.emitOpError("must have an else block if defining values"); 2040 2041 return mlir::success(); 2042 } 2043 2044 static void print(mlir::OpAsmPrinter &p, fir::IfOp op) { 2045 bool printBlockTerminators = false; 2046 p << ' ' << op.condition(); 2047 if (!op.results().empty()) { 2048 p << " -> (" << op.getResultTypes() << ')'; 2049 printBlockTerminators = true; 2050 } 2051 p.printRegion(op.thenRegion(), /*printEntryBlockArgs=*/false, 2052 printBlockTerminators); 2053 2054 // Print the 'else' regions if it exists and has a block. 2055 auto &otherReg = op.elseRegion(); 2056 if (!otherReg.empty()) { 2057 p << " else"; 2058 p.printRegion(otherReg, /*printEntryBlockArgs=*/false, 2059 printBlockTerminators); 2060 } 2061 p.printOptionalAttrDict(op->getAttrs()); 2062 } 2063 2064 void fir::IfOp::resultToSourceOps(llvm::SmallVectorImpl<mlir::Value> &results, 2065 unsigned resultNum) { 2066 auto *term = thenRegion().front().getTerminator(); 2067 if (resultNum < term->getNumOperands()) 2068 results.push_back(term->getOperand(resultNum)); 2069 term = elseRegion().front().getTerminator(); 2070 if (resultNum < term->getNumOperands()) 2071 results.push_back(term->getOperand(resultNum)); 2072 } 2073 2074 //===----------------------------------------------------------------------===// 2075 2076 mlir::ParseResult fir::isValidCaseAttr(mlir::Attribute attr) { 2077 if (attr.dyn_cast_or_null<mlir::UnitAttr>() || 2078 attr.dyn_cast_or_null<ClosedIntervalAttr>() || 2079 attr.dyn_cast_or_null<PointIntervalAttr>() || 2080 attr.dyn_cast_or_null<LowerBoundAttr>() || 2081 attr.dyn_cast_or_null<UpperBoundAttr>()) 2082 return mlir::success(); 2083 return mlir::failure(); 2084 } 2085 2086 unsigned fir::getCaseArgumentOffset(llvm::ArrayRef<mlir::Attribute> cases, 2087 unsigned dest) { 2088 unsigned o = 0; 2089 for (unsigned i = 0; i < dest; ++i) { 2090 auto &attr = cases[i]; 2091 if (!attr.dyn_cast_or_null<mlir::UnitAttr>()) { 2092 ++o; 2093 if (attr.dyn_cast_or_null<ClosedIntervalAttr>()) 2094 ++o; 2095 } 2096 } 2097 return o; 2098 } 2099 2100 mlir::ParseResult fir::parseSelector(mlir::OpAsmParser &parser, 2101 mlir::OperationState &result, 2102 mlir::OpAsmParser::OperandType &selector, 2103 mlir::Type &type) { 2104 if (parser.parseOperand(selector) || parser.parseColonType(type) || 2105 parser.resolveOperand(selector, type, result.operands) || 2106 parser.parseLSquare()) 2107 return mlir::failure(); 2108 return mlir::success(); 2109 } 2110 2111 /// Generic pretty-printer of a binary operation 2112 static void printBinaryOp(Operation *op, OpAsmPrinter &p) { 2113 assert(op->getNumOperands() == 2 && "binary op must have two operands"); 2114 assert(op->getNumResults() == 1 && "binary op must have one result"); 2115 2116 p << ' ' << op->getOperand(0) << ", " << op->getOperand(1); 2117 p.printOptionalAttrDict(op->getAttrs()); 2118 p << " : " << op->getResult(0).getType(); 2119 } 2120 2121 /// Generic pretty-printer of an unary operation 2122 static void printUnaryOp(Operation *op, OpAsmPrinter &p) { 2123 assert(op->getNumOperands() == 1 && "unary op must have one operand"); 2124 assert(op->getNumResults() == 1 && "unary op must have one result"); 2125 2126 p << ' ' << op->getOperand(0); 2127 p.printOptionalAttrDict(op->getAttrs()); 2128 p << " : " << op->getResult(0).getType(); 2129 } 2130 2131 bool fir::isReferenceLike(mlir::Type type) { 2132 return type.isa<fir::ReferenceType>() || type.isa<fir::HeapType>() || 2133 type.isa<fir::PointerType>(); 2134 } 2135 2136 mlir::FuncOp fir::createFuncOp(mlir::Location loc, mlir::ModuleOp module, 2137 StringRef name, mlir::FunctionType type, 2138 llvm::ArrayRef<mlir::NamedAttribute> attrs) { 2139 if (auto f = module.lookupSymbol<mlir::FuncOp>(name)) 2140 return f; 2141 mlir::OpBuilder modBuilder(module.getBodyRegion()); 2142 modBuilder.setInsertionPoint(module.getBody()->getTerminator()); 2143 auto result = modBuilder.create<mlir::FuncOp>(loc, name, type, attrs); 2144 result.setVisibility(mlir::SymbolTable::Visibility::Private); 2145 return result; 2146 } 2147 2148 fir::GlobalOp fir::createGlobalOp(mlir::Location loc, mlir::ModuleOp module, 2149 StringRef name, mlir::Type type, 2150 llvm::ArrayRef<mlir::NamedAttribute> attrs) { 2151 if (auto g = module.lookupSymbol<fir::GlobalOp>(name)) 2152 return g; 2153 mlir::OpBuilder modBuilder(module.getBodyRegion()); 2154 auto result = modBuilder.create<fir::GlobalOp>(loc, name, type, attrs); 2155 result.setVisibility(mlir::SymbolTable::Visibility::Private); 2156 return result; 2157 } 2158 2159 bool fir::valueHasFirAttribute(mlir::Value value, 2160 llvm::StringRef attributeName) { 2161 // If this is a fir.box that was loaded, the fir attributes will be on the 2162 // related fir.ref<fir.box> creation. 2163 if (value.getType().isa<fir::BoxType>()) 2164 if (auto definingOp = value.getDefiningOp()) 2165 if (auto loadOp = mlir::dyn_cast<fir::LoadOp>(definingOp)) 2166 value = loadOp.memref(); 2167 // If this is a function argument, look in the argument attributes. 2168 if (auto blockArg = value.dyn_cast<mlir::BlockArgument>()) { 2169 if (blockArg.getOwner() && blockArg.getOwner()->isEntryBlock()) 2170 if (auto funcOp = 2171 mlir::dyn_cast<mlir::FuncOp>(blockArg.getOwner()->getParentOp())) 2172 if (funcOp.getArgAttr(blockArg.getArgNumber(), attributeName)) 2173 return true; 2174 return false; 2175 } 2176 2177 if (auto definingOp = value.getDefiningOp()) { 2178 // If this is an allocated value, look at the allocation attributes. 2179 if (mlir::isa<fir::AllocMemOp>(definingOp) || 2180 mlir::isa<AllocaOp>(definingOp)) 2181 return definingOp->hasAttr(attributeName); 2182 // If this is an imported global, look at AddrOfOp and GlobalOp attributes. 2183 // Both operations are looked at because use/host associated variable (the 2184 // AddrOfOp) can have ASYNCHRONOUS/VOLATILE attributes even if the ultimate 2185 // entity (the globalOp) does not have them. 2186 if (auto addressOfOp = mlir::dyn_cast<fir::AddrOfOp>(definingOp)) { 2187 if (addressOfOp->hasAttr(attributeName)) 2188 return true; 2189 if (auto module = definingOp->getParentOfType<mlir::ModuleOp>()) 2190 if (auto globalOp = 2191 module.lookupSymbol<fir::GlobalOp>(addressOfOp.symbol())) 2192 return globalOp->hasAttr(attributeName); 2193 } 2194 } 2195 // TODO: Construct associated entities attributes. Decide where the fir 2196 // attributes must be placed/looked for in this case. 2197 return false; 2198 } 2199 2200 // Tablegen operators 2201 2202 #define GET_OP_CLASSES 2203 #include "flang/Optimizer/Dialect/FIROps.cpp.inc" 2204