|
7 | 7 | "../syntax/types.rkt" |
8 | 8 | "../config.rkt" |
9 | 9 | "batch.rkt" |
10 | | - "egglog-program.rkt" |
11 | 10 | "../utils/common.rkt" |
12 | 11 | "egglog-subprocess.rkt") |
13 | 12 |
|
|
75 | 74 | (define (run-egglog-multi-extractor runner output-batch [label #f]) ; multi expression extraction |
76 | 75 | (define insert-batch (egglog-runner-batch runner)) |
77 | 76 | (define insert-brfs (egglog-runner-brfs runner)) |
78 | | - (define curr-program (make-egglog-program)) |
79 | 77 |
|
80 | | - ;; 1. Add the Prelude (includes all rules) |
81 | | - (prelude curr-program #:mixed-egraph? #t) |
| 78 | + ;;;; SUBPROCESS START ;;;; |
| 79 | + (define subproc (create-new-egglog-subprocess label)) |
| 80 | + |
| 81 | + (thread (lambda () |
| 82 | + (with-handlers ([exn:fail? (lambda (_) (void))]) |
| 83 | + (for ([line (in-lines (egglog-subprocess-error subproc))]) |
| 84 | + (void))))) |
| 85 | + |
| 86 | + ;; 1. Add the Prelude (includes all rules) - send directly to egglog |
| 87 | + (prelude subproc #:mixed-egraph? #t) |
82 | 88 |
|
83 | 89 | ;; 2. Inserting expressions into the egglog program and getting a Listof (exprs . extract bindings) |
84 | 90 |
|
|
125 | 131 | ;; keep track of the mapping between each binding and its corresponding constructor. |
126 | 132 |
|
127 | 133 | (define-values (all-bindings extract-bindings) |
128 | | - (egglog-add-exprs insert-batch insert-brfs (egglog-runner-ctx runner) curr-program)) |
129 | | - |
130 | | - (egglog-program-add! `(ruleset run-extract-commands) curr-program) |
131 | | - (egglog-program-add! `(rule () (,@all-bindings) :ruleset run-extract-commands) curr-program) |
132 | | - (egglog-program-add! `(run-schedule (repeat 1 run-extract-commands)) curr-program) |
| 134 | + (egglog-add-exprs insert-batch insert-brfs (egglog-runner-ctx runner) subproc)) |
133 | 135 |
|
134 | | - ;;;; SUBPROCESS START ;;;; |
135 | | - (define subproc (create-new-egglog-subprocess label)) |
136 | | - |
137 | | - (thread (lambda () |
138 | | - (with-handlers ([exn:fail? (lambda (_) (void))]) |
139 | | - (for ([line (in-lines (egglog-subprocess-error subproc))]) |
140 | | - (void))))) |
141 | | - |
142 | | - ;; Send whatever we have so far to egglog |
143 | | - ;; Expected no output anyways as there is no extraction |
144 | | - (send-to-egglog (get-current-program curr-program) subproc) |
| 136 | + (send-to-egglog (list `(ruleset run-extract-commands) |
| 137 | + `(rule () (,@all-bindings) :ruleset run-extract-commands) |
| 138 | + `(run-schedule (repeat 1 run-extract-commands))) |
| 139 | + subproc) |
145 | 140 |
|
146 | 141 | ;; 4. Running the schedule : having code inside to emulate egraph-run-rules |
147 | 142 |
|
|
161 | 156 | (for/list ([constructor-name extract-bindings]) |
162 | 157 | `(extract (,constructor-name) ,(*egglog-variants-limit*)))) |
163 | 158 |
|
164 | | - (egglog-program-add-list! extract-commands curr-program) |
165 | | - |
166 | | - ;; 6. After step-by-step building the program, process it |
167 | | - ;; by running it using egglog |
168 | | - |
169 | | - ;; Extract its returned value |
170 | 159 | (define stdout-content |
171 | 160 | (send-to-egglog extract-commands subproc #:num-extracts (length extract-commands))) |
172 | 161 |
|
|
214 | 203 | (define (normalize-cost c) |
215 | 204 | (exact-round (* c 1000))) |
216 | 205 |
|
217 | | -(define (prelude curr-program #:mixed-egraph? [mixed-egraph? #t]) |
| 206 | +(define (prelude subproc #:mixed-egraph? [mixed-egraph? #t]) |
218 | 207 | (define pform (*active-platform*)) |
219 | 208 |
|
220 | | - (define spec-egraph |
| 209 | + (send-to-egglog |
| 210 | + (list |
221 | 211 | `(datatype M (Num BigRat :cost 4294967295) (Var String :cost 4294967295) ,@(platform-spec-nodes))) |
222 | | - |
223 | | - (egglog-program-add! spec-egraph curr-program) |
224 | | - |
225 | | - (define typed-graph |
226 | | - `(datatype MTy |
227 | | - ,@(num-typed-nodes pform) |
228 | | - ,@(var-typed-nodes pform) |
229 | | - (Approx M MTy) |
230 | | - ,@(platform-impl-nodes pform))) |
231 | | - (egglog-program-add! typed-graph curr-program) |
232 | | - |
233 | | - (egglog-program-add! `(constructor do-lower (M String) MTy :unextractable) curr-program) |
234 | | - |
235 | | - (egglog-program-add! `(constructor do-lift (MTy) M :unextractable) curr-program) |
236 | | - |
237 | | - (egglog-program-add! `(ruleset const-fold) curr-program) |
238 | | - |
239 | | - (egglog-program-add! `(ruleset lower) curr-program) |
240 | | - |
241 | | - (egglog-program-add! `(ruleset lift) curr-program) |
242 | | - |
243 | | - (egglog-program-add! `(ruleset unsound) curr-program) |
244 | | - |
245 | | - ;;; Adding bad-merge detection |
246 | | - |
247 | | - ;; bad-merge detection function and rules |
248 | | - (egglog-program-add! `(function bad-merge? () bool :merge (or old new)) curr-program) |
249 | | - (egglog-program-add! `(ruleset bad-merge-rule) curr-program) |
250 | | - (egglog-program-add! `(set (bad-merge?) false) curr-program) |
251 | | - |
252 | | - (egglog-program-add! |
253 | | - `(rule ((= (Num c1) (Num c2)) (!= c1 c2)) ((set (bad-merge?) true)) :ruleset bad-merge-rule) |
254 | | - curr-program) |
255 | | - |
256 | | - (for ([curr-expr const-fold]) |
257 | | - (egglog-program-add! curr-expr curr-program)) |
258 | | - |
259 | | - (for ([curr-expr (impl-lowering-rules pform)]) |
260 | | - (egglog-program-add! curr-expr curr-program)) |
261 | | - |
262 | | - (for ([curr-expr (impl-lifting-rules pform)]) |
263 | | - (egglog-program-add! curr-expr curr-program)) |
264 | | - |
265 | | - (for ([curr-expr (num-lowering-rules)]) |
266 | | - (egglog-program-add! curr-expr curr-program)) |
267 | | - |
268 | | - (for ([curr-expr (num-lifting-rules)]) |
269 | | - (egglog-program-add! curr-expr curr-program)) |
270 | | - |
271 | | - (egglog-program-add! (approx-lifting-rule) curr-program) |
272 | | - |
273 | | - ;; Add unsound rules |
274 | | - (egglog-program-add-list! (egglog-rewrite-rules (*sound-removal-rules*) 'unsound) curr-program) |
275 | | - |
276 | | - ;; Add rewrite ruleset and rules |
277 | | - (egglog-program-add! `(ruleset rewrite) curr-program) |
278 | | - (egglog-program-add-list! (egglog-rewrite-rules (*rules*) 'rewrite) curr-program) |
| 212 | + subproc) |
| 213 | + |
| 214 | + (send-to-egglog (append (list `(datatype MTy |
| 215 | + ,@(num-typed-nodes pform) |
| 216 | + ,@(var-typed-nodes pform) |
| 217 | + (Approx M MTy) |
| 218 | + ,@(platform-impl-nodes pform)) |
| 219 | + `(constructor do-lower (M String) MTy :unextractable) |
| 220 | + `(constructor do-lift (MTy) M :unextractable) |
| 221 | + `(ruleset const-fold) |
| 222 | + `(ruleset lower) |
| 223 | + `(ruleset lift) |
| 224 | + `(ruleset unsound) |
| 225 | + `(function bad-merge? () bool :merge (or old new)) |
| 226 | + `(ruleset bad-merge-rule) |
| 227 | + `(set (bad-merge?) false) |
| 228 | + `(rule ((= (Num c1) (Num c2)) (!= c1 c2)) |
| 229 | + ((set (bad-merge?) true)) |
| 230 | + :ruleset |
| 231 | + bad-merge-rule)) |
| 232 | + const-fold |
| 233 | + (impl-lowering-rules pform) |
| 234 | + (impl-lifting-rules pform) |
| 235 | + (num-lowering-rules) |
| 236 | + (num-lifting-rules) |
| 237 | + (list (approx-lifting-rule)) |
| 238 | + (egglog-rewrite-rules (*sound-removal-rules*) 'unsound) |
| 239 | + (list `(ruleset rewrite)) |
| 240 | + (egglog-rewrite-rules (*rules*) 'rewrite)) |
| 241 | + subproc) |
279 | 242 |
|
280 | 243 | (void)) |
281 | 244 |
|
|
503 | 466 | :ruleset |
504 | 467 | ,tag))) |
505 | 468 |
|
506 | | -(define (egglog-add-exprs batch brfs ctx curr-program) |
| 469 | +(define (egglog-add-exprs batch brfs ctx subproc) |
507 | 470 | (define mappings (build-vector (batch-length batch) values)) |
508 | 471 | (define bindings (make-hash)) |
509 | 472 | (define vars (make-hash)) |
| 473 | + (define commands-to-send '()) ; Accumulate all commands to send |
510 | 474 | (define (remap x spec?) |
511 | 475 | (cond |
512 | 476 | [(hash-has-key? vars x) |
|
584 | 548 | (for ([var (in-list (context-vars ctx))] |
585 | 549 | [repr (in-list (context-var-reprs ctx))]) |
586 | 550 |
|
587 | | - (define curr-var-lowering-rule |
588 | | - `(rule ((= e (Var ,(symbol->string var)))) |
589 | | - ((let ty ,(symbol->string (representation-name repr)) |
590 | | - ) |
591 | | - (let ety (,(typed-var-id (representation-name repr)) |
592 | | - ,(symbol->string var)) |
593 | | - ) |
594 | | - (union (do-lower e ty) ety)) |
595 | | - :ruleset |
596 | | - lower)) |
597 | | - |
598 | | - (egglog-program-add! curr-var-lowering-rule curr-program)) |
| 551 | + (set! commands-to-send |
| 552 | + (cons `(rule ((= e (Var ,(symbol->string var)))) |
| 553 | + ((let ty ,(symbol->string (representation-name repr)) |
| 554 | + ) |
| 555 | + (let ety (,(typed-var-id (representation-name repr)) |
| 556 | + ,(symbol->string var)) |
| 557 | + ) |
| 558 | + (union (do-lower e ty) ety)) |
| 559 | + :ruleset |
| 560 | + lower) |
| 561 | + commands-to-send))) |
599 | 562 |
|
600 | 563 | ; Var-lifting-rules |
601 | 564 | (for ([var (in-list (context-vars ctx))] |
602 | 565 | [repr (in-list (context-var-reprs ctx))]) |
603 | 566 |
|
604 | | - (define curr-var-lifting-rule |
605 | | - `(rule ((= e (,(typed-var-id (representation-name repr)) ,(symbol->string var)))) |
606 | | - ((let se (Var |
607 | | - ,(symbol->string var)) |
608 | | - ) |
609 | | - (union (do-lift e) se)) |
610 | | - :ruleset |
611 | | - lift)) |
612 | | - |
613 | | - (egglog-program-add! curr-var-lifting-rule curr-program)) |
| 567 | + (set! commands-to-send |
| 568 | + (cons `(rule ((= e (,(typed-var-id (representation-name repr)) ,(symbol->string var)))) |
| 569 | + ((let se (Var |
| 570 | + ,(symbol->string var)) |
| 571 | + ) |
| 572 | + (union (do-lift e) se)) |
| 573 | + :ruleset |
| 574 | + lift) |
| 575 | + commands-to-send))) |
614 | 576 |
|
615 | 577 | (define all-bindings '()) |
616 | 578 | (define binding->constructor (make-hash)) ; map from binding name to constructor name |
|
627 | 589 | ; Define the actual binding |
628 | 590 | (define curr-var-spec-binding `(let ,binding-name (Var ,(symbol->string var)))) |
629 | 591 |
|
630 | | - ; Add the unique constructur to the program |
631 | | - (egglog-program-add! `(constructor ,constructor-name () M :unextractable) curr-program) |
| 592 | + ; Add the unique constructor to send list |
| 593 | + (set! commands-to-send |
| 594 | + (cons `(constructor ,constructor-name () M :unextractable) commands-to-send)) |
632 | 595 |
|
633 | 596 | ; Add the binding and constructor set to all-bindings for the future rule |
634 | 597 | (set! all-bindings (cons curr-var-spec-binding all-bindings)) |
|
648 | 611 | (define curr-var-typed-binding |
649 | 612 | `(let ,binding-name (,(typed-var-id (representation-name repr)) ,(symbol->string var)))) |
650 | 613 |
|
651 | | - ; Add the unique constructur to the program |
652 | | - (egglog-program-add! `(constructor ,constructor-name () MTy :unextractable) curr-program) |
| 614 | + ; Add the unique constructor to send list |
| 615 | + (set! commands-to-send |
| 616 | + (cons `(constructor ,constructor-name () MTy :unextractable) commands-to-send)) |
653 | 617 |
|
654 | 618 | ; Add the binding and constructor set to all-bindings for the future rule |
655 | 619 | (set! all-bindings (cons curr-var-typed-binding all-bindings)) |
|
682 | 646 |
|
683 | 647 | (define curr-binding-exprs `(let ,binding-name ,actual-binding)) |
684 | 648 |
|
685 | | - (egglog-program-add! `(constructor ,constructor-name () ,curr-datatype :unextractable) |
686 | | - curr-program) |
| 649 | + (set! commands-to-send |
| 650 | + (cons `(constructor ,constructor-name () ,curr-datatype :unextractable) commands-to-send)) |
687 | 651 |
|
688 | 652 | (set! all-bindings (cons curr-binding-exprs all-bindings)) |
689 | 653 | (set! all-bindings (cons `(set (,constructor-name) ,binding-name) all-bindings)) |
|
702 | 666 |
|
703 | 667 | (hash-ref binding->constructor curr-binding-name))) |
704 | 668 |
|
| 669 | + ; Send all accumulated commands to egglog |
| 670 | + (send-to-egglog (reverse commands-to-send) subproc) |
| 671 | + |
705 | 672 | (values (reverse all-bindings) curr-bindings)) |
706 | 673 |
|
707 | 674 | (define (egglog-unsound-detected-subprocess tag subproc) |
|
0 commit comments