vile / scheme / native_pairs_d_vile_native_test.v
240 lines · 225 sloc · 10.03 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import reader
4
5fn native_pair_test_list(n int) Value {
6 mut xs := nil_value()
7 for i in 1 .. n + 1 { xs = cons(integer(i),xs) }
8 return xs
9}
10
11fn test_native_existing_integer_list_fold_runs_in_one_entry() {
12 mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})!
13 vm.eval('(define (fold xs a)(if (null? (cdr xs)) (+ a (car xs)) (fold (cdr xs) (+ a (car xs)))))')!
14 xs := native_pair_test_list(3000)
15 tail := xs.pair_pointer().cdr
16 vm.with_native_execution(fn [xs,tail] (mut vm VM) !Value {
17 assert vm.call(vm.lookup('fold')!,[xs,integer(0)])!.whole() == 4501500
18 assert xs.pair_pointer().car.whole() == 3000
19 assert xs.pair_pointer().cdr.pointer() == tail.pointer()
20 hits,deopts := vm.native_execution_stats()
21 assert hits == 1 && deopts == 0
22 return unspecified()
23 })!
24}
25
26fn test_native_pair_input_plan_handles_nil_then_mutated_existing_lists() {
27 mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})!
28 vm.eval('(define xs (list 1 2 3))
29 (define (fold xs a)(if (null? xs) a (fold (cdr xs) (+ a (car xs)))))')!
30 vm.with_native_execution(fn (mut vm VM) !Value {
31 fold := vm.lookup('fold')!
32 assert vm.call(fold,[nil_value(),integer(0)])!.whole() == 0
33 assert vm.call(fold,[vm.lookup('xs')!,integer(0)])!.whole() == 6
34 vm.eval('(set-car! xs 10) (set-cdr! (cdr xs) (list 30 40))')!
35 $if gcboehm ? { gc_collect() }
36 assert vm.call(fold,[vm.lookup('xs')!,integer(0)])!.whole() == 82
37 hits,deopts := vm.native_execution_stats()
38 assert hits == 3 && deopts == 0
39 return unspecified()
40 })!
41}
42
43fn test_native_pair_inputs_refresh_globals_and_same_code_captures() {
44 mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})!
45 vm.eval('(define xs (list 1 2 3))
46 (define (global)(+ (car xs) (car (cdr xs))))
47 (define (make values)(lambda()(+ (car values) (car (cdr values)) (car (cdr (cdr values))))))
48 (define a (make (list 1 2 3))) (define b (make (list 10 20 30)))')!
49 vm.with_native_execution(fn (mut vm VM) !Value {
50 assert vm.call(vm.lookup('global')!,[])!.whole() == 3
51 assert vm.call(vm.lookup('a')!,[])!.whole() == 6
52 assert vm.call(vm.lookup('b')!,[])!.whole() == 60
53 vm.eval('(set! xs (list 10 20)) (set! a (make (list 100 200 300)))')!
54 $if gcboehm ? { gc_collect() }
55 assert vm.call(vm.lookup('global')!,[])!.whole() == 30
56 assert vm.call(vm.lookup('a')!,[])!.whole() == 600
57 _,deopts := vm.native_execution_stats()
58 assert deopts == 0
59 return unspecified()
60 })!
61}
62
63fn test_native_pair_inputs_share_capture_imports_without_exporting_identity() {
64 mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})!
65 xs := native_pair_test_list(1300)
66 vm.define('xs',xs,reader.ReadOptions{})!
67 vm.eval('(define run ((lambda(xs)(lambda(a b)(+ (car a) (car b) (car xs) (car xs)))) xs))')!
68 vm.with_native_execution(fn [xs] (mut vm VM) !Value {
69 assert vm.call(vm.lookup('run')!,[xs,xs])!.whole() == 5200
70 assert xs.pair_pointer().car.whole() == 1300
71 hits,deopts := vm.native_execution_stats()
72 assert hits == 1 && deopts == 0
73 return unspecified()
74 })!
75}
76
77fn test_native_pair_import_rejections_are_bounded_and_keep_scalar_entries() {
78 mut cycle := cons(integer(7),nil_value())
79 mut cell := cycle.pair_pointer()
80 cell.cdr = cycle
81 mut setup := new_with_options(Options{max_steps: 0,max_frames: 0})!
82 big := setup.eval('9223372036854775808')!
83 for bad in [cons(real(1.5),nil_value()),cons(big,nil_value()),cons(string_value('head'),nil_value()),
84 cons(integer(1),integer(2)),cycle,native_pair_test_list(4097)] {
85 mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})!
86 vm.eval('(define (work xs)(if (pair? xs) (car xs) 42))')!
87 work := vm.lookup('work')!
88 expected := write(vm.call(work,[bad])!)!
89 vm.with_native_execution(fn [bad,expected] (mut vm VM) !Value {
90 work := vm.lookup('work')!
91 assert vm.call(work,[nil_value()])!.whole() == 42
92 assert write(vm.call(work,[bad])!)! == expected
93 scope := unsafe { &NativeScope(vm.native_execution_scope) }
94 visited := scope.pair_nodes_visited
95 assert visited > 0 && visited <= 4096 && scope.pair_input_rejections == 1
96 for _ in 0 .. 16 { assert write(vm.call(work,[bad])!)! == expected }
97 assert scope.pair_nodes_visited == visited && scope.pair_input_rejections == 1
98 assert vm.call(work,[integer(3)])!.whole() == 42
99 assert vm.call(work,[nil_value()])!.whole() == 42
100 assert scope.hits == 3 && scope.deopts == 0
101 return unspecified()
102 })!
103 }
104}
105
106fn test_native_pair_pool_capacity_includes_imports_and_private_cons() {
107 assert sizeof(C.VileNativePair) == 16
108 for n in [4095,4096] {
109 mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})!
110 vm.eval('(define (work xs)(car (cons 7 xs)))')!
111 xs := native_pair_test_list(n)
112 vm.with_native_execution(fn [xs,n] (mut vm VM) !Value {
113 assert vm.call(vm.lookup('work')!,[xs])!.whole() == 7
114 hits,deopts := vm.native_execution_stats()
115 if n == 4095 { assert hits == 1 && deopts == 0 } else { assert deopts == 1 }
116 assert xs.pair_pointer().car.whole() == n
117 return unspecified()
118 })!
119 }
120 mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})!
121 vm.eval('(define (fold xs a)(if (null? xs) a (fold (cdr xs) (+ a (car xs)))))')!
122 xs := native_pair_test_list(4096)
123 vm.with_native_execution(fn [xs] (mut vm VM) !Value {
124 assert vm.call(vm.lookup('fold')!,[xs,integer(0)])!.whole() == 8390656
125 hits,deopts := vm.native_execution_stats()
126 assert hits == 1 && deopts == 0
127 return unspecified()
128 })!
129}
130
131fn test_native_pair_results_and_stores_replay_with_original_tail_identity() {
132 mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})!
133 vm.eval('(define (tail xs v)(vector-set! v 0 (+ (vector-ref v 0) 1)) (cdr xs))
134 (define (store xs v)(vector-set! v 0 (+ (vector-ref v 0) 1)) (vector-set! v 1 (cdr xs)) 42)
135 (define cell (let ((held (quote ()))) (list
136 (lambda(xs v)(vector-set! v 0 (+ (vector-ref v 0) 1)) (set! held (cdr xs)) 42)
137 (lambda()held)))) (define capture (car cell)) (define get-held (cadr cell))')!
138 xs := native_pair_test_list(3)
139 tail := xs.pair_pointer().cdr
140 vm.with_native_execution(fn [xs,tail] (mut vm VM) !Value {
141 v := vector([integer(0),boolean(false)])
142 assert vm.call(vm.lookup('tail')!,[xs,v])!.pointer() == tail.pointer()
143 assert v.vector_pointer().items[0].whole() == 1
144 assert vm.call(vm.lookup('store')!,[xs,v])!.whole() == 42
145 assert v.vector_pointer().items[0].whole() == 2
146 assert v.vector_pointer().items[1].pointer() == tail.pointer()
147 assert vm.call(vm.lookup('capture')!,[xs,v])!.whole() == 42
148 assert v.vector_pointer().items[0].whole() == 3
149 assert vm.call(vm.lookup('get-held')!,[])!.pointer() == tail.pointer()
150 _,deopts := vm.native_execution_stats()
151 assert deopts >= 3
152 return unspecified()
153 })!
154}
155
156fn test_native_pair_overflow_discards_scalar_and_vector_speculation_once() {
157 mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})!
158 vm.eval('(define count 0)
159 (define (make)(let ((count 0))(list
160 (lambda(xs v)(set! count (+ count (car xs)))
161 (vector-set! v 0 (+ (vector-ref v 0) (car xs))) (+ 9223372036854775807 1))
162 (lambda()count))))
163 (define cells (make)) (define work (car cells)) (define get (cadr cells))')!
164 xs := native_pair_test_list(3)
165 vm.with_native_execution(fn [xs] (mut vm VM) !Value {
166 v := vector([integer(0)])
167 assert vm.call(vm.lookup('work')!,[xs,v])!.tag == .big_integer
168 assert vm.call(vm.lookup('get')!,[])!.whole() == 3
169 assert v.vector_pointer().items[0].whole() == 3
170 assert xs.pair_pointer().car.whole() == 3
171 _,deopts := vm.native_execution_stats()
172 assert deopts > 0
173 return unspecified()
174 })!
175}
176
177fn test_native_pair_vector_reads_stay_on_the_existing_replay_path() {
178 mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})!
179 vm.eval('(define (work v)(vector-set! v 0 (+ (vector-ref v 0) 1)) (car (vector-ref v 1)))')!
180 xs := native_pair_test_list(77)
181 vm.with_native_execution(fn [xs] (mut vm VM) !Value {
182 v := vector([integer(0),xs])
183 assert vm.call(vm.lookup('work')!,[v])!.whole() == 77
184 assert v.vector_pointer().items[0].whole() == 1
185 _,deopts := vm.native_execution_stats()
186 assert deopts == 1
187 return unspecified()
188 })!
189}
190
191fn test_native_pair_input_folds_still_obey_vm_instruction_budgets() {
192 mut vm := new_with_options(Options{max_steps: 200,max_frames: 0})!
193 vm.eval('(define (fold xs a)(if (null? xs) a (fold (cdr xs) (+ a (car xs)))))')!
194 xs := native_pair_test_list(3000)
195 vm.with_native_execution(fn [xs] (mut vm VM) !Value {
196 if _ := vm.call(vm.lookup('fold')!,[xs,integer(0)]) { return error('pair fold bypassed its budget') }
197 hits,_ := vm.native_execution_stats()
198 assert hits == 0
199 return unspecified()
200 })!
201}
202
203fn test_native_pair_inputs_work_with_vile_frontend() {
204 mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})!
205 vm.eval_alt('work := fn(xs) { car(xs) + car(cdr(xs)) }')!
206 vm.with_native_execution(fn (mut vm VM) !Value {
207 assert vm.call(vm.lookup('work')!,[list([integer(20),integer(22)])])!.whole() == 42
208 hits,deopts := vm.native_execution_stats()
209 assert hits == 1 && deopts == 0
210 return unspecified()
211 })!
212}
213
214fn test_native_pair_inputs_share_one_bounded_pool_and_preserve_scalar_calls() {
215 mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})!
216 vm.eval('(define (work a b)(if (pair? a) (+ (car a) (car b)) 42))')!
217 xs := native_pair_test_list(3000)
218 vm.with_native_execution(fn [xs] (mut vm VM) !Value {
219 assert vm.call(vm.lookup('work')!,[xs,xs])!.whole() == 6000
220 scope := unsafe { &NativeScope(vm.native_execution_scope) }
221 assert scope.pair_input_rejections == 1 && scope.pair_nodes_visited == 4096
222 assert vm.call(vm.lookup('work')!,[integer(0),nil_value()])!.whole() == 42
223 assert scope.hits == 1 && scope.deopts == 0
224 return unspecified()
225 })!
226}
227
228fn test_native_pair_inputs_are_not_copied_for_graphs_that_do_not_read_them() {
229 mut vm := new_with_options(Options{max_steps: 0,max_frames: 0})!
230 vm.eval('(define (work xs)42)')!
231 xs := native_pair_test_list(3000)
232 vm.with_native_execution(fn [xs] (mut vm VM) !Value {
233 assert vm.call(vm.lookup('work')!,[xs])!.whole() == 42
234 scope := unsafe { &NativeScope(vm.native_execution_scope) }
235 assert scope.pair_nodes_visited == 0 && scope.hits == 0
236 assert vm.call(vm.lookup('work')!,[integer(0)])!.whole() == 42
237 assert scope.hits == 1 && scope.deopts == 0
238 return unspecified()
239 })!
240}
241