|
10 | 10 | (struct-out batchref) ; temporarily for patch.rkt |
11 | 11 | (struct-out mutable-batch) ; temporarily for patch.rkt |
12 | 12 | batch-length ; Batch -> Integer |
13 | | - batch-ref ; Batch -> Index -> Expr |
| 13 | + batch-ref ; Batch -> Idx -> Expr |
14 | 14 | deref ; Batchref -> Expr |
15 | 15 | batch-replace ; Batch -> (Expr<Batchref> -> Expr<Batchref>) -> Batch |
16 | 16 | egg-nodes->batch ; Nodes -> Spec-maps -> Batch -> (Listof Batchref) |
17 | 17 | batchref->expr ; Batchref -> Expr |
18 | 18 | batch-remove-zombie ; Batch -> *(Vectorof Root) -> Batch |
19 | | - mutable-batch-add-expr! ; Mutable-batch -> Root |
| 19 | + mutable-batch-munge! ; Mutable-batch -> Root |
20 | 20 | mutable-batch->batch ; Mutable-batch -> Batch |
| 21 | + make-mutable-batch ; Mutable-batch |
| 22 | + mutable-batch-devour-batchref! ; Mutable-batch -> Batchref -> Idx |
21 | 23 | batch->mutable-batch ; Batch -> Mutable-batch |
22 | | - batch-push!) ; Mutable-batch -> Expr -> Index |
| 24 | + batch-push!) ; Mutable-batch -> Expr -> Idx |
23 | 25 |
|
24 | 26 | ;; This function defines the recursive structure of expressions |
25 | 27 | (define (expr-recurse expr f) |
26 | 28 | (match expr |
27 | | - [(approx spec impl) (approx spec (f impl))] |
| 29 | + [(approx spec impl) (approx (f spec) (f impl))] |
28 | 30 | [(list op args ...) (cons op (map f args))] |
29 | 31 | [_ expr])) |
30 | 32 |
|
|
89 | 91 | (timeline-push! 'compiler size (batch-length final))) |
90 | 92 | final) |
91 | 93 |
|
92 | | -(define (mutable-batch-add-expr! b expr) |
| 94 | +(define (mutable-batch-munge! b expr) |
93 | 95 | (define (munge prog) |
94 | 96 | (batch-push! b (expr-recurse prog munge))) |
95 | 97 | (munge expr)) |
96 | 98 |
|
| 99 | +(define (mutable-batch-devour-batchref! b ref) |
| 100 | + (match-define (batchref b* idx) ref) |
| 101 | + (define nodes* (batch-nodes b*)) |
| 102 | + (define (munge idx) |
| 103 | + (batch-push! b (expr-recurse (vector-ref nodes* idx) munge))) |
| 104 | + (munge idx)) |
| 105 | + |
97 | 106 | (define (batch->progs b [roots (batch-roots b)]) |
98 | 107 | (define exprs (make-vector (batch-length b))) |
99 | 108 | (for ([node (in-vector (batch-nodes b))] |
|
125 | 134 | ; The function removes any zombie nodes from batch with respect to the roots |
126 | 135 | ; Time complexity: O(|R| + |N|), where |R| - number of roots, |N| - length of nodes |
127 | 136 | ; Space complexity: O(|N| + |N*| + |R|), where |N*| is a length of nodes without zombie nodes |
128 | | -(define (batch-remove-zombie input-batch [roots (batch-roots input-batch)]) |
| 137 | +; The flag keep-vars is used in compiler.rkt when vars should be preserved no matter what |
| 138 | +(define (batch-remove-zombie input-batch [roots (batch-roots input-batch)] #:keep-vars [keep-vars #f]) |
129 | 139 | (define nodes (batch-nodes input-batch)) |
130 | 140 | (define nodes-length (batch-length input-batch)) |
131 | 141 |
|
|
142 | 152 | (vector-ref mappings idx)) |
143 | 153 |
|
144 | 154 | (define out (make-mutable-batch)) |
| 155 | + (when keep-vars |
| 156 | + (for ([var (in-list (batch-vars input-batch))]) |
| 157 | + (batch-push! out var))) |
| 158 | + |
145 | 159 | (for ([node (in-vector nodes)] |
146 | 160 | [zmb (in-vector zombie-mask)] |
147 | 161 | [n (in-naturals)] |
|
155 | 169 | (define (unmunge reg) |
156 | 170 | (define node (vector-ref (batch-nodes batch) reg)) |
157 | 171 | (match node |
158 | | - [(approx spec impl) (approx spec (unmunge impl))] |
| 172 | + [(approx spec impl) (approx (unmunge spec) (unmunge impl))] |
159 | 173 | [(list op regs ...) (cons op (map unmunge regs))] |
160 | 174 | [_ node])) |
161 | 175 | (unmunge reg)) |
|
167 | 181 |
|
168 | 182 | (define (egg-nodes->batch egg-nodes id->spec input-batch rename-dict) |
169 | 183 | (define out (batch->mutable-batch input-batch)) |
170 | | - |
171 | 184 | ; This fuction here is only because of cycles in loads:( Can not be imported from egg-herbie.rkt |
172 | 185 | (define (egg-parsed->expr expr rename-dict type) |
173 | 186 | (let loop ([expr expr] |
|
202 | 215 | (error 'regraph-extract-variants "no initial approx node in eclass")) |
203 | 216 | (define spec-type (if (representation? type) (representation-type type) type)) |
204 | 217 | (define final-spec (egg-parsed->expr spec* rename-dict spec-type)) |
205 | | - (approx final-spec (add-enode (eggref impl) type))] |
| 218 | + (define final-spec-idx (mutable-batch-munge! out final-spec)) |
| 219 | + (approx final-spec-idx (add-enode (eggref impl) type))] |
206 | 220 | [(list 'if cond ift iff) |
207 | 221 | (if (representation? type) |
208 | 222 | (list 'if |
|
272 | 286 | #:roots (vector 5))) |
273 | 287 | (check-equal? (vector 0 1/2 '(+ 0 1)) |
274 | 288 | (zombie-test #:nodes (vector 0 1/2 '(+ 0 1) '(* 2 0)) #:roots (vector 2))) |
275 | | - (check-equal? (vector 0 (approx '(exp 2) 0)) |
276 | | - (zombie-test #:nodes (vector 0 1/2 '(+ 0 1) '(* 2 0) (approx '(exp 2) 0)) |
277 | | - #:roots (vector 4))) |
278 | | - (check-equal? (vector 2 1/2 (approx '(* x x) 0) '(pow 1 2)) |
279 | | - (zombie-test #:nodes (vector 2 1/2 '(sqrt 0) '(cbrt 0) (approx '(* x x) 0) '(pow 1 4)) |
| 289 | + (check-equal? (vector 0 1/2 '(exp 1) (approx 2 0)) |
| 290 | + (zombie-test #:nodes (vector 0 1/2 '(+ 0 1) '(* 2 0) '(exp 1) (approx 4 0)) |
280 | 291 | #:roots (vector 5))) |
281 | | - (check-equal? (vector 2 1/2 '(sqrt 0) (approx '(* x x) 0) '(pow 1 3)) |
282 | | - (zombie-test #:nodes (vector 2 1/2 '(sqrt 0) '(cbrt 0) (approx '(* x x) 0) '(pow 1 4)) |
283 | | - #:roots (vector 5 2)))) |
| 292 | + (check-equal? (vector 'x 2 1/2 '(* 0 0) (approx 3 1) '(pow 2 4)) |
| 293 | + (zombie-test #:nodes |
| 294 | + (vector 'x 2 1/2 '(sqrt 1) '(cbrt 1) '(* 0 0) (approx 5 1) '(pow 2 6)) |
| 295 | + #:roots (vector 7))) |
| 296 | + (check-equal? (vector 'x 2 1/2 '(sqrt 1) '(* 0 0) (approx 4 1) '(pow 2 5)) |
| 297 | + (zombie-test #:nodes |
| 298 | + (vector 'x 2 1/2 '(sqrt 1) '(cbrt 1) '(* 0 0) (approx 5 1) '(pow 2 6)) |
| 299 | + #:roots (vector 7 3)))) |
0 commit comments