|
@@ -1,3 +1,4 @@
|
|
|
+import math
|
|
|
import keyword
|
|
|
from collections import defaultdict
|
|
|
import modelverse_kernel.primitives as primitive_functions
|
|
@@ -852,6 +853,27 @@ def favor_loops(body_bytecode):
|
|
|
|
|
|
return temperature, 1
|
|
|
|
|
|
+def favor_small_loops(body_bytecode):
|
|
|
+ """Computes the initial temperature of a function. Code within a loop makes
|
|
|
+ the function hotter; code outside loops makes the function colder. The
|
|
|
+ temperature is incremented by one on every call."""
|
|
|
+ reachable_instructions = body_bytecode.get_reachable()
|
|
|
+ # First set the temperature to the negative number of instructions.
|
|
|
+ temperature = ADAPTIVE_FAST_JIT_TEMPERATURE_THRESHOLD - 50 - len(reachable_instructions)
|
|
|
+ for instruction in reachable_instructions:
|
|
|
+ if isinstance(instruction, bytecode_ir.WhileInstruction):
|
|
|
+ # Then increase the temperature by the number of instructions reachable
|
|
|
+ # from loop bodies. Note that the algorithm will count nested loops twice.
|
|
|
+ # This is actually by design.
|
|
|
+ loop_body_instructions = instruction.body.get_reachable(
|
|
|
+ lambda x: not isinstance(
|
|
|
+ x, (bytecode_ir.BreakInstruction, bytecode_ir.ContinueInstruction)))
|
|
|
+ temperature += (
|
|
|
+ (ADAPTIVE_JIT_LOOP_INSTRUCTION_MULTIPLIER ** 2) *
|
|
|
+ int(math.sqrt(len(loop_body_instructions))))
|
|
|
+
|
|
|
+ return temperature, max(int(math.log(len(reachable_instructions), 2)), 1)
|
|
|
+
|
|
|
class AdaptiveJitState(object):
|
|
|
"""Shared state for adaptive JIT compilation."""
|
|
|
def __init__(
|