Skip to content

Commit c1b7ae6

Browse files
authored
Merge pull request #1348 from herbie-fp/fix-bugs
Fix some random bugs
2 parents 4e7ee9a + fd6b936 commit c1b7ae6

File tree

3 files changed

+46
-35
lines changed

3 files changed

+46
-35
lines changed

src/api/server.rkt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@
101101
(define manager #f)
102102

103103
(define (manager-ask msg . args)
104-
(log "Asking manager: ~a, ~a.\n" msg args)
104+
(log "Asking manager: ~a.\n" msg)
105105
(match manager
106106
[(? place? x) (apply manager-ask-threaded x msg args)]
107107
['basic (apply manager-ask-basic msg args)]))
@@ -122,22 +122,25 @@
122122
(define command (hash-ref queued-jobs job-id #f))
123123
(define result (and command (herbie-do-server-job command job-id)))
124124
(when command
125+
(hash-remove! queued-jobs job-id)
125126
(hash-set! completed-jobs job-id result))
126127
result]
127128
[(list 'result job-id) (hash-ref completed-jobs job-id #f)]
128129
[(list 'timeline job-id)
129130
(define command (hash-ref queued-jobs job-id #f))
130131
(define result (and command (herbie-do-server-job command job-id)))
131132
(when command
133+
(hash-remove! queued-jobs job-id)
132134
(hash-set! completed-jobs job-id result))
133135
result]
134136
[(list 'check job-id)
135137
(define command (hash-ref queued-jobs job-id #f))
136138
(define result (and command (herbie-do-server-job command job-id)))
137139
(when command
140+
(hash-remove! queued-jobs job-id)
138141
(hash-set! completed-jobs job-id result))
139142
job-id]
140-
[(list 'count) 0]
143+
[(list 'count) (hash-count queued-jobs)]
141144
[(list 'improve)
142145
(for/list ([(job-id result) (in-hash completed-jobs)]
143146
#:when (equal? (hash-ref result 'command) "improve"))
@@ -197,6 +200,7 @@
197200
; Check if the job is already in progress.
198201
(unless (hash-has-key? current-jobs jid)
199202
(place-channel-put worker (list 'apply self command jid))
203+
(hash-set! current-jobs jid wid)
200204
(hash-set! reassigned wid jid)
201205
(hash-set! busy-workers wid worker)))
202206
; remove X many jobs from the Q and update waiting-workers

src/core/egg-herbie.rkt

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -294,12 +294,23 @@
294294
[`(Explanation ,body ...) `(Explanation ,@(map (lambda (e) (loop e type)) body))]
295295
[(list 'Rewrite=> rule expr) (list 'Rewrite=> (get-canon-rule-name rule rule) (loop expr type))]
296296
[(list 'Rewrite<= rule expr) (list 'Rewrite<= (get-canon-rule-name rule rule) (loop expr type))]
297-
[(list (? impl-exists? impl) args ...) (cons impl (map loop args (impl-info impl 'itype)))]
298297
[(list op args ...)
299298
#:when (string-contains? (~a op) "unsound")
300299
(define op* (string->symbol (string-replace (symbol->string (car expr)) "unsound-" "")))
301300
(cons op* (map loop args (map (const 'real) args)))]
302-
[(list op args ...) (cons op (map loop args (operator-info op 'itype)))])))
301+
[(list op args ...)
302+
;; Unfortunately the type parameter doesn't tell us much because mixed exprs exist
303+
;; so if we see something like (and a b) we literally don't know which "and" it is
304+
(cons op
305+
(map loop
306+
args
307+
(cond
308+
[(and (operator-exists? op) (impl-exists? op))
309+
(if (representation? type)
310+
(impl-info op 'itype)
311+
(operator-info op 'itype))]
312+
[(impl-exists? op) (impl-info op 'itype)]
313+
[(operator-exists? op) (operator-info op 'itype)])))])))
303314

304315
;; Parses a string from egg into a single S-expr.
305316
(define (egg-expr->expr egg-expr ctx)
@@ -537,9 +548,10 @@
537548
(cond
538549
[(eq? f '$approx) (platform-reprs (*active-platform*))]
539550
[(string-contains? (~a f) "unsound") (list 'real)]
540-
[(impl-exists? f) (list (impl-info f 'otype))]
541-
[(eq? f 'if) '(real bool)]
542-
[else (list (operator-info f 'otype))])]))
551+
[else
552+
(filter values
553+
(list (and (impl-exists? f) (impl-info f 'otype))
554+
(and (operator-exists? f) (operator-info f 'otype))))])]))
543555

544556
;; Rebuilds an e-node using typed e-classes
545557
(define (rebuild-enode enode type lookup)
@@ -558,8 +570,7 @@
558570
[else
559571
(define itypes
560572
(cond
561-
[(impl-exists? f) (impl-info f 'itype)]
562-
[(eq? f 'if) (list 'bool type type)]
573+
[(representation? type) (impl-info f 'itype)]
563574
[else (operator-info f 'itype)]))
564575
; unsafe since we don't check that |itypes| = |ids|
565576
; optimize for common cases to avoid extra allocations
@@ -1016,9 +1027,13 @@
10161027
(representation-type type)
10171028
type))
10181029
(approx (loop spec spec-type) (loop impl type))]
1019-
[(list (? impl-exists? impl) args ...) (cons impl (map loop args (impl-info impl 'itype)))]
1020-
[(list 'if c t f) (list 'if (loop c 'bool) (loop t 'real) (loop f 'real))]
1021-
[(list op args ...) (cons op (map loop args (operator-info op 'itype)))])))
1030+
[(list op args ...)
1031+
(cons op
1032+
(map loop
1033+
args
1034+
(if (representation? type)
1035+
(impl-info op 'itype)
1036+
(operator-info op 'itype))))])))
10221037

10231038
(define (eggref id)
10241039
(cdr (vector-ref egg-nodes id)))
@@ -1048,20 +1063,14 @@
10481063
(define final-spec (egg-parsed->expr spec* spec-type))
10491064
(define final-spec-idx (mutable-batch-munge! out final-spec))
10501065
(approx final-spec-idx (loop impl type))]
1051-
[(list (? impl-exists? impl) (app eggref args) ...)
1052-
(define args*
1053-
(for/list ([arg (in-list args)]
1054-
[type (in-list (impl-info impl 'itype))])
1055-
(loop arg type)))
1056-
(cons impl args*)]
1057-
[(list 'if c t f)
1058-
(list 'if (loop (eggref c) 'bool) (loop (eggref t) type) (loop (eggref f) type))]
1059-
[(list (? operator-exists? op) (app eggref args) ...)
1066+
[(list impl (app eggref args) ...)
10601067
(define args*
10611068
(for/list ([arg (in-list args)]
1062-
[type (in-list (operator-info op 'itype))])
1069+
[type (in-list (if (representation? type)
1070+
(impl-info impl 'itype)
1071+
(operator-info impl 'itype)))])
10631072
(loop arg type)))
1064-
(cons op args*)]))
1073+
(cons impl args*)]))
10651074
(mutable-batch-push! out enode*)))
10661075
(batchref input-batch idx))
10671076

src/core/searchreals.rkt

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,14 @@
8989

9090
(define (find-intervals compiler rects #:fuel [depth 128])
9191
(define var-reprs (real-compiler-var-reprs compiler))
92-
(if (or (null? rects) (null? (first rects)))
93-
(map (curryr cons 'other) rects)
94-
(let loop ([space (apply make-search-space rects)]
95-
[n 0])
96-
(match-define (search-space true false other true-hints other-hints) space)
97-
(timeline-push! 'sampling n (make-sampling-table var-reprs true false other))
92+
(let loop ([space (apply make-search-space rects)]
93+
[n 0])
94+
(match-define (search-space true false other true-hints other-hints) space)
95+
(timeline-push! 'sampling n (make-sampling-table var-reprs true false other))
9896

99-
(define n* (remainder n (length (first rects))))
100-
(if (or (>= n depth) (empty? (search-space-other space)) (>= (length other) (expt 2 depth)))
101-
(list (append (search-space-true space) (search-space-other space))
102-
(append (search-space-true-hints space) (search-space-other-hints space))
103-
(make-sampling-table var-reprs true false other))
104-
(loop (search-step compiler space n*) (+ n 1))))))
97+
(define n* (remainder n (vector-length var-reprs)))
98+
(if (or (>= n depth) (empty? (search-space-other space)) (>= (length other) (expt 2 depth)))
99+
(list (append (search-space-true space) (search-space-other space))
100+
(append (search-space-true-hints space) (search-space-other-hints space))
101+
(make-sampling-table var-reprs true false other))
102+
(loop (search-step compiler space n*) (+ n 1)))))

0 commit comments

Comments
 (0)