PageRenderTime 29ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/rpython/translator/backendopt/test/test_malloc.py

https://bitbucket.org/pypy/pypy/
Python | 353 lines | 307 code | 37 blank | 9 comment | 29 complexity | a9c75ce84e78d0d56901baa8a9880eda MD5 | raw file
Possible License(s): AGPL-3.0, BSD-3-Clause, Apache-2.0
  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. class TestMallocRemoval(object):
  12. MallocRemover = LLTypeMallocRemover
  13. def check_malloc_removed(cls, graph):
  14. remover = cls.MallocRemover()
  15. checkgraph(graph)
  16. count1 = count2 = 0
  17. for node in graph.iterblocks():
  18. for op in node.operations:
  19. if op.opname == cls.MallocRemover.MALLOC_OP:
  20. S = op.args[0].value
  21. if not remover.union_wrapper(S): # union wrappers are fine
  22. count1 += 1
  23. if op.opname in ('direct_call', 'indirect_call'):
  24. count2 += 1
  25. assert count1 == 0 # number of mallocs left
  26. assert count2 == 0 # number of calls left
  27. check_malloc_removed = classmethod(check_malloc_removed)
  28. def check(self, fn, signature, args, expected_result, must_be_removed=True,
  29. inline=None):
  30. remover = self.MallocRemover()
  31. t = TranslationContext()
  32. t.buildannotator().build_types(fn, signature)
  33. t.buildrtyper().specialize()
  34. graph = graphof(t, fn)
  35. if inline is not None:
  36. from rpython.translator.backendopt.inline import auto_inline_graphs
  37. auto_inline_graphs(t, t.graphs, inline)
  38. if option.view:
  39. t.view()
  40. # to detect broken intermediate graphs,
  41. # we do the loop ourselves instead of calling remove_simple_mallocs()
  42. while True:
  43. progress = remover.remove_mallocs_once(graph)
  44. simplify.transform_dead_op_vars_in_blocks(list(graph.iterblocks()),
  45. [graph])
  46. if progress and option.view:
  47. t.view()
  48. if expected_result is not Ellipsis:
  49. interp = LLInterpreter(t.rtyper)
  50. res = interp.eval_graph(graph, args)
  51. assert res == expected_result
  52. if not progress:
  53. break
  54. if must_be_removed:
  55. self.check_malloc_removed(graph)
  56. return graph
  57. def test_fn1(self):
  58. def fn1(x, y):
  59. if x > 0:
  60. t = x+y, x-y
  61. else:
  62. t = x-y, x+y
  63. s, d = t
  64. return s*d
  65. self.check(fn1, [int, int], [15, 10], 125)
  66. def test_fn2(self):
  67. class T:
  68. pass
  69. def fn2(x, y):
  70. t = T()
  71. t.x = x
  72. t.y = y
  73. if x > 0:
  74. return t.x + t.y
  75. else:
  76. return t.x - t.y
  77. self.check(fn2, [int, int], [-6, 7], -13)
  78. def test_fn3(self):
  79. def fn3(x):
  80. a, ((b, c), d, e) = x+1, ((x+2, x+3), x+4, x+5)
  81. return a+b+c+d+e
  82. self.check(fn3, [int], [10], 65)
  83. def test_fn4(self):
  84. class A:
  85. pass
  86. class B(A):
  87. pass
  88. def fn4(i):
  89. a = A()
  90. b = B()
  91. a.b = b
  92. b.i = i
  93. return a.b.i
  94. self.check(fn4, [int], [42], 42)
  95. def test_fn5(self):
  96. class A:
  97. attr = 666
  98. class B(A):
  99. attr = 42
  100. def fn5():
  101. b = B()
  102. return b.attr
  103. self.check(fn5, [], [], 42)
  104. def test_aliasing(self):
  105. class A:
  106. pass
  107. def fn6(n):
  108. a1 = A()
  109. a1.x = 5
  110. a2 = A()
  111. a2.x = 6
  112. if n > 0:
  113. a = a1
  114. else:
  115. a = a2
  116. a.x = 12
  117. return a1.x
  118. self.check(fn6, [int], [1], 12, must_be_removed=False)
  119. def test_bogus_cast_pointer(self):
  120. class S:
  121. pass
  122. class T(S):
  123. def f(self):
  124. self.y += 1
  125. def f(x):
  126. T().y = 5
  127. s = S()
  128. s.x = 123
  129. if x < 0:
  130. s.f()
  131. return s.x
  132. graph = self.check(f, [int], [5], 123, inline=20)
  133. found_operations = {}
  134. for block in graph.iterblocks():
  135. for op in block.operations:
  136. found_operations[op.opname] = True
  137. assert 'debug_fatalerror' in found_operations
  138. def test_dont_remove_with__del__(self):
  139. import os
  140. delcalls = [0]
  141. class A(object):
  142. nextid = 0
  143. def __init__(self):
  144. self.id = self.nextid
  145. self.nextid += 1
  146. def __del__(self):
  147. delcalls[0] += 1
  148. #os.write(1, "__del__\n")
  149. def f(x=int):
  150. a = A()
  151. i = 0
  152. while i < x:
  153. a = A()
  154. os.write(1, str(delcalls[0]) + "\n")
  155. i += 1
  156. return 1
  157. t = TranslationContext()
  158. t.buildannotator().build_types(f, [int])
  159. t.buildrtyper().specialize()
  160. graph = graphof(t, f)
  161. backend_optimizations(t)
  162. op = graph.startblock.exits[0].target.exits[1].target.operations[0]
  163. assert op.opname == "malloc"
  164. def test_wrapper_cannot_be_removed(self):
  165. SMALL = lltype.OpaqueType('SMALL')
  166. BIG = lltype.GcStruct('BIG', ('z', lltype.Signed), ('s', SMALL))
  167. def g(small):
  168. return -1
  169. def fn():
  170. b = lltype.malloc(BIG)
  171. g(b.s)
  172. self.check(fn, [], [], None, must_be_removed=False)
  173. def test_direct_fieldptr(self):
  174. S = lltype.GcStruct('S', ('x', lltype.Signed))
  175. def fn():
  176. s = lltype.malloc(S)
  177. s.x = 11
  178. p = lltype.direct_fieldptr(s, 'x')
  179. return p[0]
  180. self.check(fn, [], [], 11)
  181. def test_direct_fieldptr_2(self):
  182. T = lltype.GcStruct('T', ('z', lltype.Signed))
  183. S = lltype.GcStruct('S', ('t', T),
  184. ('x', lltype.Signed),
  185. ('y', lltype.Signed))
  186. def fn():
  187. s = lltype.malloc(S)
  188. s.x = 10
  189. s.t.z = 1
  190. px = lltype.direct_fieldptr(s, 'x')
  191. py = lltype.direct_fieldptr(s, 'y')
  192. pz = lltype.direct_fieldptr(s.t, 'z')
  193. py[0] = 31
  194. return px[0] + s.y + pz[0]
  195. self.check(fn, [], [], 42)
  196. def test_getarraysubstruct(self):
  197. py.test.skip("fails because of the interior structure changes")
  198. U = lltype.Struct('U', ('n', lltype.Signed))
  199. for length in [1, 2]:
  200. S = lltype.GcStruct('S', ('a', lltype.FixedSizeArray(U, length)))
  201. for index in range(length):
  202. def fn():
  203. s = lltype.malloc(S)
  204. s.a[index].n = 12
  205. return s.a[index].n
  206. self.check(fn, [], [], 12)
  207. def test_ptr_nonzero(self):
  208. S = lltype.GcStruct('S')
  209. def fn():
  210. s = lltype.malloc(S)
  211. return bool(s)
  212. self.check(fn, [], [], True)
  213. def test_substruct_not_accessed(self):
  214. SMALL = lltype.Struct('SMALL', ('x', lltype.Signed))
  215. BIG = lltype.GcStruct('BIG', ('z', lltype.Signed), ('s', SMALL))
  216. def fn():
  217. x = lltype.malloc(BIG)
  218. while x.z < 10: # makes several blocks
  219. x.z += 3
  220. return x.z
  221. self.check(fn, [], [], 12)
  222. def test_union(self):
  223. py.test.skip("fails because of the interior structure changes")
  224. UNION = lltype.Struct('UNION', ('a', lltype.Signed), ('b', lltype.Signed),
  225. hints = {'union': True})
  226. BIG = lltype.GcStruct('BIG', ('u1', UNION), ('u2', UNION))
  227. def fn():
  228. x = lltype.malloc(BIG)
  229. x.u1.a = 3
  230. x.u2.b = 6
  231. return x.u1.b * x.u2.a
  232. self.check(fn, [], [], Ellipsis)
  233. def test_keep_all_keepalives(self):
  234. SIZE = llmemory.sizeof(lltype.Signed)
  235. PARRAY = lltype.Ptr(lltype.FixedSizeArray(lltype.Signed, 1))
  236. class A:
  237. def __init__(self):
  238. self.addr = llmemory.raw_malloc(SIZE)
  239. def __del__(self):
  240. llmemory.raw_free(self.addr)
  241. class B:
  242. pass
  243. def myfunc():
  244. b = B()
  245. b.keep = A()
  246. b.data = llmemory.cast_adr_to_ptr(b.keep.addr, PARRAY)
  247. b.data[0] = 42
  248. ptr = b.data
  249. # normally 'b' could go away as early as here, which would free
  250. # the memory held by the instance of A in b.keep...
  251. res = ptr[0]
  252. # ...so we explicitly keep 'b' alive until here
  253. objectmodel.keepalive_until_here(b)
  254. return res
  255. graph = self.check(myfunc, [], [], 42,
  256. must_be_removed=False) # 'A' instance left
  257. # there is a getarrayitem near the end of the graph of myfunc.
  258. # However, the memory it accesses must still be protected by the
  259. # following keepalive, even after malloc removal
  260. entrymap = mkentrymap(graph)
  261. [link] = entrymap[graph.returnblock]
  262. assert link.prevblock.operations[-1].opname == 'keepalive'
  263. def test_nested_struct(self):
  264. S = lltype.GcStruct("S", ('x', lltype.Signed))
  265. T = lltype.GcStruct("T", ('s', S))
  266. def f(x):
  267. t = lltype.malloc(T)
  268. s = t.s
  269. if x:
  270. s.x = x
  271. return t.s.x + s.x
  272. graph = self.check(f, [int], [42], 2 * 42)
  273. def test_interior_ptr(self):
  274. py.test.skip("fails")
  275. S = lltype.Struct("S", ('x', lltype.Signed))
  276. T = lltype.GcStruct("T", ('s', S))
  277. def f(x):
  278. t = lltype.malloc(T)
  279. t.s.x = x
  280. return t.s.x
  281. graph = self.check(f, [int], [42], 42)
  282. def test_interior_ptr_with_index(self):
  283. S = lltype.Struct("S", ('x', lltype.Signed))
  284. T = lltype.GcArray(S)
  285. def f(x):
  286. t = lltype.malloc(T, 1)
  287. t[0].x = x
  288. return t[0].x
  289. graph = self.check(f, [int], [42], 42)
  290. def test_interior_ptr_with_field_and_index(self):
  291. S = lltype.Struct("S", ('x', lltype.Signed))
  292. T = lltype.GcStruct("T", ('items', lltype.Array(S)))
  293. def f(x):
  294. t = lltype.malloc(T, 1)
  295. t.items[0].x = x
  296. return t.items[0].x
  297. graph = self.check(f, [int], [42], 42)
  298. def test_interior_ptr_with_index_and_field(self):
  299. S = lltype.Struct("S", ('x', lltype.Signed))
  300. T = lltype.Struct("T", ('s', S))
  301. U = lltype.GcArray(T)
  302. def f(x):
  303. u = lltype.malloc(U, 1)
  304. u[0].s.x = x
  305. return u[0].s.x
  306. graph = self.check(f, [int], [42], 42)
  307. def test_two_paths_one_with_constant(self):
  308. py.test.skip("XXX implement me?")
  309. def fn(n):
  310. if n > 100:
  311. tup = (0,)
  312. else:
  313. tup = (n,)
  314. (n,) # <- flowspace
  315. return tup[0]
  316. self.check(fn, [int], [42], 42)