1 USING: compiler.tree.escape-analysis
2 compiler.tree.escape-analysis.allocations compiler.tree.builder
3 compiler.tree.recursive compiler.tree.normalization
4 math.functions compiler.tree.propagation compiler.tree.cleanup
5 compiler.tree.combinators compiler.tree sequences math
6 math.private kernel tools.test accessors slots.private
7 quotations.private prettyprint classes.tuple.private classes
8 classes.tuple namespaces
9 compiler.tree.propagation.info stack-checker.errors
10 compiler.tree.checker compiler.tree.def-use compiler.tree.dead-code
11 kernel.private vectors ;
12 IN: compiler.tree.escape-analysis.tests
14 GENERIC: count-unboxed-allocations* ( m node -- n )
16 : (count-unboxed-allocations) ( m node -- n )
17 out-d>> first escaping-allocation? [ 1 + ] unless ;
19 M: #call count-unboxed-allocations*
20 dup immutable-tuple-boa?
21 [ (count-unboxed-allocations) ] [ drop ] if ;
23 M: #push count-unboxed-allocations*
24 dup literal>> class-of immutable-tuple-class?
25 [ (count-unboxed-allocations) ] [ drop ] if ;
27 M: #introduce count-unboxed-allocations*
28 out-d>> [ escaping-allocation? [ 1 + ] unless ] each ;
30 M: node count-unboxed-allocations* drop ;
32 : count-unboxed-allocations ( quot -- sizes )
42 0 swap [ count-unboxed-allocations* ] each-node ;
44 { 0 } [ [ [ + ] curry ] count-unboxed-allocations ] unit-test
46 { 1 } [ [ [ + ] curry drop ] count-unboxed-allocations ] unit-test
48 { 1 } [ [ [ + ] curry 3 slot ] count-unboxed-allocations ] unit-test
50 { 1 } [ [ [ + ] curry 3 slot drop ] count-unboxed-allocations ] unit-test
52 { 1 } [ [ [ + ] curry uncurry ] count-unboxed-allocations ] unit-test
54 { 1 } [ [ [ + ] curry call ] count-unboxed-allocations ] unit-test
56 { 1 } [ [ [ + ] curry call ] count-unboxed-allocations ] unit-test
58 { 0 } [ [ [ [ + ] curry ] [ drop [ ] ] if ] count-unboxed-allocations ] unit-test
61 [ [ [ + ] curry ] [ [ * ] curry ] if uncurry ] count-unboxed-allocations
65 [ [ [ + ] curry ] [ [ * ] curry ] if ] count-unboxed-allocations
69 [ [ [ + ] curry ] [ dup [ [ * ] curry ] [ [ / ] curry ] if ] if uncurry ] count-unboxed-allocations
73 [ [ [ + ] curry 4 ] [ dup [ [ * ] curry ] [ [ / ] curry ] if uncurry ] if ] count-unboxed-allocations
77 [ [ [ + ] curry ] [ dup [ [ * ] curry ] [ [ / ] curry ] if ] if ] count-unboxed-allocations
80 TUPLE: cons { car read-only } { cdr read-only } ;
91 ] count-unboxed-allocations
105 ] count-unboxed-allocations
117 ] count-unboxed-allocations
131 ] count-unboxed-allocations
145 ] count-unboxed-allocations
150 [ dup cons boa ] [ drop 1 2 cons boa ] if car>>
151 ] count-unboxed-allocations
157 [ cons boa ] [ cons boa 3 cons boa ] if
158 [ car>> ] [ cdr>> ] bi
159 ] count-unboxed-allocations
164 3dup [ cons boa ] [ cons boa . 1 2 cons boa ] if
165 [ car>> ] [ cdr>> ] bi
166 ] count-unboxed-allocations
170 [ [ 3 cons boa ] [ "A" throw ] if car>> ]
171 count-unboxed-allocations
175 [ 10 [ drop ] each-integer ] count-unboxed-allocations
180 1 2 cons boa 10 [ drop 2 cons boa ] each-integer car>>
181 ] count-unboxed-allocations
184 : infinite-cons-loop ( a -- b ) 2 cons boa infinite-cons-loop ; inline recursive
188 1 2 cons boa infinite-cons-loop
189 ] count-unboxed-allocations
196 { 0 } [ [ <rw-box> i>> ] count-unboxed-allocations ] unit-test
198 : fake-fib ( m -- n )
199 dup i>> 1 <= [ drop 1 <rw-box> ] when ; inline recursive
201 { 0 } [ [ <rw-box> fake-fib i>> ] count-unboxed-allocations ] unit-test
203 TUPLE: ro-box { i read-only } ;
207 : tuple-fib ( m -- n )
216 swap i>> swap i>> + <ro-box>
217 ] if ; inline recursive
219 { 5 } [ [ <ro-box> tuple-fib i>> ] count-unboxed-allocations ] unit-test
221 { 3 } [ [ <ro-box> tuple-fib ] count-unboxed-allocations ] unit-test
223 : tuple-fib' ( m -- n )
224 dup 1 <= [ 1 - tuple-fib' i>> ] when <ro-box> ; inline recursive
226 { 0 } [ [ tuple-fib' ] count-unboxed-allocations ] unit-test
228 : bad-tuple-fib-1 ( m -- n )
236 bad-tuple-fib-1 dup .
237 swap i>> swap i>> + <ro-box>
238 ] if ; inline recursive
240 { 3 } [ [ <ro-box> bad-tuple-fib-1 i>> ] count-unboxed-allocations ] unit-test
242 : bad-tuple-fib-2 ( m -- n )
252 swap i>> swap i>> + <ro-box>
253 ] if ; inline recursive
255 { 2 } [ [ <ro-box> bad-tuple-fib-2 i>> ] count-unboxed-allocations ] unit-test
257 : tuple-fib-2 ( m -- n )
264 swap i>> swap i>> + <ro-box>
265 ] if ; inline recursive
267 { 2 } [ [ tuple-fib-2 i>> ] count-unboxed-allocations ] unit-test
269 : tuple-fib-3 ( m -- n )
275 1 - tuple-fib-3 dup .
276 swap i>> swap i>> + <ro-box>
277 ] if ; inline recursive
279 { 0 } [ [ tuple-fib-3 i>> ] count-unboxed-allocations ] unit-test
281 : bad-tuple-fib-3 ( m -- n )
285 1 - dup bad-tuple-fib-3
289 ] if ; inline recursive
291 { 0 } [ [ bad-tuple-fib-3 i>> ] count-unboxed-allocations ] unit-test
293 { 1 } [ [ complex boa >rect ] count-unboxed-allocations ] unit-test
295 { 0 } [ [ 1 cons boa 2 cons boa ] count-unboxed-allocations ] unit-test
297 { 1 } [ [ 1 cons boa 2 cons boa car>> ] count-unboxed-allocations ] unit-test
299 { 0 } [ [ 1 cons boa 2 cons boa dup . car>> ] count-unboxed-allocations ] unit-test
301 { 0 } [ [ 1 cons boa "x" get slot ] count-unboxed-allocations ] unit-test
304 [ dup -1 over >= [ 0 >= [ "A" throw ] unless ] [ drop ] if ]
305 count-unboxed-allocations
309 [ \ too-many->r boa f f \ inference-error boa ]
310 count-unboxed-allocations
313 ! Doug found a regression
317 { } [ [ empty-tuple boa layout-of ] count-unboxed-allocations drop ] unit-test
321 { 1 } [ [ { complex } declare real>> ] count-unboxed-allocations ] unit-test
324 [ { complex } declare [ real>> ] [ imaginary>> ] bi ]
325 count-unboxed-allocations
329 [ { vector } declare length>> ]
330 count-unboxed-allocations
333 ! Bug found while tweaking benchmark.raytracer-simd
335 TUPLE: point-2d { x read-only } { y read-only } ;
336 TUPLE: point-3d < point-2d { z read-only } ;
339 [ { point-2d } declare dup point-3d? [ z>> ] [ x>> ] if ]
340 count-unboxed-allocations
344 [ point-2d boa dup point-3d? [ z>> ] [ x>> ] if ]
345 count-unboxed-allocations