Skip to content

Op selector

op_selector

OpSelector

Bases: NamedTuple

A specifier of an operation in a module. Useful when a stable reference is needed across copies of a module.

Source code in xdsl/utils/op_selector.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class OpSelector(NamedTuple):
    """
    A specifier of an operation in a module.
    Useful when a stable reference is needed across copies of a module.
    """

    idx: int
    """
    The index of the operation in a module, when walking top to bottom as printed in the
    IR.
    """
    op_name: str
    """
    The name of the expected operation, used to check that the op found is the expected
    one.
    """

    def get_op(self, module_op: ModuleOp):
        """
        Returns the matching op, raising IndexError if the index is out of bounds and
        ValueError if the name does not match.
        """
        op = next(islice(module_op.walk(), self.idx, None), None)
        if op is None:
            raise IndexError(f"Matching index {self.idx} out of range.")
        if op.name != self.op_name:
            raise ValueError(
                f"Unexpected op {op.name} at index {self.idx}, expected {self.op_name}."
            )
        return op

idx: int instance-attribute

The index of the operation in a module, when walking top to bottom as printed in the IR.

op_name: str instance-attribute

The name of the expected operation, used to check that the op found is the expected one.

get_op(module_op: ModuleOp)

Returns the matching op, raising IndexError if the index is out of bounds and ValueError if the name does not match.

Source code in xdsl/utils/op_selector.py
27
28
29
30
31
32
33
34
35
36
37
38
39
def get_op(self, module_op: ModuleOp):
    """
    Returns the matching op, raising IndexError if the index is out of bounds and
    ValueError if the name does not match.
    """
    op = next(islice(module_op.walk(), self.idx, None), None)
    if op is None:
        raise IndexError(f"Matching index {self.idx} out of range.")
    if op.name != self.op_name:
        raise ValueError(
            f"Unexpected op {op.name} at index {self.idx}, expected {self.op_name}."
        )
    return op