jit.py 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986
  1. import math
  2. import keyword
  3. from collections import defaultdict
  4. import modelverse_kernel.primitives as primitive_functions
  5. import modelverse_jit.runtime as jit_runtime
  6. # Import JitCompilationFailedException because it used to be defined
  7. # in this module.
  8. JitCompilationFailedException = jit_runtime.JitCompilationFailedException
  9. def map_and_simplify_generator(function, instruction):
  10. """Applies the given mapping function to every instruction in the tree
  11. that has the given instruction as root, and simplifies it on-the-fly.
  12. This is at least as powerful as first mapping and then simplifying, as
  13. maps and simplifications are interspersed.
  14. This function assumes that function creates a generator that returns by
  15. raising a primitive_functions.PrimitiveFinished."""
  16. # First handle the children by mapping on them and then simplifying them.
  17. new_children = []
  18. for inst in instruction.get_children():
  19. new_inst, = yield [("CALL_ARGS", [map_and_simplify_generator, (function, inst)])]
  20. new_children.append(new_inst)
  21. # Then apply the function to the top-level node.
  22. transformed, = yield [("CALL_ARGS", [function, (instruction.create(new_children),)])]
  23. # Finally, simplify the transformed top-level node.
  24. raise primitive_functions.PrimitiveFinished(transformed.simplify_node())
  25. def expand_constant_read(instruction):
  26. """Tries to replace a read of a constant node by a literal."""
  27. if isinstance(instruction, tree_ir.ReadValueInstruction) and \
  28. isinstance(instruction.node_id, tree_ir.LiteralInstruction):
  29. val, = yield [("RV", [instruction.node_id.literal])]
  30. raise primitive_functions.PrimitiveFinished(tree_ir.LiteralInstruction(val))
  31. else:
  32. raise primitive_functions.PrimitiveFinished(instruction)
  33. def optimize_tree_ir(instruction):
  34. """Optimizes an IR tree."""
  35. return map_and_simplify_generator(expand_constant_read, instruction)
  36. def create_bare_function(function_name, parameter_list, function_body):
  37. """Creates a function definition from the given function name, parameter list
  38. and function body. No prolog is included."""
  39. # Wrap the IR in a function definition, give it a unique name.
  40. return tree_ir.DefineFunctionInstruction(
  41. function_name,
  42. parameter_list + ['**' + jit_runtime.KWARGS_PARAMETER_NAME],
  43. function_body)
  44. def create_function(
  45. function_name, parameter_list, param_dict,
  46. body_param_dict, function_body, source_map_name=None,
  47. compatible_temporary_protects=False):
  48. """Creates a function from the given function name, parameter list,
  49. variable-to-parameter name map, variable-to-local name map and
  50. function body. An optional source map can be included, too."""
  51. # Write a prologue and prepend it to the generated function body.
  52. prolog_statements = []
  53. # If the source map is not None, then we should generate a "DEBUG_INFO"
  54. # request.
  55. if source_map_name is not None:
  56. prolog_statements.append(
  57. tree_ir.RegisterDebugInfoInstruction(
  58. tree_ir.LiteralInstruction(function_name),
  59. tree_ir.LoadGlobalInstruction(source_map_name),
  60. tree_ir.LiteralInstruction(jit_runtime.BASELINE_JIT_ORIGIN_NAME)))
  61. # Create a LOCALS_NODE_NAME node, and connect it to the user root.
  62. prolog_statements.append(
  63. tree_ir.create_new_local_node(
  64. jit_runtime.LOCALS_NODE_NAME,
  65. tree_ir.LoadIndexInstruction(
  66. tree_ir.LoadLocalInstruction(jit_runtime.KWARGS_PARAMETER_NAME),
  67. tree_ir.LiteralInstruction('task_root')),
  68. jit_runtime.LOCALS_EDGE_NAME))
  69. for (key, val) in list(param_dict.items()):
  70. arg_ptr = tree_ir.create_new_local_node(
  71. body_param_dict[key],
  72. tree_ir.LoadLocalInstruction(jit_runtime.LOCALS_NODE_NAME))
  73. prolog_statements.append(arg_ptr)
  74. prolog_statements.append(
  75. tree_ir.CreateDictionaryEdgeInstruction(
  76. tree_ir.LoadLocalInstruction(body_param_dict[key]),
  77. tree_ir.LiteralInstruction('value'),
  78. tree_ir.LoadLocalInstruction(val)))
  79. constructed_body = tree_ir.create_block(
  80. *(prolog_statements + [function_body]))
  81. # Shield temporaries from the GC.
  82. constructed_body = tree_ir.protect_temporaries_from_gc(
  83. constructed_body,
  84. tree_ir.LoadLocalInstruction(jit_runtime.LOCALS_NODE_NAME),
  85. compatible_temporary_protects)
  86. return create_bare_function(function_name, parameter_list, constructed_body)
  87. def print_value(val):
  88. """A thin wrapper around 'print'."""
  89. print(val)
  90. class ModelverseJit(object):
  91. """A high-level interface to the modelverse JIT compiler."""
  92. def __init__(self, max_instructions=None, compiled_function_lookup=None):
  93. self.todo_entry_points = set()
  94. self.no_jit_entry_points = set()
  95. self.jitted_parameters = {}
  96. self.jit_globals = {
  97. 'PrimitiveFinished' : primitive_functions.PrimitiveFinished,
  98. jit_runtime.CALL_FUNCTION_NAME : jit_runtime.call_function,
  99. jit_runtime.GET_INPUT_FUNCTION_NAME : jit_runtime.get_input,
  100. jit_runtime.JIT_THUNK_CONSTANT_FUNCTION_NAME : self.jit_thunk_constant_function,
  101. jit_runtime.JIT_THUNK_GLOBAL_FUNCTION_NAME : self.jit_thunk_global,
  102. jit_runtime.JIT_REJIT_FUNCTION_NAME : self.jit_rejit,
  103. jit_runtime.JIT_COMPILE_FUNCTION_BODY_FAST_FUNCTION_NAME : compile_function_body_fast,
  104. jit_runtime.UNREACHABLE_FUNCTION_NAME : jit_runtime.unreachable
  105. }
  106. # jitted_entry_points maps body ids to values in jit_globals.
  107. self.jitted_entry_points = {}
  108. # global_functions maps global value names to body ids.
  109. self.global_functions = {}
  110. # global_functions_inv maps body ids to global value names.
  111. self.global_functions_inv = {}
  112. # bytecode_graphs maps body ids to their parsed bytecode graphs.
  113. self.bytecode_graphs = {}
  114. # jitted_function_aliases maps body ids to known aliases.
  115. self.jitted_function_aliases = defaultdict(set)
  116. self.jit_count = 0
  117. self.max_instructions = max_instructions
  118. self.compiled_function_lookup = compiled_function_lookup
  119. self.compilation_dependencies = {}
  120. self.jit_enabled = True
  121. self.direct_calls_allowed = True
  122. self.tracing_enabled = False
  123. self.source_maps_enabled = True
  124. self.input_function_enabled = False
  125. self.nop_insertion_enabled = True
  126. self.thunks_enabled = True
  127. self.jit_success_log_function = None
  128. self.jit_code_log_function = None
  129. self.compile_function_body = compile_function_body_baseline
  130. def set_jit_enabled(self, is_enabled=True):
  131. """Enables or disables the JIT."""
  132. self.jit_enabled = is_enabled
  133. def allow_direct_calls(self, is_allowed=True):
  134. """Allows or disallows direct calls from jitted to jitted code."""
  135. self.direct_calls_allowed = is_allowed
  136. def use_input_function(self, is_enabled=True):
  137. """Configures the JIT to compile 'input' instructions as function calls."""
  138. self.input_function_enabled = is_enabled
  139. def enable_tracing(self, is_enabled=True):
  140. """Enables or disables tracing for jitted code."""
  141. self.tracing_enabled = is_enabled
  142. def enable_source_maps(self, is_enabled=True):
  143. """Enables or disables the creation of source maps for jitted code. Source maps
  144. convert lines in the generated code to debug information.
  145. Source maps are enabled by default."""
  146. self.source_maps_enabled = is_enabled
  147. def enable_nop_insertion(self, is_enabled=True):
  148. """Enables or disables nop insertion for jitted code. If enabled, the JIT will
  149. insert nops at loop back-edges. Inserting nops sacrifices performance to
  150. keep the jitted code from blocking the thread of execution and consuming
  151. all resources; nops give the Modelverse server an opportunity to interrupt
  152. the currently running code."""
  153. self.nop_insertion_enabled = is_enabled
  154. def enable_thunks(self, is_enabled=True):
  155. """Enables or disables thunks for jitted code. Thunks delay the compilation of
  156. functions until they are actually used. Thunks generally reduce start-up
  157. time.
  158. Thunks are enabled by default."""
  159. self.thunks_enabled = is_enabled
  160. def set_jit_success_log(self, log_function=print_value):
  161. """Configures this JIT instance with a function that prints output to a log.
  162. Success and failure messages for specific functions are then sent to said log."""
  163. self.jit_success_log_function = log_function
  164. def set_jit_code_log(self, log_function=print_value):
  165. """Configures this JIT instance with a function that prints output to a log.
  166. Function definitions of jitted functions are then sent to said log."""
  167. self.jit_code_log_function = log_function
  168. def set_function_body_compiler(self, compile_function_body):
  169. """Sets the function that the JIT uses to compile function bodies."""
  170. self.compile_function_body = compile_function_body
  171. def mark_entry_point(self, body_id):
  172. """Marks the node with the given identifier as a function entry point."""
  173. if body_id not in self.no_jit_entry_points and body_id not in self.jitted_entry_points:
  174. self.todo_entry_points.add(body_id)
  175. def is_entry_point(self, body_id):
  176. """Tells if the node with the given identifier is a function entry point."""
  177. return body_id in self.todo_entry_points or \
  178. body_id in self.no_jit_entry_points or \
  179. body_id in self.jitted_entry_points
  180. def is_jittable_entry_point(self, body_id):
  181. """Tells if the node with the given identifier is a function entry point that
  182. has not been marked as non-jittable. This only returns `True` if the JIT
  183. is enabled and the function entry point has been marked jittable, or if
  184. the function has already been compiled."""
  185. return ((self.jit_enabled and body_id in self.todo_entry_points) or
  186. self.has_compiled(body_id))
  187. def has_compiled(self, body_id):
  188. """Tests if the function belonging to the given body node has been compiled yet."""
  189. return body_id in self.jitted_entry_points
  190. def get_compiled_name(self, body_id):
  191. """Gets the name of the compiled version of the given body node in the JIT
  192. global state."""
  193. if body_id in self.jitted_entry_points:
  194. return self.jitted_entry_points[body_id]
  195. else:
  196. return None
  197. def mark_no_jit(self, body_id):
  198. """Informs the JIT that the node with the given identifier is a function entry
  199. point that must never be jitted."""
  200. self.no_jit_entry_points.add(body_id)
  201. if body_id in self.todo_entry_points:
  202. self.todo_entry_points.remove(body_id)
  203. def generate_name(self, infix, suggested_name=None):
  204. """Generates a new name or picks the suggested name if it is still
  205. available."""
  206. if suggested_name is not None \
  207. and suggested_name not in self.jit_globals \
  208. and not keyword.iskeyword(suggested_name):
  209. self.jit_count += 1
  210. return suggested_name
  211. else:
  212. function_name = 'jit_%s%d' % (infix, self.jit_count)
  213. self.jit_count += 1
  214. return function_name
  215. def generate_function_name(self, body_id, suggested_name=None):
  216. """Generates a new function name or picks the suggested name if it is still
  217. available."""
  218. if suggested_name is None:
  219. suggested_name = self.get_global_name(body_id)
  220. return self.generate_name('func', suggested_name)
  221. def register_global(self, body_id, global_name):
  222. """Associates the given body id with the given global name."""
  223. self.global_functions[global_name] = body_id
  224. self.global_functions_inv[body_id] = global_name
  225. def get_global_name(self, body_id):
  226. """Gets the name of the global function with the given body id.
  227. Returns None if no known global exists with the given id."""
  228. if body_id in self.global_functions_inv:
  229. return self.global_functions_inv[body_id]
  230. else:
  231. return None
  232. def get_global_body_id(self, global_name):
  233. """Gets the body id of the global function with the given name.
  234. Returns None if no known global exists with the given name."""
  235. if global_name in self.global_functions:
  236. return self.global_functions[global_name]
  237. else:
  238. return None
  239. def register_compiled(self, body_id, compiled_function, function_name=None):
  240. """Registers a compiled entry point with the JIT."""
  241. # Get the function's name.
  242. actual_function_name = self.generate_function_name(body_id,
  243. function_name)
  244. # Map the body id to the given parameter list.
  245. self.jitted_entry_points[body_id] = actual_function_name
  246. self.jit_globals[actual_function_name] = compiled_function
  247. if function_name is not None:
  248. self.register_global(body_id, function_name)
  249. if body_id in self.todo_entry_points:
  250. self.todo_entry_points.remove(body_id)
  251. def import_value(self, value, suggested_name=None):
  252. """Imports the given value into the JIT's global scope, with the given suggested name.
  253. The actual name of the value (within the JIT's global scope) is returned."""
  254. actual_name = self.generate_name('import', suggested_name)
  255. self.jit_globals[actual_name] = value
  256. return actual_name
  257. def __lookup_compiled_body_impl(self, body_id):
  258. """Looks up a compiled function by body id. Returns a matching function,
  259. or None if no function was found."""
  260. if body_id is not None and body_id in self.jitted_entry_points:
  261. return self.jit_globals[self.jitted_entry_points[body_id]]
  262. else:
  263. return None
  264. def __lookup_external_body_impl(self, global_name, body_id):
  265. """Looks up an external function by global name. Returns a matching function,
  266. or None if no function was found."""
  267. if global_name is not None and self.compiled_function_lookup is not None:
  268. result = self.compiled_function_lookup(global_name)
  269. if result is not None and body_id is not None:
  270. self.register_compiled(body_id, result, global_name)
  271. return result
  272. else:
  273. return None
  274. def lookup_compiled_body(self, body_id):
  275. """Looks up a compiled function by body id. Returns a matching function,
  276. or None if no function was found."""
  277. result = self.__lookup_compiled_body_impl(body_id)
  278. if result is not None:
  279. return result
  280. else:
  281. global_name = self.get_global_name(body_id)
  282. return self.__lookup_external_body_impl(global_name, body_id)
  283. def lookup_compiled_function(self, global_name):
  284. """Looks up a compiled function by global name. Returns a matching function,
  285. or None if no function was found."""
  286. body_id = self.get_global_body_id(global_name)
  287. result = self.__lookup_compiled_body_impl(body_id)
  288. if result is not None:
  289. return result
  290. else:
  291. return self.__lookup_external_body_impl(global_name, body_id)
  292. def jit_signature(self, body_id):
  293. """Acquires the signature for the given body id node, which consists of the
  294. parameter variables, parameter name and a flag that tells if the given function
  295. is mutable."""
  296. if body_id not in self.jitted_parameters:
  297. signature_id, = yield [("RRD", [body_id, jit_runtime.FUNCTION_BODY_KEY])]
  298. signature_id = signature_id[0]
  299. param_set_id, is_mutable = yield [
  300. ("RD", [signature_id, "params"]),
  301. ("RD", [signature_id, jit_runtime.MUTABLE_FUNCTION_KEY])]
  302. if param_set_id is None:
  303. self.jitted_parameters[body_id] = ([], [], is_mutable)
  304. else:
  305. param_name_ids, = yield [("RDK", [param_set_id])]
  306. param_names = yield [("RV", [n]) for n in param_name_ids]
  307. #NOTE Patch up strange links...
  308. param_names = [i for i in param_names if i is not None]
  309. param_vars = yield [("RD", [param_set_id, k]) for k in param_names]
  310. #NOTE that variables might not be in the correct order, as we just read them out!
  311. lst = sorted([(name, var) for name, var in zip(param_names, param_vars)])
  312. param_vars = [i[1] for i in lst]
  313. param_names = [i[0] for i in lst]
  314. self.jitted_parameters[body_id] = (param_vars, param_names, is_mutable)
  315. raise primitive_functions.PrimitiveFinished(self.jitted_parameters[body_id])
  316. def jit_parse_bytecode(self, body_id):
  317. """Parses the given function body as a bytecode graph."""
  318. if body_id in self.bytecode_graphs:
  319. raise primitive_functions.PrimitiveFinished(self.bytecode_graphs[body_id])
  320. parser = bytecode_parser.BytecodeParser()
  321. result, = yield [("CALL_ARGS", [parser.parse_instruction, (body_id,)])]
  322. self.bytecode_graphs[body_id] = result
  323. raise primitive_functions.PrimitiveFinished(result)
  324. def check_jittable(self, body_id, suggested_name=None):
  325. """Checks if the function with the given body id is obviously non-jittable. If it's
  326. non-jittable, then a `JitCompilationFailedException` exception is thrown."""
  327. if body_id is None:
  328. raise ValueError('body_id cannot be None: ' + suggested_name)
  329. elif body_id in self.no_jit_entry_points:
  330. # We're not allowed to jit this function or have tried and failed before.
  331. raise JitCompilationFailedException(
  332. 'Cannot jit function %s at %d because it is marked non-jittable.' % (
  333. '' if suggested_name is None else "'" + suggested_name + "'",
  334. body_id))
  335. elif not self.jit_enabled:
  336. # We're not allowed to jit anything.
  337. raise JitCompilationFailedException(
  338. 'Cannot jit function %s at %d because the JIT has been disabled.' % (
  339. '' if suggested_name is None else "'" + suggested_name + "'",
  340. body_id))
  341. def jit_recompile(self, task_root, body_id, function_name, compile_function_body=None):
  342. """Replaces the function with the given name by compiling the bytecode at the given
  343. body id."""
  344. if compile_function_body is None:
  345. compile_function_body = self.compile_function_body
  346. self.check_jittable(body_id, function_name)
  347. # Generate a name for the function we're about to analyze, and pretend that
  348. # it already exists. (we need to do this for recursive functions)
  349. self.jitted_entry_points[body_id] = function_name
  350. self.jit_globals[function_name] = None
  351. (_, _, is_mutable), = yield [
  352. ("CALL_ARGS", [self.jit_signature, (body_id,)])]
  353. dependencies = set([body_id])
  354. self.compilation_dependencies[body_id] = dependencies
  355. def handle_jit_exception(exception):
  356. # If analysis fails, then a JitCompilationFailedException will be thrown.
  357. print("EXCEPTION with mutable")
  358. del self.compilation_dependencies[body_id]
  359. for dep in dependencies:
  360. self.mark_no_jit(dep)
  361. if dep in self.jitted_entry_points:
  362. del self.jitted_entry_points[dep]
  363. failure_message = "%s (function '%s' at %d)" % (
  364. str(exception), function_name, body_id)
  365. if self.jit_success_log_function is not None:
  366. self.jit_success_log_function('JIT compilation failed: %s' % failure_message)
  367. raise JitCompilationFailedException(failure_message)
  368. # Try to analyze the function's body.
  369. yield [("TRY", [])]
  370. yield [("CATCH", [JitCompilationFailedException, handle_jit_exception])]
  371. if is_mutable:
  372. # We can't just JIT mutable functions. That'd be dangerous.
  373. raise JitCompilationFailedException(
  374. "Function was marked '%s'." % jit_runtime.MUTABLE_FUNCTION_KEY)
  375. compiled_function, = yield [
  376. ("CALL_ARGS", [compile_function_body, (self, function_name, body_id, task_root)])]
  377. yield [("END_TRY", [])]
  378. del self.compilation_dependencies[body_id]
  379. if self.jit_success_log_function is not None:
  380. assert self.jitted_entry_points[body_id] == function_name
  381. self.jit_success_log_function(
  382. "JIT compilation successful: (function '%s' at %d)" % (function_name, body_id))
  383. raise primitive_functions.PrimitiveFinished(compiled_function)
  384. def get_source_map_name(self, function_name):
  385. """Gets the name of the given jitted function's source map. None is returned if source maps
  386. are disabled."""
  387. if self.source_maps_enabled:
  388. return function_name + "_source_map"
  389. else:
  390. return None
  391. def get_can_rejit_name(self, function_name):
  392. """Gets the name of the given jitted function's can-rejit flag."""
  393. return function_name + "_can_rejit"
  394. def jit_define_function(self, function_name, function_def):
  395. """Converts the given tree-IR function definition to Python code, defines it,
  396. and extracts the resulting function."""
  397. # The comment below makes pylint shut up about our (hopefully benign) use of exec here.
  398. # pylint: disable=I0011,W0122
  399. if self.jit_code_log_function is not None:
  400. self.jit_code_log_function(function_def)
  401. # Convert the function definition to Python code, and compile it.
  402. code_generator = tree_ir.PythonGenerator()
  403. function_def.generate_python_def(code_generator)
  404. source_map_name = self.get_source_map_name(function_name)
  405. if source_map_name is not None:
  406. self.jit_globals[source_map_name] = code_generator.source_map_builder.source_map
  407. exec(str(code_generator), self.jit_globals)
  408. # Extract the compiled function from the JIT global state.
  409. return self.jit_globals[function_name]
  410. def jit_delete_function(self, function_name):
  411. """Deletes the function with the given function name."""
  412. del self.jit_globals[function_name]
  413. def jit_compile(self, task_root, body_id, suggested_name=None):
  414. """Tries to jit the function defined by the given entry point id and parameter list."""
  415. if body_id is None:
  416. raise ValueError('body_id cannot be None: ' + str(suggested_name))
  417. elif body_id in self.jitted_entry_points:
  418. raise primitive_functions.PrimitiveFinished(
  419. self.jit_globals[self.jitted_entry_points[body_id]])
  420. compiled_func = self.lookup_compiled_body(body_id)
  421. if compiled_func is not None:
  422. raise primitive_functions.PrimitiveFinished(compiled_func)
  423. # Generate a name for the function we're about to analyze, and 're-compile'
  424. # it for the first time.
  425. function_name = self.generate_function_name(body_id, suggested_name)
  426. yield [("TAIL_CALL_ARGS", [self.jit_recompile, (task_root, body_id, function_name)])]
  427. def jit_rejit(self, task_root, body_id, function_name, compile_function_body=None):
  428. """Re-compiles the given function. If compilation fails, then the can-rejit
  429. flag is set to false."""
  430. old_jitted_func = self.jitted_entry_points[body_id]
  431. def __handle_jit_failed(_):
  432. self.jit_globals[self.get_can_rejit_name(function_name)] = False
  433. self.jitted_entry_points[body_id] = old_jitted_func
  434. self.no_jit_entry_points.remove(body_id)
  435. raise primitive_functions.PrimitiveFinished(None)
  436. yield [("TRY", [])]
  437. yield [("CATCH", [jit_runtime.JitCompilationFailedException, __handle_jit_failed])]
  438. jitted_function, = yield [
  439. ("CALL_ARGS",
  440. [self.jit_recompile, (task_root, body_id, function_name, compile_function_body)])]
  441. yield [("END_TRY", [])]
  442. # Update all aliases.
  443. for function_alias in self.jitted_function_aliases[body_id]:
  444. self.jit_globals[function_alias] = jitted_function
  445. def jit_thunk(self, get_function_body, global_name=None):
  446. """Creates a thunk from the given IR tree that computes the function's body id.
  447. This thunk is a function that will invoke the function whose body id is retrieved.
  448. The thunk's name in the JIT's global context is returned."""
  449. # The general idea is to first create a function that looks a bit like this:
  450. #
  451. # def jit_get_function_body(**kwargs):
  452. # raise primitive_functions.PrimitiveFinished(<get_function_body>)
  453. #
  454. get_function_body_name = self.generate_name('get_function_body')
  455. get_function_body_func_def = create_function(
  456. get_function_body_name, [], {}, {}, tree_ir.ReturnInstruction(get_function_body))
  457. get_function_body_func = self.jit_define_function(
  458. get_function_body_name, get_function_body_func_def)
  459. # Next, we want to create a thunk that invokes said function, and then replaces itself.
  460. thunk_name = self.generate_name('thunk', global_name)
  461. def __jit_thunk(**kwargs):
  462. # Compute the body id, and delete the function that computes the body id; we won't
  463. # be needing it anymore after this call.
  464. body_id, = yield [("CALL_KWARGS", [get_function_body_func, kwargs])]
  465. self.jit_delete_function(get_function_body_name)
  466. # Try to associate the global name with the body id, if that's at all possible.
  467. if global_name is not None:
  468. self.register_global(body_id, global_name)
  469. compiled_function = self.lookup_compiled_body(body_id)
  470. if compiled_function is not None:
  471. # Replace this thunk by the compiled function.
  472. self.jit_globals[thunk_name] = compiled_function
  473. self.jitted_function_aliases[body_id].add(thunk_name)
  474. else:
  475. def __handle_jit_exception(_):
  476. # Replace this thunk by a different thunk: one that calls the interpreter
  477. # directly, without checking if the function is jittable.
  478. (_, parameter_names, _), = yield [
  479. ("CALL_ARGS", [self.jit_signature, (body_id,)])]
  480. def __interpreter_thunk(**new_kwargs):
  481. named_arg_dict = {name : new_kwargs[name] for name in parameter_names}
  482. return jit_runtime.interpret_function_body(
  483. body_id, named_arg_dict, **new_kwargs)
  484. self.jit_globals[thunk_name] = __interpreter_thunk
  485. yield [("TRY", [])]
  486. yield [("CATCH", [JitCompilationFailedException, __handle_jit_exception])]
  487. compiled_function, = yield [
  488. ("CALL_ARGS",
  489. [self.jit_recompile, (kwargs['task_root'], body_id, thunk_name)])]
  490. yield [("END_TRY", [])]
  491. # Call the compiled function.
  492. yield [("TAIL_CALL_KWARGS", [compiled_function, kwargs])]
  493. self.jit_globals[thunk_name] = __jit_thunk
  494. return thunk_name
  495. def jit_thunk_constant_body(self, body_id):
  496. """Creates a thunk from the given body id.
  497. This thunk is a function that will invoke the function whose body id is given.
  498. The thunk's name in the JIT's global context is returned."""
  499. self.lookup_compiled_body(body_id)
  500. compiled_name = self.get_compiled_name(body_id)
  501. if compiled_name is not None:
  502. # We might have compiled the function with the given body id already. In that case,
  503. # we need not bother with constructing the thunk; we can return the compiled function
  504. # right away.
  505. return compiled_name
  506. else:
  507. # Looks like we'll just have to build that thunk after all.
  508. return self.jit_thunk(tree_ir.LiteralInstruction(body_id))
  509. def jit_thunk_constant_function(self, body_id):
  510. """Creates a thunk from the given function id.
  511. This thunk is a function that will invoke the function whose function id is given.
  512. The thunk's name in the JIT's global context is returned."""
  513. return self.jit_thunk(
  514. tree_ir.ReadDictionaryValueInstruction(
  515. tree_ir.LiteralInstruction(body_id),
  516. tree_ir.LiteralInstruction(jit_runtime.FUNCTION_BODY_KEY)))
  517. def jit_thunk_global(self, global_name):
  518. """Creates a thunk from given global name.
  519. This thunk is a function that will invoke the function whose body id is given.
  520. The thunk's name in the JIT's global context is returned."""
  521. # We might have compiled the function with the given name already. In that case,
  522. # we need not bother with constructing the thunk; we can return the compiled function
  523. # right away.
  524. body_id = self.get_global_body_id(global_name)
  525. if body_id is not None:
  526. self.lookup_compiled_body(body_id)
  527. compiled_name = self.get_compiled_name(body_id)
  528. if compiled_name is not None:
  529. return compiled_name
  530. # Looks like we'll just have to build that thunk after all.
  531. # We want to look up the global function like so
  532. #
  533. # _globals, = yield [("RD", [kwargs['task_root'], "globals"])]
  534. # global_var, = yield [("RD", [_globals, global_name])]
  535. # function_id, = yield [("RD", [global_var, "value"])]
  536. # body_id, = yield [("RD", [function_id, jit_runtime.FUNCTION_BODY_KEY])]
  537. #
  538. return self.jit_thunk(
  539. tree_ir.ReadDictionaryValueInstruction(
  540. tree_ir.ReadDictionaryValueInstruction(
  541. tree_ir.ReadDictionaryValueInstruction(
  542. tree_ir.ReadDictionaryValueInstruction(
  543. tree_ir.LoadIndexInstruction(
  544. tree_ir.LoadLocalInstruction(jit_runtime.KWARGS_PARAMETER_NAME),
  545. tree_ir.LiteralInstruction('task_root')),
  546. tree_ir.LiteralInstruction('globals')),
  547. tree_ir.LiteralInstruction(global_name)),
  548. tree_ir.LiteralInstruction('value')),
  549. tree_ir.LiteralInstruction(jit_runtime.FUNCTION_BODY_KEY)),
  550. global_name)
  551. def new_compile(self, body_id):
  552. print("Compiling body ID " + str(body_id))
  553. raise JitCompilationFailedException("Function was marked '%s'." % jit_runtime.MUTABLE_FUNCTION_KEY)
  554. #raise primitive_functions.PrimitiveFinished("pass")
  555. def compile_function_body_interpret(jit, function_name, body_id, task_root, header=None):
  556. print("INTERPRET")
  557. """Create a function that invokes the interpreter on the given function."""
  558. (parameter_ids, parameter_list, _), = yield [
  559. ("CALL_ARGS", [jit.jit_signature, (body_id,)])]
  560. param_dict = dict(list(zip(parameter_ids, parameter_list)))
  561. body_bytecode, = yield [("CALL_ARGS", [jit.jit_parse_bytecode, (body_id,)])]
  562. def __interpret_function(**kwargs):
  563. if header is not None:
  564. (done, result), = yield [("CALL_KWARGS", [header, kwargs])]
  565. if done:
  566. raise primitive_functions.PrimitiveFinished(result)
  567. local_args = {}
  568. inner_kwargs = dict(kwargs)
  569. for param_id, name in list(param_dict.items()):
  570. local_args[param_id] = inner_kwargs[name]
  571. del inner_kwargs[name]
  572. yield [("TAIL_CALL_ARGS",
  573. [bytecode_interpreter.interpret_bytecode_function,
  574. (function_name, body_bytecode, local_args, inner_kwargs)])]
  575. jit.jit_globals[function_name] = __interpret_function
  576. raise primitive_functions.PrimitiveFinished(__interpret_function)
  577. def compile_function_body_baseline(
  578. jit, function_name, body_id, task_root,
  579. header=None, compatible_temporary_protects=False):
  580. print("BASELINE")
  581. """Have the baseline JIT compile the function with the given name and body id."""
  582. (parameter_ids, parameter_list, _), = yield [
  583. ("CALL_ARGS", [jit.jit_signature, (body_id,)])]
  584. if None in parameter_list:
  585. raise JitCompilationFailedException('JIT exception...')
  586. param_dict = dict(list(zip(parameter_ids, parameter_list)))
  587. body_param_dict = dict(list(zip(parameter_ids, [p + "_ptr" for p in parameter_list])))
  588. body_bytecode, = yield [("CALL_ARGS", [jit.jit_parse_bytecode, (body_id,)])]
  589. state = bytecode_to_tree.AnalysisState(
  590. jit, body_id, task_root, body_param_dict,
  591. jit.max_instructions)
  592. constructed_body, = yield [("CALL_ARGS", [state.analyze, (body_bytecode,)])]
  593. if header is not None:
  594. constructed_body = tree_ir.create_block(header, constructed_body)
  595. # Optimize the function's body.
  596. constructed_body, = yield [("CALL_ARGS", [optimize_tree_ir, (constructed_body,)])]
  597. # Wrap the tree IR in a function definition.
  598. constructed_function = create_function(
  599. function_name, parameter_list, param_dict,
  600. body_param_dict, constructed_body, jit.get_source_map_name(function_name),
  601. compatible_temporary_protects)
  602. # Convert the function definition to Python code, and compile it.
  603. raise primitive_functions.PrimitiveFinished(
  604. jit.jit_define_function(function_name, constructed_function))
  605. def compile_function_body_fast(jit, function_name, body_id, _):
  606. print("FAST2")
  607. """Have the fast JIT compile the function with the given name and body id."""
  608. (parameter_ids, parameter_list, _), = yield [
  609. ("CALL_ARGS", [jit.jit_signature, (body_id,)])]
  610. param_dict = dict(list(zip(parameter_ids, parameter_list)))
  611. print("Got body bytecode: " + str(body_id))
  612. constructed_body, = yield [("CALL_ARGS", [jit.new_compile, (body_id,)])]
  613. print("Constructed body:")
  614. print(constructed_body)
  615. constructed_function = create_bare_function(function_name, parameter_list, constructed_body)
  616. # Convert the function definition to Python code, and compile it.
  617. raise primitive_functions.PrimitiveFinished(
  618. jit.jit_define_function(function_name, constructed_function))
  619. def compile_function_body_fast_original(jit, function_name, body_id, _):
  620. print("FAST_ORIGINAL")
  621. """Have the fast JIT compile the function with the given name and body id."""
  622. (parameter_ids, parameter_list, _), = yield [
  623. ("CALL_ARGS", [jit.jit_signature, (body_id,)])]
  624. param_dict = dict(list(zip(parameter_ids, parameter_list)))
  625. if None in param_dict:
  626. del param_dict[None]
  627. print("REMOVING NONE IN PARAMETERS for ")
  628. print(locals())
  629. body_bytecode, = yield [("CALL_ARGS", [jit.jit_parse_bytecode, (body_id,)])]
  630. bytecode_analyzer = bytecode_to_cfg.AnalysisState(jit, function_name, param_dict)
  631. bytecode_analyzer.analyze(body_bytecode)
  632. entry_point, = yield [
  633. ("CALL_ARGS", [cfg_optimization.optimize, (bytecode_analyzer.entry_point, jit)])]
  634. if jit.jit_code_log_function is not None:
  635. jit.jit_code_log_function(
  636. "CFG for function '%s' at '%d':\n%s" % (
  637. function_name, body_id,
  638. '\n'.join(map(str, cfg_ir.get_all_reachable_blocks(entry_point)))))
  639. # Lower the CFG to tree IR.
  640. constructed_body = cfg_to_tree.lower_flow_graph(entry_point, jit)
  641. # Optimize the tree that was generated.
  642. constructed_body, = yield [("CALL_ARGS", [optimize_tree_ir, (constructed_body,)])]
  643. print("OUTPUT: " + str(constructed_body))
  644. constructed_function = create_bare_function(function_name, parameter_list, constructed_body)
  645. # Convert the function definition to Python code, and compile it.
  646. raise primitive_functions.PrimitiveFinished(
  647. jit.jit_define_function(function_name, constructed_function))
  648. def favor_large_functions(body_bytecode):
  649. """Computes the initial temperature of a function based on the size of
  650. its body bytecode. Larger functions are favored and the temperature
  651. is incremented by one on every call."""
  652. # The rationale for this heuristic is that it does some damage control:
  653. # we can afford to decide (wrongly) not to fast-jit a small function,
  654. # because we can just fast-jit that function later on. Since the function
  655. # is so small, it will (hopefully) not be able to deal us a heavy blow in
  656. # terms of performance.
  657. #
  658. # If we decide not to fast-jit a large function however, we might end up
  659. # in a situation where said function runs for a long time before we
  660. # realize that we really should have jitted it. And that's exactly what
  661. # this heuristic tries to avoid.
  662. return len(body_bytecode.get_reachable()), 1
  663. def favor_small_functions(body_bytecode):
  664. """Computes the initial temperature of a function based on the size of
  665. its body bytecode. Smaller functions are favored and the temperature
  666. is incremented by one on every call."""
  667. # The rationale for this heuristic is that small functions are easy to
  668. # fast-jit, because they probably won't trigger the non-linear complexity
  669. # of fast-jit's algorithms. So it might be cheaper to fast-jit small
  670. # functions and get a performance boost from that than to fast-jit large
  671. # functions.
  672. return ADAPTIVE_FAST_JIT_TEMPERATURE_THRESHOLD - len(body_bytecode.get_reachable()), 1
  673. ADAPTIVE_JIT_LOOP_INSTRUCTION_MULTIPLIER = 4
  674. #ADAPTIVE_BASELINE_JIT_TEMPERATURE_THRESHOLD = 100
  675. # TODO bug in the ByteCode Interpreter results in erroneous execution with random jumps; disable for now!
  676. ADAPTIVE_BASELINE_JIT_TEMPERATURE_THRESHOLD = -float('inf')
  677. """The threshold temperature at which the adaptive JIT will use the baseline JIT."""
  678. ADAPTIVE_FAST_JIT_TEMPERATURE_THRESHOLD = 250
  679. """The threshold temperature at which the adaptive JIT will use the fast JIT."""
  680. def favor_loops(body_bytecode):
  681. """Computes the initial temperature of a function. Code within a loop makes
  682. the function hotter; code outside loops makes the function colder. The
  683. temperature is incremented by one on every call."""
  684. reachable_instructions = body_bytecode.get_reachable()
  685. # First set the temperature to the negative number of instructions.
  686. temperature = ADAPTIVE_BASELINE_JIT_TEMPERATURE_THRESHOLD - len(reachable_instructions)
  687. for instruction in reachable_instructions:
  688. if isinstance(instruction, bytecode_ir.WhileInstruction):
  689. # Then increase the temperature by the number of instructions reachable
  690. # from loop bodies. Note that the algorithm will count nested loops twice.
  691. # This is actually by design.
  692. loop_body_instructions = instruction.body.get_reachable(
  693. lambda x: not isinstance(
  694. x, (bytecode_ir.BreakInstruction, bytecode_ir.ContinueInstruction)))
  695. temperature += ADAPTIVE_JIT_LOOP_INSTRUCTION_MULTIPLIER * len(loop_body_instructions)
  696. return temperature, 1
  697. def favor_small_loops(body_bytecode):
  698. """Computes the initial temperature of a function. Code within a loop makes
  699. the function hotter; code outside loops makes the function colder. The
  700. temperature is incremented by one on every call."""
  701. reachable_instructions = body_bytecode.get_reachable()
  702. # First set the temperature to the negative number of instructions.
  703. temperature = ADAPTIVE_FAST_JIT_TEMPERATURE_THRESHOLD - 50 - len(reachable_instructions)
  704. for instruction in reachable_instructions:
  705. if isinstance(instruction, bytecode_ir.WhileInstruction):
  706. # Then increase the temperature by the number of instructions reachable
  707. # from loop bodies. Note that the algorithm will count nested loops twice.
  708. # This is actually by design.
  709. loop_body_instructions = instruction.body.get_reachable(
  710. lambda x: not isinstance(
  711. x, (bytecode_ir.BreakInstruction, bytecode_ir.ContinueInstruction)))
  712. temperature += (
  713. (ADAPTIVE_JIT_LOOP_INSTRUCTION_MULTIPLIER ** 2) *
  714. int(math.sqrt(len(loop_body_instructions))))
  715. return temperature, max(int(math.log(len(reachable_instructions), 2)), 1)
  716. class AdaptiveJitState(object):
  717. """Shared state for adaptive JIT compilation."""
  718. def __init__(
  719. self, temperature_counter_name,
  720. temperature_increment, can_rejit_name):
  721. self.temperature_counter_name = temperature_counter_name
  722. self.temperature_increment = temperature_increment
  723. self.can_rejit_name = can_rejit_name
  724. def compile_interpreter(
  725. self, jit, function_name, body_id, task_root):
  726. """Compiles the given function as a function that controls the temperature counter
  727. and calls the interpreter."""
  728. def __increment_temperature(**kwargs):
  729. if jit.jit_globals[self.can_rejit_name]:
  730. temperature_counter_val = jit.jit_globals[self.temperature_counter_name]
  731. temperature_counter_val += self.temperature_increment
  732. jit.jit_globals[self.temperature_counter_name] = temperature_counter_val
  733. if temperature_counter_val >= ADAPTIVE_BASELINE_JIT_TEMPERATURE_THRESHOLD:
  734. if temperature_counter_val >= ADAPTIVE_FAST_JIT_TEMPERATURE_THRESHOLD:
  735. yield [
  736. ("CALL_ARGS",
  737. [jit.jit_rejit,
  738. (task_root, body_id, function_name, compile_function_body_fast)])]
  739. else:
  740. yield [
  741. ("CALL_ARGS",
  742. [jit.jit_rejit,
  743. (task_root, body_id, function_name, self.compile_baseline)])]
  744. result, = yield [("CALL_KWARGS", [jit.jit_globals[function_name], kwargs])]
  745. raise primitive_functions.PrimitiveFinished((True, result))
  746. raise primitive_functions.PrimitiveFinished((False, None))
  747. yield [
  748. ("TAIL_CALL_ARGS",
  749. [compile_function_body_interpret,
  750. (jit, function_name, body_id, task_root, __increment_temperature)])]
  751. def compile_baseline(
  752. self, jit, function_name, body_id, task_root):
  753. """Compiles the given function with the baseline JIT, and inserts logic that controls
  754. the temperature counter."""
  755. (_, parameter_list, _), = yield [
  756. ("CALL_ARGS", [jit.jit_signature, (body_id,)])]
  757. # This tree represents the following logic:
  758. #
  759. # if can_rejit:
  760. # global temperature_counter
  761. # temperature_counter = temperature_counter + temperature_increment
  762. # if temperature_counter >= ADAPTIVE_FAST_JIT_TEMPERATURE_THRESHOLD:
  763. # yield [("CALL_KWARGS", [jit_runtime.JIT_REJIT_FUNCTION_NAME, {...}])]
  764. # yield [("TAIL_CALL_KWARGS", [function_name, {...}])]
  765. header = tree_ir.SelectInstruction(
  766. tree_ir.LoadGlobalInstruction(self.can_rejit_name),
  767. tree_ir.create_block(
  768. tree_ir.DeclareGlobalInstruction(self.temperature_counter_name),
  769. tree_ir.IgnoreInstruction(
  770. tree_ir.StoreGlobalInstruction(
  771. self.temperature_counter_name,
  772. tree_ir.BinaryInstruction(
  773. tree_ir.LoadGlobalInstruction(self.temperature_counter_name),
  774. '+',
  775. tree_ir.LiteralInstruction(self.temperature_increment)))),
  776. tree_ir.SelectInstruction(
  777. tree_ir.BinaryInstruction(
  778. tree_ir.LoadGlobalInstruction(self.temperature_counter_name),
  779. '>=',
  780. tree_ir.LiteralInstruction(ADAPTIVE_FAST_JIT_TEMPERATURE_THRESHOLD)),
  781. tree_ir.create_block(
  782. tree_ir.RunGeneratorFunctionInstruction(
  783. tree_ir.LoadGlobalInstruction(jit_runtime.JIT_REJIT_FUNCTION_NAME),
  784. tree_ir.DictionaryLiteralInstruction([
  785. (tree_ir.LiteralInstruction('task_root'),
  786. bytecode_to_tree.load_task_root()),
  787. (tree_ir.LiteralInstruction('body_id'),
  788. tree_ir.LiteralInstruction(body_id)),
  789. (tree_ir.LiteralInstruction('function_name'),
  790. tree_ir.LiteralInstruction(function_name)),
  791. (tree_ir.LiteralInstruction('compile_function_body'),
  792. tree_ir.LoadGlobalInstruction(
  793. jit_runtime.JIT_COMPILE_FUNCTION_BODY_FAST_FUNCTION_NAME))]),
  794. result_type=tree_ir.NO_RESULT_TYPE),
  795. bytecode_to_tree.create_return(
  796. tree_ir.create_jit_call(
  797. tree_ir.LoadGlobalInstruction(function_name),
  798. [(name, tree_ir.LoadLocalInstruction(name))
  799. for name in parameter_list],
  800. tree_ir.LoadLocalInstruction(jit_runtime.KWARGS_PARAMETER_NAME)))),
  801. tree_ir.EmptyInstruction())),
  802. tree_ir.EmptyInstruction())
  803. # Compile with the baseline JIT, and insert the header.
  804. yield [
  805. ("TAIL_CALL_ARGS",
  806. [compile_function_body_baseline,
  807. (jit, function_name, body_id, task_root, header, True)])]
  808. def compile_function_body_adaptive(
  809. jit, function_name, body_id, task_root,
  810. temperature_heuristic=favor_loops):
  811. print("ADAPTIVE")
  812. """Compile the function with the given name and body id. An execution engine is picked
  813. automatically, and the function may be compiled again at a later time."""
  814. # The general idea behind this compilation technique is to first use the baseline JIT
  815. # to compile a function, and then switch to the fast JIT when we determine that doing
  816. # so would be a good idea. We maintain a 'temperature' counter, which has an initial value
  817. # and gets incremented every time the function is executed.
  818. body_bytecode, = yield [("CALL_ARGS", [jit.jit_parse_bytecode, (body_id,)])]
  819. initial_temperature, temperature_increment = temperature_heuristic(body_bytecode)
  820. if jit.jit_success_log_function is not None:
  821. jit.jit_success_log_function(
  822. "Initial temperature for '%s': %d" % (function_name, initial_temperature))
  823. if initial_temperature >= ADAPTIVE_FAST_JIT_TEMPERATURE_THRESHOLD:
  824. # Initial temperature exceeds the fast-jit threshold.
  825. # Compile this thing with fast-jit right away.
  826. if jit.jit_success_log_function is not None:
  827. jit.jit_success_log_function(
  828. "Compiling '%s' with fast-jit." % function_name)
  829. yield [("CALL_ARGS", [compile_function_body_fast, (jit, function_name, body_id, task_root)])]
  830. print("NEXT...")
  831. temperature_counter_name = jit.import_value(
  832. initial_temperature, function_name + "_temperature_counter")
  833. can_rejit_name = jit.get_can_rejit_name(function_name)
  834. jit.jit_globals[can_rejit_name] = True
  835. state = AdaptiveJitState(temperature_counter_name, temperature_increment, can_rejit_name)
  836. if initial_temperature >= ADAPTIVE_BASELINE_JIT_TEMPERATURE_THRESHOLD:
  837. # Initial temperature exceeds the baseline JIT threshold.
  838. # Compile this thing with baseline JIT right away.
  839. if jit.jit_success_log_function is not None:
  840. jit.jit_success_log_function(
  841. "Compiling '%s' with baseline-jit." % function_name)
  842. yield [
  843. ("TAIL_CALL_ARGS",
  844. [state.compile_baseline, (jit, function_name, body_id, task_root)])]
  845. else:
  846. # Looks like we'll use the interpreter initially.
  847. if jit.jit_success_log_function is not None:
  848. jit.jit_success_log_function(
  849. "Compiling '%s' with bytecode-interpreter." % function_name)
  850. yield [
  851. ("TAIL_CALL_ARGS",
  852. [state.compile_interpreter, (jit, function_name, body_id, task_root)])]