13 #ifndef MLIR_IR_OPERATION_H 14 #define MLIR_IR_OPERATION_H 21 #include "llvm/ADT/Twine.h" 29 :
public llvm::ilist_node_with_parent<Operation, Block>,
30 private llvm::TrailingObjects<Operation, detail::OperandStorage,
31 BlockOperand, Region, OpOperand> {
43 DictionaryAttr attributes,
BlockRange successors,
52 DictionaryAttr attributes,
115 bool cloneRegionsFlag : 1;
117 bool cloneOperandsFlag : 1;
175 template <
typename OpTy>
178 while ((op = op->getParentOp()))
179 if (
auto parentOp = dyn_cast<OpTy>(op))
185 template <
template <
typename T>
class Trait>
209 template <
typename ValuesT>
232 void moveBefore(
Block *block, llvm::iplist<Operation>::iterator iterator);
241 void moveAfter(
Block *block, llvm::iplist<Operation>::iterator iterator);
271 return LLVM_LIKELY(hasOperandStorage) ? getOperandStorage().size() : 0;
285 getOperandStorage().eraseOperands(idx, length);
291 getOperandStorage().eraseOperands(eraseIndices);
308 return LLVM_LIKELY(hasOperandStorage) ? getOperandStorage().getOperands()
313 return getOperandStorage().getOperands()[idx];
370 assert(newAttrs &&
"expected valid attribute dictionary");
381 template <
typename AttrClass>
385 template <
typename AttrClass>
392 bool hasAttr(StringAttr name) {
return attrs.contains(name); }
393 bool hasAttr(StringRef name) {
return attrs.contains(name); }
394 template <
typename AttrClass,
typename NameT>
396 return static_cast<bool>(
397 getAttrOfType<AttrClass>(std::forward<NameT>(name)));
404 if (attributes.
set(name, value) !=
value)
427 :
public llvm::filter_iterator<ArrayRef<NamedAttribute>::iterator,
428 bool (*)(NamedAttribute)> {
431 return attr.
getName().strref().count(
'.');
436 : llvm::filter_iterator<ArrayRef<NamedAttribute>::iterator,
460 template <
typename DialectAttrT>
463 attrs.
append(std::begin(dialectAttrs), std::end(dialectAttrs));
465 if (!attr.getName().strref().contains(
'.'))
479 auto *regions = getTrailingObjects<Region>();
480 return {regions, numRegions};
485 assert(index < numRegions &&
"invalid region index");
494 return {getTrailingObjects<BlockOperand>(), numSuccs};
525 template <
template <
typename T>
class Trait>
533 template <
template <
typename T>
class Trait>
570 typename std::enable_if<
571 llvm::function_traits<std::decay_t<FnT>>::num_args == 1, RetT>::type
573 return detail::walk<Order>(
this, std::forward<FnT>(callback));
597 template <
typename FnT,
typename RetT = detail::walkResultType<FnT>>
598 typename std::enable_if<
599 llvm::function_traits<std::decay_t<FnT>>::num_args == 2, RetT>::type
611 result.dropAllUses();
677 static constexpr
unsigned kInvalidOrderIdx = -1;
681 static constexpr
unsigned kOrderStride = 5;
685 void updateOrderIfNecessary();
688 bool hasValidOrder() {
return orderIndex != kInvalidOrderIdx; }
692 unsigned numSuccessors,
unsigned numRegions,
693 DictionaryAttr attributes,
bool hasOperandStorage);
701 static size_t prefixAllocSize(
unsigned numOutOfLineResults,
702 unsigned numInlineResults) {
707 size_t prefixAllocSize() {
709 unsigned numOutOfLineResults = OpResult::getNumTrailing(numResults);
710 unsigned numInlineResults = OpResult::getNumInline(numResults);
711 return prefixAllocSize(numOutOfLineResults, numInlineResults);
716 assert(hasOperandStorage &&
"expected operation to have operand storage");
717 return *getTrailingObjects<detail::OperandStorage>();
740 if (resultNumber < maxInlineResults)
741 return getInlineOpResult(resultNumber);
742 return getOutOfLineOpResult(resultNumber - maxInlineResults);
750 Block *getParent()
const {
return block; }
753 Block *block =
nullptr;
761 mutable unsigned orderIndex = 0;
763 const unsigned numResults;
764 const unsigned numSuccs;
765 const unsigned numRegions : 31;
770 bool hasOperandStorage : 1;
776 DictionaryAttr attrs;
793 size_t numTrailingObjects(OverloadToken<detail::OperandStorage>)
const {
794 return hasOperandStorage ? 1 : 0;
796 size_t numTrailingObjects(OverloadToken<BlockOperand>)
const {
799 size_t numTrailingObjects(OverloadToken<Region>)
const {
return numRegions; }
811 template <
typename T>
814 CastInfo<T, ::mlir::Operation *>> {
817 template <
typename T>
820 CastInfo<T, ::mlir::Operation *>> {};
823 template <
typename T>
827 CastInfo<T, ::mlir::Operation>> {
833 template <
typename T>
836 CastInfo<T, ::mlir::Operation>> {};
845 ::mlir::Operation *, ::mlir::Operation *,
846 CastInfo<::mlir::Operation *, ::mlir::Operation *>> {
853 const ::mlir::Operation *, const ::mlir::Operation *,
854 CastInfo<::mlir::Operation *, ::mlir::Operation *>> {};
857 #endif // MLIR_IR_OPERATION_H bool use_empty() const
Returns true if no results in this range have uses.
operand_range::iterator operand_iterator
void moveBefore(Operation *existingOp)
Unlink this operation from its current block and insert it right before existingOp which may be in th...
TODO: Remove this file when SCCP and integer range analysis have been ported to the new framework...
static Operation * create(Location location, OperationName name, TypeRange resultTypes, ValueRange operands, ArrayRef< NamedAttribute > attributes, BlockRange successors, unsigned numRegions)
Create a new Operation with the specific fields.
This class contains a list of basic blocks and a link to the parent operation it is attached to...
use_iterator use_begin() const
bool isRegistered() const
Return if this operation is registered.
U dyn_cast_or_null() const
The OpAsmOpInterface, see OpAsmInterface.td for more details.
NamedAttrList is array of NamedAttributes that tracks whether it is sorted and does some basic work t...
Operation is a basic unit of execution within MLIR.
MutableArrayRef< Region > getRegions()
Returns the regions held by this operation.
CloneOptions & cloneRegions(bool enable=true)
Configures whether cloning should traverse into any of the regions of the operation.
void setAttrs(ArrayRef< NamedAttribute > newAttrs)
bool isAncestor(Operation *other)
Return true if this operation is an ancestor of the other operation.
iterator_range< use_iterator > use_range
This is a value defined by a result of an operation.
operand_range getOperands()
Returns an iterator on the underlying Value's.
use_range getUses()
Returns a range of all uses, which is useful for iterating over all uses.
unsigned getNumRegions()
Returns the number of regions held by this operation.
This class represents a diagnostic that is inflight and set to be reported.
void setOperands(ValueRange operands)
Replace the current operands of this operation with the ones provided in 'operands'.
Block represents an ordered list of Operations.
CloneOptions()
Default constructs an option with all flags set to false.
bool hasOneUse()
Returns true if this operation has exactly one use.
ArrayRef< NamedAttribute > getAttrs()
Return all of the attributes on this operation.
Value getOperand(unsigned idx)
AttrClass getAttrOfType(StringAttr name)
bool isUsedOutsideOfBlock(Block *block)
Returns true if the results of this operation are used outside of the given block.
bool shouldCloneOperands() const
Returns whether operands should be cloned as well.
Attribute set(StringAttr name, Attribute value)
If the an attribute exists with the specified name, change it to the new value.
std::enable_if< llvm::function_traits< std::decay_t< FnT > >::num_args==2, RetT >::type walk(FnT &&callback)
Generic walker with a stage aware callback.
unsigned getNumOperands()
bool mightHaveTrait()
Returns true if the operation might have the provided trait.
A block operand represents an operand that holds a reference to a Block, e.g.
operand_type_range getOperandTypes()
ValueTypeRange< ResultRange > type_range
Operation * getParentWithTrait()
Returns the closest surrounding parent operation with trait Trait.
bool isBeforeInBlock(Operation *other)
Given an operation 'other' that is within the same parent block, return whether the current operation...
DictionaryAttr getDictionary(MLIRContext *context) const
Return a dictionary attribute for the underlying dictionary.
This class implements the result iterators for the Operation class.
bool hasAttr(StringRef name)
type_range getTypes() const
unsigned getNumSuccessors()
An iterator over the users of an IRObject.
bool isRegistered()
Returns true if this operation has a registered operation description, otherwise false.
Attribute erase(StringAttr name)
Erase the attribute with the given name from the list.
Block * getBlock()
Returns the operation block that contains this operation.
raw_ostream & operator<<(raw_ostream &os, const AliasResult &result)
void insertOperands(unsigned index, ValueRange operands)
Insert the given operands into the operand list at the given 'index'.
CloneOptions & cloneOperands(bool enable=true)
Configures whether operation' operands should be cloned.
void destroy()
Destroys this operation and its subclass data.
OpTy getParentOfType()
Return the closest surrounding parent operation that is of type 'OpTy'.
static bool isPossible(::mlir::Operation &val)
static constexpr const bool value
void erase()
Remove this operation from its parent block and delete it.
void setSuccessor(Block *block, unsigned index)
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
NamedAttribute represents a combination of a name and an Attribute value.
MLIRContext * getContext()
Return the context this operation is associated with.
decltype(walk(nullptr, std::declval< FnT >())) walkResultType
Utility to provide the return type of a templated walk method.
bool hasTrait() const
Returns true if the operation was registered with a particular trait, e.g.
MutableArrayRef< OpOperand > getOpOperands()
static CloneOptions all()
Returns an instance with all flags set to true.
std::enable_if_t<!std::is_convertible< ValuesT, Operation * >::value > replaceAllUsesWith(ValuesT &&values)
Replace all uses of results of this range with the provided 'values'.
AttrClass getAttrOfType(StringRef name)
bool shouldCloneRegions() const
Returns whether regions of the operation should be cloned as well.
static bool isPossible(::mlir::Operation *op)
std::enable_if< llvm::function_traits< std::decay_t< FnT > >::num_args==1, RetT >::type walk(FnT &&callback)
Walk the operation by calling the callback for each nested operation (including this one)...
void eraseOperands(unsigned idx, unsigned length=1)
Erase the operands starting at position idx and ending at position 'idx'+'length'.
This class implements iteration on the types of a given range of values.
This class represents an efficient way to signal success or failure.
bool hasAttr(StringAttr name)
Return true if the operation has an attribute with the provided name, false otherwise.
succ_iterator successor_end()
Block * getSuccessor(unsigned index)
MLIRContext * getContext() const
Return the context this attribute belongs to.
dialect_attr_iterator dialect_attr_begin()
void set(IRValueT newValue)
Set the current value being used by this operand.
SuccessorRange::iterator succ_iterator
Operation * clone(BlockAndValueMapping &mapper, CloneOptions options=CloneOptions::all())
Create a deep copy of this operation, remapping any operands that use values outside of the operation...
Attributes are known-constant values of operations.
dialect_attr_iterator dialect_attr_end()
bool hasTrait()
Returns true if the operation was registered with a particular trait, e.g.
Operation * getParentOp()
Returns the closest surrounding operation that contains this operation or nullptr if this is a top-le...
void replaceAllUsesWith(ValuesT &&values)
Replace all uses of results of this operation with the provided 'values'.
ValueUserIterator< use_iterator, OpOperand > user_iterator
StringAttr getName() const
Return the name of the attribute.
static T doCast(::mlir::Operation &val)
Attribute removeAttr(StringAttr name)
Remove the attribute with the specified name if it exists.
Dialects are groups of MLIR operations, types and attributes, as well as behavior associated with the...
OpResult getResult(unsigned idx)
Get the 'idx'th result of this operation.
succ_iterator successor_begin()
This class provides an abstraction over the various different ranges of value types.
Optional< RegisteredOperationName > getRegisteredInfo() const
If this operation is registered, returns the registered information, None otherwise.
Location getLoc()
The source location the operation was defined or derived from.
IRValueT get() const
Return the current value being used by this operand.
This represents an operation in an abstracted form, suitable for use with the builder APIs...
MutableArrayRef< BlockOperand > getBlockOperands()
result_type_iterator result_type_end()
void dropAllDefinedValueUses()
Drop uses of all values defined by this operation or its nested regions.
InFlightDiagnostic emitWarning(const Twine &message={})
Emit a warning about this operation, reporting up to any diagnostic handlers that may be listening...
bool hasAttrOfType(NameT &&name)
operand_type_iterator operand_type_begin()
InFlightDiagnostic emitRemark(const Twine &message={})
Emit a remark about this operation, reporting up to any diagnostic handlers that may be listening...
result_range getOpResults()
void dropAllUses()
Drop all uses of results of this operation.
void setOperand(unsigned idx, Value value)
This class implements the successor iterators for Block.
void print(raw_ostream &os, const OpPrintingFlags &flags=llvm::None)
DictionaryAttr getAttrDictionary()
Return all of the attributes on this operation as a DictionaryAttr.
bool use_empty()
Returns true if this operation has no uses.
ValueTypeIterator< iterator > type_iterator
Returns the types of the values within this range.
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
void eraseOperands(const BitVector &eraseIndices)
Erases the operands that have their corresponding bit set in eraseIndices and removes them from the o...
operand_type_iterator operand_type_end()
This class implements a use iterator for a range of operation results.
This class implements iteration on the types of a given range of values.
static llvm::ManagedStatic< PassManagerOptions > options
void eraseOperand(unsigned idx)
Erase the operand at position idx.
OpPrintingFlags & useLocalScope()
Use local scope when printing the operation.
void dropAllReferences()
This drops all operand uses from this operation, which is an essential step in breaking cyclic depend...
void moveAfter(Operation *existingOp)
Unlink this operation from its current block and insert it right after existingOp which may be in the...
operand_iterator operand_begin()
SuccessorRange getSuccessors()
void setAttr(StringAttr name, Attribute value)
If the an attribute exists with the specified name, change it to the new value.
Set of flags used to control the behavior of the various IR print methods (e.g.
result_iterator result_end()
use_range getUses() const
Returns a range of all uses of results within this range, which is useful for iterating over all uses...
Class encompassing various options related to cloning an operation.
void setAttr(StringRef name, Attribute value)
result_type_iterator result_type_begin()
Attribute removeAttr(StringRef name)
void setLoc(Location loc)
Set the source location the operation was defined or derived from.
bool isUsedOutsideOfBlock(Block *block)
Returns true if the value is used outside of the given block.
A utility iterator that filters out non-dialect attributes.
void walk(Operation *op, function_ref< void(Region *)> callback, WalkOrder order)
Walk all of the regions, blocks, or operations nested under (and including) the given operation...
This class provides the implementation for an operation result whose index cannot be represented "inl...
ValueTypeIterator< iterator > type_iterator
Returns the types of the values within this range.
Dialect * getDialect()
Return the dialect this operation is associated with, or nullptr if the associated dialect is not loa...
ValueTypeRange< OperandRange > type_range
OpOperand & getOpOperand(unsigned idx)
void append(StringRef name, Attribute attr)
Add an attribute with the specified name.
OpResult getOpResult(unsigned idx)
MLIRContext is the top-level object for a collection of MLIR operations.
This class represents an operand of an operation.
This class provides an abstraction over the different types of ranges over Blocks.
This class provides an abstraction over the different types of ranges over Regions.
void setDialectAttrs(DialectAttrT &&dialectAttrs)
Set the dialect attributes for this operation, and preserve all dependent.
bool mightHaveTrait() const
Returns true if the operation might have the provided trait.
This class implements the operand iterators for the Operation class.
This class provides the implementation for an operation result.
static bool isPossible(::mlir::Operation *op)
result_range::iterator result_iterator
Dialect * getDialect() const
Return the dialect this operation is registered to if the dialect is loaded in the context...
Region * getParentRegion()
Returns the region to which the instruction belongs.
Attribute getAttr(StringRef name)
use_iterator use_end() const
::mlir::Operation * doCast(::mlir::Operation *op)
LogicalResult fold(ArrayRef< Attribute > operands, SmallVectorImpl< OpFoldResult > &results)
Attempt to fold this operation with the specified constant operand values.
unsigned getNumResults()
Return the number of results held by this operation.
dialect_attr_range getDialectAttrs()
Return a range corresponding to the dialect attributes for this operation.
ResultRange result_range
Support result iteration.
user_iterator user_begin()
operand_iterator operand_end()
InFlightDiagnostic emitOpError(const Twine &message={})
Emit an error with the op name prefixed, like "'dim' op " which is convenient for verifiers...
Optional< RegisteredOperationName > getRegisteredInfo()
If this operation has a registered operation description, return it.
OperationName getName()
The name of an operation is the key identifier for it.
WalkOrder
Traversal order for region, block and operation walk utilities.
InFlightDiagnostic emitError(const Twine &message={})
Emit an error about fatal conditions with this operation, reporting up to any diagnostic handlers tha...
user_range getUsers()
Returns a range of all users.
void replaceUsesOfWith(Value from, Value to)
Replace any uses of 'from' with 'to' within this operation.
void setAttrs(DictionaryAttr newAttrs)
Set the attribute dictionary on this operation.
result_range getResults()
This class provides an abstraction over the different types of ranges over Values.
void push_back(NamedAttribute newAttribute)
Add an attribute with the specified name.
Attribute getAttr(StringAttr name)
Return the specified attribute if present, null otherwise.
result_type_range getResultTypes()
bool isProperAncestor(Operation *other)
Return true if this operation is a proper ancestor of the other operation.
This class provides the implementation for an operation result whose index can be represented "inline...
Region & getRegion(unsigned index)
Returns the region held by this operation at position 'index'.
result_iterator result_begin()
This class provides management for the lifetime of the state used when printing the IR...
Operation * cloneWithoutRegions()
Create a partial copy of this operation without traversing into attached regions. ...
type_range getTypes() const
static unsigned getMaxInlineResults()
Returns the maximum number of results that can be stored inline.
This class handles the management of operation operands.