16#include "llvm/ADT/SmallVectorExtras.h"
17#include "llvm/Support/DebugLog.h"
28 return FusedLoc::get(locs, metadata,
context);
38 return Float8E5M3FNUType::get(
context);
76 return IntegerType::get(
context, width);
80 return IntegerType::get(
81 context, width, isSigned ? IntegerType::Signed : IntegerType::Unsigned);
85 return FunctionType::get(
context, inputs, results);
89 return GraphType::get(
context, inputs, results);
93 return TupleType::get(
context, elementTypes);
113 return DictionaryAttr::get(
context, value);
117 return IntegerAttr::get(
getIndexType(), APInt(64, value));
150 VectorType::get(
static_cast<float>(values.size()),
getF32Type()), values);
155 VectorType::get(
static_cast<double>(values.size()),
getF64Type()),
189 RankedTensorType::get(
static_cast<int64_t>(values.size()),
196 RankedTensorType::get(
static_cast<int64_t>(values.size()),
203 RankedTensorType::get(
static_cast<int64_t>(values.size()),
212 APInt(32, value,
true));
217 APInt(32, value,
true));
222 APInt(32, (uint64_t)value,
false));
227 APInt(16, value,
true));
234 APInt(8, value,
true));
239 return IntegerAttr::get(type, APInt(64, value));
248 return IntegerAttr::get(type, value);
252 return FloatAttr::get(
getF64Type(), APFloat(value));
256 return FloatAttr::get(
getF32Type(), APFloat(value));
264 return FloatAttr::get(type, value);
268 return FloatAttr::get(type, value);
272 return StringAttr::get(
context, bytes);
276 return ArrayAttr::get(
context, value);
280 auto attrs = llvm::map_to_vector<8>(
286 auto attrs = llvm::map_to_vector<8>(
291 auto attrs = llvm::map_to_vector<8>(
297 auto attrs = llvm::map_to_vector<8>(values, [
this](
int64_t v) ->
Attribute {
304 auto attrs = llvm::map_to_vector<8>(
310 auto attrs = llvm::map_to_vector<8>(
316 auto attrs = llvm::map_to_vector<8>(
322 auto attrs = llvm::map_to_vector<8>(
323 values, [](
Type v) ->
Attribute {
return TypeAttr::get(v); });
328 auto attrs = llvm::map_to_vector<8>(
334 if (llvm::isa<FloatType>(type))
336 if (llvm::isa<IndexType>(type))
338 if (llvm::dyn_cast<IntegerType>(type))
340 APInt(llvm::cast<IntegerType>(type).getWidth(), 0));
341 if (llvm::isa<RankedTensorType, VectorType>(type)) {
342 auto vtType = llvm::cast<ShapedType>(type);
343 auto element =
getZeroAttr(vtType.getElementType());
352 if (llvm::isa<FloatType>(type))
354 if (llvm::isa<IndexType>(type))
356 if (llvm::dyn_cast<IntegerType>(type))
358 APInt(llvm::cast<IntegerType>(type).getWidth(), 1));
359 if (llvm::isa<RankedTensorType, VectorType>(type)) {
360 auto vtType = llvm::cast<ShapedType>(type);
361 auto element =
getOneAttr(vtType.getElementType());
398 dimExprs.reserve(rank);
399 for (
unsigned i = 0; i < rank; ++i)
420 shiftedResults.push_back(resultExpr + shift);
432 block->getOperations().insert(insertPoint, op);
434 listener->notifyOperationInserted(op, {});
441 assert(parent &&
"expected valid parent region");
442 assert(argTypes.size() == locs.size() &&
"argument location mismatch");
444 insertPt = parent->
end();
447 b->addArguments(argTypes, locs);
452 listener->notifyBlockInserted(
b,
nullptr, {});
460 assert(insertBefore &&
"expected valid insertion block");
476 OperationState state(loc, opName, operands, types, attributes, successors,
484 assert(results.empty() &&
"expected empty results");
487 results.reserve(opResults.size());
488 auto cleanupFailure = [&] {
495 return cleanupFailure();
499 LDBG() <<
"Trying to fold: "
501 if (failed(op->
fold(foldResults)))
502 return cleanupFailure();
506 LDBG() <<
"Folded in place #" << count
509 }
while (foldResults.empty() && succeeded(op->
fold(foldResults)));
512 if (foldResults.empty())
521 for (
auto [foldResult, expectedType] :
522 llvm::zip_equal(foldResults, opResults.
getTypes())) {
525 if (
auto value = llvm::dyn_cast_if_present<Value>(foldResult)) {
526 results.push_back(value);
532 return cleanupFailure();
535 Attribute attr = cast<Attribute>(foldResult);
540 for (
Operation *cst : generatedConstants)
542 return cleanupFailure();
546 generatedConstants.push_back(constOp);
547 results.push_back(constOp->getResult(0));
551 for (
Operation *cst : generatedConstants)
555 if (materializedConstants)
556 *materializedConstants = std::move(generatedConstants);
566 for (
Block &
b : r.getBlocks())
583 listener->notifyOperationInserted(walkedOp, {});
595 return clone(op, mapper);
600 region.
cloneInto(&parent, before, mapping);
607 for (
auto it = mapping.
lookup(®ion.
front())->getIterator(); it != before;
609 listener->notifyBlockInserted(&*it,
nullptr,
612 listener->notifyOperationInserted(walkedOp, {});
static void notifyBlockInsertions(Operation *op, OpBuilder::Listener *listener)
Helper function that sends block insertion notifications for every block that is directly nested in t...
Base type for affine expression.
A multi-dimensional affine map Affine map's are immutable like Type's, and they are uniqued.
static AffineMap get(MLIRContext *context)
Returns a zero result affine map with no dimensions or symbols: () -> ().
unsigned getNumSymbols() const
unsigned getNumDims() const
ArrayRef< AffineExpr > getResults() const
unsigned getNumResults() const
Attributes are known-constant values of operations.
This class provides an abstraction over the different types of ranges over Blocks.
Block represents an ordered list of Operations.
Region * getParent() const
Provide a 'getParent' method for ilist_node_with_parent methods.
Special case of IntegerAttr to represent boolean integers, i.e., signless i1 integers.
static BoolAttr get(MLIRContext *context, bool value)
IntegerAttr getIndexAttr(int64_t value)
AffineMap getSingleDimShiftAffineMap(int64_t shift)
Returns a map that shifts its (single) input dimension by 'shift'.
ArrayAttr getIndexArrayAttr(ArrayRef< int64_t > values)
DenseF64ArrayAttr getDenseF64ArrayAttr(ArrayRef< double > values)
DenseI8ArrayAttr getDenseI8ArrayAttr(ArrayRef< int8_t > values)
IntegerAttr getI32IntegerAttr(int32_t value)
DenseI32ArrayAttr getDenseI32ArrayAttr(ArrayRef< int32_t > values)
DenseIntElementsAttr getBoolVectorAttr(ArrayRef< bool > values)
Vector-typed DenseIntElementsAttr getters. values must not be empty.
TupleType getTupleType(TypeRange elementTypes)
IntegerAttr getIntegerAttr(Type type, int64_t value)
FloatAttr getF64FloatAttr(double value)
AffineMap getShiftedAffineMap(AffineMap map, int64_t shift)
Returns an affine map that is a translation (shift) of all result expressions in 'map' by 'shift'.
ArrayAttr getI32ArrayAttr(ArrayRef< int32_t > values)
DenseI64ArrayAttr getDenseI64ArrayAttr(ArrayRef< int64_t > values)
FloatAttr getF16FloatAttr(float value)
FloatType getF8E5M2Type()
AffineMap getDimIdentityMap()
AffineMap getMultiDimIdentityMap(unsigned rank)
IntegerAttr getI16IntegerAttr(int16_t value)
DenseI16ArrayAttr getDenseI16ArrayAttr(ArrayRef< int16_t > values)
AffineExpr getAffineSymbolExpr(unsigned position)
DenseFPElementsAttr getF32VectorAttr(ArrayRef< float > values)
FloatAttr getFloatAttr(Type type, double value)
AffineExpr getAffineConstantExpr(int64_t constant)
DenseIntElementsAttr getI32TensorAttr(ArrayRef< int32_t > values)
Tensor-typed DenseIntElementsAttr getters.
FunctionType getFunctionType(TypeRange inputs, TypeRange results)
IntegerAttr getI64IntegerAttr(int64_t value)
IntegerType getIntegerType(unsigned width)
DenseIntElementsAttr getI64TensorAttr(ArrayRef< int64_t > values)
BoolAttr getBoolAttr(bool value)
StringAttr getStringAttr(const Twine &bytes)
AffineMap getEmptyAffineMap()
Returns a zero result affine map with no dimensions or symbols: () -> ().
IntegerAttr getSI32IntegerAttr(int32_t value)
Signed and unsigned integer attribute getters.
GraphType getGraphType(TypeRange inputs, TypeRange results)
TypedAttr getZeroAttr(Type type)
Location getFusedLoc(ArrayRef< Location > locs, Attribute metadata=Attribute())
AffineExpr getAffineDimExpr(unsigned position)
DenseIntElementsAttr getIndexTensorAttr(ArrayRef< int64_t > values)
AffineMap getConstantAffineMap(int64_t val)
Returns a single constant result affine map with 0 dimensions and 0 symbols.
ArrayAttr getTypeArrayAttr(TypeRange values)
FloatType getF8E8M0Type()
DenseIntElementsAttr getI32VectorAttr(ArrayRef< int32_t > values)
DenseF32ArrayAttr getDenseF32ArrayAttr(ArrayRef< float > values)
FloatType getF8E5M3FNUType()
DenseIntElementsAttr getI64VectorAttr(ArrayRef< int64_t > values)
AffineMap getSymbolIdentityMap()
ArrayAttr getF64ArrayAttr(ArrayRef< double > values)
DenseFPElementsAttr getF64VectorAttr(ArrayRef< double > values)
ArrayAttr getArrayAttr(ArrayRef< Attribute > value)
MLIRContext * getContext() const
DenseBoolArrayAttr getDenseBoolArrayAttr(ArrayRef< bool > values)
Tensor-typed DenseArrayAttr getters.
ArrayAttr getI64ArrayAttr(ArrayRef< int64_t > values)
FloatAttr getF32FloatAttr(float value)
DictionaryAttr getDictionaryAttr(ArrayRef< NamedAttribute > value)
FloatType getF8E4M3FNType()
NamedAttribute getNamedAttr(StringRef name, Attribute val)
IntegerAttr getUI32IntegerAttr(uint32_t value)
IntegerAttr getI8IntegerAttr(int8_t value)
ArrayAttr getF32ArrayAttr(ArrayRef< float > values)
ArrayAttr getBoolArrayAttr(ArrayRef< bool > values)
ArrayAttr getStrArrayAttr(ArrayRef< StringRef > values)
DenseIntElementsAttr getIndexVectorAttr(ArrayRef< int64_t > values)
ArrayAttr getAffineMapArrayAttr(ArrayRef< AffineMap > values)
TypedAttr getOneAttr(Type type)
static DenseElementsAttr get(ShapedType type, ArrayRef< Attribute > values)
Constructs a dense elements attribute from an array of element values.
An attribute that represents a reference to a dense float vector or tensor object.
static DenseFPElementsAttr get(const ShapedType &type, Arg &&arg)
Get an instance of a DenseFPElementsAttr with the given arguments.
An attribute that represents a reference to a dense integer vector or tensor object.
static DenseIntElementsAttr get(const ShapedType &type, Arg &&arg)
Get an instance of a DenseIntElementsAttr with the given arguments.
Dialects are groups of MLIR operations, types and attributes, as well as behavior associated with the...
virtual Operation * materializeConstant(OpBuilder &builder, Attribute value, Type type, Location loc)
Registered hook to materialize a single constant operation from a given attribute value with the desi...
This is a utility class for mapping one set of IR entities to another.
auto lookup(T from) const
Lookup a mapped value within the map.
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.
Block * createBlock(Region *parent, Region::iterator insertPt={}, TypeRange argTypes={}, ArrayRef< Location > locs={})
Add new block with 'argTypes' arguments and set the insertion point to the end of it.
OpBuilder(MLIRContext *ctx, Listener *listener=nullptr)
Create a builder with the given context.
Operation * clone(Operation &op, IRMapping &mapper)
Creates a deep copy of the specified operation, remapping any operands that use values outside of the...
void setInsertionPointToEnd(Block *block)
Sets the insertion point to the end of the specified block.
void cloneRegionBefore(Region ®ion, Region &parent, Region::iterator before, IRMapping &mapping)
Clone the blocks that belong to "region" before the given position in another region "parent".
LogicalResult tryFold(Operation *op, SmallVectorImpl< Value > &results, SmallVectorImpl< Operation * > *materializedConstants=nullptr)
Attempts to fold the given operation and places new results within results.
Listener * listener
The optional listener for events of this builder.
Operation * create(const OperationState &state)
Creates an operation given the fields represented as an OperationState.
Operation * insert(Operation *op)
Insert the given operation at the current insertion point and return it.
Set of flags used to control the behavior of the various IR print methods (e.g.
A wrapper class that allows for printing an operation with a set of flags, useful to act as a "stream...
Operation is the basic unit of execution within MLIR.
Dialect * getDialect()
Return the dialect this operation is associated with, or nullptr if the associated dialect is not loa...
LogicalResult fold(ArrayRef< Attribute > operands, SmallVectorImpl< OpFoldResult > &results)
Attempt to fold this operation with the specified constant operand values.
Location getLoc()
The source location the operation was defined or derived from.
MutableArrayRef< Region > getRegions()
Returns the regions held by this operation.
static Operation * create(Location location, OperationName name, TypeRange resultTypes, ValueRange operands, NamedAttrList &&attributes, PropertyRef properties, BlockRange successors, unsigned numRegions)
Create a new Operation with the specific fields.
result_range getResults()
Operation * clone(IRMapping &mapper, const CloneOptions &options=CloneOptions::all())
Create a deep copy of this operation, remapping any operands that use values outside of the operation...
This class contains a list of basic blocks and a link to the parent operation it is attached to.
void cloneInto(Region *dest, IRMapping &mapper)
Clone the internal blocks from this region into dest.
BlockListType & getBlocks()
BlockListType::iterator iterator
This class implements the result iterators for the Operation class.
type_range getTypes() const
This class provides an abstraction over the various different ranges of value types.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
bool isSignedInteger() const
Return true if this is a signed integer type (with the specified width).
unsigned getIntOrFloatBitWidth() const
Return the bit width of an integer or a float type, assert failure on other types.
This class provides an abstraction over the different types of ranges over Values.
static DenseArrayAttrImpl get(MLIRContext *context, ArrayRef< bool > content)
Include the generated interface declarations.
bool matchPattern(Value value, const Pattern &pattern)
Entry point for matching a pattern over a Value.
detail::DenseArrayAttrImpl< int64_t > DenseI64ArrayAttr
detail::DenseArrayAttrImpl< int8_t > DenseI8ArrayAttr
detail::DenseArrayAttrImpl< int32_t > DenseI32ArrayAttr
AffineExpr getAffineConstantExpr(int64_t constant, MLIRContext *context)
detail::DenseArrayAttrImpl< double > DenseF64ArrayAttr
detail::DenseArrayAttrImpl< bool > DenseBoolArrayAttr
detail::DenseArrayAttrImpl< float > DenseF32ArrayAttr
detail::constant_op_matcher m_Constant()
Matches a constant foldable operation.
AffineExpr getAffineDimExpr(unsigned position, MLIRContext *context)
These free functions allow clients of the API to not use classes in detail.
detail::DenseArrayAttrImpl< int16_t > DenseI16ArrayAttr
AffineExpr getAffineSymbolExpr(unsigned position, MLIRContext *context)
This class represents a listener that may be used to hook into various actions within an OpBuilder.
virtual void notifyBlockInserted(Block *block, Region *previous, Region::iterator previousIt)
Notify the listener that the specified block was inserted.
This represents an operation in an abstracted form, suitable for use with the builder APIs.