|
1217 | 1217 |
|
1218 | 1218 | ;; Runs rules over the egraph with the given egg parameters. |
1219 | 1219 | ;; Invariant: the returned egraph is never unsound |
1220 | | -(define (egraph-run-rules egg-graph0 egg-rules params) |
1221 | | - (define node-limit (dict-ref params 'node #f)) |
1222 | | - (define iter-limit (dict-ref params 'iteration #f)) |
1223 | | - (define scheduler (dict-ref params 'scheduler 'backoff)) |
| 1220 | +(define (egraph-run-rules egg-graph0 |
| 1221 | + egg-rules |
| 1222 | + #:node-limit [node-limit #f] |
| 1223 | + #:iter-limit [iter-limit #f] |
| 1224 | + #:scheduler [scheduler 'backoff]) |
1224 | 1225 | (define ffi-rules (map cdr egg-rules)) |
1225 | 1226 |
|
1226 | 1227 | ;; run the rules |
|
1247 | 1248 |
|
1248 | 1249 | ; run the schedule |
1249 | 1250 | (define egg-graph* |
1250 | | - (for/fold ([egg-graph egg-graph]) ([(rules params) (in-dict schedule)]) |
1251 | | - ; run rules in the egraph |
1252 | | - (define egg-rules |
1253 | | - (expand-rules (match rules |
1254 | | - [`lift (platform-lifting-rules)] |
1255 | | - [`lower (platform-lowering-rules)] |
1256 | | - [else rules]))) |
1257 | | - (define-values (egg-graph* iteration-data) (egraph-run-rules egg-graph egg-rules params)) |
| 1251 | + (for/fold ([egg-graph egg-graph]) ([step (in-list schedule)]) |
| 1252 | + (define-values (egg-graph* iteration-data) |
| 1253 | + (match step |
| 1254 | + ['lift |
| 1255 | + (define rules (expand-rules (platform-lifting-rules))) |
| 1256 | + (egraph-run-rules egg-graph rules #:iter-limit 1 #:scheduler 'simple)] |
| 1257 | + ['lower |
| 1258 | + (define rules (expand-rules (platform-lowering-rules))) |
| 1259 | + (egraph-run-rules egg-graph rules #:iter-limit 1 #:scheduler 'simple)] |
| 1260 | + ['rewrite |
| 1261 | + (define rules (expand-rules (*rules*))) |
| 1262 | + (egraph-run-rules egg-graph rules #:node-limit (*node-limit*))])) |
1258 | 1263 |
|
1259 | 1264 | ; get cost statistics |
1260 | 1265 | (for ([iter (in-list iteration-data)] |
|
1290 | 1295 |
|
1291 | 1296 | ;; Constructs an egg runner. |
1292 | 1297 | ;; |
1293 | | -;; The schedule is a list of pairs specifying |
1294 | | -;; - a list of rules |
1295 | | -;; - scheduling parameters: |
1296 | | -;; - node limit: `(node . <number>)` |
1297 | | -;; - iteration limit: `(iteration . <number>)` |
1298 | | -;; - scheduler: `(scheduler . <name>)` [default: backoff] |
1299 | | -;; - `simple`: run all rules without banning |
1300 | | -;; - `backoff`: ban rules if the fire too much |
| 1298 | +;; The schedule is a list of step symbols: |
| 1299 | +;; - `lift`: run lifting rules for 1 iteration with simple scheduler |
| 1300 | +;; - `rewrite`: run rewrite rules up to node limit with backoff scheduler |
| 1301 | +;; - `lower`: run lowering rules for 1 iteration with simple scheduler |
1301 | 1302 | (define (make-egraph batch brfs reprs schedule ctx) |
1302 | 1303 | (define (oops! fmt . args) |
1303 | 1304 | (apply error 'verify-schedule! fmt args)) |
1304 | 1305 | ; verify the schedule |
1305 | | - (for ([instr (in-list schedule)]) |
1306 | | - (match instr |
1307 | | - [(cons rules params) |
1308 | | - ;; `run` instruction |
1309 | | - |
1310 | | - (unless (or (equal? `lift rules) |
1311 | | - (equal? `lower rules) |
1312 | | - (and (list? rules) (andmap rule? rules))) |
1313 | | - (oops! "expected list of rules: `~a`" rules)) |
1314 | | - |
1315 | | - (for ([param (in-list params)]) |
1316 | | - (match param |
1317 | | - [(cons 'node (? nonnegative-integer?)) (void)] |
1318 | | - [(cons 'iteration (? nonnegative-integer?)) (void)] |
1319 | | - [(cons 'scheduler mode) |
1320 | | - (unless (set-member? '(simple backoff) mode) |
1321 | | - (oops! "in instruction `~a`, unknown scheduler `~a`" instr mode))] |
1322 | | - [_ (oops! "in instruction `~a`, unknown parameter `~a`" instr param)]))] |
1323 | | - [_ (oops! "expected `(<rules> . <params>)`, got `~a`" instr)])) |
| 1306 | + (for ([step (in-list schedule)]) |
| 1307 | + (unless (memq step '(lift lower rewrite)) |
| 1308 | + (oops! "unknown schedule step `~a`" step))) |
1324 | 1309 |
|
1325 | 1310 | (define-values (root-ids egg-graph) (egraph-run-schedule batch brfs schedule ctx)) |
1326 | 1311 |
|
|
0 commit comments