Skip to content
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

transformations: (pdl) Apply PDL patterns from source #3391

Merged
merged 8 commits into from
Nov 4, 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
9 changes: 9 additions & 0 deletions tests/filecheck/interpreters/extra_file.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
pdl.pattern : benefit(1) {
%zero_attr = pdl.attribute = 0
%root = pdl.operation "test.op" {"attr" = %zero_attr}
pdl.rewrite %root {
%one_attr = pdl.attribute = 1
%new_op = pdl.operation "test.op" {"attr" = %one_attr}
pdl.replace %root with %new_op
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// RUN: xdsl-opt %s -p 'apply-pdl{pdl_file="%p/extra_file.mlir"}' | filecheck %s

"test.op"() {attr = 0} : () -> ()

//CHECK: builtin.module {
// CHECK-NEXT: "test.op"() {"attr" = 1 : i64} : () -> ()
// CHECK-NEXT: }
26 changes: 26 additions & 0 deletions tests/filecheck/interpreters/test_pdl_interpreter_simple.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// RUN: xdsl-opt %s -p apply-pdl | filecheck %s

"test.op"() {attr = 0} : () -> ()

pdl.pattern : benefit(1) {
%zero_attr = pdl.attribute = 0
%root = pdl.operation "test.op" {"attr" = %zero_attr}
pdl.rewrite %root {
%one_attr = pdl.attribute = 1
%new_op = pdl.operation "test.op" {"attr" = %one_attr}
pdl.replace %root with %new_op
}
}

//CHECK: builtin.module {
// CHECK-NEXT: "test.op"() {"attr" = 1 : i64} : () -> ()
// CHECK-NEXT: pdl.pattern : benefit(1) {
// CHECK-NEXT: %zero_attr = pdl.attribute = 0 : i64
// CHECK-NEXT: %root = pdl.operation "test.op" {"attr" = %zero_attr}
// CHECK-NEXT: pdl.rewrite %root {
// CHECK-NEXT: %one_attr = pdl.attribute = 1 : i64
// CHECK-NEXT: %new_op = pdl.operation "test.op" {"attr" = %one_attr}
// CHECK-NEXT: pdl.replace %root with %new_op
// CHECK-NEXT: }
// CHECK-NEXT: }
// CHECK-NEXT: }
6 changes: 6 additions & 0 deletions xdsl/transforms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
def get_all_passes() -> dict[str, Callable[[], type[ModulePass]]]:
"""Return the list of all available passes."""

def get_apply_pdl():
from xdsl.transforms import apply_pdl

return apply_pdl.ApplyPDLPass

def get_arith_add_fastmath():
from xdsl.transforms import arith_add_fastmath

Expand Down Expand Up @@ -446,6 +451,7 @@ def get_varith_fuse_repeated_operands():
return varith_transformations.VarithFuseRepeatedOperandsPass

return {
"apply-pdl": get_apply_pdl,
"arith-add-fastmath": get_arith_add_fastmath,
"loop-hoist-memref": get_loop_hoist_memref,
"canonicalize-dmp": get_canonicalize_dmp,
Expand Down
40 changes: 40 additions & 0 deletions xdsl/transforms/apply_pdl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import os
from dataclasses import dataclass

from xdsl.context import MLContext
from xdsl.dialects import builtin, pdl
from xdsl.interpreters.pdl import (
PDLRewritePattern,
)
from xdsl.parser import Parser
from xdsl.passes import ModulePass
from xdsl.pattern_rewriter import (
GreedyRewritePatternApplier,
PatternRewriteWalker,
RewritePattern,
)


@dataclass(frozen=True)
class ApplyPDLPass(ModulePass):
name = "apply-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] = [
PDLRewritePattern(op, ctx, None)
for op in pdl_module.walk()
if isinstance(op, pdl.RewriteOp)
]
pattern_applier = GreedyRewritePatternApplier(rewrite_patterns)
PatternRewriteWalker(pattern_applier).rewrite_op(payload_module)
Loading