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
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
@classmethod
@abc.abstractmethod
def get_canonicalization_patterns(cls) -> tuple[RewritePattern, ...]:
    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
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())

    @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

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
@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()

ConditionallySpeculatableInterface dataclass

Bases: Operation, ABC

An operation subclassing this interface must implement the is_speculatable method, which indicates whether the operation can be speculatively executed.

Source code in xdsl/interfaces.py
class ConditionallySpeculatableInterface(Operation, abc.ABC):
    """
    An operation subclassing this interface must implement the
    `is_speculatable` method, which indicates whether the operation
    can be speculatively executed.
    """

    traits = traits_def(_ConditionallySpeculatableInterfaceTrait())

    @abc.abstractmethod
    def is_speculatable(self) -> bool:
        """
        Returns True if this operation can be speculatively executed,
        False otherwise. Speculatable ops have no undefined behavior
        and no observable side effects.
        """
        raise NotImplementedError()

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

is_speculatable() -> bool abstractmethod

Returns True if this operation can be speculatively executed, False otherwise. Speculatable ops have no undefined behavior and no observable side effects.

Source code in xdsl/interfaces.py
@abc.abstractmethod
def is_speculatable(self) -> bool:
    """
    Returns True if this operation can be speculatively executed,
    False otherwise. Speculatable ops have no undefined behavior
    and no observable side effects.
    """
    raise NotImplementedError()

RegionBranchOpInterface dataclass

Bases: Operation, ABC

An operation subclassing this interface must implement successor_regions, and may override the other methods, whose semantics are documented on RegionBranchOpTrait. Wraps RegionBranchOpTrait.

Source code in xdsl/interfaces.py
class RegionBranchOpInterface(Operation, abc.ABC):
    """
    An operation subclassing this interface must implement `successor_regions`, and may
    override the other methods, whose semantics are documented on `RegionBranchOpTrait`.
    Wraps `RegionBranchOpTrait`.
    """

    traits = traits_def(_RegionBranchOpInterfaceTrait())

    def entry_successor_regions(
        self, operands: Sequence[Attribute | None]
    ) -> Sequence[RegionSuccessor]:
        """See `RegionBranchOpTrait.entry_successor_regions`."""
        return self.successor_regions(RegionBranchPoint.PARENT)

    @abc.abstractmethod
    def successor_regions(self, point: RegionBranchPoint) -> Sequence[RegionSuccessor]:
        """See `RegionBranchOpTrait.successor_regions`."""
        raise NotImplementedError()

    def successor_regions_from_region(
        self, region: Region
    ) -> Iterator[RegionSuccessor]:
        """See `RegionBranchOpTrait.successor_regions_from_region`."""
        for block in region.blocks:
            terminator = block.last_op
            if terminator is not None and terminator.has_trait(
                RegionBranchTerminatorOpTrait
            ):
                yield from self.successor_regions(RegionBranchPoint(terminator))

    def successor_inputs(self, successor: RegionSuccessor) -> Sequence[SSAValue]:
        """See `RegionBranchOpTrait.successor_inputs`."""
        return ()

    def predecessors(self, successor: RegionSuccessor) -> Iterator[RegionBranchPoint]:
        """See `RegionBranchOpTrait.predecessors`."""
        for point in _RegionBranchOpInterfaceTrait.all_region_branch_points(self):
            if successor in self.successor_regions(point):
                yield point

    def region_invocation_bounds(
        self, operands: Sequence[Attribute | None]
    ) -> Sequence[InvocationBounds]:
        """See `RegionBranchOpTrait.region_invocation_bounds`."""
        return [InvocationBounds.UNKNOWN] * len(self.regions)

    def are_types_compatible(self, lhs: Attribute, rhs: Attribute) -> bool:
        """See `RegionBranchOpTrait.are_types_compatible`."""
        return lhs == rhs

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

entry_successor_regions(operands: Sequence[Attribute | None]) -> Sequence[RegionSuccessor]

See RegionBranchOpTrait.entry_successor_regions.

Source code in xdsl/interfaces.py
def entry_successor_regions(
    self, operands: Sequence[Attribute | None]
) -> Sequence[RegionSuccessor]:
    """See `RegionBranchOpTrait.entry_successor_regions`."""
    return self.successor_regions(RegionBranchPoint.PARENT)

successor_regions(point: RegionBranchPoint) -> Sequence[RegionSuccessor] abstractmethod

See RegionBranchOpTrait.successor_regions.

Source code in xdsl/interfaces.py
@abc.abstractmethod
def successor_regions(self, point: RegionBranchPoint) -> Sequence[RegionSuccessor]:
    """See `RegionBranchOpTrait.successor_regions`."""
    raise NotImplementedError()

successor_regions_from_region(region: Region) -> Iterator[RegionSuccessor]

See RegionBranchOpTrait.successor_regions_from_region.

Source code in xdsl/interfaces.py
def successor_regions_from_region(
    self, region: Region
) -> Iterator[RegionSuccessor]:
    """See `RegionBranchOpTrait.successor_regions_from_region`."""
    for block in region.blocks:
        terminator = block.last_op
        if terminator is not None and terminator.has_trait(
            RegionBranchTerminatorOpTrait
        ):
            yield from self.successor_regions(RegionBranchPoint(terminator))

successor_inputs(successor: RegionSuccessor) -> Sequence[SSAValue]

See RegionBranchOpTrait.successor_inputs.

Source code in xdsl/interfaces.py
def successor_inputs(self, successor: RegionSuccessor) -> Sequence[SSAValue]:
    """See `RegionBranchOpTrait.successor_inputs`."""
    return ()

predecessors(successor: RegionSuccessor) -> Iterator[RegionBranchPoint]

See RegionBranchOpTrait.predecessors.

Source code in xdsl/interfaces.py
def predecessors(self, successor: RegionSuccessor) -> Iterator[RegionBranchPoint]:
    """See `RegionBranchOpTrait.predecessors`."""
    for point in _RegionBranchOpInterfaceTrait.all_region_branch_points(self):
        if successor in self.successor_regions(point):
            yield point

region_invocation_bounds(operands: Sequence[Attribute | None]) -> Sequence[InvocationBounds]

See RegionBranchOpTrait.region_invocation_bounds.

Source code in xdsl/interfaces.py
def region_invocation_bounds(
    self, operands: Sequence[Attribute | None]
) -> Sequence[InvocationBounds]:
    """See `RegionBranchOpTrait.region_invocation_bounds`."""
    return [InvocationBounds.UNKNOWN] * len(self.regions)

are_types_compatible(lhs: Attribute, rhs: Attribute) -> bool

See RegionBranchOpTrait.are_types_compatible.

Source code in xdsl/interfaces.py
def are_types_compatible(self, lhs: Attribute, rhs: Attribute) -> bool:
    """See `RegionBranchOpTrait.are_types_compatible`."""
    return lhs == rhs

RegionBranchTerminatorOpInterface dataclass

Bases: Operation, ABC

An operation subclassing this interface is a terminator that branches between the regions of its parent RegionBranchOpInterface. Wraps RegionBranchTerminatorOpTrait.

Source code in xdsl/interfaces.py
class RegionBranchTerminatorOpInterface(Operation, abc.ABC):
    """
    An operation subclassing this interface is a terminator that branches between the
    regions of its parent `RegionBranchOpInterface`. Wraps
    `RegionBranchTerminatorOpTrait`.
    """

    traits = traits_def(_RegionBranchTerminatorOpInterfaceTrait())

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