|
7 | 7 | u32vector-set! |
8 | 8 | u32vector-ref |
9 | 9 | list->u32vector |
10 | | - u32vector->list)) |
| 10 | + u32vector->list) |
| 11 | + json) ; for dumping |
11 | 12 |
|
12 | 13 | (require "programs.rkt" |
13 | 14 | "rules.rkt" |
|
879 | 880 | ; construct the `regraph` instance |
880 | 881 | (regraph eclasses types leaf? constants specs parents canon egg->herbie)) |
881 | 882 |
|
| 883 | +(define (regraph-nodes->json regraph) |
| 884 | + (define cost (platform-node-cost-proc (*active-platform*))) |
| 885 | + (for/hash ([n (in-naturals)] |
| 886 | + [eclass (in-vector (regraph-eclasses regraph))] |
| 887 | + #:when true |
| 888 | + [k (in-naturals)] |
| 889 | + [enode eclass]) |
| 890 | + (define type (vector-ref (regraph-types regraph) n)) |
| 891 | + (define cost |
| 892 | + (if (representation? type) |
| 893 | + (match enode |
| 894 | + [(? number?) (platform-repr-cost (*active-platform*) type)] |
| 895 | + [(? symbol?) (platform-repr-cost (*active-platform*) type)] |
| 896 | + [(list '$approx x y) 0] |
| 897 | + [(list 'if c x y) |
| 898 | + (match (platform-impl-cost (*active-platform*) 'if) |
| 899 | + [`(max ,n) n] ; Not quite right |
| 900 | + [`(sum ,n) n])] |
| 901 | + [(list op args ...) (platform-impl-cost (*active-platform*) op)]) |
| 902 | + 1)) |
| 903 | + (values (string->symbol (format "~a.~a" n k)) |
| 904 | + (hash 'op |
| 905 | + (~a (if (list? enode) (car enode) enode)) |
| 906 | + 'children |
| 907 | + (if (list? enode) (map ~a (cdr enode)) '()) |
| 908 | + 'eclass |
| 909 | + (~a n) |
| 910 | + 'cost |
| 911 | + cost)))) |
| 912 | + |
882 | 913 | ;; Egraph node has children. |
883 | 914 | ;; Nullary operators have no children! |
884 | 915 | (define (node-has-children? node) |
|
1322 | 1353 | ; make the runner |
1323 | 1354 | (egg-runner batch roots reprs schedule ctx)) |
1324 | 1355 |
|
| 1356 | +(define (regraph-dump regraph root-ids reprs) |
| 1357 | + (define dump-dir "dump-egg") |
| 1358 | + (unless (directory-exists? dump-dir) |
| 1359 | + (make-directory dump-dir)) |
| 1360 | + (define name |
| 1361 | + (for/first ([i (in-naturals)] |
| 1362 | + #:unless (file-exists? (build-path dump-dir (format "~a.json" i)))) |
| 1363 | + (build-path dump-dir (format "~a.json" i)))) |
| 1364 | + (define nodes (regraph-nodes->json regraph)) |
| 1365 | + (define canon (regraph-canon regraph)) |
| 1366 | + (define roots |
| 1367 | + (filter values |
| 1368 | + (for/list ([id (in-list root-ids)] |
| 1369 | + [type (in-list reprs)]) |
| 1370 | + (hash-ref canon (cons id type) #f)))) |
| 1371 | + (call-with-output-file |
| 1372 | + name |
| 1373 | + #:exists 'replace |
| 1374 | + (lambda (p) (write-json (hash 'nodes nodes 'root_eclasses (map ~a roots) 'class_data (hash)) p)))) |
| 1375 | + |
1325 | 1376 | ;; Runs egg using an egg runner. |
1326 | 1377 | ;; |
1327 | 1378 | ;; Argument `cmd` specifies what to get from the e-graph: |
|
1342 | 1393 | (define regraph (make-regraph egg-graph)) |
1343 | 1394 | (define extract-id (extractor regraph)) |
1344 | 1395 | (define reprs (egg-runner-reprs runner)) |
| 1396 | + (when (flag-set? 'dump 'egg) |
| 1397 | + (regraph-dump regraph root-ids reprs)) |
1345 | 1398 | (for/list ([id (in-list root-ids)] |
1346 | 1399 | [repr (in-list reprs)]) |
1347 | 1400 | (regraph-extract-best regraph extract-id id repr))] |
1348 | 1401 | [`(multi . ,extractor) ; multi expression extraction |
1349 | 1402 | (define regraph (make-regraph egg-graph)) |
1350 | 1403 | (define extract-id (extractor regraph)) |
1351 | 1404 | (define reprs (egg-runner-reprs runner)) |
| 1405 | + (when (flag-set? 'dump 'egg) |
| 1406 | + (regraph-dump regraph root-ids reprs)) |
1352 | 1407 |
|
1353 | 1408 | ; List of roots inside the batch |
1354 | 1409 | (for/list ([id (in-list root-ids)] |
|
0 commit comments