9 #ifndef MLIR_IR_BUILDERS_H 10 #define MLIR_IR_BUILDERS_H 13 #include "llvm/Support/Compiler.h" 18 class BlockAndValueMapping;
29 class RankedTensorType;
30 class UnrankedTensorType;
41 class DenseElementsAttr;
42 class DenseIntElementsAttr;
82 template <
typename Ty,
typename... Args>
84 return Ty::get(
context, std::forward<Args>(args)...);
89 template <
typename Attr,
typename... Args>
91 return Attr::get(
context, std::forward<Args>(args)...);
190 :
Builder(ctx), listener(listener) {}
195 if (!region->
empty())
205 setInsertionPoint(op);
211 setInsertionPoint(block, insertPoint);
231 assert(terminator !=
nullptr &&
"the block has no terminator");
272 : block(insertBlock), point(insertPt) {}
275 bool isSet()
const {
return (block !=
nullptr); }
281 Block *block =
nullptr;
289 : builder(&builder), ip(builder.saveInsertionPoint()) {}
293 builder->restoreInsertionPoint(ip);
303 : builder(other.builder), ip(other.ip) {
304 other.builder =
nullptr;
318 this->block =
nullptr;
324 return InsertPoint(getInsertionBlock(), getInsertionPoint());
332 clearInsertionPoint();
339 this->insertPoint = insertPoint;
364 setInsertionPointToStart(blockArg.getOwner());
370 setInsertionPoint(block, block->
begin());
375 setInsertionPoint(block, block->
end());
426 template <
typename OpT>
430 if (LLVM_UNLIKELY(!opName)) {
431 llvm::report_fatal_error(
432 "Building op `" + OpT::getOperationName() +
433 "` but it isn't registered in this MLIRContext: the dialect may not " 434 "be loaded or this operation isn't registered by the dialect. See " 435 "also https://mlir.llvm.org/getting_started/Faq/" 436 "#registered-loaded-dependent-whats-up-with-dialects-management");
443 template <
typename OpTy,
typename... Args>
446 getCheckRegisteredInfo<OpTy>(location.
getContext()));
447 OpTy::build(*
this, state, std::forward<Args>(args)...);
448 auto *op = create(state);
449 auto result = dyn_cast<OpTy>(op);
450 assert(result &&
"builder didn't return the right type");
457 template <
typename OpTy,
typename... Args>
463 getCheckRegisteredInfo<OpTy>(location.
getContext()));
464 OpTy::build(*
this, state, std::forward<Args>(args)...);
475 template <
typename OpTy,
typename... Args>
476 typename std::enable_if<OpTy::template hasTrait<OpTrait::OneResult>(),
480 createOrFold<OpTy>(results, location, std::forward<Args>(args)...);
481 return results.front();
485 template <
typename OpTy,
typename... Args>
486 typename std::enable_if<OpTy::template hasTrait<OpTrait::ZeroResults>(),
489 auto op = create<OpTy>(location, std::forward<Args>(args)...);
491 (
void)tryFold(op.getOperation(), unused);
520 template <
typename OpT>
522 return cast<OpT>(cloneWithoutRegions(*op.getOperation()));
527 Block *block =
nullptr;
TODO: Remove this file when SCCP and integer range analysis have been ported to the new framework...
OpTy create(Location location, Args &&...args)
Create an operation of specific op type at the current insertion point.
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.
AffineMap getEmptyAffineMap()
Returns a zero result affine map with no dimensions or symbols: () -> ().
InsertionGuard(OpBuilder &builder)
This class contains a list of basic blocks and a link to the parent operation it is attached to...
DenseIntElementsAttr getI64VectorAttr(ArrayRef< int64_t > values)
MLIRContext * getContext() const
void createOrFold(SmallVectorImpl< Value > &results, Location location, Args &&...args)
Create an operation of specific op type at the current insertion point, and immediately try to fold i...
AffineMap getMultiDimIdentityMap(unsigned rank)
IntegerAttr getUI32IntegerAttr(uint32_t value)
Block * getInsertionBlock() const
Return the block the current insertion point belongs to.
Operation is a basic unit of execution within MLIR.
OpBuilder(Region ®ion, Listener *listener=nullptr)
DenseIntElementsAttr getIndexTensorAttr(ArrayRef< int64_t > values)
AffineMap getSingleDimShiftAffineMap(int64_t shift)
Returns a map that shifts its (single) input dimension by 'shift'.
TupleType getTupleType(TypeRange elementTypes)
Block::iterator getPoint() const
Attribute getZeroAttr(Type type)
Block represents an ordered list of Operations.
void setInsertionPoint(Block *block, Block::iterator insertPoint)
Set the insertion point to the specified location.
Operation * cloneWithoutRegions(Operation &op)
OpBuilder(Operation *op, Listener *listener=nullptr)
Create a builder and set insertion point to the given operation, which will cause subsequent insertio...
Operation * cloneWithoutRegions(Operation &op, BlockAndValueMapping &mapper)
Creates a deep copy of this operation but keep the operation regions empty.
AffineExpr getAffineSymbolExpr(unsigned position)
BlockListType::iterator iterator
FloatAttr getF64FloatAttr(double value)
static OpBuilder atBlockEnd(Block *block, Listener *listener=nullptr)
Create a builder and set the insertion point to after the last operation in the block but still insid...
bool succeeded(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a success value...
virtual void notifyOperationInserted(Operation *op)
Notification handler for when an operation is inserted into the builder.
NamedAttribute getNamedAttr(StringRef name, Attribute val)
ArrayAttr getI64ArrayAttr(ArrayRef< int64_t > values)
An inlay hint that for a type annotation.
Block * getBlock()
Returns the operation block that contains this operation.
void destroy()
Destroys this operation and its subclass data.
Ty getType(Args &&...args)
Get or construct an instance of the type Ty with provided arguments.
This class represents a listener that may be used to hook into various actions within an OpBuilder...
virtual void notifyBlockCreated(Block *block)
Notification handler for when a block is created using the builder.
static constexpr const bool value
static OpBuilder atBlockBegin(Block *block, Listener *listener=nullptr)
Create a builder and set the insertion point to before the first operation in the block but still ins...
IntegerAttr getI16IntegerAttr(int16_t value)
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
FloatAttr getF16FloatAttr(float value)
ArrayAttr getIndexArrayAttr(ArrayRef< int64_t > values)
NamedAttribute represents a combination of a name and an Attribute value.
Block * getBlock() const
Returns the current block of the builder.
void setInsertionPointAfter(Operation *op)
Sets the insertion point to the node after the specified operation, which will cause subsequent inser...
ArrayAttr getAffineMapArrayAttr(ArrayRef< AffineMap > values)
FloatAttr getFloatAttr(Type type, double value)
IntegerAttr getI32IntegerAttr(int32_t value)
This class represents an efficient way to signal success or failure.
InsertionGuard(InsertionGuard &&other) noexcept
Implement the move constructor to clear the builder field of other.
OpListType::iterator iterator
OpBuilder(Region *region, Listener *listener=nullptr)
Create a builder and set the insertion point to the start of the region.
InsertPoint(Block *insertBlock, Block::iterator insertPt)
Creates a new insertion point at the given location.
std::enable_if< OpTy::template hasTrait< OpTrait::OneResult >), Value >::type createOrFold(Location location, Args &&...args)
Overload to create or fold a single result operation.
IntegerAttr getIntegerAttr(Type type, int64_t value)
IntegerAttr getI64IntegerAttr(int64_t value)
IntegerAttr getI8IntegerAttr(int8_t value)
Attributes are known-constant values of operations.
Special case of IntegerAttr to represent boolean integers, i.e., signless i1 integers.
DictionaryAttr getDictionaryAttr(ArrayRef< NamedAttribute > value)
OpT cloneWithoutRegions(OpT op)
IntegerAttr getSI32IntegerAttr(int32_t value)
Signed and unsigned integer attribute getters.
IntegerType getIntegerType(unsigned width)
Base type for affine expression.
OpBuilder(MLIRContext *ctx, Listener *listener=nullptr)
Create a builder with the given context.
Location getFusedLoc(ArrayRef< Location > locs, Attribute metadata=Attribute())
void setInsertionPointAfterValue(Value val)
Sets the insertion point to the node after the specified value.
DenseIntElementsAttr getBoolVectorAttr(ArrayRef< bool > values)
Vector-typed DenseIntElementsAttr getters. values must not be empty.
This class provides an abstraction over the various different ranges of value types.
ArrayAttr getI32ArrayAttr(ArrayRef< int32_t > values)
This represents an operation in an abstracted form, suitable for use with the builder APIs...
Listener * getListener() const
Returns the current listener of this builder, or nullptr if this builder doesn't have a listener...
A multi-dimensional affine map Affine map's are immutable like Type's, and they are uniqued...
DenseIntElementsAttr getI32TensorAttr(ArrayRef< int32_t > values)
Tensor-typed DenseIntElementsAttr getters.
InsertPoint saveInsertionPoint() const
Return a saved insertion point.
Attr getAttr(Args &&...args)
Get or construct an instance of the attribute Attr with provided arguments.
This class represents an argument of a Block.
Builder(MLIRContext *context)
void setInsertionPoint(Operation *op)
Sets the insertion point to the specified operation, which will cause subsequent insertions to go rig...
static Optional< RegisteredOperationName > lookup(StringRef name, MLIRContext *ctx)
Lookup the registered operation information for the given operation.
ArrayAttr getBoolArrayAttr(ArrayRef< bool > values)
AffineMap getSymbolIdentityMap()
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
void clearInsertionPoint()
Reset the insertion point to no location.
ArrayAttr getF32ArrayAttr(ArrayRef< float > values)
Operation * getTerminator()
Get the terminator operation of this block.
void setInsertionPointToStart(Block *block)
Sets the insertion point to the start of the specified block.
AffineExpr getAffineConstantExpr(int64_t constant)
RAII guard to reset the insertion point of the builder when destroyed.
AffineMap getShiftedAffineMap(AffineMap map, int64_t shift)
Returns an affine map that is a translation (shift) of all result expressions in 'map' by 'shift'...
MLIRContext * getContext() const
Return the context this location is uniqued in.
static OpBuilder atBlockTerminator(Block *block, Listener *listener=nullptr)
Create a builder and set the insertion point to before the block terminator.
This class is a general helper class for creating context-global objects like types, attributes, and affine expressions.
AffineMap getDimIdentityMap()
This class represents a saved insertion point.
void setListener(Listener *newListener)
Sets the listener of this builder to the one provided.
OpBuilder(Block *block, Block::iterator insertPoint, Listener *listener=nullptr)
DenseIntElementsAttr getI64TensorAttr(ArrayRef< int64_t > values)
ArrayAttr getTypeArrayAttr(TypeRange values)
BoolAttr getBoolAttr(bool value)
Operation * getDefiningOp() const
If this value is the result of an operation, return the operation that defines it.
MLIRContext is the top-level object for a collection of MLIR operations.
DenseIntElementsAttr getI32VectorAttr(ArrayRef< int32_t > values)
This class provides an abstraction over the different types of ranges over Blocks.
AffineExpr getAffineDimExpr(unsigned position)
void setInsertionPointToEnd(Block *block)
Sets the insertion point to the end of the specified block.
AffineMap getConstantAffineMap(int64_t val)
Returns a single constant result affine map with 0 dimensions and 0 symbols.
FunctionType getFunctionType(TypeRange inputs, TypeRange results)
This is a "type erased" representation of a registered operation.
static void setInsertionPointAfter(OpBuilder &b, Value value)
Block::iterator getInsertionPoint() const
Returns the current insertion point of the builder.
FloatAttr getF32FloatAttr(float value)
std::enable_if< OpTy::template hasTrait< OpTrait::ZeroResults >), OpTy >::type createOrFold(Location location, Args &&...args)
Overload to create or fold a zero result operation.
void restoreInsertionPoint(InsertPoint ip)
Restore the insert point to a previously saved point.
This class helps build Operations.
ArrayAttr getArrayAttr(ArrayRef< Attribute > value)
This class provides an abstraction over the different types of ranges over Values.
IntegerAttr getIndexAttr(int64_t value)
StringAttr getStringAttr(const Twine &bytes)
DenseIntElementsAttr getIndexVectorAttr(ArrayRef< int64_t > values)
Operation * cloneWithoutRegions(BlockAndValueMapping &mapper)
Create a partial copy of this operation without traversing into attached regions. ...
bool isSet() const
Returns true if this insert point is set.
An attribute that represents a reference to a dense integer vector or tensor object.
ArrayAttr getF64ArrayAttr(ArrayRef< double > values)
ArrayAttr getStrArrayAttr(ArrayRef< StringRef > values)