Skip to content

Inline

Inline dataclass

Inline(heuristic: Callable[[ir.Statement], bool])

Bases: RewriteRule

heuristic instance-attribute

heuristic: Callable[[Statement], bool]

inline heuristic that determines whether a function should be inlined

inline_call_like

inline_call_like(
    call_like: ir.Statement,
    args: tuple[ir.SSAValue, ...],
    region: ir.Region,
)

Inline a function call-like statement

Parameters:

Name Type Description Default
call_like Statement

the call-like statement

required
args tuple[SSAValue, ...]

the arguments of the call (first one is the callee)

required
region Region

the region of the callee

required
Source code in src/kirin/rewrite/inline.py
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
def inline_call_like(
    self,
    call_like: ir.Statement,
    args: tuple[ir.SSAValue, ...],
    region: ir.Region,
):
    """
    Inline a function call-like statement

    Args:
        call_like (ir.Statement): the call-like statement
        args (tuple[ir.SSAValue, ...]): the arguments of the call (first one is the callee)
        region (ir.Region): the region of the callee
    """
    if not call_like.parent_block:
        return

    if not call_like.parent_region:
        return

    # NOTE: we cannot change region because it may be used elsewhere
    inline_region: ir.Region = region.clone()

    # Preserve source information by attributing inlined code to the call site
    if call_like.source is not None:
        for block in inline_region.blocks:
            if block.source is None:
                block.source = call_like.source
            for stmt in block.stmts:
                if stmt.source is None:
                    stmt.source = call_like.source

    if self._can_use_simple_inline(inline_region):
        return self._inline_simple(call_like, args, inline_region.blocks[0])

    return self._inline_complex(call_like, args, inline_region)