Skip to content

Warning

This page is under construction. The content may be incomplete or incorrect. Submit an issue on GitHub if you need help or want to contribute.

SCF Dialects

Reference

For krin-statement

For(
    iterable: ir.SSAValue,
    body: ir.Region,
    *initializers: ir.SSAValue
)

Bases: Statement

Source code in src/kirin/dialects/scf/stmts.py
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
def __init__(
    self,
    iterable: ir.SSAValue,
    body: ir.Region,
    *initializers: ir.SSAValue,
):
    stmt = body.blocks[0].last_stmt
    if stmt is None or not isinstance(stmt, Yield):
        raise DialectLoweringError("for loop body must terminate with a yield")

    if len(body.blocks) != 1:
        raise DialectLoweringError("for loop body must have a single block")

    if len(body.blocks[0].args) != len(initializers) + 1:
        raise DialectLoweringError(
            "for loop body must have arguments for all initializers and the loop variable"
        )

    super().__init__(
        args=(iterable, *initializers),
        regions=(body,),
        result_types=tuple(value.type for value in stmt.values),
        args_slice={"iterable": 0, "initializers": slice(1, None)},
    )

body kirin-region kw-only

body: Region = region(multi=False)

initializers kirin-argument

initializers: tuple[SSAValue, ...] = argument(Any)

iterable kirin-argument

iterable: SSAValue = argument(Any)

name class-attribute instance-attribute

name = 'for'

print_impl

print_impl(printer: Printer) -> None
Source code in src/kirin/dialects/scf/stmts.py
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
def print_impl(self, printer: Printer) -> None:
    printer.print_name(self)
    printer.plain_print(" ")
    block = self.body.blocks[0]
    printer.print(block.args[0])
    printer.plain_print(" in ", style="keyword")
    printer.print(self.iterable)
    with printer.indent():
        printer.print_newline()
        printer.plain_print("iter_args(")
        for idx, (arg, val) in enumerate(zip(block.args[1:], self.initializers)):
            printer.print(arg)
            printer.plain_print(" = ")
            printer.print(val)
            if idx < len(self.initializers) - 1:
                printer.plain_print(", ")
        printer.plain_print(") {")

        with printer.align(printer.result_width(block.stmts)):
            for stmt in block.stmts:
                printer.print_newline()
                printer.print_stmt(stmt)
    printer.print_newline()
    printer.plain_print("}")

verify

verify() -> None

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

Source code in src/kirin/dialects/scf/stmts.py
120
121
122
123
124
125
126
127
128
129
130
131
132
133
def verify(self) -> None:
    stmt = self.body.blocks[0].last_stmt
    if stmt is None or not isinstance(stmt, Yield):
        raise VerificationError(self, "for loop body must terminate with a yield")
    if len(stmt.values) != len(self.initializers):
        raise VerificationError(
            self,
            "for loop body must have the same number of results as initializers",
        )
    if len(self.results) != len(stmt.values):
        raise VerificationError(
            self,
            "for loop must have the same number of results as the yield in the body",
        )

IfElse krin-statement

IfElse(
    cond: ir.SSAValue,
    then_body: ir.Region | ir.Block,
    else_body: ir.Region | ir.Block | None = None,
)

Bases: Statement

Python-like if-else statement.

This statement has a condition, then body, and else body.

Then body either terminates with a yield statement or scf.return.

Source code in src/kirin/dialects/scf/stmts.py
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
68
69
70
71
72
73
74
75
def __init__(
    self,
    cond: ir.SSAValue,
    then_body: ir.Region | ir.Block,
    else_body: ir.Region | ir.Block | None = None,
):
    if isinstance(then_body, ir.Region):
        if len(then_body.blocks) != 1:
            raise DialectLoweringError(
                "if-else statement must have a single block in the then region"
            )
        then_body_region = then_body
        then_body = then_body_region.blocks[0]
    elif isinstance(then_body, ir.Block):
        then_body_region = ir.Region(then_body)

    if isinstance(else_body, ir.Region):
        if not else_body.blocks:  # empty region
            else_body_region = else_body
            else_body = None
        elif len(else_body.blocks) != 1:
            raise DialectLoweringError(
                "if-else statement must have a single block in the else region"
            )
        else:
            else_body_region = else_body
            else_body = else_body_region.blocks[0]
    elif isinstance(else_body, ir.Block):
        else_body_region = ir.Region(else_body)
    else:
        else_body_region = ir.Region()

    # if either then or else body has yield, we generate results
    # we assume if both have yields, they have the same number of results
    then_yield = then_body.last_stmt
    else_yield = else_body.last_stmt if else_body is not None else None
    if then_yield is not None and isinstance(then_yield, Yield):
        results = then_yield.values
    elif else_yield is not None and isinstance(else_yield, Yield):
        results = else_yield.values
    else:
        results = ()

    result_types = tuple(value.type for value in results)
    super().__init__(
        args=(cond,),
        regions=(then_body_region, else_body_region),
        result_types=result_types,
        args_slice={"cond": 0},
    )

cond kirin-argument

cond: SSAValue = argument(Any)

else_body kirin-region kw-only

else_body: Region = region(
    multi=False, default_factory=Region
)

name class-attribute instance-attribute

name = 'if'

then_body kirin-region kw-only

then_body: Region = region(multi=False)

print_impl

print_impl(printer: Printer) -> None
Source code in src/kirin/dialects/scf/stmts.py
77
78
79
80
81
82
83
84
85
def print_impl(self, printer: Printer) -> None:
    printer.print_name(self)
    printer.plain_print(" ")
    printer.print(self.cond)
    printer.plain_print(" ")
    printer.print(self.then_body)
    if self.else_body.blocks:
        printer.plain_print(" else ", style="keyword")
        printer.print(self.else_body)

Yield krin-statement

Yield(*values: ir.SSAValue)

Bases: Statement

Source code in src/kirin/dialects/scf/stmts.py
167
168
def __init__(self, *values: ir.SSAValue):
    super().__init__(args=values, args_slice={"values": slice(None)})

name class-attribute instance-attribute

name = 'yield'

traits class-attribute instance-attribute

traits = frozenset({IsTerminator()})

values kirin-argument

values: tuple[SSAValue, ...] = argument(Any)

print_impl

print_impl(printer: Printer) -> None
Source code in src/kirin/dialects/scf/stmts.py
170
171
172
def print_impl(self, printer: Printer) -> None:
    printer.print_name(self)
    printer.print_seq(self.values, prefix=" ", delim=", ")