-
Notifications
You must be signed in to change notification settings - Fork 52
Description
A superblock will be a linear sequence of "micro ops", that may have multiple side exits, but will only have one entry point.
The start of the superblock will be determined by "hotspots" in the code. The tricky bit is creating the rest of the superblock.
For non-branching code, extending the superblock is easy enough.
For branches we either need the base interpreter to record which way the branch goes, or to make a estimate based on static analysis and the value being tested, if it is available.
For non-local jumps, like calls and return, we need to rely on the information recorded by the specializing adaptive interpreter.
We should end the superblock when either the estimated likelihood of execution staying in the superblock drops below a threshold, say 40%, or when we cannot estimate that likelihood.
An example:
...
x = typing.cast(int, x)
if unpredicable:
...
The bytecode for the above snippet is:
LOAD_GLOBAL 0 (typing)
LOAD_ATTR 3 (NULL|self + cast)
LOAD_GLOBAL 4 (int)
LOAD_FAST 0 (x)
CALL 2
STORE_FAST 0 (x)
LOAD_GLOBAL 6 (unpredicable)
POP_JUMP_IF_FALSE 13 (to 100)
and the bytecode for typing.cast
is:
RESUME 0
LOAD_FAST 1 (val)
RETURN_VALUE
Starting at the LOAD_GLOBAL 0 (typing)
the resulting superblock might look something like:
SAVE_IP
CHECK_GLOBALS_DICT_KEY_VERSION
SAVE_IP
LOAD_GLOBAL_MODULE_UNCHECKED (typing)
SAVE_IP
CHECK_MODULE_DICT_VERSION
SAVE_IP
LOAD_ATTR_MODULE_UNCHECKED 3 (NULL|self + cast)
SAVE_IP
CHECK_BUILTINS_DICT_KEY_VERSION
SAVE_IP
LOAD_GLOBAL_BUILTINS_UNCHECKED (int)
SAVE_IP
LOAD_FAST 0 (x)
SAVE_IP
CHECK_FUNCTION_VERSION
SAVE_IP
PUSH_FRAME
SAVE_IP
CHECK_EVAL_BREAKER
SAVE_IP
LOAD_FAST 1 (val)
SAVE_IP
RETURN_VALUE
SAVE_IP
STORE_FAST 0 (x)
SAVE_IP
CHECK_GLOBALS_DICT_KEY_VERSION
SAVE_IP
LOAD_GLOBAL_MODULE_UNCHECKED (unpredicable)
SAVE_IP
EXIT # Can't predict where we are going
All the SAVE_IP
instructions exist to make sure that the frame->prev_instr
field gets updated correctly.
Don't worry, optimization should remove almost all of them.