Browse Source

Institute an upper bound on the size of jittable functions

jonathanvdc 8 years ago
parent
commit
8f43e78834
1 changed files with 8 additions and 3 deletions
  1. 8 3
      kernel/modelverse_jit/jit.py

+ 8 - 3
kernel/modelverse_jit/jit.py

@@ -11,7 +11,7 @@ class JitCompilationFailedException(Exception):
 class ModelverseJit(object):
     """A high-level interface to the modelverse JIT compiler."""
 
-    def __init__(self):
+    def __init__(self, max_instructions=None):
         self.todo_entry_points = set()
         self.no_jit_entry_points = set()
         self.jitted_entry_points = {}
@@ -19,6 +19,7 @@ class ModelverseJit(object):
             'PrimitiveFinished' : primitive_functions.PrimitiveFinished
         }
         self.jit_count = 0
+        self.max_instructions = 30 if max_instructions is None else max_instructions
 
     def mark_entry_point(self, body_id):
         """Marks the node with the given identifier as a function entry point."""
@@ -60,8 +61,8 @@ class ModelverseJit(object):
             raise JitCompilationFailedException(
                 'Cannot jit function at %d because it is marked non-jittable.' % body_id)
 
-        gen = AnalysisState().analyze(body_id)
         try:
+            gen = AnalysisState(self.max_instructions).analyze(body_id)
             inp = None
             while True:
                 inp = yield gen.send(inp)
@@ -92,8 +93,9 @@ class ModelverseJit(object):
 class AnalysisState(object):
     """The state of a bytecode analysis call graph."""
 
-    def __init__(self):
+    def __init__(self, max_instructions=None):
         self.analyzed_instructions = set()
+        self.max_instructions = max_instructions
 
     def get_local_name(self, local_id):
         """Gets the name for a local with the given id."""
@@ -115,6 +117,9 @@ class AnalysisState(object):
         # infinite loops.
         if instruction_id in self.analyzed_instructions:
             raise JitCompilationFailedException('Cannot jit non-tree instruction graph.')
+        elif (self.max_instructions is not None and
+              len(self.analyzed_instructions) > self.max_instructions):
+            raise JitCompilationFailedException('Maximal number of instructions exceeded.')
 
         self.analyzed_instructions.add(instruction_id)
         instruction_val, = yield [("RV", [instruction_id])]