Skip to content

Replace incompatible fpga

replace_incompatible_fpga

ReplaceCopySignOpByXilinxMath dataclass

Bases: RewritePattern

Source code in xdsl/transforms/experimental/replace_incompatible_fpga.py
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
@dataclass
class ReplaceCopySignOpByXilinxMath(RewritePattern):
    def __init__(self, op: builtin.ModuleOp):
        self.module = op
        self.func_def_declaration = False

    @op_type_rewrite_pattern
    def match_and_rewrite(self, op: CopySignOp, rewriter: PatternRewriter, /):
        if not self.func_def_declaration:
            func_def = FuncOp.external("llvm.copysign.f64", [f64, f64], [f64])
            self.module.body.block.add_op(func_def)
            self.func_def_declaration = True

        call = CallOp("llvm.copysign.f64", [op.lhs, op.rhs], [f64])

        rewriter.replace_op(op, [call])

module = op instance-attribute

func_def_declaration = False instance-attribute

__init__(op: builtin.ModuleOp)

Source code in xdsl/transforms/experimental/replace_incompatible_fpga.py
21
22
23
def __init__(self, op: builtin.ModuleOp):
    self.module = op
    self.func_def_declaration = False

match_and_rewrite(op: CopySignOp, rewriter: PatternRewriter)

Source code in xdsl/transforms/experimental/replace_incompatible_fpga.py
25
26
27
28
29
30
31
32
33
34
@op_type_rewrite_pattern
def match_and_rewrite(self, op: CopySignOp, rewriter: PatternRewriter, /):
    if not self.func_def_declaration:
        func_def = FuncOp.external("llvm.copysign.f64", [f64, f64], [f64])
        self.module.body.block.add_op(func_def)
        self.func_def_declaration = True

    call = CallOp("llvm.copysign.f64", [op.lhs, op.rhs], [f64])

    rewriter.replace_op(op, [call])

ReplaceMaximumfByXilinxMath dataclass

Bases: RewritePattern

Source code in xdsl/transforms/experimental/replace_incompatible_fpga.py
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
@dataclass
class ReplaceMaximumfByXilinxMath(RewritePattern):
    def __init__(self, op: builtin.ModuleOp):
        self.module = op
        self.func_def_declaration = False

    @op_type_rewrite_pattern
    def match_and_rewrite(self, op: MaximumfOp, rewriter: PatternRewriter, /):
        if not self.func_def_declaration:
            func_def = FuncOp.external("llvm.maxnum.f64", [f64, f64], [f64])
            self.module.body.block.add_op(func_def)
            self.func_def_declaration = True

        call = CallOp("llvm.maxnum.f64", [op.lhs, op.rhs], [f64])

        rewriter.replace_op(op, [call])

module = op instance-attribute

func_def_declaration = False instance-attribute

__init__(op: builtin.ModuleOp)

Source code in xdsl/transforms/experimental/replace_incompatible_fpga.py
39
40
41
def __init__(self, op: builtin.ModuleOp):
    self.module = op
    self.func_def_declaration = False

match_and_rewrite(op: MaximumfOp, rewriter: PatternRewriter)

Source code in xdsl/transforms/experimental/replace_incompatible_fpga.py
43
44
45
46
47
48
49
50
51
52
@op_type_rewrite_pattern
def match_and_rewrite(self, op: MaximumfOp, rewriter: PatternRewriter, /):
    if not self.func_def_declaration:
        func_def = FuncOp.external("llvm.maxnum.f64", [f64, f64], [f64])
        self.module.body.block.add_op(func_def)
        self.func_def_declaration = True

    call = CallOp("llvm.maxnum.f64", [op.lhs, op.rhs], [f64])

    rewriter.replace_op(op, [call])

ReplaceAbsOpByXilinxMath dataclass

Bases: RewritePattern

