| 1 |
import py |
| 2 |
from rpython.translator.backendopt.malloc import LLTypeMallocRemover |
| 3 |
from rpython.translator.backendopt.all import backend_optimizations |
| 4 |
from rpython.translator.translator import TranslationContext, graphof |
| 5 |
from rpython.translator import simplify |
| 6 |
from rpython.flowspace.model import checkgraph, Block, mkentrymap |
| 7 |
from rpython.rtyper.llinterp import LLInterpreter |
| 8 |
from rpython.rtyper.lltypesystem import lltype, llmemory |
| 9 |
from rpython.rlib import objectmodel |
| 10 |
from rpython.conftest import option |
| 11 |
|
| 12 |
class TestMallocRemoval(object): |
| 13 |
MallocRemover = LLTypeMallocRemover |
| 14 |
|
| 15 |
def check_malloc_removed(cls, graph): |
| 16 |
remover = cls.MallocRemover() |
| 17 |
checkgraph(graph) |
| 18 |
count1 = count2 = 0 |
| 19 |
for node in graph.iterblocks(): |
| 20 |
for op in node.operations: |
| 21 |
if op.opname == cls.MallocRemover.MALLOC_OP: |
| 22 |
S = op.args[0].value |
| 23 |
if not remover.union_wrapper(S): |
| 24 |
count1 += 1 |
| 25 |
if op.opname in ('direct_call', 'indirect_call'): |
| 26 |
count2 += 1 |
| 27 |
assert count1 == 0 |
| 28 |
assert count2 == 0 |
| 29 |
check_malloc_removed = classmethod(check_malloc_removed) |
| 30 |
|
| 31 |
def check(self, fn, signature, args, expected_result, must_be_removed=True, |
| 32 |
inline=None): |
| 33 |
remover = self.MallocRemover() |
| 34 |
t = TranslationContext() |
| 35 |
t.buildannotator().build_types(fn, signature) |
| 36 |
t.buildrtyper().specialize() |
| 37 |
graph = graphof(t, fn) |
| 38 |
if inline is not None: |
| 39 |
from rpython.translator.backendopt.inline import auto_inline_graphs |
| 40 |
auto_inline_graphs(t, t.graphs, inline) |
| 41 |
if option.view: |
| 42 |
t.view() |
| 43 |
|
| 44 |
|
| 45 |
while True: |
| 46 |
progress = remover.remove_mallocs_once(graph) |
| 47 |
simplify.transform_dead_op_vars_in_blocks(list(graph.iterblocks()), |
| 48 |
[graph]) |
| 49 |
if progress and option.view: |
| 50 |
t.view() |
| 51 |
if expected_result is not Ellipsis: |
| 52 |
interp = LLInterpreter(t.rtyper) |
| 53 |
res = interp.eval_graph(graph, args) |
| 54 |
assert res == expected_result |
| 55 |
if not progress: |
| 56 |
break |
| 57 |
if must_be_removed: |
| 58 |
self.check_malloc_removed(graph) |
| 59 |
return graph |
| 60 |
|
| 61 |
def test_fn1(self): |
| 62 |
def fn1(x, y): |
| 63 |
if x > 0: |
| 64 |
t = x+y, x-y |
| 65 |
else: |
| 66 |
t = x-y, x+y |
| 67 |
s, d = t |
| 68 |
return s*d |
| 69 |
self.check(fn1, [int, int], [15, 10], 125) |
| 70 |
|
| 71 |
def test_fn2(self): |
| 72 |
class T: |
| 73 |
pass |
| 74 |
def fn2(x, y): |
| 75 |
t = T() |
| 76 |
t.x = x |
| 77 |
t.y = y |
| 78 |
if x > 0: |
| 79 |
return t.x + t.y |
| 80 |
else: |
| 81 |
return t.x - t.y |
| 82 |
self.check(fn2, [int, int], [-6, 7], -13) |
| 83 |
|
| 84 |
def test_fn3(self): |
| 85 |
def fn3(x): |
| 86 |
a, ((b, c), d, e) = x+1, ((x+2, x+3), x+4, x+5) |
| 87 |
return a+b+c+d+e |
| 88 |
self.check(fn3, [int], [10], 65) |
| 89 |
|
| 90 |
def test_fn4(self): |
| 91 |
class A: |
| 92 |
pass |
| 93 |
class B(A): |
| 94 |
pass |
| 95 |
def fn4(i): |
| 96 |
a = A() |
| 97 |
b = B() |
| 98 |
a.b = b |
| 99 |
b.i = i |
| 100 |
return a.b.i |
| 101 |
self.check(fn4, [int], [42], 42) |
| 102 |
|
| 103 |
def test_fn5(self): |
| 104 |
class A: |
| 105 |
attr = 666 |
| 106 |
class B(A): |
| 107 |
attr = 42 |
| 108 |
def fn5(): |
| 109 |
b = B() |
| 110 |
return b.attr |
| 111 |
self.check(fn5, [], [], 42) |
| 112 |
|
| 113 |
def test_aliasing(self): |
| 114 |
class A: |
| 115 |
pass |
| 116 |
def fn6(n): |
| 117 |
a1 = A() |
| 118 |
a1.x = 5 |
| 119 |
a2 = A() |
| 120 |
a2.x = 6 |
| 121 |
if n > 0: |
| 122 |
a = a1 |
| 123 |
else: |
| 124 |
a = a2 |
| 125 |
a.x = 12 |
| 126 |
return a1.x |
| 127 |
self.check(fn6, [int], [1], 12, must_be_removed=False) |
| 128 |
|
| 129 |
def test_aliasing_identity_op(self): |
| 130 |
class A: |
| 131 |
pass |
| 132 |
class B(A): |
| 133 |
pass |
| 134 |
T = lltype.GcStruct('T', ('z', lltype.Signed)) |
| 135 |
S = lltype.GcStruct('S', ('t', T), |
| 136 |
('x', lltype.Signed), |
| 137 |
('y', lltype.Signed)) |
| 138 |
def fn(n): |
| 139 |
a1 = lltype.malloc(S) |
| 140 |
a1.x = 5 |
| 141 |
a2 = lltype.malloc(S) |
| 142 |
a2.x = 6 |
| 143 |
if n > 0: |
| 144 |
a = a1 |
| 145 |
a = lltype.cast_pointer(lltype.Ptr(T), a1) |
| 146 |
|
| 147 |
else: |
| 148 |
a = a2 |
| 149 |
a = lltype.cast_pointer(lltype.Ptr(T), a2) |
| 150 |
a = lltype.cast_pointer(lltype.Ptr(S), a) |
| 151 |
a.x = 12 |
| 152 |
return a1.x |
| 153 |
self.check(fn, [int], [1], 12, must_be_removed=False) |
| 154 |
|
| 155 |
def test_merge(self): |
| 156 |
class A(object): pass |
| 157 |
class B(A): pass |
| 158 |
|
| 159 |
def fn(x): |
| 160 |
if x: |
| 161 |
a = A() |
| 162 |
a.x = x |
| 163 |
else: |
| 164 |
a = A() |
| 165 |
a.x = 17 |
| 166 |
return a.x |
| 167 |
self.check(fn, [int], [0], 17) |
| 168 |
|
| 169 |
def test_bogus_cast_pointer(self): |
| 170 |
class S: |
| 171 |
pass |
| 172 |
class T(S): |
| 173 |
def f(self): |
| 174 |
self.y += 1 |
| 175 |
def f(x): |
| 176 |
T().y = 5 |
| 177 |
s = S() |
| 178 |
s.x = 123 |
| 179 |
if x < 0: |
| 180 |
s.f() |
| 181 |
return s.x |
| 182 |
graph = self.check(f, [int], [5], 123, inline=20) |
| 183 |
found_operations = {} |
| 184 |
for block in graph.iterblocks(): |
| 185 |
for op in block.operations: |
| 186 |
found_operations[op.opname] = True |
| 187 |
assert 'debug_fatalerror' in found_operations |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
def test_dont_remove_with__del__(self): |
| 192 |
import os |
| 193 |
delcalls = [0] |
| 194 |
class A(object): |
| 195 |
nextid = 0 |
| 196 |
def __init__(self): |
| 197 |
self.id = self.nextid |
| 198 |
self.nextid += 1 |
| 199 |
|
| 200 |
def __del__(self): |
| 201 |
delcalls[0] += 1 |
| 202 |
|
| 203 |
|
| 204 |
def f(x=int): |
| 205 |
a = A() |
| 206 |
i = 0 |
| 207 |
while i < x: |
| 208 |
a = A() |
| 209 |
os.write(1, str(delcalls[0]) + "\n") |
| 210 |
i += 1 |
| 211 |
return 1 |
| 212 |
t = TranslationContext() |
| 213 |
t.buildannotator().build_types(f, [int]) |
| 214 |
t.buildrtyper().specialize() |
| 215 |
graph = graphof(t, f) |
| 216 |
backend_optimizations(t) |
| 217 |
op = graph.startblock.exits[0].target.exits[1].target.operations[0] |
| 218 |
assert op.opname == "malloc" |
| 219 |
|
| 220 |
def test_wrapper_cannot_be_removed(self): |
| 221 |
SMALL = lltype.OpaqueType('SMALL') |
| 222 |
BIG = lltype.GcStruct('BIG', ('z', lltype.Signed), ('s', SMALL)) |
| 223 |
|
| 224 |
def g(small): |
| 225 |
return -1 |
| 226 |
def fn(): |
| 227 |
b = lltype.malloc(BIG) |
| 228 |
g(b.s) |
| 229 |
|
| 230 |
self.check(fn, [], [], None, must_be_removed=False) |
| 231 |
|
| 232 |
def test_direct_fieldptr(self): |
| 233 |
S = lltype.GcStruct('S', ('x', lltype.Signed)) |
| 234 |
|
| 235 |
def fn(): |
| 236 |
s = lltype.malloc(S) |
| 237 |
s.x = 11 |
| 238 |
p = lltype.direct_fieldptr(s, 'x') |
| 239 |
return p[0] |
| 240 |
|
| 241 |
self.check(fn, [], [], 11) |
| 242 |
|
| 243 |
def test_direct_fieldptr_2(self): |
| 244 |
T = lltype.GcStruct('T', ('z', lltype.Signed)) |
| 245 |
S = lltype.GcStruct('S', ('t', T), |
| 246 |
('x', lltype.Signed), |
| 247 |
('y', lltype.Signed)) |
| 248 |
def fn(): |
| 249 |
s = lltype.malloc(S) |
| 250 |
s.x = 10 |
| 251 |
s.t.z = 1 |
| 252 |
px = lltype.direct_fieldptr(s, 'x') |
| 253 |
py = lltype.direct_fieldptr(s, 'y') |
| 254 |
pz = lltype.direct_fieldptr(s.t, 'z') |
| 255 |
py[0] = 31 |
| 256 |
return px[0] + s.y + pz[0] |
| 257 |
|
| 258 |
self.check(fn, [], [], 42) |
| 259 |
|
| 260 |
def test_getarraysubstruct(self): |
| 261 |
py.test.skip("fails because of the interior structure changes") |
| 262 |
U = lltype.Struct('U', ('n', lltype.Signed)) |
| 263 |
for length in [1, 2]: |
| 264 |
S = lltype.GcStruct('S', ('a', lltype.FixedSizeArray(U, length))) |
| 265 |
for index in range(length): |
| 266 |
|
| 267 |
def fn(): |
| 268 |
s = lltype.malloc(S) |
| 269 |
s.a[index].n = 12 |
| 270 |
return s.a[index].n |
| 271 |
self.check(fn, [], [], 12) |
| 272 |
|
| 273 |
def test_ptr_nonzero(self): |
| 274 |
S = lltype.GcStruct('S') |
| 275 |
def fn(): |
| 276 |
s = lltype.malloc(S) |
| 277 |
return bool(s) |
| 278 |
self.check(fn, [], [], True) |
| 279 |
|
| 280 |
def test_substruct_not_accessed(self): |
| 281 |
SMALL = lltype.Struct('SMALL', ('x', lltype.Signed)) |
| 282 |
BIG = lltype.GcStruct('BIG', ('z', lltype.Signed), ('s', SMALL)) |
| 283 |
def fn(): |
| 284 |
x = lltype.malloc(BIG) |
| 285 |
while x.z < 10: |
| 286 |
x.z += 3 |
| 287 |
return x.z |
| 288 |
self.check(fn, [], [], 12) |
| 289 |
|
| 290 |
def test_union(self): |
| 291 |
py.test.skip("fails because of the interior structure changes") |
| 292 |
UNION = lltype.Struct('UNION', ('a', lltype.Signed), ('b', lltype.Signed), |
| 293 |
hints = {'union': True}) |
| 294 |
BIG = lltype.GcStruct('BIG', ('u1', UNION), ('u2', UNION)) |
| 295 |
def fn(): |
| 296 |
x = lltype.malloc(BIG) |
| 297 |
x.u1.a = 3 |
| 298 |
x.u2.b = 6 |
| 299 |
return x.u1.b * x.u2.a |
| 300 |
self.check(fn, [], [], Ellipsis) |
| 301 |
|
| 302 |
def test_keep_all_keepalives(self): |
| 303 |
SIZE = llmemory.sizeof(lltype.Signed) |
| 304 |
PARRAY = lltype.Ptr(lltype.FixedSizeArray(lltype.Signed, 1)) |
| 305 |
class A: |
| 306 |
def __init__(self): |
| 307 |
self.addr = llmemory.raw_malloc(SIZE) |
| 308 |
def __del__(self): |
| 309 |
llmemory.raw_free(self.addr) |
| 310 |
class B: |
| 311 |
pass |
| 312 |
def myfunc(): |
| 313 |
b = B() |
| 314 |
b.keep = A() |
| 315 |
b.data = llmemory.cast_adr_to_ptr(b.keep.addr, PARRAY) |
| 316 |
b.data[0] = 42 |
| 317 |
ptr = b.data |
| 318 |
|
| 319 |
|
| 320 |
res = ptr[0] |
| 321 |
|
| 322 |
objectmodel.keepalive_until_here(b) |
| 323 |
return res |
| 324 |
graph = self.check(myfunc, [], [], 42, |
| 325 |
must_be_removed=False) |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
entrymap = mkentrymap(graph) |
| 331 |
[link] = entrymap[graph.returnblock] |
| 332 |
assert link.prevblock.operations[-1].opname == 'keepalive' |
| 333 |
|
| 334 |
def test_nested_struct(self): |
| 335 |
S = lltype.GcStruct("S", ('x', lltype.Signed)) |
| 336 |
T = lltype.GcStruct("T", ('s', S)) |
| 337 |
def f(x): |
| 338 |
t = lltype.malloc(T) |
| 339 |
s = t.s |
| 340 |
if x: |
| 341 |
s.x = x |
| 342 |
return t.s.x + s.x |
| 343 |
graph = self.check(f, [int], [42], 2 * 42) |
| 344 |
|
| 345 |
def test_interior_ptr(self): |
| 346 |
py.test.skip("fails") |
| 347 |
S = lltype.Struct("S", ('x', lltype.Signed)) |
| 348 |
T = lltype.GcStruct("T", ('s', S)) |
| 349 |
def f(x): |
| 350 |
t = lltype.malloc(T) |
| 351 |
t.s.x = x |
| 352 |
return t.s.x |
| 353 |
graph = self.check(f, [int], [42], 42) |
| 354 |
|
| 355 |
def test_interior_ptr_with_index(self): |
| 356 |
S = lltype.Struct("S", ('x', lltype.Signed)) |
| 357 |
T = lltype.GcArray(S) |
| 358 |
def f(x): |
| 359 |
t = lltype.malloc(T, 1) |
| 360 |
t[0].x = x |
| 361 |
return t[0].x |
| 362 |
graph = self.check(f, [int], [42], 42) |
| 363 |
|
| 364 |
def test_interior_ptr_with_field_and_index(self): |
| 365 |
S = lltype.Struct("S", ('x', lltype.Signed)) |
| 366 |
T = lltype.GcStruct("T", ('items', lltype.Array(S))) |
| 367 |
def f(x): |
| 368 |
t = lltype.malloc(T, 1) |
| 369 |
t.items[0].x = x |
| 370 |
return t.items[0].x |
| 371 |
graph = self.check(f, [int], [42], 42) |
| 372 |
|
| 373 |
def test_interior_ptr_with_index_and_field(self): |
| 374 |
S = lltype.Struct("S", ('x', lltype.Signed)) |
| 375 |
T = lltype.Struct("T", ('s', S)) |
| 376 |
U = lltype.GcArray(T) |
| 377 |
def f(x): |
| 378 |
u = lltype.malloc(U, 1) |
| 379 |
u[0].s.x = x |
| 380 |
return u[0].s.x |
| 381 |
graph = self.check(f, [int], [42], 42) |
| 382 |
|
| 383 |
def test_two_paths_one_with_constant(self): |
| 384 |
py.test.skip("XXX implement me?") |
| 385 |
def fn(n): |
| 386 |
if n > 100: |
| 387 |
tup = (0,) |
| 388 |
else: |
| 389 |
tup = (n,) |
| 390 |
(n,) |
| 391 |
return tup[0] |
| 392 |
|
| 393 |
self.check(fn, [int], [42], 42) |