Skip to content

kirin.ir.nodes.stmt.Statement

← Module overview

classStatementsource

kirin.ir.nodes.stmt.Statement

Bases: IRNode['Block']

The Statement is an instruction in the IR

Signature
class Statement(*, args: Sequence[SSAValue] = (), regions: Sequence[Region] = (), successors: Sequence[Block] = (), attributes: Mapping[str, Attribute] = {}, results: Sequence[ResultValue] = (), result_types: Sequence[TypeAttribute] = (), args_slice: Mapping[str, int | slice] = {}, source: SourceInfo | None = None)

!!! note “Pretty Printing” This object is pretty printable via .print() method.

Parameters

NameTypeDefaultDescription
argsSequence[SSAValue]()
regionsSequence[Region]()
successorsSequence[Block]()
attributesMapping[str, Attribute]{}
resultsSequence[ResultValue]()
result_typesSequence[TypeAttribute]()
args_sliceMapping[str, int | slice]{}
sourceSourceInfo | NoneNone

Attributes

NameTypeDefaultDescription
IS_STATEMENTboolTrue
namestrrequired
dialectDialect | Nonefield(default=None, init=False, repr=False)
traitsfrozenset[Trait['Statement']]frozenset()
successorslist[Block]list(successors)
attributesdict[str, Attribute]dict(attributes)
parentBlock | NoneNone
sourceSourceInfo | NonesourceThe source information of the Statement for debugging/stacktracing.
AttributeType—TypeVar('AttributeType', bound=Attribute)
TraitType—TypeVar('TraitType', bound=(Trait['Statement']))

propertyparent_stmtsource

kirin.ir.nodes.stmt.Statement.parent_stmt

parent_stmt: Statement | None

Get the parent statement.

Returns

Statement | NoneThe parent statement.

source

propertyparent_nodesource

kirin.ir.nodes.stmt.Statement.parent_node

parent_node: Block | None

Get the parent node.

Returns

Block | NoneThe parent node.

source

propertyparent_regionsource

kirin.ir.nodes.stmt.Statement.parent_region

parent_region: Region | None

Get the parent Region. Returns: Region | None: The parent Region.

source

propertyparent_blocksource

kirin.ir.nodes.stmt.Statement.parent_block

parent_block: Block | None

Get the parent Block.

Returns

Block | NoneThe parent Block.

source

propertynext_stmtsource

kirin.ir.nodes.stmt.Statement.next_stmt

next_stmt: Statement | None

Get the next statement.

source

propertyprev_stmtsource

kirin.ir.nodes.stmt.Statement.prev_stmt

prev_stmt: Statement | None

Get the previous statement.

source

methodinsert_aftersource

kirin.ir.nodes.stmt.Statement.insert_after

def insert_after(stmt: Statement) -> None

Insert the current Statement after the input Statement.

Parameters

NameTypeDescription
stmtStatementInput Statement.

Example

The following example demonstrates how to insert a Statement after another Statement. After insert_after is called, stmt1 will be inserted after stmt2, which appears in IR in the order (stmt2 -> stmt1)

stmt1 = Statement()
stmt2 = Statement()
stmt1.insert_after(stmt2)
source

methodinsert_beforesource

kirin.ir.nodes.stmt.Statement.insert_before

def insert_before(stmt: Statement) -> None

Insert the current Statement before the input Statement.

Parameters

NameTypeDescription
stmtStatementInput Statement.

Example

The following example demonstrates how to insert a Statement before another Statement. After insert_before is called, stmt1 will be inserted before stmt2, which appears in IR in the order (stmt1 -> stmt2)

stmt1 = Statement()
stmt2 = Statement()
stmt1.insert_before(stmt2)
source

methodreplace_bysource

kirin.ir.nodes.stmt.Statement.replace_by

def replace_by(stmt: Statement) -> None

Replace the current Statement by the input Statement.

Parameters

NameTypeDescription
stmtStatementInput Statement.
source

propertyargssource

kirin.ir.nodes.stmt.Statement.args

args: ArgumentList

Get the arguments of the Statement.

Returns

ArgumentListArgumentList: The arguments View of the Statement.

source

propertyresultssource

kirin.ir.nodes.stmt.Statement.results

results: ResultList

Get the result values of the Statement.

Returns

ResultListResultList: The result values View of the Statement.

source

propertyregionssource

kirin.ir.nodes.stmt.Statement.regions

regions: list[Region]

Get a list of regions of the Statement.

Returns

list[Region]The list of regions of the Statement.

source

methoddrop_all_referencessource

kirin.ir.nodes.stmt.Statement.drop_all_references

def drop_all_references() -> None

Remove all the dependency that reference/uses this Statement.

source

methoddeletesource

kirin.ir.nodes.stmt.Statement.delete

def delete(safe: bool = True) -> None

Delete the Statement completely from the IR graph.

Parameters

NameTypeDefaultDescription
safeboolTrueIf True, raise error if there is anything that still reference components in the Statement. Defaults to True.

Note

This method will detach + remove references of the Statement.

source

methoddetachsource

kirin.ir.nodes.stmt.Statement.detach

def detach() -> None

detach the statement from its parent block.

source

methodfrom_stmtsource

kirin.ir.nodes.stmt.Statement.from_stmt

