Skip to content

Commit 130f032

Browse files
committed
Remove egglog-program
1 parent 0d1aff1 commit 130f032

File tree

2 files changed

+81
-147
lines changed

2 files changed

+81
-147
lines changed

src/core/egglog-herbie.rkt

Lines changed: 81 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
"../syntax/types.rkt"
88
"../config.rkt"
99
"batch.rkt"
10-
"egglog-program.rkt"
1110
"../utils/common.rkt"
1211
"egglog-subprocess.rkt")
1312

@@ -75,10 +74,17 @@
7574
(define (run-egglog-multi-extractor runner output-batch [label #f]) ; multi expression extraction
7675
(define insert-batch (egglog-runner-batch runner))
7776
(define insert-brfs (egglog-runner-brfs runner))
78-
(define curr-program (make-egglog-program))
7977

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)
8288

8389
;; 2. Inserting expressions into the egglog program and getting a Listof (exprs . extract bindings)
8490

@@ -125,23 +131,12 @@
125131
;; keep track of the mapping between each binding and its corresponding constructor.
126132

127133
(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))
133135

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)
145140

146141
;; 4. Running the schedule : having code inside to emulate egraph-run-rules
147142

@@ -161,12 +156,6 @@
161156
(for/list ([constructor-name extract-bindings])
162157
`(extract (,constructor-name) ,(*egglog-variants-limit*))))
163158

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
170159
(define stdout-content
171160
(send-to-egglog extract-commands subproc #:num-extracts (length extract-commands)))
172161

@@ -214,68 +203,42 @@
214203
(define (normalize-cost c)
215204
(exact-round (* c 1000)))
216205

217-
(define (prelude curr-program #:mixed-egraph? [mixed-egraph? #t])
206+
(define (prelude subproc #:mixed-egraph? [mixed-egraph? #t])
218207
(define pform (*active-platform*))
219208

220-
(define spec-egraph
209+
(send-to-egglog
210+
(list
221211
`(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)
279242

280243
(void))
281244

@@ -503,10 +466,11 @@
503466
:ruleset
504467
,tag)))
505468

506-
(define (egglog-add-exprs batch brfs ctx curr-program)
469+
(define (egglog-add-exprs batch brfs ctx subproc)
507470
(define mappings (build-vector (batch-length batch) values))
508471
(define bindings (make-hash))
509472
(define vars (make-hash))
473+
(define commands-to-send '()) ; Accumulate all commands to send
510474
(define (remap x spec?)
511475
(cond
512476
[(hash-has-key? vars x)
@@ -584,33 +548,31 @@
584548
(for ([var (in-list (context-vars ctx))]
585549
[repr (in-list (context-var-reprs ctx))])
586550

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)))
599562

600563
; Var-lifting-rules
601564
(for ([var (in-list (context-vars ctx))]
602565
[repr (in-list (context-var-reprs ctx))])
603566

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)))
614576

615577
(define all-bindings '())
616578
(define binding->constructor (make-hash)) ; map from binding name to constructor name
@@ -627,8 +589,9 @@
627589
; Define the actual binding
628590
(define curr-var-spec-binding `(let ,binding-name (Var ,(symbol->string var))))
629591

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))
632595

633596
; Add the binding and constructor set to all-bindings for the future rule
634597
(set! all-bindings (cons curr-var-spec-binding all-bindings))
@@ -648,8 +611,9 @@
648611
(define curr-var-typed-binding
649612
`(let ,binding-name (,(typed-var-id (representation-name repr)) ,(symbol->string var))))
650613

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))
653617

654618
; Add the binding and constructor set to all-bindings for the future rule
655619
(set! all-bindings (cons curr-var-typed-binding all-bindings))
@@ -682,8 +646,8 @@
682646

683647
(define curr-binding-exprs `(let ,binding-name ,actual-binding))
684648

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))
687651

688652
(set! all-bindings (cons curr-binding-exprs all-bindings))
689653
(set! all-bindings (cons `(set (,constructor-name) ,binding-name) all-bindings))
@@ -702,6 +666,9 @@
702666

703667
(hash-ref binding->constructor curr-binding-name)))
704668

669+
; Send all accumulated commands to egglog
670+
(send-to-egglog (reverse commands-to-send) subproc)
671+
705672
(values (reverse all-bindings) curr-bindings))
706673

707674
(define (egglog-unsound-detected-subprocess tag subproc)

src/core/egglog-program.rkt

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)