Skip to content

Commit 1deb9e1

Browse files
authored
Merge pull request #1416 from herbie-fp/sound-X-rewrites
Drop `sound-X` operations with rewrites
2 parents 6b657f9 + fe2d605 commit 1deb9e1

File tree

4 files changed

+29
-25
lines changed

4 files changed

+29
-25
lines changed

AGENTS.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
- Run `racket src/main.rkt report bench/tutorial.fpcore tmp` to test
99
that your changes work; this should take about 5-10 seconds and all
1010
of the tests should pass, getting basically perfect accuracy.
11+
- After running tests, you should be able to look into `tmp`, and see
12+
one directory per benchmark. Each directory has a `graph.html` with
13+
more detail on what happened, including tracebacks for crashes.
14+
- Herbie prints a seed every time it runs; you can pass --seed N after
15+
the "report" argument to fix a seed. That should be perfectly
16+
reproducible.
1117
- You can also run the unit tests with `raco test <file>`, when unit
1218
tests exist. Often they don't.
1319

src/core/egg-herbie.rkt

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@
289289
[(list 'Rewrite<= rule expr) (list 'Rewrite<= (get-canon-rule-name rule rule) (loop expr type))]
290290
[(list op args ...)
291291
#:when (string-prefix? (symbol->string op) "sound-")
292-
(define op* (string->symbol (substring (symbol->string (car expr)) (string-length "sound-"))))
292+
(define op* (string->symbol (substring (symbol->string op) (string-length "sound-"))))
293293
(define args* (drop-right args 1))
294294
(cons op* (map loop args* (map (const 'real) args*)))]
295295
[(list op args ...)
@@ -525,16 +525,13 @@
525525
;; Synthesizes lowering rules for a given platform.
526526
(define (platform-lowering-rules [pform (*active-platform*)])
527527
(define impls (platform-impls pform))
528-
(append*
529-
(for/list ([impl (in-list impls)])
530-
(hash-ref!
531-
(*lowering-rules*)
532-
(cons impl pform)
533-
(lambda ()
534-
(define name (sym-append 'lower- impl))
535-
(define-values (vars spec-expr impl-expr) (impl->rule-parts impl))
536-
(list (rule name spec-expr impl-expr '(lowering))
537-
(rule (sym-append 'lower-sound- impl) (add-sound spec-expr) impl-expr '(lowering))))))))
528+
(append* (for/list ([impl (in-list impls)])
529+
(hash-ref! (*lowering-rules*)
530+
(cons impl pform)
531+
(lambda ()
532+
(define name (sym-append 'lower- impl))
533+
(define-values (vars spec-expr impl-expr) (impl->rule-parts impl))
534+
(list (rule name spec-expr impl-expr '(lowering))))))))
538535

539536
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
540537
;; Racket egraph
@@ -1257,6 +1254,9 @@
12571254
['lower
12581255
(define rules (expand-rules (platform-lowering-rules)))
12591256
(egraph-run-rules egg-graph rules #:iter-limit 1 #:scheduler 'simple)]
1257+
['unsound
1258+
(define rules (expand-rules (*sound-removal-rules*)))
1259+
(egraph-run-rules egg-graph rules #:iter-limit 1 #:scheduler 'simple)]
12601260
['rewrite
12611261
(define rules (expand-rules (*rules*)))
12621262
(egraph-run-rules egg-graph rules #:node-limit (*node-limit*))]))
@@ -1298,13 +1298,14 @@
12981298
;; The schedule is a list of step symbols:
12991299
;; - `lift`: run lifting rules for 1 iteration with simple scheduler
13001300
;; - `rewrite`: run rewrite rules up to node limit with backoff scheduler
1301+
;; - `unsound`: run sound-removal rules for 1 iteration with simple scheduler
13011302
;; - `lower`: run lowering rules for 1 iteration with simple scheduler
13021303
(define (make-egraph batch brfs reprs schedule ctx)
13031304
(define (oops! fmt . args)
13041305
(apply error 'verify-schedule! fmt args))
13051306
; verify the schedule
13061307
(for ([step (in-list schedule)])
1307-
(unless (memq step '(lift lower rewrite))
1308+
(unless (memq step '(lift lower unsound rewrite))
13081309
(oops! "unknown schedule step `~a`" step)))
13091310

13101311
(define-values (root-ids egg-graph) (egraph-run-schedule batch brfs schedule ctx))

src/core/patch.rkt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,9 @@
157157

158158
(define (run-rr altns global-batch)
159159
(timeline-event! 'rewrite)
160-
; generate required rules
161-
(define rules (*rules*))
162160

163-
; egg schedule (3-phases for mathematical rewrites and implementation selection)
164-
(define schedule '(lift rewrite lower))
161+
; egg schedule (4-phases for mathematical rewrites, sound-X removal, and implementation selection)
162+
(define schedule '(lift rewrite unsound lower))
165163

166164
(define brfs (map alt-expr altns))
167165
(define reprs (map (batch-reprs global-batch (*context*)) brfs))

src/core/rules.rkt

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
"../syntax/syntax.rkt")
77

88
(provide *rules*
9-
(struct-out rule)
10-
add-sound)
9+
*sound-removal-rules*
10+
(struct-out rule))
1111

1212
;; A rule represents "find-and-replacing" `input` by `output`. Both
1313
;; are patterns, meaning that symbols represent pattern variables.
@@ -24,13 +24,6 @@
2424
(define (*rules*)
2525
(filter rule-enabled? *all-rules*))
2626

27-
(define (add-sound expr)
28-
(match expr
29-
[(list (and (or '/ 'pow 'log) op) args ...)
30-
`(,(sym-append "sound-" op) ,@(map add-sound args) ,(gensym))]
31-
[(list op args ...) (cons op (map add-sound expr))]
32-
[_ expr]))
33-
3427
(define-syntax-rule (define-rule rname group input output)
3528
(set! *all-rules* (cons (rule 'rname 'input 'output '(group)) *all-rules*)))
3629

@@ -612,3 +605,9 @@
612605
[cosh-atanh-rev (/ 1 (sqrt (- 1 (* x x)))) (cosh (atanh x))]
613606
[asinh-2 (acosh (+ (* 2 (* x x)) 1)) (* 2 (asinh (fabs x)))]
614607
[acosh-2-rev (* 2 (acosh x)) (acosh (- (* 2 (* x x)) 1))])
608+
609+
; Sound-X removal rules: run these before lowering
610+
(define (*sound-removal-rules*)
611+
(list (rule 'remove-sound-/ '(sound-/ a b fallback) '(/ a b) '(sound-removal))
612+
(rule 'remove-sound-pow '(sound-pow a b fallback) '(pow a b) '(sound-removal))
613+
(rule 'remove-sound-log '(sound-log a fallback) '(log a) '(sound-removal))))

0 commit comments

Comments
 (0)