Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dialects: (x86) PR11 - RMI Operations #2529

Merged
merged 1 commit into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions tests/filecheck/dialects/x86/x86_assembly_emission.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,8 @@ x86.mi.mov %0, 2, 8 : (!x86.reg<rax>) -> ()

%rri_imul = x86.rri.imul %1, 2 : (!x86.reg<rdx>) -> !x86.reg<rax>
// CHECK: imul rax, rdx, 2

%rmi_imul_no_offset = x86.rmi.imul %1, 2 : (!x86.reg<rdx>) -> !x86.reg<rax>
// CHECK: imul rax, [rdx], 2
%rmi_imul = x86.rmi.imul %1, 2, 8 : (!x86.reg<rdx>) -> !x86.reg<rax>
// CHECK: imul rax, [rdx+8], 2
5 changes: 5 additions & 0 deletions tests/filecheck/dialects/x86/x86_ops.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,8 @@ x86.mi.mov %0, 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<>

%rmi_imul_no_offset = x86.rmi.imul %1, 2 : (!x86.reg<>) -> !x86.reg<>
// CHECK-NEXT: %{{.*}} = x86.rmi.imul %{{.*}}, 2 : (!x86.reg<>) -> !x86.reg<>
%rmi_imul = x86.rmi.imul %1, 2, 8 : (!x86.reg<>) -> !x86.reg<>
// CHECK-NEXT: %{{.*}} = x86.rmi.imul %{{.*}}, 2, 8 : (!x86.reg<>) -> !x86.reg<>
2 changes: 2 additions & 0 deletions xdsl/dialects/x86/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
RM_OrOp,
RM_SubOp,
RM_XorOp,
RMI_ImulOp,
RR_AddOp,
RR_AndOp,
RR_ImulOp,
Expand Down Expand Up @@ -80,6 +81,7 @@
MI_XorOp,
MI_MovOp,
RRI_ImulOP,
RMI_ImulOp,
GetRegisterOp,
],
[
Expand Down
86 changes: 86 additions & 0 deletions xdsl/dialects/x86/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,92 @@ class RRI_ImulOP(R_RRI_Operation[GeneralRegisterType, GeneralRegisterType]):
name = "x86.rri.imul"


class R_RMI_Operation(Generic[R1InvT, R2InvT], IRDLOperation, X86Instruction, ABC):
"""
A base class for x86 operations that have one source register, one memory reference and an immediate value.
"""

r2 = operand_def(R2InvT)
immediate: AnyIntegerAttr = attr_def(AnyIntegerAttr)
offset: AnyIntegerAttr | None = opt_attr_def(AnyIntegerAttr)

r1 = result_def(R1InvT)

def __init__(
self,
r2: Operation | SSAValue,
immediate: int | AnyIntegerAttr,
offset: int | AnyIntegerAttr | None,
*,
comment: str | StringAttr | None = None,
r1: R1InvT,
):
if isinstance(immediate, int):
immediate = IntegerAttr(
immediate, 32
) # the default immediate size is 32 bits
if isinstance(offset, int):
offset = IntegerAttr(offset, 64)
if isinstance(comment, str):
comment = StringAttr(comment)

super().__init__(
operands=[r2],
attributes={
"immediate": immediate,
"offset": offset,
"comment": comment,
},
result_types=[r1],
)

def assembly_line_args(self) -> tuple[AssemblyInstructionArg | None, ...]:
destination = _assembly_arg_str(self.r1)
source = _assembly_arg_str(self.r2)
immediate = _assembly_arg_str(self.immediate)
if self.offset is not None:
offset = _assembly_arg_str(self.offset)
if self.offset.value.data > 0:
source = f"[{source}+{offset}]"
else:
source = f"[{source}{offset}]"
else:
source = f"[{source}]"
return destination, source, immediate

@classmethod
def custom_parse_attributes(cls, parser: Parser) -> dict[str, Attribute]:
attributes = dict[str, Attribute]()
temp = _parse_immediate_value(parser, IntegerType(64, Signedness.SIGNED))
attributes["immediate"] = temp
if parser.parse_optional_punctuation(",") is not None:
temp2 = _parse_optional_immediate_value(
parser, IntegerType(32, Signedness.SIGNED)
)
if temp2 is not None:
attributes["offset"] = temp2
return attributes

def custom_print_attributes(self, printer: Printer) -> Set[str]:
printer.print(", ")
_print_immediate_value(printer, self.immediate)
if self.offset is not None:
printer.print(", ")
_print_immediate_value(printer, self.offset)
return {"immediate", "offset"}


@irdl_op_definition
class RMI_ImulOp(R_RMI_Operation[GeneralRegisterType, GeneralRegisterType]):
"""
Multiplies the immediate value with the memory location pointed to by r2 and stores the result in r1.
x[r1] = [x[r2]] * immediate
https://www.felixcloutier.com/x86/imul
"""

name = "x86.rmi.imul"


# region Assembly printing
def _append_comment(line: str, comment: StringAttr | None) -> str:
if comment is None:
Expand Down
Loading