Переглянути джерело

Define a new kernel config: --kernel=adaptive-jit-favor-small-loops

jonathanvdc 8 роки тому
батько
коміт
3e3ead19ee

+ 5 - 1
hybrid_server/classes/mvkcontroller.xml

@@ -54,10 +54,14 @@
                     self.mvk.jit.set_function_body_compiler(
                         lambda *args: jit.compile_function_body_adaptive(
                             *args, temperature_heuristic=jit.favor_small_functions))
-                elif opt == 'adaptive-jit' or opt == 'adaptive-jit-favor-loops':
+                elif opt == 'adaptive-jit-favor-loops':
                     self.mvk.jit.set_function_body_compiler(
                         lambda *args: jit.compile_function_body_adaptive(
                             *args, temperature_heuristic=jit.favor_loops))
+                elif opt == 'adaptive-jit' or opt == 'adaptive-jit-favor-small-loops':
+                    self.mvk.jit.set_function_body_compiler(
+                        lambda *args: jit.compile_function_body_adaptive(
+                            *args, temperature_heuristic=jit.favor_small_loops))
                 else:
                     print("warning: unknown kernel option '%s'." % opt)
 

+ 22 - 0
kernel/modelverse_jit/jit.py

@@ -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__(

+ 3 - 1
performance/utils.py

@@ -37,6 +37,7 @@ OPTIMIZATION_LEVEL_FAST_JIT_NO_NOPS = "fast-jit,no-insert-nops"
 OPTIMIZATION_LEVEL_ADAPTIVE_JIT_FAVOR_LARGE_FUNCTIONS = "adaptive-jit-favor-large-functions"
 OPTIMIZATION_LEVEL_ADAPTIVE_JIT_FAVOR_SMALL_FUNCTIONS = "adaptive-jit-favor-small-functions"
 OPTIMIZATION_LEVEL_ADAPTIVE_JIT_FAVOR_LOOPS = "adaptive-jit-favor-loops"
+OPTIMIZATION_LEVEL_ADAPTIVE_JIT_FAVOR_SMALL_LOOPS = "adaptive-jit-favor-small-loops"
 ALL_OPTIMIZATION_LEVELS = [
     OPTIMIZATION_LEVEL_LEGACY_INTERPRETER,
     OPTIMIZATION_LEVEL_INTERPRETER,
@@ -47,7 +48,8 @@ ALL_OPTIMIZATION_LEVELS = [
     OPTIMIZATION_LEVEL_FAST_JIT_NO_NOPS,
     OPTIMIZATION_LEVEL_ADAPTIVE_JIT_FAVOR_LARGE_FUNCTIONS,
     OPTIMIZATION_LEVEL_ADAPTIVE_JIT_FAVOR_SMALL_FUNCTIONS,
-    OPTIMIZATION_LEVEL_ADAPTIVE_JIT_FAVOR_LOOPS
+    OPTIMIZATION_LEVEL_ADAPTIVE_JIT_FAVOR_LOOPS,
+    OPTIMIZATION_LEVEL_ADAPTIVE_JIT_FAVOR_SMALL_LOOPS
 ]
 
 class ModelverseTerminated(Exception):