瀏覽代碼

Anonymize some variables in the JIT, improve 'input' construction

jonathanvdc 8 年之前
父節點
當前提交
f6f19702b5
共有 1 個文件被更改,包括 35 次插入15 次删除
  1. 35 15
      kernel/modelverse_jit/jit.py

+ 35 - 15
kernel/modelverse_jit/jit.py

@@ -36,8 +36,7 @@ class JitCompilationFailedException(Exception):
 
 class ModelverseJit(object):
     """A high-level interface to the modelverse JIT compiler."""
-
-    def __init__(self, max_instructions=None, compiled_function_lookup=None):
+    def __init__(self, max_instructions=50, compiled_function_lookup=None):
         self.todo_entry_points = set()
         self.no_jit_entry_points = set()
         self.jitted_entry_points = {}
@@ -46,7 +45,7 @@ class ModelverseJit(object):
             'PrimitiveFinished' : primitive_functions.PrimitiveFinished
         }
         self.jit_count = 0
-        self.max_instructions = 200 if max_instructions is None else max_instructions
+        self.max_instructions = max_instructions
         self.compiled_function_lookup = compiled_function_lookup
         # jit_intrinsics is a function name -> intrinsic map.
         self.jit_intrinsics = {}
@@ -290,6 +289,12 @@ class AnalysisState(object):
                 tree_ir.LoadLocalInstruction(KWARGS_PARAMETER_NAME),
                 tree_ir.LiteralInstruction('user_root')))
 
+    def load_kernel(self):
+        """Creates an instruction that loads the Modelverse kernel."""
+        return tree_ir.LoadIndexInstruction(
+            tree_ir.LoadLocalInstruction(KWARGS_PARAMETER_NAME),
+            tree_ir.LiteralInstruction('mvk'))
+
     def analyze(self, instruction_id):
         """Tries to build an intermediate representation from the instruction with the
         given id."""
@@ -489,31 +494,41 @@ class AnalysisState(object):
 
     def analyze_input(self, _):
         """Tries to analyze the given 'input' instruction."""
+
+        # Possible alternative to the explicit syntax tree:
+        #
+        # raise primitive_functions.PrimitiveFinished(
+        #     tree_ir.JitCallInstruction(
+        #         tree_ir.LoadGlobalInstruction('__get_input'),
+        #         [],
+        #         tree_ir.LoadLocalInstruction(KWARGS_PARAMETER_NAME)))
+
         # The plan is to generate this tree:
         #
         #     value = None
         #     while True:
+        #         _input = yield [("RD", [user_root, "input"])]
+        #         value = yield [("RD", [_input, "value"])]
+        #
         #         if value is None:
-        #             yield None # nop
+        #             kwargs['mvk'].success = False # to avoid blocking
+        #             yield None # nop/interrupt
         #         else:
         #             break
         #
-        #         _input = yield [("RD", [user_root, "input"])]
-        #         value = yield [("RD", [_input, "value"])]
-        #
         #     _next = yield [("RD", [_input, "next"])]
         #     yield [("CD", [user_root, "input", _next])]
         #     yield [("DN", [_input])]
 
         user_root = self.retrieve_user_root()
         _input = tree_ir.StoreLocalInstruction(
-            '_input',
+            None,
             tree_ir.ReadDictionaryValueInstruction(
                 user_root.create_load(),
                 tree_ir.LiteralInstruction('input')))
 
         value = tree_ir.StoreLocalInstruction(
-            'value',
+            None,
             tree_ir.ReadDictionaryValueInstruction(
                 _input.create_load(),
                 tree_ir.LiteralInstruction('value')))
@@ -525,15 +540,20 @@ class AnalysisState(object):
                     value.create_store(tree_ir.LiteralInstruction(None)),
                     tree_ir.LoopInstruction(
                         tree_ir.create_block(
+                            _input,
+                            value,
                             tree_ir.SelectInstruction(
                                 tree_ir.BinaryInstruction(
                                     value.create_load(),
                                     'is',
                                     tree_ir.LiteralInstruction(None)),
-                                tree_ir.NopInstruction(),
-                                tree_ir.BreakInstruction()),
-                            _input,
-                            value)),
+                                tree_ir.create_block(
+                                    tree_ir.StoreMemberInstruction(
+                                        self.load_kernel(),
+                                        'success',
+                                        tree_ir.LiteralInstruction(False)),
+                                    tree_ir.NopInstruction()),
+                                tree_ir.BreakInstruction()))),
                     tree_ir.CreateDictionaryEdgeInstruction(
                         user_root.create_load(),
                         tree_ir.LiteralInstruction('input'),
@@ -692,8 +712,8 @@ class AnalysisState(object):
         #     _, _ =       yield [("CD", [variable, "value", value]),
         #                         ("DE", [value_link])]
 
-        variable = tree_ir.StoreLocalInstruction('variable', var_r)
-        value = tree_ir.StoreLocalInstruction('value', value_r)
+        variable = tree_ir.StoreLocalInstruction(None, var_r)
+        value = tree_ir.StoreLocalInstruction(None, value_r)
         value_link = tree_ir.StoreLocalInstruction(
             'value_link',
             tree_ir.ReadDictionaryEdgeInstruction(