Signature
def from_stmt(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.

Parameters

NameTypeDefaultDescription
otherStatementrequired
argsSequence[SSAValue] | NoneNone
regionslist[Region] | NoneNone
successorslist[Block] | NoneNone
attributesdict[str, Attribute] | NoneNone

Returns

Self

source

methodwalksource

kirin.ir.nodes.stmt.Statement.walk

Signature
def walk(*, reverse: bool = False, region_first: bool = False, include_self: bool = True) -> Iterator[Statement]

Traversal the Statements of Regions.

Parameters

NameTypeDefaultDescription
reverseboolFalseIf walk in the reversed manner. Defaults to False.
region_firstboolFalseIf the walk should go through the Statement first or the Region of a Statement first. Defaults to False.
include_selfboolTrueIf the walk should include the Statement itself. Defaults to True.

Returns

Iterator[Statement]

Yields

Statement — Iterator[Statement]: An iterator that yield Statements of Blocks in the Region, in the specified order.

source

methodis_structurally_equalsource

kirin.ir.nodes.stmt.Statement.is_structurally_equal

Signature
def is_structurally_equal(other: IRNode, context: dict[IRNode | SSAValue, IRNode | SSAValue] | None = None) -> bool

Check if the Statement is structurally equal to another Statement.

Parameters

NameTypeDefaultDescription
otherIRNoderequiredThe other node to compare with.
contextdict[IRNode | SSAValue, IRNode | SSAValue] | NoneNoneA map of IRNode/SSAValue to hint that they are equivalent so the check will treat them as equivalent. Defaults to None.

Returns

boolbool: True if the IRNode is structurally equal to the other.

source

methodprint_implsource

kirin.ir.nodes.stmt.Statement.print_impl

def print_impl(printer: Printer) -> None

Parameters

NameTypeDescription
printerPrinter
source

methodget_attributesource

kirin.ir.nodes.stmt.Statement.get_attribute

Signature
def get_attribute(key: str, default: Attribute | None = None) -> Attribute | None

Get the attribute or property of the Statement.

Parameters

NameTypeDefaultDescription
keystrrequiredThe key of the attribute or property.
defaultAttribute | NoneNone

Returns

Attribute | NoneThe attribute or property of the Statement.

source

methodget_attribute_castedsource

kirin.ir.nodes.stmt.Statement.get_attribute_casted

Signature
def get_attribute_casted(key: str, expect: type[AttributeType], default: AttributeType | None = None) -> AttributeType | None

Get the attribute or property of the Statement.

Parameters

NameTypeDefaultDescription
keystrrequiredThe key of the attribute or property.
expecttype[AttributeType]requiredThe expected type of the attribute.
defaultAttributeType | NoneNoneThe default value to return if the attribute is not found. Defaults to None.

Returns

AttributeType | NoneAttributeType: The attribute or property of the Statement.

source

methodget_attribute_typedsource

kirin.ir.nodes.stmt.Statement.get_attribute_typed

Signature
def get_attribute_typed(key: str, expect: type[AttributeType], default: AttributeType | None = None) -> AttributeType | None

Get the attribute or property of the Statement.

Parameters

NameTypeDefaultDescription
keystrrequiredThe key of the attribute or property.
expecttype[AttributeType]requiredThe expected type of the attribute.
defaultAttributeType | NoneNoneThe default value to return if the attribute is not found. Defaults to None.

Returns

AttributeType | NoneAttributeType: The attribute or property of the Statement.

source

methodhas_traitsource

kirin.ir.nodes.stmt.Statement.has_trait

def has_trait(trait_type: type[Trait['Statement']]) -> bool

Check if the Statement has a specific trait.

Parameters

NameTypeDescription
trait_typetype[Trait['Statement']]The type of trait to check for.

Returns

boolbool: True if the class has the specified trait, False otherwise.

source

methodget_traitsource

kirin.ir.nodes.stmt.Statement.get_trait

def get_trait(trait: type[TraitType]) -> TraitType | None

Get the trait of the Statement.

Parameters

NameTypeDescription
traittype[TraitType]

Returns

TraitType | None

source

methodget_present_traitsource

kirin.ir.nodes.stmt.Statement.get_present_trait

def get_present_trait(trait: type[TraitType]) -> TraitType

Just like get_trait, but expects the trait to be there. Useful for linter checks, when you know the trait is present.

Parameters

NameTypeDescription
traittype[TraitType]

Returns

TraitType

source

methodexpect_one_resultsource

kirin.ir.nodes.stmt.Statement.expect_one_result

def expect_one_result() -> ResultValue

Check if the statement contain only one result, and return it

Returns

ResultValue

source

methodcheck_typesource

kirin.ir.nodes.stmt.Statement.check_type

def check_type() -> None

Check the types of the Block. Raises Exception if the types are not correct. This method is called by the verify_type method, which will detect the source of the error in the IR. One should always call the verify_type method to verify the types of the IR.

Note

This method is generated by the @statement decorator. But can be overridden if needed.

source

methodchecksource

kirin.ir.nodes.stmt.Statement.check

def check() -> None

Check the statement. Raises Exception if the statement is not correct. This method is called by the verify method, which will detect the source of the error in the IR. One should always call the verify method to verify the IR.

The difference between check and check_type is that check is called at any time to check the structure of the IR by verify, while check_type is called after the type inference to check the types of the IR.

source

methodverifysource

kirin.ir.nodes.stmt.Statement.verify

def verify() -> None
source

methodverify_typesource

kirin.ir.nodes.stmt.Statement.verify_type

def verify_type() -> None

Verify the type of the statement.

Note

This API should be called after all the types are figured out (by typeinfer)

source