cfg_to_tree.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. """Lowers CFG-IR to tree-IR."""
  2. import modelverse_jit.cfg_ir as cfg_ir
  3. import modelverse_jit.cfg_optimization as cfg_optimization
  4. import modelverse_jit.tree_ir as tree_ir
  5. import modelverse_jit.runtime as jit_runtime
  6. import modelverse_jit.bytecode_to_tree as bytecode_to_tree
  7. # The CFG deconstruction code here is based on the relooper algorithm
  8. # as detailed in https://github.com/kripken/Relooper/blob/master/paper.pdf
  9. class FlowGraphComponent(object):
  10. """Represents a control-flow graph component."""
  11. def __init__(self, entry_blocks, blocks, reachable):
  12. self.entry_blocks = entry_blocks
  13. self.blocks = blocks
  14. self.reachable = reachable
  15. def get_entry_reachable_blocks(self):
  16. """Gets the set of all blocks that are reachable from the entry points."""
  17. return set.union(*[self.get_reachable_blocks(block) for block in self.entry_blocks])
  18. def get_reachable_blocks(self, block):
  19. """Gets all blocks in this component that are reachable from the given block."""
  20. return set.intersection(self.reachable[block], self.blocks)
  21. def get_directly_reachable_blocks(self, block):
  22. """Gets all blocks in this component that are reachable from the given block by taking
  23. exactly one branch."""
  24. return set.intersection(cfg_optimization.get_directly_reachable_blocks(block), self.blocks)
  25. def can_reach(self, source_block, target_block):
  26. """Tests if the first block can reach the second."""
  27. return target_block in self.reachable[source_block] and target_block in self.blocks
  28. def sub_component(self, new_entry_points):
  29. """Creates a sub-component of this component, with the given new entry points."""
  30. result = FlowGraphComponent(new_entry_points, self.blocks, self.reachable)
  31. result.blocks = result.get_entry_reachable_blocks()
  32. return result
  33. def create_graph_component(entry_point):
  34. """Creates a flow graph component from the given entry point."""
  35. reachable = cfg_optimization.get_all_reachable_blocks(entry_point)
  36. return FlowGraphComponent([entry_point], reachable[entry_point], reachable)
  37. class SimpleBlock(object):
  38. """A 'simple' block in the relooper algorithm."""
  39. def __init__(self, body, next_block):
  40. self.body = body
  41. self.next_block = next_block
  42. def lower(self, state):
  43. """Lowers this 'simple' block to a tree."""
  44. return tree_ir.create_block(
  45. state.lower_block(self.body),
  46. self.next_block.lower(state))
  47. class EmptyBlock(object):
  48. """An empty relooper block."""
  49. def lower(self, _):
  50. """Lowers this empty block to a tree."""
  51. return tree_ir.EmptyInstruction()
  52. class LoopBlock(object):
  53. """A 'loop' block in the relooper algorithm."""
  54. def __init__(self, inner, next_block):
  55. self.inner = inner
  56. self.next_block = next_block
  57. def lower(self, state):
  58. """Lowers this 'loop' block to a tree."""
  59. inner = tree_ir.LoopInstruction(
  60. tree_ir.create_block(
  61. self.inner.lower(state),
  62. tree_ir.BreakInstruction()))
  63. return tree_ir.create_block(
  64. inner,
  65. self.next_block.lower(state))
  66. class MultipleBlock(object):
  67. """A 'multiple' block in the relooper algorithm that does _not_ require a loop."""
  68. def __init__(self, handled_blocks, next_block):
  69. self.handled_blocks = handled_blocks
  70. self.next_block = next_block
  71. def lower_handled_blocks(self, state):
  72. """Lowers the handled blocks of this 'multiple' block to a tree."""
  73. result = tree_ir.EmptyInstruction()
  74. for entry, block in self.handled_blocks:
  75. result = tree_ir.SelectInstruction(
  76. tree_ir.BinaryInstruction(
  77. tree_ir.LoadLocalInstruction(state.label_variable),
  78. '==',
  79. tree_ir.LiteralInstruction(entry.index)),
  80. block.lower(state),
  81. result)
  82. return result
  83. def lower(self, state):
  84. """Lowers this 'multiple' block to a tree."""
  85. return tree_ir.create_block(
  86. self.lower_handled_blocks(state),
  87. self.next_block.lower(state))
  88. class MultipleLoopBlock(MultipleBlock):
  89. """A 'multiple' block in the relooper algorithm."""
  90. def __init__(self, handled_blocks, next_block):
  91. MultipleBlock.__init__(self, handled_blocks, next_block)
  92. def lower(self, state):
  93. """Lowers this 'multiple' block to a tree."""
  94. inner = tree_ir.LoopInstruction(
  95. tree_ir.create_block(
  96. self.lower_handled_blocks(state),
  97. tree_ir.BreakInstruction()))
  98. return tree_ir.create_block(
  99. inner,
  100. self.next_block.lower(state))
  101. class ContinueFlow(cfg_ir.FlowInstruction):
  102. """Represents a control flow instruction which continues to the next loop iteration."""
  103. def __init__(self, loop):
  104. cfg_ir.FlowInstruction.__init__(self)
  105. self.loop = loop
  106. def get_dependencies(self):
  107. """Gets all definitions and instructions on which this instruction depends."""
  108. return []
  109. def branches(self):
  110. """Gets a list of basic blocks targeted by this flow instruction."""
  111. return []
  112. def __str__(self):
  113. return 'continue'
  114. class BreakFlow(cfg_ir.FlowInstruction):
  115. """Represents a control flow instruction which breaks out of a loop."""
  116. def __init__(self, loop):
  117. cfg_ir.FlowInstruction.__init__(self)
  118. self.loop = loop
  119. def get_dependencies(self):
  120. """Gets all definitions and instructions on which this instruction depends."""
  121. return []
  122. def branches(self):
  123. """Gets a list of basic blocks targeted by this flow instruction."""
  124. return []
  125. def __str__(self):
  126. return 'break'
  127. def solipsize(graph_component, loop, entry_blocks, next_blocks):
  128. """Replaces branches to entry blocks and next blocks by jumps to 'continue' and 'break'
  129. blocks, respectively."""
  130. all_blocks = set()
  131. reachable_from_inner = set()
  132. for block in graph_component.blocks:
  133. if block in next_blocks:
  134. continue
  135. all_blocks.add(block)
  136. for branch in block.flow.branches():
  137. if branch.block in entry_blocks:
  138. # Create a 'continue' block, and point to that.
  139. continue_block = cfg_ir.BasicBlock(block.counter)
  140. for param in branch.block.parameters:
  141. continue_block.append_parameter(param)
  142. continue_block.flow = ContinueFlow(loop)
  143. branch.block = continue_block
  144. all_blocks.add(continue_block)
  145. elif branch.block in next_blocks:
  146. # Create a 'break' block, and point to that.
  147. break_block = cfg_ir.BasicBlock(block.counter)
  148. for param in branch.block.parameters:
  149. break_block.append_parameter(param)
  150. break_block.flow = BreakFlow(loop)
  151. branch.block = break_block
  152. all_blocks.add(break_block)
  153. reachable_from_inner.add(branch.block)
  154. return reachable_from_inner, FlowGraphComponent(
  155. graph_component.entry_blocks,
  156. all_blocks,
  157. {
  158. block : cfg_optimization.get_reachable_blocks(block)
  159. for block in all_blocks
  160. })
  161. def to_relooper_loop(graph_component):
  162. """Converts the given graph component to a relooper 'loop'."""
  163. entry_blocks = graph_component.entry_blocks
  164. result = LoopBlock(None, None)
  165. inner_blocks = []
  166. next_blocks = []
  167. for block in graph_component.blocks:
  168. if any([graph_component.can_reach(block, ep) for ep in entry_blocks]):
  169. inner_blocks.append(block)
  170. else:
  171. next_blocks.append(block)
  172. reachable_from_inner, inner_component = solipsize(
  173. graph_component, result, entry_blocks, next_blocks)
  174. result.inner = reloop(inner_component)
  175. next_component = FlowGraphComponent(
  176. reachable_from_inner, next_blocks, graph_component.reachable)
  177. result.next_block = reloop(next_component)
  178. return result
  179. def to_relooper_multiple_or_loop(graph_component):
  180. """Converts the given graph component to a relooper 'multiple' or 'loop'."""
  181. # From the Emscripten paper:
  182. #
  183. # If we have more than one entry, try to create a Multiple block:
  184. # For each entry, find all the labels it reaches that
  185. # cannot be reached by any other entry. If at least one entry
  186. # has such labels, return a Multiple block, whose Handled
  187. # blocks are blocks for those labels (and whose entries are
  188. # those labels), and whose Next block is all the rest. Entries
  189. # for the next block are entries that did not become part of
  190. # the Handled blocks, and also labels that can be reached
  191. # from the Handled blocks.
  192. entry_blocks = graph_component.entry_blocks
  193. if len(entry_blocks) <= 1:
  194. return to_relooper_loop(graph_component)
  195. entry_reachables = {ep : graph_component.get_reachable_blocks(ep) for ep in entry_blocks}
  196. exclusive_entries = {}
  197. for entry in entry_blocks:
  198. exclusive_blocks = set(entry_reachables[entry])
  199. for other_entry in entry_blocks:
  200. if other_entry != entry:
  201. exclusive_blocks.difference_update(entry_reachables[other_entry])
  202. if len(exclusive_blocks) > 0:
  203. exclusive_entries[entry] = exclusive_blocks
  204. if len(exclusive_entries) == 0:
  205. return to_relooper_loop(graph_component)
  206. next_entries = set(graph_component.entry_blocks)
  207. for block_set in exclusive_entries.values():
  208. for elem in block_set:
  209. directly_reachable = graph_component.get_directly_reachable_blocks(elem)
  210. directly_reachable.remove(elem)
  211. next_entries.update(directly_reachable)
  212. next_entries.difference_update(exclusive_entries.keys())
  213. result = MultipleLoopBlock({}, None)
  214. for entry, exclusive_blocks in exclusive_entries.items():
  215. other_blocks = set(graph_component.blocks)
  216. other_blocks.difference_update(exclusive_blocks)
  217. result.handled_blocks[entry] = reloop(
  218. solipsize(graph_component, result, set([entry]), other_blocks))
  219. result.next_block = reloop(graph_component.sub_component(next_entries))
  220. return result
  221. def reloop(graph_component):
  222. """Applies the relooper algorithm to the given graph component."""
  223. entry_blocks = graph_component.entry_blocks
  224. if len(entry_blocks) == 0:
  225. return EmptyBlock()
  226. reachable_set = graph_component.get_entry_reachable_blocks()
  227. if len(entry_blocks) == 1 and entry_blocks[0] not in reachable_set:
  228. graph_component.blocks.remove(entry_blocks[0])
  229. return SimpleBlock(
  230. entry_blocks[0],
  231. reloop(
  232. FlowGraphComponent(
  233. graph_component.get_directly_reachable_blocks(entry_blocks[0]),
  234. graph_component.blocks,
  235. graph_component.reachable)))
  236. elif all([block in reachable_set for block in entry_blocks]):
  237. return to_relooper_loop(graph_component)
  238. else:
  239. return to_relooper_multiple_or_loop(graph_component)
  240. def reloop_trivial(graph_component):
  241. """Converts the given control-flow graph to a 'multiple' block that contains only 'simple'
  242. blocks."""
  243. return MultipleLoopBlock(
  244. [(block, SimpleBlock(block, EmptyBlock())) for block in graph_component.blocks],
  245. EmptyBlock())
  246. def reloop_function_body(entry_point):
  247. """Reloops the control-flow graph defined by the given entry point."""
  248. return reloop_trivial(create_graph_component(entry_point))
  249. class LoweringState(object):
  250. """Stores information related to the relooper->tree conversion."""
  251. def __init__(self, jit):
  252. self.jit = jit
  253. self.label_variable = tree_ir.VariableName('__label')
  254. self.definition_loads = {}
  255. self.local_name_map = bytecode_to_tree.LocalNameMap()
  256. self.root_edge_names = {}
  257. def __get_root_edge_name(self, root_node):
  258. """Gets the name of the given root edge's variable."""
  259. return self.__get_root_node_name(root_node) + '_edge'
  260. def __get_root_node_name(self, root_node):
  261. """Gets the name of the given root node's variable."""
  262. if isinstance(root_node, cfg_ir.Definition):
  263. return self.__get_root_edge_name(root_node.value)
  264. if root_node in self.root_edge_names:
  265. return self.root_edge_names[root_node]
  266. result = 'jit_locals%d' % len(self.root_edge_names)
  267. self.root_edge_names[root_node] = result
  268. return result
  269. def __create_value_load(self, value):
  270. """Creates a tree that loads the given value."""
  271. if value.has_value():
  272. if isinstance(value, cfg_ir.Literal):
  273. return self.lower_literal(value)
  274. else:
  275. return tree_ir.LoadLocalInstruction(None)
  276. else:
  277. return tree_ir.LiteralInstruction(None)
  278. def load_definition(self, definition):
  279. """Loads the given definition's variable."""
  280. if definition in self.definition_loads:
  281. return self.definition_loads[definition]
  282. result = self.__create_value_load(definition.value)
  283. self.definition_loads[definition] = result
  284. return result
  285. def lower_block(self, block):
  286. """Lowers the given (relooped) block to a tree."""
  287. statements = []
  288. for definition in block.definitions:
  289. statements.append(self.lower_definition(definition))
  290. statements.append(self.lower_flow(block.flow))
  291. return tree_ir.create_block(*statements)
  292. def lower_definition(self, definition):
  293. """Lowers the given definition to a tree."""
  294. instruction = definition.value
  295. tree_instruction = self.lower_value(instruction)
  296. def_load = self.load_definition(definition)
  297. if isinstance(def_load, tree_ir.LocalInstruction):
  298. return def_load.create_store(tree_instruction)
  299. else:
  300. return tree_instruction
  301. def lower_value(self, value):
  302. """Lowers the given instruction to a tree."""
  303. value_type = type(value)
  304. if value_type in LoweringState.value_lowerings:
  305. return LoweringState.value_lowerings[value_type](self, value)
  306. else:
  307. raise jit_runtime.JitCompilationFailedException(
  308. "Unknown CFG instruction: '%s'" % value)
  309. def lower_literal(self, value):
  310. """Lowers the given literal value."""
  311. return tree_ir.LiteralInstruction(value.literal)
  312. def lower_check_local_exists(self, value):
  313. """Lowers a 'check-value-exists' value."""
  314. return tree_ir.LocalExistsInstruction(
  315. self.local_name_map.get_local_name(value.variable.node_id))
  316. def lower_declare_local(self, value):
  317. """Lowers a 'declare-local' value."""
  318. local_name = self.local_name_map.get_local_name(value.variable.node_id)
  319. return tree_ir.create_block(
  320. tree_ir.create_new_local_node(
  321. local_name,
  322. self.load_definition(value.root_node)),
  323. tree_ir.LoadLocalInstruction(local_name))
  324. def lower_resolve_local(self, value):
  325. """Lowers a 'resolve-local' value."""
  326. return tree_ir.LoadLocalInstruction(
  327. self.local_name_map.get_local_name(value.variable.node_id))
  328. def lower_declare_global(self, value):
  329. """Lowers a 'declare-global' value."""
  330. #
  331. # global_var, = yield [("CN", [])]
  332. # _globals, = yield [("RD", [task_root, "globals"])]
  333. # yield [("CD", [_globals, var_name, global_var])]
  334. #
  335. task_root = bytecode_to_tree.retrieve_task_root()
  336. global_var = tree_ir.StoreLocalInstruction(None, tree_ir.CreateNodeInstruction())
  337. return tree_ir.create_block(
  338. global_var.create_store(
  339. tree_ir.CreateNodeInstruction()),
  340. tree_ir.CreateDictionaryEdgeInstruction(
  341. tree_ir.ReadDictionaryValueInstruction(
  342. task_root.create_load(),
  343. tree_ir.LiteralInstruction('globals')),
  344. tree_ir.LiteralInstruction(
  345. value.variable.name),
  346. global_var.create_load()),
  347. global_var.create_load())
  348. def lower_resolve_global(self, value):
  349. """Lowers a 'resolve-global' value."""
  350. #
  351. # _globals, = yield [("RD", [task_root, "globals"])]
  352. # global_var, = yield [("RD", [_globals, var_name])]
  353. #
  354. task_root = bytecode_to_tree.retrieve_task_root()
  355. return tree_ir.ReadDictionaryValueInstruction(
  356. tree_ir.ReadDictionaryValueInstruction(
  357. task_root.create_load(),
  358. tree_ir.LiteralInstruction('globals')),
  359. tree_ir.LiteralInstruction(value.variable.name))
  360. def lower_alloc_root_node(self, value):
  361. """Lowers an 'alloc-root-node' value."""
  362. local_name = tree_ir.VariableName(self.__get_root_node_name(value))
  363. return tree_ir.create_block(
  364. tree_ir.create_new_local_node(
  365. local_name,
  366. tree_ir.LoadIndexInstruction(
  367. tree_ir.LoadLocalInstruction(jit_runtime.KWARGS_PARAMETER_NAME),
  368. tree_ir.LiteralInstruction('task_root')),
  369. self.__get_root_edge_name(value)),
  370. tree_ir.LoadLocalInstruction(local_name))
  371. def lower_free_root_node(self, value):
  372. """Lowers a 'free-root-node' value."""
  373. return tree_ir.DeleteEdgeInstruction(
  374. tree_ir.LoadLocalInstruction(self.__get_root_edge_name(value.root_node)))
  375. def lower_load_pointer(self, value):
  376. """Lowers a 'load' value."""
  377. return bytecode_to_tree.create_access(self.load_definition(value.pointer))
  378. def lower_store_pointer(self, value):
  379. """Lowers a 'store' value."""
  380. return bytecode_to_tree.create_assign(
  381. self.load_definition(value.pointer), self.load_definition(value.value))
  382. def lower_binary(self, value):
  383. """Lowers a 'binary' value."""
  384. return tree_ir.BinaryInstruction(
  385. self.load_definition(value.lhs),
  386. value.operator,
  387. self.load_definition(value.rhs))
  388. def lower_input(self, _):
  389. """Lowers an 'input' value."""
  390. return bytecode_to_tree.create_input(self.jit.use_input_function)
  391. def lower_output(self, value):
  392. """Lowers an 'output' value."""
  393. return bytecode_to_tree.create_output(self.load_definition(value.value))
  394. def lower_direct_call(self, value):
  395. """Lowers a direct function call."""
  396. calling_convention = value.calling_convention
  397. if calling_convention in LoweringState.call_lowerings:
  398. return LoweringState.call_lowerings[calling_convention](self, value)
  399. else:
  400. raise jit_runtime.JitCompilationFailedException(
  401. "Unknown calling convention: '%s' in instruction '%s'" %
  402. (calling_convention, value))
  403. def lower_indirect_call(self, value):
  404. """Lowers an indirect function call."""
  405. return bytecode_to_tree.create_indirect_call(
  406. self.load_definition(value.target),
  407. [(name, self.load_definition(arg)) for name, arg in value.argument_list])
  408. value_lowerings = {
  409. cfg_ir.Literal : lower_literal,
  410. cfg_ir.CheckLocalExists : lower_check_local_exists,
  411. cfg_ir.DeclareLocal : lower_declare_local,
  412. cfg_ir.ResolveLocal : lower_resolve_local,
  413. cfg_ir.DeclareGlobal : lower_declare_global,
  414. cfg_ir.ResolveGlobal : lower_resolve_global,
  415. cfg_ir.AllocateRootNode : lower_alloc_root_node,
  416. cfg_ir.DeallocateRootNode : lower_free_root_node,
  417. cfg_ir.LoadPointer : lower_load_pointer,
  418. cfg_ir.StoreAtPointer : lower_store_pointer,
  419. cfg_ir.Binary : lower_binary,
  420. cfg_ir.Input : lower_input,
  421. cfg_ir.Output : lower_output,
  422. cfg_ir.DirectFunctionCall : lower_direct_call,
  423. cfg_ir.IndirectFunctionCall : lower_indirect_call
  424. }
  425. def lower_simple_positional_call(self, value):
  426. """Lowers a direct call that uses the 'simple-positional' calling convention."""
  427. return tree_ir.CallInstruction(
  428. tree_ir.LoadGlobalInstruction(value.target_name),
  429. [self.load_definition(arg) for _, arg in value.argument_list])
  430. def lower_jit_call(self, value):
  431. """Lowers a direct call that uses the 'jit' calling convention."""
  432. return tree_ir.create_jit_call(
  433. tree_ir.LoadGlobalInstruction(value.target_name),
  434. [(name, self.load_definition(arg)) for name, arg in value.argument_list],
  435. tree_ir.LoadLocalInstruction(jit_runtime.KWARGS_PARAMETER_NAME))
  436. call_lowerings = {
  437. cfg_ir.SIMPLE_POSITIONAL_CALLING_CONVENTION : lower_simple_positional_call,
  438. cfg_ir.JIT_CALLING_CONVENTION : lower_jit_call
  439. }
  440. def lower_flow(self, flow):
  441. """Lowers the given (relooped) flow instruction to a tree."""
  442. flow_type = type(flow)
  443. if flow_type in LoweringState.flow_lowerings:
  444. return LoweringState.flow_lowerings[flow_type](self, flow)
  445. else:
  446. raise jit_runtime.JitCompilationFailedException(
  447. "Unknown CFG flow instruction: '%s'" % flow)
  448. def lower_jump(self, flow):
  449. """Lowers the given 'jump' flow instruction to a tree."""
  450. return self.lower_branch(flow.branch)
  451. def lower_select(self, flow):
  452. """Lowers the given 'select' flow instruction to a tree."""
  453. return tree_ir.SelectInstruction(
  454. self.load_definition(flow.condition),
  455. self.lower_branch(flow.if_branch),
  456. self.lower_branch(flow.else_branch))
  457. def lower_return(self, flow):
  458. """Lowers the given 'return' flow instruction to a tree."""
  459. return tree_ir.ReturnInstruction(self.load_definition(flow.value))
  460. def lower_throw(self, flow):
  461. """Lowers the given 'throw' flow instruction to a tree."""
  462. return tree_ir.RaiseInstruction(self.load_definition(flow.exception))
  463. def lower_unreachable(self, _):
  464. """Lowers the given 'unreachable' flow instruction to a tree."""
  465. return tree_ir.EmptyInstruction()
  466. def lower_break(self, _):
  467. """Lowers the given 'break' flow instruction to a tree."""
  468. return tree_ir.BreakInstruction()
  469. def lower_continue(self, _):
  470. """Lowers the given 'continue' flow instruction to a tree."""
  471. return tree_ir.ContinueInstruction()
  472. def lower_branch(self, branch):
  473. """Lowers the given (relooped) branch to a tree."""
  474. for param, arg in zip(branch.block.parameters, branch.arguments):
  475. self.load_definition(param).create_store(self.load_definition(arg))
  476. return tree_ir.StoreLocalInstruction(
  477. self.label_variable,
  478. tree_ir.LiteralInstruction(branch.block.index))
  479. flow_lowerings = {
  480. cfg_ir.JumpFlow : lower_jump,
  481. cfg_ir.SelectFlow : lower_select,
  482. cfg_ir.ReturnFlow : lower_return,
  483. cfg_ir.ThrowFlow : lower_throw,
  484. cfg_ir.UnreachableFlow : lower_unreachable,
  485. BreakFlow : lower_break,
  486. ContinueFlow : lower_continue
  487. }