Skip to content

Commit

Permalink
core: Refactor irdl_op_definition (#2575)
Browse files Browse the repository at this point in the history
Refactor get_accessors_from_op_def out of irdl_op_definition, towards
allowing us to define new Python classes with only an `OpDef`, rather
than an `irdl_op_definition`

Co-authored-by: Emilien Bauer <papychacal@gmail.com>
  • Loading branch information
math-fehr and PapyChacal authored May 13, 2024
1 parent 7c62a61 commit d09b184
Showing 1 changed file with 27 additions and 16 deletions.
43 changes: 27 additions & 16 deletions xdsl/irdl/irdl.py
Original file line number Diff line number Diff line change
Expand Up @@ -2121,14 +2121,10 @@ def fun(self: Any, idx: int = arg_idx, previous_vars: int = previous_variadics):
)


def irdl_op_definition(cls: type[IRDLOperationInvT]) -> type[IRDLOperationInvT]:
"""Decorator used on classes to define a new operation definition."""

assert issubclass(
cls, IRDLOperation
), f"class {cls.__name__} should be a subclass of IRDLOperation"

op_def = OpDef.from_pyrdl(cls)
def get_accessors_from_op_def(
op_def: OpDef, custom_verify: Any | None
) -> dict[str, Any]:
"""Get python accessors from an operation definition."""
new_attrs = dict[str, Any]()

# Add operand access fields
Expand Down Expand Up @@ -2215,14 +2211,6 @@ def get_irdl_definition(cls: type[IRDLOperationInvT]):

new_attrs["get_irdl_definition"] = get_irdl_definition

custom_verify = getattr(cls, "verify_")

def verify_(self: IRDLOperation):
op_def.verify(self)
custom_verify(self)

new_attrs["verify_"] = verify_

if op_def.assembly_format is not None:
from xdsl.irdl.declarative_assembly_format import FormatProgram

Expand All @@ -2245,6 +2233,29 @@ def print_with_format(self: IRDLOperation, printer: Printer):
new_attrs["parse"] = parse_with_format
new_attrs["print"] = print_with_format

if custom_verify is not None:

def verify_(self: IRDLOperation):
op_def.verify(self)
custom_verify(self)

new_attrs["verify_"] = verify_
else:
new_attrs["verify_"] = op_def.verify

return new_attrs


def irdl_op_definition(cls: type[IRDLOperationInvT]) -> type[IRDLOperationInvT]:
"""Decorator used on classes to define a new operation definition."""

assert issubclass(
cls, IRDLOperation
), f"class {cls.__name__} should be a subclass of IRDLOperation"

op_def = OpDef.from_pyrdl(cls)
new_attrs = get_accessors_from_op_def(op_def, getattr(cls, "verify_", None))

return type.__new__(
type(cls), cls.__name__, cls.__mro__, {**cls.__dict__, **new_attrs}
)
Expand Down

0 comments on commit d09b184

Please sign in to comment.