Skip to content

[MLIR] Add folding constants canonicalization for mlir::index::AddOp. #111084

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions mlir/include/mlir/Dialect/Index/IR/IndexOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ def Index_AddOp : IndexBinaryOp<"add", [Commutative, Pure]> {
%c = index.add %a, %b
```
}];

let hasCanonicalizeMethod = 1;
}

//===----------------------------------------------------------------------===//
Expand Down
22 changes: 22 additions & 0 deletions mlir/lib/Dialect/Index/IR/IndexOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,28 @@ OpFoldResult AddOp::fold(FoldAdaptor adaptor) {

return {};
}
/// Canonicalize
/// ` x = v + c1; y = x + c2` to `x = v + (c1 + c2)`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only permutation of the pattern that you actually need to implement. The canonicalizer will make sure that constants for commutative ops are always on the RHS.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, good to know, PR updated!

LogicalResult AddOp::canonicalize(AddOp op, PatternRewriter &rewriter) {
IntegerAttr c1, c2;
if (!mlir::matchPattern(op.getRhs(), mlir::m_Constant(&c1)))
return rewriter.notifyMatchFailure(op.getLoc(), "RHS is not a constant");

auto add = op.getLhs().getDefiningOp<mlir::index::AddOp>();
if (!add)
return rewriter.notifyMatchFailure(op.getLoc(), "LHS is not a add");

if (!mlir::matchPattern(add.getRhs(), mlir::m_Constant(&c2)))
return rewriter.notifyMatchFailure(op.getLoc(), "RHS is not a constant");

auto c = rewriter.create<mlir::index::ConstantOp>(op->getLoc(),
c1.getInt() + c2.getInt());
auto newAdd =
rewriter.create<mlir::index::AddOp>(op->getLoc(), add.getLhs(), c);

rewriter.replaceOp(op, newAdd);
return success();
}

//===----------------------------------------------------------------------===//
// SubOp
Expand Down
13 changes: 13 additions & 0 deletions mlir/test/Dialect/Index/index-canonicalize.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ func.func @add_overflow() -> (index, index) {
return %2, %3 : index, index
}

// CHECK-LABEL: @add
func.func @add_fold_constants(%arg: index) -> (index) {
%0 = index.constant 1
%1 = index.constant 2
%2 = index.add %arg, %0
%3 = index.add %2, %1

// CHECK-DAG: [[C3:%.*]] = index.constant 3
// CHECK-DAG: [[V0:%.*]] = index.add %arg0, [[C3]]
// CHECK: return [[V0]]
return %3 : index
}

// CHECK-LABEL: @sub
func.func @sub() -> index {
%0 = index.constant -2000000000
Expand Down
Loading