Skip to content

Commit

Permalink
Merge pull request #980 from herbie-fp/tiny-egraph-speedups
Browse files Browse the repository at this point in the history
Tiny e-graph speedups
  • Loading branch information
pavpanchekha authored Sep 13, 2024
2 parents 1a4f2f3 + 0b259f2 commit 697848d
Showing 1 changed file with 7 additions and 20 deletions.
27 changes: 7 additions & 20 deletions src/core/egg-herbie.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@
(define (egraph-eclasses egraph-data)
(egraph_get_eclasses (egraph-data-egraph-pointer egraph-data)))

(define empty-u32vec (make-u32vector 0))

;; Extracts the nodes of an e-class as a vector
;; where each enode is either a symbol, number, or list
(define (egraph-get-eclass egraph-data id)
Expand All @@ -217,7 +219,7 @@
(for ([enode (in-vector eclass)]
[i (in-naturals)])
(when (and (symbol? enode) (not (hash-has-key? egg->herbie enode)))
(vector-set! eclass i (cons enode (make-u32vector 0)))))
(vector-set! eclass i (cons enode empty-u32vec))))
eclass)

(define (egraph-find egraph-data id)
Expand Down Expand Up @@ -1204,36 +1206,21 @@
(define root-ids (egraph-add-exprs egg-graph exprs ctx))

; run the schedule
(define rule-apps (make-hash))
(define egg-graph*
(for/fold ([egg-graph egg-graph]) ([instr (in-list schedule)])
(match-define (cons rules params) instr)
(for/fold ([egg-graph egg-graph]) ([(rules params) (in-dict schedule)])
; run rules in the egraph
(define egg-rules (expand-rules rules))
(define-values (egg-graph* iteration-data) (egraph-run-rules egg-graph egg-rules params))

; get cost statistics
(for/fold ([time 0])
([iter (in-list iteration-data)]
[i (in-naturals)])
(for ([iter (in-list iteration-data)]
[i (in-naturals)])
(define cnt (iteration-data-num-nodes iter))
(define cost (apply + (map (λ (id) (egraph-get-cost egg-graph* id i)) root-ids)))
(define new-time (+ time (iteration-data-time iter)))
(timeline-push! 'egraph i cnt cost new-time)
new-time)

;; get rule statistics
(for ([(egg-rule ffi-rule) (in-dict egg-rules)])
(define count (egraph-get-times-applied egg-graph* ffi-rule))
(define canon-name (hash-ref (*canon-names*) (rule-name egg-rule)))
(hash-update! rule-apps canon-name (curry + count) count))
(timeline-push! 'egraph i cnt cost (iteration-data-time iter)))

egg-graph*))

; report rule statistics
(for ([(name count) (in-hash rule-apps)])
(when (> count 0)
(timeline-push! 'rules (~a name) count)))
; root eclasses may have changed
(define root-ids* (map (lambda (id) (egraph-find egg-graph* id)) root-ids))
; return what we need
Expand Down

0 comments on commit 697848d

Please sign in to comment.