jit.py 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  1. import modelverse_kernel.primitives as primitive_functions
  2. import modelverse_jit.tree_ir as tree_ir
  3. KWARGS_PARAMETER_NAME = "kwargs"
  4. """The name of the kwargs parameter in jitted functions."""
  5. def get_parameter_names(compiled_function):
  6. """Gets the given compiled function's parameter names."""
  7. if hasattr(compiled_function, '__code__'):
  8. return compiled_function.__code__.co_varnames[
  9. :compiled_function.__code__.co_argcount]
  10. elif hasattr(compiled_function, '__init__'):
  11. return get_parameter_names(compiled_function.__init__)[1:]
  12. else:
  13. raise ValueError("'compiled_function' must be a function or a type.")
  14. def apply_intrinsic(intrinsic_function, named_args):
  15. """Applies the given intrinsic to the given sequence of named arguments."""
  16. param_names = get_parameter_names(intrinsic_function)
  17. if tuple(param_names) == tuple([n for n, _ in named_args]):
  18. # Perfect match. Yay!
  19. return intrinsic_function(**dict(named_args))
  20. else:
  21. # We'll have to store the arguments into locals to preserve
  22. # the order of evaluation.
  23. stored_args = [(name, tree_ir.StoreLocalInstruction(None, arg)) for name, arg in named_args]
  24. arg_value_dict = dict([(name, arg.create_load()) for name, arg in stored_args])
  25. store_instructions = [instruction for _, instruction in stored_args]
  26. return tree_ir.CompoundInstruction(
  27. tree_ir.create_block(*store_instructions),
  28. intrinsic_function(**arg_value_dict))
  29. class JitCompilationFailedException(Exception):
  30. """A type of exception that is raised when the jit fails to compile a function."""
  31. pass
  32. class ModelverseJit(object):
  33. """A high-level interface to the modelverse JIT compiler."""
  34. def __init__(self, max_instructions=None, compiled_function_lookup=None):
  35. self.todo_entry_points = set()
  36. self.no_jit_entry_points = set()
  37. self.jitted_entry_points = {}
  38. self.jitted_parameters = {}
  39. self.jit_globals = {
  40. 'PrimitiveFinished' : primitive_functions.PrimitiveFinished
  41. }
  42. self.jit_count = 0
  43. self.max_instructions = 30 if max_instructions is None else max_instructions
  44. self.compiled_function_lookup = compiled_function_lookup
  45. # jit_intrinsics is a function name -> intrinsic map.
  46. self.jit_intrinsics = {}
  47. self.compilation_dependencies = {}
  48. self.jit_enabled = True
  49. def set_jit_enabled(self, is_enabled=True):
  50. """Enables or disables the JIT."""
  51. self.jit_enabled = is_enabled
  52. def mark_entry_point(self, body_id):
  53. """Marks the node with the given identifier as a function entry point."""
  54. if body_id not in self.no_jit_entry_points and body_id not in self.jitted_entry_points:
  55. self.todo_entry_points.add(body_id)
  56. def is_entry_point(self, body_id):
  57. """Tells if the node with the given identifier is a function entry point."""
  58. return body_id in self.todo_entry_points or \
  59. body_id in self.no_jit_entry_points or \
  60. body_id in self.jitted_entry_points
  61. def is_jittable_entry_point(self, body_id):
  62. """Tells if the node with the given identifier is a function entry point that
  63. has not been marked as non-jittable. This only returns `True` if the JIT
  64. is enabled and the function entry point has been marked jittable, or if
  65. the function has already been compiled."""
  66. return ((self.jit_enabled and body_id in self.todo_entry_points) or
  67. self.has_compiled(body_id))
  68. def has_compiled(self, body_id):
  69. """Tests if the function belonging to the given body node has been compiled yet."""
  70. return body_id in self.jitted_entry_points
  71. def get_compiled_name(self, body_id):
  72. """Gets the name of the compiled version of the given body node in the JIT
  73. global state."""
  74. return self.jitted_entry_points[body_id]
  75. def mark_no_jit(self, body_id):
  76. """Informs the JIT that the node with the given identifier is a function entry
  77. point that must never be jitted."""
  78. self.no_jit_entry_points.add(body_id)
  79. if body_id in self.todo_entry_points:
  80. self.todo_entry_points.remove(body_id)
  81. def generate_function_name(self, suggested_name=None):
  82. """Generates a new function name or picks the suggested name if it is still
  83. available."""
  84. if suggested_name is not None and suggested_name not in self.jit_globals:
  85. self.jit_count += 1
  86. return suggested_name
  87. else:
  88. function_name = 'jit_func%d' % self.jit_count
  89. self.jit_count += 1
  90. return function_name
  91. def register_compiled(self, body_id, compiled_function, function_name=None):
  92. """Registers a compiled entry point with the JIT."""
  93. # Get the function's name.
  94. function_name = self.generate_function_name(function_name)
  95. # Map the body id to the given parameter list.
  96. self.jitted_entry_points[body_id] = function_name
  97. self.jit_globals[function_name] = compiled_function
  98. if body_id in self.todo_entry_points:
  99. self.todo_entry_points.remove(body_id)
  100. def lookup_compiled_function(self, name):
  101. """Looks up a compiled function by name. Returns a matching function,
  102. or None if no function was found."""
  103. if name in self.jit_globals:
  104. return self.jit_globals[name]
  105. elif self.compiled_function_lookup is not None:
  106. return self.compiled_function_lookup(name)
  107. else:
  108. return None
  109. def get_intrinsic(self, name):
  110. """Tries to find an intrinsic version of the function with the
  111. given name."""
  112. if name in self.jit_intrinsics:
  113. return self.jit_intrinsics[name]
  114. else:
  115. return None
  116. def register_intrinsic(self, name, intrinsic_function):
  117. """Registers the given intrisic with the JIT. This will make the JIT replace calls to
  118. the function with the given entry point by an application of the specified function."""
  119. self.jit_intrinsics[name] = intrinsic_function
  120. def register_binary_intrinsic(self, name, operator):
  121. """Registers an intrinsic with the JIT that represents the given binary operation."""
  122. self.register_intrinsic(name, lambda a, b: tree_ir.CreateNodeWithValueInstruction(
  123. tree_ir.BinaryInstruction(
  124. tree_ir.ReadValueInstruction(a),
  125. operator,
  126. tree_ir.ReadValueInstruction(b))))
  127. def register_unary_intrinsic(self, name, operator):
  128. """Registers an intrinsic with the JIT that represents the given unary operation."""
  129. self.register_intrinsic(name, lambda a: tree_ir.CreateNodeWithValueInstruction(
  130. tree_ir.UnaryInstruction(
  131. operator,
  132. tree_ir.ReadValueInstruction(a))))
  133. def jit_parameters(self, body_id):
  134. """Acquires the parameter list for the given body id node."""
  135. if body_id not in self.jitted_parameters:
  136. signature_id, = yield [("RRD", [body_id, "body"])]
  137. signature_id = signature_id[0]
  138. param_set_id, = yield [("RD", [signature_id, "params"])]
  139. if param_set_id is None:
  140. self.jitted_parameters[body_id] = ([], [])
  141. else:
  142. param_name_ids, = yield [("RDK", [param_set_id])]
  143. param_names = yield [("RV", [n]) for n in param_name_ids]
  144. param_vars = yield [("RD", [param_set_id, k]) for k in param_names]
  145. self.jitted_parameters[body_id] = (param_vars, param_names)
  146. raise primitive_functions.PrimitiveFinished(self.jitted_parameters[body_id])
  147. def jit_compile(self, user_root, body_id, suggested_name=None):
  148. """Tries to jit the function defined by the given entry point id and parameter list."""
  149. # The comment below makes pylint shut up about our (hopefully benign) use of exec here.
  150. # pylint: disable=I0011,W0122
  151. if body_id in self.jitted_entry_points:
  152. # We have already compiled this function.
  153. raise primitive_functions.PrimitiveFinished(
  154. self.jit_globals[self.jitted_entry_points[body_id]])
  155. elif body_id in self.no_jit_entry_points:
  156. # We're not allowed to jit this function or have tried and failed before.
  157. raise JitCompilationFailedException(
  158. 'Cannot jit function %s at %d because it is marked non-jittable.' % (
  159. '' if suggested_name is None else "'" + suggested_name + "'",
  160. body_id))
  161. # Generate a name for the function we're about to analyze, and pretend that
  162. # it already exists. (we need to do this for recursive functions)
  163. function_name = self.generate_function_name(suggested_name)
  164. self.jitted_entry_points[body_id] = function_name
  165. self.jit_globals[function_name] = None
  166. try:
  167. gen = self.jit_parameters(body_id)
  168. inp = None
  169. while True:
  170. inp = yield gen.send(inp)
  171. except primitive_functions.PrimitiveFinished as ex:
  172. parameter_ids, parameter_list = ex.result
  173. param_dict = dict(zip(parameter_ids, parameter_list))
  174. body_param_dict = dict(zip(parameter_ids, [p + "_ptr" for p in parameter_list]))
  175. dependencies = set([body_id])
  176. self.compilation_dependencies[body_id] = dependencies
  177. try:
  178. gen = AnalysisState(
  179. self, body_id, user_root, body_param_dict,
  180. self.max_instructions).analyze(body_id)
  181. inp = None
  182. while True:
  183. inp = yield gen.send(inp)
  184. except primitive_functions.PrimitiveFinished as ex:
  185. del self.compilation_dependencies[body_id]
  186. constructed_body = ex.result
  187. except JitCompilationFailedException as ex:
  188. del self.compilation_dependencies[body_id]
  189. for dep in dependencies:
  190. self.mark_no_jit(dep)
  191. if dep in self.jitted_entry_points:
  192. del self.jitted_entry_points[dep]
  193. raise JitCompilationFailedException(
  194. "%s (function '%s' at %d)" % (ex.message, function_name, body_id))
  195. # Write a prologue and prepend it to the generated function body.
  196. prologue_statements = []
  197. for (key, val) in param_dict.items():
  198. arg_ptr = tree_ir.StoreLocalInstruction(
  199. body_param_dict[key],
  200. tree_ir.CreateNodeInstruction())
  201. prologue_statements.append(arg_ptr)
  202. prologue_statements.append(
  203. tree_ir.CreateDictionaryEdgeInstruction(
  204. arg_ptr.create_load(),
  205. tree_ir.LiteralInstruction('value'),
  206. tree_ir.LoadLocalInstruction(val)))
  207. constructed_body = tree_ir.create_block(
  208. *(prologue_statements + [constructed_body]))
  209. # Wrap the IR in a function definition, give it a unique name.
  210. constructed_function = tree_ir.DefineFunctionInstruction(
  211. function_name,
  212. parameter_list + ['**' + KWARGS_PARAMETER_NAME],
  213. constructed_body.simplify())
  214. # Convert the function definition to Python code, and compile it.
  215. exec(str(constructed_function), self.jit_globals)
  216. # Extract the compiled function from the JIT global state.
  217. compiled_function = self.jit_globals[function_name]
  218. print(constructed_function)
  219. raise primitive_functions.PrimitiveFinished(compiled_function)
  220. class AnalysisState(object):
  221. """The state of a bytecode analysis call graph."""
  222. def __init__(self, jit, body_id, user_root, local_mapping, max_instructions=None):
  223. self.analyzed_instructions = set()
  224. self.function_vars = set()
  225. self.local_vars = set()
  226. self.body_id = body_id
  227. self.max_instructions = max_instructions
  228. self.user_root = user_root
  229. self.jit = jit
  230. self.local_mapping = local_mapping
  231. def get_local_name(self, local_id):
  232. """Gets the name for a local with the given id."""
  233. if local_id not in self.local_mapping:
  234. self.local_mapping[local_id] = 'local%d' % local_id
  235. return self.local_mapping[local_id]
  236. def register_local_var(self, local_id):
  237. """Registers the given variable node id as a local."""
  238. if local_id in self.function_vars:
  239. raise JitCompilationFailedException(
  240. "Local is used as target of function call.")
  241. self.local_vars.add(local_id)
  242. def register_function_var(self, local_id):
  243. """Registers the given variable node id as a function."""
  244. if local_id in self.local_vars:
  245. raise JitCompilationFailedException(
  246. "Local is used as target of function call.")
  247. self.function_vars.add(local_id)
  248. def retrieve_user_root(self):
  249. """Creates an instruction that stores the user_root variable
  250. in a local."""
  251. return tree_ir.StoreLocalInstruction(
  252. 'user_root',
  253. tree_ir.LoadIndexInstruction(
  254. tree_ir.LoadLocalInstruction(KWARGS_PARAMETER_NAME),
  255. tree_ir.LiteralInstruction('user_root')))
  256. def analyze(self, instruction_id):
  257. """Tries to build an intermediate representation from the instruction with the
  258. given id."""
  259. # Check the analyzed_instructions set for instruction_id to avoid
  260. # infinite loops.
  261. if instruction_id in self.analyzed_instructions:
  262. raise JitCompilationFailedException('Cannot jit non-tree instruction graph.')
  263. elif (self.max_instructions is not None and
  264. len(self.analyzed_instructions) > self.max_instructions):
  265. raise JitCompilationFailedException('Maximum number of instructions exceeded.')
  266. self.analyzed_instructions.add(instruction_id)
  267. instruction_val, = yield [("RV", [instruction_id])]
  268. instruction_val = instruction_val["value"]
  269. if instruction_val in self.instruction_analyzers:
  270. try:
  271. gen = self.instruction_analyzers[instruction_val](self, instruction_id)
  272. inp = None
  273. while True:
  274. inp = yield gen.send(inp)
  275. except StopIteration:
  276. raise Exception(
  277. "Instruction analyzer (for '%s') finished without returning a value!" %
  278. (instruction_val))
  279. except primitive_functions.PrimitiveFinished as outer_e:
  280. # Check if the instruction has a 'next' instruction.
  281. next_instr, = yield [("RD", [instruction_id, "next"])]
  282. if next_instr is None:
  283. raise outer_e
  284. else:
  285. gen = self.analyze(next_instr)
  286. try:
  287. inp = None
  288. while True:
  289. inp = yield gen.send(inp)
  290. except primitive_functions.PrimitiveFinished as inner_e:
  291. raise primitive_functions.PrimitiveFinished(
  292. tree_ir.CompoundInstruction(
  293. outer_e.result,
  294. inner_e.result))
  295. else:
  296. raise JitCompilationFailedException(
  297. "Unknown instruction type: '%s'" % (instruction_val))
  298. def analyze_all(self, instruction_ids):
  299. """Tries to compile a list of IR trees from the given list of instruction ids."""
  300. results = []
  301. for inst in instruction_ids:
  302. gen = self.analyze(inst)
  303. try:
  304. inp = None
  305. while True:
  306. inp = yield gen.send(inp)
  307. except primitive_functions.PrimitiveFinished as ex:
  308. results.append(ex.result)
  309. raise primitive_functions.PrimitiveFinished(results)
  310. def analyze_return(self, instruction_id):
  311. """Tries to analyze the given 'return' instruction."""
  312. retval_id, = yield [("RD", [instruction_id, 'value'])]
  313. if retval_id is None:
  314. raise primitive_functions.PrimitiveFinished(
  315. tree_ir.ReturnInstruction(
  316. tree_ir.EmptyInstruction()))
  317. else:
  318. gen = self.analyze(retval_id)
  319. try:
  320. inp = None
  321. while True:
  322. inp = yield gen.send(inp)
  323. except primitive_functions.PrimitiveFinished as ex:
  324. raise primitive_functions.PrimitiveFinished(
  325. tree_ir.ReturnInstruction(ex.result))
  326. def analyze_if(self, instruction_id):
  327. """Tries to analyze the given 'if' instruction."""
  328. cond, true, false = yield [
  329. ("RD", [instruction_id, "cond"]),
  330. ("RD", [instruction_id, "then"]),
  331. ("RD", [instruction_id, "else"])]
  332. try:
  333. gen = self.analyze_all(
  334. [cond, true]
  335. if false is None
  336. else [cond, true, false])
  337. inp = None
  338. while True:
  339. inp = yield gen.send(inp)
  340. except primitive_functions.PrimitiveFinished as ex:
  341. if false is None:
  342. cond_r, true_r = ex.result
  343. false_r = tree_ir.EmptyInstruction()
  344. else:
  345. cond_r, true_r, false_r = ex.result
  346. raise primitive_functions.PrimitiveFinished(
  347. tree_ir.SelectInstruction(
  348. tree_ir.ReadValueInstruction(cond_r),
  349. true_r,
  350. false_r))
  351. def analyze_while(self, instruction_id):
  352. """Tries to analyze the given 'while' instruction."""
  353. cond, body = yield [
  354. ("RD", [instruction_id, "cond"]),
  355. ("RD", [instruction_id, "body"])]
  356. try:
  357. gen = self.analyze_all([cond, body])
  358. inp = None
  359. while True:
  360. inp = yield gen.send(inp)
  361. except primitive_functions.PrimitiveFinished as ex:
  362. cond_r, body_r = ex.result
  363. raise primitive_functions.PrimitiveFinished(
  364. tree_ir.LoopInstruction(
  365. tree_ir.CompoundInstruction(
  366. tree_ir.SelectInstruction(
  367. tree_ir.ReadValueInstruction(cond_r),
  368. tree_ir.EmptyInstruction(),
  369. tree_ir.BreakInstruction()),
  370. body_r)))
  371. def analyze_constant(self, instruction_id):
  372. """Tries to analyze the given 'constant' (literal) instruction."""
  373. node_id, = yield [("RD", [instruction_id, "node"])]
  374. raise primitive_functions.PrimitiveFinished(
  375. tree_ir.LiteralInstruction(node_id))
  376. def analyze_output(self, instruction_id):
  377. """Tries to analyze the given 'output' instruction."""
  378. # The plan is to basically generate this tree:
  379. #
  380. # value = <some tree>
  381. # last_output, last_output_link, new_last_output = \
  382. # yield [("RD", [user_root, "last_output"]),
  383. # ("RDE", [user_root, "last_output"]),
  384. # ("CN", []),
  385. # ]
  386. # _, _, _, _ = \
  387. # yield [("CD", [last_output, "value", value]),
  388. # ("CD", [last_output, "next", new_last_output]),
  389. # ("CD", [user_root, "last_output", new_last_output]),
  390. # ("DE", [last_output_link])
  391. # ]
  392. # yield None
  393. value_id, = yield [("RD", [instruction_id, "value"])]
  394. try:
  395. gen = self.analyze(value_id)
  396. inp = None
  397. while True:
  398. inp = yield gen.send(inp)
  399. except primitive_functions.PrimitiveFinished as ex:
  400. value_local = tree_ir.StoreLocalInstruction('value', ex.result)
  401. store_user_root = self.retrieve_user_root()
  402. last_output = tree_ir.StoreLocalInstruction(
  403. 'last_output',
  404. tree_ir.ReadDictionaryValueInstruction(
  405. store_user_root.create_load(),
  406. tree_ir.LiteralInstruction('last_output')))
  407. last_output_link = tree_ir.StoreLocalInstruction(
  408. 'last_output_link',
  409. tree_ir.ReadDictionaryEdgeInstruction(
  410. store_user_root.create_load(),
  411. tree_ir.LiteralInstruction('last_output')))
  412. new_last_output = tree_ir.StoreLocalInstruction(
  413. 'new_last_output',
  414. tree_ir.CreateNodeInstruction())
  415. result = tree_ir.create_block(
  416. value_local,
  417. store_user_root,
  418. last_output,
  419. last_output_link,
  420. new_last_output,
  421. tree_ir.CreateDictionaryEdgeInstruction(
  422. last_output.create_load(),
  423. tree_ir.LiteralInstruction('value'),
  424. value_local.create_load()),
  425. tree_ir.CreateDictionaryEdgeInstruction(
  426. last_output.create_load(),
  427. tree_ir.LiteralInstruction('next'),
  428. new_last_output.create_load()),
  429. tree_ir.CreateDictionaryEdgeInstruction(
  430. store_user_root.create_load(),
  431. tree_ir.LiteralInstruction('last_output'),
  432. new_last_output.create_load()),
  433. tree_ir.DeleteEdgeInstruction(last_output_link.create_load()),
  434. tree_ir.NopInstruction())
  435. raise primitive_functions.PrimitiveFinished(result)
  436. def analyze_input(self, _):
  437. """Tries to analyze the given 'input' instruction."""
  438. # The plan is to generate this tree:
  439. #
  440. # value = None
  441. # while True:
  442. # if value is None:
  443. # yield None # nop
  444. # else:
  445. # break
  446. #
  447. # _input = yield [("RD", [user_root, "input"])]
  448. # value = yield [("RD", [_input, "value"])]
  449. #
  450. # _next = yield [("RD", [_input, "next"])]
  451. # yield [("CD", [user_root, "input", _next])]
  452. # yield [("DN", [_input])]
  453. user_root = self.retrieve_user_root()
  454. _input = tree_ir.StoreLocalInstruction(
  455. '_input',
  456. tree_ir.ReadDictionaryValueInstruction(
  457. user_root.create_load(),
  458. tree_ir.LiteralInstruction('input')))
  459. value = tree_ir.StoreLocalInstruction(
  460. 'value',
  461. tree_ir.ReadDictionaryValueInstruction(
  462. _input.create_load(),
  463. tree_ir.LiteralInstruction('value')))
  464. raise primitive_functions.PrimitiveFinished(
  465. tree_ir.CompoundInstruction(
  466. tree_ir.create_block(
  467. user_root,
  468. value.create_store(tree_ir.LiteralInstruction(None)),
  469. tree_ir.LoopInstruction(
  470. tree_ir.create_block(
  471. tree_ir.SelectInstruction(
  472. tree_ir.BinaryInstruction(
  473. value.create_load(),
  474. 'is',
  475. tree_ir.LiteralInstruction(None)),
  476. tree_ir.NopInstruction(),
  477. tree_ir.BreakInstruction()),
  478. _input,
  479. value)),
  480. tree_ir.CreateDictionaryEdgeInstruction(
  481. user_root.create_load(),
  482. tree_ir.LiteralInstruction('input'),
  483. tree_ir.ReadDictionaryValueInstruction(
  484. _input.create_load(),
  485. tree_ir.LiteralInstruction('next'))),
  486. tree_ir.DeleteNodeInstruction(_input.create_load())),
  487. value.create_load()))
  488. def analyze_resolve(self, instruction_id):
  489. """Tries to analyze the given 'resolve' instruction."""
  490. var_id, = yield [("RD", [instruction_id, "var"])]
  491. var_name, = yield [("RV", [var_id])]
  492. # To resolve a variable, we'll do something along the
  493. # lines of:
  494. #
  495. # if 'local_var' in locals():
  496. # tmp = local_var
  497. # else:
  498. # _globals, = yield [("RD", [user_root, "globals"])]
  499. # global_var, = yield [("RD", [_globals, var_name])]
  500. #
  501. # if global_var is None:
  502. # raise Exception("Runtime error: global '%s' not found" % (var_name))
  503. #
  504. # tmp = global_var
  505. name = self.get_local_name(var_id)
  506. if var_name is None:
  507. raise primitive_functions.PrimitiveFinished(
  508. tree_ir.LoadLocalInstruction(name))
  509. user_root = self.retrieve_user_root()
  510. global_var = tree_ir.StoreLocalInstruction(
  511. 'global_var',
  512. tree_ir.ReadDictionaryValueInstruction(
  513. tree_ir.ReadDictionaryValueInstruction(
  514. user_root.create_load(),
  515. tree_ir.LiteralInstruction('globals')),
  516. tree_ir.LiteralInstruction(var_name)))
  517. err_block = tree_ir.SelectInstruction(
  518. tree_ir.BinaryInstruction(
  519. global_var.create_load(),
  520. 'is',
  521. tree_ir.LiteralInstruction(None)),
  522. tree_ir.RaiseInstruction(
  523. tree_ir.CallInstruction(
  524. tree_ir.LoadGlobalInstruction('Exception'),
  525. [tree_ir.LiteralInstruction(
  526. "Runtime error: global '%s' not found" % var_name)
  527. ])),
  528. tree_ir.EmptyInstruction())
  529. raise primitive_functions.PrimitiveFinished(
  530. tree_ir.SelectInstruction(
  531. tree_ir.LocalExistsInstruction(name),
  532. tree_ir.LoadLocalInstruction(name),
  533. tree_ir.CompoundInstruction(
  534. tree_ir.create_block(
  535. user_root,
  536. global_var,
  537. err_block),
  538. global_var.create_load())))
  539. def analyze_declare(self, instruction_id):
  540. """Tries to analyze the given 'declare' function."""
  541. var_id, = yield [("RD", [instruction_id, "var"])]
  542. self.register_local_var(var_id)
  543. name = self.get_local_name(var_id)
  544. # The following logic declares a local:
  545. #
  546. # if 'local_name' not in locals():
  547. # local_name, = yield [("CN", [])]
  548. raise primitive_functions.PrimitiveFinished(
  549. tree_ir.SelectInstruction(
  550. tree_ir.LocalExistsInstruction(name),
  551. tree_ir.EmptyInstruction(),
  552. tree_ir.StoreLocalInstruction(
  553. name,
  554. tree_ir.CreateNodeInstruction())))
  555. def analyze_global(self, instruction_id):
  556. """Tries to analyze the given 'global' (declaration) instruction."""
  557. var_id, = yield [("RD", [instruction_id, "var"])]
  558. var_name, = yield [("RV", [var_id])]
  559. # To resolve a variable, we'll do something along the
  560. # lines of:
  561. #
  562. # _globals, = yield [("RD", [user_root, "globals"])]
  563. # global_var = yield [("RD", [_globals, var_name])]
  564. #
  565. # if global_var is None:
  566. # global_var, = yield [("CN", [])]
  567. # yield [("CD", [_globals, var_name, global_var])]
  568. #
  569. # tmp = global_var
  570. user_root = self.retrieve_user_root()
  571. _globals = tree_ir.StoreLocalInstruction(
  572. '_globals',
  573. tree_ir.ReadDictionaryValueInstruction(
  574. user_root.create_load(),
  575. tree_ir.LiteralInstruction('globals')))
  576. global_var = tree_ir.StoreLocalInstruction(
  577. 'global_var',
  578. tree_ir.ReadDictionaryValueInstruction(
  579. _globals.create_load(),
  580. tree_ir.LiteralInstruction(var_name)))
  581. raise primitive_functions.PrimitiveFinished(
  582. tree_ir.CompoundInstruction(
  583. tree_ir.create_block(
  584. user_root,
  585. _globals,
  586. global_var,
  587. tree_ir.SelectInstruction(
  588. tree_ir.BinaryInstruction(
  589. global_var.create_load(),
  590. 'is',
  591. tree_ir.LiteralInstruction(None)),
  592. tree_ir.create_block(
  593. global_var.create_store(
  594. tree_ir.CreateNodeInstruction()),
  595. tree_ir.CreateDictionaryEdgeInstruction(
  596. _globals.create_load(),
  597. tree_ir.LiteralInstruction(var_name),
  598. global_var.create_load())),
  599. tree_ir.EmptyInstruction())),
  600. global_var.create_load()))
  601. def analyze_assign(self, instruction_id):
  602. """Tries to analyze the given 'assign' instruction."""
  603. var_id, value_id = yield [("RD", [instruction_id, "var"]),
  604. ("RD", [instruction_id, "value"])]
  605. try:
  606. gen = self.analyze_all([var_id, value_id])
  607. inp = None
  608. while True:
  609. inp = yield gen.send(inp)
  610. except primitive_functions.PrimitiveFinished as ex:
  611. var_r, value_r = ex.result
  612. # Assignments work like this:
  613. #
  614. # value_link = yield [("RDE", [variable, "value"])]
  615. # _, _ = yield [("CD", [variable, "value", value]),
  616. # ("DE", [value_link])]
  617. variable = tree_ir.StoreLocalInstruction('variable', var_r)
  618. value = tree_ir.StoreLocalInstruction('value', value_r)
  619. value_link = tree_ir.StoreLocalInstruction(
  620. 'value_link',
  621. tree_ir.ReadDictionaryEdgeInstruction(
  622. variable.create_load(),
  623. tree_ir.LiteralInstruction('value')))
  624. raise primitive_functions.PrimitiveFinished(
  625. tree_ir.create_block(
  626. variable,
  627. value,
  628. value_link,
  629. tree_ir.CreateDictionaryEdgeInstruction(
  630. variable.create_load(),
  631. tree_ir.LiteralInstruction('value'),
  632. value.create_load()),
  633. tree_ir.DeleteEdgeInstruction(
  634. value_link.create_load())))
  635. def analyze_access(self, instruction_id):
  636. """Tries to analyze the given 'access' instruction."""
  637. var_id, = yield [("RD", [instruction_id, "var"])]
  638. try:
  639. gen = self.analyze(var_id)
  640. inp = None
  641. while True:
  642. inp = yield gen.send(inp)
  643. except primitive_functions.PrimitiveFinished as ex:
  644. var_r = ex.result
  645. # Accessing a variable is pretty easy. It really just boils
  646. # down to reading the value corresponding to the 'value' key
  647. # of the variable.
  648. #
  649. # value, = yield [("RD", [returnvalue, "value"])]
  650. raise primitive_functions.PrimitiveFinished(
  651. tree_ir.ReadDictionaryValueInstruction(
  652. var_r,
  653. tree_ir.LiteralInstruction('value')))
  654. def analyze_direct_call(self, callee_id, callee_name, first_parameter_id):
  655. """Tries to analyze a direct 'call' instruction."""
  656. self.register_function_var(callee_id)
  657. body_id, = yield [("RD", [callee_id, "body"])]
  658. # Make this function dependent on the callee.
  659. if body_id in self.jit.compilation_dependencies:
  660. self.jit.compilation_dependencies[body_id].add(self.body_id)
  661. # Figure out if the function might be an intrinsic.
  662. intrinsic = self.jit.get_intrinsic(callee_name)
  663. if intrinsic is None:
  664. compiled_func = self.jit.lookup_compiled_function(callee_name)
  665. if compiled_func is None:
  666. # Compile the callee.
  667. try:
  668. gen = self.jit.jit_compile(self.user_root, body_id, callee_name)
  669. inp = None
  670. while True:
  671. inp = yield gen.send(inp)
  672. except primitive_functions.PrimitiveFinished as ex:
  673. pass
  674. else:
  675. self.jit.register_compiled(body_id, compiled_func, callee_name)
  676. # Get the callee's name.
  677. compiled_func_name = self.jit.get_compiled_name(body_id)
  678. # Analyze the argument dictionary.
  679. try:
  680. gen = self.analyze_arguments(first_parameter_id)
  681. inp = None
  682. while True:
  683. inp = yield gen.send(inp)
  684. except primitive_functions.PrimitiveFinished as ex:
  685. named_args = ex.result
  686. if intrinsic is not None:
  687. raise primitive_functions.PrimitiveFinished(
  688. apply_intrinsic(intrinsic, named_args))
  689. else:
  690. raise primitive_functions.PrimitiveFinished(
  691. tree_ir.JitCallInstruction(
  692. tree_ir.LoadGlobalInstruction(compiled_func_name),
  693. named_args,
  694. tree_ir.LoadLocalInstruction(KWARGS_PARAMETER_NAME)))
  695. def analyze_arguments(self, first_argument_id):
  696. """Analyzes the parameter-to-argument mapping started by the specified first argument
  697. node."""
  698. next_param = first_argument_id
  699. named_args = []
  700. while next_param is not None:
  701. param_name_id, = yield [("RD", [next_param, "name"])]
  702. param_name, = yield [("RV", [param_name_id])]
  703. param_val_id, = yield [("RD", [next_param, "value"])]
  704. try:
  705. gen = self.analyze(param_val_id)
  706. inp = None
  707. while True:
  708. inp = yield gen.send(inp)
  709. except primitive_functions.PrimitiveFinished as ex:
  710. named_args.append((param_name, ex.result))
  711. next_param, = yield [("RD", [next_param, "next_param"])]
  712. raise primitive_functions.PrimitiveFinished(named_args)
  713. def analyze_call(self, instruction_id):
  714. """Tries to analyze the given 'call' instruction."""
  715. func_id, first_param_id, = yield [("RD", [instruction_id, "func"]),
  716. ("RD", [instruction_id, "params"])]
  717. # Figure out what the 'func' instruction's type is.
  718. func_instruction_op, = yield [("RV", [func_id])]
  719. if func_instruction_op['value'] == 'access':
  720. # Calls to 'access(resolve(var))' instructions are translated to direct calls.
  721. access_value_id, = yield [("RD", [func_id, "var"])]
  722. access_value_op, = yield [("RV", [access_value_id])]
  723. if access_value_op['value'] == 'resolve':
  724. resolved_var_id, = yield [("RD", [access_value_id, "var"])]
  725. resolved_var_name, = yield [("RV", [resolved_var_id])]
  726. # Try to look the name up as a global.
  727. _globals, = yield [("RD", [self.user_root, "globals"])]
  728. global_var, = yield [("RD", [_globals, resolved_var_name])]
  729. global_val, = yield [("RD", [global_var, "value"])]
  730. if global_val is None:
  731. raise JitCompilationFailedException(
  732. "Cannot JIT function calls that target an unknown value.")
  733. else:
  734. gen = self.analyze_direct_call(
  735. global_val, resolved_var_name, first_param_id)
  736. inp = None
  737. while True:
  738. inp = yield gen.send(inp)
  739. # PrimitiveFinished exception will bubble up from here.
  740. raise JitCompilationFailedException("Cannot JIT indirect function calls yet.")
  741. instruction_analyzers = {
  742. 'if' : analyze_if,
  743. 'while' : analyze_while,
  744. 'return' : analyze_return,
  745. 'constant' : analyze_constant,
  746. 'resolve' : analyze_resolve,
  747. 'declare' : analyze_declare,
  748. 'global' : analyze_global,
  749. 'assign' : analyze_assign,
  750. 'access' : analyze_access,
  751. 'output' : analyze_output,
  752. 'input' : analyze_input,
  753. 'call' : analyze_call
  754. }