Skip to content

Base

IRNode dataclass

IRNode()

Bases: Generic[ParentType], ABC, Printable

Base class for all IR nodes. All IR nodes are hashable and can be compared for equality. The hash of an IR node is the same as the id of the object.

Pretty Printing

This object is pretty printable via .print() method.

parent_node abstractmethod property writable

parent_node: ParentType | None

Parent node of the current node.

attach

attach(parent: ParentType) -> None

Attach the current node to the parent node.

Source code in src/kirin/ir/nodes/base.py
80
81
82
83
84
85
86
87
88
def attach(self, parent: ParentType) -> None:
    """Attach the current node to the parent node."""
    assert isinstance(parent, IRNode), f"Expected IRNode, got {type(parent)}"

    if self.parent_node:
        raise ValueError("Node already has a parent")
    if self.is_ancestor(parent):
        raise ValueError("Node is an ancestor of the parent")
    self.parent_node = parent

delete abstractmethod

delete(safe: bool = True) -> None

Delete the current node.

Parameters:

Name Type Description Default
safe bool

If True, check if the node has any references before deleting.

True
Source code in src/kirin/ir/nodes/base.py
100
101
102
103
104
105
106
107
@abstractmethod
def delete(self, safe: bool = True) -> None:
    """Delete the current node.

    Args:
        safe: If True, check if the node has any references before deleting.
    """
    ...

detach abstractmethod

detach() -> None

Detach the current node from the parent node.

Source code in src/kirin/ir/nodes/base.py
90
91
92
93
@abstractmethod
def detach(self) -> None:
    """Detach the current node from the parent node."""
    ...

drop_all_references abstractmethod

drop_all_references() -> None

Drop all references to other nodes.

Source code in src/kirin/ir/nodes/base.py
95
96
97
98
@abstractmethod
def drop_all_references(self) -> None:
    """Drop all references to other nodes."""
    ...

get_root

get_root() -> IRNode

Get the root node of the current node.

Source code in src/kirin/ir/nodes/base.py
55
56
57
58
59
def get_root(self) -> IRNode:
    """Get the root node of the current node."""
    if (parent := self.parent_node) is None:
        return self
    return parent.get_root()

is_ancestor

is_ancestor(op: IRNode) -> bool

Check if the given node is an ancestor of the current node.

Source code in src/kirin/ir/nodes/base.py
47
48
49
50
51
52
53
def is_ancestor(self, op: IRNode) -> bool:
    """Check if the given node is an ancestor of the current node."""
    if op is self:
        return True
    if (parent := op.parent_node) is None:
        return False
    return self.is_ancestor(parent)

is_equal

is_equal(other: IRNode, context: dict = {}) -> bool

Check if the current node is equal to the other node.

Parameters:

Name Type Description Default
other IRNode

The other node to compare.

required
context dict

The context to store the visited nodes. Defaults to {}.

{}

Returns:

Type Description
bool

True if the nodes are equal, False otherwise.

Note

This method is not the same as the == operator. It checks for structural equality rather than identity. To change the behavior of structural equality, override the is_structurally_equal method.

Source code in src/kirin/ir/nodes/base.py
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
def is_equal(self, other: IRNode, context: dict = {}) -> bool:
    """Check if the current node is equal to the other node.

    Args:
        other: The other node to compare.
        context: The context to store the visited nodes. Defaults to {}.

    Returns:
        True if the nodes are equal, False otherwise.

    !!! note
        This method is not the same as the `==` operator. It checks for
        structural equality rather than identity. To change the behavior
        of structural equality, override the `is_structurally_equal` method.
    """
    if not isinstance(other, type(self)):
        return False
    return self.is_structurally_equal(other, context)

is_structurally_equal abstractmethod

is_structurally_equal(
    other: Self,
    context: (
        dict[IRNode | SSAValue, IRNode | SSAValue] | None
    ) = None,
) -> bool

Check if the current node is structurally equal to the other node.

Note

This method is for tweaking the behavior of structural equality. To check if two nodes are structurally equal, use the is_equal method.

Parameters:

Name Type Description Default
other Self

The other node to compare.

required
context dict[IRNode | SSAValue, IRNode | SSAValue] | None

The context to store the visited nodes.

None

Returns:

Type Description
bool

True if the nodes are structurally equal, False otherwise.

Source code in src/kirin/ir/nodes/base.py
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
@abstractmethod
def is_structurally_equal(
    self,
    other: Self,
    context: dict[IRNode | SSAValue, IRNode | SSAValue] | None = None,
) -> bool:
    """Check if the current node is structurally equal to the other node.

    !!! note
        This method is for tweaking the behavior of structural equality.
        To check if two nodes are structurally equal, use the `is_equal` method.

    Args:
        other: The other node to compare.
        context: The context to store the visited nodes.

    Returns:
        True if the nodes are structurally equal, False otherwise.
    """
    ...

verify abstractmethod

verify() -> None

run mandatory validation checks. This is not same as verify_type, which may be optional.

Source code in src/kirin/ir/nodes/base.py
144
145
146
147
@abstractmethod
def verify(self) -> None:
    """run mandatory validation checks. This is not same as verify_type, which may be optional."""
    ...

verify_type abstractmethod

verify_type() -> None

verify the type of the node.

Source code in src/kirin/ir/nodes/base.py
149
150
151
152
@abstractmethod
def verify_type(self) -> None:
    """verify the type of the node."""
    ...