9 #ifndef MLIR_DIALECT_BUFFERIZATION_IR_BUFFERIZABLEOPINTERFACE_H_
10 #define MLIR_DIALECT_BUFFERIZATION_IR_BUFFERIZABLEOPINTERFACE_H_
15 #include "llvm/ADT/SetVector.h"
18 #include "mlir/Dialect/Bufferization/IR/BufferizationEnums.h.inc"
23 namespace bufferization {
26 class BufferizableOpInterface;
54 template <
typename... DialectTs>
58 (allowDialectImpl<DialectTs>(), ...);
64 template <
typename... DialectTs>
66 (denyDialectImpl<DialectTs>(), ...);
74 return op->getDialect()->getNamespace() == dialectNamespace;
76 entries.push_back(
Entry{filterFn, Entry::FilterType::ALLOW});
82 template <
typename... OpTys>
84 (allowOperationImpl<OpTys>(), ...);
90 template <
typename... OpTys>
92 (denyOperationImpl<OpTys>(), ...);
100 return op->getName().getStringRef() == opName;
110 return op->getName().getStringRef() == opName;
119 entries.push_back(
Entry{fn, Entry::FilterType::ALLOW});
126 entries.push_back(
Entry{fn, Entry::FilterType::DENY});
131 bool hasAllowRule()
const {
132 for (
const Entry &e : entries)
133 if (e.type == Entry::FilterType::ALLOW)
139 template <
typename DialectT>
140 void allowDialectImpl() {
145 template <
typename DialectT>
146 void denyDialectImpl() {
151 template <
typename OpTy>
152 void allowOperationImpl() {
157 template <
typename OpTy>
158 void denyOperationImpl() {
166 SmallVector<Entry> entries;
218 Value allocatedBuffer)
const;
267 LayoutMapOption::InferLayoutMap;
375 bool followEquivalentOnly =
false)
const;
436 bool escape,
const BufferizationOptions &
options,
447 const BufferizationOptions &
options);
458 const BufferizationOptions &
options);
480 template <
typename OpTy,
typename... Args>
483 auto newOp = rewriter.
create<OpTy>(op->
getLoc(), std::forward<Args>(args)...);
492 const BufferizationOptions &
options);
507 MemRefLayoutAttrInterface layout = {},
508 Attribute memorySpace =
nullptr);
514 Attribute memorySpace =
nullptr);
520 Attribute memorySpace =
nullptr);
528 const BufferizationOptions &
options);
533 const BufferizationOptions &
options);
537 const BufferizationOptions &
options);
563 #include "mlir/Dialect/Bufferization/IR/BufferizableOpInterface.h.inc"
static void copy(Location loc, Value dst, Value src, Value size, OpBuilder &builder)
Copies the given number of bytes from src to dst pointers.
static llvm::ManagedStatic< PassManagerOptions > options
#define MLIR_DECLARE_EXPLICIT_TYPE_ID(CLASS_NAME)
Attributes are known-constant values of operations.
This class provides a shared interface for ranked and unranked memref types.
This class provides support for representing a failure result, or a valid value of type T.
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
This class helps build Operations.
Operation * create(const OperationState &state)
Creates an operation given the fields represented as an OperationState.
This class represents an operand of an operation.
This is a value defined by a result of an operation.
Operation is the basic unit of execution within MLIR.
Location getLoc()
The source location the operation was defined or derived from.
result_range getResults()
This class coordinates the application of a rewrite on a set of IR, providing a way for clients to tr...
This class provides an efficient unique identifier for a specific C++ type.
This class provides an abstraction over the different types of ranges over Values.
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
AnalysisState provides a variety of helper functions for dealing with tensor values.
bool isValueRead(Value value) const
Return true if the given value is read by an op that bufferizes to a memory read.
virtual ~AnalysisState()=default
virtual bool areAliasingBufferizedValues(Value v1, Value v2) const
Return true if v1 and v2 may bufferize to aliasing buffers.
virtual bool hasUndefinedContents(OpOperand *opOperand) const
Return true if the given tensor has undefined contents.
SetVector< Value > findValueInReverseUseDefChain(Value value, llvm::function_ref< bool(Value)> condition, bool followEquivalentOnly=false) const
Starting from value, follow the use-def chain in reverse, always selecting the aliasing OpOperands.
bool canOmitTensorCopy(OpOperand &opOperand) const
Return true if a copy can always be avoided when allocating a new tensor for the given OpOperand.
bool bufferizesToMemoryWrite(OpOperand &opOperand) const
Return true if opOperand bufferizes to a memory write.
virtual bool isInPlace(OpOperand &opOperand) const
Return true if the given OpResult has been decided to bufferize inplace.
bool bufferizesToAliasOnly(OpOperand &opOperand) const
Return true if opOperand does neither read nor write but bufferizes to an alias.
AnalysisState(const BufferizationOptions &options)
const BufferizationOptions & getOptions() const
Return a reference to the BufferizationOptions.
SetVector< Value > findLastPrecedingWrite(Value value) const
Find the Values of the last preceding write of a given Value.
virtual bool isTensorYielded(Value tensor) const
Return true if the given tensor (or an aliasing tensor) is yielded from the containing block.
bool bufferizesToMemoryRead(OpOperand &opOperand) const
Return true if opOperand bufferizes to a memory read.
static bool classof(const AnalysisState *base)
virtual bool areEquivalentBufferizedValues(Value v1, Value v2) const
Return true if v1 and v2 bufferize to equivalent buffers.
SmallVector< OpOperand * > getAliasingOpOperand(OpResult result) const
Determine which OpOperand* will alias with result if the op is bufferized in place.
SmallVector< OpResult > getAliasingOpResult(OpOperand &opOperand) const
Determine which OpResult will alias with opOperand if the op is bufferized in place.
AnalysisState(const AnalysisState &)=delete
void denyOperation()
Deny the given ops.
void allowOperation(StringRef opName)
Allow the given op.
void denyOperation(StringRef opName)
Deny the given op.
void denyDialect()
Deny the given dialects.
void allowOperation(Entry::FilterFn fn)
Allow ops that are matched by fn.
void allowDialect()
Allow the given dialects.
void allowDialect(StringRef dialectNamespace)
Allow the given dialect.
void denyOperation(Entry::FilterFn fn)
Deny ops that are matched by fn.
bool isOpAllowed(Operation *op) const
Return whether the op is allowed or not.
void allowOperation()
Allow the given ops.
FailureOr< BaseMemRefType > defaultGetBufferType(Value value, const BufferizationOptions &options, const DenseMap< Value, BaseMemRefType > &fixedTypes)
This is the default implementation of BufferizableOpInterface::getBufferType.
bool defaultIsRepetitiveRegion(BufferizableOpInterface bufferizableOp, unsigned index)
This is the default implementation of BufferizableOpInterface::isRepetitiveRegion.
void replaceOpWithBufferizedValues(RewriterBase &rewriter, Operation *op, ValueRange values)
Replace an op with replacement values.
BaseMemRefType getMemRefTypeWithStaticIdentityLayout(TensorType tensorType, Attribute memorySpace=nullptr)
Return a MemRef type with a static identity layout (i.e., no layout map).
Operation * getOwnerOfValue(Value value)
Return the owner of the given value.
bool allocationDoesNotEscape(OpResult opResult)
Return true if the allocation of the given op is guaranteed to not escape the containing block.
BaseMemRefType getMemRefType(Value value, const BufferizationOptions &options, MemRefLayoutAttrInterface layout={}, Attribute memorySpace=nullptr)
Return a MemRefType to which the type of the given value can be bufferized.
bool shouldDeallocateOpResult(OpResult opResult, const BufferizationOptions &options)
Return true if the buffer of given OpResult should be deallocated.
Region * getEnclosingRepetitiveRegion(Operation *op, const BufferizationOptions &options)
Return the closest enclosing repetitive region around the given op.
FailureOr< Value > allocateTensorForShapedValue(OpBuilder &b, Location loc, Value shapedValue, bool escape, const BufferizationOptions &options, bool copy=true)
Create an AllocTensorOp for the given shaped value (memref or tensor).
OpTy replaceOpWithNewBufferizedOp(RewriterBase &rewriter, Operation *op, Args &&...args)
Replace an op with a new op.
bool isFunctionArgument(Value value)
Return true if the given value is a BlockArgument of a func::FuncOp.
FailureOr< BaseMemRefType > getBufferType(Value value, const BufferizationOptions &options)
Return the buffer type for a given Value (tensor) after bufferization without bufferizing any IR.
FailureOr< Value > getBuffer(RewriterBase &rewriter, Value value, const BufferizationOptions &options)
Lookup the buffer for the given value.
BaseMemRefType getMemRefTypeWithFullyDynamicLayout(TensorType tensorType, Attribute memorySpace=nullptr)
Return a MemRef type with fully dynamic layout.
BufferRelation
Specify fine-grain relationship between buffers to enable more analysis.
Include the generated interface declarations.
This class represents an efficient way to signal success or failure.
Options for BufferizableOpInterface-based bufferization.
bool createDeallocs
Specifies whether dealloc ops should be generated along with alloc ops.
bool copyBeforeWrite
If set to true, the analysis is skipped.
unsigned analysisFuzzerSeed
Seed for the analysis fuzzer.
std::function< LogicalResult(OpBuilder &, Location, Value)> DeallocationFn
Deallocator function: Deallocate a buffer that was allocated with AllocatorFn.
std::function< void(AnalysisState &)> AnalysisStateInitFn
Initializer function for analysis state.
bool allowUnknownOps
Specifies whether not bufferizable ops are allowed in the input.
BufferizableOpInterface dynCastBufferizableOp(Operation *op) const
Try to cast the given op to BufferizableOpInterface if the op is allow listed.
std::function< BaseMemRefType(Value, Attribute memorySpace, const BufferizationOptions &)> UnknownTypeConverterFn
Tensor -> MemRef type converter.
LayoutMapOption functionBoundaryTypeConversion
This flag controls buffer types on function signatures.
unsigned int bufferAlignment
Buffer alignment for new memory allocations.
bool printConflicts
If set to true, the IR is annotated with details about RaW conflicts.
std::optional< AllocationFn > allocationFn
Helper functions for allocation, deallocation, memory copying.
bool testAnalysisOnly
If set to true, does not modify the IR apart from adding attributes (for checking the results of the ...
bool enforceAliasingInvariants
Certain ops have aliasing OpOperand/OpResult invariants (e.g., scf.for).
OpFilter opFilter
A filter that specifies which ops should be bufferized and which ops should be ignored.
bool isOpAllowed(Operation *op) const
Return true if the given op should be bufferized.
std::optional< DeallocationFn > deallocationFn
std::optional< MemCpyFn > memCpyFn
std::optional< Attribute > defaultMemorySpace
The default memory space that should be used when it cannot be inferred from the context.
UnknownTypeConverterFn unknownTypeConverterFn
Type converter from tensors to memrefs.
bool bufferizeFunctionBoundaries
Specifies whether function boundaries (ops in the func dialect) should be bufferized or not.
std::function< LogicalResult(OpBuilder &, Location, Value, Value)> MemCpyFn
Memcpy function: Generate a memcpy between two buffers.
LogicalResult createDealloc(OpBuilder &b, Location loc, Value allocatedBuffer) const
Creates a memref deallocation.
FailureOr< Value > createAlloc(OpBuilder &b, Location loc, MemRefType type, ValueRange dynShape) const
Create a memref allocation with the given type and dynamic extents.
std::function< FailureOr< Value >(OpBuilder &, Location, MemRefType, ValueRange, unsigned int)> AllocationFn
Allocator function: Generate a memref allocation with the given type, dynamic extents and alignment.
LogicalResult createMemCpy(OpBuilder &b, Location loc, Value from, Value to) const
Creates a memcpy between two given buffers.
SmallVector< AnalysisStateInitFn > stateInitializers
Initializer functions for analysis state.
FilterType
Filter type: A filter can either be a DENY filter or an ALLOW filter.
std::function< bool(Operation *)> FilterFn
If the filter function evaluates to true, the filter matches.