Debian Codegraph @

rpython/jit/codewriter/policy.py

raw symbols SCIP
1 from __future__ import print_function
2
3 from rpython.jit.metainterp import history
4 from rpython.tool.udir import udir
5 from rpython.tool.ansi_print import AnsiLogger
6
7 log = AnsiLogger('jitcodewriter')
8
9
10 class JitPolicy(object):
11 def __init__(self, jithookiface=None):
12 self.unsafe_loopy_graphs = set()
13 self.supports_floats = False
14 self.supports_longlong = False
15 self.supports_singlefloats = False
16 self.jithookiface = jithookiface
17
18 def set_supports_floats(self, flag):
19 self.supports_floats = flag
20
21 def set_supports_longlong(self, flag):
22 self.supports_longlong = flag
23
24 def set_supports_singlefloats(self, flag):
25 self.supports_singlefloats = flag
26
27 def dump_unsafe_loops(self):
28 f = udir.join("unsafe-loops.txt").open('w')
29 strs = [str(graph) for graph in self.unsafe_loopy_graphs]
30 strs.sort()
31 for graph in strs:
32 print(graph, file=f)
33 f.close()
34
35 def look_inside_function(self, func):
36 return True # look into everything by default
37
38 def _reject_function(self, func):
39 # explicitly elidable functions are always opaque
40 if getattr(func, '_elidable_function_', False):
41 return True
42 # rpython.rtyper.module.* are opaque helpers
43 mod = func.__module__ or '?'
44 if mod.startswith('rpython.rtyper.module.'):
45 return True
46 return False
47
48 def look_inside_graph(self, graph):
49 from rpython.translator.backendopt.support import find_backedges
50 contains_loop = bool(find_backedges(graph))
51 try:
52 func = graph.func
53 except AttributeError:
54 see_function = True
55 else:
56 if hasattr(func, '_jit_look_inside_'):
57 see_function = func._jit_look_inside_ # override guessing
58 else:
59 see_function = (self.look_inside_function(func) and not
60 self._reject_function(func))
61 contains_loop = contains_loop and not getattr(
62 func, '_jit_unroll_safe_', False)
63
64 res = see_function and not contains_unsupported_variable_type(graph,
65 self.supports_floats,
66 self.supports_longlong,
67 self.supports_singlefloats)
68 if res and contains_loop:
69 self.unsafe_loopy_graphs.add(graph)
70 res = res and not contains_loop
71 if (see_function and not res and
72 getattr(graph, "access_directly", False)):
73 # This happens when we have a function which has an argument with
74 # the access_directly flag, and the annotator has determined we will
75 # see the function. (See
76 # pypy/annotation/specialize.py:default_specialize) However,
77 # look_inside_graph just decided that we will not see it. (It has a
78 # loop or unsupported variables.) If we return False, the call will
79 # be turned into a residual call, but the graph is access_directly!
80 # If such a function is called and accesses a virtualizable, the JIT
81 # will not notice, and the virtualizable will fall out of sync. So,
82 # we fail loudly now.
83 raise ValueError("access_directly on a function which we don't see %s" % graph)
84 return res
85
86 def contains_unsupported_variable_type(graph, supports_floats,
87 supports_longlong,
88 supports_singlefloats):
89 getkind = history.getkind
90 try:
91 for block in graph.iterblocks():
92 for v in block.inputargs:
93 getkind(v.concretetype, supports_floats,
94 supports_longlong,
95 supports_singlefloats)
96 for op in block.operations:
97 for v in op.args:
98 getkind(v.concretetype, supports_floats,
99 supports_longlong,
100 supports_singlefloats)
101 v = op.result
102 getkind(v.concretetype, supports_floats,
103 supports_longlong,
104 supports_singlefloats)
105 except NotImplementedError as e:
106 log.WARNING('%s, ignoring graph' % (e,))
107 log.WARNING(' %s' % (graph,))
108 return True
109 return False
110
111 # ____________________________________________________________
112
113 class StopAtXPolicy(JitPolicy):
114 def __init__(self, *funcs):
115 JitPolicy.__init__(self)
116 self.funcs = funcs
117
118 def look_inside_function(self, func):
119 return func not in self.funcs