jonathanvdc пре 8 година
родитељ
комит
d145513ba5
1 измењених фајлова са 17 додато и 1 уклоњено
  1. 17 1
      kernel/modelverse_jit/jit.py

+ 17 - 1
kernel/modelverse_jit/jit.py

@@ -98,6 +98,7 @@ class ModelverseJit(object):
         self.direct_calls_allowed = True
         self.tracing_enabled = False
         self.input_function_enabled = False
+        self.nop_insertion_enabled = True
 
     def set_jit_enabled(self, is_enabled=True):
         """Enables or disables the JIT."""
@@ -115,6 +116,13 @@ class ModelverseJit(object):
         """Enables or disables tracing for jitted code."""
         self.tracing_enabled = is_enabled
 
+    def enable_nop_insertion(self, is_enabled=True):
+        """Enables or disables nop insertion for jitted code. The JIT will insert nops at loop
+           back-edges. Inserting nops sacrifices performance to keep the jitted code from
+           blocking the thread of execution by consuming all resources; nops give the
+           Modelverse server an opportunity to interrupt the currently running code."""
+        self.nop_insertion_enabled = is_enabled
+
     def mark_entry_point(self, body_id):
         """Marks the node with the given identifier as a function entry point."""
         if body_id not in self.no_jit_entry_points and body_id not in self.jitted_entry_points:
@@ -478,9 +486,17 @@ class AnalysisState(object):
             ("RD", [instruction_id, "body"])]
 
         (cond_r, body_r), = yield [("CALL_ARGS", [self.analyze_all, ([cond, body],)])]
+        if self.jit.nop_insertion_enabled:
+            create_loop_body = lambda check, body: tree_ir.create_block(
+                check,
+                body_r,
+                tree_ir.NopInstruction())
+        else:
+            create_loop_body = tree_ir.CompoundInstruction
+
         raise primitive_functions.PrimitiveFinished(
             tree_ir.LoopInstruction(
-                tree_ir.CompoundInstruction(
+                create_loop_body(
                     tree_ir.SelectInstruction(
                         tree_ir.ReadValueInstruction(cond_r),
                         tree_ir.EmptyInstruction(),