| //===- Builders.h - Helpers for constructing MLIR Classes -------*- C++ -*-===// |
| // |
| // Copyright 2019 The MLIR Authors. |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); |
| // you may not use this file except in compliance with the License. |
| // You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| // See the License for the specific language governing permissions and |
| // limitations under the License. |
| // ============================================================================= |
| |
| #ifndef MLIR_IR_BUILDERS_H |
| #define MLIR_IR_BUILDERS_H |
| |
| #include "mlir/IR/Function.h" |
| #include "mlir/IR/Operation.h" |
| |
| namespace mlir { |
| |
| class AffineExpr; |
| class BlockAndValueMapping; |
| class Module; |
| class UnknownLoc; |
| class UniquedFilename; |
| class FileLineColLoc; |
| class Type; |
| class PrimitiveType; |
| class IntegerType; |
| class FunctionType; |
| class MemRefType; |
| class VectorType; |
| class RankedTensorType; |
| class UnrankedTensorType; |
| class TupleType; |
| class NoneType; |
| class BoolAttr; |
| class IntegerAttr; |
| class FloatAttr; |
| class StringAttr; |
| class TypeAttr; |
| class ArrayAttr; |
| class FunctionAttr; |
| class ElementsAttr; |
| class DenseElementsAttr; |
| class DenseIntElementsAttr; |
| class AffineMapAttr; |
| class AffineMap; |
| class UnitAttr; |
| |
| /// This class is a general helper class for creating context-global objects |
| /// like types, attributes, and affine expressions. |
| class Builder { |
| public: |
| explicit Builder(MLIRContext *context) : context(context) {} |
| explicit Builder(Module *module); |
| |
| MLIRContext *getContext() const { return context; } |
| |
| Identifier getIdentifier(StringRef str); |
| Module *createModule(); |
| |
| // Locations. |
| UnknownLoc getUnknownLoc(); |
| UniquedFilename getUniquedFilename(StringRef filename); |
| FileLineColLoc getFileLineColLoc(UniquedFilename filename, unsigned line, |
| unsigned column); |
| Location getFusedLoc(ArrayRef<Location> locs, |
| Attribute metadata = Attribute()); |
| |
| // Types. |
| FloatType getBF16Type(); |
| FloatType getF16Type(); |
| FloatType getF32Type(); |
| FloatType getF64Type(); |
| |
| IndexType getIndexType(); |
| |
| IntegerType getI1Type(); |
| IntegerType getIntegerType(unsigned width); |
| FunctionType getFunctionType(ArrayRef<Type> inputs, ArrayRef<Type> results); |
| MemRefType getMemRefType(ArrayRef<int64_t> shape, Type elementType, |
| ArrayRef<AffineMap> affineMapComposition = {}, |
| unsigned memorySpace = 0); |
| VectorType getVectorType(ArrayRef<int64_t> shape, Type elementType); |
| RankedTensorType getTensorType(ArrayRef<int64_t> shape, Type elementType); |
| UnrankedTensorType getTensorType(Type elementType); |
| TupleType getTupleType(ArrayRef<Type> elementTypes); |
| NoneType getNoneType(); |
| |
| /// Get or construct an instance of the type 'ty' with provided arguments. |
| template <typename Ty, typename... Args> Ty getType(Args... args) { |
| return Ty::get(context, args...); |
| } |
| |
| // Attributes. |
| NamedAttribute getNamedAttr(StringRef name, Attribute val); |
| |
| UnitAttr getUnitAttr(); |
| BoolAttr getBoolAttr(bool value); |
| IntegerAttr getIntegerAttr(Type type, int64_t value); |
| IntegerAttr getIntegerAttr(Type type, const APInt &value); |
| FloatAttr getFloatAttr(Type type, double value); |
| FloatAttr getFloatAttr(Type type, const APFloat &value); |
| StringAttr getStringAttr(StringRef bytes); |
| ArrayAttr getArrayAttr(ArrayRef<Attribute> value); |
| AffineMapAttr getAffineMapAttr(AffineMap map); |
| IntegerSetAttr getIntegerSetAttr(IntegerSet set); |
| TypeAttr getTypeAttr(Type type); |
| FunctionAttr getFunctionAttr(Function *value); |
| ElementsAttr getSplatElementsAttr(ShapedType type, Attribute elt); |
| ElementsAttr getDenseElementsAttr(ShapedType type, ArrayRef<char> data); |
| ElementsAttr getDenseElementsAttr(ShapedType type, |
| ArrayRef<Attribute> values); |
| ElementsAttr getDenseIntElementsAttr(ShapedType type, |
| ArrayRef<int64_t> values); |
| ElementsAttr getSparseElementsAttr(ShapedType type, |
| DenseIntElementsAttr indices, |
| DenseElementsAttr values); |
| ElementsAttr getOpaqueElementsAttr(Dialect *dialect, ShapedType type, |
| StringRef bytes); |
| // Returns a 0-valued attribute of the given `type`. This function only |
| // supports boolean, integer, and 32-/64-bit float types, and vector or ranked |
| // tensor of them. Returns null attribute otherwise. |
| Attribute getZeroAttr(Type type); |
| |
| // Convenience methods for fixed types. |
| FloatAttr getF32FloatAttr(float value); |
| FloatAttr getF64FloatAttr(double value); |
| IntegerAttr getI32IntegerAttr(int32_t value); |
| IntegerAttr getI64IntegerAttr(int64_t value); |
| |
| ArrayAttr getI32ArrayAttr(ArrayRef<int32_t> values); |
| ArrayAttr getI64ArrayAttr(ArrayRef<int64_t> values); |
| ArrayAttr getF32ArrayAttr(ArrayRef<float> values); |
| ArrayAttr getF64ArrayAttr(ArrayRef<double> values); |
| ArrayAttr getStrArrayAttr(ArrayRef<StringRef> values); |
| |
| // Affine expressions and affine maps. |
| AffineExpr getAffineDimExpr(unsigned position); |
| AffineExpr getAffineSymbolExpr(unsigned position); |
| AffineExpr getAffineConstantExpr(int64_t constant); |
| |
| AffineMap getAffineMap(unsigned dimCount, unsigned symbolCount, |
| ArrayRef<AffineExpr> results, |
| ArrayRef<AffineExpr> rangeSizes); |
| |
| // Special cases of affine maps and integer sets |
| /// Returns a single constant result affine map with 0 dimensions and 0 |
| /// symbols. One constant result: () -> (val). |
| AffineMap getConstantAffineMap(int64_t val); |
| // One dimension id identity map: (i) -> (i). |
| AffineMap getDimIdentityMap(); |
| // Multi-dimensional identity map: (d0, d1, d2) -> (d0, d1, d2). |
| AffineMap getMultiDimIdentityMap(unsigned rank); |
| // One symbol identity map: ()[s] -> (s). |
| AffineMap getSymbolIdentityMap(); |
| |
| /// Returns a map that shifts its (single) input dimension by 'shift'. |
| /// (d0) -> (d0 + shift) |
| AffineMap getSingleDimShiftAffineMap(int64_t shift); |
| |
| /// Returns an affine map that is a translation (shift) of all result |
| /// expressions in 'map' by 'shift'. |
| /// Eg: input: (d0, d1)[s0] -> (d0, d1 + s0), shift = 2 |
| /// returns: (d0, d1)[s0] -> (d0 + 2, d1 + s0 + 2) |
| AffineMap getShiftedAffineMap(AffineMap map, int64_t shift); |
| |
| // Integer set. |
| IntegerSet getIntegerSet(unsigned dimCount, unsigned symbolCount, |
| ArrayRef<AffineExpr> constraints, |
| ArrayRef<bool> isEq); |
| // TODO: Helpers for affine map/exprs, etc. |
| protected: |
| MLIRContext *context; |
| }; |
| |
| /// This class helps build a Function. Operations that are created are |
| /// automatically inserted at an insertion point. The builder is copyable. |
| class FuncBuilder : public Builder { |
| public: |
| /// Create a function builder and set the insertion point to the start of |
| /// the function. |
| explicit FuncBuilder(Function *func) |
| : Builder(func->getContext()), function(func) { |
| if (!func->empty()) |
| setInsertionPoint(&func->front(), func->front().begin()); |
| else |
| clearInsertionPoint(); |
| } |
| |
| explicit FuncBuilder(Function &func) : FuncBuilder(&func) {} |
| virtual ~FuncBuilder(); |
| |
| /// Create a function builder and set insertion point to the given |
| /// operation, which will cause subsequent insertions to go right before it. |
| FuncBuilder(Operation *op) : FuncBuilder(op->getFunction()) { |
| setInsertionPoint(op); |
| } |
| |
| FuncBuilder(Block *block) : FuncBuilder(block->getFunction()) { |
| setInsertionPoint(block, block->end()); |
| } |
| |
| FuncBuilder(Block *block, Block::iterator insertPoint) |
| : FuncBuilder(block->getFunction()) { |
| setInsertionPoint(block, insertPoint); |
| } |
| |
| /// Return the function this builder is referring to. |
| Function *getFunction() const { return function; } |
| |
| /// This class represents a saved insertion point. |
| class InsertPoint { |
| public: |
| /// Creates a new insertion point which doesn't point to anything. |
| InsertPoint() = default; |
| |
| /// Creates a new insertion point at the given location. |
| InsertPoint(Block *insertBlock, Block::iterator insertPt) |
| : block(insertBlock), point(insertPt) {} |
| |
| /// Returns true if this insert point is set. |
| bool isSet() const { return (block != nullptr); } |
| |
| Block *getBlock() const { return block; } |
| Block::iterator getPoint() const { return point; } |
| |
| private: |
| Block *block = nullptr; |
| Block::iterator point; |
| }; |
| |
| /// Reset the insertion point to no location. Creating an operation without a |
| /// set insertion point is an error, but this can still be useful when the |
| /// current insertion point a builder refers to is being removed. |
| void clearInsertionPoint() { |
| this->block = nullptr; |
| insertPoint = Block::iterator(); |
| } |
| |
| /// Return a saved insertion point. |
| InsertPoint saveInsertionPoint() const { |
| return InsertPoint(getInsertionBlock(), getInsertionPoint()); |
| } |
| |
| /// Restore the insert point to a previously saved point. |
| void restoreInsertionPoint(InsertPoint ip) { |
| if (ip.isSet()) |
| setInsertionPoint(ip.getBlock(), ip.getPoint()); |
| else |
| clearInsertionPoint(); |
| } |
| |
| /// Set the insertion point to the specified location. |
| void setInsertionPoint(Block *block, Block::iterator insertPoint) { |
| // TODO: check that insertPoint is in this rather than some other block. |
| this->block = block; |
| this->insertPoint = insertPoint; |
| } |
| |
| /// Sets the insertion point to the specified operation, which will cause |
| /// subsequent insertions to go right before it. |
| void setInsertionPoint(Operation *op) { |
| setInsertionPoint(op->getBlock(), Block::iterator(op)); |
| } |
| |
| /// Sets the insertion point to the start of the specified block. |
| void setInsertionPointToStart(Block *block) { |
| setInsertionPoint(block, block->begin()); |
| } |
| |
| /// Sets the insertion point to the end of the specified block. |
| void setInsertionPointToEnd(Block *block) { |
| setInsertionPoint(block, block->end()); |
| } |
| |
| /// Return the block the current insertion point belongs to. Note that the |
| /// the insertion point is not necessarily the end of the block. |
| Block *getInsertionBlock() const { return block; } |
| |
| /// Returns the current insertion point of the builder. |
| Block::iterator getInsertionPoint() const { return insertPoint; } |
| |
| /// Add new block and set the insertion point to the end of it. If an |
| /// 'insertBefore' block is passed, the block will be placed before the |
| /// specified block. If not, the block will be appended to the end of the |
| /// current function. |
| Block *createBlock(Block *insertBefore = nullptr); |
| |
| /// Returns the current block of the builder. |
| Block *getBlock() const { return block; } |
| |
| /// Creates an operation given the fields represented as an OperationState. |
| virtual Operation *createOperation(const OperationState &state); |
| |
| /// Create operation of specific op type at the current insertion point. |
| template <typename OpTy, typename... Args> |
| OpTy create(Location location, Args... args) { |
| OperationState state(getContext(), location, OpTy::getOperationName()); |
| OpTy::build(this, &state, args...); |
| auto *op = createOperation(state); |
| auto result = dyn_cast<OpTy>(op); |
| assert(result && "Builder didn't return the right type"); |
| return result; |
| } |
| |
| /// Creates a deep copy of the specified 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(Operation &op, BlockAndValueMapping &mapper) { |
| Operation *cloneOp = op.clone(mapper, getContext()); |
| block->getOperations().insert(insertPoint, cloneOp); |
| return cloneOp; |
| } |
| Operation *clone(Operation &op) { |
| Operation *cloneOp = op.clone(getContext()); |
| block->getOperations().insert(insertPoint, cloneOp); |
| return cloneOp; |
| } |
| |
| /// Creates a deep copy of this operation but keep the operation regions |
| /// empty. Operands are remapped using `mapper` (if present), and `mapper` is |
| /// updated to contain the results. |
| Operation *cloneWithoutRegions(Operation &op, BlockAndValueMapping &mapper) { |
| Operation *cloneOp = op.cloneWithoutRegions(mapper, getContext()); |
| block->getOperations().insert(insertPoint, cloneOp); |
| return cloneOp; |
| } |
| Operation *cloneWithoutRegions(Operation &op) { |
| Operation *cloneOp = op.cloneWithoutRegions(getContext()); |
| block->getOperations().insert(insertPoint, cloneOp); |
| return cloneOp; |
| } |
| |
| private: |
| Function *function; |
| Block *block = nullptr; |
| Block::iterator insertPoint; |
| }; |
| |
| } // namespace mlir |
| |
| #endif |