Debian Codegraph @

rpython/jit/codewriter/codewriter.py

raw symbols SCIP
1 from __future__ import print_function
2
3 from rpython.jit.codewriter import support, heaptracker
4 from rpython.jit.codewriter.regalloc import perform_register_allocation
5 from rpython.jit.codewriter.flatten import flatten_graph, KINDS
6 from rpython.jit.codewriter.assembler import Assembler, JitCode
7 from rpython.jit.codewriter.jtransform import transform_graph
8 from rpython.jit.codewriter.format import format_assembler
9 from rpython.jit.codewriter.liveness import compute_liveness
10 from rpython.jit.codewriter.call import CallControl
11 from rpython.jit.codewriter.policy import log
12 from rpython.flowspace.model import copygraph
13 from rpython.tool.udir import udir
14
15
16 class CodeWriter(object):
17 callcontrol = None # for tests
18 debug = True
19
20 def __init__(self, cpu=None, jitdrivers_sd=[]):
21 self.cpu = cpu
22 self.assembler = Assembler()
23 self.callcontrol = CallControl(cpu, jitdrivers_sd)
24
25 def transform_func_to_jitcode(self, func, values):
26 """For testing."""
27 rtyper = support.annotate(func, values)
28 graph = rtyper.annotator.translator.graphs[0]
29 jitcode = JitCode("test")
30 self.transform_graph_to_jitcode(graph, jitcode, True, 0)
31 return jitcode
32
33 def transform_graph_to_jitcode(self, graph, jitcode, verbose, index):
34 """Transform a graph into a JitCode containing the same bytecode
35 in a different format.
36 """
37 portal_jd = self.callcontrol.jitdriver_sd_from_portal_graph(graph)
38 graph = copygraph(graph, shallowvars=True)
39 #
40 # step 1: mangle the graph so that it contains the final instructions
41 # that we want in the JitCode, but still as a control flow graph
42 transform_graph(graph, self.cpu, self.callcontrol, portal_jd)
43 #
44 # step 2: perform register allocation on it
45 regallocs = {}
46 for kind in KINDS:
47 regallocs[kind] = perform_register_allocation(graph, kind)
48 #
49 # step 3: flatten the graph to produce human-readable "assembler",
50 # which means mostly producing a linear list of operations and
51 # inserting jumps or conditional jumps. This is a list of tuples
52 # of the shape ("opname", arg1, ..., argN) or (Label(...),).
53 ssarepr = flatten_graph(graph, regallocs, cpu=self.callcontrol.cpu)
54 #
55 # step 3b: compute the liveness around certain operations
56 compute_liveness(ssarepr)
57 #
58 # step 4: "assemble" it into a JitCode, which contains a sequence
59 # of bytes and lists of constants. It's during this step that
60 # constants are cast to their normalized type (Signed, GCREF or
61 # Float).
62 num_regs = {kind:
63 (max(regallocs[kind]._coloring.values()) + 1
64 if regallocs[kind]._coloring
65 else 0)
66 for kind in KINDS}
67 self.assembler.assemble(ssarepr, jitcode, num_regs)
68 jitcode.index = index
69 #
70 # print the resulting assembler
71 if self.debug:
72 self.print_ssa_repr(ssarepr, portal_jd, verbose)
73
74 def make_jitcodes(self, verbose=False):
75 log.info("making JitCodes...")
76 self.callcontrol.grab_initial_jitcodes()
77 count = 0
78 all_jitcodes = []
79 for graph, jitcode in self.callcontrol.enum_pending_graphs():
80 self.transform_graph_to_jitcode(graph, jitcode, verbose, len(all_jitcodes))
81 all_jitcodes.append(jitcode)
82 count += 1
83 if not count % 500:
84 log.info("Produced %d jitcodes" % count)
85 self.assembler.finished(self.callcontrol.callinfocollection)
86 log.info("There are %d JitCode instances." % count)
87 log.info("There are %d -live- ops. Size of liveness is %s bytes" % (
88 self.assembler.num_liveness_ops, self.assembler.all_liveness_length))
89 return all_jitcodes
90
91 def setup_vrefinfo(self, vrefinfo):
92 # must be called at most once
93 assert self.callcontrol.virtualref_info is None
94 self.callcontrol.virtualref_info = vrefinfo
95
96 def setup_jitdriver(self, jitdriver_sd):
97 # Must be called once per jitdriver. Usually jitdriver_sd is an
98 # instance of rpython.jit.metainterp.jitdriver.JitDriverStaticData.
99 self.callcontrol.jitdrivers_sd.append(jitdriver_sd)
100
101 def find_all_graphs(self, policy):
102 return self.callcontrol.find_all_graphs(policy)
103
104 def print_ssa_repr(self, ssarepr, portal_jitdriver, verbose):
105 if verbose:
106 print('%s:' % (ssarepr.name,))
107 print(format_assembler(ssarepr))
108 else:
109 log.dot()
110 dir = udir.ensure("jitcodes", dir=1)
111 if portal_jitdriver:
112 name = "%02d_portal_runner" % (portal_jitdriver.index,)
113 elif ssarepr.name and ssarepr.name != '?':
114 name = ssarepr.name
115 else:
116 name = 'unnamed' % id(ssarepr)
117 i = 1
118 # escape names like <lambda> for windows by removing any strange
119 # character; then make sure the names are not too long
120 name = ''.join(c for c in name if c.isalnum() or c == '_')[:60]
121 extra = ''
122 while dir.join(name+extra).check():
123 i += 1
124 extra = '.%d' % i
125 dir.join(name+extra).write(format_assembler(ssarepr))