MLIR  15.0.0git
Public Member Functions | List of all members
mlir::OperationFolder Class Reference

A utility class for folding operations, and unifying duplicated constants generated along the way. More...

#include "mlir/Transforms/FoldUtils.h"

Public Member Functions

 OperationFolder (MLIRContext *ctx)
 
LogicalResult tryToFold (Operation *op, function_ref< void(Operation *)> processGeneratedConstants=nullptr, function_ref< void(Operation *)> preReplaceAction=nullptr, bool *inPlaceUpdate=nullptr)
 Tries to perform folding on the given op, including unifying deduplicated constants. More...
 
bool insertKnownConstant (Operation *op, Attribute constValue={})
 Tries to fold a pre-existing constant operation. More...
 
void notifyRemoval (Operation *op)
 Notifies that the given constant op should be remove from this OperationFolder's internal bookkeeping. More...
 
template<typename OpTy , typename... Args>
void create (OpBuilder &builder, SmallVectorImpl< Value > &results, Location location, Args &&... args)
 Create an operation of specific op type with the given builder, and immediately try to fold it. More...
 
template<typename OpTy , typename... Args>
std::enable_if< OpTy::template hasTrait< OpTrait::OneResult >), Value >::type create (OpBuilder &builder, Location location, Args &&... args)
 Overload to create or fold a single result operation. More...
 
template<typename OpTy , typename... Args>
std::enable_if< OpTy::template hasTrait< OpTrait::ZeroResults >), OpTy >::type create (OpBuilder &builder, Location location, Args &&... args)
 Overload to create or fold a zero result operation. More...
 
void clear ()
 Clear out any constants cached inside of the folder. More...
 
Value getOrCreateConstant (OpBuilder &builder, Dialect *dialect, Attribute value, Type type, Location loc)
 Get or create a constant using the given builder. More...
 

Detailed Description

A utility class for folding operations, and unifying duplicated constants generated along the way.

Definition at line 32 of file FoldUtils.h.

Constructor & Destructor Documentation

◆ OperationFolder()

mlir::OperationFolder::OperationFolder ( MLIRContext ctx)
inline

Definition at line 34 of file FoldUtils.h.

References insertKnownConstant(), notifyRemoval(), and tryToFold().

Member Function Documentation

◆ clear()

void OperationFolder::clear ( )

Clear out any constants cached inside of the folder.

Definition at line 192 of file FoldUtils.cpp.

Referenced by create().

◆ create() [1/3]

template<typename OpTy , typename... Args>
void mlir::OperationFolder::create ( OpBuilder builder,
SmallVectorImpl< Value > &  results,
Location  location,
Args &&...  args 
)
inline

Create an operation of specific op type with the given builder, and immediately try to fold it.

This function populates 'results' with the results after folding the operation.

Definition at line 69 of file FoldUtils.h.

References mlir::Operation::create(), mlir::Operation::destroy(), mlir::failed(), mlir::OpBuilder::insert(), mlir::Operation::result_begin(), mlir::Operation::result_end(), and tryToFold().

◆ create() [2/3]

template<typename OpTy , typename... Args>
std::enable_if<OpTy::template hasTrait<OpTrait::OneResult>), Value>::type mlir::OperationFolder::create ( OpBuilder builder,
Location  location,
Args &&...  args 
)
inline

Overload to create or fold a single result operation.

Definition at line 91 of file FoldUtils.h.

◆ create() [3/3]

template<typename OpTy , typename... Args>
std::enable_if<OpTy::template hasTrait<OpTrait::ZeroResults>), OpTy>::type mlir::OperationFolder::create ( OpBuilder builder,
Location  location,
Args &&...  args 
)
inline

Overload to create or fold a zero result operation.

Definition at line 101 of file FoldUtils.h.

References clear(), mlir::OpBuilder::create(), getOrCreateConstant(), tryToFold(), and value.

◆ getOrCreateConstant()

Value OperationFolder::getOrCreateConstant ( OpBuilder builder,
Dialect dialect,
Attribute  value,
Type  type,
Location  loc 
)

◆ insertKnownConstant()

bool OperationFolder::insertKnownConstant ( Operation op,
Attribute  constValue = {} 
)

Tries to fold a pre-existing constant operation.

constValue represents the value of the constant, and can be optionally passed if the value is already known (e.g. if the constant was discovered by m_Constant). This is purely an optimization opportunity for callers that already know the value of the constant. Returns false if an existing constant for op already exists in the folder, in which case op is replaced and erased. Otherwise, returns true and op is inserted into the folder (and hoisted if necessary).

Definition at line 113 of file FoldUtils.cpp.

References mlir::Operation::erase(), mlir::Region::front(), mlir::Block::front(), mlir::Operation::getBlock(), mlir::Operation::getDialect(), getInsertionRegion(), mlir::m_Constant(), mlir::matchPattern(), mlir::Operation::moveBefore(), mlir::Operation::replaceAllUsesWith(), and mlir::Operation::result_type_begin().

Referenced by OperationFolder().

◆ notifyRemoval()

void OperationFolder::notifyRemoval ( Operation op)

Notifies that the given constant op should be remove from this OperationFolder's internal bookkeeping.

Note: this method must be called if a constant op is to be deleted externally to this OperationFolder. op must be a constant op.

Definition at line 168 of file FoldUtils.cpp.

References mlir::Operation::getBlock(), getInsertionRegion(), mlir::Operation::getResult(), mlir::Value::getType(), mlir::m_Constant(), and mlir::matchPattern().

Referenced by OperationFolder().

◆ tryToFold()

LogicalResult OperationFolder::tryToFold ( Operation op,
function_ref< void(Operation *)>  processGeneratedConstants = nullptr,
function_ref< void(Operation *)>  preReplaceAction = nullptr,
bool *  inPlaceUpdate = nullptr 
)

Tries to perform folding on the given op, including unifying deduplicated constants.

If successful, replaces op's uses with folded results, and returns success. preReplaceAction is invoked on op before it is replaced. 'processGeneratedConstants' is invoked for any new operations generated when folding. If the op was completely folded it is erased. If it is just updated in place, inPlaceUpdate is set to true.

Definition at line 70 of file FoldUtils.cpp.

References mlir::Operation::erase(), mlir::failed(), mlir::failure(), mlir::Block::front(), mlir::Operation::getBlock(), mlir::Operation::getResult(), mlir::Operation::moveBefore(), mlir::Value::replaceAllUsesWith(), and mlir::success().

Referenced by create(), getOrCreateConstant(), and OperationFolder().


The documentation for this class was generated from the following files: