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