Skip to content

Func to pdl rewrite

func_to_pdl_rewrite

A pass that applies the interpreter to operations with no side effects where all the inputs are constant, replacing the computation with a constant value.

FuncOpToPdlRewritePattern dataclass

Bases: RewritePattern

Rewrite pattern that transforms a function into a PDL rewrite operation.

Source code in xdsl/transforms/experimental/func_to_pdl_rewrite.py
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
@dataclass
class FuncOpToPdlRewritePattern(RewritePattern):
    """Rewrite pattern that transforms a function into a PDL rewrite operation."""

    @op_type_rewrite_pattern
    def match_and_rewrite(self, op: func.FuncOp, rewriter: PatternRewriter):
        pdl_root = test.TestPureOp(result_types=(pdl.OperationType(),))
        op.detach_region(func_body := op.regions[0])
        rewrite_root = pdl_root.results[0]
        for arg in func_body.block.args:
            arg.replace_all_uses_with(rewrite_root)
            func_body.block.erase_arg(arg)

        pdl_pattern = pdl.PatternOp(
            benefit=1,
            sym_name=op.sym_name.data,
            body=Region(
                Block([pdl_root, pdl.RewriteOp(root=rewrite_root, body=func_body)])
            ),
        )
        rewriter.replace_op(op, pdl_pattern)

__init__() -> None

match_and_rewrite(op: func.FuncOp, rewriter: PatternRewriter)

Source code in xdsl/transforms/experimental/func_to_pdl_rewrite.py
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
@op_type_rewrite_pattern
def match_and_rewrite(self, op: func.FuncOp, rewriter: PatternRewriter):
    pdl_root = test.TestPureOp(result_types=(pdl.OperationType(),))
    op.detach_region(func_body := op.regions[0])
    rewrite_root = pdl_root.results[0]
    for arg in func_body.block.args:
        arg.replace_all_uses_with(rewrite_root)
        func_body.block.erase_arg(arg)

    pdl_pattern = pdl.PatternOp(
        benefit=1,
        sym_name=op.sym_name.data,
        body=Region(
            Block([pdl_root, pdl.RewriteOp(root=rewrite_root, body=func_body)])
        ),
    )
    rewriter.replace_op(op, pdl_pattern)

ReturnOpToPdlRewritePattern dataclass

Bases: RewritePattern

Rewrite pattern that erases return ops in PDL rewrite operations.

Source code in xdsl/transforms/experimental/func_to_pdl_rewrite.py
44
45
46
47
48
49
50
@dataclass
class ReturnOpToPdlRewritePattern(RewritePattern):
    """Rewrite pattern that erases return ops in PDL rewrite operations."""

    @op_type_rewrite_pattern
    def match_and_rewrite(self, op: func.ReturnOp, rewriter: PatternRewriter):
        rewriter.erase_op(op)

__init__() -> None

match_and_rewrite(op: func.ReturnOp, rewriter: PatternRewriter)

Source code in xdsl/transforms/experimental/func_to_pdl_rewrite.py
48
49
50
@op_type_rewrite_pattern
def match_and_rewrite(self, op: func.ReturnOp, rewriter: PatternRewriter):
    rewriter.erase_op(op)

FuncToPdlRewrite dataclass

Bases: ModulePass

A pass that transforms a function into a PDL rewrite operation.

Source code in xdsl/transforms/experimental/func_to_pdl_rewrite.py
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
class FuncToPdlRewrite(ModulePass):
    """
    A pass that transforms a function into a PDL rewrite operation.
    """

    name = "func-to-pdl-rewrite"

    def apply(self, ctx: Context, op: builtin.ModuleOp) -> None:
        """Apply the pass."""
        PatternRewriteWalker(
            GreedyRewritePatternApplier(
                [
                    FuncOpToPdlRewritePattern(),
                    ReturnOpToPdlRewritePattern(),
                ]
            )
        ).rewrite_module(op)

name = 'func-to-pdl-rewrite' class-attribute instance-attribute

apply(ctx: Context, op: builtin.ModuleOp) -> None

Apply the pass.

Source code in xdsl/transforms/experimental/func_to_pdl_rewrite.py
60
61
62
63
64
65
66
67
68
69
def apply(self, ctx: Context, op: builtin.ModuleOp) -> None:
    """Apply the pass."""
    PatternRewriteWalker(
        GreedyRewritePatternApplier(
            [
                FuncOpToPdlRewritePattern(),
                ReturnOpToPdlRewritePattern(),
            ]
        )
    ).rewrite_module(op)