Source code in xdsl/transforms/experimental/replace_incompatible_fpga.py
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
@dataclass
class ReplaceAbsOpByXilinxMath(RewritePattern):
    def __init__(self, op: builtin.ModuleOp):
        self.module = op
        self.func_def_declaration = False

    @op_type_rewrite_pattern
    def match_and_rewrite(self, op: AbsFOp, rewriter: PatternRewriter, /):
        if not self.func_def_declaration:
            func_def = FuncOp.external("llvm.fabs.f64", [f64], [f64])
            self.module.body.block.add_op(func_def)
            self.func_def_declaration = True

        call = CallOp("llvm.fabs.f64", [op.operand], [f64])

        rewriter.replace_op(op, [call])

module = op instance-attribute

func_def_declaration = False instance-attribute

__init__(op: builtin.ModuleOp)

Source code in xdsl/transforms/experimental/replace_incompatible_fpga.py
57
58
59
def __init__(self, op: builtin.ModuleOp):
    self.module = op
    self.func_def_declaration = False

match_and_rewrite(op: AbsFOp, rewriter: PatternRewriter)

Source code in xdsl/transforms/experimental/replace_incompatible_fpga.py
61
62
63
64
65
66
67
68
69
70
@op_type_rewrite_pattern
def match_and_rewrite(self, op: AbsFOp, rewriter: PatternRewriter, /):
    if not self.func_def_declaration:
        func_def = FuncOp.external("llvm.fabs.f64", [f64], [f64])
        self.module.body.block.add_op(func_def)
        self.func_def_declaration = True

    call = CallOp("llvm.fabs.f64", [op.operand], [f64])

    rewriter.replace_op(op, [call])

ReplaceIncompatibleFPGA dataclass

Bases: ModulePass

Source code in xdsl/transforms/experimental/replace_incompatible_fpga.py
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
@dataclass(frozen=True)
class ReplaceIncompatibleFPGA(ModulePass):
    name = "replace-incompatible-fpga"

    def apply(self, ctx: Context, op: builtin.ModuleOp) -> None:
        def gen_greedy_walkers(
            passes: list[RewritePattern],
        ) -> list[PatternRewriteWalker]:
            # Creates a greedy walker for each pass, so that they can be run sequentially even after
            # matching
            walkers: list[PatternRewriteWalker] = []

            for i in range(len(passes)):
                walkers.append(
                    PatternRewriteWalker(
                        GreedyRewritePatternApplier([passes[i]]), apply_recursively=True
                    )
                )

            return walkers

        walkers = gen_greedy_walkers(
            [
                # ReplaceCopySignOpByEquivalent(),
                ReplaceCopySignOpByXilinxMath(op),
                # ReplaceMaximumfOpByEquivalent(),
                ReplaceMaximumfByXilinxMath(op),
                # ReplaceAbsOpByEquivalent(),
                ReplaceAbsOpByXilinxMath(op),
            ]
        )

        for walker in walkers:
            walker.rewrite_module(op)

name = 'replace-incompatible-fpga' class-attribute instance-attribute

__init__() -> None

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

Source code in xdsl/transforms/experimental/replace_incompatible_fpga.py
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
def apply(self, ctx: Context, op: builtin.ModuleOp) -> None:
    def gen_greedy_walkers(
        passes: list[RewritePattern],
    ) -> list[PatternRewriteWalker]:
        # Creates a greedy walker for each pass, so that they can be run sequentially even after
        # matching
        walkers: list[PatternRewriteWalker] = []

        for i in range(len(passes)):
            walkers.append(
                PatternRewriteWalker(
                    GreedyRewritePatternApplier([passes[i]]), apply_recursively=True
                )
            )

        return walkers

    walkers = gen_greedy_walkers(
        [
            # ReplaceCopySignOpByEquivalent(),
            ReplaceCopySignOpByXilinxMath(op),
            # ReplaceMaximumfOpByEquivalent(),
            ReplaceMaximumfByXilinxMath(op),
            # ReplaceAbsOpByEquivalent(),
            ReplaceAbsOpByXilinxMath(op),
        ]
    )

    for walker in walkers:
        walker.rewrite_module(op)