Skip to content

Interfaces

interfaces

Interfaces are a convenience wrapper around traits, providing logic on the operation directly.

Operations inherit all the traits of their superclasses, allowing them to combine behaviours via sublcassing. This can be more convenient than adding the traits explicitly.

HasCanonicalizationPatternsInterface dataclass

Bases: Operation, ABC

An operation subclassing this interface must implement the get_canonicalization_patterns method, which returns a tuple of patterns that canonicalize this operation. Wraps CanonicalizationPatternsTrait.

Source code in xdsl/interfaces.py
43
44
45
46
47
48
49
50
51
52
53
54
55
56
class HasCanonicalizationPatternsInterface(Operation, abc.ABC):
    """
    An operation subclassing this interface must implement the
    `get_canonicalization_patterns` method, which returns a tuple of patterns that
    canonicalize this operation.
    Wraps `CanonicalizationPatternsTrait`.
    """

    traits = traits_def(_HasCanonicalizationPatternsInterfaceTrait())

    @classmethod
    @abc.abstractmethod
    def get_canonicalization_patterns(cls) -> tuple[RewritePattern, ...]:
        raise NotImplementedError()

traits = traits_def(_HasCanonicalizationPatternsInterfaceTrait()) class-attribute instance-attribute

get_canonicalization_patterns() -> tuple[RewritePattern, ...] abstractmethod classmethod

Source code in xdsl/interfaces.py
53
54
55
56
@classmethod
@abc.abstractmethod
def get_canonicalization_patterns(cls) -> tuple[RewritePattern, ...]:
    raise NotImplementedError()

ConstantLikeInterface dataclass

Bases: Operation, ABC

An operation subclassing this interface must implement the get_constant_value method, which returns the constant value of this operation. Wraps ConstantLikeTrait.

Source code in xdsl/interfaces.py
74
75
76
77
78
79
80
81
82
83
84
85
class ConstantLikeInterface(Operation, abc.ABC):
    """
    An operation subclassing this interface must implement the
    `get_constant_value` method, which returns the constant value of this operation.
    Wraps `ConstantLikeTrait`.
    """

    traits = traits_def(_ConstantLikeInterfaceTrait())

    @abc.abstractmethod
    def get_constant_value(self) -> Attribute:
        raise NotImplementedError()

traits = traits_def(_ConstantLikeInterfaceTrait()) class-attribute instance-attribute

get_constant_value() -> Attribute abstractmethod

Source code in xdsl/interfaces.py
83
84
85
@abc.abstractmethod
def get_constant_value(self) -> Attribute:
    raise NotImplementedError()

HasFolderInterface dataclass

Bases: Operation, ABC

An operation subclassing this interface must implement the fold method, which attempts to fold the operation. Wraps HasFolderTrait.

Source code in xdsl/interfaces.py
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
class HasFolderInterface(Operation, abc.ABC):
    """
    An operation subclassing this interface must implement the
    `fold` method, which attempts to fold the operation.
    Wraps `HasFolderTrait`.
    """

    traits = traits_def(_HasFolderInterfaceTrait())

    def get_constant(self, operand: SSAValue) -> Attribute | None:
        if (
            isinstance(operand_op := operand.owner, Operation)
            and (t := operand_op.get_trait(ConstantLike)) is not None
        ):
            return t.get_constant_value(operand_op)

    @abc.abstractmethod
    def fold(self) -> Sequence[SSAValue | Attribute] | None:
        """
        Attempts to fold the operation. The fold method cannot modify the IR.
        Returns either an existing SSAValue or an Attribute for each result of the operation.
        When folding is unsuccessful, returns None.

        The fold method is not allowed to mutate the operation being folded.
        """
        raise NotImplementedError()

traits = traits_def(_HasFolderInterfaceTrait()) class-attribute instance-attribute

get_constant(operand: SSAValue) -> Attribute | None

Source code in xdsl/interfaces.py
112
113
114
115
116
117
def get_constant(self, operand: SSAValue) -> Attribute | None:
    if (
        isinstance(operand_op := operand.owner, Operation)
        and (t := operand_op.get_trait(ConstantLike)) is not None
    ):
        return t.get_constant_value(operand_op)

fold() -> Sequence[SSAValue | Attribute] | None abstractmethod

Attempts to fold the operation. The fold method cannot modify the IR. Returns either an existing SSAValue or an Attribute for each result of the operation. When folding is unsuccessful, returns None.

The fold method is not allowed to mutate the operation being folded.

Source code in xdsl/interfaces.py
119
120
121
122
123
124
125
126
127
128
@abc.abstractmethod
def fold(self) -> Sequence[SSAValue | Attribute] | None:
    """
    Attempts to fold the operation. The fold method cannot modify the IR.
    Returns either an existing SSAValue or an Attribute for each result of the operation.
    When folding is unsuccessful, returns None.

    The fold method is not allowed to mutate the operation being folded.
    """
    raise NotImplementedError()