Skip to content

Commit

Permalink
extracting to batch is dooooooooone
Browse files Browse the repository at this point in the history
  • Loading branch information
AYadrov committed Aug 29, 2024
1 parent e7c196e commit 8b553f0
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 72 deletions.
12 changes: 3 additions & 9 deletions src/core/batch.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@
(define out (make-mutable-batch))
(set-mutable-batch-index! out (batch-restore-index input-batch))

; This fuction here is only because of cycles in loads:(
(define (egg-parsed->expr expr rename-dict type)
(let loop ([expr expr]
[type type])
Expand All @@ -170,9 +171,6 @@
[(list '$approx spec impl) ; approx
(define spec-type (if (representation? type) (representation-type type) type))
(approx (loop spec spec-type) (loop impl type))]
[`(Explanation ,body ...) `(Explanation ,@(map (lambda (e) (loop e type)) body))]
[(list 'Rewrite=> rule expr) (list 'Rewrite=> rule (loop expr type))]
[(list 'Rewrite<= rule expr) (list 'Rewrite<= rule (loop expr type))]
[(list 'if cond ift iff)
(if (representation? type)
(list 'if (loop cond (get-representation 'bool)) (loop ift type) (loop iff type))
Expand Down Expand Up @@ -220,14 +218,10 @@
(batch-push! out (cons op args))]))

; Returns roots while updating nodes of input-batch
(define (finalize-batch roots)
(define (finalize-batch)
(set-batch-nodes! input-batch
(vector-append (batch-nodes input-batch)
(list->vector (reverse (mutable-batch-nodes out)))))
#;(list->vector (reverse roots))

(for/list ([root (in-list roots)])
(batch-ref input-batch root)))
(list->vector (reverse (mutable-batch-nodes out))))))

; Cleaning the batch to start over
(define (clean-batch)
Expand Down
99 changes: 41 additions & 58 deletions src/core/egg-herbie.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -1217,77 +1217,60 @@
; at least one extractable expression
[(hash-has-key? canon key)
(define id* (hash-ref canon key))

(define egg-exprs
(for/list ([enode (vector-ref eclasses id*)])
(match enode
[(? number?) enode]
[(? symbol?) enode]
[(list '$approx spec impl)
(define spec* (vector-ref id->spec spec))
(unless spec*
(error 'regraph-extract-variants "no initial approx node in eclass ~a" id*))
(define impl* (build-expr impl))
(list '$approx spec* impl*)]
[(list 'if cond ift iff)
(define cond* (build-expr cond))
(define ift* (build-expr ift))
(define iff* (build-expr iff))
(list 'if cond* ift* iff*)]
[(list (? impl-exists? impl) ids ...)
(define args
(for/list ([id (in-list ids)])
(define expr (build-expr id))
expr))
(cons impl args)]
[(list (? operator-exists? op) ids ...)
(define args
(for/list ([id (in-list ids)])
(define expr (build-expr id))
expr))
(cons op args)])))

(clean-batch)
(define roots
(for/list ([enode (vector-ref eclasses id*)])
(add-root enode type)))
(define exprs* (finalize-batch roots))
(finalize-batch)

; -------------------------- Debooging
; This debooging crashes if unparsed approx nodes exist in the batch
#;(printf "roots#=~a, eclasses#=~a\n"
(vector-length (batch-roots batch))
(vector-length (vector-ref eclasses id*)))
#;(define egg-exprs-batch (batch->progs batch))
#;(when (not (equal? egg-exprs-batch egg-exprs))
; translate egg IR to Herbie IR
#;(define egg-exprs
(for/list ([enode (vector-ref eclasses id*)])
(match enode
[(? number?) enode]
[(? symbol?) enode]
[(list '$approx spec impl)
(define spec* (vector-ref id->spec spec))
(unless spec*
(error 'regraph-extract-variants "no initial approx node in eclass ~a" id*))
(define impl* (build-expr impl))
(list '$approx spec* impl*)]
[(list 'if cond ift iff)
(define cond* (build-expr cond))
(define ift* (build-expr ift))
(define iff* (build-expr iff))
(list 'if cond* ift* iff*)]
[(list (? impl-exists? impl) ids ...)
(define args
(for/list ([id (in-list ids)])
(define expr (build-expr id))
expr))
(cons impl args)]
[(list (? operator-exists? op) ids ...)
(define args
(for/list ([id (in-list ids)])
(define expr (build-expr id))
expr))
(cons op args)])))

#;(define egg->herbie (regraph-egg->herbie regraph))
#;(define exprs
(for/list ([egg-expr (in-list egg-exprs)])
(egg-parsed->expr (flatten-let egg-expr) egg->herbie type)))

#;(define exprs* (batch->progs batch))
#;(when (not (equal? exprs* exprs))
(println (vector-ref eclasses id*))
(for ([expr* (in-list egg-exprs-batch)]
[expr (in-list egg-exprs)])
(for ([expr* (in-list exprs*)]
[expr (in-list exprs)])
(printf "expr* = ~a\n" expr*)
(printf "expr = ~a\n\n" expr)
(sleep 5)))
#;(println "oolright\n")
; ---------------------------

; translate egg IR to Herbie IR
(define egg->herbie (regraph-egg->herbie regraph))
(define exprs
(for/list ([egg-expr (in-list egg-exprs)])
(egg-parsed->expr (flatten-let egg-expr) egg->herbie type)))

; -------------------------- Debooging
#;(define exprs* (batch->progs batch))
(when (not (equal? exprs* exprs))
(println (vector-ref eclasses id*))
(for ([expr* (in-list exprs*)]
[expr (in-list exprs)])
(printf "expr* = ~a\n" expr*)
(printf "expr = ~a\n\n" expr)
(sleep 5)))
;(println "oolright\n")
; -------------------------

exprs*]
roots]
; no extractable expressions
[else (list)]))

Expand Down
10 changes: 5 additions & 5 deletions src/core/patch.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,16 @@
(define reprs (map (curryr repr-of (*context*)) exprs))
(timeline-push! 'inputs (map ~a exprs))
(define runner (make-egg-runner exprs reprs schedule #:context (*context*)))
; variantss is a (listof batch))
(define batches (run-egg runner `(multi . ,extractor)))
; variantss is a (listof roots))
(define rootss (run-egg runner `(multi . ,extractor)))

; apply changelists
(define rewritten
(reap [sow]
(for ([batch (in-list batches)]
(for ([roots (in-list rootss)]
[altn (in-list altns)])
(for ([root (in-list batch)])
(sow (alt root (list 'rr runner #f #f) (list altn) '()))))))
(for ([root (in-list roots)])
(sow (alt (batch-ref global-batch root) (list 'rr runner #f #f) (list altn) '()))))))

#;(define rewritten
(reap [sow]
Expand Down

0 comments on commit 8b553f0

Please sign in to comment.