-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5111b75
commit f5157af
Showing
3 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
tests/filecheck/transforms/apply-eqsat-pdl/apply_eqsat_pdl_swap_inputs.mlir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// RUN: xdsl-opt %s -p apply-eqsat-pdl | filecheck %s | ||
|
||
// CHECK: func.func @impl() -> i32 { | ||
// CHECK-NEXT: %c4 = arith.constant 4 : i32 | ||
// CHECK-NEXT: %c4_eq = eqsat.eclass %c4 : i32 | ||
// CHECK-NEXT: %c2 = arith.constant 2 : i32 | ||
// CHECK-NEXT: %c2_eq = eqsat.eclass %c2 : i32 | ||
// CHECK-NEXT: %0 = arith.addi %c2_eq, %c4_eq : i32 | ||
// CHECK-NEXT: %sum = arith.addi %c4_eq, %c2_eq : i32 | ||
// CHECK-NEXT: %sum_eq = eqsat.eclass %sum, %0 : i32 | ||
// CHECK-NEXT: func.return %sum_eq : i32 | ||
// CHECK-NEXT: } | ||
|
||
func.func @impl() -> i32 { | ||
%c4 = arith.constant 4 : i32 | ||
%c4_eq = eqsat.eclass %c4 : i32 | ||
%c2 = arith.constant 2 : i32 | ||
%c2_eq = eqsat.eclass %c2 : i32 | ||
%sum = arith.addi %c4_eq, %c2_eq : i32 | ||
%sum_eq = eqsat.eclass %sum : i32 | ||
func.return %sum_eq : i32 | ||
} | ||
|
||
pdl.pattern : benefit(1) { | ||
%x = pdl.operand | ||
%y = pdl.operand | ||
%type = pdl.type | ||
%x_y = pdl.operation "arith.addi" (%x, %y : !pdl.value, !pdl.value) -> (%type : !pdl.type) | ||
pdl.rewrite %x_y { | ||
%y_x = pdl.operation "arith.addi" (%y, %x : !pdl.value, !pdl.value) -> (%type : !pdl.type) | ||
pdl.replace %x_y with %y_x | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import os | ||
from dataclasses import dataclass | ||
|
||
from xdsl.context import MLContext | ||
from xdsl.dialects import builtin, pdl | ||
from xdsl.interpreters.eqsat_pdl import EqsatPDLRewritePattern | ||
from xdsl.parser import Parser | ||
from xdsl.passes import ModulePass | ||
from xdsl.pattern_rewriter import ( | ||
GreedyRewritePatternApplier, | ||
PatternRewriteWalker, | ||
RewritePattern, | ||
) | ||
|
||
|
||
@dataclass(frozen=True) | ||
class ApplyEqsatPDLPass(ModulePass): | ||
name = "apply-eqsat-pdl" | ||
|
||
pdl_file: str | None = None | ||
|
||
def apply(self, ctx: MLContext, op: builtin.ModuleOp) -> None: | ||
payload_module = op | ||
if self.pdl_file is not None: | ||
assert os.path.exists(self.pdl_file) | ||
with open(self.pdl_file) as f: | ||
pdl_module_str = f.read() | ||
parser = Parser(ctx, pdl_module_str) | ||
pdl_module = parser.parse_module() | ||
else: | ||
pdl_module = payload_module | ||
rewrite_patterns: list[RewritePattern] = [ | ||
EqsatPDLRewritePattern(op, ctx, None) | ||
for op in pdl_module.walk() | ||
if isinstance(op, pdl.RewriteOp) | ||
] | ||
pattern_applier = GreedyRewritePatternApplier(rewrite_patterns) | ||
# TODO: remove apply_recursively=False | ||
PatternRewriteWalker(pattern_applier, apply_recursively=False).rewrite_op( | ||
payload_module | ||
) |