Skip to content

Hashable module

hashable_module

HashableModule dataclass

Bases: Hashable

A class wrapping a ModuleOp, that forwards to structural equality checking for ==. The module in this class should not be mutated.

Source code in xdsl/utils/hashable_module.py
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
@dataclass(frozen=True)
class HashableModule(Hashable):
    """
    A class wrapping a ModuleOp, that forwards to structural equality checking for `==`.
    The module in this class should not be mutated.
    """

    module: ModuleOp

    def __eq__(self, other: object) -> bool:
        return isinstance(
            other, HashableModule
        ) and self.module.is_structurally_equivalent(other.module)

    def __hash__(self) -> int:
        """
        The hash of the module is a hash of the ordered combination of operation names.
        As most transformations on IR modify at least one operation, this should be
        enough to minimise collisions.
        """
        hasher = Hasher()
        for op in self.module.walk():
            hasher.combine(op.name)
        return hasher.hash

module: ModuleOp instance-attribute

__init__(module: ModuleOp) -> None

__eq__(other: object) -> bool

Source code in xdsl/utils/hashable_module.py
17
18
19
20
def __eq__(self, other: object) -> bool:
    return isinstance(
        other, HashableModule
    ) and self.module.is_structurally_equivalent(other.module)

__hash__() -> int

The hash of the module is a hash of the ordered combination of operation names. As most transformations on IR modify at least one operation, this should be enough to minimise collisions.

Source code in xdsl/utils/hashable_module.py
22
23
24
25
26
27
28
29
30
31
def __hash__(self) -> int:
    """
    The hash of the module is a hash of the ordered combination of operation names.
    As most transformations on IR modify at least one operation, this should be
    enough to minimise collisions.
    """
    hasher = Hasher()
    for op in self.module.walk():
        hasher.combine(op.name)
    return hasher.hash