MLIR  19.0.0git
Public Member Functions | List of all members
SortCommutativeOperands Class Reference

Sorts the operands of op in ascending order of the "key" associated with each operand iff op is commutative. More...

+ Inheritance diagram for SortCommutativeOperands:

Public Member Functions

 SortCommutativeOperands (MLIRContext *context)
 
LogicalResult matchAndRewrite (Operation *op, PatternRewriter &rewriter) const override
 Attempt to match against code rooted at the specified operation, which is the same operation code as getRootKind(). More...
 
- Public Member Functions inherited from mlir::RewritePattern
virtual ~RewritePattern ()=default
 
virtual void rewrite (Operation *op, PatternRewriter &rewriter) const
 Rewrite the IR rooted at the specified operation with the result of this pattern, generating any new operations with the specified builder. More...
 
virtual LogicalResult match (Operation *op) const
 Attempt to match against code rooted at the specified operation, which is the same operation code as getRootKind(). More...
 
- Public Member Functions inherited from mlir::Pattern
ArrayRef< OperationNamegetGeneratedOps () const
 Return a list of operations that may be generated when rewriting an operation instance with this pattern. More...
 
std::optional< OperationNamegetRootKind () const
 Return the root node that this pattern matches. More...
 
std::optional< TypeIDgetRootInterfaceID () const
 Return the interface ID used to match the root operation of this pattern. More...
 
std::optional< TypeIDgetRootTraitID () const
 Return the trait ID used to match the root operation of this pattern. More...
 
PatternBenefit getBenefit () const
 Return the benefit (the inverse of "cost") of matching this pattern. More...
 
bool hasBoundedRewriteRecursion () const
 Returns true if this pattern is known to result in recursive application, i.e. More...
 
MLIRContextgetContext () const
 Return the MLIRContext used to create this pattern. More...
 
StringRef getDebugName () const
 Return a readable name for this pattern. More...
 
void setDebugName (StringRef name)
 Set the human readable debug name used for this pattern. More...
 
ArrayRef< StringRef > getDebugLabels () const
 Return the set of debug labels attached to this pattern. More...
 
void addDebugLabels (ArrayRef< StringRef > labels)
 Add the provided debug labels to this pattern. More...
 
void addDebugLabels (StringRef label)
 

Additional Inherited Members

- Static Public Member Functions inherited from mlir::RewritePattern
template<typename T , typename... Args>
static std::unique_ptr< T > create (Args &&...args)
 This method provides a convenient interface for creating and initializing derived rewrite patterns of the given type T. More...
 
- Protected Member Functions inherited from mlir::RewritePattern
 Pattern (StringRef rootName, PatternBenefit benefit, MLIRContext *context, ArrayRef< StringRef > generatedNames={})
 Inherit the base constructors from Pattern. More...
 
 Pattern (MatchAnyOpTypeTag tag, PatternBenefit benefit, MLIRContext *context, ArrayRef< StringRef > generatedNames={})
 Inherit the base constructors from Pattern. More...
 
 Pattern (MatchInterfaceOpTypeTag tag, TypeID interfaceID, PatternBenefit benefit, MLIRContext *context, ArrayRef< StringRef > generatedNames={})
 Inherit the base constructors from Pattern. More...
 
 Pattern (MatchTraitOpTypeTag tag, TypeID traitID, PatternBenefit benefit, MLIRContext *context, ArrayRef< StringRef > generatedNames={})
 Inherit the base constructors from Pattern. More...
 
- Protected Member Functions inherited from mlir::Pattern
 Pattern (StringRef rootName, PatternBenefit benefit, MLIRContext *context, ArrayRef< StringRef > generatedNames={})
 Construct a pattern with a certain benefit that matches the operation with the given root name. More...
 
 Pattern (MatchAnyOpTypeTag tag, PatternBenefit benefit, MLIRContext *context, ArrayRef< StringRef > generatedNames={})
 Construct a pattern that may match any operation type. More...
 
 Pattern (MatchInterfaceOpTypeTag tag, TypeID interfaceID, PatternBenefit benefit, MLIRContext *context, ArrayRef< StringRef > generatedNames={})
 Construct a pattern that may match any operation that implements the interface defined by the provided interfaceID. More...
 
 Pattern (MatchTraitOpTypeTag tag, TypeID traitID, PatternBenefit benefit, MLIRContext *context, ArrayRef< StringRef > generatedNames={})
 Construct a pattern that may match any operation that implements the trait defined by the provided traitID. More...
 
void setHasBoundedRewriteRecursion (bool hasBoundedRecursionArg=true)
 Set the flag detailing if this pattern has bounded rewrite recursion or not. More...
 

Detailed Description

Sorts the operands of op in ascending order of the "key" associated with each operand iff op is commutative.

This is a stable sort.

After the application of this pattern, since the commutative operands now have a deterministic order in which they occur in an op, the matching of large DAGs becomes much simpler, i.e., requires much less number of checks to be written by a user in her/his pattern matching function.

Some examples of such a sorting:

Assume that the sorting is being applied to foo.commutative, which is a commutative op.

Example 1:

%1 = foo.const 0 %2 = foo.mul <block argument>, <block argument> %3 = foo.commutative %1, %2

Here,

  1. The key associated with %1 is: `{ {CONSTANT_OP, "foo.const"} }`
  2. The key associated with %2 is: `{ {NON_CONSTANT_OP, "foo.mul"}, {BLOCK_ARGUMENT, ""}, {BLOCK_ARGUMENT, ""} }`

The key of %2 < the key of %1 Thus, the sorted foo.commutative is: %3 = foo.commutative %2, %1

Example 2:

%1 = foo.const 0 %2 = foo.mul <block argument>, <block argument> %3 = foo.mul %2, %1 %4 = foo.add %2, %1 %5 = foo.commutative %1, %2, %3, %4

Here,

  1. The key associated with %1 is: `{ {CONSTANT_OP, "foo.const"} }`
  2. The key associated with %2 is: `{ {NON_CONSTANT_OP, "foo.mul"}, {BLOCK_ARGUMENT, ""} }`
  3. The key associated with %3 is: `{ {NON_CONSTANT_OP, "foo.mul"}, {NON_CONSTANT_OP, "foo.mul"}, {CONSTANT_OP, "foo.const"}, {BLOCK_ARGUMENT, ""}, {BLOCK_ARGUMENT, ""} }`
  4. The key associated with %4 is: `{ {NON_CONSTANT_OP, "foo.add"}, {NON_CONSTANT_OP, "foo.mul"}, {CONSTANT_OP, "foo.const"}, {BLOCK_ARGUMENT, ""}, {BLOCK_ARGUMENT, ""} }`

Thus, the sorted foo.commutative is: %5 = foo.commutative %4, %3, %2, %1

Definition at line 230 of file CommutativityUtils.cpp.

Constructor & Destructor Documentation

◆ SortCommutativeOperands()

SortCommutativeOperands::SortCommutativeOperands ( MLIRContext context)
inline

Definition at line 232 of file CommutativityUtils.cpp.

Member Function Documentation

◆ matchAndRewrite()

LogicalResult SortCommutativeOperands::matchAndRewrite ( Operation op,
PatternRewriter rewriter 
) const
inlineoverridevirtual

Attempt to match against code rooted at the specified operation, which is the same operation code as getRootKind().

If successful, this function will automatically perform the rewrite.

Reimplemented from mlir::RewritePattern.

Definition at line 234 of file CommutativityUtils.cpp.


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