Skip to content

Index

statement

statement(
    cls=None, **kwargs: Unpack[StatementOptions]
) -> Callable[[type[StmtType]], type[StmtType]]

Declare a new statement class.

This decorator is used to declare a new statement class. It is used to generate the necessary boilerplate code for the class. The class should inherit from kirin.ir.Statement.

Parameters:

Name Type Description Default
init(bool)

Whether to generate an __init__ method.

required
repr(bool)

Whether to generate a __repr__ method.

required
kw_only(bool)

Whether to use keyword-only arguments in the __init__ method.

required
dialect(Optional[Dialect])

The dialect of the statement.

required
property(bool)

Whether to generate property methods for attributes.

required
Example

The following is an example of how to use the statement decorator.

@statement
class MyStatement(ir.Statement):
    name = "some_name"
    traits = frozenset({TraitA(), TraitB()})
    some_input: ir.SSAValue = info.argument()
    some_output: ir.ResultValue = info.result()
    body: ir.Region = info.region()
    successor: ir.Block = info.block()

If the name field is not specified, a lowercase name field will be auto generated.

In addition, one can optionally register the statement to a dialect by providing the dialect argument to the decorator.

The following example register the statement to a dialect my_dialect_object, and name = "myfoo" field is autogenerated

@statement(dialect=my_dialect_object)
class MyFoo(ir.Statement):
    traits = frozenset({ir.FromPythonCall()})
    value: str = info.attribute()
Source code in src/kirin/decl/__init__.py
 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
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
@dataclass_transform(
    field_specifiers=(
        info.attribute,
        info.argument,
        info.region,
        info.result,
        info.block,
    )
)
def statement(
    cls=None,
    **kwargs: Unpack[StatementOptions],
) -> Callable[[type[StmtType]], type[StmtType]]:
    """Declare a new statement class.

    This decorator is used to declare a new statement class. It is used to
    generate the necessary boilerplate code for the class. The class should
    inherit from `kirin.ir.Statement`.

    Args:
        init(bool): Whether to generate an `__init__` method.
        repr(bool): Whether to generate a `__repr__` method.
        kw_only(bool): Whether to use keyword-only arguments in the `__init__`
            method.
        dialect(Optional[Dialect]): The dialect of the statement.
        property(bool): Whether to generate property methods for attributes.

    Example:
        The following is an example of how to use the `statement` decorator.

        ```python
        @statement
        class MyStatement(ir.Statement):
            name = "some_name"
            traits = frozenset({TraitA(), TraitB()})
            some_input: ir.SSAValue = info.argument()
            some_output: ir.ResultValue = info.result()
            body: ir.Region = info.region()
            successor: ir.Block = info.block()
        ```

        If the `name` field is not specified, a lowercase name field will be auto generated.

        In addition, one can optionally register the statement to a dialect
        by providing the `dialect` argument to the decorator.

        The following example register the statement to a dialect `my_dialect_object`, and `name = "myfoo"` field is autogenerated

        ```python
        @statement(dialect=my_dialect_object)
        class MyFoo(ir.Statement):
            traits = frozenset({ir.FromPythonCall()})
            value: str = info.attribute()
        ```
    """

    def wrap(cls):
        decl = StatementDecl(cls, **kwargs)
        decl.scan_fields()
        decl.verify()
        decl.emit()
        decl.register()
        return cls

    if cls is None:
        return wrap
    return wrap(cls)