Debian Codegraph @

rpython/jit/metainterp/optimizeopt/util.py

raw symbols SCIP
1 from __future__ import print_function
2
3 import itertools
4
5 import py
6 from rpython.rlib.objectmodel import r_dict, compute_identity_hash, specialize
7 from rpython.rlib.rarithmetic import intmask
8 from rpython.rlib.unroll import unrolling_iterable
9 from rpython.rlib.objectmodel import we_are_translated
10 from rpython.jit.metainterp import resoperation
11 from rpython.rlib.debug import make_sure_not_resized
12 from rpython.jit.metainterp.resoperation import rop
13
14 # ____________________________________________________________
15 # Misc. utilities
16
17 def _findall(Class, name_prefix, op_prefix=None):
18 result = []
19 for name in dir(Class):
20 if name.startswith(name_prefix):
21 opname = name[len(name_prefix):]
22 if opname.isupper():
23 assert hasattr(resoperation.rop, opname)
24 for value, name in resoperation.opname.items():
25 if op_prefix and not name.startswith(op_prefix):
26 continue
27 if hasattr(Class, name_prefix + name):
28 opclass = resoperation.opclasses[getattr(rop, name)]
29 assert name in opclass.__name__
30 result.append((value, opclass, getattr(Class, name_prefix + name)))
31 return unrolling_iterable(result)
32
33 def make_dispatcher_method(Class, name_prefix, op_prefix=None, default=None):
34 ops = _findall(Class, name_prefix, op_prefix)
35 def dispatch(self, op, *args):
36 if we_are_translated():
37 opnum = op.getopnum()
38 for value, cls, func in ops:
39 if opnum == value:
40 assert isinstance(op, cls)
41 return func(self, op, *args)
42 if default:
43 return default(self, op, *args)
44 else:
45 func = getattr(Class, name_prefix + op.getopname().upper(), None)
46 if func is not None:
47 return func(self, op, *args)
48 if default:
49 return default(self, op, *args)
50 dispatch.__name__ = "dispatch_" + name_prefix
51 return dispatch
52
53 def have_dispatcher_method(Class, name_prefix, op_prefix=None, default=None):
54 ops = _findall(Class, name_prefix, op_prefix)
55 if default:
56 def check(self, opnum):
57 return True
58 else:
59 def check(self, opnum):
60 if we_are_translated():
61 for value, cls, func in ops:
62 if opnum == value:
63 return True
64 return False
65 else:
66 return hasattr(Class, name_prefix + resoperation.opname[opnum].upper())
67 check.__name__ = "check_" + name_prefix
68 return check
69
70 def partition(array, left, right):
71 last_item = array[right]
72 pivot = last_item.sort_key()
73 storeindex = left
74 for i in range(left, right):
75 if array[i].sort_key() <= pivot:
76 array[i], array[storeindex] = array[storeindex], array[i]
77 storeindex += 1
78 # Move pivot to its final place
79 array[storeindex], array[right] = last_item, array[storeindex]
80 return storeindex
81
82 def quicksort(array, left, right):
83 # sort array[left:right+1] (i.e. bounds included)
84 if right > left:
85 pivotnewindex = partition(array, left, right)
86 quicksort(array, left, pivotnewindex - 1)
87 quicksort(array, pivotnewindex + 1, right)
88
89 def sort_descrs(lst):
90 # unused, should I leave it or kill it?
91 quicksort(lst, 0, len(lst)-1)
92
93 def get_box_replacement(op):
94 if op is None:
95 return op
96 return op.get_box_replacement()
97
98 # ____________________________________________________________
99
100 def args_eq(args1, args2):
101 make_sure_not_resized(args1)
102 make_sure_not_resized(args2)
103 if len(args1) != len(args2):
104 return False
105 for i in range(len(args1)):
106 arg1 = args1[i]
107 arg2 = args2[i]
108 if arg1 is None:
109 if arg2 is not None:
110 return False
111 elif not arg1.same_box(arg2):
112 return False
113 return True
114
115 def args_hash(args):
116 make_sure_not_resized(args)
117 res = 0x345678
118 for arg in args:
119 if arg is None:
120 y = 17
121 else:
122 y = arg._get_hash_()
123 res = intmask((1000003 * res) ^ y)
124 return res
125
126 @specialize.call_location()
127 def args_dict():
128 return r_dict(args_eq, args_hash)
129
130 # ____________________________________________________________
131
132 def equaloplists(oplist1, oplist2, strict_fail_args=True, remap={},
133 text_right=None):
134 # try to use the full width of the terminal to display the list
135 # unfortunately, does not work with the default capture method of py.test
136 # (which is fd), you you need to use either -s or --capture=sys, else you
137 # get the standard 80 columns width
138 totwidth = py.io.get_terminal_width()
139 width = totwidth / 2 - 1
140 print(' Comparing lists '.center(totwidth, '-'))
141 text_right = text_right or 'expected'
142 memo = {}
143 print('%s| %s' % ('optimized'.center(width), text_right.center(width)))
144 for op1, op2 in itertools.izip_longest(oplist1, oplist2, fillvalue=''):
145 if op1:
146 txt1 = op1.repr(memo)
147 else:
148 txt1 = ''
149 if op2:
150 txt2 = op2.repr(memo)
151 else:
152 txt2 = ''
153 while txt1 or txt2:
154 part1 = txt1[:width]
155 part2 = txt2[:width]
156 if part1 == part2:
157 sep = '| '
158 else:
159 sep = '<>'
160 print('%s%s%s' % (part1.ljust(width), sep, part2))
161 txt1 = txt1[width:]
162 txt2 = txt2[width:]
163 print('-' * totwidth)
164
165 for i_count, (op1, op2) in enumerate(zip(oplist1, oplist2)):
166 assert op1.getopnum() == op2.getopnum()
167 assert op1.numargs() == op2.numargs()
168 for i in range(op1.numargs()):
169 x = op1.getarg(i)
170 y = op2.getarg(i)
171 assert x.same_box(remap.get(y, y))
172 assert x.same_shape(remap.get(y, y))
173 if op2 in remap:
174 assert op1.same_box(remap[op2])
175 else:
176 if op1.type != 'v':
177 remap[op2] = op1
178 if (op1.getopnum() not in [rop.JUMP, rop.LABEL, rop.FINISH] and
179 not rop.is_guard(op1.getopnum())):
180 assert op1.getdescr() == op2.getdescr()
181 if op1.getfailargs() or op2.getfailargs():
182 assert len(op1.getfailargs()) == len(op2.getfailargs())
183 if strict_fail_args:
184 for x, y in zip(op1.getfailargs(), op2.getfailargs()):
185 if x is None:
186 assert remap.get(y, y) is None
187 else:
188 assert x.same_box(remap.get(y, y))
189 assert x.same_shape(remap.get(y, y))
190 else:
191 fail_args1 = set(op1.getfailargs())
192 fail_args2 = set([remap.get(y, y) for y in op2.getfailargs()])
193 for x in fail_args1:
194 for y in fail_args2:
195 if x.same_box(y):
196 fail_args2.remove(y)
197 break
198 else:
199 assert False
200
201 assert len(oplist1) == len(oplist2)
202 return True