jit.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. import modelverse_kernel.primitives as primitive_functions
  2. import modelverse_jit.bytecode_ir as bytecode_ir
  3. import modelverse_jit.bytecode_parser as bytecode_parser
  4. import modelverse_jit.bytecode_to_tree as bytecode_to_tree
  5. import modelverse_jit.tree_ir as tree_ir
  6. import modelverse_jit.runtime as jit_runtime
  7. import keyword
  8. # Import JitCompilationFailedException because it used to be defined
  9. # in this module.
  10. JitCompilationFailedException = jit_runtime.JitCompilationFailedException
  11. def map_and_simplify_generator(function, instruction):
  12. """Applies the given mapping function to every instruction in the tree
  13. that has the given instruction as root, and simplifies it on-the-fly.
  14. This is at least as powerful as first mapping and then simplifying, as
  15. maps and simplifications are interspersed.
  16. This function assumes that function creates a generator that returns by
  17. raising a primitive_functions.PrimitiveFinished."""
  18. # First handle the children by mapping on them and then simplifying them.
  19. new_children = []
  20. for inst in instruction.get_children():
  21. new_inst, = yield [("CALL_ARGS", [map_and_simplify_generator, (function, inst)])]
  22. new_children.append(new_inst)
  23. # Then apply the function to the top-level node.
  24. transformed, = yield [("CALL_ARGS", [function, (instruction.create(new_children),)])]
  25. # Finally, simplify the transformed top-level node.
  26. raise primitive_functions.PrimitiveFinished(transformed.simplify_node())
  27. def expand_constant_read(instruction):
  28. """Tries to replace a read of a constant node by a literal."""
  29. if isinstance(instruction, tree_ir.ReadValueInstruction) and \
  30. isinstance(instruction.node_id, tree_ir.LiteralInstruction):
  31. val, = yield [("RV", [instruction.node_id.literal])]
  32. raise primitive_functions.PrimitiveFinished(tree_ir.LiteralInstruction(val))
  33. else:
  34. raise primitive_functions.PrimitiveFinished(instruction)
  35. def optimize_tree_ir(instruction):
  36. """Optimizes an IR tree."""
  37. return map_and_simplify_generator(expand_constant_read, instruction)
  38. def print_value(val):
  39. """A thin wrapper around 'print'."""
  40. print(val)
  41. class ModelverseJit(object):
  42. """A high-level interface to the modelverse JIT compiler."""
  43. def __init__(self, max_instructions=None, compiled_function_lookup=None):
  44. self.todo_entry_points = set()
  45. self.no_jit_entry_points = set()
  46. self.jitted_parameters = {}
  47. self.jit_globals = {
  48. 'PrimitiveFinished' : primitive_functions.PrimitiveFinished,
  49. jit_runtime.CALL_FUNCTION_NAME : jit_runtime.call_function,
  50. jit_runtime.GET_INPUT_FUNCTION_NAME : jit_runtime.get_input
  51. }
  52. # jitted_entry_points maps body ids to values in jit_globals.
  53. self.jitted_entry_points = {}
  54. # global_functions maps global value names to body ids.
  55. self.global_functions = {}
  56. # global_functions_inv maps body ids to global value names.
  57. self.global_functions_inv = {}
  58. # bytecode_graphs maps body ids to their parsed bytecode graphs.
  59. self.bytecode_graphs = {}
  60. self.jit_count = 0
  61. self.max_instructions = max_instructions
  62. self.compiled_function_lookup = compiled_function_lookup
  63. # jit_intrinsics is a function name -> intrinsic map.
  64. self.jit_intrinsics = {}
  65. self.compilation_dependencies = {}
  66. self.jit_enabled = True
  67. self.direct_calls_allowed = True
  68. self.tracing_enabled = False
  69. self.input_function_enabled = False
  70. self.nop_insertion_enabled = True
  71. self.jit_success_log_function = None
  72. self.jit_code_log_function = None
  73. def set_jit_enabled(self, is_enabled=True):
  74. """Enables or disables the JIT."""
  75. self.jit_enabled = is_enabled
  76. def allow_direct_calls(self, is_allowed=True):
  77. """Allows or disallows direct calls from jitted to jitted code."""
  78. self.direct_calls_allowed = is_allowed
  79. def use_input_function(self, is_enabled=True):
  80. """Configures the JIT to compile 'input' instructions as function calls."""
  81. self.input_function_enabled = is_enabled
  82. def enable_tracing(self, is_enabled=True):
  83. """Enables or disables tracing for jitted code."""
  84. self.tracing_enabled = is_enabled
  85. def enable_nop_insertion(self, is_enabled=True):
  86. """Enables or disables nop insertion for jitted code. The JIT will insert nops at loop
  87. back-edges. Inserting nops sacrifices performance to keep the jitted code from
  88. blocking the thread of execution by consuming all resources; nops give the
  89. Modelverse server an opportunity to interrupt the currently running code."""
  90. self.nop_insertion_enabled = is_enabled
  91. def set_jit_success_log(self, log_function=print_value):
  92. """Configures this JIT instance with a function that prints output to a log.
  93. Success and failure messages for specific functions are then sent to said log."""
  94. self.jit_success_log_function = log_function
  95. def set_jit_code_log(self, log_function=print_value):
  96. """Configures this JIT instance with a function that prints output to a log.
  97. Function definitions of jitted functions are then sent to said log."""
  98. self.jit_code_log_function = log_function
  99. def mark_entry_point(self, body_id):
  100. """Marks the node with the given identifier as a function entry point."""
  101. if body_id not in self.no_jit_entry_points and body_id not in self.jitted_entry_points:
  102. self.todo_entry_points.add(body_id)
  103. def is_entry_point(self, body_id):
  104. """Tells if the node with the given identifier is a function entry point."""
  105. return body_id in self.todo_entry_points or \
  106. body_id in self.no_jit_entry_points or \
  107. body_id in self.jitted_entry_points
  108. def is_jittable_entry_point(self, body_id):
  109. """Tells if the node with the given identifier is a function entry point that
  110. has not been marked as non-jittable. This only returns `True` if the JIT
  111. is enabled and the function entry point has been marked jittable, or if
  112. the function has already been compiled."""
  113. return ((self.jit_enabled and body_id in self.todo_entry_points) or
  114. self.has_compiled(body_id))
  115. def has_compiled(self, body_id):
  116. """Tests if the function belonging to the given body node has been compiled yet."""
  117. return body_id in self.jitted_entry_points
  118. def get_compiled_name(self, body_id):
  119. """Gets the name of the compiled version of the given body node in the JIT
  120. global state."""
  121. return self.jitted_entry_points[body_id]
  122. def mark_no_jit(self, body_id):
  123. """Informs the JIT that the node with the given identifier is a function entry
  124. point that must never be jitted."""
  125. self.no_jit_entry_points.add(body_id)
  126. if body_id in self.todo_entry_points:
  127. self.todo_entry_points.remove(body_id)
  128. def generate_name(self, infix, suggested_name=None):
  129. """Generates a new name or picks the suggested name if it is still
  130. available."""
  131. if suggested_name is not None \
  132. and suggested_name not in self.jit_globals \
  133. and not keyword.iskeyword(suggested_name):
  134. self.jit_count += 1
  135. return suggested_name
  136. else:
  137. function_name = 'jit_%s%d' % (infix, self.jit_count)
  138. self.jit_count += 1
  139. return function_name
  140. def generate_function_name(self, body_id, suggested_name=None):
  141. """Generates a new function name or picks the suggested name if it is still
  142. available."""
  143. if suggested_name is None:
  144. suggested_name = self.get_global_name(body_id)
  145. return self.generate_name('func', suggested_name)
  146. def register_global(self, body_id, global_name):
  147. """Associates the given body id with the given global name."""
  148. self.global_functions[global_name] = body_id
  149. self.global_functions_inv[body_id] = global_name
  150. def get_global_name(self, body_id):
  151. """Gets the name of the global function with the given body id.
  152. Returns None if no known global exists with the given id."""
  153. if body_id in self.global_functions_inv:
  154. return self.global_functions_inv[body_id]
  155. else:
  156. return None
  157. def get_global_body_id(self, global_name):
  158. """Gets the body id of the global function with the given name.
  159. Returns None if no known global exists with the given name."""
  160. if global_name in self.global_functions:
  161. return self.global_functions[global_name]
  162. else:
  163. return None
  164. def register_compiled(self, body_id, compiled_function, function_name=None):
  165. """Registers a compiled entry point with the JIT."""
  166. # Get the function's name.
  167. function_name = self.generate_function_name(body_id, function_name)
  168. # Map the body id to the given parameter list.
  169. self.jitted_entry_points[body_id] = function_name
  170. self.jit_globals[function_name] = compiled_function
  171. if body_id in self.todo_entry_points:
  172. self.todo_entry_points.remove(body_id)
  173. def import_value(self, value, suggested_name=None):
  174. """Imports the given value into the JIT's global scope, with the given suggested name.
  175. The actual name of the value (within the JIT's global scope) is returned."""
  176. actual_name = self.generate_name('import', suggested_name)
  177. self.jit_globals[actual_name] = value
  178. return actual_name
  179. def lookup_compiled_function(self, name):
  180. """Looks up a compiled function by name. Returns a matching function,
  181. or None if no function was found."""
  182. if name is None:
  183. return None
  184. elif name in self.jit_globals:
  185. return self.jit_globals[name]
  186. elif self.compiled_function_lookup is not None:
  187. return self.compiled_function_lookup(name)
  188. else:
  189. return None
  190. def get_intrinsic(self, name):
  191. """Tries to find an intrinsic version of the function with the
  192. given name."""
  193. if name in self.jit_intrinsics:
  194. return self.jit_intrinsics[name]
  195. else:
  196. return None
  197. def register_intrinsic(self, name, intrinsic_function):
  198. """Registers the given intrisic with the JIT. This will make the JIT replace calls to
  199. the function with the given entry point by an application of the specified function."""
  200. self.jit_intrinsics[name] = intrinsic_function
  201. def register_binary_intrinsic(self, name, operator):
  202. """Registers an intrinsic with the JIT that represents the given binary operation."""
  203. self.register_intrinsic(name, lambda a, b: tree_ir.CreateNodeWithValueInstruction(
  204. tree_ir.BinaryInstruction(
  205. tree_ir.ReadValueInstruction(a),
  206. operator,
  207. tree_ir.ReadValueInstruction(b))))
  208. def register_unary_intrinsic(self, name, operator):
  209. """Registers an intrinsic with the JIT that represents the given unary operation."""
  210. self.register_intrinsic(name, lambda a: tree_ir.CreateNodeWithValueInstruction(
  211. tree_ir.UnaryInstruction(
  212. operator,
  213. tree_ir.ReadValueInstruction(a))))
  214. def register_cast_intrinsic(self, name, target_type):
  215. """Registers an intrinsic with the JIT that represents a unary conversion operator."""
  216. self.register_intrinsic(name, lambda a: tree_ir.CreateNodeWithValueInstruction(
  217. tree_ir.CallInstruction(
  218. tree_ir.LoadGlobalInstruction(target_type.__name__),
  219. [tree_ir.ReadValueInstruction(a)])))
  220. def jit_signature(self, body_id):
  221. """Acquires the signature for the given body id node, which consists of the
  222. parameter variables, parameter name and a flag that tells if the given function
  223. is mutable."""
  224. if body_id not in self.jitted_parameters:
  225. signature_id, = yield [("RRD", [body_id, jit_runtime.FUNCTION_BODY_KEY])]
  226. signature_id = signature_id[0]
  227. param_set_id, is_mutable = yield [
  228. ("RD", [signature_id, "params"]),
  229. ("RD", [signature_id, jit_runtime.MUTABLE_FUNCTION_KEY])]
  230. if param_set_id is None:
  231. self.jitted_parameters[body_id] = ([], [], is_mutable)
  232. else:
  233. param_name_ids, = yield [("RDK", [param_set_id])]
  234. param_names = yield [("RV", [n]) for n in param_name_ids]
  235. param_vars = yield [("RD", [param_set_id, k]) for k in param_names]
  236. self.jitted_parameters[body_id] = (param_vars, param_names, is_mutable)
  237. raise primitive_functions.PrimitiveFinished(self.jitted_parameters[body_id])
  238. def jit_parse_bytecode(self, body_id):
  239. """Parses the given function body as a bytecode graph."""
  240. if body_id in self.bytecode_graphs:
  241. raise primitive_functions.PrimitiveFinished(self.bytecode_graphs[body_id])
  242. parser = bytecode_parser.BytecodeParser()
  243. result, = yield [("CALL_ARGS", [parser.parse_instruction, (body_id,)])]
  244. self.bytecode_graphs[body_id] = result
  245. raise primitive_functions.PrimitiveFinished(result)
  246. def jit_compile(self, user_root, body_id, suggested_name=None):
  247. """Tries to jit the function defined by the given entry point id and parameter list."""
  248. # The comment below makes pylint shut up about our (hopefully benign) use of exec here.
  249. # pylint: disable=I0011,W0122
  250. if body_id is None:
  251. raise ValueError('body_id cannot be None')
  252. elif body_id in self.jitted_entry_points:
  253. # We have already compiled this function.
  254. raise primitive_functions.PrimitiveFinished(
  255. self.jit_globals[self.jitted_entry_points[body_id]])
  256. elif body_id in self.no_jit_entry_points:
  257. # We're not allowed to jit this function or have tried and failed before.
  258. raise JitCompilationFailedException(
  259. 'Cannot jit function %s at %d because it is marked non-jittable.' % (
  260. '' if suggested_name is None else "'" + suggested_name + "'",
  261. body_id))
  262. elif not self.jit_enabled:
  263. # We're not allowed to jit anything.
  264. raise JitCompilationFailedException(
  265. 'Cannot jit function %s at %d because the JIT has been disabled.' % (
  266. '' if suggested_name is None else "'" + suggested_name + "'",
  267. body_id))
  268. # Generate a name for the function we're about to analyze, and pretend that
  269. # it already exists. (we need to do this for recursive functions)
  270. function_name = self.generate_function_name(body_id, suggested_name)
  271. self.jitted_entry_points[body_id] = function_name
  272. self.jit_globals[function_name] = None
  273. (parameter_ids, parameter_list, is_mutable), = yield [
  274. ("CALL_ARGS", [self.jit_signature, (body_id,)])]
  275. param_dict = dict(zip(parameter_ids, parameter_list))
  276. body_param_dict = dict(zip(parameter_ids, [p + "_ptr" for p in parameter_list]))
  277. dependencies = set([body_id])
  278. self.compilation_dependencies[body_id] = dependencies
  279. def handle_jit_exception(exception):
  280. # If analysis fails, then a JitCompilationFailedException will be thrown.
  281. del self.compilation_dependencies[body_id]
  282. for dep in dependencies:
  283. self.mark_no_jit(dep)
  284. if dep in self.jitted_entry_points:
  285. del self.jitted_entry_points[dep]
  286. failure_message = "%s (function '%s' at %d)" % (
  287. exception.message, function_name, body_id)
  288. if self.jit_success_log_function is not None:
  289. self.jit_success_log_function('JIT compilation failed: %s' % failure_message)
  290. raise JitCompilationFailedException(failure_message)
  291. # Try to analyze the function's body.
  292. yield [("TRY", [])]
  293. yield [("CATCH", [JitCompilationFailedException, handle_jit_exception])]
  294. if is_mutable:
  295. # We can't just JIT mutable functions. That'd be dangerous.
  296. raise JitCompilationFailedException(
  297. "Function was marked '%s'." % jit_runtime.MUTABLE_FUNCTION_KEY)
  298. body_bytecode, = yield [("CALL_ARGS", [self.jit_parse_bytecode, (body_id,)])]
  299. state = bytecode_to_tree.AnalysisState(
  300. self, body_id, user_root, body_param_dict,
  301. self.max_instructions)
  302. constructed_body, = yield [("CALL_ARGS", [state.analyze, (body_bytecode,)])]
  303. yield [("END_TRY", [])]
  304. del self.compilation_dependencies[body_id]
  305. # Write a prologue and prepend it to the generated function body.
  306. prologue_statements = []
  307. # Create a LOCALS_NODE_NAME node, and connect it to the user root.
  308. prologue_statements.append(
  309. tree_ir.create_new_local_node(
  310. jit_runtime.LOCALS_NODE_NAME,
  311. tree_ir.LoadIndexInstruction(
  312. tree_ir.LoadLocalInstruction(jit_runtime.KWARGS_PARAMETER_NAME),
  313. tree_ir.LiteralInstruction('user_root')),
  314. jit_runtime.LOCALS_EDGE_NAME))
  315. for (key, val) in param_dict.items():
  316. arg_ptr = tree_ir.create_new_local_node(
  317. body_param_dict[key],
  318. tree_ir.LoadLocalInstruction(jit_runtime.LOCALS_NODE_NAME))
  319. prologue_statements.append(arg_ptr)
  320. prologue_statements.append(
  321. tree_ir.CreateDictionaryEdgeInstruction(
  322. tree_ir.LoadLocalInstruction(body_param_dict[key]),
  323. tree_ir.LiteralInstruction('value'),
  324. tree_ir.LoadLocalInstruction(val)))
  325. constructed_body = tree_ir.create_block(
  326. *(prologue_statements + [constructed_body]))
  327. # Optimize the function's body.
  328. constructed_body, = yield [("CALL_ARGS", [optimize_tree_ir, (constructed_body,)])]
  329. # Shield temporaries from the GC.
  330. constructed_body = tree_ir.protect_temporaries_from_gc(
  331. constructed_body, tree_ir.LoadLocalInstruction(jit_runtime.LOCALS_NODE_NAME))
  332. # Wrap the IR in a function definition, give it a unique name.
  333. constructed_function = tree_ir.DefineFunctionInstruction(
  334. function_name,
  335. parameter_list + ['**' + jit_runtime.KWARGS_PARAMETER_NAME],
  336. constructed_body)
  337. # Convert the function definition to Python code, and compile it.
  338. exec(str(constructed_function), self.jit_globals)
  339. # Extract the compiled function from the JIT global state.
  340. compiled_function = self.jit_globals[function_name]
  341. if self.jit_success_log_function is not None:
  342. self.jit_success_log_function(
  343. "JIT compilation successful: (function '%s' at %d)" % (function_name, body_id))
  344. if self.jit_code_log_function is not None:
  345. self.jit_code_log_function(constructed_function)
  346. raise primitive_functions.PrimitiveFinished(compiled_function)