1 //===- Operation.cpp - Operation support code -----------------------------===// 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 #include "mlir/IR/Operation.h" 10 #include "mlir/IR/BlockAndValueMapping.h" 11 #include "mlir/IR/BuiltinTypes.h" 12 #include "mlir/IR/Dialect.h" 13 #include "mlir/IR/OpImplementation.h" 14 #include "mlir/IR/PatternMatch.h" 15 #include "mlir/IR/TypeUtilities.h" 16 #include "mlir/Interfaces/FoldInterfaces.h" 17 #include "llvm/ADT/StringExtras.h" 18 #include <numeric> 19 20 using namespace mlir; 21 22 //===----------------------------------------------------------------------===// 23 // Operation 24 //===----------------------------------------------------------------------===// 25 26 /// Create a new Operation with the specific fields. 27 Operation *Operation::create(Location location, OperationName name, 28 TypeRange resultTypes, ValueRange operands, 29 ArrayRef<NamedAttribute> attributes, 30 BlockRange successors, unsigned numRegions) { 31 return create(location, name, resultTypes, operands, 32 DictionaryAttr::get(location.getContext(), attributes), 33 successors, numRegions); 34 } 35 36 /// Create a new Operation from operation state. 37 Operation *Operation::create(const OperationState &state) { 38 return create(state.location, state.name, state.types, state.operands, 39 state.attributes.getDictionary(state.getContext()), 40 state.successors, state.regions); 41 } 42 43 /// Create a new Operation with the specific fields. 44 Operation *Operation::create(Location location, OperationName name, 45 TypeRange resultTypes, ValueRange operands, 46 DictionaryAttr attributes, BlockRange successors, 47 RegionRange regions) { 48 unsigned numRegions = regions.size(); 49 Operation *op = create(location, name, resultTypes, operands, attributes, 50 successors, numRegions); 51 for (unsigned i = 0; i < numRegions; ++i) 52 if (regions[i]) 53 op->getRegion(i).takeBody(*regions[i]); 54 return op; 55 } 56 57 /// Overload of create that takes an existing DictionaryAttr to avoid 58 /// unnecessarily uniquing a list of attributes. 59 Operation *Operation::create(Location location, OperationName name, 60 TypeRange resultTypes, ValueRange operands, 61 DictionaryAttr attributes, BlockRange successors, 62 unsigned numRegions) { 63 assert(llvm::all_of(resultTypes, [](Type t) { return t; }) && 64 "unexpected null result type"); 65 66 // We only need to allocate additional memory for a subset of results. 67 unsigned numTrailingResults = OpResult::getNumTrailing(resultTypes.size()); 68 unsigned numInlineResults = OpResult::getNumInline(resultTypes.size()); 69 unsigned numSuccessors = successors.size(); 70 unsigned numOperands = operands.size(); 71 unsigned numResults = resultTypes.size(); 72 73 // If the operation is known to have no operands, don't allocate an operand 74 // storage. 75 bool needsOperandStorage = 76 operands.empty() ? !name.hasTrait<OpTrait::ZeroOperands>() : true; 77 78 // Compute the byte size for the operation and the operand storage. This takes 79 // into account the size of the operation, its trailing objects, and its 80 // prefixed objects. 81 size_t byteSize = 82 totalSizeToAlloc<detail::OperandStorage, BlockOperand, Region, OpOperand>( 83 needsOperandStorage ? 1 : 0, numSuccessors, numRegions, numOperands); 84 size_t prefixByteSize = llvm::alignTo( 85 Operation::prefixAllocSize(numTrailingResults, numInlineResults), 86 alignof(Operation)); 87 char *mallocMem = reinterpret_cast<char *>(malloc(byteSize + prefixByteSize)); 88 void *rawMem = mallocMem + prefixByteSize; 89 90 // Create the new Operation. 91 Operation *op = 92 ::new (rawMem) Operation(location, name, numResults, numSuccessors, 93 numRegions, attributes, needsOperandStorage); 94 95 assert((numSuccessors == 0 || op->mightHaveTrait<OpTrait::IsTerminator>()) && 96 "unexpected successors in a non-terminator operation"); 97 98 // Initialize the results. 99 auto resultTypeIt = resultTypes.begin(); 100 for (unsigned i = 0; i < numInlineResults; ++i, ++resultTypeIt) 101 new (op->getInlineOpResult(i)) detail::InlineOpResult(*resultTypeIt, i); 102 for (unsigned i = 0; i < numTrailingResults; ++i, ++resultTypeIt) { 103 new (op->getOutOfLineOpResult(i)) 104 detail::OutOfLineOpResult(*resultTypeIt, i); 105 } 106 107 // Initialize the regions. 108 for (unsigned i = 0; i != numRegions; ++i) 109 new (&op->getRegion(i)) Region(op); 110 111 // Initialize the operands. 112 if (needsOperandStorage) { 113 new (&op->getOperandStorage()) detail::OperandStorage( 114 op, op->getTrailingObjects<OpOperand>(), operands); 115 } 116 117 // Initialize the successors. 118 auto blockOperands = op->getBlockOperands(); 119 for (unsigned i = 0; i != numSuccessors; ++i) 120 new (&blockOperands[i]) BlockOperand(op, successors[i]); 121 122 return op; 123 } 124 125 Operation::Operation(Location location, OperationName name, unsigned numResults, 126 unsigned numSuccessors, unsigned numRegions, 127 DictionaryAttr attributes, bool hasOperandStorage) 128 : location(location), numResults(numResults), numSuccs(numSuccessors), 129 numRegions(numRegions), hasOperandStorage(hasOperandStorage), name(name), 130 attrs(attributes) { 131 assert(attributes && "unexpected null attribute dictionary"); 132 #ifndef NDEBUG 133 if (!getDialect() && !getContext()->allowsUnregisteredDialects()) 134 llvm::report_fatal_error( 135 name.getStringRef() + 136 " created with unregistered dialect. If this is intended, please call " 137 "allowUnregisteredDialects() on the MLIRContext, or use " 138 "-allow-unregistered-dialect with the MLIR tool used."); 139 #endif 140 } 141 142 // Operations are deleted through the destroy() member because they are 143 // allocated via malloc. 144 Operation::~Operation() { 145 assert(block == nullptr && "operation destroyed but still in a block"); 146 #ifndef NDEBUG 147 if (!use_empty()) { 148 { 149 InFlightDiagnostic diag = 150 emitOpError("operation destroyed but still has uses"); 151 for (Operation *user : getUsers()) 152 diag.attachNote(user->getLoc()) << "- use: " << *user << "\n"; 153 } 154 llvm::report_fatal_error("operation destroyed but still has uses"); 155 } 156 #endif 157 // Explicitly run the destructors for the operands. 158 if (hasOperandStorage) 159 getOperandStorage().~OperandStorage(); 160 161 // Explicitly run the destructors for the successors. 162 for (auto &successor : getBlockOperands()) 163 successor.~BlockOperand(); 164 165 // Explicitly destroy the regions. 166 for (auto ®ion : getRegions()) 167 region.~Region(); 168 } 169 170 /// Destroy this operation or one of its subclasses. 171 void Operation::destroy() { 172 // Operations may have additional prefixed allocation, which needs to be 173 // accounted for here when computing the address to free. 174 char *rawMem = reinterpret_cast<char *>(this) - 175 llvm::alignTo(prefixAllocSize(), alignof(Operation)); 176 this->~Operation(); 177 free(rawMem); 178 } 179 180 /// Return true if this operation is a proper ancestor of the `other` 181 /// operation. 182 bool Operation::isProperAncestor(Operation *other) { 183 while ((other = other->getParentOp())) 184 if (this == other) 185 return true; 186 return false; 187 } 188 189 /// Replace any uses of 'from' with 'to' within this operation. 190 void Operation::replaceUsesOfWith(Value from, Value to) { 191 if (from == to) 192 return; 193 for (auto &operand : getOpOperands()) 194 if (operand.get() == from) 195 operand.set(to); 196 } 197 198 /// Replace the current operands of this operation with the ones provided in 199 /// 'operands'. 200 void Operation::setOperands(ValueRange operands) { 201 if (LLVM_LIKELY(hasOperandStorage)) 202 return getOperandStorage().setOperands(this, operands); 203 assert(operands.empty() && "setting operands without an operand storage"); 204 } 205 206 /// Replace the operands beginning at 'start' and ending at 'start' + 'length' 207 /// with the ones provided in 'operands'. 'operands' may be smaller or larger 208 /// than the range pointed to by 'start'+'length'. 209 void Operation::setOperands(unsigned start, unsigned length, 210 ValueRange operands) { 211 assert((start + length) <= getNumOperands() && 212 "invalid operand range specified"); 213 if (LLVM_LIKELY(hasOperandStorage)) 214 return getOperandStorage().setOperands(this, start, length, operands); 215 assert(operands.empty() && "setting operands without an operand storage"); 216 } 217 218 /// Insert the given operands into the operand list at the given 'index'. 219 void Operation::insertOperands(unsigned index, ValueRange operands) { 220 if (LLVM_LIKELY(hasOperandStorage)) 221 return setOperands(index, /*length=*/0, operands); 222 assert(operands.empty() && "inserting operands without an operand storage"); 223 } 224 225 //===----------------------------------------------------------------------===// 226 // Diagnostics 227 //===----------------------------------------------------------------------===// 228 229 /// Emit an error about fatal conditions with this operation, reporting up to 230 /// any diagnostic handlers that may be listening. 231 InFlightDiagnostic Operation::emitError(const Twine &message) { 232 InFlightDiagnostic diag = mlir::emitError(getLoc(), message); 233 if (getContext()->shouldPrintOpOnDiagnostic()) { 234 diag.attachNote(getLoc()) 235 .append("see current operation: ") 236 .appendOp(*this, OpPrintingFlags().printGenericOpForm()); 237 } 238 return diag; 239 } 240 241 /// Emit a warning about this operation, reporting up to any diagnostic 242 /// handlers that may be listening. 243 InFlightDiagnostic Operation::emitWarning(const Twine &message) { 244 InFlightDiagnostic diag = mlir::emitWarning(getLoc(), message); 245 if (getContext()->shouldPrintOpOnDiagnostic()) 246 diag.attachNote(getLoc()) << "see current operation: " << *this; 247 return diag; 248 } 249 250 /// Emit a remark about this operation, reporting up to any diagnostic 251 /// handlers that may be listening. 252 InFlightDiagnostic Operation::emitRemark(const Twine &message) { 253 InFlightDiagnostic diag = mlir::emitRemark(getLoc(), message); 254 if (getContext()->shouldPrintOpOnDiagnostic()) 255 diag.attachNote(getLoc()) << "see current operation: " << *this; 256 return diag; 257 } 258 259 //===----------------------------------------------------------------------===// 260 // Operation Ordering 261 //===----------------------------------------------------------------------===// 262 263 constexpr unsigned Operation::kInvalidOrderIdx; 264 constexpr unsigned Operation::kOrderStride; 265 266 /// Given an operation 'other' that is within the same parent block, return 267 /// whether the current operation is before 'other' in the operation list 268 /// of the parent block. 269 /// Note: This function has an average complexity of O(1), but worst case may 270 /// take O(N) where N is the number of operations within the parent block. 271 bool Operation::isBeforeInBlock(Operation *other) { 272 assert(block && "Operations without parent blocks have no order."); 273 assert(other && other->block == block && 274 "Expected other operation to have the same parent block."); 275 // If the order of the block is already invalid, directly recompute the 276 // parent. 277 if (!block->isOpOrderValid()) { 278 block->recomputeOpOrder(); 279 } else { 280 // Update the order either operation if necessary. 281 updateOrderIfNecessary(); 282 other->updateOrderIfNecessary(); 283 } 284 285 return orderIndex < other->orderIndex; 286 } 287 288 /// Update the order index of this operation of this operation if necessary, 289 /// potentially recomputing the order of the parent block. 290 void Operation::updateOrderIfNecessary() { 291 assert(block && "expected valid parent"); 292 293 // If the order is valid for this operation there is nothing to do. 294 if (hasValidOrder()) 295 return; 296 Operation *blockFront = &block->front(); 297 Operation *blockBack = &block->back(); 298 299 // This method is expected to only be invoked on blocks with more than one 300 // operation. 301 assert(blockFront != blockBack && "expected more than one operation"); 302 303 // If the operation is at the end of the block. 304 if (this == blockBack) { 305 Operation *prevNode = getPrevNode(); 306 if (!prevNode->hasValidOrder()) 307 return block->recomputeOpOrder(); 308 309 // Add the stride to the previous operation. 310 orderIndex = prevNode->orderIndex + kOrderStride; 311 return; 312 } 313 314 // If this is the first operation try to use the next operation to compute the 315 // ordering. 316 if (this == blockFront) { 317 Operation *nextNode = getNextNode(); 318 if (!nextNode->hasValidOrder()) 319 return block->recomputeOpOrder(); 320 // There is no order to give this operation. 321 if (nextNode->orderIndex == 0) 322 return block->recomputeOpOrder(); 323 324 // If we can't use the stride, just take the middle value left. This is safe 325 // because we know there is at least one valid index to assign to. 326 if (nextNode->orderIndex <= kOrderStride) 327 orderIndex = (nextNode->orderIndex / 2); 328 else 329 orderIndex = kOrderStride; 330 return; 331 } 332 333 // Otherwise, this operation is between two others. Place this operation in 334 // the middle of the previous and next if possible. 335 Operation *prevNode = getPrevNode(), *nextNode = getNextNode(); 336 if (!prevNode->hasValidOrder() || !nextNode->hasValidOrder()) 337 return block->recomputeOpOrder(); 338 unsigned prevOrder = prevNode->orderIndex, nextOrder = nextNode->orderIndex; 339 340 // Check to see if there is a valid order between the two. 341 if (prevOrder + 1 == nextOrder) 342 return block->recomputeOpOrder(); 343 orderIndex = prevOrder + ((nextOrder - prevOrder) / 2); 344 } 345 346 //===----------------------------------------------------------------------===// 347 // ilist_traits for Operation 348 //===----------------------------------------------------------------------===// 349 350 auto llvm::ilist_detail::SpecificNodeAccess< 351 typename llvm::ilist_detail::compute_node_options< 352 ::mlir::Operation>::type>::getNodePtr(pointer N) -> node_type * { 353 return NodeAccess::getNodePtr<OptionsT>(N); 354 } 355 356 auto llvm::ilist_detail::SpecificNodeAccess< 357 typename llvm::ilist_detail::compute_node_options< 358 ::mlir::Operation>::type>::getNodePtr(const_pointer N) 359 -> const node_type * { 360 return NodeAccess::getNodePtr<OptionsT>(N); 361 } 362 363 auto llvm::ilist_detail::SpecificNodeAccess< 364 typename llvm::ilist_detail::compute_node_options< 365 ::mlir::Operation>::type>::getValuePtr(node_type *N) -> pointer { 366 return NodeAccess::getValuePtr<OptionsT>(N); 367 } 368 369 auto llvm::ilist_detail::SpecificNodeAccess< 370 typename llvm::ilist_detail::compute_node_options< 371 ::mlir::Operation>::type>::getValuePtr(const node_type *N) 372 -> const_pointer { 373 return NodeAccess::getValuePtr<OptionsT>(N); 374 } 375 376 void llvm::ilist_traits<::mlir::Operation>::deleteNode(Operation *op) { 377 op->destroy(); 378 } 379 380 Block *llvm::ilist_traits<::mlir::Operation>::getContainingBlock() { 381 size_t Offset(size_t(&((Block *)nullptr->*Block::getSublistAccess(nullptr)))); 382 iplist<Operation> *Anchor(static_cast<iplist<Operation> *>(this)); 383 return reinterpret_cast<Block *>(reinterpret_cast<char *>(Anchor) - Offset); 384 } 385 386 /// This is a trait method invoked when an operation is added to a block. We 387 /// keep the block pointer up to date. 388 void llvm::ilist_traits<::mlir::Operation>::addNodeToList(Operation *op) { 389 assert(!op->getBlock() && "already in an operation block!"); 390 op->block = getContainingBlock(); 391 392 // Invalidate the order on the operation. 393 op->orderIndex = Operation::kInvalidOrderIdx; 394 } 395 396 /// This is a trait method invoked when an operation is removed from a block. 397 /// We keep the block pointer up to date. 398 void llvm::ilist_traits<::mlir::Operation>::removeNodeFromList(Operation *op) { 399 assert(op->block && "not already in an operation block!"); 400 op->block = nullptr; 401 } 402 403 /// This is a trait method invoked when an operation is moved from one block 404 /// to another. We keep the block pointer up to date. 405 void llvm::ilist_traits<::mlir::Operation>::transferNodesFromList( 406 ilist_traits<Operation> &otherList, op_iterator first, op_iterator last) { 407 Block *curParent = getContainingBlock(); 408 409 // Invalidate the ordering of the parent block. 410 curParent->invalidateOpOrder(); 411 412 // If we are transferring operations within the same block, the block 413 // pointer doesn't need to be updated. 414 if (curParent == otherList.getContainingBlock()) 415 return; 416 417 // Update the 'block' member of each operation. 418 for (; first != last; ++first) 419 first->block = curParent; 420 } 421 422 /// Remove this operation (and its descendants) from its Block and delete 423 /// all of them. 424 void Operation::erase() { 425 if (auto *parent = getBlock()) 426 parent->getOperations().erase(this); 427 else 428 destroy(); 429 } 430 431 /// Remove the operation from its parent block, but don't delete it. 432 void Operation::remove() { 433 if (Block *parent = getBlock()) 434 parent->getOperations().remove(this); 435 } 436 437 /// Unlink this operation from its current block and insert it right before 438 /// `existingOp` which may be in the same or another block in the same 439 /// function. 440 void Operation::moveBefore(Operation *existingOp) { 441 moveBefore(existingOp->getBlock(), existingOp->getIterator()); 442 } 443 444 /// Unlink this operation from its current basic block and insert it right 445 /// before `iterator` in the specified basic block. 446 void Operation::moveBefore(Block *block, 447 llvm::iplist<Operation>::iterator iterator) { 448 block->getOperations().splice(iterator, getBlock()->getOperations(), 449 getIterator()); 450 } 451 452 /// Unlink this operation from its current block and insert it right after 453 /// `existingOp` which may be in the same or another block in the same function. 454 void Operation::moveAfter(Operation *existingOp) { 455 moveAfter(existingOp->getBlock(), existingOp->getIterator()); 456 } 457 458 /// Unlink this operation from its current block and insert it right after 459 /// `iterator` in the specified block. 460 void Operation::moveAfter(Block *block, 461 llvm::iplist<Operation>::iterator iterator) { 462 assert(iterator != block->end() && "cannot move after end of block"); 463 moveBefore(block, std::next(iterator)); 464 } 465 466 /// This drops all operand uses from this operation, which is an essential 467 /// step in breaking cyclic dependences between references when they are to 468 /// be deleted. 469 void Operation::dropAllReferences() { 470 for (auto &op : getOpOperands()) 471 op.drop(); 472 473 for (auto ®ion : getRegions()) 474 region.dropAllReferences(); 475 476 for (auto &dest : getBlockOperands()) 477 dest.drop(); 478 } 479 480 /// This drops all uses of any values defined by this operation or its nested 481 /// regions, wherever they are located. 482 void Operation::dropAllDefinedValueUses() { 483 dropAllUses(); 484 485 for (auto ®ion : getRegions()) 486 for (auto &block : region) 487 block.dropAllDefinedValueUses(); 488 } 489 490 void Operation::setSuccessor(Block *block, unsigned index) { 491 assert(index < getNumSuccessors()); 492 getBlockOperands()[index].set(block); 493 } 494 495 /// Attempt to fold this operation using the Op's registered foldHook. 496 LogicalResult Operation::fold(ArrayRef<Attribute> operands, 497 SmallVectorImpl<OpFoldResult> &results) { 498 // If we have a registered operation definition matching this one, use it to 499 // try to constant fold the operation. 500 Optional<RegisteredOperationName> info = getRegisteredInfo(); 501 if (info && succeeded(info->foldHook(this, operands, results))) 502 return success(); 503 504 // Otherwise, fall back on the dialect hook to handle it. 505 Dialect *dialect = getDialect(); 506 if (!dialect) 507 return failure(); 508 509 auto *interface = dialect->getRegisteredInterface<DialectFoldInterface>(); 510 if (!interface) 511 return failure(); 512 513 return interface->fold(this, operands, results); 514 } 515 516 /// Emit an error with the op name prefixed, like "'dim' op " which is 517 /// convenient for verifiers. 518 InFlightDiagnostic Operation::emitOpError(const Twine &message) { 519 return emitError() << "'" << getName() << "' op " << message; 520 } 521 522 //===----------------------------------------------------------------------===// 523 // Operation Cloning 524 //===----------------------------------------------------------------------===// 525 526 /// Create a deep copy of this operation but keep the operation regions empty. 527 /// Operands are remapped using `mapper` (if present), and `mapper` is updated 528 /// to contain the results. 529 Operation *Operation::cloneWithoutRegions(BlockAndValueMapping &mapper) { 530 SmallVector<Value, 8> operands; 531 SmallVector<Block *, 2> successors; 532 533 // Remap the operands. 534 operands.reserve(getNumOperands()); 535 for (auto opValue : getOperands()) 536 operands.push_back(mapper.lookupOrDefault(opValue)); 537 538 // Remap the successors. 539 successors.reserve(getNumSuccessors()); 540 for (Block *successor : getSuccessors()) 541 successors.push_back(mapper.lookupOrDefault(successor)); 542 543 // Create the new operation. 544 auto *newOp = create(getLoc(), getName(), getResultTypes(), operands, attrs, 545 successors, getNumRegions()); 546 547 // Remember the mapping of any results. 548 for (unsigned i = 0, e = getNumResults(); i != e; ++i) 549 mapper.map(getResult(i), newOp->getResult(i)); 550 551 return newOp; 552 } 553 554 Operation *Operation::cloneWithoutRegions() { 555 BlockAndValueMapping mapper; 556 return cloneWithoutRegions(mapper); 557 } 558 559 /// Create a deep copy of this operation, remapping any operands that use 560 /// values outside of the operation using the map that is provided (leaving 561 /// them alone if no entry is present). Replaces references to cloned 562 /// sub-operations to the corresponding operation that is copied, and adds 563 /// those mappings to the map. 564 Operation *Operation::clone(BlockAndValueMapping &mapper) { 565 auto *newOp = cloneWithoutRegions(mapper); 566 567 // Clone the regions. 568 for (unsigned i = 0; i != numRegions; ++i) 569 getRegion(i).cloneInto(&newOp->getRegion(i), mapper); 570 571 return newOp; 572 } 573 574 Operation *Operation::clone() { 575 BlockAndValueMapping mapper; 576 return clone(mapper); 577 } 578 579 //===----------------------------------------------------------------------===// 580 // OpState trait class. 581 //===----------------------------------------------------------------------===// 582 583 // The fallback for the parser is to reject the custom assembly form. 584 ParseResult OpState::parse(OpAsmParser &parser, OperationState &result) { 585 return parser.emitError(parser.getNameLoc(), "has no custom assembly form"); 586 } 587 588 // The fallback for the printer is to print in the generic assembly form. 589 void OpState::print(Operation *op, OpAsmPrinter &p) { p.printGenericOp(op); } 590 // The fallback for the printer is to print in the generic assembly form. 591 void OpState::printOpName(Operation *op, OpAsmPrinter &p, 592 StringRef defaultDialect) { 593 StringRef name = op->getName().getStringRef(); 594 if (name.startswith((defaultDialect + ".").str())) 595 name = name.drop_front(defaultDialect.size() + 1); 596 // TODO: remove this special case (and update test/IR/parser.mlir) 597 else if ((defaultDialect.empty() || defaultDialect == "builtin") && 598 name.startswith("std.")) 599 name = name.drop_front(4); 600 p.getStream() << name; 601 } 602 603 /// Emit an error about fatal conditions with this operation, reporting up to 604 /// any diagnostic handlers that may be listening. 605 InFlightDiagnostic OpState::emitError(const Twine &message) { 606 return getOperation()->emitError(message); 607 } 608 609 /// Emit an error with the op name prefixed, like "'dim' op " which is 610 /// convenient for verifiers. 611 InFlightDiagnostic OpState::emitOpError(const Twine &message) { 612 return getOperation()->emitOpError(message); 613 } 614 615 /// Emit a warning about this operation, reporting up to any diagnostic 616 /// handlers that may be listening. 617 InFlightDiagnostic OpState::emitWarning(const Twine &message) { 618 return getOperation()->emitWarning(message); 619 } 620 621 /// Emit a remark about this operation, reporting up to any diagnostic 622 /// handlers that may be listening. 623 InFlightDiagnostic OpState::emitRemark(const Twine &message) { 624 return getOperation()->emitRemark(message); 625 } 626 627 //===----------------------------------------------------------------------===// 628 // Op Trait implementations 629 //===----------------------------------------------------------------------===// 630 631 OpFoldResult OpTrait::impl::foldIdempotent(Operation *op) { 632 auto *argumentOp = op->getOperand(0).getDefiningOp(); 633 if (argumentOp && op->getName() == argumentOp->getName()) { 634 // Replace the outer operation output with the inner operation. 635 return op->getOperand(0); 636 } 637 638 return {}; 639 } 640 641 OpFoldResult OpTrait::impl::foldInvolution(Operation *op) { 642 auto *argumentOp = op->getOperand(0).getDefiningOp(); 643 if (argumentOp && op->getName() == argumentOp->getName()) { 644 // Replace the outer involutions output with inner's input. 645 return argumentOp->getOperand(0); 646 } 647 648 return {}; 649 } 650 651 LogicalResult OpTrait::impl::verifyZeroOperands(Operation *op) { 652 if (op->getNumOperands() != 0) 653 return op->emitOpError() << "requires zero operands"; 654 return success(); 655 } 656 657 LogicalResult OpTrait::impl::verifyOneOperand(Operation *op) { 658 if (op->getNumOperands() != 1) 659 return op->emitOpError() << "requires a single operand"; 660 return success(); 661 } 662 663 LogicalResult OpTrait::impl::verifyNOperands(Operation *op, 664 unsigned numOperands) { 665 if (op->getNumOperands() != numOperands) { 666 return op->emitOpError() << "expected " << numOperands 667 << " operands, but found " << op->getNumOperands(); 668 } 669 return success(); 670 } 671 672 LogicalResult OpTrait::impl::verifyAtLeastNOperands(Operation *op, 673 unsigned numOperands) { 674 if (op->getNumOperands() < numOperands) 675 return op->emitOpError() 676 << "expected " << numOperands << " or more operands, but found " 677 << op->getNumOperands(); 678 return success(); 679 } 680 681 /// If this is a vector type, or a tensor type, return the scalar element type 682 /// that it is built around, otherwise return the type unmodified. 683 static Type getTensorOrVectorElementType(Type type) { 684 if (auto vec = type.dyn_cast<VectorType>()) 685 return vec.getElementType(); 686 687 // Look through tensor<vector<...>> to find the underlying element type. 688 if (auto tensor = type.dyn_cast<TensorType>()) 689 return getTensorOrVectorElementType(tensor.getElementType()); 690 return type; 691 } 692 693 LogicalResult OpTrait::impl::verifyIsIdempotent(Operation *op) { 694 // FIXME: Add back check for no side effects on operation. 695 // Currently adding it would cause the shared library build 696 // to fail since there would be a dependency of IR on SideEffectInterfaces 697 // which is cyclical. 698 return success(); 699 } 700 701 LogicalResult OpTrait::impl::verifyIsInvolution(Operation *op) { 702 // FIXME: Add back check for no side effects on operation. 703 // Currently adding it would cause the shared library build 704 // to fail since there would be a dependency of IR on SideEffectInterfaces 705 // which is cyclical. 706 return success(); 707 } 708 709 LogicalResult 710 OpTrait::impl::verifyOperandsAreSignlessIntegerLike(Operation *op) { 711 for (auto opType : op->getOperandTypes()) { 712 auto type = getTensorOrVectorElementType(opType); 713 if (!type.isSignlessIntOrIndex()) 714 return op->emitOpError() << "requires an integer or index type"; 715 } 716 return success(); 717 } 718 719 LogicalResult OpTrait::impl::verifyOperandsAreFloatLike(Operation *op) { 720 for (auto opType : op->getOperandTypes()) { 721 auto type = getTensorOrVectorElementType(opType); 722 if (!type.isa<FloatType>()) 723 return op->emitOpError("requires a float type"); 724 } 725 return success(); 726 } 727 728 LogicalResult OpTrait::impl::verifySameTypeOperands(Operation *op) { 729 // Zero or one operand always have the "same" type. 730 unsigned nOperands = op->getNumOperands(); 731 if (nOperands < 2) 732 return success(); 733 734 auto type = op->getOperand(0).getType(); 735 for (auto opType : llvm::drop_begin(op->getOperandTypes(), 1)) 736 if (opType != type) 737 return op->emitOpError() << "requires all operands to have the same type"; 738 return success(); 739 } 740 741 LogicalResult OpTrait::impl::verifyZeroRegion(Operation *op) { 742 if (op->getNumRegions() != 0) 743 return op->emitOpError() << "requires zero regions"; 744 return success(); 745 } 746 747 LogicalResult OpTrait::impl::verifyOneRegion(Operation *op) { 748 if (op->getNumRegions() != 1) 749 return op->emitOpError() << "requires one region"; 750 return success(); 751 } 752 753 LogicalResult OpTrait::impl::verifyNRegions(Operation *op, 754 unsigned numRegions) { 755 if (op->getNumRegions() != numRegions) 756 return op->emitOpError() << "expected " << numRegions << " regions"; 757 return success(); 758 } 759 760 LogicalResult OpTrait::impl::verifyAtLeastNRegions(Operation *op, 761 unsigned numRegions) { 762 if (op->getNumRegions() < numRegions) 763 return op->emitOpError() << "expected " << numRegions << " or more regions"; 764 return success(); 765 } 766 767 LogicalResult OpTrait::impl::verifyZeroResult(Operation *op) { 768 if (op->getNumResults() != 0) 769 return op->emitOpError() << "requires zero results"; 770 return success(); 771 } 772 773 LogicalResult OpTrait::impl::verifyOneResult(Operation *op) { 774 if (op->getNumResults() != 1) 775 return op->emitOpError() << "requires one result"; 776 return success(); 777 } 778 779 LogicalResult OpTrait::impl::verifyNResults(Operation *op, 780 unsigned numOperands) { 781 if (op->getNumResults() != numOperands) 782 return op->emitOpError() << "expected " << numOperands << " results"; 783 return success(); 784 } 785 786 LogicalResult OpTrait::impl::verifyAtLeastNResults(Operation *op, 787 unsigned numOperands) { 788 if (op->getNumResults() < numOperands) 789 return op->emitOpError() 790 << "expected " << numOperands << " or more results"; 791 return success(); 792 } 793 794 LogicalResult OpTrait::impl::verifySameOperandsShape(Operation *op) { 795 if (failed(verifyAtLeastNOperands(op, 1))) 796 return failure(); 797 798 if (failed(verifyCompatibleShapes(op->getOperandTypes()))) 799 return op->emitOpError() << "requires the same shape for all operands"; 800 801 return success(); 802 } 803 804 LogicalResult OpTrait::impl::verifySameOperandsAndResultShape(Operation *op) { 805 if (failed(verifyAtLeastNOperands(op, 1)) || 806 failed(verifyAtLeastNResults(op, 1))) 807 return failure(); 808 809 SmallVector<Type, 8> types(op->getOperandTypes()); 810 types.append(llvm::to_vector<4>(op->getResultTypes())); 811 812 if (failed(verifyCompatibleShapes(types))) 813 return op->emitOpError() 814 << "requires the same shape for all operands and results"; 815 816 return success(); 817 } 818 819 LogicalResult OpTrait::impl::verifySameOperandsElementType(Operation *op) { 820 if (failed(verifyAtLeastNOperands(op, 1))) 821 return failure(); 822 auto elementType = getElementTypeOrSelf(op->getOperand(0)); 823 824 for (auto operand : llvm::drop_begin(op->getOperands(), 1)) { 825 if (getElementTypeOrSelf(operand) != elementType) 826 return op->emitOpError("requires the same element type for all operands"); 827 } 828 829 return success(); 830 } 831 832 LogicalResult 833 OpTrait::impl::verifySameOperandsAndResultElementType(Operation *op) { 834 if (failed(verifyAtLeastNOperands(op, 1)) || 835 failed(verifyAtLeastNResults(op, 1))) 836 return failure(); 837 838 auto elementType = getElementTypeOrSelf(op->getResult(0)); 839 840 // Verify result element type matches first result's element type. 841 for (auto result : llvm::drop_begin(op->getResults(), 1)) { 842 if (getElementTypeOrSelf(result) != elementType) 843 return op->emitOpError( 844 "requires the same element type for all operands and results"); 845 } 846 847 // Verify operand's element type matches first result's element type. 848 for (auto operand : op->getOperands()) { 849 if (getElementTypeOrSelf(operand) != elementType) 850 return op->emitOpError( 851 "requires the same element type for all operands and results"); 852 } 853 854 return success(); 855 } 856 857 LogicalResult OpTrait::impl::verifySameOperandsAndResultType(Operation *op) { 858 if (failed(verifyAtLeastNOperands(op, 1)) || 859 failed(verifyAtLeastNResults(op, 1))) 860 return failure(); 861 862 auto type = op->getResult(0).getType(); 863 auto elementType = getElementTypeOrSelf(type); 864 for (auto resultType : llvm::drop_begin(op->getResultTypes())) { 865 if (getElementTypeOrSelf(resultType) != elementType || 866 failed(verifyCompatibleShape(resultType, type))) 867 return op->emitOpError() 868 << "requires the same type for all operands and results"; 869 } 870 for (auto opType : op->getOperandTypes()) { 871 if (getElementTypeOrSelf(opType) != elementType || 872 failed(verifyCompatibleShape(opType, type))) 873 return op->emitOpError() 874 << "requires the same type for all operands and results"; 875 } 876 return success(); 877 } 878 879 LogicalResult OpTrait::impl::verifyIsTerminator(Operation *op) { 880 Block *block = op->getBlock(); 881 // Verify that the operation is at the end of the respective parent block. 882 if (!block || &block->back() != op) 883 return op->emitOpError("must be the last operation in the parent block"); 884 return success(); 885 } 886 887 static LogicalResult verifyTerminatorSuccessors(Operation *op) { 888 auto *parent = op->getParentRegion(); 889 890 // Verify that the operands lines up with the BB arguments in the successor. 891 for (Block *succ : op->getSuccessors()) 892 if (succ->getParent() != parent) 893 return op->emitError("reference to block defined in another region"); 894 return success(); 895 } 896 897 LogicalResult OpTrait::impl::verifyZeroSuccessor(Operation *op) { 898 if (op->getNumSuccessors() != 0) { 899 return op->emitOpError("requires 0 successors but found ") 900 << op->getNumSuccessors(); 901 } 902 return success(); 903 } 904 905 LogicalResult OpTrait::impl::verifyOneSuccessor(Operation *op) { 906 if (op->getNumSuccessors() != 1) { 907 return op->emitOpError("requires 1 successor but found ") 908 << op->getNumSuccessors(); 909 } 910 return verifyTerminatorSuccessors(op); 911 } 912 LogicalResult OpTrait::impl::verifyNSuccessors(Operation *op, 913 unsigned numSuccessors) { 914 if (op->getNumSuccessors() != numSuccessors) { 915 return op->emitOpError("requires ") 916 << numSuccessors << " successors but found " 917 << op->getNumSuccessors(); 918 } 919 return verifyTerminatorSuccessors(op); 920 } 921 LogicalResult OpTrait::impl::verifyAtLeastNSuccessors(Operation *op, 922 unsigned numSuccessors) { 923 if (op->getNumSuccessors() < numSuccessors) { 924 return op->emitOpError("requires at least ") 925 << numSuccessors << " successors but found " 926 << op->getNumSuccessors(); 927 } 928 return verifyTerminatorSuccessors(op); 929 } 930 931 LogicalResult OpTrait::impl::verifyResultsAreBoolLike(Operation *op) { 932 for (auto resultType : op->getResultTypes()) { 933 auto elementType = getTensorOrVectorElementType(resultType); 934 bool isBoolType = elementType.isInteger(1); 935 if (!isBoolType) 936 return op->emitOpError() << "requires a bool result type"; 937 } 938 939 return success(); 940 } 941 942 LogicalResult OpTrait::impl::verifyResultsAreFloatLike(Operation *op) { 943 for (auto resultType : op->getResultTypes()) 944 if (!getTensorOrVectorElementType(resultType).isa<FloatType>()) 945 return op->emitOpError() << "requires a floating point type"; 946 947 return success(); 948 } 949 950 LogicalResult 951 OpTrait::impl::verifyResultsAreSignlessIntegerLike(Operation *op) { 952 for (auto resultType : op->getResultTypes()) 953 if (!getTensorOrVectorElementType(resultType).isSignlessIntOrIndex()) 954 return op->emitOpError() << "requires an integer or index type"; 955 return success(); 956 } 957 958 LogicalResult OpTrait::impl::verifyValueSizeAttr(Operation *op, 959 StringRef attrName, 960 StringRef valueGroupName, 961 size_t expectedCount) { 962 auto sizeAttr = op->getAttrOfType<DenseIntElementsAttr>(attrName); 963 if (!sizeAttr) 964 return op->emitOpError("requires 1D i32 elements attribute '") 965 << attrName << "'"; 966 967 auto sizeAttrType = sizeAttr.getType(); 968 if (sizeAttrType.getRank() != 1 || 969 !sizeAttrType.getElementType().isInteger(32)) 970 return op->emitOpError("requires 1D i32 elements attribute '") 971 << attrName << "'"; 972 973 if (llvm::any_of(sizeAttr.getValues<APInt>(), [](const APInt &element) { 974 return !element.isNonNegative(); 975 })) 976 return op->emitOpError("'") 977 << attrName << "' attribute cannot have negative elements"; 978 979 size_t totalCount = std::accumulate( 980 sizeAttr.begin(), sizeAttr.end(), 0, 981 [](unsigned all, APInt one) { return all + one.getZExtValue(); }); 982 983 if (totalCount != expectedCount) 984 return op->emitOpError() 985 << valueGroupName << " count (" << expectedCount 986 << ") does not match with the total size (" << totalCount 987 << ") specified in attribute '" << attrName << "'"; 988 return success(); 989 } 990 991 LogicalResult OpTrait::impl::verifyOperandSizeAttr(Operation *op, 992 StringRef attrName) { 993 return verifyValueSizeAttr(op, attrName, "operand", op->getNumOperands()); 994 } 995 996 LogicalResult OpTrait::impl::verifyResultSizeAttr(Operation *op, 997 StringRef attrName) { 998 return verifyValueSizeAttr(op, attrName, "result", op->getNumResults()); 999 } 1000 1001 LogicalResult OpTrait::impl::verifyNoRegionArguments(Operation *op) { 1002 for (Region ®ion : op->getRegions()) { 1003 if (region.empty()) 1004 continue; 1005 1006 if (region.getNumArguments() != 0) { 1007 if (op->getNumRegions() > 1) 1008 return op->emitOpError("region #") 1009 << region.getRegionNumber() << " should have no arguments"; 1010 else 1011 return op->emitOpError("region should have no arguments"); 1012 } 1013 } 1014 return success(); 1015 } 1016 1017 LogicalResult OpTrait::impl::verifyElementwise(Operation *op) { 1018 auto isMappableType = [](Type type) { 1019 return type.isa<VectorType, TensorType>(); 1020 }; 1021 auto resultMappableTypes = llvm::to_vector<1>( 1022 llvm::make_filter_range(op->getResultTypes(), isMappableType)); 1023 auto operandMappableTypes = llvm::to_vector<2>( 1024 llvm::make_filter_range(op->getOperandTypes(), isMappableType)); 1025 1026 // If the op only has scalar operand/result types, then we have nothing to 1027 // check. 1028 if (resultMappableTypes.empty() && operandMappableTypes.empty()) 1029 return success(); 1030 1031 if (!resultMappableTypes.empty() && operandMappableTypes.empty()) 1032 return op->emitOpError("if a result is non-scalar, then at least one " 1033 "operand must be non-scalar"); 1034 1035 assert(!operandMappableTypes.empty()); 1036 1037 if (resultMappableTypes.empty()) 1038 return op->emitOpError("if an operand is non-scalar, then there must be at " 1039 "least one non-scalar result"); 1040 1041 if (resultMappableTypes.size() != op->getNumResults()) 1042 return op->emitOpError( 1043 "if an operand is non-scalar, then all results must be non-scalar"); 1044 1045 SmallVector<Type, 4> types = llvm::to_vector<2>( 1046 llvm::concat<Type>(operandMappableTypes, resultMappableTypes)); 1047 TypeID expectedBaseTy = types.front().getTypeID(); 1048 if (!llvm::all_of(types, 1049 [&](Type t) { return t.getTypeID() == expectedBaseTy; }) || 1050 failed(verifyCompatibleShapes(types))) { 1051 return op->emitOpError() << "all non-scalar operands/results must have the " 1052 "same shape and base type"; 1053 } 1054 1055 return success(); 1056 } 1057 1058 /// Check for any values used by operations regions attached to the 1059 /// specified "IsIsolatedFromAbove" operation defined outside of it. 1060 LogicalResult OpTrait::impl::verifyIsIsolatedFromAbove(Operation *isolatedOp) { 1061 assert(isolatedOp->hasTrait<OpTrait::IsIsolatedFromAbove>() && 1062 "Intended to check IsolatedFromAbove ops"); 1063 1064 // List of regions to analyze. Each region is processed independently, with 1065 // respect to the common `limit` region, so we can look at them in any order. 1066 // Therefore, use a simple vector and push/pop back the current region. 1067 SmallVector<Region *, 8> pendingRegions; 1068 for (auto ®ion : isolatedOp->getRegions()) { 1069 pendingRegions.push_back(®ion); 1070 1071 // Traverse all operations in the region. 1072 while (!pendingRegions.empty()) { 1073 for (Operation &op : pendingRegions.pop_back_val()->getOps()) { 1074 for (Value operand : op.getOperands()) { 1075 // operand should be non-null here if the IR is well-formed. But 1076 // we don't assert here as this function is called from the verifier 1077 // and so could be called on invalid IR. 1078 if (!operand) 1079 return op.emitOpError("operation's operand is null"); 1080 1081 // Check that any value that is used by an operation is defined in the 1082 // same region as either an operation result. 1083 auto *operandRegion = operand.getParentRegion(); 1084 if (!region.isAncestor(operandRegion)) { 1085 return op.emitOpError("using value defined outside the region") 1086 .attachNote(isolatedOp->getLoc()) 1087 << "required by region isolation constraints"; 1088 } 1089 } 1090 1091 // Schedule any regions in the operation for further checking. Don't 1092 // recurse into other IsolatedFromAbove ops, because they will check 1093 // themselves. 1094 if (op.getNumRegions() && 1095 !op.hasTrait<OpTrait::IsIsolatedFromAbove>()) { 1096 for (Region &subRegion : op.getRegions()) 1097 pendingRegions.push_back(&subRegion); 1098 } 1099 } 1100 } 1101 } 1102 1103 return success(); 1104 } 1105 1106 bool OpTrait::hasElementwiseMappableTraits(Operation *op) { 1107 return op->hasTrait<Elementwise>() && op->hasTrait<Scalarizable>() && 1108 op->hasTrait<Vectorizable>() && op->hasTrait<Tensorizable>(); 1109 } 1110 1111 //===----------------------------------------------------------------------===// 1112 // BinaryOp implementation 1113 //===----------------------------------------------------------------------===// 1114 1115 // These functions are out-of-line implementations of the methods in BinaryOp, 1116 // which avoids them being template instantiated/duplicated. 1117 1118 void impl::buildBinaryOp(OpBuilder &builder, OperationState &result, Value lhs, 1119 Value rhs) { 1120 assert(lhs.getType() == rhs.getType()); 1121 result.addOperands({lhs, rhs}); 1122 result.types.push_back(lhs.getType()); 1123 } 1124 1125 ParseResult impl::parseOneResultSameOperandTypeOp(OpAsmParser &parser, 1126 OperationState &result) { 1127 SmallVector<OpAsmParser::OperandType, 2> ops; 1128 Type type; 1129 // If the operand list is in-between parentheses, then we have a generic form. 1130 // (see the fallback in `printOneResultOp`). 1131 llvm::SMLoc loc = parser.getCurrentLocation(); 1132 if (!parser.parseOptionalLParen()) { 1133 if (parser.parseOperandList(ops) || parser.parseRParen() || 1134 parser.parseOptionalAttrDict(result.attributes) || 1135 parser.parseColon() || parser.parseType(type)) 1136 return failure(); 1137 auto fnType = type.dyn_cast<FunctionType>(); 1138 if (!fnType) { 1139 parser.emitError(loc, "expected function type"); 1140 return failure(); 1141 } 1142 if (parser.resolveOperands(ops, fnType.getInputs(), loc, result.operands)) 1143 return failure(); 1144 result.addTypes(fnType.getResults()); 1145 return success(); 1146 } 1147 return failure(parser.parseOperandList(ops) || 1148 parser.parseOptionalAttrDict(result.attributes) || 1149 parser.parseColonType(type) || 1150 parser.resolveOperands(ops, type, result.operands) || 1151 parser.addTypeToList(type, result.types)); 1152 } 1153 1154 void impl::printOneResultOp(Operation *op, OpAsmPrinter &p) { 1155 assert(op->getNumResults() == 1 && "op should have one result"); 1156 1157 // If not all the operand and result types are the same, just use the 1158 // generic assembly form to avoid omitting information in printing. 1159 auto resultType = op->getResult(0).getType(); 1160 if (llvm::any_of(op->getOperandTypes(), 1161 [&](Type type) { return type != resultType; })) { 1162 p.printGenericOp(op, /*printOpName=*/false); 1163 return; 1164 } 1165 1166 p << ' '; 1167 p.printOperands(op->getOperands()); 1168 p.printOptionalAttrDict(op->getAttrs()); 1169 // Now we can output only one type for all operands and the result. 1170 p << " : " << resultType; 1171 } 1172 1173 //===----------------------------------------------------------------------===// 1174 // CastOp implementation 1175 //===----------------------------------------------------------------------===// 1176 1177 /// Attempt to fold the given cast operation. 1178 LogicalResult 1179 impl::foldCastInterfaceOp(Operation *op, ArrayRef<Attribute> attrOperands, 1180 SmallVectorImpl<OpFoldResult> &foldResults) { 1181 OperandRange operands = op->getOperands(); 1182 if (operands.empty()) 1183 return failure(); 1184 ResultRange results = op->getResults(); 1185 1186 // Check for the case where the input and output types match 1-1. 1187 if (operands.getTypes() == results.getTypes()) { 1188 foldResults.append(operands.begin(), operands.end()); 1189 return success(); 1190 } 1191 1192 return failure(); 1193 } 1194 1195 /// Attempt to verify the given cast operation. 1196 LogicalResult impl::verifyCastInterfaceOp( 1197 Operation *op, function_ref<bool(TypeRange, TypeRange)> areCastCompatible) { 1198 auto resultTypes = op->getResultTypes(); 1199 if (llvm::empty(resultTypes)) 1200 return op->emitOpError() 1201 << "expected at least one result for cast operation"; 1202 1203 auto operandTypes = op->getOperandTypes(); 1204 if (!areCastCompatible(operandTypes, resultTypes)) { 1205 InFlightDiagnostic diag = op->emitOpError("operand type"); 1206 if (llvm::empty(operandTypes)) 1207 diag << "s []"; 1208 else if (llvm::size(operandTypes) == 1) 1209 diag << " " << *operandTypes.begin(); 1210 else 1211 diag << "s " << operandTypes; 1212 return diag << " and result type" << (resultTypes.size() == 1 ? " " : "s ") 1213 << resultTypes << " are cast incompatible"; 1214 } 1215 1216 return success(); 1217 } 1218 1219 void impl::buildCastOp(OpBuilder &builder, OperationState &result, Value source, 1220 Type destType) { 1221 result.addOperands(source); 1222 result.addTypes(destType); 1223 } 1224 1225 ParseResult impl::parseCastOp(OpAsmParser &parser, OperationState &result) { 1226 OpAsmParser::OperandType srcInfo; 1227 Type srcType, dstType; 1228 return failure(parser.parseOperand(srcInfo) || 1229 parser.parseOptionalAttrDict(result.attributes) || 1230 parser.parseColonType(srcType) || 1231 parser.resolveOperand(srcInfo, srcType, result.operands) || 1232 parser.parseKeywordType("to", dstType) || 1233 parser.addTypeToList(dstType, result.types)); 1234 } 1235 1236 void impl::printCastOp(Operation *op, OpAsmPrinter &p) { 1237 p << ' ' << op->getOperand(0); 1238 p.printOptionalAttrDict(op->getAttrs()); 1239 p << " : " << op->getOperand(0).getType() << " to " 1240 << op->getResult(0).getType(); 1241 } 1242 1243 Value impl::foldCastOp(Operation *op) { 1244 // Identity cast 1245 if (op->getOperand(0).getType() == op->getResult(0).getType()) 1246 return op->getOperand(0); 1247 return nullptr; 1248 } 1249 1250 LogicalResult 1251 impl::verifyCastOp(Operation *op, 1252 function_ref<bool(Type, Type)> areCastCompatible) { 1253 auto opType = op->getOperand(0).getType(); 1254 auto resType = op->getResult(0).getType(); 1255 if (!areCastCompatible(opType, resType)) 1256 return op->emitError("operand type ") 1257 << opType << " and result type " << resType 1258 << " are cast incompatible"; 1259 1260 return success(); 1261 } 1262 1263 //===----------------------------------------------------------------------===// 1264 // Misc. utils 1265 //===----------------------------------------------------------------------===// 1266 1267 /// Insert an operation, generated by `buildTerminatorOp`, at the end of the 1268 /// region's only block if it does not have a terminator already. If the region 1269 /// is empty, insert a new block first. `buildTerminatorOp` should return the 1270 /// terminator operation to insert. 1271 void impl::ensureRegionTerminator( 1272 Region ®ion, OpBuilder &builder, Location loc, 1273 function_ref<Operation *(OpBuilder &, Location)> buildTerminatorOp) { 1274 OpBuilder::InsertionGuard guard(builder); 1275 if (region.empty()) 1276 builder.createBlock(®ion); 1277 1278 Block &block = region.back(); 1279 if (!block.empty() && block.back().hasTrait<OpTrait::IsTerminator>()) 1280 return; 1281 1282 builder.setInsertionPointToEnd(&block); 1283 builder.insert(buildTerminatorOp(builder, loc)); 1284 } 1285 1286 /// Create a simple OpBuilder and forward to the OpBuilder version of this 1287 /// function. 1288 void impl::ensureRegionTerminator( 1289 Region ®ion, Builder &builder, Location loc, 1290 function_ref<Operation *(OpBuilder &, Location)> buildTerminatorOp) { 1291 OpBuilder opBuilder(builder.getContext()); 1292 ensureRegionTerminator(region, opBuilder, loc, buildTerminatorOp); 1293 } 1294