Skip to content

Stmt

Statement dataclass

Statement(
    *,
    args=(),
    regions=(),
    successors=(),
    attributes={},
    properties={},
    results=(),
    result_types=(),
    args_slice={},
    source=None
)

Bases: IRNode['Block']

Source code in src/kirin/ir/nodes/stmt.py
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
def __init__(
    self,
    *,
    args: Sequence[SSAValue] = (),
    regions: Sequence[Region] = (),
    successors: Sequence[Block] = (),
    attributes: Mapping[str, Attribute] = {},
    properties: Mapping[str, Attribute] = {},
    results: Sequence[ResultValue] = (),
    result_types: Sequence[TypeAttribute] = (),
    args_slice: Mapping[str, int | slice] = {},
    source: SourceInfo | None = None,
) -> None:
    super().__init__()

    self._args = ()
    self._regions = []
    self._name_args_slice = dict(args_slice)
    self.source = source
    self.args = args

    if results:
        self._results = list(results)
        assert (
            len(result_types) == 0
        ), "expect either results or result_types specified, got both"

    if result_types:
        self._results = [
            ResultValue(self, idx, type=type)
            for idx, type in enumerate(result_types)
        ]

    if not results and not result_types:
        self._results = list(results)

    self.successors = list(successors)
    self.properties = dict(properties)
    self.attributes = dict(attributes)
    self.regions = list(regions)

    self.parent = None
    self._next_stmt = None
    self._prev_stmt = None
    self.__post_init__()

detach

detach()

detach the statement from its parent block.

Source code in src/kirin/ir/nodes/stmt.py
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
def detach(self) -> None:
    """detach the statement from its parent block."""
    if self.parent is None:
        return

    parent: Block = self.parent
    prev_stmt = self.prev_stmt
    next_stmt = self.next_stmt

    if prev_stmt is not None:
        prev_stmt._next_stmt = next_stmt
        self._prev_stmt = None
    else:
        assert (
            parent._first_stmt is self
        ), "Invalid statement, has no prev_stmt but not first_stmt"
        parent._first_stmt = next_stmt

    if next_stmt is not None:
        next_stmt._prev_stmt = prev_stmt
        self._next_stmt = None
    else:
        assert (
            parent._last_stmt is self
        ), "Invalid statement, has no next_stmt but not last_stmt"
        parent._last_stmt = prev_stmt

    self.parent = None
    parent._stmt_len -= 1
    return

from_stmt classmethod

from_stmt(
    other,
    args=None,
    regions=None,
    successors=None,
    attributes=None,
)

Create a similar Statement with new ResultValue and without attaching to any parent block. This still references to the old successor and regions.

Source code in src/kirin/ir/nodes/stmt.py
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
@classmethod
def from_stmt(
    cls,
    other: Statement,
    args: Sequence[SSAValue] | None = None,
    regions: list[Region] | None = None,
    successors: list[Block] | None = None,
    attributes: dict[str, Attribute] | None = None,
) -> Self:
    """Create a similar Statement with new `ResultValue` and without
    attaching to any parent block. This still references to the old successor
    and regions.
    """
    obj = cls.__new__(cls)
    Statement.__init__(
        obj,
        args=args or other._args,
        regions=regions or other._regions,
        successors=successors or other.successors,
        attributes=attributes or other.attributes,
        properties=other.properties,  # properties are immutable, thus no need to copy
        result_types=[result.type for result in other._results],
        args_slice=other._name_args_slice,
    )
    return obj