Prop
Frame dataclass
Frame(
code: Statement,
worklist: WorkList[Successor[ResultType]] = WorkList(),
visited: dict[
Block, set[tuple[Successor[ResultType], int]]
] = dict(),
should_be_pure: set[Statement] = set(),
frame_is_not_pure: bool = False,
*,
parent: FrameABC | None = None,
has_parent_access: bool = False,
lineno_offset: int = 0,
entries: dict[SSAValue, ValueType] = dict()
)
Bases: ForwardFrame[Result]
flowchart TD
kirin.analysis.const.prop.Frame[Frame]
kirin.analysis.forward.ForwardFrame[ForwardFrame]
kirin.interp.abstract.AbstractFrame[AbstractFrame]
kirin.interp.frame.Frame[Frame]
kirin.interp.frame.FrameABC[FrameABC]
kirin.analysis.forward.ForwardFrame --> kirin.analysis.const.prop.Frame
kirin.interp.abstract.AbstractFrame --> kirin.analysis.forward.ForwardFrame
kirin.interp.frame.Frame --> kirin.interp.abstract.AbstractFrame
kirin.interp.frame.FrameABC --> kirin.interp.frame.Frame
click kirin.analysis.const.prop.Frame href "" "kirin.analysis.const.prop.Frame"
click kirin.analysis.forward.ForwardFrame href "" "kirin.analysis.forward.ForwardFrame"
click kirin.interp.abstract.AbstractFrame href "" "kirin.interp.abstract.AbstractFrame"
click kirin.interp.frame.Frame href "" "kirin.interp.frame.Frame"
click kirin.interp.frame.FrameABC href "" "kirin.interp.frame.FrameABC"
frame_is_not_pure class-attribute instance-attribute
frame_is_not_pure: bool = False
If we hit any non-pure statement.
should_be_pure class-attribute instance-attribute
should_be_pure: set[Statement] = field(default_factory=set)
If any ir.MaybePure is actually pure.
Propagate dataclass
Propagate(
dialects: DialectGroup,
*,
max_depth: int = 128,
max_python_recursion_depth: int = 131072,
debug: bool = False,
recursion_rounds: int = 8,
cache_calls: bool = False
)
Bases: ForwardExtra[Frame, Result]
flowchart TD
kirin.analysis.const.prop.Propagate[Propagate]
kirin.analysis.forward.ForwardExtra[ForwardExtra]
kirin.interp.abstract.AbstractInterpreter[AbstractInterpreter]
kirin.interp.abc.InterpreterABC[InterpreterABC]
kirin.analysis.forward.ForwardExtra --> kirin.analysis.const.prop.Propagate
kirin.interp.abstract.AbstractInterpreter --> kirin.analysis.forward.ForwardExtra
kirin.interp.abc.InterpreterABC --> kirin.interp.abstract.AbstractInterpreter
click kirin.analysis.const.prop.Propagate href "" "kirin.analysis.const.prop.Propagate"
click kirin.analysis.forward.ForwardExtra href "" "kirin.analysis.forward.ForwardExtra"
click kirin.interp.abstract.AbstractInterpreter href "" "kirin.interp.abstract.AbstractInterpreter"
click kirin.interp.abc.InterpreterABC href "" "kirin.interp.abc.InterpreterABC"
Forward dataflow analysis for constant propagation.
This analysis is a forward dataflow analysis that propagates constant values through the program. It uses the Result lattice to track the constant values and purity of the values.
The analysis is implemented as a forward dataflow analysis, where the eval_stmt method is overridden to handle the different types of statements in the IR. The analysis uses the interp.Interpreter to evaluate the statements and propagate the constant values.
When a statement is registered under the "constprop" key in the method table, the analysis will call the method to evaluate the statement instead of using the interpreter. This allows for custom handling of statements.
With cache_calls set, within one run, a call with the same callee and the same argument facts as a call already analyzed reuses that call's result and frame instead of analyzing the callee again. A run starts whenever a frame is created without a caller; the IR may change between runs, so nothing is reused across them. Calls cut off at the depth limit, and calls whose frame holds a closure, are analyzed every time.
cache_calls class-attribute instance-attribute
cache_calls: bool = field(default=False, kw_only=True)
Whether identical calls within a run reuse one analysis. Off unless asked for: a reused call skips the method tables that would have run for it, and hands out the same facts where the callee would have produced fresh ones.
keys class-attribute instance-attribute
keys = ('constprop',)
The name of the interpreter to select from dialects by order. First matching key will be used.
lattice class-attribute instance-attribute
lattice = Result
lattice type for the abstract interpreter.
call
call(
node: Statement | Method,
*args: Result,
**kwargs: Result
) -> tuple[Frame, Result]
Call node, and solve a call that reaches itself as a fixpoint.
A call of a callable node with the arguments of a call that is still on the stack returns the result assumed for that call, at first bottom. The outer call then runs again with its own result joined in, until the result stays within the assumption, which is then the result. A result that keeps growing, such as a tuple that nests deeper at every round, is assumed to be top after recursion_rounds rounds. This bounds the stack by the number of distinct calls.
The recursive call also takes its frame facts, such as purity, from the previous round of the outer call. The outer call runs again until recursive_frame_settled accepts these facts.
Source code in src/kirin/analysis/const/prop.py
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | |
frame_eval
frame_eval(
frame: Frame, node: Statement
) -> interp.StatementResult[Result]
Run a statement within the current frame. This is the entry point of running a statement. It will look up the statement implementation in the dialect registry, or optionally call a fallback implementation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame | FrameType | the current frame | required |
node | Statement | the statement to run | required |
Returns:
| Name | Type | Description |
|---|---|---|
StatementResult | StatementResult[ValueType] | the result of running the statement |
Source code in src/kirin/analysis/const/prop.py
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | |
initialize_frame
initialize_frame(
node: Statement, *, has_parent_access: bool = False
) -> Frame
Initialize a new call frame for the given callable node.
Source code in src/kirin/analysis/const/prop.py
147 148 149 150 151 152 153 154 | |
method_self
method_self(method: Method) -> Result
Return the self value for the given method.
Source code in src/kirin/analysis/const/prop.py
176 177 | |
recursion_limit_reached
recursion_limit_reached() -> Result
Handle the recursion limit reached.
This method is called when the maximum depth of the interpreter stack when calling a callable node is reached. By default a StackOverflowError is raised. Overload this method to provide a custom behavior, e.g. in the case of abstract interpreter, the recursion limit returns a bottom value.
Source code in src/kirin/analysis/const/prop.py
143 144 145 | |
recursive_call_frame
recursive_call_frame(
node: Statement, assumed: Frame | None
) -> Frame
Return the frame of a recursive call with the purity of assumed.
The first round assumes that the recursive call is pure. If the outer call turns out impure, recursive_frame_settled starts another round, and the recursive call becomes impure too. Dead code elimination then keeps a recursive call whose result is unused but whose body has side effects.
Source code in src/kirin/analysis/const/prop.py
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | |
recursive_frame_settled
recursive_frame_settled(
assumed: Frame | None, frame: Frame
) -> bool
Check that a round of a recursive call confirms its assumed frame facts.
The recursive call inside the round took its facts from assumed. The outer call of the round produced frame. If the facts in frame differ from the facts in assumed, the outer call runs another round. If an analysis overrides recursive_call_frame, it must override this method as well.
Source code in src/kirin/analysis/const/prop.py
172 173 174 | |