Skip to content

Commit 2db7c82

Browse files
authored
Merge pull request #1341 from herbie-fp/codex/remove-preprocessing-field-from-alts
Remove preprocessing field from alternatives
2 parents 6d3c726 + c802d87 commit 2db7c82

File tree

8 files changed

+42
-43
lines changed

8 files changed

+42
-43
lines changed

src/core/bsearch.rkt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@
5252

5353
;; We don't want unused alts in our history!
5454
(define-values (alts* splitpoints*) (remove-unused-alts alts splitpoints))
55-
(define preprocessing (alt-preprocessing (first alts*)))
56-
(alt expr* (list 'regimes splitpoints*) alts* preprocessing)]))
55+
(alt expr* (list 'regimes splitpoints*) alts*)]))
5756

5857
(define (remove-unused-alts alts splitpoints)
5958
(for/fold ([alts* '()]

src/core/derivations.rkt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616
(define (add-derivations-to altn)
1717
(match altn
1818
; recursive rewrite or simplify, both using egg
19-
[(alt expr (list 'rr loc (? egg-runner? runner) #f) `(,prev) preprocessing)
19+
[(alt expr (list 'rr loc (? egg-runner? runner) #f) `(,prev))
2020
(define start-expr (location-get loc (alt-expr prev)))
2121
(define end-expr (location-get loc expr))
2222
(define proof
2323
(and (not (flag-set? 'generate 'egglog)) (egraph-prove runner start-expr end-expr)))
2424
(define proof* (canonicalize-proof (alt-expr altn) proof loc))
25-
(alt expr `(rr ,loc ,runner ,proof*) (list prev) preprocessing)]
25+
(alt expr `(rr ,loc ,runner ,proof*) (list prev))]
2626

2727
; everything else
2828
[_ altn]))

src/core/mainloop.rkt

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
;; Starting program for the current run
3434
(define *start-prog* (make-parameter #f))
3535
(define *pcontext* (make-parameter #f))
36+
(define *preprocessing* (make-parameter '()))
3637

3738
;; These high-level functions give the high-level workflow of Herbie:
3839
;; - Initial steps: explain, preprocessing, initialize the alt table
@@ -46,7 +47,8 @@
4647
(define pcontext* (preprocess-pcontext context pcontext preprocessing))
4748
(*pcontext* pcontext*)
4849
(*start-prog* initial)
49-
(define start-alt (alt initial 'start '() preprocessing))
50+
(*preprocessing* preprocessing)
51+
(define start-alt (alt initial 'start '()))
5052
(^table^ (make-alt-table pcontext start-alt context))
5153

5254
(for ([iteration (in-range (*num-iterations*))]
@@ -55,17 +57,9 @@
5557
(define alternatives (extract!))
5658
(timeline-event! 'preprocess)
5759
(for/list ([altn alternatives])
58-
(apply-preprocessing altn context pcontext)))
59-
60-
(define (apply-preprocessing altn context pcontext)
61-
(define expr (alt-expr altn))
62-
(define initial-preprocessing (alt-preprocessing altn))
63-
(define useful-preprocessing
64-
(remove-unnecessary-preprocessing expr context pcontext initial-preprocessing))
65-
(define expr*
66-
(for/fold ([expr expr]) ([preprocessing (in-list (reverse useful-preprocessing))])
67-
(compile-preprocessing expr context preprocessing)))
68-
(alt expr* 'add-preprocessing (list altn) '()))
60+
(define expr (alt-expr altn))
61+
(define expr* (compile-useful-preprocessing expr context pcontext (*preprocessing*)))
62+
(alt expr* 'add-preprocessing (list altn))))
6963

7064
(define (extract!)
7165
(timeline-push-alts! '())
@@ -168,7 +162,7 @@
168162
;; takes a patch and converts it to a full alt
169163
(define (reconstruct-alt altn loc0 orig)
170164
(let loop ([altn altn])
171-
(match-define (alt _ event prevs _) altn)
165+
(match-define (alt _ event prevs) altn)
172166
(match event
173167
['patch orig]
174168
[_
@@ -183,7 +177,7 @@
183177
location-set-cache
184178
(alt-expr orig)
185179
(alt-expr altn)))
186-
(alt expr* event* (list (loop (first prevs))) (alt-preprocessing orig))])))
180+
(alt expr* event* (list (loop (first prevs))))])))
187181

188182
(^patched^ (remove-duplicates
189183
(reap [sow]

src/core/patch.rkt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
(for ([i (in-range (*taylor-order-limit*))])
5555
(define gen (approx spec (hole (representation-name repr) (genexpr))))
5656
(define idx (mutable-batch-munge! global-batch-mutable gen)) ; Munge gen
57-
(sow (alt (batchref global-batch idx) `(taylor ,name ,var) (list altn) '()))))
57+
(sow (alt (batchref global-batch idx) `(taylor ,name ,var) (list altn)))))
5858
(timeline-stop!))
5959
(batch-copy-mutable-nodes! global-batch global-batch-mutable))) ; Update global-batch
6060

@@ -99,7 +99,7 @@
9999
(for ([batchrefs (in-list batchrefss)]
100100
[altn (in-list altns)])
101101
(for ([batchref* (in-list batchrefs)])
102-
(sow (alt batchref* (list 'rr runner #f) (list altn) '()))))))
102+
(sow (alt batchref* (list 'rr runner #f) (list altn)))))))
103103

104104
(define (run-evaluate altns global-batch)
105105
(timeline-event! 'sample)
@@ -138,7 +138,7 @@
138138
[altn (in-list real-altns)]
139139
#:when (equal? status 'valid))
140140
(define idx (mutable-batch-munge! global-batch-mutable literal))
141-
(alt (batchref global-batch idx) '(evaluate) (list altn) '())))
141+
(alt (batchref global-batch idx) '(evaluate) (list altn))))
142142
(batch-copy-mutable-nodes! global-batch global-batch-mutable)
143143
final-altns)
144144

@@ -179,7 +179,7 @@
179179
(for ([batchrefs (in-list batchrefss)]
180180
[altn (in-list altns)])
181181
(for ([batchref* (in-list batchrefs)])
182-
(sow (alt batchref* (list 'rr runner #f) (list altn) '()))))))
182+
(sow (alt batchref* (list 'rr runner #f) (list altn)))))))
183183

184184
(timeline-push! 'outputs (map (compose ~a debatchref alt-expr) rewritten))
185185
(timeline-push! 'count (length altns) (length rewritten))
@@ -192,7 +192,7 @@
192192
; Starting alternatives
193193
(define start-altns
194194
(for/list ([root (in-vector (batch-roots global-batch))])
195-
(alt (batchref global-batch root) 'patch '() '())))
195+
(alt (batchref global-batch root) 'patch '())))
196196

197197
(define evaluations
198198
(if (flag-set? 'generate 'evaluate)

src/core/preprocess.rkt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
(provide find-preprocessing
1717
preprocess-pcontext
1818
remove-unnecessary-preprocessing
19-
compile-preprocessing)
19+
compile-preprocessing
20+
compile-useful-preprocessing)
2021

2122
(define (has-fabs-impl? repr)
2223
(get-fpcore-impl 'fabs (repr->prop repr) (list repr)))
@@ -179,3 +180,9 @@
179180
(define copysign (get-fpcore-impl 'copysign (repr->prop repr) (list repr repr)))
180181
`(,mul (,copysign ,(literal 1 (representation-name repr)) ,var)
181182
,(replace-expression expression var replacement))]))
183+
184+
(define (compile-useful-preprocessing expression context pcontext preprocessing)
185+
(define useful-preprocessing
186+
(remove-unnecessary-preprocessing expression context pcontext preprocessing))
187+
(for/fold ([expr expression]) ([prep (in-list (reverse useful-preprocessing))])
188+
(compile-preprocessing expr context prep)))

src/reports/history.rkt

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,12 @@
9191
(sow (alt-expr altn)))
9292

9393
(match altn
94-
[(alt prog 'start (list) _) (void)]
95-
[(alt prog 'add-preprocessing `(,prev) _) (loop prev)]
96-
[(alt prog `(evaluate ,loc) `(,prev) _) (loop prev)]
97-
[(alt _ `(regimes ,splitpoints) prevs _) (for-each loop prevs)]
98-
[(alt prog `(taylor ,loc ,pt ,var) `(,prev) _) (loop prev)]
99-
[(alt prog `(rr ,loc ,input ,proof) `(,prev) _)
94+
[(alt prog 'start (list)) (void)]
95+
[(alt prog 'add-preprocessing `(,prev)) (loop prev)]
96+
[(alt prog `(evaluate ,loc) `(,prev)) (loop prev)]
97+
[(alt _ `(regimes ,splitpoints) prevs) (for-each loop prevs)]
98+
[(alt prog `(taylor ,loc ,pt ,var) `(,prev)) (loop prev)]
99+
[(alt prog `(rr ,loc ,input ,proof) `(,prev))
100100
(loop prev)
101101
(when proof
102102
(for ([step proof])
@@ -195,10 +195,10 @@
195195
"N/A"))
196196

197197
(match altn
198-
[(alt prog 'start (list) _)
198+
[(alt prog 'start (list))
199199
`#hash((program . ,(fpcore->string (program->fpcore prog ctx))) (type . "start") (error . ,err))]
200200

201-
[(alt prog `(regimes ,splitpoints) prevs _)
201+
[(alt prog `(regimes ,splitpoints) prevs)
202202
(define intervals
203203
(for/list ([start-sp (cons (sp -1 -1 #f) splitpoints)]
204204
[end-sp splitpoints])
@@ -217,7 +217,7 @@
217217
(define mask* (map and-fn mask new-mask))
218218
(render-json entry pcontext ctx errcache mask*))))]
219219

220-
[(alt prog `(taylor ,loc ,pt ,var) `(,prev) _)
220+
[(alt prog `(taylor ,loc ,pt ,var) `(,prev))
221221
`#hash((program . ,(fpcore->string (program->fpcore prog ctx)))
222222
(type . "taylor")
223223
(prev . ,(render-json prev pcontext ctx errcache mask))
@@ -226,14 +226,14 @@
226226
(loc . ,loc)
227227
(error . ,err))]
228228

229-
[(alt prog `(evaluate ,loc) `(,prev) _)
229+
[(alt prog `(evaluate ,loc) `(,prev))
230230
`#hash((program . ,(fpcore->string (program->fpcore prog ctx)))
231231
(type . "evaluate")
232232
(prev . ,(render-json prev pcontext ctx errcache mask))
233233
(loc . ,loc)
234234
(error . ,err))]
235235

236-
[(alt prog `(rr ,loc ,input ,proof) `(,prev) _)
236+
[(alt prog `(rr ,loc ,input ,proof) `(,prev))
237237
`#hash((program . ,(fpcore->string (program->fpcore prog ctx)))
238238
(type . "rr")
239239
(prev . ,(render-json prev pcontext ctx errcache mask))
@@ -243,12 +243,11 @@
243243
(loc . ,loc)
244244
(error . ,err))]
245245

246-
[(alt prog 'add-preprocessing `(,prev) preprocessing)
246+
[(alt prog 'add-preprocessing `(,prev))
247247
`#hash((program . ,(fpcore->string (program->fpcore prog ctx)))
248248
(type . "add-preprocessing")
249249
(prev . ,(render-json prev pcontext ctx errcache mask))
250-
(error . ,err)
251-
(preprocessing . ,(map (curry map symbol->string) preprocessing)))]))
250+
(error . ,err))]))
252251

253252
(define (render-proof-json proof pcontext ctx errcache mask)
254253
(for/list ([step proof])

src/reports/plot.rkt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,9 @@
182182
(-> alt? (or/c (listof sp?) #f))
183183
(let loop ([altn altn])
184184
(match altn
185-
[(alt _ `(regimes ,splitpoints) prevs _) splitpoints]
186-
[(alt _ _ (list) _) #f]
187-
[(alt _ _ (list prev _ ...) _) (loop prev)])))
185+
[(alt _ `(regimes ,splitpoints) prevs) splitpoints]
186+
[(alt _ _ (list)) #f]
187+
[(alt _ _ (list prev _ ...)) (loop prev)])))
188188

189189
(define (regime-splitpoints altn)
190190
(map sp-point (drop-right (regime-info altn) 1)))

src/utils/alternative.rkt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88

99
;; Alts are an expression plus a derivation for it.
1010

11-
(struct alt (expr event prevs preprocessing) #:prefab)
11+
(struct alt (expr event prevs) #:prefab)
1212

1313
(define (make-alt expr)
14-
(alt expr 'start '() '()))
14+
(alt expr 'start '()))
1515

1616
(define (alt-cost altn repr)
1717
(define expr-cost (platform-cost-proc (*active-platform*)))

0 commit comments

Comments
 (0)