|
341 | 341 | (define spec (impl-info impl 'spec))
|
342 | 342 | (values vars spec (cons impl vars)))
|
343 | 343 |
|
344 |
| -;; Parses holes into spec-expr instead of vars |
| 344 | +;; Parses holes into expr instead of vars |
345 | 345 | ;; (_ x y) -> (_ ($hole 'binary64 x) ($hole 'binary64 y))
|
346 | 346 | (define (replace-vars-with-holes expr vars itypes)
|
347 | 347 | (define replacements
|
|
356 | 356 | [_ expr]))) ; it can be a literal or a number
|
357 | 357 |
|
358 | 358 | ;; Synthesizes lifting rules for a given platform.
|
| 359 | +;; Lifting rule applies a rewrite like: |
| 360 | +;; (+.f64 ($hole 'binary64 x) ($hole 'binary64 y)) -> ($hole binary64 (+ x y)) |
| 361 | +;; (hypot.f64 ($hole 'binary64 x) ($hole 'binary64 y)) -> ($hole binary64 (sqrt (* x x) (* y y)))) |
359 | 362 | (define (platform-lifting-rules [pform (*active-platform*)])
|
360 | 363 | ;; every impl maps to a spec
|
361 | 364 | (define impls (platform-impls pform))
|
362 | 365 | (define impl-rules
|
363 | 366 | (for/list ([impl (in-list impls)])
|
364 |
| - (hash-ref! |
365 |
| - (*lifting-rules*) |
366 |
| - (cons impl pform) |
367 |
| - (lambda () |
368 |
| - (define name (sym-append 'lift- impl)) |
369 |
| - (define itypes (impl-info impl 'itype)) |
370 |
| - (define otype (impl-info impl 'otype)) |
371 |
| - (define-values (vars spec-expr impl-expr) (impl->rule-parts impl)) |
372 |
| - (define lhs (replace-vars-with-holes impl-expr vars itypes)) |
373 |
| - (define rhs `($hole ,(representation-name otype) ,spec-expr)) |
374 |
| - |
375 |
| - ; Lifting rule applies a rewrite like: |
376 |
| - ; (+.f64 ($hole 'binary64 x) ($hole 'binary64 y)) -> ($hole binary64 (+ x y)) |
377 |
| - ; (hypot.f64 ($hole 'binary64 x) ($hole 'binary64 y)) -> ($hole binary64 (sqrt (* x x) (* y y)))) |
378 |
| - (rule name lhs rhs (map cons vars itypes) otype '(lifting)))))) |
| 367 | + (hash-ref! (*lifting-rules*) |
| 368 | + (cons impl pform) |
| 369 | + (lambda () |
| 370 | + (define name (sym-append 'lift- impl)) |
| 371 | + (define itypes (impl-info impl 'itype)) |
| 372 | + (define otype (impl-info impl 'otype)) |
| 373 | + (define-values (vars spec-expr impl-expr) (impl->rule-parts impl)) |
| 374 | + (define lhs (replace-vars-with-holes impl-expr vars itypes)) |
| 375 | + (define rhs `($hole ,(representation-name otype) ,spec-expr)) |
| 376 | + (rule name lhs rhs (map cons vars itypes) otype '(lifting)))))) |
| 377 | + |
| 378 | + (define lift-literal-rule |
| 379 | + (rule 'lift-literal |
| 380 | + '($literal a repr) |
| 381 | + '($hole repr a) |
| 382 | + '((a . real) (repr . real)) |
| 383 | + 'real |
| 384 | + '(lifting))) |
| 385 | + |
| 386 | + (define lift-approx-rule |
| 387 | + (rule 'lift-approx |
| 388 | + '($approx s ($hole r t)) |
| 389 | + '($hole r ($approx s t)) |
| 390 | + '((s . real) (r . real) (t . real)) |
| 391 | + 'real |
| 392 | + '(lifting))) |
| 393 | + |
| 394 | + (define lift-if-rule |
| 395 | + (rule 'lift-if |
| 396 | + '(if ($hole bool c) |
| 397 | + ($hole r t) |
| 398 | + ($hole r f)) |
| 399 | + '($hole r (if c t f)) |
| 400 | + '((c . real) (r . real) (t . real) (f . real)) |
| 401 | + 'real |
| 402 | + '(lifting))) |
| 403 | + |
379 | 404 | ;; special rule for approx nodes
|
380 | 405 | ; (define approx-rule (rule 'lift-approx (approx 'a 'b) 'a '((a . real) (b . real)) 'real))
|
381 | 406 | ; (cons approx-rule impl-rules))
|
382 |
| - impl-rules) |
| 407 | + (list* lift-if-rule lift-approx-rule lift-literal-rule impl-rules)) |
383 | 408 |
|
384 | 409 | ;; Synthesizes lowering rules for a given platform.
|
| 410 | +;; Lowering rules apply a rewrite like |
| 411 | +;; ($hole binary64 (+ x y)) -> (+.f64 ($hole binary64 x) ($hole binary64 y)) |
| 412 | +;; question? what to do when we may end up with ($hole binary64 ($hole binary32 x)) ? |
385 | 413 | (define (platform-lowering-rules [pform (*active-platform*)])
|
386 | 414 | (define impls (platform-impls pform))
|
387 | 415 | (for/list ([impl (in-list impls)])
|
|
392 | 420 | (define itypes (impl-info impl 'itype))
|
393 | 421 | (define otype (impl-info impl 'otype))
|
394 | 422 | (define-values (vars spec-expr impl-expr) (impl->rule-parts impl))
|
395 |
| - |
396 |
| - ; shrinking lowering rules |
397 |
| - ; ($hole binary64 (+ x y)) -> (+.f64 ($hole binary64 x) ($hole binary64 y)) |
398 |
| - ; question? what to do when we may end up with ($hole binary64 ($hole binary32 x)) ? |
399 |
| - (define name* (sym-append 'lower-shrink- impl)) |
400 |
| - (define op (car spec-expr)) |
401 |
| - (define op* (car impl-expr)) |
402 |
| - (define lhs `($hole ,(representation-name otype) ,(list* op vars))) |
403 |
| - (define rhs |
404 |
| - `,(list* op* (map (λ (x y) `($hole ,(representation-name y) ,x)) vars itypes))) |
405 |
| - (define shrinking-lowering-rule |
406 |
| - (rule name* |
407 |
| - lhs |
408 |
| - rhs |
409 |
| - (map cons vars (map representation-type itypes)) |
410 |
| - (representation-type otype) |
411 |
| - '(lowering))) |
412 |
| - |
413 |
| - #;(rule name spec-expr impl-expr (map cons vars itypes) otype '(lowering)) |
414 |
| - shrinking-lowering-rule)))) |
| 423 | + (define lhs `($hole ,(representation-name otype) ,spec-expr)) |
| 424 | + (define rhs (replace-vars-with-holes impl-expr vars itypes)) |
| 425 | + (rule name |
| 426 | + lhs |
| 427 | + rhs |
| 428 | + (map cons vars (map representation-type itypes)) |
| 429 | + (representation-type otype) |
| 430 | + '(lowering)))))) |
415 | 431 |
|
416 | 432 | (define (expr-otype expr)
|
417 | 433 | (match expr
|
|
0 commit comments