Skip to content

Op inserter

op_inserter

OpInserter dataclass

Class responsible for inserting operations at the right place in the generated IR.

Source code in xdsl/frontend/pyast/utils/op_inserter.py
 7
 8
 9
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
@dataclass
class OpInserter:
    """
    Class responsible for inserting operations at the right place in the
    generated IR.
    """

    insertion_point: Block
    """
    Insertion point, i.e. the pointer to the block to which the operations are
    appended.
    """

    stack: list[SSAValue] = field(default_factory=list[SSAValue])
    """
    Stack to hold the intermediate results of operations. For each new
    operation, its operands will be popped from the stack.
    """

    def get_operand(self) -> SSAValue:
        """
        Pops the last value from the operand stack and returns it.
        """
        if len(self.stack) == 0:
            raise FrontendProgramException(
                "Trying to get an operand from an empty stack."
            )
        return self.stack.pop()

    def insert_op(self, op: Operation) -> None:
        """Inserts a new operation and places its results on the stack."""
        self.insertion_point.add_op(op)
        for result in op.results:
            self.stack.append(result)

    def set_insertion_point_from_op(self, op: Operation) -> None:
        """
        Sets the insertion point to the last block in the last region of the
        operation.
        """
        if not op.regions:
            raise FrontendProgramException(
                f"Trying to set the insertion point for operation '{op.name}' with no regions."
            )
        if (last_block := op.regions[-1].blocks.last) is None:
            raise FrontendProgramException(
                f"Trying to set the insertion point for operation '{op.name}' with no blocks in its last region."
            )
        self.insertion_point = last_block

    def set_insertion_point_from_region(self, region: Region) -> None:
        """Sets the insertion point to the last block in this region."""
        if (last_block := region.blocks.last) is None:
            raise FrontendProgramException(
                "Trying to set the insertion point from the region without blocks."
            )
        self.insertion_point = last_block

    def set_insertion_point_from_block(self, block: Block) -> None:
        """Sets the insertion point to this block."""
        self.insertion_point = block

insertion_point: Block instance-attribute

Insertion point, i.e. the pointer to the block to which the operations are appended.

stack: list[SSAValue] = field(default_factory=(list[SSAValue])) class-attribute instance-attribute

Stack to hold the intermediate results of operations. For each new operation, its operands will be popped from the stack.

__init__(insertion_point: Block, stack: list[SSAValue] = list[SSAValue]()) -> None

get_operand() -> SSAValue

Pops the last value from the operand stack and returns it.

Source code in xdsl/frontend/pyast/utils/op_inserter.py
26
27
28
29
30
31
32
33
34
def get_operand(self) -> SSAValue:
    """
    Pops the last value from the operand stack and returns it.
    """
    if len(self.stack) == 0:
        raise FrontendProgramException(
            "Trying to get an operand from an empty stack."
        )
    return self.stack.pop()

insert_op(op: Operation) -> None

Inserts a new operation and places its results on the stack.

Source code in xdsl/frontend/pyast/utils/op_inserter.py
36
37
38
39
40
def insert_op(self, op: Operation) -> None:
    """Inserts a new operation and places its results on the stack."""
    self.insertion_point.add_op(op)
    for result in op.results:
        self.stack.append(result)

set_insertion_point_from_op(op: Operation) -> None

Sets the insertion point to the last block in the last region of the operation.

Source code in xdsl/frontend/pyast/utils/op_inserter.py
42
43
44
45
46
47
48
49
50
51
52
53
54
55
def set_insertion_point_from_op(self, op: Operation) -> None:
    """
    Sets the insertion point to the last block in the last region of the
    operation.
    """
    if not op.regions:
        raise FrontendProgramException(
            f"Trying to set the insertion point for operation '{op.name}' with no regions."
        )
    if (last_block := op.regions[-1].blocks.last) is None:
        raise FrontendProgramException(
            f"Trying to set the insertion point for operation '{op.name}' with no blocks in its last region."
        )
    self.insertion_point = last_block

set_insertion_point_from_region(region: Region) -> None

Sets the insertion point to the last block in this region.

Source code in xdsl/frontend/pyast/utils/op_inserter.py
57
58
59
60
61
62
63
def set_insertion_point_from_region(self, region: Region) -> None:
    """Sets the insertion point to the last block in this region."""
    if (last_block := region.blocks.last) is None:
        raise FrontendProgramException(
            "Trying to set the insertion point from the region without blocks."
        )
    self.insertion_point = last_block

set_insertion_point_from_block(block: Block) -> None

Sets the insertion point to this block.

Source code in xdsl/frontend/pyast/utils/op_inserter.py
65
66
67
def set_insertion_point_from_block(self, block: Block) -> None:
    """Sets the insertion point to this block."""
    self.insertion_point = block