Skip to content

Apply pdl interp

apply_pdl_interp

PDLInterpRewritePattern dataclass

Bases: RewritePattern

A rewrite pattern that uses the pdl_interp dialect for matching and rewriting operations.

Source code in xdsl/transforms/apply_pdl_interp.py
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
@dataclass
class PDLInterpRewritePattern(RewritePattern):
    """
    A rewrite pattern that uses the pdl_interp dialect for matching and rewriting operations.
    """

    interpreter: Interpreter
    functions: PDLInterpFunctions
    matcher: pdl_interp.FuncOp
    name: None | str = None

    def __init__(
        self,
        matcher: pdl_interp.FuncOp,
        interpreter: Interpreter,
        functions: PDLInterpFunctions,
        name: None | str = None,
    ):
        self.functions = functions
        module = matcher.parent_op()
        assert isinstance(module, ModuleOp)
        self.interpreter = interpreter
        if matcher.sym_name.data != "matcher":
            raise ValueError("Matcher function name must be 'matcher'")
        self.matcher = matcher
        self.name = name

    def match_and_rewrite(self, xdsl_op: Operation, rewriter: PatternRewriter) -> None:
        # Setup the rewriter
        self.functions.set_rewriter(self.interpreter, rewriter)

        # Call the matcher function on the operation
        self.interpreter.call_op(self.matcher, (xdsl_op,))

functions: PDLInterpFunctions = functions instance-attribute

interpreter: Interpreter = interpreter instance-attribute

matcher: pdl_interp.FuncOp = matcher instance-attribute

name: None | str = name class-attribute instance-attribute

__init__(matcher: pdl_interp.FuncOp, interpreter: Interpreter, functions: PDLInterpFunctions, name: None | str = None)

Source code in xdsl/transforms/apply_pdl_interp.py
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
def __init__(
    self,
    matcher: pdl_interp.FuncOp,
    interpreter: Interpreter,
    functions: PDLInterpFunctions,
    name: None | str = None,
):
    self.functions = functions
    module = matcher.parent_op()
    assert isinstance(module, ModuleOp)
    self.interpreter = interpreter
    if matcher.sym_name.data != "matcher":
        raise ValueError("Matcher function name must be 'matcher'")
    self.matcher = matcher
    self.name = name

match_and_rewrite(xdsl_op: Operation, rewriter: PatternRewriter) -> None

Source code in xdsl/transforms/apply_pdl_interp.py
42
43
44
45
46
47
def match_and_rewrite(self, xdsl_op: Operation, rewriter: PatternRewriter) -> None:
    # Setup the rewriter
    self.functions.set_rewriter(self.interpreter, rewriter)

    # Call the matcher function on the operation
    self.interpreter.call_op(self.matcher, (xdsl_op,))

ApplyPDLInterpPass dataclass

Bases: ModulePass

Source code in xdsl/transforms/apply_pdl_interp.py
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
@dataclass(frozen=True)
class ApplyPDLInterpPass(ModulePass):
    name = "apply-pdl-interp"

    pdl_interp_file: str | None = None

    def apply(self, ctx: Context, op: builtin.ModuleOp) -> None:
        if self.pdl_interp_file is not None:
            assert os.path.exists(self.pdl_interp_file)
            with open(self.pdl_interp_file) as f:
                pdl_interp_module_str = f.read()
                parser = Parser(ctx, pdl_interp_module_str)
                pdl_interp_module = parser.parse_module()
        else:
            pdl_interp_module = op
        matcher = None
        for cur in pdl_interp_module.walk():
            if isinstance(cur, pdl_interp.FuncOp) and cur.sym_name.data == "matcher":
                matcher = cur
                break
        assert matcher is not None, "matcher function not found"
        interpreter = Interpreter(pdl_interp_module)
        implementations = PDLInterpFunctions()
        PDLInterpFunctions.set_ctx(interpreter, ctx)
        interpreter.register_implementations(implementations)
        rewrite_pattern = PDLInterpRewritePattern(matcher, interpreter, implementations)
        PatternRewriteWalker(rewrite_pattern).rewrite_module(op)

name = 'apply-pdl-interp' class-attribute instance-attribute

pdl_interp_file: str | None = None class-attribute instance-attribute

__init__(pdl_interp_file: str | None = None) -> None

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

Source code in xdsl/transforms/apply_pdl_interp.py
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
def apply(self, ctx: Context, op: builtin.ModuleOp) -> None:
    if self.pdl_interp_file is not None:
        assert os.path.exists(self.pdl_interp_file)
        with open(self.pdl_interp_file) as f:
            pdl_interp_module_str = f.read()
            parser = Parser(ctx, pdl_interp_module_str)
            pdl_interp_module = parser.parse_module()
    else:
        pdl_interp_module = op
    matcher = None
    for cur in pdl_interp_module.walk():
        if isinstance(cur, pdl_interp.FuncOp) and cur.sym_name.data == "matcher":
            matcher = cur
            break
    assert matcher is not None, "matcher function not found"
    interpreter = Interpreter(pdl_interp_module)
    implementations = PDLInterpFunctions()
    PDLInterpFunctions.set_ctx(interpreter, ctx)
    interpreter.register_implementations(implementations)
    rewrite_pattern = PDLInterpRewritePattern(matcher, interpreter, implementations)
    PatternRewriteWalker(rewrite_pattern).rewrite_module(op)