jit.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. import keyword
  2. import modelverse_kernel.primitives as primitive_functions
  3. import modelverse_jit.bytecode_parser as bytecode_parser
  4. import modelverse_jit.bytecode_to_tree as bytecode_to_tree
  5. import modelverse_jit.bytecode_to_cfg as bytecode_to_cfg
  6. import modelverse_jit.cfg_optimization as cfg_optimization
  7. import modelverse_jit.cfg_to_tree as cfg_to_tree
  8. import modelverse_jit.cfg_ir as cfg_ir
  9. import modelverse_jit.tree_ir as tree_ir
  10. import modelverse_jit.runtime as jit_runtime
  11. # Import JitCompilationFailedException because it used to be defined
  12. # in this module.
  13. JitCompilationFailedException = jit_runtime.JitCompilationFailedException
  14. def map_and_simplify_generator(function, instruction):
  15. """Applies the given mapping function to every instruction in the tree
  16. that has the given instruction as root, and simplifies it on-the-fly.
  17. This is at least as powerful as first mapping and then simplifying, as
  18. maps and simplifications are interspersed.
  19. This function assumes that function creates a generator that returns by
  20. raising a primitive_functions.PrimitiveFinished."""
  21. # First handle the children by mapping on them and then simplifying them.
  22. new_children = []
  23. for inst in instruction.get_children():
  24. new_inst, = yield [("CALL_ARGS", [map_and_simplify_generator, (function, inst)])]
  25. new_children.append(new_inst)
  26. # Then apply the function to the top-level node.
  27. transformed, = yield [("CALL_ARGS", [function, (instruction.create(new_children),)])]
  28. # Finally, simplify the transformed top-level node.
  29. raise primitive_functions.PrimitiveFinished(transformed.simplify_node())
  30. def expand_constant_read(instruction):
  31. """Tries to replace a read of a constant node by a literal."""
  32. if isinstance(instruction, tree_ir.ReadValueInstruction) and \
  33. isinstance(instruction.node_id, tree_ir.LiteralInstruction):
  34. val, = yield [("RV", [instruction.node_id.literal])]
  35. raise primitive_functions.PrimitiveFinished(tree_ir.LiteralInstruction(val))
  36. else:
  37. raise primitive_functions.PrimitiveFinished(instruction)
  38. def optimize_tree_ir(instruction):
  39. """Optimizes an IR tree."""
  40. return map_and_simplify_generator(expand_constant_read, instruction)
  41. def create_bare_function(function_name, parameter_list, function_body):
  42. """Creates a function definition from the given function name, parameter list
  43. and function body. No prolog is included."""
  44. # Wrap the IR in a function definition, give it a unique name.
  45. return tree_ir.DefineFunctionInstruction(
  46. function_name,
  47. parameter_list + ['**' + jit_runtime.KWARGS_PARAMETER_NAME],
  48. function_body)
  49. def create_function(
  50. function_name, parameter_list, param_dict,
  51. body_param_dict, function_body):
  52. """Creates a function from the given function name, parameter list,
  53. variable-to-parameter name map, variable-to-local name map and
  54. function body."""
  55. # Write a prologue and prepend it to the generated function body.
  56. prologue_statements = []
  57. # Create a LOCALS_NODE_NAME node, and connect it to the user root.
  58. prologue_statements.append(
  59. tree_ir.create_new_local_node(
  60. jit_runtime.LOCALS_NODE_NAME,
  61. tree_ir.LoadIndexInstruction(
  62. tree_ir.LoadLocalInstruction(jit_runtime.KWARGS_PARAMETER_NAME),
  63. tree_ir.LiteralInstruction('task_root')),
  64. jit_runtime.LOCALS_EDGE_NAME))
  65. for (key, val) in param_dict.items():
  66. arg_ptr = tree_ir.create_new_local_node(
  67. body_param_dict[key],
  68. tree_ir.LoadLocalInstruction(jit_runtime.LOCALS_NODE_NAME))
  69. prologue_statements.append(arg_ptr)
  70. prologue_statements.append(
  71. tree_ir.CreateDictionaryEdgeInstruction(
  72. tree_ir.LoadLocalInstruction(body_param_dict[key]),
  73. tree_ir.LiteralInstruction('value'),
  74. tree_ir.LoadLocalInstruction(val)))
  75. constructed_body = tree_ir.create_block(
  76. *(prologue_statements + [function_body]))
  77. # Shield temporaries from the GC.
  78. constructed_body = tree_ir.protect_temporaries_from_gc(
  79. constructed_body, tree_ir.LoadLocalInstruction(jit_runtime.LOCALS_NODE_NAME))
  80. return create_bare_function(function_name, parameter_list, constructed_body)
  81. def print_value(val):
  82. """A thin wrapper around 'print'."""
  83. print(val)
  84. class ModelverseJit(object):
  85. """A high-level interface to the modelverse JIT compiler."""
  86. def __init__(self, max_instructions=None, compiled_function_lookup=None):
  87. self.todo_entry_points = set()
  88. self.no_jit_entry_points = set()
  89. self.jitted_parameters = {}
  90. self.jit_globals = {
  91. 'PrimitiveFinished' : primitive_functions.PrimitiveFinished,
  92. jit_runtime.CALL_FUNCTION_NAME : jit_runtime.call_function,
  93. jit_runtime.GET_INPUT_FUNCTION_NAME : jit_runtime.get_input,
  94. jit_runtime.JIT_THUNK_CONSTANT_FUNCTION_NAME : self.jit_thunk_constant,
  95. jit_runtime.JIT_THUNK_GLOBAL_FUNCTION_NAME : self.jit_thunk_global
  96. }
  97. # jitted_entry_points maps body ids to values in jit_globals.
  98. self.jitted_entry_points = {}
  99. # global_functions maps global value names to body ids.
  100. self.global_functions = {}
  101. # global_functions_inv maps body ids to global value names.
  102. self.global_functions_inv = {}
  103. # bytecode_graphs maps body ids to their parsed bytecode graphs.
  104. self.bytecode_graphs = {}
  105. self.jit_count = 0
  106. self.max_instructions = max_instructions
  107. self.compiled_function_lookup = compiled_function_lookup
  108. # jit_intrinsics is a function name -> intrinsic map.
  109. self.jit_intrinsics = {}
  110. # cfg_jit_intrinsics is a function name -> intrinsic map.
  111. self.cfg_jit_intrinsics = {}
  112. self.compilation_dependencies = {}
  113. self.jit_enabled = True
  114. self.direct_calls_allowed = True
  115. self.tracing_enabled = False
  116. self.input_function_enabled = False
  117. self.nop_insertion_enabled = True
  118. self.thunks_enabled = True
  119. self.jit_success_log_function = None
  120. self.jit_code_log_function = None
  121. self.compile_function_body = compile_function_body_baseline
  122. def set_jit_enabled(self, is_enabled=True):
  123. """Enables or disables the JIT."""
  124. self.jit_enabled = is_enabled
  125. def allow_direct_calls(self, is_allowed=True):
  126. """Allows or disallows direct calls from jitted to jitted code."""
  127. self.direct_calls_allowed = is_allowed
  128. def use_input_function(self, is_enabled=True):
  129. """Configures the JIT to compile 'input' instructions as function calls."""
  130. self.input_function_enabled = is_enabled
  131. def enable_tracing(self, is_enabled=True):
  132. """Enables or disables tracing for jitted code."""
  133. self.tracing_enabled = is_enabled
  134. def enable_nop_insertion(self, is_enabled=True):
  135. """Enables or disables nop insertion for jitted code. If enabled, the JIT will
  136. insert nops at loop back-edges. Inserting nops sacrifices performance to
  137. keep the jitted code from blocking the thread of execution and consuming
  138. all resources; nops give the Modelverse server an opportunity to interrupt
  139. the currently running code."""
  140. self.nop_insertion_enabled = is_enabled
  141. def enable_thunks(self, is_enabled=True):
  142. """Enables or disables thunks for jitted code. Thunks delay the compilation of
  143. functions until they are actually used. Thunks generally reduce start-up
  144. time.
  145. Thunks are enabled by default."""
  146. self.thunks_enabled = is_enabled
  147. def set_jit_success_log(self, log_function=print_value):
  148. """Configures this JIT instance with a function that prints output to a log.
  149. Success and failure messages for specific functions are then sent to said log."""
  150. self.jit_success_log_function = log_function
  151. def set_jit_code_log(self, log_function=print_value):
  152. """Configures this JIT instance with a function that prints output to a log.
  153. Function definitions of jitted functions are then sent to said log."""
  154. self.jit_code_log_function = log_function
  155. def set_function_body_compiler(self, compile_function_body):
  156. """Sets the function that the JIT uses to compile function bodies."""
  157. self.compile_function_body = compile_function_body
  158. def mark_entry_point(self, body_id):
  159. """Marks the node with the given identifier as a function entry point."""
  160. if body_id not in self.no_jit_entry_points and body_id not in self.jitted_entry_points:
  161. self.todo_entry_points.add(body_id)
  162. def is_entry_point(self, body_id):
  163. """Tells if the node with the given identifier is a function entry point."""
  164. return body_id in self.todo_entry_points or \
  165. body_id in self.no_jit_entry_points or \
  166. body_id in self.jitted_entry_points
  167. def is_jittable_entry_point(self, body_id):
  168. """Tells if the node with the given identifier is a function entry point that
  169. has not been marked as non-jittable. This only returns `True` if the JIT
  170. is enabled and the function entry point has been marked jittable, or if
  171. the function has already been compiled."""
  172. return ((self.jit_enabled and body_id in self.todo_entry_points) or
  173. self.has_compiled(body_id))
  174. def has_compiled(self, body_id):
  175. """Tests if the function belonging to the given body node has been compiled yet."""
  176. return body_id in self.jitted_entry_points
  177. def get_compiled_name(self, body_id):
  178. """Gets the name of the compiled version of the given body node in the JIT
  179. global state."""
  180. if body_id in self.jitted_entry_points:
  181. return self.jitted_entry_points[body_id]
  182. else:
  183. return None
  184. def mark_no_jit(self, body_id):
  185. """Informs the JIT that the node with the given identifier is a function entry
  186. point that must never be jitted."""
  187. self.no_jit_entry_points.add(body_id)
  188. if body_id in self.todo_entry_points:
  189. self.todo_entry_points.remove(body_id)
  190. def generate_name(self, infix, suggested_name=None):
  191. """Generates a new name or picks the suggested name if it is still
  192. available."""
  193. if suggested_name is not None \
  194. and suggested_name not in self.jit_globals \
  195. and not keyword.iskeyword(suggested_name):
  196. self.jit_count += 1
  197. return suggested_name
  198. else:
  199. function_name = 'jit_%s%d' % (infix, self.jit_count)
  200. self.jit_count += 1
  201. return function_name
  202. def generate_function_name(self, body_id, suggested_name=None):
  203. """Generates a new function name or picks the suggested name if it is still
  204. available."""
  205. if suggested_name is None:
  206. suggested_name = self.get_global_name(body_id)
  207. return self.generate_name('func', suggested_name)
  208. def register_global(self, body_id, global_name):
  209. """Associates the given body id with the given global name."""
  210. self.global_functions[global_name] = body_id
  211. self.global_functions_inv[body_id] = global_name
  212. def get_global_name(self, body_id):
  213. """Gets the name of the global function with the given body id.
  214. Returns None if no known global exists with the given id."""
  215. if body_id in self.global_functions_inv:
  216. return self.global_functions_inv[body_id]
  217. else:
  218. return None
  219. def get_global_body_id(self, global_name):
  220. """Gets the body id of the global function with the given name.
  221. Returns None if no known global exists with the given name."""
  222. if global_name in self.global_functions:
  223. return self.global_functions[global_name]
  224. else:
  225. return None
  226. def register_compiled(self, body_id, compiled_function, function_name=None):
  227. """Registers a compiled entry point with the JIT."""
  228. # Get the function's name.
  229. actual_function_name = self.generate_function_name(body_id, function_name)
  230. # Map the body id to the given parameter list.
  231. self.jitted_entry_points[body_id] = actual_function_name
  232. self.jit_globals[actual_function_name] = compiled_function
  233. if function_name is not None:
  234. self.register_global(body_id, function_name)
  235. if body_id in self.todo_entry_points:
  236. self.todo_entry_points.remove(body_id)
  237. def import_value(self, value, suggested_name=None):
  238. """Imports the given value into the JIT's global scope, with the given suggested name.
  239. The actual name of the value (within the JIT's global scope) is returned."""
  240. actual_name = self.generate_name('import', suggested_name)
  241. self.jit_globals[actual_name] = value
  242. return actual_name
  243. def __lookup_compiled_body_impl(self, body_id):
  244. """Looks up a compiled function by body id. Returns a matching function,
  245. or None if no function was found."""
  246. if body_id is not None and body_id in self.jitted_entry_points:
  247. return self.jit_globals[self.jitted_entry_points[body_id]]
  248. else:
  249. return None
  250. def __lookup_external_body_impl(self, global_name, body_id):
  251. """Looks up an external function by global name. Returns a matching function,
  252. or None if no function was found."""
  253. if self.compiled_function_lookup is not None:
  254. result = self.compiled_function_lookup(global_name)
  255. if result is not None and body_id is not None:
  256. self.register_compiled(body_id, result, global_name)
  257. return result
  258. else:
  259. return None
  260. def lookup_compiled_body(self, body_id):
  261. """Looks up a compiled function by body id. Returns a matching function,
  262. or None if no function was found."""
  263. result = self.__lookup_compiled_body_impl(body_id)
  264. if result is not None:
  265. return result
  266. else:
  267. global_name = self.get_global_name(body_id)
  268. return self.__lookup_external_body_impl(global_name, body_id)
  269. def lookup_compiled_function(self, global_name):
  270. """Looks up a compiled function by global name. Returns a matching function,
  271. or None if no function was found."""
  272. body_id = self.get_global_body_id(global_name)
  273. result = self.__lookup_compiled_body_impl(body_id)
  274. if result is not None:
  275. return result
  276. else:
  277. return self.__lookup_external_body_impl(global_name, body_id)
  278. def get_intrinsic(self, name):
  279. """Tries to find an intrinsic version of the function with the
  280. given name."""
  281. if name in self.jit_intrinsics:
  282. return self.jit_intrinsics[name]
  283. else:
  284. return None
  285. def get_cfg_intrinsic(self, name):
  286. """Tries to find an intrinsic version of the function with the
  287. given name that is specialized for CFGs."""
  288. if name in self.cfg_jit_intrinsics:
  289. return self.cfg_jit_intrinsics[name]
  290. else:
  291. return None
  292. def register_intrinsic(self, name, intrinsic_function, cfg_intrinsic_function=None):
  293. """Registers the given intrisic with the JIT. This will make the JIT replace calls to
  294. the function with the given entry point by an application of the specified function."""
  295. self.jit_intrinsics[name] = intrinsic_function
  296. if cfg_intrinsic_function is not None:
  297. self.cfg_jit_intrinsics[name] = cfg_intrinsic_function
  298. def register_binary_intrinsic(self, name, operator):
  299. """Registers an intrinsic with the JIT that represents the given binary operation."""
  300. self.register_intrinsic(
  301. name,
  302. lambda a, b:
  303. tree_ir.CreateNodeWithValueInstruction(
  304. tree_ir.BinaryInstruction(
  305. tree_ir.ReadValueInstruction(a),
  306. operator,
  307. tree_ir.ReadValueInstruction(b))),
  308. lambda original_def, a, b:
  309. original_def.redefine(
  310. cfg_ir.CreateNode(
  311. original_def.insert_before(
  312. cfg_ir.Binary(
  313. original_def.insert_before(cfg_ir.Read(a)),
  314. operator,
  315. original_def.insert_before(cfg_ir.Read(b)))))))
  316. def register_unary_intrinsic(self, name, operator):
  317. """Registers an intrinsic with the JIT that represents the given unary operation."""
  318. self.register_intrinsic(name, lambda a: tree_ir.CreateNodeWithValueInstruction(
  319. tree_ir.UnaryInstruction(
  320. operator,
  321. tree_ir.ReadValueInstruction(a))))
  322. def register_cast_intrinsic(self, name, target_type):
  323. """Registers an intrinsic with the JIT that represents a unary conversion operator."""
  324. self.register_intrinsic(name, lambda a: tree_ir.CreateNodeWithValueInstruction(
  325. tree_ir.CallInstruction(
  326. tree_ir.LoadGlobalInstruction(target_type.__name__),
  327. [tree_ir.ReadValueInstruction(a)])))
  328. def jit_signature(self, body_id):
  329. """Acquires the signature for the given body id node, which consists of the
  330. parameter variables, parameter name and a flag that tells if the given function
  331. is mutable."""
  332. if body_id not in self.jitted_parameters:
  333. signature_id, = yield [("RRD", [body_id, jit_runtime.FUNCTION_BODY_KEY])]
  334. signature_id = signature_id[0]
  335. param_set_id, is_mutable = yield [
  336. ("RD", [signature_id, "params"]),
  337. ("RD", [signature_id, jit_runtime.MUTABLE_FUNCTION_KEY])]
  338. if param_set_id is None:
  339. self.jitted_parameters[body_id] = ([], [], is_mutable)
  340. else:
  341. param_name_ids, = yield [("RDK", [param_set_id])]
  342. param_names = yield [("RV", [n]) for n in param_name_ids]
  343. param_vars = yield [("RD", [param_set_id, k]) for k in param_names]
  344. self.jitted_parameters[body_id] = (param_vars, param_names, is_mutable)
  345. raise primitive_functions.PrimitiveFinished(self.jitted_parameters[body_id])
  346. def jit_parse_bytecode(self, body_id):
  347. """Parses the given function body as a bytecode graph."""
  348. if body_id in self.bytecode_graphs:
  349. raise primitive_functions.PrimitiveFinished(self.bytecode_graphs[body_id])
  350. parser = bytecode_parser.BytecodeParser()
  351. result, = yield [("CALL_ARGS", [parser.parse_instruction, (body_id,)])]
  352. self.bytecode_graphs[body_id] = result
  353. raise primitive_functions.PrimitiveFinished(result)
  354. def check_jittable(self, body_id, suggested_name=None):
  355. """Checks if the function with the given body id is obviously non-jittable. If it's
  356. non-jittable, then a `JitCompilationFailedException` exception is thrown."""
  357. if body_id is None:
  358. raise ValueError('body_id cannot be None')
  359. elif body_id in self.jitted_entry_points:
  360. # We have already compiled this function.
  361. raise primitive_functions.PrimitiveFinished(
  362. self.jit_globals[self.jitted_entry_points[body_id]])
  363. elif body_id in self.no_jit_entry_points:
  364. # We're not allowed to jit this function or have tried and failed before.
  365. raise JitCompilationFailedException(
  366. 'Cannot jit function %s at %d because it is marked non-jittable.' % (
  367. '' if suggested_name is None else "'" + suggested_name + "'",
  368. body_id))
  369. elif not self.jit_enabled:
  370. # We're not allowed to jit anything.
  371. raise JitCompilationFailedException(
  372. 'Cannot jit function %s at %d because the JIT has been disabled.' % (
  373. '' if suggested_name is None else "'" + suggested_name + "'",
  374. body_id))
  375. def jit_recompile(self, task_root, body_id, function_name):
  376. """Replaces the function with the given name by compiling the bytecode at the given
  377. body id."""
  378. self.check_jittable(body_id, function_name)
  379. # Generate a name for the function we're about to analyze, and pretend that
  380. # it already exists. (we need to do this for recursive functions)
  381. self.jitted_entry_points[body_id] = function_name
  382. self.jit_globals[function_name] = None
  383. (_, _, is_mutable), = yield [
  384. ("CALL_ARGS", [self.jit_signature, (body_id,)])]
  385. dependencies = set([body_id])
  386. self.compilation_dependencies[body_id] = dependencies
  387. def handle_jit_exception(exception):
  388. # If analysis fails, then a JitCompilationFailedException will be thrown.
  389. del self.compilation_dependencies[body_id]
  390. for dep in dependencies:
  391. self.mark_no_jit(dep)
  392. if dep in self.jitted_entry_points:
  393. del self.jitted_entry_points[dep]
  394. failure_message = "%s (function '%s' at %d)" % (
  395. exception.message, function_name, body_id)
  396. if self.jit_success_log_function is not None:
  397. self.jit_success_log_function('JIT compilation failed: %s' % failure_message)
  398. raise JitCompilationFailedException(failure_message)
  399. # Try to analyze the function's body.
  400. yield [("TRY", [])]
  401. yield [("CATCH", [JitCompilationFailedException, handle_jit_exception])]
  402. if is_mutable:
  403. # We can't just JIT mutable functions. That'd be dangerous.
  404. raise JitCompilationFailedException(
  405. "Function was marked '%s'." % jit_runtime.MUTABLE_FUNCTION_KEY)
  406. constructed_function, = yield [
  407. ("CALL_ARGS", [self.compile_function_body, (self, function_name, body_id, task_root)])]
  408. yield [("END_TRY", [])]
  409. del self.compilation_dependencies[body_id]
  410. # Convert the function definition to Python code, and compile it.
  411. compiled_function = self.jit_define_function(function_name, constructed_function)
  412. if self.jit_success_log_function is not None:
  413. self.jit_success_log_function(
  414. "JIT compilation successful: (function '%s' at %d)" % (function_name, body_id))
  415. raise primitive_functions.PrimitiveFinished(compiled_function)
  416. def jit_define_function(self, function_name, function_def):
  417. """Converts the given tree-IR function definition to Python code, defines it,
  418. and extracts the resulting function."""
  419. # The comment below makes pylint shut up about our (hopefully benign) use of exec here.
  420. # pylint: disable=I0011,W0122
  421. if self.jit_code_log_function is not None:
  422. self.jit_code_log_function(function_def)
  423. # Convert the function definition to Python code, and compile it.
  424. exec(str(function_def), self.jit_globals)
  425. # Extract the compiled function from the JIT global state.
  426. return self.jit_globals[function_name]
  427. def jit_delete_function(self, function_name):
  428. """Deletes the function with the given function name."""
  429. del self.jit_globals[function_name]
  430. def jit_compile(self, task_root, body_id, suggested_name=None):
  431. """Tries to jit the function defined by the given entry point id and parameter list."""
  432. # Generate a name for the function we're about to analyze, and pretend that
  433. # it already exists. (we need to do this for recursive functions)
  434. function_name = self.generate_function_name(body_id, suggested_name)
  435. yield [("TAIL_CALL_ARGS", [self.jit_recompile, (task_root, body_id, function_name)])]
  436. def jit_thunk(self, get_function_body, global_name=None):
  437. """Creates a thunk from the given IR tree that computes the function's body id.
  438. This thunk is a function that will invoke the function whose body id is retrieved.
  439. The thunk's name in the JIT's global context is returned."""
  440. # The general idea is to first create a function that looks a bit like this:
  441. #
  442. # def jit_get_function_body(**kwargs):
  443. # raise primitive_functions.PrimitiveFinished(<get_function_body>)
  444. #
  445. get_function_body_name = self.generate_name('get_function_body')
  446. get_function_body_func_def = create_function(
  447. get_function_body_name, [], {}, {}, tree_ir.ReturnInstruction(get_function_body))
  448. get_function_body_func = self.jit_define_function(
  449. get_function_body_name, get_function_body_func_def)
  450. # Next, we want to create a thunk that invokes said function, and then replaces itself.
  451. thunk_name = self.generate_name('thunk', global_name)
  452. def __jit_thunk(**kwargs):
  453. # Compute the body id, and delete the function that computes the body id; we won't
  454. # be needing it anymore after this call.
  455. body_id, = yield [("CALL_KWARGS", [get_function_body_func, kwargs])]
  456. self.jit_delete_function(get_function_body_name)
  457. # Try to associate the global name with the body id, if that's at all possible.
  458. if global_name is not None:
  459. self.register_global(body_id, global_name)
  460. compiled_function = self.lookup_compiled_body(body_id)
  461. if compiled_function is not None:
  462. # Replace this thunk by the compiled function.
  463. self.jit_globals[thunk_name] = compiled_function
  464. else:
  465. def __handle_jit_exception(_):
  466. # Replace this thunk by a different thunk: one that calls the interpreter
  467. # directly, without checking if the function is jittable.
  468. (_, parameter_names, _), = yield [
  469. ("CALL_ARGS", [self.jit_signature, (body_id,)])]
  470. def __interpreter_thunk(**new_kwargs):
  471. named_arg_dict = {name : new_kwargs[name] for name in parameter_names}
  472. return jit_runtime.interpret_function_body(
  473. body_id, named_arg_dict, **new_kwargs)
  474. self.jit_globals[thunk_name] = __interpreter_thunk
  475. yield [("TRY", [])]
  476. yield [("CATCH", [JitCompilationFailedException, __handle_jit_exception])]
  477. compiled_function, = yield [
  478. ("CALL_ARGS",
  479. [self.jit_recompile, (kwargs['task_root'], body_id, thunk_name)])]
  480. yield [("END_TRY", [])]
  481. # Call the compiled function.
  482. yield [("TAIL_CALL_KWARGS", [compiled_function, kwargs])]
  483. self.jit_globals[thunk_name] = __jit_thunk
  484. return thunk_name
  485. def jit_thunk_constant(self, body_id):
  486. """Creates a thunk from given body id.
  487. This thunk is a function that will invoke the function whose body id is given.
  488. The thunk's name in the JIT's global context is returned."""
  489. self.lookup_compiled_body(body_id)
  490. compiled_name = self.get_compiled_name(body_id)
  491. if compiled_name is not None:
  492. # We might have compiled the function with the given body id already. In that case,
  493. # we need not bother with constructing the thunk; we can return the compiled function
  494. # right away.
  495. return compiled_name
  496. else:
  497. # Looks like we'll just have to build that thunk after all.
  498. return self.jit_thunk(tree_ir.LiteralInstruction(body_id))
  499. def jit_thunk_global(self, global_name):
  500. """Creates a thunk from given global name.
  501. This thunk is a function that will invoke the function whose body id is given.
  502. The thunk's name in the JIT's global context is returned."""
  503. # We might have compiled the function with the given name already. In that case,
  504. # we need not bother with constructing the thunk; we can return the compiled function
  505. # right away.
  506. body_id = self.get_global_body_id(global_name)
  507. if body_id is not None:
  508. self.lookup_compiled_body(body_id)
  509. compiled_name = self.get_compiled_name(body_id)
  510. if compiled_name is not None:
  511. return compiled_name
  512. # Looks like we'll just have to build that thunk after all.
  513. # We want to look up the global function like so
  514. #
  515. # _globals, = yield [("RD", [kwargs['task_root'], "globals"])]
  516. # global_var, = yield [("RD", [_globals, global_name])]
  517. # function_id, = yield [("RD", [global_var, "value"])]
  518. # body_id, = yield [("RD", [function_id, jit_runtime.FUNCTION_BODY_KEY])]
  519. #
  520. return self.jit_thunk(
  521. tree_ir.ReadDictionaryValueInstruction(
  522. tree_ir.ReadDictionaryValueInstruction(
  523. tree_ir.ReadDictionaryValueInstruction(
  524. tree_ir.ReadDictionaryValueInstruction(
  525. tree_ir.LoadIndexInstruction(
  526. tree_ir.LoadLocalInstruction(jit_runtime.KWARGS_PARAMETER_NAME),
  527. tree_ir.LiteralInstruction('task_root')),
  528. tree_ir.LiteralInstruction('globals')),
  529. tree_ir.LiteralInstruction(global_name)),
  530. tree_ir.LiteralInstruction('value')),
  531. tree_ir.LiteralInstruction(jit_runtime.FUNCTION_BODY_KEY)),
  532. global_name)
  533. def compile_function_body_baseline(jit, function_name, body_id, task_root):
  534. """Have the baseline JIT compile the function with the given name and body id."""
  535. (parameter_ids, parameter_list, _), = yield [
  536. ("CALL_ARGS", [jit.jit_signature, (body_id,)])]
  537. param_dict = dict(zip(parameter_ids, parameter_list))
  538. body_param_dict = dict(zip(parameter_ids, [p + "_ptr" for p in parameter_list]))
  539. body_bytecode, = yield [("CALL_ARGS", [jit.jit_parse_bytecode, (body_id,)])]
  540. state = bytecode_to_tree.AnalysisState(
  541. jit, body_id, task_root, body_param_dict,
  542. jit.max_instructions)
  543. constructed_body, = yield [("CALL_ARGS", [state.analyze, (body_bytecode,)])]
  544. # Optimize the function's body.
  545. constructed_body, = yield [("CALL_ARGS", [optimize_tree_ir, (constructed_body,)])]
  546. # Wrap the tree IR in a function definition.
  547. raise primitive_functions.PrimitiveFinished(
  548. create_function(
  549. function_name, parameter_list, param_dict, body_param_dict, constructed_body))
  550. def compile_function_body_fast(jit, function_name, body_id, _):
  551. """Have the fast JIT compile the function with the given name and body id."""
  552. (parameter_ids, parameter_list, _), = yield [
  553. ("CALL_ARGS", [jit.jit_signature, (body_id,)])]
  554. param_dict = dict(zip(parameter_ids, parameter_list))
  555. body_bytecode, = yield [("CALL_ARGS", [jit.jit_parse_bytecode, (body_id,)])]
  556. bytecode_analyzer = bytecode_to_cfg.AnalysisState(param_dict)
  557. bytecode_analyzer.analyze(body_bytecode)
  558. yield [
  559. ("CALL_ARGS", [cfg_optimization.optimize, (bytecode_analyzer.entry_point, jit)])]
  560. if jit.jit_code_log_function is not None:
  561. jit.jit_code_log_function(
  562. "CFG for function '%s' at '%d':\n%s" % (
  563. function_name, body_id,
  564. '\n'.join(
  565. map(
  566. str,
  567. cfg_ir.get_all_reachable_blocks(
  568. bytecode_analyzer.entry_point)))))
  569. raise primitive_functions.PrimitiveFinished(
  570. create_bare_function(
  571. function_name, parameter_list,
  572. cfg_to_tree.lower_flow_graph(bytecode_analyzer.entry_point, jit)))