MLIR 24.0.0git
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 Types

enum class  ConstantInsertionResult { Unchanged , Hoisted , Replaced }
 The effect of inserting a known constant on the IR. More...

Public Member Functions

 OperationFolder (MLIRContext *ctx, OpBuilder::Listener *listener=nullptr)
LogicalResult tryToFold (Operation *op, bool *inPlaceUpdate=nullptr, int maxIterations=INT_MAX)
 Tries to perform folding on the given op, including unifying deduplicated constants.
ConstantInsertionResult insertKnownConstant (Operation *op, Attribute constValue={})
 Tries to fold a pre-existing constant operation.
void notifyRemoval (Operation *op)
 Notifies that the given constant op should be remove from this OperationFolder's internal bookkeeping.
void clear ()
 Clear out any constants cached inside of the folder.
Value getOrCreateConstant (Block *block, Dialect *dialect, Attribute value, Type type)
 Get or create a constant for use in the specified block.

Detailed Description

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

Definition at line 33 of file FoldUtils.h.

Member Enumeration Documentation

◆ ConstantInsertionResult

The effect of inserting a known constant on the IR.

Enumerator
Unchanged 

The operation was retained without modifying the IR.

Hoisted 

The operation was retained but moved to the insertion point.

Replaced 

The operation was replaced with an existing constant and erased.

Definition at line 49 of file FoldUtils.h.

Constructor & Destructor Documentation

◆ OperationFolder()

mlir::OperationFolder::OperationFolder ( MLIRContext * ctx,
OpBuilder::Listener * listener = nullptr )
inline

Definition at line 35 of file FoldUtils.h.

References mlir::get().

Member Function Documentation

◆ clear()

void OperationFolder::clear ( )

Clear out any constants cached inside of the folder.

Definition at line 205 of file FoldUtils.cpp.

◆ getOrCreateConstant()

Value OperationFolder::getOrCreateConstant ( Block * block,
Dialect * dialect,
Attribute value,
Type type )

Get or create a constant for use in the specified block.

Get or create a constant using the given builder.

The constant may be created in a parent block. On success this returns the constant operation, nullptr otherwise.

On success this returns the constant operation, nullptr otherwise.

Definition at line 212 of file FoldUtils.cpp.

References getInsertionRegion(), and mlir::Operation::getResult().

Referenced by replaceWithConstant().

◆ insertKnownConstant()

OperationFolder::ConstantInsertionResult 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 Replaced if an existing constant for op already exists in the folder, in which case op is replaced and erased. Otherwise, op is inserted into the folder (and hoisted if necessary), returning Hoisted if it was moved or Unchanged if the IR was not modified.

Definition at line 114 of file FoldUtils.cpp.

References mlir::Block::front(), mlir::Region::front(), mlir::Operation::getBlock(), mlir::Operation::getDialect(), getInsertionRegion(), mlir::Operation::getResults(), Hoisted, mlir::m_Constant(), mlir::matchPattern(), notifyRemoval(), Replaced, result, mlir::Operation::result_type_begin(), mlir::Operation::setLoc(), and Unchanged.

◆ 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 181 of file FoldUtils.cpp.

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

Referenced by insertKnownConstant(), and tryToFold().

◆ tryToFold()

LogicalResult OperationFolder::tryToFold ( Operation * op,
bool * inPlaceUpdate = nullptr,
int maxIterations = INT_MAX )

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

If successful, replaces op's uses with folded results, and returns success. If the op was completely folded it is erased. If it is just updated in place, inPlaceUpdate is set to true. On success() and when in-place, the folder is invoked until maxIterations is reached (default INT_MAX).

Definition at line 71 of file FoldUtils.cpp.

References mlir::Block::front(), mlir::Operation::getBlock(), notifyRemoval(), mlir::Operation::setLoc(), success(), and tryToFold().

Referenced by tryToFold().


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