| 1 |
import py |
| 2 |
import pytest |
| 3 |
|
| 4 |
from rpython.jit.metainterp.compile import invent_fail_descr_for_op |
| 5 |
from rpython.jit.metainterp.history import TargetToken, JitCellToken, TreeLoop |
| 6 |
from rpython.jit.metainterp.optimizeopt.dependency import (DependencyGraph, Dependency, |
| 7 |
IndexVar, MemoryRef, Node) |
| 8 |
from rpython.jit.metainterp.optimizeopt.vector import VectorLoop |
| 9 |
from rpython.jit.metainterp.optimizeopt.test.test_util import ( |
| 10 |
BaseTest, convert_old_style_to_targets, FakeJitDriverStaticData) |
| 11 |
from rpython.jit.metainterp.resoperation import rop, ResOperation |
| 12 |
from rpython.jit.backend.llgraph.runner import ArrayDescr |
| 13 |
from rpython.jit.tool.oparser import OpParser |
| 14 |
from rpython.rtyper.lltypesystem import rffi |
| 15 |
from rpython.rtyper.lltypesystem import lltype |
| 16 |
from rpython.conftest import option |
| 17 |
|
| 18 |
class FakeDependencyGraph(DependencyGraph): |
| 19 |
""" A dependency graph that is able to emit every instruction |
| 20 |
one by one. """ |
| 21 |
def __init__(self, loop): |
| 22 |
self.loop = loop |
| 23 |
if isinstance(loop, list): |
| 24 |
self.nodes = loop |
| 25 |
else: |
| 26 |
operations = loop.operations |
| 27 |
self.nodes = [Node(op,i) for i,op in \ |
| 28 |
enumerate(operations)] |
| 29 |
self.schedulable_nodes = list(reversed(self.nodes)) |
| 30 |
self.guards = [] |
| 31 |
|
| 32 |
|
| 33 |
class DependencyBaseTest(BaseTest): |
| 34 |
|
| 35 |
def setup_method(self, method): |
| 36 |
self.test_name = method.__name__ |
| 37 |
if not self.cpu.vector_ext.is_enabled(): |
| 38 |
py.test.skip("cpu %s needs to implement the vector backend" % self.cpu) |
| 39 |
|
| 40 |
def build_dependency(self, ops): |
| 41 |
loop = self.parse_loop(ops) |
| 42 |
graph = DependencyGraph(loop) |
| 43 |
self.show_dot_graph(graph, self.test_name) |
| 44 |
for node in graph.nodes: |
| 45 |
assert node.independent(node) |
| 46 |
graph.parsestr = ops |
| 47 |
return graph |
| 48 |
|
| 49 |
def match_op(self, expected, actual, remap): |
| 50 |
if expected.getopnum() != actual.getopnum(): |
| 51 |
return False |
| 52 |
expargs = expected.getarglist() |
| 53 |
actargs = [remap.get(arg, None) for arg in actual.getarglist()] |
| 54 |
if not all([e == a or a is None for e,a in zip(expargs,actargs)]): |
| 55 |
return False |
| 56 |
if expected.getfailargs(): |
| 57 |
expargs = expected.getfailargs() |
| 58 |
actargs = [remap.get(arg, None) for arg in actual.getfailargs()] |
| 59 |
if not all([e == a or a is None for e,a in zip(expargs,actargs)]): |
| 60 |
return False |
| 61 |
return True |
| 62 |
|
| 63 |
def ensure_operations(self, opstrlist, trace): |
| 64 |
oparse = OpParser('', self.cpu, self.namespace, None, |
| 65 |
None, True, None) |
| 66 |
oplist = [] |
| 67 |
for op_str in opstrlist: |
| 68 |
op = oparse.parse_next_op(op_str) |
| 69 |
if not op.returns_void(): |
| 70 |
var = op_str.split('=')[0].strip() |
| 71 |
if '[' in var: |
| 72 |
var = var[:var.find('[')] |
| 73 |
elem = op_str[:len(var)] |
| 74 |
oparse._cache['lltype', elem] = op |
| 75 |
oplist.append(op) |
| 76 |
oplist_i = 0 |
| 77 |
remap = {} |
| 78 |
last_match = 0 |
| 79 |
for i, op in enumerate(trace.operations): |
| 80 |
if oplist_i >= len(oplist): |
| 81 |
break |
| 82 |
curtomatch = oplist[oplist_i] |
| 83 |
if self.match_op(curtomatch, op, remap): |
| 84 |
if not op.returns_void(): |
| 85 |
remap[curtomatch] = op |
| 86 |
oplist_i += 1 |
| 87 |
last_match = i |
| 88 |
|
| 89 |
msg = "could not find all ops in the trace sequence\n\n" |
| 90 |
if oplist_i != len(oplist): |
| 91 |
l = [str(o) for o in oplist[oplist_i:]] |
| 92 |
msg += "sequence\n " + '\n '.join(l) |
| 93 |
msg += "\n\ndoes not match\n " |
| 94 |
l = [str(o) for o in trace.operations[last_match+1:]] |
| 95 |
msg += '\n '.join(l) |
| 96 |
assert oplist_i == len(oplist), msg |
| 97 |
|
| 98 |
def parse_loop(self, ops, add_label=True): |
| 99 |
loop = self.parse(ops) |
| 100 |
loop.operations = filter(lambda op: op.getopnum() != rop.DEBUG_MERGE_POINT, loop.operations) |
| 101 |
token = JitCellToken() |
| 102 |
if add_label: |
| 103 |
label = ResOperation(rop.LABEL, loop.inputargs, descr=TargetToken(token)) |
| 104 |
else: |
| 105 |
label = loop.operations[0] |
| 106 |
label.setdescr(TargetToken(token)) |
| 107 |
jump = loop.operations[-1] |
| 108 |
loop = VectorLoop(label, loop.operations[0:-1], jump) |
| 109 |
loop.jump.setdescr(token) |
| 110 |
class Optimizer(object): |
| 111 |
metainterp_sd = self.metainterp_sd |
| 112 |
jitdriver_sd = FakeJitDriverStaticData() |
| 113 |
opt = Optimizer() |
| 114 |
opt.jitdriver_sd.vec = True |
| 115 |
for op in loop.operations: |
| 116 |
if op.is_guard() and not op.getdescr(): |
| 117 |
descr = invent_fail_descr_for_op(op.getopnum(), opt) |
| 118 |
op.setdescr(descr) |
| 119 |
return loop |
| 120 |
|
| 121 |
def parse_trace(self, source, inc_label_jump=True, pargs=2, iargs=10, |
| 122 |
fargs=6, additional_args=None, replace_args=None): |
| 123 |
args = [] |
| 124 |
for prefix, rang in [('p',range(pargs)), |
| 125 |
('i',range(iargs)), |
| 126 |
('f',range(fargs))]: |
| 127 |
for i in rang: |
| 128 |
args.append(prefix + str(i)) |
| 129 |
|
| 130 |
assert additional_args is None or isinstance(additional_args,list) |
| 131 |
for arg in additional_args or []: |
| 132 |
args.append(arg) |
| 133 |
for k,v in (replace_args or {}).items(): |
| 134 |
for i,_ in enumerate(args): |
| 135 |
if k == args[i]: |
| 136 |
args[i] = v |
| 137 |
break |
| 138 |
indent = " " |
| 139 |
joinedargs = ','.join(args) |
| 140 |
fmt = (indent, joinedargs, source, indent, joinedargs) |
| 141 |
src = "%s[%s]\n%s\n%sjump(%s)" % fmt |
| 142 |
loop = self.parse_loop(src) |
| 143 |
|
| 144 |
|
| 145 |
[str(arg) for arg in loop.inputargs] |
| 146 |
loop.graph = FakeDependencyGraph(loop) |
| 147 |
loop.setup_vectorization() |
| 148 |
return loop |
| 149 |
|
| 150 |
|
| 151 |
def assert_edges(self, graph, edge_list, exceptions): |
| 152 |
""" Check if all dependencies are met. for complex cases |
| 153 |
adding None instead of a list of integers skips the test. |
| 154 |
This checks both if a dependency forward and backward exists. |
| 155 |
""" |
| 156 |
assert len(edge_list) == len(graph.nodes) + 2 |
| 157 |
edge_list = edge_list[1:-1] |
| 158 |
for idx,edges in enumerate(edge_list): |
| 159 |
if edges is None: |
| 160 |
continue |
| 161 |
node_a = graph.getnode(idx) |
| 162 |
dependencies = node_a.provides()[:] |
| 163 |
for idx_b in edges: |
| 164 |
if idx_b == 0 or idx_b >= len(graph.nodes) + 2 -1: |
| 165 |
continue |
| 166 |
idx_b -= 1 |
| 167 |
node_b = graph.getnode(idx_b) |
| 168 |
dependency = node_a.getedge_to(node_b) |
| 169 |
if dependency is None and idx_b not in exceptions.setdefault(idx,[]): |
| 170 |
self.show_dot_graph(graph, self.test_name + '_except') |
| 171 |
assert dependency is not None or node_b.getopnum() == rop.JUMP, \ |
| 172 |
" it is expected that instruction at index" + \ |
| 173 |
" %s depends on instr on index %s but it does not.\n%s" \ |
| 174 |
% (node_a.getindex(), node_b.getindex(), graph) |
| 175 |
elif dependency is not None: |
| 176 |
dependencies.remove(dependency) |
| 177 |
assert dependencies == [], \ |
| 178 |
"dependencies unexpected %s.\n%s" \ |
| 179 |
% (dependencies,graph) |
| 180 |
|
| 181 |
def assert_dependencies(self, graph, full_check=True): |
| 182 |
import re |
| 183 |
deps = {} |
| 184 |
exceptions = {} |
| 185 |
for i,line in enumerate(graph.parsestr.splitlines()): |
| 186 |
dep_pattern = re.compile("#\s*(\d+):") |
| 187 |
dep_match = dep_pattern.search(line) |
| 188 |
if dep_match: |
| 189 |
label = int(dep_match.group(1)) |
| 190 |
deps_list = [] |
| 191 |
deps[label] = [] |
| 192 |
for to in [d for d in line[dep_match.end():].split(',') if len(d) > 0]: |
| 193 |
exception = to.endswith("?") |
| 194 |
if exception: |
| 195 |
to = to[:-1] |
| 196 |
exceptions.setdefault(label,[]).append(int(to)) |
| 197 |
deps[label].append(int(to)) |
| 198 |
|
| 199 |
if full_check: |
| 200 |
edges = [ None ] * len(deps) |
| 201 |
for k,l in deps.items(): |
| 202 |
edges[k] = l |
| 203 |
self.assert_edges(graph, edges, exceptions) |
| 204 |
return graph |
| 205 |
|
| 206 |
def assert_independent(self, graph, a, b): |
| 207 |
a -= 1 |
| 208 |
b -= 1 |
| 209 |
a = graph.getnode(a) |
| 210 |
b = graph.getnode(b) |
| 211 |
assert a.independent(b), "{a} and {b} are dependent!".format(a=a,b=b) |
| 212 |
|
| 213 |
def assert_dependent(self, graph, a, b): |
| 214 |
a -= 1 |
| 215 |
b -= 1 |
| 216 |
a = graph.getnode(a) |
| 217 |
b = graph.getnode(b) |
| 218 |
assert not a.independent(b), "{a} and {b} are independent!".format(a=a,b=b) |
| 219 |
|
| 220 |
def show_dot_graph(self, graph, name): |
| 221 |
if option and option.viewdeps: |
| 222 |
from rpython.translator.tool.graphpage import GraphPage |
| 223 |
page = GraphPage() |
| 224 |
page.source = graph.as_dot() |
| 225 |
page.links = [] |
| 226 |
page.display() |
| 227 |
|
| 228 |
def debug_print_operations(self, loop): |
| 229 |
print('--- loop instr numbered ---') |
| 230 |
for i,op in enumerate(loop.operations): |
| 231 |
print "[",i,"]",op, |
| 232 |
if op.is_guard(): |
| 233 |
print op.getfailargs() |
| 234 |
else: |
| 235 |
print "" |
| 236 |
|
| 237 |
def assert_memory_ref_adjacent(self, m1, m2): |
| 238 |
assert m1.is_adjacent_to(m2) |
| 239 |
assert m2.is_adjacent_to(m1) |
| 240 |
|
| 241 |
def assert_memory_ref_not_adjacent(self, m1, m2): |
| 242 |
assert not m1.is_adjacent_to(m2) |
| 243 |
assert not m2.is_adjacent_to(m1) |
| 244 |
|
| 245 |
class TestDependencyGraph(DependencyBaseTest): |
| 246 |
|
| 247 |
def test_index_var_basic(self): |
| 248 |
b = FakeBox() |
| 249 |
i = IndexVar(b,1,1,0) |
| 250 |
j = IndexVar(b,1,1,0) |
| 251 |
assert i.is_identity() |
| 252 |
assert i.same_variable(j) |
| 253 |
assert i.constant_diff(j) == 0 |
| 254 |
|
| 255 |
def test_index_var_diff(self): |
| 256 |
b = FakeBox() |
| 257 |
i = IndexVar(b,4,2,0) |
| 258 |
j = IndexVar(b,1,1,1) |
| 259 |
assert not i.is_identity() |
| 260 |
assert not j.is_identity() |
| 261 |
assert not i.same_mulfactor(j) |
| 262 |
assert i.constant_diff(j) == -1 |
| 263 |
|
| 264 |
def test_memoryref_basic(self): |
| 265 |
i = FakeBox() |
| 266 |
a = FakeBox() |
| 267 |
m1 = memoryref(a, i, (1,1,0)) |
| 268 |
m2 = memoryref(a, i, (1,1,0)) |
| 269 |
assert m1.alias(m2) |
| 270 |
|
| 271 |
@py.test.mark.parametrize('coeff1,coeff2,state', |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
[((1,1,0), (1,1,0), 'ffft'), |
| 278 |
((4,2,0), (8,4,0), 'ffft'), |
| 279 |
((4,2,0), (8,2,0), 'ffft'), |
| 280 |
((4,2,1), (8,4,0), 'tftf'), |
| 281 |
]) |
| 282 |
def test_memoryref_adjacent_alias(self, coeff1, coeff2, state): |
| 283 |
i = FakeBox() |
| 284 |
a = FakeBox() |
| 285 |
m1 = memoryref(a, i, coeff1) |
| 286 |
m2 = memoryref(a, i, coeff2) |
| 287 |
adja = state[0] == 't' |
| 288 |
adja_after = state[1] == 't' |
| 289 |
adja_before = state[2] == 't' |
| 290 |
alias = state[3] == 't' |
| 291 |
assert m1.is_adjacent_to(m2) == adja |
| 292 |
assert m2.is_adjacent_to(m1) == adja |
| 293 |
assert m1.is_adjacent_after(m2) == adja_after |
| 294 |
assert m2.is_adjacent_after(m1) == adja_before |
| 295 |
assert m1.alias(m2) == alias |
| 296 |
|
| 297 |
def test_dependency_empty(self): |
| 298 |
graph = self.build_dependency(""" |
| 299 |
[] # 0: 1 |
| 300 |
jump() # 1: |
| 301 |
""") |
| 302 |
self.assert_dependencies(graph, full_check=True) |
| 303 |
|
| 304 |
def test_dependency_of_constant_not_used(self): |
| 305 |
graph = self.build_dependency(""" |
| 306 |
[] # 0: 2 |
| 307 |
i1 = int_add(1,1) # 1: 2 |
| 308 |
jump() # 2: |
| 309 |
""") |
| 310 |
self.assert_dependencies(graph, full_check=True) |
| 311 |
|
| 312 |
def test_dependency_simple(self): |
| 313 |
graph = self.build_dependency(""" |
| 314 |
[] # 0: 4 |
| 315 |
i1 = int_add(1,1) # 1: 2 |
| 316 |
i2 = int_add(i1,1) # 2: 3 |
| 317 |
guard_value(i2,3) [] # 3: 4 |
| 318 |
jump() # 4: |
| 319 |
""") |
| 320 |
graph = self.assert_dependencies(graph, full_check=True) |
| 321 |
self.assert_dependent(graph, 1,2) |
| 322 |
self.assert_dependent(graph, 2,3) |
| 323 |
self.assert_dependent(graph, 1,3) |
| 324 |
|
| 325 |
def test_def_use_jump_use_def(self): |
| 326 |
graph = self.build_dependency(""" |
| 327 |
[i3] # 0: 1 |
| 328 |
i1 = int_add(i3,1) # 1: 2, 3 |
| 329 |
guard_value(i1,0) [] # 2: 3 |
| 330 |
jump(i1) # 3: |
| 331 |
""") |
| 332 |
self.assert_dependencies(graph, full_check=True) |
| 333 |
|
| 334 |
def test_dependency_guard(self): |
| 335 |
graph = self.build_dependency(""" |
| 336 |
[i3] # 0: 2,3 |
| 337 |
i1 = int_add(1,1) # 1: 2 |
| 338 |
guard_value(i1,0) [i3] # 2: 3 |
| 339 |
jump(i3) # 3: |
| 340 |
""") |
| 341 |
self.assert_dependencies(graph, full_check=True) |
| 342 |
|
| 343 |
def test_dependency_guard_2(self): |
| 344 |
graph = self.build_dependency(""" |
| 345 |
[i1] # 0: 1,2?,3 |
| 346 |
i2 = int_le(i1, 10) # 1: 2 |
| 347 |
guard_true(i2) [i1] # 2: |
| 348 |
i3 = int_add(i1,1) # 3: 4 |
| 349 |
jump(i3) # 4: |
| 350 |
""") |
| 351 |
self.assert_dependencies(graph, full_check=True) |
| 352 |
|
| 353 |
def test_no_edge_duplication(self): |
| 354 |
graph = self.build_dependency(""" |
| 355 |
[i1] # 0: 1,2?,3 |
| 356 |
i2 = int_lt(i1,10) # 1: 2 |
| 357 |
guard_false(i2) [i1] # 2: |
| 358 |
i3 = int_add(i1,i1) # 3: 4 |
| 359 |
jump(i3) # 4: |
| 360 |
""") |
| 361 |
self.assert_dependencies(graph, full_check=True) |
| 362 |
|
| 363 |
def test_no_edge_duplication_in_guard_failargs(self): |
| 364 |
graph = self.build_dependency(""" |
| 365 |
[i1] # 0: 1,2?,3? |
| 366 |
i2 = int_lt(i1,10) # 1: 2 |
| 367 |
guard_false(i2) [i1,i1,i2,i1,i2,i1] # 2: 3 |
| 368 |
jump(i1) # 3: |
| 369 |
""") |
| 370 |
self.assert_dependencies(graph, full_check=True) |
| 371 |
|
| 372 |
def test_dependencies_1(self): |
| 373 |
graph = self.build_dependency(""" |
| 374 |
[i0, i1, i2] # 0: 1,3,6,7,11? |
| 375 |
i4 = int_gt(i1, 0) # 1: 2 |
| 376 |
guard_true(i4) [] # 2: 5, 11? |
| 377 |
i6 = int_sub(i1, 1) # 3: 4 |
| 378 |
i8 = int_gt(i6, 0) # 4: 5 |
| 379 |
guard_false(i8) [] # 5: 10 |
| 380 |
i10 = int_add(i2, 1) # 6: 8 |
| 381 |
i12 = int_sub(i0, 1) # 7: 9, 11 |
| 382 |
i14 = int_add(i10, 1) # 8: 11 |
| 383 |
i16 = int_gt(i12, 0) # 9: 10 |
| 384 |
guard_true(i16) [] # 10: 11 |
| 385 |
jump(i12, i1, i14) # 11: |
| 386 |
""") |
| 387 |
self.assert_dependencies(graph, full_check=True) |
| 388 |
self.assert_independent(graph, 6, 2) |
| 389 |
self.assert_independent(graph, 6, 1) |
| 390 |
|
| 391 |
def test_prevent_double_arg(self): |
| 392 |
graph = self.build_dependency(""" |
| 393 |
[i0, i1, i2] # 0: 1,3 |
| 394 |
i4 = int_gt(i1, i0) # 1: 2 |
| 395 |
guard_true(i4) [] # 2: 3 |
| 396 |
jump(i0, i1, i2) # 3: |
| 397 |
""") |
| 398 |
self.assert_dependencies(graph, full_check=True) |
| 399 |
|
| 400 |
def test_ovf_dep(self): |
| 401 |
graph = self.build_dependency(""" |
| 402 |
[i0, i1, i2] # 0: 2,3 |
| 403 |
i4 = int_sub_ovf(1, 0) # 1: 2 |
| 404 |
guard_overflow() [i2] # 2: 3 |
| 405 |
jump(i0, i1, i2) # 3: |
| 406 |
""") |
| 407 |
self.assert_dependencies(graph, full_check=True) |
| 408 |
|
| 409 |
def test_exception_dep(self): |
| 410 |
graph = self.build_dependency(""" |
| 411 |
[p0, i1, i2] # 0: 1,3? |
| 412 |
i4 = call_i(p0, 1, descr=nonwritedescr) # 1: 2,3 |
| 413 |
guard_no_exception() [] # 2: 3 |
| 414 |
jump(p0, i1, i2) # 3: |
| 415 |
""") |
| 416 |
self.assert_dependencies(graph, full_check=True) |
| 417 |
|
| 418 |
def test_call_dependency_on_ptr_but_not_index_value(self): |
| 419 |
graph = self.build_dependency(""" |
| 420 |
[p0, p1, i2] # 0: 1,2?,3?,4?,5? |
| 421 |
i3 = int_add(i2,1) # 1: 2 |
| 422 |
i4 = call_i(p0, i3, descr=nonwritedescr) # 2: 3,4,5? |
| 423 |
guard_no_exception() [i2] # 3: |
| 424 |
p2 = getarrayitem_gc_r(p1, i3, descr=arraydescr) # 4: 5 |
| 425 |
jump(p2, p1, i3) # 5: |
| 426 |
""") |
| 427 |
self.assert_dependencies(graph, full_check=True) |
| 428 |
|
| 429 |
def test_call_dependency(self): |
| 430 |
graph = self.build_dependency(""" |
| 431 |
[p0, p1, i2, i5] # 0: 1,2?,3?,4?,5? |
| 432 |
i3 = int_add(i2,1) # 1: 2 |
| 433 |
i4 = call_i(i5, i3, descr=nonwritedescr) # 2: 3,4,5? |
| 434 |
guard_no_exception() [i2] # 3: 5? |
| 435 |
p2 = getarrayitem_gc_r(p1,i3,descr=chararraydescr) # 4: 5 |
| 436 |
jump(p2, p1, i3, i5) # 5: |
| 437 |
""") |
| 438 |
self.assert_dependencies(graph, full_check=True) |
| 439 |
|
| 440 |
def test_call_not_forced_exception(self): |
| 441 |
graph = self.build_dependency(""" |
| 442 |
[p0, p1, i2, i5] # 0: 1,2,4?,5,6 |
| 443 |
i4 = call_i(i5, i2, descr=nonwritedescr) # 1: 2,4,6 |
| 444 |
guard_not_forced() [i2] # 2: 3 |
| 445 |
guard_no_exception() [] # 3: 6 |
| 446 |
i3 = int_add(i2,1) # 4: 5 |
| 447 |
p2 = getarrayitem_gc_r(p1,i3,descr=chararraydescr) # 5: 6 |
| 448 |
jump(p2, p1, i2, i5) # 6: |
| 449 |
""") |
| 450 |
self.assert_dependencies(graph, full_check=True) |
| 451 |
assert graph.nodes[1].priority == 100 |
| 452 |
assert graph.nodes[2].priority == 100 |
| 453 |
|
| 454 |
def test_setarrayitem_dependency(self): |
| 455 |
graph = self.build_dependency(""" |
| 456 |
[p0, i1] # 0: 1,2?,3?,4? |
| 457 |
setarrayitem_raw(p0, i1, 1, descr=floatarraydescr) # 1: 2,3 |
| 458 |
i2 = getarrayitem_raw_i(p0, i1, descr=floatarraydescr) # 2: 4 |
| 459 |
setarrayitem_raw(p0, i1, 2, descr=floatarraydescr) # 3: 4 |
| 460 |
jump(p0, i2) # 4: |
| 461 |
""") |
| 462 |
self.assert_dependencies(graph, full_check=True) |
| 463 |
|
| 464 |
def test_setarrayitem_alias_dependency(self): |
| 465 |
|
| 466 |
|
| 467 |
graph = self.build_dependency(""" |
| 468 |
[p0, i1, i2] # 0: 1,2?,3? |
| 469 |
setarrayitem_raw(p0, i1, 1, descr=floatarraydescr) # 1: 2 |
| 470 |
setarrayitem_raw(p0, i2, 2, descr=floatarraydescr) # 2: 3 |
| 471 |
jump(p0, i1, i2) # 3: |
| 472 |
""") |
| 473 |
self.assert_dependencies(graph, full_check=True) |
| 474 |
self.assert_dependent(graph, 1,2) |
| 475 |
|
| 476 |
def test_setarrayitem_dont_depend_with_memref_info(self): |
| 477 |
graph = self.build_dependency(""" |
| 478 |
[p0, i1] # 0: 1,2,3?,4? |
| 479 |
setarrayitem_raw(p0, i1, 1, descr=chararraydescr) # 1: 4 |
| 480 |
i2 = int_add(i1,1) # 2: 3 |
| 481 |
setarrayitem_raw(p0, i2, 2, descr=chararraydescr) # 3: 4 |
| 482 |
jump(p0, i1) # 4: |
| 483 |
""") |
| 484 |
self.assert_dependencies(graph, full_check=True) |
| 485 |
self.assert_independent(graph, 1,2) |
| 486 |
self.assert_independent(graph, 1,3) |
| 487 |
|
| 488 |
def test_dependency_complex_trace(self): |
| 489 |
graph = self.build_dependency(""" |
| 490 |
[i0, i1, i2, i3, i4, i5, i6, i7] # 0: |
| 491 |
i9 = int_mul(i0, 8) # 1: 2 |
| 492 |
i10 = raw_load_i(i3, i9, descr=arraydescr) # 2: 5, 10 |
| 493 |
i11 = int_mul(i0, 8) # 3: 4 |
| 494 |
i12 = raw_load_i(i4, i11, descr=arraydescr) # 4: 5,10 |
| 495 |
i13 = int_add(i10, i12) # 5: 7,10 |
| 496 |
i14 = int_mul(i0, 8) # 6: 7 |
| 497 |
raw_store(i3, i14, i13, descr=arraydescr) # 7: 10,12,20 |
| 498 |
i16 = int_add(i0, 1) # 8: 9,10,11,13,16,18 |
| 499 |
i17 = int_lt(i16, i7) # 9: 10 |
| 500 |
guard_true(i17) [i7, i13, i5, i4, i3, i12, i10, i16] # 10: 17, 20 |
| 501 |
i18 = int_mul(i16, 9) # 11: 12 |
| 502 |
i19 = raw_load_i(i3, i18, descr=arraydescr) # 12: 15, 20 |
| 503 |
i20 = int_mul(i16, 8) # 13: 14 |
| 504 |
i21 = raw_load_i(i4, i20, descr=arraydescr) # 14: 15, 20 |
| 505 |
i22 = int_add(i19, i21) # 15: 17, 20 |
| 506 |
i23 = int_mul(i16, 8) # 16: 17 |
| 507 |
raw_store(i5, i23, i22, descr=arraydescr) # 17: 20 |
| 508 |
i24 = int_add(i16, 1) # 18: 19, 20 |
| 509 |
i25 = int_lt(i24, i7) # 19: 20 |
| 510 |
guard_true(i25) [i7, i22, i5, i4, i3, i21, i19, i24] # 20: |
| 511 |
jump(i24, i19, i21, i3, i4, i5, i22, i7) # 21: |
| 512 |
""") |
| 513 |
self.assert_dependencies(graph, full_check=True) |
| 514 |
self.assert_dependent(graph, 2,12) |
| 515 |
self.assert_dependent(graph, 7,12) |
| 516 |
self.assert_dependent(graph, 4,12) |
| 517 |
|
| 518 |
def test_getfield(self): |
| 519 |
graph = self.build_dependency(""" |
| 520 |
[p0, p1] # 0: 1,2,5 |
| 521 |
p2 = getfield_gc_r(p0, descr=valuedescr) # 1: 3,5 |
| 522 |
p3 = getfield_gc_r(p0, descr=valuedescr) # 2: 4 |
| 523 |
guard_nonnull(p2) [p2] # 3: 4,5 |
| 524 |
guard_nonnull(p3) [p3] # 4: 5 |
| 525 |
jump(p0,p2) # 5: |
| 526 |
""") |
| 527 |
self.assert_dependencies(graph, full_check=True) |
| 528 |
|
| 529 |
def test_cyclic(self): |
| 530 |
graph = self.build_dependency(""" |
| 531 |
[p0, p1, p5, p6, p7, p9, p11, p12] # 0: 1,6 |
| 532 |
p13 = getfield_gc_r(p9, descr=valuedescr) # 1: 2,5 |
| 533 |
guard_nonnull(p13) [] # 2: 4,5 |
| 534 |
i14 = getfield_gc_i(p9, descr=valuedescr) # 3: 5 |
| 535 |
p15 = getfield_gc_r(p13, descr=valuedescr) # 4: 5 |
| 536 |
guard_class(p15, 14073732) [p1, p0, p9, i14, p15, p13, p5, p6, p7] # 5: 6 |
| 537 |
jump(p0,p1,p5,p6,p7,p9,p11,p12) # 6: |
| 538 |
""") |
| 539 |
self.assert_dependencies(graph, full_check=True) |
| 540 |
|
| 541 |
def test_dep_on_vector_op(self): |
| 542 |
graph = self.build_dependency(""" |
| 543 |
[p0, i1] # 0: 1,2,3 |
| 544 |
i19 = int_mul(i1, 8) # 1: 2 |
| 545 |
v20[2xi64] = vec_load_i(p0, i19, 1, 0, descr=arraydescr) # 2: |
| 546 |
jump(p0, i1) # 3: |
| 547 |
""") |
| 548 |
self.assert_dependencies(graph, full_check=True) |
| 549 |
|
| 550 |
|
| 551 |
def test_iterate(self): |
| 552 |
n1,n2,n3,n4,n5 = [FakeNode(i+1) for i in range(5)] |
| 553 |
|
| 554 |
|
| 555 |
n1.edge_to(n2); n2.edge_to(n4); n4.edge_to(n5) |
| 556 |
n1.edge_to(n3); n3.edge_to(n4); |
| 557 |
|
| 558 |
paths = list(n5.iterate_paths(n1, backwards=True)) |
| 559 |
assert all([path.check_acyclic() for path in paths]) |
| 560 |
assert len(paths) == 2 |
| 561 |
assert paths[0].as_str() == "n5 -> n4 -> n2 -> n1" |
| 562 |
assert paths[1].as_str() == "n5 -> n4 -> n3 -> n1" |
| 563 |
paths = list(n1.iterate_paths(n5)) |
| 564 |
assert all([path.check_acyclic() for path in paths]) |
| 565 |
assert len(paths) == 2 |
| 566 |
assert paths[0].as_str() == "n1 -> n2 -> n4 -> n5" |
| 567 |
assert paths[1].as_str() == "n1 -> n3 -> n4 -> n5" |
| 568 |
|
| 569 |
|
| 570 |
def test_iterate_one_many_one(self): |
| 571 |
r = range(19) |
| 572 |
n0 = FakeNode(0) |
| 573 |
nodes = [FakeNode(i+1) for i in r] |
| 574 |
nend = FakeNode(len(r)+1) |
| 575 |
|
| 576 |
assert len(list(n0.iterate_paths(nodes[0], backwards=True))) == 0 |
| 577 |
|
| 578 |
for i in r: |
| 579 |
n0.edge_to(nodes[i]) |
| 580 |
nodes[i].edge_to(nend) |
| 581 |
|
| 582 |
paths = list(nend.iterate_paths(n0, backwards=True)) |
| 583 |
assert all([path.check_acyclic() for path in paths]) |
| 584 |
assert len(paths) == len(r) |
| 585 |
for i in r: |
| 586 |
assert paths[i].as_str() == "n%d -> %s -> n0" % (len(r)+1, nodes[i]) |
| 587 |
|
| 588 |
paths = list(n0.iterate_paths(nend)) |
| 589 |
assert all([path.check_acyclic() for path in paths]) |
| 590 |
assert len(paths) == len(r) |
| 591 |
for i in r: |
| 592 |
assert paths[i].as_str() == "n0 -> %s -> n%d" % (nodes[i], len(r)+1) |
| 593 |
|
| 594 |
def test_iterate_blacklist_diamond(self): |
| 595 |
blacklist = {} |
| 596 |
n1,n2,n3,n4 = [FakeNode(i+1) for i in range(4)] |
| 597 |
|
| 598 |
|
| 599 |
n1.edge_to(n2); n2.edge_to(n4); |
| 600 |
n1.edge_to(n3); n3.edge_to(n4); |
| 601 |
|
| 602 |
paths = list(n1.iterate_paths(n4, blacklist=True)) |
| 603 |
assert len(paths) == 2 |
| 604 |
assert paths[0].as_str() == "n1 -> n2 -> n4" |
| 605 |
assert paths[1].as_str() == "n1 -> n3 -> n4" |
| 606 |
|
| 607 |
def test_iterate_blacklist_double_diamond(self): |
| 608 |
blacklist = {} |
| 609 |
n1,n2,n3,n4,n5,n6,n7,n8 = [FakeNode(i+1) for i in range(8)] |
| 610 |
|
| 611 |
|
| 612 |
n1.edge_to(n2); n2.edge_to(n4); |
| 613 |
n1.edge_to(n3); n3.edge_to(n4); |
| 614 |
n4.edge_to(n5) |
| 615 |
n5.edge_to(n6); n6.edge_to(n8); |
| 616 |
n5.edge_to(n7); n7.edge_to(n8); |
| 617 |
|
| 618 |
paths = list(n1.iterate_paths(n8, blacklist=True)) |
| 619 |
assert len(paths) == 3 |
| 620 |
assert paths[0].as_str() == "n1 -> n2 -> n4 -> n5 -> n6 -> n8" |
| 621 |
assert paths[1].as_str() == "n1 -> n2 -> n4 -> n5 -> n7 -> n8" |
| 622 |
assert paths[2].as_str() == "n1 -> n3 -> n4" |
| 623 |
|
| 624 |
def test_iterate_blacklist_split_path(self): |
| 625 |
blacklist = {} |
| 626 |
n1,n2,n3,n4,n5,n6,n7,n8 = [FakeNode(i+1) for i in range(8)] |
| 627 |
n1.edge_to(n2); |
| 628 |
n3.edge_to(n2); |
| 629 |
n2.edge_to(n4); |
| 630 |
n3.edge_to(n4); |
| 631 |
|
| 632 |
paths = list(n4.iterate_paths(n3, backwards=True, blacklist=True)) |
| 633 |
assert len(paths) == 2 |
| 634 |
assert paths[0].as_str() == "n4 -> n2 -> n3" |
| 635 |
assert paths[1].as_str() == "n4 -> n3" |
| 636 |
|
| 637 |
n5.edge_to(n1) |
| 638 |
n5.edge_to(n3) |
| 639 |
|
| 640 |
paths = list(n4.iterate_paths(n5, backwards=True, blacklist=True)) |
| 641 |
assert len(paths) == 3 |
| 642 |
assert paths[0].as_str() == "n4 -> n2 -> n1 -> n5" |
| 643 |
assert paths[1].as_str() == "n4 -> n2 -> n3 -> n5" |
| 644 |
assert paths[2].as_str() == "n4 -> n3" |
| 645 |
|
| 646 |
def test_sccs(self): |
| 647 |
n1,n2 = FakeNode(1), FakeNode(2) |
| 648 |
n1.edge_to(n2); n2.edge_to(n1) |
| 649 |
|
| 650 |
graph = FakeDependencyGraph([n1,n2]) |
| 651 |
cycle = graph.cycles() |
| 652 |
assert cycle == [n1, n2] |
| 653 |
|
| 654 |
n3 = FakeNode(0) |
| 655 |
graph.nodes = [n3] |
| 656 |
cycle = graph.cycles() |
| 657 |
assert cycle is None |
| 658 |
|
| 659 |
def test_cycles_2(self): |
| 660 |
n1,n2,n3,n4 = FakeNode(1), FakeNode(2), FakeNode(3), FakeNode(4) |
| 661 |
n1.edge_to(n3); n3.edge_to(n4); n4.edge_to(n1) |
| 662 |
|
| 663 |
graph = FakeDependencyGraph([n1,n2]) |
| 664 |
graph.nodes = [n1,n2,n3] |
| 665 |
cycle = graph.cycles() |
| 666 |
assert cycle is not None |
| 667 |
assert cycle == [n1,n3,n4] |
| 668 |
|
| 669 |
class FakeMemoryRefResOp(object): |
| 670 |
def __init__(self, array, descr): |
| 671 |
self.array = array |
| 672 |
self.descr = descr |
| 673 |
def getarg(self, index): |
| 674 |
return self.array |
| 675 |
def getdescr(self): |
| 676 |
return self.descr |
| 677 |
|
| 678 |
FLOAT = ArrayDescr(lltype.GcArray(lltype.Float), None) |
| 679 |
|
| 680 |
def memoryref(array, var, mod=(1,1,0), descr=None, raw=False): |
| 681 |
if descr is None: |
| 682 |
descr = FLOAT |
| 683 |
mul, div, off = mod |
| 684 |
op = FakeMemoryRefResOp(array, descr) |
| 685 |
return MemoryRef(op, |
| 686 |
IndexVar(var, mul, div, off), |
| 687 |
raw) |
| 688 |
|
| 689 |
class FakeBox(object): |
| 690 |
pass |
| 691 |
|
| 692 |
class FakeNode(Node): |
| 693 |
def __init__(self, i): |
| 694 |
Node.__init__(self, None, i) |
| 695 |
pass |
| 696 |
|
| 697 |
def __repr__(self): |
| 698 |
return "n%d" % self.opidx |