//===- Operation.h - MLIR Operation Class -----------------------*- C++ -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // This file defines the Operation class. // //===----------------------------------------------------------------------===// #ifndef MLIR_IR_OPERATION_H #define MLIR_IR_OPERATION_H #include "mlir/IR/Block.h" #include "mlir/IR/BuiltinAttributes.h" #include "mlir/IR/Diagnostics.h" #include "mlir/IR/OperationSupport.h" #include "mlir/IR/Region.h" #include "llvm/ADT/Twine.h" namespace mlir { /// Operation is a basic unit of execution within MLIR. Operations can /// be nested within `Region`s held by other operations effectively forming a /// tree. Child operations are organized into operation blocks represented by a /// 'Block' class. class alignas(8) Operation final : public llvm::ilist_node_with_parent, private llvm::TrailingObjects { public: /// Create a new Operation with the specific fields. static Operation *create(Location location, OperationName name, TypeRange resultTypes, ValueRange operands, ArrayRef attributes, BlockRange successors, unsigned numRegions); /// Overload of create that takes an existing DictionaryAttr to avoid /// unnecessarily uniquing a list of attributes. static Operation *create(Location location, OperationName name, TypeRange resultTypes, ValueRange operands, DictionaryAttr attributes, BlockRange successors, unsigned numRegions); /// Create a new Operation from the fields stored in `state`. static Operation *create(const OperationState &state); /// Create a new Operation with the specific fields. static Operation *create(Location location, OperationName name, TypeRange resultTypes, ValueRange operands, DictionaryAttr attributes, BlockRange successors = {}, RegionRange regions = {}); /// The name of an operation is the key identifier for it. OperationName getName() { return name; } /// If this operation has a registered operation description, return it. /// Otherwise return null. const AbstractOperation *getAbstractOperation() { return getName().getAbstractOperation(); } /// Returns true if this operation has a registered operation description, /// otherwise false. bool isRegistered() { return getAbstractOperation(); } /// Remove this operation from its parent block and delete it. void erase(); /// Remove the operation from its parent block, but don't delete it. void remove(); /// Create a deep copy of this operation, remapping any operands that use /// values outside of the operation using the map that is provided (leaving /// them alone if no entry is present). Replaces references to cloned /// sub-operations to the corresponding operation that is copied, and adds /// those mappings to the map. Operation *clone(BlockAndValueMapping &mapper); Operation *clone(); /// Create a partial copy of this operation without traversing into attached /// regions. The new operation will have the same number of regions as the /// original one, but they will be left empty. /// Operands are remapped using `mapper` (if present), and `mapper` is updated /// to contain the results. Operation *cloneWithoutRegions(BlockAndValueMapping &mapper); /// Create a partial copy of this operation without traversing into attached /// regions. The new operation will have the same number of regions as the /// original one, but they will be left empty. Operation *cloneWithoutRegions(); /// Returns the operation block that contains this operation. Block *getBlock() { return block; } /// Return the context this operation is associated with. MLIRContext *getContext() { return location->getContext(); } /// Return the dialect this operation is associated with, or nullptr if the /// associated dialect is not registered. Dialect *getDialect() { return getName().getDialect(); } /// The source location the operation was defined or derived from. Location getLoc() { return location; } /// Set the source location the operation was defined or derived from. void setLoc(Location loc) { location = loc; } /// Returns the region to which the instruction belongs. Returns nullptr if /// the instruction is unlinked. Region *getParentRegion() { return block ? block->getParent() : nullptr; } /// Returns the closest surrounding operation that contains this operation /// or nullptr if this is a top-level operation. Operation *getParentOp() { return block ? block->getParentOp() : nullptr; } /// Return the closest surrounding parent operation that is of type 'OpTy'. template OpTy getParentOfType() { auto *op = this; while ((op = op->getParentOp())) if (auto parentOp = dyn_cast(op)) return parentOp; return OpTy(); } /// Returns the closest surrounding parent operation with trait `Trait`. template