Repository navigation
Open
Labels
epic-tier2-optimizerLinear code region optimizer for 3.13 and beyond.
Description
This pass defers the creation and update of objects, including the frame stack.
By maintaining a "shadow frame stack" we can defer operations that create new objects, or modify the frame stack, only materializing the frame stack when needed.
Doing so for SAVE_IP is trivial, we just record the current prev_instr on the shadow frame.
I will omit the SAVE_IPs for clarity in the following example.
Taking the code from the specialization example:
POP_TOP
LOAD_CONST (typing.cast)
LOAD_CONST (int)
LOAD_FAST 0 (x)
PUSH_FRAME 2
CHECK_EVAL_BREAKER
LOAD_FAST 1 (val)
RETURN_VALUE
STORE_FAST 0 (x)
LOAD_GLOBAL_MODULE_UNCHECKED (unpredicable)
EXIT # Can't predict where we are going
This produces the following superblock with commented shadow frame stack:
NOP # stack: [ typing ]
NOP # stack: [ ]
NOP # stack: [ typing.cast ]
NOP # stack: [ typing.cast, int ]
NOP # stack: [ typing.cast, int, LOCAL(0) ]
NOP # prev_frame: [] frame: (locals=(int, LOCAL(0)), stack: []) We push a shadow frame instead of a real one
CHECK_EVAL_BREAKER # We still need to check the eval breaker, but only once per superblock or loop.
NOP # prev_frame: [] frame: (locals=(int, LOCAL(0)), stack: [ LOCAL(0) ])
NOP # stack: [ LOCAL(0) ] The return is performed on the shadow stack.
NOP # stack: [] (this is a NOP as we are storing a value to itself)
LOAD_GLOBAL_MODULE_UNCHECKED (unpredicable)
SAVE_IP
EXIT # Can't predict where we are going
Which after the cleanup pass is:
CHECK_EVAL_BREAKER
LOAD_GLOBAL_MODULE_UNCHECKED (unpredicable)
SAVE_IP
EXIT # Can't predict where we are going
This is obviously a somewhat contrived example, but you get the idea.
Metadata
Metadata
Assignees
Labels
epic-tier2-optimizerLinear code region optimizer for 3.13 and beyond.
Activity
Fidget-Spinner commentedon Mar 10, 2023
Please bear with me as I'm quite confused. What keeps track of the shadow stack (how does the shadow stack suddenly know that
typingor other things are on the stack)? Or is it just another operand stack?Fidget-Spinner commentedon Mar 11, 2023
Hopefully this helps anyone trying to understand this better. After reading Mark's PhD theses https://theses.gla.ac.uk/2975/ , this is what I surmise:
Ref page 114 (5.7, Deferred Object Creation):
What's a shadow frame stack?
(Shannon, 2011)
What happens at boundaries between shadow stack objects and operand stack objects
(Shannon, 2011)
So in the example Mark gives above, it is effectively seeing:
and deciding that, after inlining
typing.cast, the whole intermediate op is effectively a no-op, and just the only thing that really matters is thereturn unpredictable. Thusx = typing.cast(x, int)becomes a no-op.In short, partial evaluation, to my understanding, is when the optimizer can determine the net input and output effects, and optimize away any known intermediate operations. Really cool idea!
markshannon commentedon Mar 16, 2023
Everything you could possibly want to know about partial evaluation (and then some): https://www.itu.dk/~sestoft/pebook/pebook.html
In this case we are partial evaluating the function
interp(sb, ctx), whereintepris the interpreter,sbis the unoptimized superblock andctxis the executing context (known types, etc) to produce a residual programopt.Full partial evaluation would include compilation, such that
opt(ctx) == interp(sb, ctx), but we want to produce a program for a tier2 interpreterinterp2, such thatinterp2(opt) == interp(sb, ctx).So this is a limited form of partial evaluation, but many of the same concepts apply.
Fidget-Spinner commentedon Sep 1, 2024
I've read enough literature to understand this properly now. Here's a proposed definition/invariant/pseudo-algo:
We shall start with a simple optimization: e.g. LOAD_FAST, POP_TOP or constant propagation will do.
flowchart BT A(Bottom, i.e. empty) --> B(S) A --> C(D) B --> E(S,S) B --> D(S, D) C --> D C --> F(D, D);Fidget-Spinner commentedon Sep 17, 2024
Relevant paper https://dl.acm.org/doi/10.1145/1929501.1929508