1 //===- Operation.h - MLIR Operation Class -----------------------*- C++ -*-===// 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 // This file defines the Operation class. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef MLIR_IR_OPERATION_H 14 #define MLIR_IR_OPERATION_H 15 16 #include "mlir/IR/Block.h" 17 #include "mlir/IR/BuiltinAttributes.h" 18 #include "mlir/IR/Diagnostics.h" 19 #include "mlir/IR/OperationSupport.h" 20 #include "mlir/IR/Region.h" 21 #include "llvm/ADT/Twine.h" 22 23 namespace mlir { 24 /// Operation is a basic unit of execution within MLIR. Operations can 25 /// be nested within `Region`s held by other operations effectively forming a 26 /// tree. Child operations are organized into operation blocks represented by a 27 /// 'Block' class. 28 class alignas(8) Operation final 29 : public llvm::ilist_node_with_parent<Operation, Block>, 30 private llvm::TrailingObjects<Operation, BlockOperand, Region, 31 detail::OperandStorage> { 32 public: 33 /// Create a new Operation with the specific fields. 34 static Operation *create(Location location, OperationName name, 35 TypeRange resultTypes, ValueRange operands, 36 ArrayRef<NamedAttribute> attributes, 37 BlockRange successors, unsigned numRegions); 38 39 /// Overload of create that takes an existing DictionaryAttr to avoid 40 /// unnecessarily uniquing a list of attributes. 41 static Operation *create(Location location, OperationName name, 42 TypeRange resultTypes, ValueRange operands, 43 DictionaryAttr attributes, BlockRange successors, 44 unsigned numRegions); 45 46 /// Create a new Operation from the fields stored in `state`. 47 static Operation *create(const OperationState &state); 48 49 /// Create a new Operation with the specific fields. 50 static Operation *create(Location location, OperationName name, 51 TypeRange resultTypes, ValueRange operands, 52 DictionaryAttr attributes, 53 BlockRange successors = {}, 54 RegionRange regions = {}); 55 56 /// The name of an operation is the key identifier for it. getName()57 OperationName getName() { return name; } 58 59 /// If this operation has a registered operation description, return it. 60 /// Otherwise return null. getAbstractOperation()61 const AbstractOperation *getAbstractOperation() { 62 return getName().getAbstractOperation(); 63 } 64 65 /// Returns true if this operation has a registered operation description, 66 /// otherwise false. isRegistered()67 bool isRegistered() { return getAbstractOperation(); } 68 69 /// Remove this operation from its parent block and delete it. 70 void erase(); 71 72 /// Remove the operation from its parent block, but don't delete it. 73 void remove(); 74 75 /// Create a deep copy of this operation, remapping any operands that use 76 /// values outside of the operation using the map that is provided (leaving 77 /// them alone if no entry is present). Replaces references to cloned 78 /// sub-operations to the corresponding operation that is copied, and adds 79 /// those mappings to the map. 80 Operation *clone(BlockAndValueMapping &mapper); 81 Operation *clone(); 82 83 /// Create a partial copy of this operation without traversing into attached 84 /// regions. The new operation will have the same number of regions as the 85 /// original one, but they will be left empty. 86 /// Operands are remapped using `mapper` (if present), and `mapper` is updated 87 /// to contain the results. 88 Operation *cloneWithoutRegions(BlockAndValueMapping &mapper); 89 90 /// Create a partial copy of this operation without traversing into attached 91 /// regions. The new operation will have the same number of regions as the 92 /// original one, but they will be left empty. 93 Operation *cloneWithoutRegions(); 94 95 /// Returns the operation block that contains this operation. getBlock()96 Block *getBlock() { return block; } 97 98 /// Return the context this operation is associated with. 99 MLIRContext *getContext(); 100 101 /// Return the dialect this operation is associated with, or nullptr if the 102 /// associated dialect is not registered. 103 Dialect *getDialect(); 104 105 /// The source location the operation was defined or derived from. getLoc()106 Location getLoc() { return location; } 107 108 /// Set the source location the operation was defined or derived from. setLoc(Location loc)109 void setLoc(Location loc) { location = loc; } 110 111 /// Returns the region to which the instruction belongs. Returns nullptr if 112 /// the instruction is unlinked. 113 Region *getParentRegion(); 114 115 /// Returns the closest surrounding operation that contains this operation 116 /// or nullptr if this is a top-level operation. 117 Operation *getParentOp(); 118 119 /// Return the closest surrounding parent operation that is of type 'OpTy'. getParentOfType()120 template <typename OpTy> OpTy getParentOfType() { 121 auto *op = this; 122 while ((op = op->getParentOp())) 123 if (auto parentOp = dyn_cast<OpTy>(op)) 124 return parentOp; 125 return OpTy(); 126 } 127 128 /// Returns the closest surrounding parent operation with trait `Trait`. 129 template <template <typename T> class Trait> getParentWithTrait()130 Operation *getParentWithTrait() { 131 Operation *op = this; 132 while ((op = op->getParentOp())) 133 if (op->hasTrait<Trait>()) 134 return op; 135 return nullptr; 136 } 137 138 /// Return true if this operation is a proper ancestor of the `other` 139 /// operation. 140 bool isProperAncestor(Operation *other); 141 142 /// Return true if this operation is an ancestor of the `other` operation. An 143 /// operation is considered as its own ancestor, use `isProperAncestor` to 144 /// avoid this. isAncestor(Operation * other)145 bool isAncestor(Operation *other) { 146 return this == other || isProperAncestor(other); 147 } 148 149 /// Replace any uses of 'from' with 'to' within this operation. 150 void replaceUsesOfWith(Value from, Value to); 151 152 /// Replace all uses of results of this operation with the provided 'values'. 153 template <typename ValuesT> 154 std::enable_if_t<!std::is_convertible<ValuesT, Operation *>::value> replaceAllUsesWith(ValuesT && values)155 replaceAllUsesWith(ValuesT &&values) { 156 assert(std::distance(values.begin(), values.end()) == getNumResults() && 157 "expected 'values' to correspond 1-1 with the number of results"); 158 159 auto valueIt = values.begin(); 160 for (unsigned i = 0, e = getNumResults(); i != e; ++i) 161 getResult(i).replaceAllUsesWith(*(valueIt++)); 162 } 163 164 /// Replace all uses of results of this operation with results of 'op'. replaceAllUsesWith(Operation * op)165 void replaceAllUsesWith(Operation *op) { 166 assert(getNumResults() == op->getNumResults()); 167 for (unsigned i = 0, e = getNumResults(); i != e; ++i) 168 getResult(i).replaceAllUsesWith(op->getResult(i)); 169 } 170 171 /// Destroys this operation and its subclass data. 172 void destroy(); 173 174 /// This drops all operand uses from this operation, which is an essential 175 /// step in breaking cyclic dependences between references when they are to 176 /// be deleted. 177 void dropAllReferences(); 178 179 /// Drop uses of all values defined by this operation or its nested regions. 180 void dropAllDefinedValueUses(); 181 182 /// Unlink this operation from its current block and insert it right before 183 /// `existingOp` which may be in the same or another block in the same 184 /// function. 185 void moveBefore(Operation *existingOp); 186 187 /// Unlink this operation from its current block and insert it right before 188 /// `iterator` in the specified block. 189 void moveBefore(Block *block, llvm::iplist<Operation>::iterator iterator); 190 191 /// Unlink this operation from its current block and insert it right after 192 /// `existingOp` which may be in the same or another block in the same 193 /// function. 194 void moveAfter(Operation *existingOp); 195 196 /// Unlink this operation from its current block and insert it right after 197 /// `iterator` in the specified block. 198 void moveAfter(Block *block, llvm::iplist<Operation>::iterator iterator); 199 200 /// Given an operation 'other' that is within the same parent block, return 201 /// whether the current operation is before 'other' in the operation list 202 /// of the parent block. 203 /// Note: This function has an average complexity of O(1), but worst case may 204 /// take O(N) where N is the number of operations within the parent block. 205 bool isBeforeInBlock(Operation *other); 206 207 void print(raw_ostream &os, OpPrintingFlags flags = llvm::None); 208 void print(raw_ostream &os, AsmState &state, 209 OpPrintingFlags flags = llvm::None); 210 void dump(); 211 212 //===--------------------------------------------------------------------===// 213 // Operands 214 //===--------------------------------------------------------------------===// 215 216 /// Replace the current operands of this operation with the ones provided in 217 /// 'operands'. 218 void setOperands(ValueRange operands); 219 220 /// Replace the operands beginning at 'start' and ending at 'start' + 'length' 221 /// with the ones provided in 'operands'. 'operands' may be smaller or larger 222 /// than the range pointed to by 'start'+'length'. 223 void setOperands(unsigned start, unsigned length, ValueRange operands); 224 225 /// Insert the given operands into the operand list at the given 'index'. 226 void insertOperands(unsigned index, ValueRange operands); 227 getNumOperands()228 unsigned getNumOperands() { 229 return LLVM_LIKELY(hasOperandStorage) ? getOperandStorage().size() : 0; 230 } 231 getOperand(unsigned idx)232 Value getOperand(unsigned idx) { return getOpOperand(idx).get(); } setOperand(unsigned idx,Value value)233 void setOperand(unsigned idx, Value value) { 234 return getOpOperand(idx).set(value); 235 } 236 237 /// Erase the operand at position `idx`. eraseOperand(unsigned idx)238 void eraseOperand(unsigned idx) { eraseOperands(idx); } 239 240 /// Erase the operands starting at position `idx` and ending at position 241 /// 'idx'+'length'. 242 void eraseOperands(unsigned idx, unsigned length = 1) { 243 getOperandStorage().eraseOperands(idx, length); 244 } 245 246 // Support operand iteration. 247 using operand_range = OperandRange; 248 using operand_iterator = operand_range::iterator; 249 operand_begin()250 operand_iterator operand_begin() { return getOperands().begin(); } operand_end()251 operand_iterator operand_end() { return getOperands().end(); } 252 253 /// Returns an iterator on the underlying Value's. getOperands()254 operand_range getOperands() { return operand_range(this); } 255 getOpOperands()256 MutableArrayRef<OpOperand> getOpOperands() { 257 return LLVM_LIKELY(hasOperandStorage) ? getOperandStorage().getOperands() 258 : MutableArrayRef<OpOperand>(); 259 } 260 getOpOperand(unsigned idx)261 OpOperand &getOpOperand(unsigned idx) { return getOpOperands()[idx]; } 262 263 // Support operand type iteration. 264 using operand_type_iterator = operand_range::type_iterator; 265 using operand_type_range = operand_range::type_range; operand_type_begin()266 operand_type_iterator operand_type_begin() { return operand_begin(); } operand_type_end()267 operand_type_iterator operand_type_end() { return operand_end(); } getOperandTypes()268 operand_type_range getOperandTypes() { return getOperands().getTypes(); } 269 270 //===--------------------------------------------------------------------===// 271 // Results 272 //===--------------------------------------------------------------------===// 273 274 /// Return the number of results held by this operation. 275 unsigned getNumResults(); 276 277 /// Get the 'idx'th result of this operation. getResult(unsigned idx)278 OpResult getResult(unsigned idx) { return OpResult(this, idx); } 279 280 /// Support result iteration. 281 using result_range = ResultRange; 282 using result_iterator = result_range::iterator; 283 result_begin()284 result_iterator result_begin() { return getResults().begin(); } result_end()285 result_iterator result_end() { return getResults().end(); } getResults()286 result_range getResults() { return result_range(this); } 287 getOpResults()288 result_range getOpResults() { return getResults(); } getOpResult(unsigned idx)289 OpResult getOpResult(unsigned idx) { return getResult(idx); } 290 291 /// Support result type iteration. 292 using result_type_iterator = result_range::type_iterator; 293 using result_type_range = result_range::type_range; result_type_begin()294 result_type_iterator result_type_begin() { return getResultTypes().begin(); } result_type_end()295 result_type_iterator result_type_end() { return getResultTypes().end(); } 296 result_type_range getResultTypes(); 297 298 //===--------------------------------------------------------------------===// 299 // Attributes 300 //===--------------------------------------------------------------------===// 301 302 // Operations may optionally carry a list of attributes that associate 303 // constants to names. Attributes may be dynamically added and removed over 304 // the lifetime of an operation. 305 306 /// Return all of the attributes on this operation. getAttrs()307 ArrayRef<NamedAttribute> getAttrs() { return attrs.getValue(); } 308 309 /// Return all of the attributes on this operation as a DictionaryAttr. getAttrDictionary()310 DictionaryAttr getAttrDictionary() { return attrs; } 311 312 /// Set the attribute dictionary on this operation. setAttrs(DictionaryAttr newAttrs)313 void setAttrs(DictionaryAttr newAttrs) { 314 assert(newAttrs && "expected valid attribute dictionary"); 315 attrs = newAttrs; 316 } setAttrs(ArrayRef<NamedAttribute> newAttrs)317 void setAttrs(ArrayRef<NamedAttribute> newAttrs) { 318 setAttrs(DictionaryAttr::get(newAttrs, getContext())); 319 } 320 321 /// Return the specified attribute if present, null otherwise. getAttr(Identifier name)322 Attribute getAttr(Identifier name) { return attrs.get(name); } getAttr(StringRef name)323 Attribute getAttr(StringRef name) { return attrs.get(name); } 324 getAttrOfType(Identifier name)325 template <typename AttrClass> AttrClass getAttrOfType(Identifier name) { 326 return getAttr(name).dyn_cast_or_null<AttrClass>(); 327 } getAttrOfType(StringRef name)328 template <typename AttrClass> AttrClass getAttrOfType(StringRef name) { 329 return getAttr(name).dyn_cast_or_null<AttrClass>(); 330 } 331 332 /// Return true if the operation has an attribute with the provided name, 333 /// false otherwise. hasAttr(Identifier name)334 bool hasAttr(Identifier name) { return static_cast<bool>(getAttr(name)); } hasAttr(StringRef name)335 bool hasAttr(StringRef name) { return static_cast<bool>(getAttr(name)); } 336 template <typename AttrClass, typename NameT> hasAttrOfType(NameT && name)337 bool hasAttrOfType(NameT &&name) { 338 return static_cast<bool>( 339 getAttrOfType<AttrClass>(std::forward<NameT>(name))); 340 } 341 342 /// If the an attribute exists with the specified name, change it to the new 343 /// value. Otherwise, add a new attribute with the specified name/value. setAttr(Identifier name,Attribute value)344 void setAttr(Identifier name, Attribute value) { 345 NamedAttrList attributes(attrs); 346 if (attributes.set(name, value) != value) 347 attrs = attributes.getDictionary(getContext()); 348 } setAttr(StringRef name,Attribute value)349 void setAttr(StringRef name, Attribute value) { 350 setAttr(Identifier::get(name, getContext()), value); 351 } 352 353 /// Remove the attribute with the specified name if it exists. Return the 354 /// attribute that was erased, or nullptr if there was no attribute with such 355 /// name. removeAttr(Identifier name)356 Attribute removeAttr(Identifier name) { 357 NamedAttrList attributes(attrs); 358 Attribute removedAttr = attributes.erase(name); 359 if (removedAttr) 360 attrs = attributes.getDictionary(getContext()); 361 return removedAttr; 362 } removeAttr(StringRef name)363 Attribute removeAttr(StringRef name) { 364 return removeAttr(Identifier::get(name, getContext())); 365 } 366 367 /// A utility iterator that filters out non-dialect attributes. 368 class dialect_attr_iterator 369 : public llvm::filter_iterator<ArrayRef<NamedAttribute>::iterator, 370 bool (*)(NamedAttribute)> { filter(NamedAttribute attr)371 static bool filter(NamedAttribute attr) { 372 // Dialect attributes are prefixed by the dialect name, like operations. 373 return attr.first.strref().count('.'); 374 } 375 dialect_attr_iterator(ArrayRef<NamedAttribute>::iterator it,ArrayRef<NamedAttribute>::iterator end)376 explicit dialect_attr_iterator(ArrayRef<NamedAttribute>::iterator it, 377 ArrayRef<NamedAttribute>::iterator end) 378 : llvm::filter_iterator<ArrayRef<NamedAttribute>::iterator, 379 bool (*)(NamedAttribute)>(it, end, &filter) {} 380 381 // Allow access to the constructor. 382 friend Operation; 383 }; 384 using dialect_attr_range = iterator_range<dialect_attr_iterator>; 385 386 /// Return a range corresponding to the dialect attributes for this operation. getDialectAttrs()387 dialect_attr_range getDialectAttrs() { 388 auto attrs = getAttrs(); 389 return {dialect_attr_iterator(attrs.begin(), attrs.end()), 390 dialect_attr_iterator(attrs.end(), attrs.end())}; 391 } dialect_attr_begin()392 dialect_attr_iterator dialect_attr_begin() { 393 auto attrs = getAttrs(); 394 return dialect_attr_iterator(attrs.begin(), attrs.end()); 395 } dialect_attr_end()396 dialect_attr_iterator dialect_attr_end() { 397 auto attrs = getAttrs(); 398 return dialect_attr_iterator(attrs.end(), attrs.end()); 399 } 400 401 /// Set the dialect attributes for this operation, and preserve all dependent. 402 template <typename DialectAttrT> setDialectAttrs(DialectAttrT && dialectAttrs)403 void setDialectAttrs(DialectAttrT &&dialectAttrs) { 404 NamedAttrList attrs; 405 attrs.append(std::begin(dialectAttrs), std::end(dialectAttrs)); 406 for (auto attr : getAttrs()) 407 if (!attr.first.strref().contains('.')) 408 attrs.push_back(attr); 409 setAttrs(attrs.getDictionary(getContext())); 410 } 411 412 //===--------------------------------------------------------------------===// 413 // Blocks 414 //===--------------------------------------------------------------------===// 415 416 /// Returns the number of regions held by this operation. getNumRegions()417 unsigned getNumRegions() { return numRegions; } 418 419 /// Returns the regions held by this operation. getRegions()420 MutableArrayRef<Region> getRegions() { 421 auto *regions = getTrailingObjects<Region>(); 422 return {regions, numRegions}; 423 } 424 425 /// Returns the region held by this operation at position 'index'. getRegion(unsigned index)426 Region &getRegion(unsigned index) { 427 assert(index < numRegions && "invalid region index"); 428 return getRegions()[index]; 429 } 430 431 //===--------------------------------------------------------------------===// 432 // Successors 433 //===--------------------------------------------------------------------===// 434 getBlockOperands()435 MutableArrayRef<BlockOperand> getBlockOperands() { 436 return {getTrailingObjects<BlockOperand>(), numSuccs}; 437 } 438 439 // Successor iteration. 440 using succ_iterator = SuccessorRange::iterator; successor_begin()441 succ_iterator successor_begin() { return getSuccessors().begin(); } successor_end()442 succ_iterator successor_end() { return getSuccessors().end(); } getSuccessors()443 SuccessorRange getSuccessors() { return SuccessorRange(this); } 444 hasSuccessors()445 bool hasSuccessors() { return numSuccs != 0; } getNumSuccessors()446 unsigned getNumSuccessors() { return numSuccs; } 447 getSuccessor(unsigned index)448 Block *getSuccessor(unsigned index) { 449 assert(index < getNumSuccessors()); 450 return getBlockOperands()[index].get(); 451 } 452 void setSuccessor(Block *block, unsigned index); 453 454 //===--------------------------------------------------------------------===// 455 // Accessors for various properties of operations 456 //===--------------------------------------------------------------------===// 457 458 /// Returns whether the operation is commutative. isCommutative()459 bool isCommutative() { 460 if (auto *absOp = getAbstractOperation()) 461 return absOp->hasProperty(OperationProperty::Commutative); 462 return false; 463 } 464 465 /// Represents the status of whether an operation is a terminator. We 466 /// represent an 'unknown' status because we want to support unregistered 467 /// terminators. 468 enum class TerminatorStatus { Terminator, NonTerminator, Unknown }; 469 470 /// Returns the status of whether this operation is a terminator or not. getTerminatorStatus()471 TerminatorStatus getTerminatorStatus() { 472 if (auto *absOp = getAbstractOperation()) { 473 return absOp->hasProperty(OperationProperty::Terminator) 474 ? TerminatorStatus::Terminator 475 : TerminatorStatus::NonTerminator; 476 } 477 return TerminatorStatus::Unknown; 478 } 479 480 /// Returns true if the operation is known to be a terminator. isKnownTerminator()481 bool isKnownTerminator() { 482 return getTerminatorStatus() == TerminatorStatus::Terminator; 483 } 484 485 /// Returns true if the operation is known to *not* be a terminator. isKnownNonTerminator()486 bool isKnownNonTerminator() { 487 return getTerminatorStatus() == TerminatorStatus::NonTerminator; 488 } 489 490 /// Returns true if the operation is known to be completely isolated from 491 /// enclosing regions, i.e., no internal regions reference values defined 492 /// above this operation. isKnownIsolatedFromAbove()493 bool isKnownIsolatedFromAbove() { 494 if (auto *absOp = getAbstractOperation()) 495 return absOp->hasProperty(OperationProperty::IsolatedFromAbove); 496 return false; 497 } 498 499 /// Attempt to fold this operation with the specified constant operand values 500 /// - the elements in "operands" will correspond directly to the operands of 501 /// the operation, but may be null if non-constant. If folding is successful, 502 /// this fills in the `results` vector. If not, `results` is unspecified. 503 LogicalResult fold(ArrayRef<Attribute> operands, 504 SmallVectorImpl<OpFoldResult> &results); 505 506 /// Returns true if the operation was registered with a particular trait, e.g. 507 /// hasTrait<OperandsAreSignlessIntegerLike>(). hasTrait()508 template <template <typename T> class Trait> bool hasTrait() { 509 auto *absOp = getAbstractOperation(); 510 return absOp ? absOp->hasTrait<Trait>() : false; 511 } 512 513 //===--------------------------------------------------------------------===// 514 // Operation Walkers 515 //===--------------------------------------------------------------------===// 516 517 /// Walk the operation in postorder, calling the callback for each nested 518 /// operation(including this one). The callback method can take any of the 519 /// following forms: 520 /// void(Operation*) : Walk all operations opaquely. 521 /// * op->walk([](Operation *nestedOp) { ...}); 522 /// void(OpT) : Walk all operations of the given derived type. 523 /// * op->walk([](ReturnOp returnOp) { ...}); 524 /// WalkResult(Operation*|OpT) : Walk operations, but allow for 525 /// interruption/cancellation. 526 /// * op->walk([](... op) { 527 /// // Interrupt, i.e cancel, the walk based on some invariant. 528 /// if (some_invariant) 529 /// return WalkResult::interrupt(); 530 /// return WalkResult::advance(); 531 /// }); 532 template <typename FnT, typename RetT = detail::walkResultType<FnT>> walk(FnT && callback)533 RetT walk(FnT &&callback) { 534 return detail::walk(this, std::forward<FnT>(callback)); 535 } 536 537 //===--------------------------------------------------------------------===// 538 // Uses 539 //===--------------------------------------------------------------------===// 540 541 /// Drop all uses of results of this operation. dropAllUses()542 void dropAllUses() { 543 for (OpResult result : getOpResults()) 544 result.dropAllUses(); 545 } 546 547 /// This class implements a use iterator for the Operation. This iterates over 548 /// all uses of all results. 549 class UseIterator final 550 : public llvm::iterator_facade_base< 551 UseIterator, std::forward_iterator_tag, OpOperand> { 552 public: 553 /// Initialize UseIterator for op, specify end to return iterator to last 554 /// use. 555 explicit UseIterator(Operation *op, bool end = false); 556 557 using llvm::iterator_facade_base<UseIterator, std::forward_iterator_tag, 558 OpOperand>::operator++; 559 UseIterator &operator++(); 560 OpOperand *operator->() const { return use.getOperand(); } 561 OpOperand &operator*() const { return *use.getOperand(); } 562 563 bool operator==(const UseIterator &rhs) const { return use == rhs.use; } 564 bool operator!=(const UseIterator &rhs) const { return !(*this == rhs); } 565 566 private: 567 void skipOverResultsWithNoUsers(); 568 569 /// The operation whose uses are being iterated over. 570 Operation *op; 571 /// The result of op who's uses are being iterated over. 572 Operation::result_iterator res; 573 /// The use of the result. 574 Value::use_iterator use; 575 }; 576 using use_iterator = UseIterator; 577 using use_range = iterator_range<use_iterator>; 578 use_begin()579 use_iterator use_begin() { return use_iterator(this); } use_end()580 use_iterator use_end() { return use_iterator(this, /*end=*/true); } 581 582 /// Returns a range of all uses, which is useful for iterating over all uses. getUses()583 use_range getUses() { return {use_begin(), use_end()}; } 584 585 /// Returns true if this operation has exactly one use. hasOneUse()586 bool hasOneUse() { return llvm::hasSingleElement(getUses()); } 587 588 /// Returns true if this operation has no uses. use_empty()589 bool use_empty() { 590 return llvm::all_of(getOpResults(), 591 [](OpResult result) { return result.use_empty(); }); 592 } 593 594 /// Returns true if the results of this operation are used outside of the 595 /// given block. isUsedOutsideOfBlock(Block * block)596 bool isUsedOutsideOfBlock(Block *block) { 597 return llvm::any_of(getOpResults(), [block](OpResult result) { 598 return result.isUsedOutsideOfBlock(block); 599 }); 600 } 601 602 //===--------------------------------------------------------------------===// 603 // Users 604 //===--------------------------------------------------------------------===// 605 606 using user_iterator = ValueUserIterator<use_iterator, OpOperand>; 607 using user_range = iterator_range<user_iterator>; 608 user_begin()609 user_iterator user_begin() { return user_iterator(use_begin()); } user_end()610 user_iterator user_end() { return user_iterator(use_end()); } 611 612 /// Returns a range of all users. getUsers()613 user_range getUsers() { return {user_begin(), user_end()}; } 614 615 //===--------------------------------------------------------------------===// 616 // Other 617 //===--------------------------------------------------------------------===// 618 619 /// Emit an error with the op name prefixed, like "'dim' op " which is 620 /// convenient for verifiers. 621 InFlightDiagnostic emitOpError(const Twine &message = {}); 622 623 /// Emit an error about fatal conditions with this operation, reporting up to 624 /// any diagnostic handlers that may be listening. 625 InFlightDiagnostic emitError(const Twine &message = {}); 626 627 /// Emit a warning about this operation, reporting up to any diagnostic 628 /// handlers that may be listening. 629 InFlightDiagnostic emitWarning(const Twine &message = {}); 630 631 /// Emit a remark about this operation, reporting up to any diagnostic 632 /// handlers that may be listening. 633 InFlightDiagnostic emitRemark(const Twine &message = {}); 634 635 private: 636 //===--------------------------------------------------------------------===// 637 // Ordering 638 //===--------------------------------------------------------------------===// 639 640 /// This value represents an invalid index ordering for an operation within a 641 /// block. 642 static constexpr unsigned kInvalidOrderIdx = -1; 643 644 /// This value represents the stride to use when computing a new order for an 645 /// operation. 646 static constexpr unsigned kOrderStride = 5; 647 648 /// Update the order index of this operation of this operation if necessary, 649 /// potentially recomputing the order of the parent block. 650 void updateOrderIfNecessary(); 651 652 /// Returns true if this operation has a valid order. hasValidOrder()653 bool hasValidOrder() { return orderIndex != kInvalidOrderIdx; } 654 655 private: 656 Operation(Location location, OperationName name, TypeRange resultTypes, 657 unsigned numSuccessors, unsigned numRegions, 658 DictionaryAttr attributes, bool hasOperandStorage); 659 660 // Operations are deleted through the destroy() member because they are 661 // allocated with malloc. 662 ~Operation(); 663 664 /// Returns the additional size necessary for allocating the given objects 665 /// before an Operation in-memory. prefixAllocSize(unsigned numTrailingResults,unsigned numInlineResults)666 static size_t prefixAllocSize(unsigned numTrailingResults, 667 unsigned numInlineResults) { 668 return sizeof(detail::TrailingOpResult) * numTrailingResults + 669 sizeof(detail::InLineOpResult) * numInlineResults; 670 } 671 /// Returns the additional size allocated before this Operation in-memory. prefixAllocSize()672 size_t prefixAllocSize() { 673 unsigned numResults = getNumResults(); 674 unsigned numTrailingResults = OpResult::getNumTrailing(numResults); 675 unsigned numInlineResults = OpResult::getNumInline(numResults); 676 return prefixAllocSize(numTrailingResults, numInlineResults); 677 } 678 679 /// Returns the operand storage object. getOperandStorage()680 detail::OperandStorage &getOperandStorage() { 681 assert(hasOperandStorage && "expected operation to have operand storage"); 682 return *getTrailingObjects<detail::OperandStorage>(); 683 } 684 685 /// Returns a pointer to the use list for the given trailing result. getTrailingResult(unsigned resultNumber)686 detail::TrailingOpResult *getTrailingResult(unsigned resultNumber) { 687 // Trailing results are stored in reverse order after(before in memory) the 688 // inline results. 689 return reinterpret_cast<detail::TrailingOpResult *>( 690 getInlineResult(OpResult::getMaxInlineResults() - 1)) - 691 ++resultNumber; 692 } 693 694 /// Returns a pointer to the use list for the given inline result. getInlineResult(unsigned resultNumber)695 detail::InLineOpResult *getInlineResult(unsigned resultNumber) { 696 // Inline results are stored in reverse order before the operation in 697 // memory. 698 return reinterpret_cast<detail::InLineOpResult *>(this) - ++resultNumber; 699 } 700 701 /// Provide a 'getParent' method for ilist_node_with_parent methods. 702 /// We mark it as a const function because ilist_node_with_parent specifically 703 /// requires a 'getParent() const' method. Once ilist_node removes this 704 /// constraint, we should drop the const to fit the rest of the MLIR const 705 /// model. getParent()706 Block *getParent() const { return block; } 707 708 /// The operation block that contains this operation. 709 Block *block = nullptr; 710 711 /// This holds information about the source location the operation was defined 712 /// or derived from. 713 Location location; 714 715 /// Relative order of this operation in its parent block. Used for 716 /// O(1) local dominance checks between operations. 717 mutable unsigned orderIndex = 0; 718 719 const unsigned numSuccs; 720 const unsigned numRegions : 30; 721 722 /// This bit signals whether this operation has an operand storage or not. The 723 /// operand storage may be elided for operations that are known to never have 724 /// operands. 725 bool hasOperandStorage : 1; 726 727 /// This holds the result types of the operation. There are three different 728 /// states recorded here: 729 /// - 0 results : The type below is null. 730 /// - 1 result : The single result type is held here. 731 /// - N results : The type here is a tuple holding the result types. 732 /// Note: We steal a bit for 'hasSingleResult' from somewhere else so that we 733 /// can use 'resultType` in an ArrayRef<Type>. 734 bool hasSingleResult : 1; 735 Type resultType; 736 737 /// This holds the name of the operation. 738 OperationName name; 739 740 /// This holds general named attributes for the operation. 741 DictionaryAttr attrs; 742 743 // allow ilist_traits access to 'block' field. 744 friend struct llvm::ilist_traits<Operation>; 745 746 // allow block to access the 'orderIndex' field. 747 friend class Block; 748 749 // allow value to access the 'ResultStorage' methods. 750 friend class Value; 751 752 // allow ilist_node_with_parent to access the 'getParent' method. 753 friend class llvm::ilist_node_with_parent<Operation, Block>; 754 755 // This stuff is used by the TrailingObjects template. 756 friend llvm::TrailingObjects<Operation, BlockOperand, Region, 757 detail::OperandStorage>; 758 size_t numTrailingObjects(OverloadToken<BlockOperand>) const { 759 return numSuccs; 760 } 761 size_t numTrailingObjects(OverloadToken<Region>) const { return numRegions; } 762 }; 763 764 inline raw_ostream &operator<<(raw_ostream &os, Operation &op) { 765 op.print(os, OpPrintingFlags().useLocalScope()); 766 return os; 767 } 768 769 } // end namespace mlir 770 771 namespace llvm { 772 /// Provide isa functionality for operation casts. 773 template <typename T> struct isa_impl<T, ::mlir::Operation> { 774 static inline bool doit(const ::mlir::Operation &op) { 775 return T::classof(const_cast<::mlir::Operation *>(&op)); 776 } 777 }; 778 779 /// Provide specializations for operation casts as the resulting T is value 780 /// typed. 781 template <typename T> struct cast_retty_impl<T, ::mlir::Operation *> { 782 using ret_type = T; 783 }; 784 template <typename T> struct cast_retty_impl<T, ::mlir::Operation> { 785 using ret_type = T; 786 }; 787 template <class T> 788 struct cast_convert_val<T, ::mlir::Operation, ::mlir::Operation> { 789 static T doit(::mlir::Operation &val) { return T(&val); } 790 }; 791 template <class T> 792 struct cast_convert_val<T, ::mlir::Operation *, ::mlir::Operation *> { 793 static T doit(::mlir::Operation *val) { return T(val); } 794 }; 795 } // end namespace llvm 796 797 #endif // MLIR_IR_OPERATION_H 798