Skip to content

Commit 43d67e7

Browse files
committed
Add an egglog-subprocess struct to clean up egglog code
1 parent bed20c8 commit 43d67e7

File tree

2 files changed

+52
-124
lines changed

2 files changed

+52
-124
lines changed

src/core/egglog-herbie.rkt

Lines changed: 22 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -166,21 +166,16 @@
166166
(egglog-program-add! `(run-schedule (repeat 1 run-extract-commands)) curr-program)
167167

168168
;;;; SUBPROCESS START ;;;;
169-
(define-values (egglog-process egglog-output egglog-in err) (create-new-egglog-subprocess))
169+
(define subproc (create-new-egglog-subprocess dump-file))
170170

171171
(thread (lambda ()
172172
(with-handlers ([exn:fail? (lambda (_) (void))])
173-
(for ([line (in-lines err)])
173+
(for ([line (in-lines (egglog-subprocess-error subproc))])
174174
(void)))))
175175

176176
;; Send whatever we have so far to egglog
177177
;; Expected no output anyways as there is no extraction
178-
(send-to-egglog (get-current-program curr-program)
179-
egglog-process
180-
egglog-output
181-
egglog-in
182-
err
183-
dump-file)
178+
(send-to-egglog (get-current-program curr-program) subproc)
184179

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

@@ -189,38 +184,14 @@
189184

190185
(for ([tag (in-list tag-schedule)])
191186
(match tag
192-
['lift
193-
(send-to-egglog (list '(run-schedule (saturate lift)))
194-
egglog-process
195-
egglog-output
196-
egglog-in
197-
err
198-
dump-file)]
199-
200-
['lower
201-
(send-to-egglog (list '(run-schedule (saturate lower)))
202-
egglog-process
203-
egglog-output
204-
egglog-in
205-
err
206-
dump-file)]
207-
208-
['unsound
209-
(send-to-egglog (list '(run-schedule (saturate unsound)))
210-
egglog-process
211-
egglog-output
212-
egglog-in
213-
err
214-
dump-file)]
215-
216-
['rewrite
217-
;; Run the rewrite ruleset interleaved with const-fold until the best iteration
218-
(egglog-unsound-detected-subprocess tag
219-
egglog-process
220-
egglog-output
221-
egglog-in
222-
err
223-
dump-file)]))
187+
['lift (send-to-egglog (list '(run-schedule (saturate lift))) subproc)]
188+
189+
['lower (send-to-egglog (list '(run-schedule (saturate lower))) subproc)]
190+
191+
['unsound (send-to-egglog (list '(run-schedule (saturate unsound))) subproc)]
192+
193+
;; Run the rewrite ruleset interleaved with const-fold until the best iteration
194+
['rewrite (egglog-unsound-detected-subprocess tag subproc)]))
224195

225196
;; 5. Extraction -> should just need constructor names from egglog-add-exprs
226197
(define extract-commands
@@ -234,13 +205,7 @@
234205

235206
;; Extract its returned value
236207
(define stdout-content
237-
(send-to-egglog extract-commands
238-
egglog-process
239-
egglog-output
240-
egglog-in
241-
err
242-
dump-file
243-
#:num-extracts (length extract-commands)))
208+
(send-to-egglog extract-commands subproc #:num-extracts (length extract-commands)))
244209

245210
;; (Listof (Listof exprs))
246211
(define herbie-exprss
@@ -255,12 +220,7 @@
255220
#:key batchref-idx)))
256221

257222
;; Close everything subprocess related
258-
(close-output-port egglog-in)
259-
(close-input-port egglog-output)
260-
(close-input-port err)
261-
(subprocess-wait egglog-process)
262-
(unless (eq? (subprocess-status egglog-process) 'done)
263-
(subprocess-kill egglog-process #f))
223+
(egglog-subprocess-close subproc)
264224

265225
;; (Listof (Listof batchref))
266226
result)
@@ -792,7 +752,7 @@
792752

793753
(values (reverse all-bindings) curr-bindings))
794754

795-
(define (egglog-unsound-detected-subprocess tag egglog-process egglog-output egglog-in err dump-file)
755+
(define (egglog-unsound-detected-subprocess tag subproc)
796756

797757
(define node-limit (*node-limit*))
798758
(define iter-limit (*default-egglog-iter-limit*))
@@ -833,13 +793,7 @@
833793

834794
;; Get egglog output
835795
(define-values (math-unsound? math-node-limit? math-total-nodes)
836-
(get-egglog-output math-schedule
837-
egglog-process
838-
egglog-output
839-
egglog-in
840-
err
841-
node-limit
842-
dump-file))
796+
(get-egglog-output math-schedule subproc node-limit))
843797

844798
(cond
845799
;; There are two condiitons where we exit unsoundness dteection WITHOUT running (pop)
@@ -866,14 +820,14 @@
866820
;; e-graph while extracting. For now, popping provides a smaller e-graph and gives
867821
;; performance comparable to Egg-Herbie, thought it doesn't affect correctness too much
868822
[math-node-limit?
869-
(send-to-egglog (list '(pop)) egglog-process egglog-output egglog-in err dump-file)
823+
(send-to-egglog (list '(pop)) subproc)
870824
(values (sub1 curr-iter) #t)]
871825

872826
;; If Unsoundness detected or node-limit reached, then return the
873827
;; optimal iter limit (one less than current) and run (pop)
874828
[math-unsound?
875829
;; Pop once at the end since the egraph isn't valid
876-
(send-to-egglog (list '(pop)) egglog-process egglog-output egglog-in err dump-file)
830+
(send-to-egglog (list '(pop)) subproc)
877831

878832
;; Return one less than current iteration and indicate that we need to run again because pop
879833
(values (sub1 curr-iter) #t)]
@@ -892,23 +846,17 @@
892846
'(extract (bad-merge?))))
893847

894848
(define-values (const-unsound? const-node-limit? const-total-nodes)
895-
(get-egglog-output const-schedule
896-
egglog-process
897-
egglog-output
898-
egglog-in
899-
err
900-
node-limit
901-
dump-file))
849+
(get-egglog-output const-schedule subproc node-limit))
902850

903851
(cond
904852
;; TODO: See the TODO from above
905853
[(equal? const-total-nodes prev-number-nodes) (values curr-iter #f)]
906854
[const-node-limit?
907-
(send-to-egglog (list '(pop)) egglog-process egglog-output egglog-in err dump-file)
855+
(send-to-egglog (list '(pop)) subproc)
908856
(values (sub1 curr-iter) #t)]
909857

910858
[const-unsound?
911-
(send-to-egglog (list '(pop)) egglog-process egglog-output egglog-in err dump-file)
859+
(send-to-egglog (list '(pop)) subproc)
912860
(values (sub1 curr-iter) #t)]
913861

914862
[else
@@ -917,20 +865,8 @@
917865

918866
(loop (add1 curr-iter))])])])))
919867

920-
(define (get-egglog-output curr-schedule
921-
egglog-process
922-
egglog-output
923-
egglog-in
924-
err
925-
node-limit
926-
dump-file)
927-
(define-values (node-values unsound?)
928-
(send-to-egglog-unsound-detection curr-schedule
929-
egglog-process
930-
egglog-output
931-
egglog-in
932-
err
933-
dump-file))
868+
(define (get-egglog-output curr-schedule subproc node-limit)
869+
(define-values (node-values unsound?) (send-to-egglog-unsound-detection curr-schedule subproc))
934870

935871
; (when unsound?
936872
; (printf "ALERT : UNSOUNDNESS DETECTED when...\n"))

src/core/egglog-subprocess.rkt

Lines changed: 30 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,39 @@
22

33
(require "../config.rkt")
44

5-
(provide create-new-egglog-subprocess
5+
(provide (struct-out egglog-subprocess)
6+
create-new-egglog-subprocess
67
send-to-egglog
7-
send-to-egglog-unsound-detection)
8+
send-to-egglog-unsound-detection
9+
egglog-subprocess-close)
10+
11+
;; Struct to hold egglog subprocess handles
12+
(struct egglog-subprocess (process output input error dump-file) #:transparent)
13+
14+
;; Close all ports and wait for/kill the subprocess
15+
(define (egglog-subprocess-close subproc)
16+
(close-output-port (egglog-subprocess-input subproc))
17+
(close-input-port (egglog-subprocess-output subproc))
18+
(close-input-port (egglog-subprocess-error subproc))
19+
(subprocess-wait (egglog-subprocess-process subproc))
20+
(unless (eq? (subprocess-status (egglog-subprocess-process subproc)) 'done)
21+
(subprocess-kill (egglog-subprocess-process subproc) #f)))
822

923
;; High-level function that writes the program to a file, runs it then returns output
1024
;;
1125
;; If the flag is set to dump the egglog file, since a new subprocess is starting, we can
1226
;; create a new file to dump the egglog program is and set it
13-
(define (create-new-egglog-subprocess)
27+
(define (create-new-egglog-subprocess dump-file)
1428
(define egglog-path
1529
(or (find-executable-path "egglog") (error "egglog executable not found in PATH")))
1630

1731
; TODO : "RUST_BACKTRACE=1"
1832
(define-values (egglog-process egglog-output egglog-in err) (subprocess #f #f #f egglog-path))
1933

20-
(values egglog-process egglog-output egglog-in err))
34+
(egglog-subprocess egglog-process egglog-output egglog-in err dump-file))
2135

22-
(define (send-to-egglog commands
23-
egglog-process
24-
egglog-output
25-
egglog-in
26-
err
27-
dump-file
28-
#:num-extracts [num-extracts 0])
36+
(define (send-to-egglog commands subproc #:num-extracts [num-extracts 0])
37+
(match-define (egglog-subprocess egglog-process egglog-output egglog-in err dump-file) subproc)
2938

3039
(define egglog-program (apply ~s #:separator "\n" commands))
3140

@@ -56,12 +65,9 @@
5665
(for/list ([i (in-range num-extracts)])
5766
(read egglog-output))))
5867

59-
(define (send-to-egglog-unsound-detection commands
60-
egglog-process
61-
egglog-output
62-
egglog-in
63-
err
64-
dump-file)
68+
(define (send-to-egglog-unsound-detection commands subproc)
69+
(match-define (egglog-subprocess egglog-process egglog-output egglog-in err dump-file) subproc)
70+
6571
(define egglog-program (apply ~s #:separator "\n" commands))
6672

6773
(when dump-file
@@ -103,11 +109,11 @@
103109

104110
(module+ test
105111
(when (find-executable-path "egglog")
106-
(define-values (egglog-process egglog-output egglog-in err) (create-new-egglog-subprocess))
112+
(define subproc (create-new-egglog-subprocess #f))
107113

108114
(thread (lambda ()
109115
(with-handlers ([exn:fail? (lambda (_) (void))])
110-
(for ([line (in-lines err)])
116+
(for ([line (in-lines (egglog-subprocess-error subproc))])
111117
(printf "[egglog-log] ~a\n" line)))))
112118

113119
(define first-commands
@@ -139,26 +145,20 @@
139145
'(run init 1)))
140146

141147
; Nothing to output
142-
(send-to-egglog first-commands egglog-process egglog-output egglog-in err #f)
148+
(send-to-egglog first-commands subproc)
143149

144150
; Has extract 1 thing
145151
(define second-commands (list '(extract (const1))))
146152

147-
(define lines1
148-
(send-to-egglog second-commands egglog-process egglog-output egglog-in err #f #:num-extracts 1))
153+
(define lines1 (send-to-egglog second-commands subproc #:num-extracts 1))
149154
(printf "\noutput-vals1 : ~a\n\n" lines1)
150155

151156
;; Print size
152157

153158
(define print-size-commands (list '(print-size) '(run unsound-rule 1) '(extract (unsound))))
154159

155160
(define-values (node-values unsound?)
156-
(send-to-egglog-unsound-detection print-size-commands
157-
egglog-process
158-
egglog-output
159-
egglog-in
160-
err
161-
#f))
161+
(send-to-egglog-unsound-detection print-size-commands subproc))
162162

163163
(for ([line node-values]
164164
#:when (> (string-length line) 0))
@@ -171,18 +171,10 @@
171171
;; last two
172172
(define third-commands (list '(extract (const2)) '(extract (const3))))
173173

174-
(define lines2
175-
(send-to-egglog third-commands egglog-process egglog-output egglog-in err #f #:num-extracts 2))
174+
(define lines2 (send-to-egglog third-commands subproc #:num-extracts 2))
176175
(printf "\noutput-vals2 : ~a\n\n" lines2)
177176

178-
(close-output-port egglog-in)
179-
(close-input-port egglog-output)
180-
(close-input-port err)
181-
182-
(subprocess-wait egglog-process)
183-
184-
(unless (eq? (subprocess-status egglog-process) 'done)
185-
(subprocess-kill egglog-process #f))))
177+
(egglog-subprocess-close subproc)))
186178

187179
(define (calculate-nodes lines)
188180
;; Don't start from last index, but previous to last index - as last has current unsoundness result

0 commit comments

Comments
 (0)