From 0562dcf7276c8e83cafa4f2b69f5269739fb1297 Mon Sep 17 00:00:00 2001 From: ChrisG Date: Thu, 2 May 2024 14:27:30 +0100 Subject: [PATCH 1/4] added support for RRI operations --- .../dialects/x86/x86_assembly_emission.mlir | 3 + tests/filecheck/dialects/x86/x86_ops.mlir | 3 + xdsl/dialects/x86/__init__.py | 2 + xdsl/dialects/x86/ops.py | 61 +++++++++++++++++++ 4 files changed, 69 insertions(+) diff --git a/tests/filecheck/dialects/x86/x86_assembly_emission.mlir b/tests/filecheck/dialects/x86/x86_assembly_emission.mlir index 477097eb76..b468b35192 100644 --- a/tests/filecheck/dialects/x86/x86_assembly_emission.mlir +++ b/tests/filecheck/dialects/x86/x86_assembly_emission.mlir @@ -84,3 +84,6 @@ x86.mi.xor %0, 2, 8 : (!x86.reg) -> () // CHECK: xor [rax+8], 2 x86.mi.mov %0, 2, 8 : (!x86.reg) -> () // CHECK: mov [rax+8], 2 + +%rri_imul = x86.rri.imul %1, 2 : (!x86.reg) -> !x86.reg +// CHECK: imul rax, rdx, 2 \ No newline at end of file diff --git a/tests/filecheck/dialects/x86/x86_ops.mlir b/tests/filecheck/dialects/x86/x86_ops.mlir index 444b9fbb3c..5e2373db0d 100644 --- a/tests/filecheck/dialects/x86/x86_ops.mlir +++ b/tests/filecheck/dialects/x86/x86_ops.mlir @@ -85,3 +85,6 @@ x86.mi.xor %0, 2, 8 : (!x86.reg<>) -> () // CHECK-NEXT: x86.mi.xor %{{.*}}, 2, 8 : (!x86.reg<>) -> () x86.mi.mov %0, 2, 8 : (!x86.reg<>) -> () // CHECK-NEXT: x86.mi.mov %{{.*}}, 2, 8 : (!x86.reg<>) -> () + +%rri_imul = x86.rri.imul %1, 2 : (!x86.reg<>) -> !x86.reg<> +// CHECK-NEXT: %{{.*}} = x86.rri.imul %{{.*}}, 2 : (!x86.reg<>) -> !x86.reg<> \ No newline at end of file diff --git a/xdsl/dialects/x86/__init__.py b/xdsl/dialects/x86/__init__.py index 40db8c503d..feaa08078a 100644 --- a/xdsl/dialects/x86/__init__.py +++ b/xdsl/dialects/x86/__init__.py @@ -37,6 +37,7 @@ RR_OrOp, RR_SubOp, RR_XorOp, + RRI_ImulOP, ) from .register import GeneralRegisterType @@ -78,6 +79,7 @@ MI_OrOp, MI_XorOp, MI_MovOp, + RRI_ImulOP, GetRegisterOp, ], [ diff --git a/xdsl/dialects/x86/ops.py b/xdsl/dialects/x86/ops.py index cc781d29bb..dffb76c388 100644 --- a/xdsl/dialects/x86/ops.py +++ b/xdsl/dialects/x86/ops.py @@ -948,6 +948,67 @@ class MI_MovOp(M_MI_Operation[GeneralRegisterType]): name = "x86.mi.mov" +class R_RRI_Operation(Generic[R1InvT, R2InvT], IRDLOperation, X86Instruction, ABC): + """ + A base class for x86 operations that have one destination register, one source register and an immediate value. + """ + + r2 = operand_def(R2InvT) + immediate: AnyIntegerAttr = attr_def(AnyIntegerAttr) + + r1 = result_def(R1InvT) + + def __init__( + self, + r2: Operation | SSAValue, + immediate: int | AnyIntegerAttr, + *, + comment: str | StringAttr | None = None, + r1: R1InvT, + ): + if isinstance(immediate, int): + immediate = IntegerAttr( + immediate, 32 + ) # the deault immediate size is 32 bits + if isinstance(comment, str): + comment = StringAttr(comment) + + super().__init__( + operands=[r2], + attributes={ + "immediate": immediate, + "comment": comment, + }, + result_types=[r1], + ) + + def assembly_line_args(self) -> tuple[AssemblyInstructionArg | None, ...]: + return self.r1, self.r2, self.immediate + + @classmethod + def custom_parse_attributes(cls, parser: Parser) -> dict[str, Attribute]: + attributes = dict[str, Attribute]() + temp = _parse_immediate_value(parser, IntegerType(32, Signedness.SIGNED)) + attributes["immediate"] = temp + return attributes + + def custom_print_attributes(self, printer: Printer) -> Set[str]: + printer.print(", ") + _print_immediate_value(printer, self.immediate) + return {"immediate"} + + +@irdl_op_definition +class RRI_ImulOP(R_RRI_Operation[GeneralRegisterType, GeneralRegisterType]): + """ + Multiplies the immediate value with the source register and stores the result in the destination register. + x[r1] = x[r2] * immediate + https://www.felixcloutier.com/x86/imul + """ + + name = "x86.rri.imul" + + # region Assembly printing def _append_comment(line: str, comment: StringAttr | None) -> str: if comment is None: From fbc014057bfdfc320cd9f62be7ffdb92ea08ebaa Mon Sep 17 00:00:00 2001 From: KGrykiel Date: Thu, 2 May 2024 14:37:25 +0100 Subject: [PATCH 2/4] Update tests/filecheck/dialects/x86/x86_assembly_emission.mlir Co-authored-by: Sasha Lopoukhine --- tests/filecheck/dialects/x86/x86_assembly_emission.mlir | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/filecheck/dialects/x86/x86_assembly_emission.mlir b/tests/filecheck/dialects/x86/x86_assembly_emission.mlir index b468b35192..a65b4c7062 100644 --- a/tests/filecheck/dialects/x86/x86_assembly_emission.mlir +++ b/tests/filecheck/dialects/x86/x86_assembly_emission.mlir @@ -86,4 +86,4 @@ x86.mi.mov %0, 2, 8 : (!x86.reg) -> () // CHECK: mov [rax+8], 2 %rri_imul = x86.rri.imul %1, 2 : (!x86.reg) -> !x86.reg -// CHECK: imul rax, rdx, 2 \ No newline at end of file +// CHECK: imul rax, rdx, 2 From 90ea342c95fa653a993442cbcd17f270c94f5e0b Mon Sep 17 00:00:00 2001 From: KGrykiel Date: Thu, 2 May 2024 14:37:30 +0100 Subject: [PATCH 3/4] Update tests/filecheck/dialects/x86/x86_ops.mlir Co-authored-by: Sasha Lopoukhine --- tests/filecheck/dialects/x86/x86_ops.mlir | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/filecheck/dialects/x86/x86_ops.mlir b/tests/filecheck/dialects/x86/x86_ops.mlir index 5e2373db0d..79b9131e22 100644 --- a/tests/filecheck/dialects/x86/x86_ops.mlir +++ b/tests/filecheck/dialects/x86/x86_ops.mlir @@ -87,4 +87,4 @@ x86.mi.mov %0, 2, 8 : (!x86.reg<>) -> () // CHECK-NEXT: x86.mi.mov %{{.*}}, 2, 8 : (!x86.reg<>) -> () %rri_imul = x86.rri.imul %1, 2 : (!x86.reg<>) -> !x86.reg<> -// CHECK-NEXT: %{{.*}} = x86.rri.imul %{{.*}}, 2 : (!x86.reg<>) -> !x86.reg<> \ No newline at end of file +// CHECK-NEXT: %{{.*}} = x86.rri.imul %{{.*}}, 2 : (!x86.reg<>) -> !x86.reg<> From 03aebb8524a080a16344558da9dbbff9052728ae Mon Sep 17 00:00:00 2001 From: KGrykiel Date: Thu, 2 May 2024 15:14:33 +0100 Subject: [PATCH 4/4] Update xdsl/dialects/x86/ops.py Co-authored-by: Sasha Lopoukhine --- xdsl/dialects/x86/ops.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xdsl/dialects/x86/ops.py b/xdsl/dialects/x86/ops.py index dffb76c388..9fbb2f9047 100644 --- a/xdsl/dialects/x86/ops.py +++ b/xdsl/dialects/x86/ops.py @@ -969,7 +969,7 @@ def __init__( if isinstance(immediate, int): immediate = IntegerAttr( immediate, 32 - ) # the deault immediate size is 32 bits + ) # the default immediate size is 32 bits if isinstance(comment, str): comment = StringAttr(comment)