| 1 |
import py |
| 2 |
from rpython.rtyper.lltypesystem import lltype, llmemory |
| 3 |
from rpython.rtyper.lltypesystem.lloperation import llop |
| 4 |
from rpython.rtyper.lltypesystem.rstr import STR |
| 5 |
from rpython.rtyper.lltypesystem.rlist import LIST_OF |
| 6 |
from rpython.translator.translator import TranslationContext, graphof |
| 7 |
from rpython.translator.backendopt.writeanalyze import WriteAnalyzer, top_set |
| 8 |
from rpython.translator.backendopt.writeanalyze import ReadWriteAnalyzer |
| 9 |
from rpython.translator.backendopt.all import backend_optimizations |
| 10 |
from rpython.conftest import option |
| 11 |
|
| 12 |
|
| 13 |
class BaseTest(object): |
| 14 |
Analyzer = WriteAnalyzer |
| 15 |
|
| 16 |
def translate(self, func, sig): |
| 17 |
t = TranslationContext() |
| 18 |
t.buildannotator().build_types(func, sig) |
| 19 |
t.buildrtyper().specialize() |
| 20 |
if option.view: |
| 21 |
t.view() |
| 22 |
return t, self.Analyzer(t) |
| 23 |
|
| 24 |
|
| 25 |
class TestWriteAnalyze(BaseTest): |
| 26 |
|
| 27 |
def test_writes_simple(self): |
| 28 |
def g(x): |
| 29 |
return True |
| 30 |
|
| 31 |
def f(x): |
| 32 |
return g(x - 1) |
| 33 |
t, wa = self.translate(f, [int]) |
| 34 |
fgraph = graphof(t, f) |
| 35 |
result = wa.analyze(fgraph.startblock.operations[0]) |
| 36 |
assert not result |
| 37 |
|
| 38 |
def test_writes_recursive(self): |
| 39 |
from rpython.translator.transform import insert_ll_stackcheck |
| 40 |
def g(x): |
| 41 |
return f(x) |
| 42 |
|
| 43 |
def f(x): |
| 44 |
if x: |
| 45 |
return g(x - 1) |
| 46 |
return 1 |
| 47 |
t, wa = self.translate(f, [int]) |
| 48 |
insert_ll_stackcheck(t) |
| 49 |
ggraph = graphof(t, g) |
| 50 |
result = wa.analyze(ggraph.startblock.operations[-1]) |
| 51 |
assert not result |
| 52 |
|
| 53 |
def test_write_to_new_struct(self): |
| 54 |
class A(object): |
| 55 |
pass |
| 56 |
def f(x): |
| 57 |
a = A() |
| 58 |
a.baz = x |
| 59 |
return a |
| 60 |
t, wa = self.translate(f, [int]) |
| 61 |
fgraph = graphof(t, f) |
| 62 |
result = wa.analyze_direct_call(fgraph) |
| 63 |
assert not result |
| 64 |
|
| 65 |
def test_write_to_new_struct_2(self): |
| 66 |
class A(object): |
| 67 |
pass |
| 68 |
def f(x): |
| 69 |
a = A() |
| 70 |
|
| 71 |
i = 10 |
| 72 |
while i > 0: |
| 73 |
i -= 1 |
| 74 |
|
| 75 |
a.baz = x |
| 76 |
return a |
| 77 |
t, wa = self.translate(f, [int]) |
| 78 |
fgraph = graphof(t, f) |
| 79 |
result = wa.analyze_direct_call(fgraph) |
| 80 |
assert not result |
| 81 |
|
| 82 |
def test_write_to_new_struct_3(self): |
| 83 |
class A(object): |
| 84 |
pass |
| 85 |
prebuilt = A() |
| 86 |
def f(x): |
| 87 |
if x > 5: |
| 88 |
a = A() |
| 89 |
else: |
| 90 |
a = A() |
| 91 |
a.baz = x |
| 92 |
return a |
| 93 |
t, wa = self.translate(f, [int]) |
| 94 |
fgraph = graphof(t, f) |
| 95 |
result = wa.analyze_direct_call(fgraph) |
| 96 |
assert not result |
| 97 |
|
| 98 |
def test_write_to_new_struct_4(self): |
| 99 |
class A(object): |
| 100 |
pass |
| 101 |
prebuilt = A() |
| 102 |
def f(x): |
| 103 |
if x > 5: |
| 104 |
a = A() |
| 105 |
else: |
| 106 |
a = prebuilt |
| 107 |
a.baz = x |
| 108 |
return a |
| 109 |
t, wa = self.translate(f, [int]) |
| 110 |
fgraph = graphof(t, f) |
| 111 |
result = wa.analyze_direct_call(fgraph) |
| 112 |
assert len(result) == 1 and 'baz' in list(result)[0][-1] |
| 113 |
|
| 114 |
def test_write_to_new_struct_5(self): |
| 115 |
class A(object): |
| 116 |
baz = 123 |
| 117 |
def f(x): |
| 118 |
if x: |
| 119 |
a = A() |
| 120 |
else: |
| 121 |
a = A() |
| 122 |
a.baz += 1 |
| 123 |
t, wa = self.translate(f, [int]) |
| 124 |
fgraph = graphof(t, f) |
| 125 |
result = wa.analyze_direct_call(fgraph) |
| 126 |
assert not result |
| 127 |
|
| 128 |
def test_method(self): |
| 129 |
class A(object): |
| 130 |
def f(self): |
| 131 |
self.x = 1 |
| 132 |
return 1 |
| 133 |
def m(self): |
| 134 |
raise ValueError |
| 135 |
class B(A): |
| 136 |
def f(self): |
| 137 |
return 2 |
| 138 |
def m(self): |
| 139 |
return 3 |
| 140 |
def f(a): |
| 141 |
return a.f() |
| 142 |
def m(a): |
| 143 |
return a.m() |
| 144 |
def h(flag): |
| 145 |
if flag: |
| 146 |
obj = A() |
| 147 |
else: |
| 148 |
obj = B() |
| 149 |
f(obj) |
| 150 |
m(obj) |
| 151 |
|
| 152 |
t, wa = self.translate(h, [int]) |
| 153 |
hgraph = graphof(t, h) |
| 154 |
|
| 155 |
block = hgraph.startblock.exits[0].target.exits[0].target |
| 156 |
op_call_f = block.operations[0] |
| 157 |
op_call_m = block.operations[1] |
| 158 |
|
| 159 |
|
| 160 |
def check_call(op, fname): |
| 161 |
assert op.opname == "direct_call" |
| 162 |
assert op.args[0].value._obj._name == fname |
| 163 |
check_call(op_call_f, "f") |
| 164 |
check_call(op_call_m, "m") |
| 165 |
|
| 166 |
result = wa.analyze(op_call_f) |
| 167 |
assert len(result) == 1 |
| 168 |
(struct, T, name), = result |
| 169 |
assert struct == "struct" |
| 170 |
assert name.endswith("x") |
| 171 |
assert not wa.analyze(op_call_m) |
| 172 |
|
| 173 |
def test_instantiate(self): |
| 174 |
from rpython.rlib.objectmodel import instantiate |
| 175 |
class A: |
| 176 |
pass |
| 177 |
class B(A): |
| 178 |
pass |
| 179 |
def g(x): |
| 180 |
if x: |
| 181 |
C = A |
| 182 |
else: |
| 183 |
C = B |
| 184 |
a = instantiate(C) |
| 185 |
def f(x): |
| 186 |
return g(x) |
| 187 |
t, wa = self.translate(f, [int]) |
| 188 |
fgraph = graphof(t, f) |
| 189 |
result = wa.analyze(fgraph.startblock.operations[0]) |
| 190 |
assert not result |
| 191 |
|
| 192 |
def test_llexternal(self): |
| 193 |
from rpython.rtyper.lltypesystem.rffi import llexternal |
| 194 |
z = llexternal('z', [lltype.Signed], lltype.Signed) |
| 195 |
def f(x): |
| 196 |
return z(x) |
| 197 |
t, wa = self.translate(f, [int]) |
| 198 |
fgraph = graphof(t, f) |
| 199 |
backend_optimizations(t) |
| 200 |
assert fgraph.startblock.operations[0].opname == 'direct_call' |
| 201 |
|
| 202 |
result = wa.analyze(fgraph.startblock.operations[0]) |
| 203 |
assert not result |
| 204 |
|
| 205 |
def test_contains(self): |
| 206 |
def g(x, y, z): |
| 207 |
l = [x] |
| 208 |
return f(l, y, z) |
| 209 |
def f(x, y, z): |
| 210 |
return y in x |
| 211 |
|
| 212 |
|
| 213 |
t, wa = self.translate(g, [int, int, int]) |
| 214 |
ggraph = graphof(t, g) |
| 215 |
assert ggraph.startblock.operations[-1].opname == 'direct_call' |
| 216 |
|
| 217 |
result = wa.analyze(ggraph.startblock.operations[-1]) |
| 218 |
assert not result |
| 219 |
|
| 220 |
def test_list(self): |
| 221 |
def g(x, y, z): |
| 222 |
return f(x, y, z) |
| 223 |
def f(x, y, z): |
| 224 |
l = [0] * x |
| 225 |
l.append(y) |
| 226 |
return len(l) + z |
| 227 |
|
| 228 |
|
| 229 |
t, wa = self.translate(g, [int, int, int]) |
| 230 |
ggraph = graphof(t, g) |
| 231 |
assert ggraph.startblock.operations[0].opname == 'direct_call' |
| 232 |
|
| 233 |
result = sorted(wa.analyze(ggraph.startblock.operations[0])) |
| 234 |
array, A = result[0] |
| 235 |
assert array == "array" |
| 236 |
assert A.TO.OF == lltype.Signed |
| 237 |
|
| 238 |
struct, S1, name = result[1] |
| 239 |
assert struct == "struct" |
| 240 |
assert S1.TO.items == A |
| 241 |
assert S1.TO.length == lltype.Signed |
| 242 |
assert name == "items" |
| 243 |
|
| 244 |
struct, S2, name = result[2] |
| 245 |
assert struct == "struct" |
| 246 |
assert name == "length" |
| 247 |
assert S1 is S2 |
| 248 |
|
| 249 |
def test_llexternal_with_callback(self): |
| 250 |
from rpython.rtyper.lltypesystem.rffi import llexternal |
| 251 |
from rpython.rtyper.lltypesystem import lltype |
| 252 |
|
| 253 |
class Abc: |
| 254 |
pass |
| 255 |
abc = Abc() |
| 256 |
|
| 257 |
FUNC = lltype.FuncType([lltype.Signed], lltype.Signed) |
| 258 |
z = llexternal('z', [lltype.Ptr(FUNC)], lltype.Signed) |
| 259 |
def g(n): |
| 260 |
abc.foobar = n |
| 261 |
return n + 1 |
| 262 |
def f(x): |
| 263 |
return z(g) |
| 264 |
t, wa = self.translate(f, [int]) |
| 265 |
fgraph = graphof(t, f) |
| 266 |
backend_optimizations(t) |
| 267 |
assert fgraph.startblock.operations[0].opname == 'direct_call' |
| 268 |
|
| 269 |
result = wa.analyze(fgraph.startblock.operations[0]) |
| 270 |
assert len(result) == 1 |
| 271 |
(struct, T, name), = result |
| 272 |
assert struct == "struct" |
| 273 |
assert name.endswith("foobar") |
| 274 |
|
| 275 |
|
| 276 |
class TestLLtypeReadWriteAnalyze(BaseTest): |
| 277 |
Analyzer = ReadWriteAnalyzer |
| 278 |
|
| 279 |
def test_read_simple(self): |
| 280 |
def g(x): |
| 281 |
return True |
| 282 |
|
| 283 |
def f(x): |
| 284 |
return g(x - 1) |
| 285 |
t, wa = self.translate(f, [int]) |
| 286 |
fgraph = graphof(t, f) |
| 287 |
result = wa.analyze(fgraph.startblock.operations[0]) |
| 288 |
assert not result |
| 289 |
|
| 290 |
def test_read_really(self): |
| 291 |
class A(object): |
| 292 |
def __init__(self, y): |
| 293 |
self.y = y |
| 294 |
def f(self): |
| 295 |
self.x = 1 |
| 296 |
return self.y |
| 297 |
def h(flag): |
| 298 |
obj = A(flag) |
| 299 |
return obj.f() |
| 300 |
|
| 301 |
t, wa = self.translate(h, [int]) |
| 302 |
hgraph = graphof(t, h) |
| 303 |
op_call_f = hgraph.startblock.operations[-1] |
| 304 |
|
| 305 |
|
| 306 |
assert op_call_f.opname == "direct_call" |
| 307 |
assert op_call_f.args[0].value._obj._name == 'A.f' |
| 308 |
|
| 309 |
result = wa.analyze(op_call_f) |
| 310 |
assert len(result) == 2 |
| 311 |
result = list(result) |
| 312 |
result.sort() |
| 313 |
[(struct1, T1, name1), (struct2, T2, name2)] = result |
| 314 |
assert struct1 == "readstruct" |
| 315 |
assert name1.endswith("y") |
| 316 |
assert struct2 == "struct" |
| 317 |
assert name2.endswith("x") |
| 318 |
assert T1 == T2 |
| 319 |
|
| 320 |
def test_cutoff(self): |
| 321 |
py.test.skip("cutoff: disabled") |
| 322 |
from rpython.rlib.unroll import unrolling_iterable |
| 323 |
cutoff = 20 |
| 324 |
attrs = unrolling_iterable(["s%s" % i for i in range(cutoff + 5)]) |
| 325 |
|
| 326 |
class A(object): |
| 327 |
def __init__(self, y): |
| 328 |
for attr in attrs: |
| 329 |
setattr(self, attr, y) |
| 330 |
def f(self): |
| 331 |
self.x = 1 |
| 332 |
res = 0 |
| 333 |
for attr in attrs: |
| 334 |
res += getattr(self, attr) |
| 335 |
return res |
| 336 |
|
| 337 |
def h(flag): |
| 338 |
obj = A(flag) |
| 339 |
return obj.f() |
| 340 |
|
| 341 |
t, wa = self.translate(h, [int]) |
| 342 |
wa.cutoff = cutoff |
| 343 |
hgraph = graphof(t, h) |
| 344 |
op_call_f = hgraph.startblock.operations[-1] |
| 345 |
|
| 346 |
|
| 347 |
assert op_call_f.opname == "direct_call" |
| 348 |
assert op_call_f.args[0].value._obj._name == 'A.f' |
| 349 |
|
| 350 |
result = wa.analyze(op_call_f) |
| 351 |
assert result is top_set |
| 352 |
|
| 353 |
def test_contains(self): |
| 354 |
def g(x, y, z): |
| 355 |
l = [x] |
| 356 |
return f(l, y, z) |
| 357 |
def f(x, y, z): |
| 358 |
return y in x |
| 359 |
|
| 360 |
t, wa = self.translate(g, [int, int, int]) |
| 361 |
ggraph = graphof(t, g) |
| 362 |
assert ggraph.startblock.operations[-1].opname == 'direct_call' |
| 363 |
|
| 364 |
result = wa.analyze(ggraph.startblock.operations[-1]) |
| 365 |
ARRAYPTR = list(result)[0][1] |
| 366 |
assert list(result) == [("readarray", ARRAYPTR)] |
| 367 |
assert isinstance(ARRAYPTR.TO, lltype.GcArray) |
| 368 |
|
| 369 |
def test_adt_method(self): |
| 370 |
def ll_callme(n): |
| 371 |
return n |
| 372 |
ll_callme = lltype.staticAdtMethod(ll_callme) |
| 373 |
S = lltype.GcStruct('S', ('x', lltype.Signed), |
| 374 |
adtmeths = {'yep': True, |
| 375 |
'callme': ll_callme}) |
| 376 |
def g(p, x, y, z): |
| 377 |
p.x = x |
| 378 |
if p.yep: |
| 379 |
z *= p.callme(y) |
| 380 |
return z |
| 381 |
def f(x, y, z): |
| 382 |
p = lltype.malloc(S) |
| 383 |
return g(p, x, y, z) |
| 384 |
|
| 385 |
t, wa = self.translate(f, [int, int, int]) |
| 386 |
fgraph = graphof(t, f) |
| 387 |
assert fgraph.startblock.operations[-1].opname == 'direct_call' |
| 388 |
|
| 389 |
result = wa.analyze(fgraph.startblock.operations[-1]) |
| 390 |
assert list(result) == [("struct", lltype.Ptr(S), "x")] |
| 391 |
|
| 392 |
def test_interiorfield(self): |
| 393 |
A = lltype.GcArray(lltype.Struct('x', ('x', lltype.Signed), |
| 394 |
('y', lltype.Signed))) |
| 395 |
|
| 396 |
def g(x): |
| 397 |
a = lltype.malloc(A, 1) |
| 398 |
a[0].y = 3 |
| 399 |
return f(a, x) |
| 400 |
|
| 401 |
def f(a, x): |
| 402 |
a[0].x = x |
| 403 |
return a[0].y |
| 404 |
|
| 405 |
t, wa = self.translate(g, [int]) |
| 406 |
ggraph = graphof(t, g) |
| 407 |
result = wa.analyze(ggraph.startblock.operations[-1]) |
| 408 |
res = list(result) |
| 409 |
assert ('readinteriorfield', lltype.Ptr(A), 'y') in res |
| 410 |
assert ('interiorfield', lltype.Ptr(A), 'x') in res |
| 411 |
|
| 412 |
|
| 413 |
class TestGcLoadStoreIndexed(BaseTest): |
| 414 |
Analyzer = ReadWriteAnalyzer |
| 415 |
|
| 416 |
def _analyze_graph(self, t, wa, fn): |
| 417 |
graph = graphof(t, fn) |
| 418 |
result = wa.analyze(graph.startblock.operations[-1]) |
| 419 |
return result |
| 420 |
|
| 421 |
def _filter_reads(self, effects): |
| 422 |
result = [item for item in effects if not item[0].startswith('read')] |
| 423 |
return frozenset(result) |
| 424 |
|
| 425 |
def test_gc_load_indexed_str(self): |
| 426 |
from rpython.rlib.buffer import StringBuffer |
| 427 |
|
| 428 |
def typed_read(buf): |
| 429 |
return buf.typed_read(lltype.Signed, 0) |
| 430 |
|
| 431 |
def direct_read(buf): |
| 432 |
return buf.value[0] |
| 433 |
|
| 434 |
def f(x): |
| 435 |
buf = StringBuffer(x) |
| 436 |
return direct_read(buf), typed_read(buf) |
| 437 |
|
| 438 |
t, wa = self.translate(f, [str]) |
| 439 |
|
| 440 |
direct_effects = self._analyze_graph(t, wa, direct_read) |
| 441 |
assert direct_effects == frozenset([ |
| 442 |
('readinteriorfield', lltype.Ptr(STR), 'chars') |
| 443 |
]) |
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
typed_effects = self._analyze_graph(t, wa, typed_read) |
| 449 |
assert direct_effects.issubset(typed_effects) |
| 450 |
|
| 451 |
def test_gc_load_indexed_list_of_chars(self): |
| 452 |
from rpython.rlib.buffer import ByteBuffer |
| 453 |
|
| 454 |
def typed_read(buf): |
| 455 |
return buf.typed_read(lltype.Signed, 0) |
| 456 |
|
| 457 |
def direct_read(buf): |
| 458 |
return buf.data[0] |
| 459 |
|
| 460 |
def f(x): |
| 461 |
buf = ByteBuffer(8) |
| 462 |
return direct_read(buf), typed_read(buf) |
| 463 |
|
| 464 |
t, wa = self.translate(f, [str]) |
| 465 |
|
| 466 |
LIST = LIST_OF(lltype.Char) |
| 467 |
PLIST = lltype.Ptr(LIST) |
| 468 |
direct_effects = self._analyze_graph(t, wa, direct_read) |
| 469 |
assert direct_effects == frozenset([ |
| 470 |
('readstruct', PLIST, 'length'), |
| 471 |
('readstruct', PLIST, 'items'), |
| 472 |
('readarray', LIST.items), |
| 473 |
]) |
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
typed_effects = self._analyze_graph(t, wa, typed_read) |
| 478 |
expected = frozenset([ |
| 479 |
('readstruct', PLIST, 'items'), |
| 480 |
('readarray', LIST.items), |
| 481 |
]) |
| 482 |
assert expected.issubset(typed_effects) |
| 483 |
|
| 484 |
def test_gc_store_indexed_str(self): |
| 485 |
from rpython.rlib.mutbuffer import MutableStringBuffer |
| 486 |
|
| 487 |
def typed_write(buf): |
| 488 |
return buf.typed_write(lltype.Signed, 0, 42) |
| 489 |
|
| 490 |
def direct_write(buf): |
| 491 |
return buf.setitem(0, 'A') |
| 492 |
|
| 493 |
def f(x): |
| 494 |
buf = MutableStringBuffer(8) |
| 495 |
return direct_write(buf), typed_write(buf) |
| 496 |
|
| 497 |
t, wa = self.translate(f, [str]) |
| 498 |
|
| 499 |
direct_effects = self._analyze_graph(t, wa, direct_write) |
| 500 |
direct_effects = self._filter_reads(direct_effects) |
| 501 |
assert direct_effects == frozenset([ |
| 502 |
('interiorfield', lltype.Ptr(STR), 'chars') |
| 503 |
]) |
| 504 |
|
| 505 |
typed_effects = self._analyze_graph(t, wa, typed_write) |
| 506 |
typed_effects = self._filter_reads(typed_effects) |
| 507 |
assert typed_effects == direct_effects |
| 508 |
|
| 509 |
def test_gc_store_indexed_list_of_chars(self): |
| 510 |
from rpython.rlib.buffer import ByteBuffer |
| 511 |
|
| 512 |
def typed_write(buf): |
| 513 |
return buf.typed_write(lltype.Signed, 0, 42) |
| 514 |
|
| 515 |
def direct_write(buf): |
| 516 |
return buf.setitem(0, 'A') |
| 517 |
|
| 518 |
def f(x): |
| 519 |
buf = ByteBuffer(8) |
| 520 |
return direct_write(buf), typed_write(buf) |
| 521 |
|
| 522 |
t, wa = self.translate(f, [str]) |
| 523 |
|
| 524 |
LIST = LIST_OF(lltype.Char) |
| 525 |
direct_effects = self._analyze_graph(t, wa, direct_write) |
| 526 |
direct_effects = self._filter_reads(direct_effects) |
| 527 |
assert direct_effects == frozenset([ |
| 528 |
('array', LIST.items), |
| 529 |
]) |
| 530 |
|
| 531 |
typed_effects = self._analyze_graph(t, wa, typed_write) |
| 532 |
typed_effects = self._filter_reads(typed_effects) |
| 533 |
assert typed_effects == direct_effects |
| 534 |
|
| 535 |
def test_explanation(self): |
| 536 |
class A(object): |
| 537 |
def methodname(self): |
| 538 |
self.x = 1 |
| 539 |
return 1 |
| 540 |
def m(self): |
| 541 |
raise ValueError |
| 542 |
class B(A): |
| 543 |
def methodname(self): |
| 544 |
return 2 |
| 545 |
def m(self): |
| 546 |
return 3 |
| 547 |
def fancyname(a): |
| 548 |
return a.methodname() |
| 549 |
def m(a): |
| 550 |
return a.m() |
| 551 |
def h(flag): |
| 552 |
if flag: |
| 553 |
obj = A() |
| 554 |
else: |
| 555 |
obj = B() |
| 556 |
fancyname(obj) |
| 557 |
m(obj) |
| 558 |
|
| 559 |
t, wa = self.translate(h, [int]) |
| 560 |
hgraph = graphof(t, h) |
| 561 |
|
| 562 |
block = hgraph.startblock.exits[0].target.exits[0].target |
| 563 |
op_call_fancyname = block.operations[0] |
| 564 |
|
| 565 |
explanation = wa.explain_analyze_slowly(op_call_fancyname) |
| 566 |
assert "fancyname" in explanation[0] |
| 567 |
assert "methodname" in explanation[1] |