Skip to content

Convert arith to x86

convert_arith_to_x86

X86_OP_BY_ARITH_BINARY_OP = {arith.AddiOp: x86.RS_AddOp, arith.AddfOp: x86.RS_FAddOp, arith.MuliOp: x86.RS_ImulOp, arith.MulfOp: x86.RS_FMulOp} module-attribute

ArithConstantToX86 dataclass

Bases: RewritePattern

Source code in xdsl/backend/x86/lowering/convert_arith_to_x86.py
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
@dataclass
class ArithConstantToX86(RewritePattern):
    @op_type_rewrite_pattern
    def match_and_rewrite(self, op: arith.ConstantOp, rewriter: PatternRewriter):
        if not isa(op.value, IntegerAttr):
            raise DiagnosticException(
                "Lowering of arith.constant is only implemented for integers"
            )
        mov_op = x86.DI_MovOp(
            immediate=op.value.value.data, destination=x86.registers.UNALLOCATED_GENERAL
        )
        cast_op, _ = UnrealizedConversionCastOp.cast_one(
            mov_op.destination, op.result.type
        )
        rewriter.replace_op(op, [mov_op, cast_op])

__init__() -> None

match_and_rewrite(op: arith.ConstantOp, rewriter: PatternRewriter)

Source code in xdsl/backend/x86/lowering/convert_arith_to_x86.py
25
26
27
28
29
30
31
32
33
34
35
36
37
@op_type_rewrite_pattern
def match_and_rewrite(self, op: arith.ConstantOp, rewriter: PatternRewriter):
    if not isa(op.value, IntegerAttr):
        raise DiagnosticException(
            "Lowering of arith.constant is only implemented for integers"
        )
    mov_op = x86.DI_MovOp(
        immediate=op.value.value.data, destination=x86.registers.UNALLOCATED_GENERAL
    )
    cast_op, _ = UnrealizedConversionCastOp.cast_one(
        mov_op.destination, op.result.type
    )
    rewriter.replace_op(op, [mov_op, cast_op])

ArithBinaryToX86 dataclass

Bases: RewritePattern

Source code in xdsl/backend/x86/lowering/convert_arith_to_x86.py
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
@dataclass
class ArithBinaryToX86(RewritePattern):
    arch: Arch

    def match_and_rewrite(self, op: Operation, rewriter: PatternRewriter):
        new_type = X86_OP_BY_ARITH_BINARY_OP.get(type(op))  # pyright: ignore
        if new_type is None:
            return

        lhs = op.operands[0]

        if isinstance(lhs.type, builtin.ShapedType):
            raise DiagnosticException(
                f"Lowering of {op.name} not implemented for ShapedType"
            )
        rewriter.name_hint = op.results[0].name_hint

        lhs_x86, rhs_x86 = self.arch.cast_to_regs(op.operands, rewriter)
        moved_rhs = self.arch.move_value_to_unallocated(
            rhs_x86, op.operands[1].type, rewriter
        )
        add_op = new_type(source=lhs_x86, register_in=moved_rhs)
        result_cast_op, _ = UnrealizedConversionCastOp.cast_one(
            add_op.register_out, lhs.type
        )
        rewriter.replace_op(op, [add_op, result_cast_op])

arch: Arch instance-attribute

__init__(arch: Arch) -> None

match_and_rewrite(op: Operation, rewriter: PatternRewriter)

Source code in xdsl/backend/x86/lowering/convert_arith_to_x86.py
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
def match_and_rewrite(self, op: Operation, rewriter: PatternRewriter):
    new_type = X86_OP_BY_ARITH_BINARY_OP.get(type(op))  # pyright: ignore
    if new_type is None:
        return

    lhs = op.operands[0]

    if isinstance(lhs.type, builtin.ShapedType):
        raise DiagnosticException(
            f"Lowering of {op.name} not implemented for ShapedType"
        )
    rewriter.name_hint = op.results[0].name_hint

    lhs_x86, rhs_x86 = self.arch.cast_to_regs(op.operands, rewriter)
    moved_rhs = self.arch.move_value_to_unallocated(
        rhs_x86, op.operands[1].type, rewriter
    )
    add_op = new_type(source=lhs_x86, register_in=moved_rhs)
    result_cast_op, _ = UnrealizedConversionCastOp.cast_one(
        add_op.register_out, lhs.type
    )
    rewriter.replace_op(op, [add_op, result_cast_op])

ConvertArithToX86Pass dataclass

Bases: ModulePass

Source code in xdsl/backend/x86/lowering/convert_arith_to_x86.py
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
@dataclass(frozen=True)
class ConvertArithToX86Pass(ModulePass):
    name = "convert-arith-to-x86"

    def apply(self, ctx: Context, op: builtin.ModuleOp) -> None:
        arch = Arch.arch_for_name(None)
        PatternRewriteWalker(
            GreedyRewritePatternApplier(
                [
                    ArithBinaryToX86(arch),
                    ArithConstantToX86(),
                ],
                dce_enabled=False,
            ),
            apply_recursively=False,
        ).rewrite_module(op)

name = 'convert-arith-to-x86' class-attribute instance-attribute

__init__() -> None

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

Source code in xdsl/backend/x86/lowering/convert_arith_to_x86.py
80
81
82
83
84
85
86
87
88
89
90
91
def apply(self, ctx: Context, op: builtin.ModuleOp) -> None:
    arch = Arch.arch_for_name(None)
    PatternRewriteWalker(
        GreedyRewritePatternApplier(
            [
                ArithBinaryToX86(arch),
                ArithConstantToX86(),
            ],
            dce_enabled=False,
        ),
        apply_recursively=False,
    ).rewrite_module(op)