jit.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. import modelverse_kernel.primitives as primitive_functions
  2. import modelverse_jit.tree_ir as tree_ir
  3. KWARGS_PARAMETER_NAME = "remainder"
  4. """The name of the kwargs parameter in jitted functions."""
  5. class JitCompilationFailedException(Exception):
  6. """A type of exception that is raised when the jit fails to compile a function."""
  7. pass
  8. class ModelverseJit(object):
  9. """A high-level interface to the modelverse JIT compiler."""
  10. def __init__(self, max_instructions=None):
  11. self.todo_entry_points = set()
  12. self.no_jit_entry_points = set()
  13. self.jitted_entry_points = {}
  14. self.jit_globals = {
  15. 'PrimitiveFinished' : primitive_functions.PrimitiveFinished
  16. }
  17. self.jit_count = 0
  18. self.max_instructions = 30 if max_instructions is None else max_instructions
  19. def mark_entry_point(self, body_id):
  20. """Marks the node with the given identifier as a function entry point."""
  21. if body_id not in self.no_jit_entry_points and body_id not in self.jitted_entry_points:
  22. self.todo_entry_points.add(body_id)
  23. def is_entry_point(self, body_id):
  24. """Tells if the node with the given identifier is a function entry point."""
  25. return body_id in self.todo_entry_points or \
  26. body_id in self.no_jit_entry_points or \
  27. body_id in self.jitted_entry_points
  28. def is_jittable_entry_point(self, body_id):
  29. """Tells if the node with the given identifier is a function entry point that
  30. has not been marked as non-jittable."""
  31. return body_id in self.todo_entry_points or \
  32. body_id in self.jitted_entry_points
  33. def mark_no_jit(self, body_id):
  34. """Informs the JIT that the node with the given identifier is a function entry
  35. point that must never be jitted."""
  36. self.no_jit_entry_points.add(body_id)
  37. if body_id in self.todo_entry_points:
  38. self.todo_entry_points.remove(body_id)
  39. def register_compiled(self, body_id, compiled):
  40. """Registers a compiled entry point with the JIT."""
  41. self.jitted_entry_points[body_id] = compiled
  42. if body_id in self.todo_entry_points:
  43. self.todo_entry_points.remove(body_id)
  44. def try_jit(self, body_id, parameter_list):
  45. """Tries to jit the function defined by the given entry point id and parameter list."""
  46. # The comment below makes pylint shut up about our (hopefully benign) use of exec here.
  47. # pylint: disable=I0011,W0122
  48. if body_id in self.jitted_entry_points:
  49. # We have already compiled this function.
  50. raise primitive_functions.PrimitiveFinished(self.jitted_entry_points[body_id])
  51. elif body_id in self.no_jit_entry_points:
  52. # We're not allowed to jit this function or have tried and failed before.
  53. raise JitCompilationFailedException(
  54. 'Cannot jit function at %d because it is marked non-jittable.' % body_id)
  55. try:
  56. gen = AnalysisState(self.max_instructions).analyze(body_id)
  57. inp = None
  58. while True:
  59. inp = yield gen.send(inp)
  60. except primitive_functions.PrimitiveFinished as ex:
  61. constructed_body = ex.result
  62. except JitCompilationFailedException as ex:
  63. self.mark_no_jit(body_id)
  64. raise JitCompilationFailedException(
  65. '%s (function at %d)' % (ex.message, body_id))
  66. # Wrap the IR in a function definition, give it a unique name.
  67. constructed_function = tree_ir.DefineFunctionInstruction(
  68. 'jit_func%d' % self.jit_count,
  69. parameter_list + ['**' + KWARGS_PARAMETER_NAME],
  70. constructed_body.simplify())
  71. self.jit_count += 1
  72. # Convert the function definition to Python code, and compile it.
  73. exec(str(constructed_function), self.jit_globals)
  74. # Extract the compiled function from the JIT global state.
  75. compiled_function = self.jit_globals[constructed_function.name]
  76. print(constructed_function)
  77. # Save the compiled function so we can reuse it later.
  78. self.jitted_entry_points[body_id] = compiled_function
  79. raise primitive_functions.PrimitiveFinished(compiled_function)
  80. class AnalysisState(object):
  81. """The state of a bytecode analysis call graph."""
  82. def __init__(self, max_instructions=None):
  83. self.analyzed_instructions = set()
  84. self.max_instructions = max_instructions
  85. def get_local_name(self, local_id):
  86. """Gets the name for a local with the given id."""
  87. return 'local%d' % local_id
  88. def retrieve_user_root(self):
  89. """Creates an instruction that stores the user_root variable
  90. in a local."""
  91. return tree_ir.StoreLocalInstruction(
  92. 'user_root',
  93. tree_ir.LoadIndexInstruction(
  94. tree_ir.LoadLocalInstruction(KWARGS_PARAMETER_NAME),
  95. tree_ir.LiteralInstruction('user_root')))
  96. def analyze(self, instruction_id):
  97. """Tries to build an intermediate representation from the instruction with the
  98. given id."""
  99. # Check the analyzed_instructions set for instruction_id to avoid
  100. # infinite loops.
  101. if instruction_id in self.analyzed_instructions:
  102. raise JitCompilationFailedException('Cannot jit non-tree instruction graph.')
  103. elif (self.max_instructions is not None and
  104. len(self.analyzed_instructions) > self.max_instructions):
  105. raise JitCompilationFailedException('Maximal number of instructions exceeded.')
  106. self.analyzed_instructions.add(instruction_id)
  107. instruction_val, = yield [("RV", [instruction_id])]
  108. instruction_val = instruction_val["value"]
  109. if instruction_val in self.instruction_analyzers:
  110. gen = self.instruction_analyzers[instruction_val](self, instruction_id)
  111. try:
  112. inp = None
  113. while True:
  114. inp = yield gen.send(inp)
  115. except StopIteration:
  116. raise Exception(
  117. "Instruction analyzer (for '%s') finished without returning a value!" %
  118. (instruction_val))
  119. except primitive_functions.PrimitiveFinished as outer_e:
  120. # Check if the instruction has a 'next' instruction.
  121. next_instr, = yield [("RD", [instruction_id, "next"])]
  122. if next_instr is None:
  123. raise outer_e
  124. else:
  125. gen = self.analyze(next_instr)
  126. try:
  127. inp = None
  128. while True:
  129. inp = yield gen.send(inp)
  130. except primitive_functions.PrimitiveFinished as inner_e:
  131. raise primitive_functions.PrimitiveFinished(
  132. tree_ir.CompoundInstruction(
  133. outer_e.result,
  134. inner_e.result))
  135. else:
  136. raise JitCompilationFailedException(
  137. "Unknown instruction type: '%s'" % (instruction_val))
  138. def analyze_all(self, instruction_ids):
  139. """Tries to compile a list of IR trees from the given list of instruction ids."""
  140. results = []
  141. for inst in instruction_ids:
  142. gen = self.analyze(inst)
  143. try:
  144. inp = None
  145. while True:
  146. inp = yield gen.send(inp)
  147. except primitive_functions.PrimitiveFinished as ex:
  148. results.append(ex.result)
  149. raise primitive_functions.PrimitiveFinished(results)
  150. def analyze_return(self, instruction_id):
  151. """Tries to analyze the given 'return' instruction."""
  152. retval_id, = yield [("RD", [instruction_id, 'value'])]
  153. if retval_id is None:
  154. raise primitive_functions.PrimitiveFinished(
  155. tree_ir.ReturnInstruction(
  156. tree_ir.EmptyInstruction()))
  157. else:
  158. gen = self.analyze(retval_id)
  159. try:
  160. inp = None
  161. while True:
  162. inp = yield gen.send(inp)
  163. except primitive_functions.PrimitiveFinished as ex:
  164. raise primitive_functions.PrimitiveFinished(
  165. tree_ir.ReturnInstruction(ex.result))
  166. def analyze_if(self, instruction_id):
  167. """Tries to analyze the given 'if' instruction."""
  168. cond, true, false = yield [
  169. ("RD", [instruction_id, "cond"]),
  170. ("RD", [instruction_id, "then"]),
  171. ("RD", [instruction_id, "else"])]
  172. gen = self.analyze_all(
  173. [cond, true]
  174. if false is None
  175. else [cond, true, false])
  176. try:
  177. inp = None
  178. while True:
  179. inp = yield gen.send(inp)
  180. except primitive_functions.PrimitiveFinished as ex:
  181. if false is None:
  182. cond_r, true_r = ex.result
  183. false_r = tree_ir.EmptyInstruction()
  184. else:
  185. cond_r, true_r, false_r = ex.result
  186. raise primitive_functions.PrimitiveFinished(
  187. tree_ir.SelectInstruction(
  188. tree_ir.ReadValueInstruction(cond_r),
  189. true_r,
  190. false_r))
  191. def analyze_while(self, instruction_id):
  192. """Tries to analyze the given 'while' instruction."""
  193. cond, body = yield [
  194. ("RD", [instruction_id, "cond"]),
  195. ("RD", [instruction_id, "body"])]
  196. gen = self.analyze_all([cond, body])
  197. try:
  198. inp = None
  199. while True:
  200. inp = yield gen.send(inp)
  201. except primitive_functions.PrimitiveFinished as ex:
  202. cond_r, body_r = ex.result
  203. raise primitive_functions.PrimitiveFinished(
  204. tree_ir.LoopInstruction(
  205. tree_ir.CompoundInstruction(
  206. tree_ir.SelectInstruction(
  207. tree_ir.ReadValueInstruction(cond_r),
  208. tree_ir.EmptyInstruction(),
  209. tree_ir.BreakInstruction()),
  210. body_r)))
  211. def analyze_constant(self, instruction_id):
  212. """Tries to analyze the given 'constant' (literal) instruction."""
  213. node_id, = yield [("RD", [instruction_id, "node"])]
  214. raise primitive_functions.PrimitiveFinished(
  215. tree_ir.LiteralInstruction(node_id))
  216. def analyze_output(self, instruction_id):
  217. """Tries to analyze the given 'output' instruction."""
  218. # The plan is to basically generate this tree:
  219. #
  220. # value = <some tree>
  221. # last_output, last_output_link, new_last_output = \
  222. # yield [("RD", [user_root, "last_output"]),
  223. # ("RDE", [user_root, "last_output"]),
  224. # ("CN", []),
  225. # ]
  226. # _, _, _, _ = \
  227. # yield [("CD", [last_output, "value", value]),
  228. # ("CD", [last_output, "next", new_last_output]),
  229. # ("CD", [user_root, "last_output", new_last_output]),
  230. # ("DE", [last_output_link])
  231. # ]
  232. # yield None
  233. value_id, = yield [("RD", [instruction_id, "value"])]
  234. gen = self.analyze(value_id)
  235. try:
  236. inp = None
  237. while True:
  238. inp = yield gen.send(inp)
  239. except primitive_functions.PrimitiveFinished as ex:
  240. value_local = tree_ir.StoreLocalInstruction('value', ex.result)
  241. store_user_root = self.retrieve_user_root()
  242. last_output = tree_ir.StoreLocalInstruction(
  243. 'last_output',
  244. tree_ir.ReadDictionaryValueInstruction(
  245. store_user_root.create_load(),
  246. tree_ir.LiteralInstruction('last_output')))
  247. last_output_link = tree_ir.StoreLocalInstruction(
  248. 'last_output_link',
  249. tree_ir.ReadDictionaryEdgeInstruction(
  250. store_user_root.create_load(),
  251. tree_ir.LiteralInstruction('last_output')))
  252. new_last_output = tree_ir.StoreLocalInstruction(
  253. 'new_last_output',
  254. tree_ir.CreateNodeInstruction())
  255. result = tree_ir.create_block(
  256. value_local,
  257. store_user_root,
  258. last_output,
  259. last_output_link,
  260. new_last_output,
  261. tree_ir.CreateDictionaryEdgeInstruction(
  262. last_output.create_load(),
  263. tree_ir.LiteralInstruction('value'),
  264. value_local.create_load()),
  265. tree_ir.CreateDictionaryEdgeInstruction(
  266. last_output.create_load(),
  267. tree_ir.LiteralInstruction('next'),
  268. new_last_output.create_load()),
  269. tree_ir.CreateDictionaryEdgeInstruction(
  270. store_user_root.create_load(),
  271. tree_ir.LiteralInstruction('last_output'),
  272. new_last_output.create_load()),
  273. tree_ir.DeleteEdgeInstruction(last_output_link.create_load()),
  274. tree_ir.NopInstruction())
  275. raise primitive_functions.PrimitiveFinished(result)
  276. def analyze_input(self, _):
  277. """Tries to analyze the given 'input' instruction."""
  278. # The plan is to generate this tree:
  279. #
  280. # value = None
  281. # while True:
  282. # if value is None:
  283. # yield None # nop
  284. # else:
  285. # break
  286. #
  287. # _input = yield [("RD", [user_root, "input"])]
  288. # value = yield [("RD", [_input, "value"])]
  289. #
  290. # _next = yield [("RD", [_input, "next"])]
  291. # yield [("CD", [user_root, "input", _next])]
  292. # yield [("DN", [_input])]
  293. user_root = self.retrieve_user_root()
  294. _input = tree_ir.StoreLocalInstruction(
  295. '_input',
  296. tree_ir.ReadDictionaryValueInstruction(
  297. user_root.create_load(),
  298. tree_ir.LiteralInstruction('input')))
  299. value = tree_ir.StoreLocalInstruction(
  300. 'value',
  301. tree_ir.ReadDictionaryValueInstruction(
  302. _input.create_load(),
  303. tree_ir.LiteralInstruction('value')))
  304. raise primitive_functions.PrimitiveFinished(
  305. tree_ir.CompoundInstruction(
  306. tree_ir.create_block(
  307. user_root,
  308. value.create_store(tree_ir.LiteralInstruction(None)),
  309. tree_ir.LoopInstruction(
  310. tree_ir.create_block(
  311. tree_ir.SelectInstruction(
  312. tree_ir.BinaryInstruction(
  313. value.create_load(),
  314. 'is',
  315. tree_ir.LiteralInstruction(None)),
  316. tree_ir.NopInstruction(),
  317. tree_ir.BreakInstruction()),
  318. _input,
  319. value)),
  320. tree_ir.CreateDictionaryEdgeInstruction(
  321. user_root.create_load(),
  322. tree_ir.LiteralInstruction('input'),
  323. tree_ir.ReadDictionaryValueInstruction(
  324. _input.create_load(),
  325. tree_ir.LiteralInstruction('next'))),
  326. tree_ir.DeleteNodeInstruction(_input.create_load())),
  327. value.create_load()))
  328. def analyze_resolve(self, instruction_id):
  329. """Tries to analyze the given 'resolve' instruction."""
  330. var_id, = yield [("RD", [instruction_id, "var"])]
  331. var_name, = yield [("RV", [var_id])]
  332. # To resolve a variable, we'll do something along the
  333. # lines of:
  334. #
  335. # if 'local_var' in locals():
  336. # tmp = local_var
  337. # else:
  338. # _globals, = yield [("RD", [user_root, "globals"])]
  339. # global_var, = yield [("RD", [_globals, var_name])]
  340. #
  341. # if global_var is None:
  342. # raise Exception("Runtime error: global '%s' not found" % (var_name))
  343. #
  344. # tmp = global_var
  345. user_root = self.retrieve_user_root()
  346. global_var = tree_ir.StoreLocalInstruction(
  347. 'global_var',
  348. tree_ir.ReadDictionaryValueInstruction(
  349. tree_ir.ReadDictionaryValueInstruction(
  350. user_root.create_load(),
  351. tree_ir.LiteralInstruction('globals')),
  352. tree_ir.LiteralInstruction(var_name)))
  353. err_block = tree_ir.SelectInstruction(
  354. tree_ir.BinaryInstruction(
  355. global_var.create_load(),
  356. 'is',
  357. tree_ir.LiteralInstruction(None)),
  358. tree_ir.RaiseInstruction(
  359. tree_ir.CallInstruction(
  360. tree_ir.LoadLocalInstruction('Exception'),
  361. [tree_ir.LiteralInstruction(
  362. "Runtime error: global '%s' not found" % var_name)
  363. ])),
  364. tree_ir.EmptyInstruction())
  365. name = self.get_local_name(var_id)
  366. raise primitive_functions.PrimitiveFinished(
  367. tree_ir.SelectInstruction(
  368. tree_ir.LocalExistsInstruction(name),
  369. tree_ir.LoadLocalInstruction(name),
  370. tree_ir.CompoundInstruction(
  371. tree_ir.create_block(
  372. user_root,
  373. global_var,
  374. err_block),
  375. global_var.create_load())))
  376. def analyze_declare(self, instruction_id):
  377. """Tries to analyze the given 'declare' function."""
  378. var_id, = yield [("RD", [instruction_id, "var"])]
  379. name = self.get_local_name(var_id)
  380. # The following logic declares a local:
  381. #
  382. # if 'local_name' not in locals():
  383. # local_name, = yield [("CN", [])]
  384. raise primitive_functions.PrimitiveFinished(
  385. tree_ir.SelectInstruction(
  386. tree_ir.LocalExistsInstruction(name),
  387. tree_ir.EmptyInstruction(),
  388. tree_ir.StoreLocalInstruction(
  389. name,
  390. tree_ir.CreateNodeInstruction())))
  391. def analyze_global(self, instruction_id):
  392. """Tries to analyze the given 'global' (declaration) instruction."""
  393. var_id, = yield [("RD", [instruction_id, "var"])]
  394. var_name, = yield [("RV", [var_id])]
  395. # To resolve a variable, we'll do something along the
  396. # lines of:
  397. #
  398. # _globals, = yield [("RD", [user_root, "globals"])]
  399. # global_var = yield [("RD", [_globals, var_name])]
  400. #
  401. # if global_var is None:
  402. # global_var, = yield [("CN", [])]
  403. # yield [("CD", [_globals, var_name, global_var])]
  404. #
  405. # tmp = global_var
  406. user_root = self.retrieve_user_root()
  407. _globals = tree_ir.StoreLocalInstruction(
  408. '_globals',
  409. tree_ir.ReadDictionaryValueInstruction(
  410. user_root.create_load(),
  411. tree_ir.LiteralInstruction('globals')))
  412. global_var = tree_ir.StoreLocalInstruction(
  413. 'global_var',
  414. tree_ir.ReadDictionaryValueInstruction(
  415. _globals.create_load(),
  416. tree_ir.LiteralInstruction(var_name)))
  417. raise primitive_functions.PrimitiveFinished(
  418. tree_ir.CompoundInstruction(
  419. tree_ir.create_block(
  420. user_root,
  421. _globals,
  422. global_var,
  423. tree_ir.SelectInstruction(
  424. tree_ir.BinaryInstruction(
  425. global_var.create_load(),
  426. 'is',
  427. tree_ir.LiteralInstruction(None)),
  428. tree_ir.create_block(
  429. global_var.create_store(
  430. tree_ir.CreateNodeInstruction()),
  431. tree_ir.CreateDictionaryEdgeInstruction(
  432. _globals.create_load(),
  433. tree_ir.LiteralInstruction(var_name),
  434. global_var.create_load())),
  435. tree_ir.EmptyInstruction())),
  436. global_var.create_load()))
  437. def analyze_assign(self, instruction_id):
  438. """Tries to analyze the given 'assign' instruction."""
  439. var_id, value_id = yield [("RD", [instruction_id, "var"]),
  440. ("RD", [instruction_id, "value"])]
  441. try:
  442. gen = self.analyze_all([var_id, value_id])
  443. inp = None
  444. while True:
  445. inp = yield gen.send(inp)
  446. except primitive_functions.PrimitiveFinished as ex:
  447. var_r, value_r = ex.result
  448. # Assignments work like this:
  449. #
  450. # value_link = yield [("RDE", [variable, "value"])]
  451. # _, _ = yield [("CD", [variable, "value", value]),
  452. # ("DE", [value_link])]
  453. variable = tree_ir.StoreLocalInstruction('variable', var_r)
  454. value = tree_ir.StoreLocalInstruction('value', value_r)
  455. value_link = tree_ir.StoreLocalInstruction(
  456. 'value_link',
  457. tree_ir.ReadDictionaryEdgeInstruction(
  458. variable.create_load(),
  459. tree_ir.LiteralInstruction('value')))
  460. raise primitive_functions.PrimitiveFinished(
  461. tree_ir.create_block(
  462. variable,
  463. value,
  464. value_link,
  465. tree_ir.CreateDictionaryEdgeInstruction(
  466. variable.create_load(),
  467. tree_ir.LiteralInstruction('value'),
  468. value.create_load()),
  469. tree_ir.DeleteEdgeInstruction(
  470. value_link.create_load())))
  471. def analyze_access(self, instruction_id):
  472. """Tries to analyze the given 'access' instruction."""
  473. var_id, = yield [("RD", [instruction_id, "var"])]
  474. try:
  475. gen = self.analyze(var_id)
  476. inp = None
  477. while True:
  478. inp = yield gen.send(inp)
  479. except primitive_functions.PrimitiveFinished as ex:
  480. var_r = ex.result
  481. # Accessing a variable is pretty easy. It really just boils
  482. # down to reading the value corresponding to the 'value' key
  483. # of the variable.
  484. #
  485. # value, = yield [("RD", [returnvalue, "value"])]
  486. raise primitive_functions.PrimitiveFinished(
  487. tree_ir.ReadDictionaryValueInstruction(
  488. var_r,
  489. tree_ir.LiteralInstruction('value')))
  490. instruction_analyzers = {
  491. 'if' : analyze_if,
  492. 'while' : analyze_while,
  493. 'return' : analyze_return,
  494. 'constant' : analyze_constant,
  495. 'resolve' : analyze_resolve,
  496. 'declare' : analyze_declare,
  497. 'global' : analyze_global,
  498. 'assign' : analyze_assign,
  499. 'access' : analyze_access,
  500. 'output' : analyze_output,
  501. 'input' : analyze_input
  502. }