1 ;;; Tree-IL partial evaluator
3 ;; Copyright (C) 2011 Free Software Foundation, Inc.
5 ;;;; This library is free software; you can redistribute it and/or
6 ;;;; modify it under the terms of the GNU Lesser General Public
7 ;;;; License as published by the Free Software Foundation; either
8 ;;;; version 3 of the License, or (at your option) any later version.
10 ;;;; This library is distributed in the hope that it will be useful,
11 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;;;; Lesser General Public License for more details.
15 ;;;; You should have received a copy of the GNU Lesser General Public
16 ;;;; License along with this library; if not, write to the Free Software
17 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 (define-module (language tree-il peval)
20 #:use-module (language tree-il)
21 #:use-module (language tree-il primitives)
22 #:use-module (ice-9 vlist)
23 #:use-module (ice-9 match)
24 #:use-module (srfi srfi-1)
25 #:use-module (srfi srfi-9)
26 #:use-module (srfi srfi-11)
27 #:use-module (srfi srfi-26)
31 ;;; Partial evaluation is Guile's most important source-to-source
32 ;;; optimization pass. It performs copy propagation, dead code
33 ;;; elimination, inlining, and constant folding, all while preserving
34 ;;; the order of effects in the residual program.
36 ;;; For more on partial evaluation, see William Cook’s excellent
37 ;;; tutorial on partial evaluation at DSL 2011, called “Build your own
38 ;;; partial evaluator in 90 minutes”[0].
40 ;;; Our implementation of this algorithm was heavily influenced by
41 ;;; Waddell and Dybvig's paper, "Fast and Effective Procedure Inlining",
42 ;;; IU CS Dept. TR 484.
44 ;;; [0] http://www.cs.utexas.edu/~wcook/tutorial/.
47 ;; First, some helpers.
49 (define-syntax *logging* (identifier-syntax #f))
51 ;; For efficiency we define *logging* to inline to #f, so that the call
52 ;; to log* gets optimized out. If you want to log, uncomment these
55 ;; (define %logging #f)
56 ;; (define-syntax *logging* (identifier-syntax %logging))
58 ;; Then you can change %logging at runtime.
64 (or (eq? *logging* #t)
65 (memq 'event *logging*)))
66 (log* 'event arg ...)))))
68 (define (log* event . args)
69 (let ((pp (module-ref (resolve-interface '(ice-9 pretty-print))
71 (pp `(log ,event . ,args))
75 (define-syntax-rule (let/ec k e e* ...)
76 (let ((tag (make-prompt-tag)))
80 (let ((k (lambda args (apply abort-to-prompt tag args))))
82 (lambda (_ res) res))))
84 (define (tree-il-any proc exp)
86 (tree-il-fold (lambda (exp res)
87 (let ((res (proc exp)))
90 (let ((res (proc exp)))
95 (define (vlist-any proc vlist)
96 (let ((len (vlist-length vlist)))
99 (or (proc (vlist-ref vlist i))
102 ;; Peval will do a one-pass analysis on the source program to determine
103 ;; the set of assigned lexicals, and to identify unreferenced and
104 ;; singly-referenced lexicals.
106 (define-record-type <var>
107 (make-var name gensym refcount set?)
111 (refcount var-refcount set-var-refcount!)
112 (set? var-set? set-var-set?!))
114 (define* (build-var-table exp #:optional (table vlist-null))
118 (($ <lexical-ref> src name gensym)
119 (let ((var (cdr (vhash-assq gensym res))))
120 (set-var-refcount! var (1+ (var-refcount var)))
125 (($ <lambda-case> src req opt rest kw init gensyms body alt)
126 (fold (lambda (name sym res)
127 (vhash-consq sym (make-var name sym 0 #f) res))
129 (append req (or opt '()) (if rest (list rest) '())
131 ((aok? (kw name sym) ...) name)
134 (($ <let> src names gensyms vals body)
135 (fold (lambda (name sym res)
136 (vhash-consq sym (make-var name sym 0 #f) res))
138 (($ <letrec> src in-order? names gensyms vals body)
139 (fold (lambda (name sym res)
140 (vhash-consq sym (make-var name sym 0 #f) res))
142 (($ <fix> src names gensyms vals body)
143 (fold (lambda (name sym res)
144 (vhash-consq sym (make-var name sym 0 #f) res))
146 (($ <lexical-set> src name gensym exp)
147 (set-var-set?! (cdr (vhash-assq gensym res)) #t)
150 (lambda (exp res) res)
153 ;; Counters are data structures used to limit the effort that peval
154 ;; spends on particular inlining attempts. Each call site in the source
155 ;; program is allocated some amount of effort. If peval exceeds the
156 ;; effort counter while attempting to inline a call site, it aborts the
157 ;; inlining attempt and residualizes a call instead.
159 ;; As there is a fixed number of call sites, that makes `peval' O(N) in
160 ;; the number of call sites in the source program.
162 ;; Counters should limit the size of the residual program as well, but
163 ;; currently this is not implemented.
165 ;; At the top level, before seeing any peval call, there is no counter,
166 ;; because inlining will terminate as there is no recursion. When peval
167 ;; sees a call at the top level, it will make a new counter, allocating
168 ;; it some amount of effort and size.
170 ;; This top-level effort counter effectively "prints money". Within a
171 ;; toplevel counter, no more effort is printed ex nihilo; for a nested
172 ;; inlining attempt to proceed, effort must be transferred from the
173 ;; toplevel counter to the nested counter.
175 ;; Via `data' and `prev', counters form a linked list, terminating in a
176 ;; toplevel counter. In practice `data' will be the a pointer to the
177 ;; source expression of the procedure being inlined.
179 ;; In this way peval can detect a recursive inlining attempt, by walking
180 ;; back on the `prev' links looking for matching `data'. Recursive
181 ;; counters receive a more limited effort allocation, as we don't want
182 ;; to spend all of the effort for a toplevel inlining site on loops.
183 ;; Also, recursive counters don't need a prompt at each inlining site:
184 ;; either the call chain folds entirely, or it will be residualized at
185 ;; its original call.
187 (define-record-type <counter>
188 (%make-counter effort size continuation recursive? data prev)
190 (effort effort-counter)
192 (continuation counter-continuation)
193 (recursive? counter-recursive? set-counter-recursive?!)
197 (define (abort-counter c)
198 ((counter-continuation c)))
200 (define (record-effort! c)
201 (let ((e (effort-counter c)))
202 (if (zero? (variable-ref e))
204 (variable-set! e (1- (variable-ref e))))))
206 (define (record-size! c)
207 (let ((s (size-counter c)))
208 (if (zero? (variable-ref s))
210 (variable-set! s (1- (variable-ref s))))))
212 (define (find-counter data counter)
214 (if (eq? data (counter-data counter))
216 (find-counter data (counter-prev counter)))))
218 (define* (transfer! from to #:optional
219 (effort (variable-ref (effort-counter from)))
220 (size (variable-ref (size-counter from))))
221 (define (transfer-counter! from-v to-v amount)
222 (let* ((from-balance (variable-ref from-v))
223 (to-balance (variable-ref to-v))
224 (amount (min amount from-balance)))
225 (variable-set! from-v (- from-balance amount))
226 (variable-set! to-v (+ to-balance amount))))
228 (transfer-counter! (effort-counter from) (effort-counter to) effort)
229 (transfer-counter! (size-counter from) (size-counter to) size))
231 (define (make-top-counter effort-limit size-limit continuation data)
232 (%make-counter (make-variable effort-limit)
233 (make-variable size-limit)
239 (define (make-nested-counter continuation data current)
240 (let ((c (%make-counter (make-variable 0)
246 (transfer! current c)
249 (define (make-recursive-counter effort-limit size-limit orig current)
250 (let ((c (%make-counter (make-variable 0)
252 (counter-continuation orig)
256 (transfer! current c effort-limit size-limit)
259 ;; Operand structures allow bindings to be processed lazily instead of
260 ;; eagerly. By doing so, hopefully we can get process them in a way
261 ;; appropriate to their use contexts. Operands also prevent values from
262 ;; being visited multiple times, wasting effort.
264 ;; TODO: Record value size in operand structure?
266 (define-record-type <operand>
267 (%make-operand var sym visit source visit-count residualize?
268 copyable? residual-value constant-value)
272 (visit %operand-visit)
273 (source operand-source)
274 (visit-count operand-visit-count set-operand-visit-count!)
275 (residualize? operand-residualize? set-operand-residualize?!)
276 (copyable? operand-copyable? set-operand-copyable?!)
277 (residual-value operand-residual-value set-operand-residual-value!)
278 (constant-value operand-constant-value set-operand-constant-value!))
280 (define* (make-operand var sym #:optional source visit)
281 ;; Bound operands are considered copyable until we prove otherwise.
282 (%make-operand var sym visit source 0 #f (and source #t) #f #f))
284 (define (make-bound-operands vars syms sources visit)
285 (map (lambda (x y z) (make-operand x y z visit)) vars syms sources))
287 (define (make-unbound-operands vars syms)
288 (map make-operand vars syms))
290 (define* (visit-operand op counter ctx #:optional effort-limit size-limit)
291 ;; Peval is O(N) in call sites of the source program. However,
292 ;; visiting an operand can introduce new call sites. If we visit an
293 ;; operand outside a counter -- i.e., outside an inlining attempt --
294 ;; this can lead to divergence. So, if we are visiting an operand to
295 ;; try to copy it, and there is no counter, make a new one.
297 ;; This will only happen at most as many times as there are lexical
298 ;; references in the source program.
299 (and (zero? (operand-visit-count op))
302 (set-operand-visit-count! op (1+ (operand-visit-count op))))
304 (and (operand-source op)
305 (if (or counter (and (not effort-limit) (not size-limit)))
306 ((%operand-visit op) (operand-source op) counter ctx)
308 (define (abort) (k #f))
311 (make-top-counter effort-limit size-limit abort op)
314 (set-operand-visit-count! op (1- (operand-visit-count op)))))))
316 ;; A helper for constant folding.
318 (define (types-check? primitive-name args)
321 ((not pair? null? list? symbol? vector? struct?)
325 ;; FIXME: add more cases?
328 (define* (peval exp #:optional (cenv (current-module)) (env vlist-null)
330 (operator-size-limit 40)
331 (operand-size-limit 20)
332 (value-size-limit 10)
334 (recursive-effort-limit 100))
335 "Partially evaluate EXP in compilation environment CENV, with
336 top-level bindings from ENV and return the resulting expression."
338 ;; This is a simple partial evaluator. It effectively performs
339 ;; constant folding, copy propagation, dead code elimination, and
344 ;; Propagate copies across toplevel bindings, if we can prove the
345 ;; bindings to be immutable.
347 ;; Specialize lambda expressions with invariant arguments.
349 (define local-toplevel-env
350 ;; The top-level environment of the module being compiled.
352 (($ <toplevel-define> _ name)
353 (vhash-consq name #t env))
354 (($ <sequence> _ exps)
357 (($ <toplevel-define> _ name)
358 (vhash-consq name #t r))
364 (define (local-toplevel? name)
365 (vhash-assq name local-toplevel-env))
368 ;; renamed-term -> original-term
370 (define store (build-var-table exp))
372 (define (lookup-var sym)
373 (let ((v (vhash-assq sym store)))
374 (if v (cdr v) (error "unbound var" sym (vlist->list store)))))
376 (define (fresh-gensyms vars)
378 (let ((new (gensym (string-append (symbol->string (var-name var))
380 (set! store (vhash-consq new var store))
384 (define (assigned-lexical? sym)
385 (var-set? (lookup-var sym)))
387 (define (lexical-refcount sym)
388 (var-refcount (lookup-var sym)))
390 ;; ORIG has been alpha-renamed to NEW. Analyze NEW and record a link
393 (define (record-source-expression! orig new)
394 (set! store (vhash-consq new (source-expression orig) store))
397 ;; Find the source expression corresponding to NEW. Used to detect
398 ;; recursive inlining attempts.
400 (define (source-expression new)
401 (let ((x (vhash-assq new store)))
404 (define* (residualize-lexical op #:optional ctx val)
405 (log 'residualize op)
406 (set-operand-residualize?! op #t)
408 (set-operand-residual-value! op val))
409 (make-lexical-ref #f (var-name (operand-var op)) (operand-sym op)))
411 (define (apply-primitive name args)
412 ;; todo: further optimize commutative primitives
417 (apply (module-ref the-scm-module name) args))
419 (values #t results))))
423 (define (inline-values exp src names gensyms body)
424 (let loop ((exp exp))
426 ;; Some expression types are always singly-valued.
435 ($ <lexical-set>) ; FIXME: these set! expressions
436 ($ <toplevel-set>) ; could return zero values in
437 ($ <toplevel-define>) ; the future
440 (and (= (length names) 1)
441 (make-let src names gensyms (list exp) body)))
442 (($ <application> src
443 ($ <primitive-ref> _ (? singly-valued-primitive? name)))
444 (and (= (length names) 1)
445 (make-let src names gensyms (list exp) body)))
447 ;; Statically-known number of values.
448 (($ <application> src ($ <primitive-ref> _ 'values) vals)
449 (and (= (length names) (length vals))
450 (make-let src names gensyms vals body)))
452 ;; Not going to copy code into both branches.
453 (($ <conditional>) #f)
455 ;; Bail on other applications.
456 (($ <application>) #f)
458 ;; Bail on prompt and abort.
462 ;; Propagate to tail positions.
463 (($ <let> src names gensyms vals body)
464 (let ((body (loop body)))
466 (make-let src names gensyms vals body))))
467 (($ <letrec> src in-order? names gensyms vals body)
468 (let ((body (loop body)))
470 (make-letrec src in-order? names gensyms vals body))))
471 (($ <fix> src names gensyms vals body)
472 (let ((body (loop body)))
474 (make-fix src names gensyms vals body))))
475 (($ <let-values> src exp
476 ($ <lambda-case> src2 req opt rest kw inits gensyms body #f))
477 (let ((body (loop body)))
479 (make-let-values src exp
480 (make-lambda-case src2 req opt rest kw
481 inits gensyms body #f)))))
482 (($ <dynwind> src winder body unwinder)
483 (let ((body (loop body)))
485 (make-dynwind src winder body unwinder))))
486 (($ <dynlet> src fluids vals body)
487 (let ((body (loop body)))
489 (make-dynlet src fluids vals body))))
490 (($ <sequence> src exps)
493 (let ((tail (loop tail)))
495 (make-sequence src (append head (list tail)))))))))))
497 (define (make-values src values)
499 ((single) single) ; 1 value
500 ((_ ...) ; 0, or 2 or more values
501 (make-application src (make-primitive-ref src 'values)
504 (define (constant-expression? x)
505 ;; Return true if X is constant---i.e., if it is known to have no
506 ;; effects, does not allocate storage for a mutable object, and does
507 ;; not access mutable data (like `car' or toplevel references).
513 (($ <lambda-case> _ req opt rest kw inits _ body alternate)
514 (and (every loop inits) (loop body)
515 (or (not alternate) (loop alternate))))
516 (($ <lexical-ref> _ _ gensym)
517 (not (assigned-lexical? gensym)))
518 (($ <primitive-ref>) #t)
519 (($ <conditional> _ condition subsequent alternate)
520 (and (loop condition) (loop subsequent) (loop alternate)))
521 (($ <application> _ ($ <primitive-ref> _ name) args)
522 (and (effect-free-primitive? name)
523 (not (constructor-primitive? name))
524 (types-check? name args)
526 (($ <application> _ ($ <lambda> _ _ body) args)
527 (and (loop body) (every loop args)))
528 (($ <sequence> _ exps)
530 (($ <let> _ _ _ vals body)
531 (and (every loop vals) (loop body)))
532 (($ <letrec> _ _ _ _ vals body)
533 (and (every loop vals) (loop body)))
534 (($ <fix> _ _ _ vals body)
535 (and (every loop vals) (loop body)))
536 (($ <let-values> _ exp body)
537 (and (loop exp) (loop body)))
538 (($ <prompt> _ tag body handler)
539 (and (loop tag) (loop body) (loop handler)))
542 (define (prune-bindings ops in-order? body counter ctx build-result)
543 ;; This helper handles both `let' and `letrec'/`fix'. In the latter
544 ;; cases we need to make sure that if referenced binding A needs
545 ;; as-yet-unreferenced binding B, that B is processed for value.
546 ;; Likewise if C, when processed for effect, needs otherwise
547 ;; unreferenced D, then D needs to be processed for value too.
549 (define (referenced? op)
550 ;; When we visit lambdas in operator context, we just copy them,
551 ;; as we will process their body later. However this does have
552 ;; the problem that any free var referenced by the lambda is not
553 ;; marked as needing residualization. Here we hack around this
554 ;; and treat all bindings as referenced if we are in operator
556 (or (eq? ctx 'operator) (operand-residualize? op)))
558 ;; values := (op ...)
559 ;; effects := (op ...)
560 (define (residualize values effects)
561 ;; Note, values and effects are reversed.
564 (let ((values (filter operand-residual-value ops)))
567 (build-result (map (compose var-name operand-var) values)
568 (map operand-sym values)
569 (map operand-residual-value values)
575 (let ((effect-vals (map operand-residual-value effects)))
576 (make-sequence #f (reverse (cons body effect-vals)))))))
579 (let ((values (reverse values)))
580 (build-result (map (compose var-name operand-var) values)
581 (map operand-sym values)
582 (map operand-residual-value values)
586 ;; values := (op ...)
587 ;; effects := ((op . value) ...)
588 (let prune ((old (map referenced? ops)) (values '()) (effects '()))
589 (let lp ((ops* ops) (values values) (effects effects))
592 (let ((new (map referenced? ops)))
593 (if (not (equal? new old))
594 (prune new values '())
596 (map (lambda (op val)
597 (set-operand-residual-value! op val)
599 (map car effects) (map cdr effects))))))
601 (let ((op (car ops*)))
604 (lp (cdr ops*) values effects))
605 ((operand-residual-value op)
606 (lp (cdr ops*) (cons op values) effects))
608 (set-operand-residual-value! op (visit-operand op counter 'value))
609 (lp (cdr ops*) (cons op values) effects))
613 (let ((effect (visit-operand op counter 'effect)))
616 (acons op effect effects))))))))))))
618 (define (small-expression? x limit)
621 (lambda (x res) ; leaf
623 (lambda (x res) ; down
632 (define (extend-env sym op env)
633 (vhash-consq (operand-sym op) op (vhash-consq sym op env)))
636 (env vlist-null) ; vhash of gensym -> <operand>
637 (counter #f) ; inlined call stack
638 (ctx 'value)) ; effect, value, test, operator, or call
641 ((vhash-assq var env) => cdr)
642 (else (error "unbound var" var))))
644 (define (visit exp ctx)
645 (loop exp env counter ctx))
647 (define (for-value exp) (visit exp 'value))
648 (define (for-test exp) (visit exp 'test))
649 (define (for-effect exp) (visit exp 'effect))
650 (define (for-call exp) (visit exp 'call))
651 (define (for-tail exp) (visit exp ctx))
654 (record-effort! counter))
656 (log 'visit ctx (and=> counter effort-counter)
657 (unparse-tree-il exp))
662 ((effect) (make-void #f))
666 ((test) (make-const #f #t))
668 (($ <lexical-ref> _ _ gensym)
669 (log 'begin-copy gensym)
670 (let ((op (lookup gensym)))
673 (log 'lexical-for-effect gensym)
676 ;; Don't propagate copies if we are residualizing a call.
677 (log 'residualize-lexical-call gensym op)
678 (residualize-lexical op))
679 ((var-set? (operand-var op))
680 ;; Assigned lexicals don't copy-propagate.
681 (log 'assigned-var gensym op)
682 (residualize-lexical op))
683 ((not (operand-copyable? op))
684 ;; We already know that this operand is not copyable.
685 (log 'not-copyable gensym op)
686 (residualize-lexical op))
687 ((and=> (operand-constant-value op)
688 (lambda (x) (or (const? x) (void? x) (primitive-ref? x))))
690 (let ((val (operand-constant-value op)))
691 (log 'memoized-constant gensym val)
693 ((visit-operand op counter ctx recursive-effort-limit operand-size-limit)
695 ;; If we end up deciding to residualize this value instead of
696 ;; copying it, save that residualized value.
699 ((not (constant-expression? val))
700 (log 'not-constant gensym op)
701 ;; At this point, ctx is operator, test, or value. A
702 ;; value that is non-constant in one context will be
703 ;; non-constant in the others, so it's safe to record
704 ;; that here, and avoid future visits.
705 (set-operand-copyable?! op #f)
706 (residualize-lexical op ctx val))
709 (primitive-ref? val))
710 ;; Always propagate simple values that cannot lead to
712 (log 'copy-simple gensym val)
713 ;; It could be this constant is the result of folding.
714 ;; If that is the case, cache it. This helps loop
715 ;; unrolling get farther.
718 (log 'memoize-constant gensym val)
719 (set-operand-constant-value! op val)))
721 ((= 1 (var-refcount (operand-var op)))
722 ;; Always propagate values referenced only once.
723 (log 'copy-single gensym val)
725 ;; FIXME: do demand-driven size accounting rather than
728 ;; A pure expression in the operator position. Inline
729 ;; if it's a lambda that's small enough.
730 (if (and (lambda? val)
731 (small-expression? val operator-size-limit))
733 (log 'copy-operator gensym val)
736 (log 'too-big-for-operator gensym val)
737 (residualize-lexical op ctx val))))
739 ;; A pure expression, processed for call or for value.
740 ;; Don't inline lambdas, because they will probably won't
741 ;; fold because we don't know the operator.
742 (if (and (small-expression? val value-size-limit)
743 (not (tree-il-any lambda? val)))
745 (log 'copy-value gensym val)
748 (log 'too-big-or-has-lambda gensym val)
749 (residualize-lexical op ctx val)))))))
751 ;; Visit failed. Either the operand isn't bound, as in
752 ;; lambda formal parameters, or the copy was aborted.
753 (log 'unbound-or-aborted gensym op)
754 (residualize-lexical op)))))
755 (($ <lexical-set> src name gensym exp)
756 (let ((op (lookup gensym)))
757 (if (zero? (var-refcount (operand-var op)))
758 (let ((exp (for-effect exp)))
761 (make-sequence src (list exp (make-void #f)))))
763 (set-operand-residualize?! op #t)
764 (make-lexical-set src name (operand-sym op) (for-value exp))))))
765 (($ <let> src names gensyms vals body)
766 (let* ((vars (map lookup-var gensyms))
767 (new (fresh-gensyms vars))
768 (ops (make-bound-operands vars new vals
769 (lambda (exp counter ctx)
770 (loop exp env counter ctx))))
771 (env (fold extend-env env gensyms ops))
772 (body (loop body env counter ctx)))
775 (for-tail (make-sequence src (append vals (list body)))))
776 ((and (lexical-ref? body)
777 (memq (lexical-ref-gensym body) new))
778 (let ((sym (lexical-ref-gensym body))
779 (pairs (map cons new vals)))
780 ;; (let ((x foo) (y bar) ...) x) => (begin bar ... foo)
784 (append (map cdr (alist-delete sym pairs eq?))
785 (list (assq-ref pairs sym)))))))
787 ;; Only include bindings for which lexical references
788 ;; have been residualized.
789 (prune-bindings ops #f body counter ctx
790 (lambda (names gensyms vals body)
791 (if (null? names) (error "what!" names))
792 (make-let src names gensyms vals body)))))))
793 (($ <letrec> src in-order? names gensyms vals body)
794 ;; Note the difference from the `let' case: here we use letrec*
795 ;; so that the `visit' procedure for the new operands closes over
796 ;; an environment that includes the operands.
797 (letrec* ((visit (lambda (exp counter ctx)
798 (loop exp env* counter ctx)))
799 (vars (map lookup-var gensyms))
800 (new (fresh-gensyms vars))
801 (ops (make-bound-operands vars new vals visit))
802 (env* (fold extend-env env gensyms ops))
803 (body* (visit body counter ctx)))
804 (if (and (const? body*)
805 (every constant-expression? vals))
807 (prune-bindings ops in-order? body* counter ctx
808 (lambda (names gensyms vals body)
809 (make-letrec src in-order?
810 names gensyms vals body))))))
811 (($ <fix> src names gensyms vals body)
812 (letrec* ((visit (lambda (exp counter ctx)
813 (loop exp env* counter ctx)))
814 (vars (map lookup-var gensyms))
815 (new (fresh-gensyms vars))
816 (ops (make-bound-operands vars new vals visit))
817 (env* (fold extend-env env gensyms ops))
818 (body* (visit body counter ctx)))
821 (prune-bindings ops #f body* counter ctx
822 (lambda (names gensyms vals body)
823 (make-fix src names gensyms vals body))))))
824 (($ <let-values> lv-src producer consumer)
825 ;; Peval the producer, then try to inline the consumer into
826 ;; the producer. If that succeeds, peval again. Otherwise
827 ;; reconstruct the let-values, pevaling the consumer.
828 (let ((producer (for-value producer)))
830 (($ <lambda-case> src req #f #f #f () gensyms body #f)
832 ((inline-values producer src req gensyms body)
836 (make-let-values lv-src producer (for-tail consumer)))))
837 (($ <dynwind> src winder body unwinder)
838 (make-dynwind src (for-value winder) (for-tail body)
839 (for-value unwinder)))
840 (($ <dynlet> src fluids vals body)
841 (make-dynlet src (map for-value fluids) (map for-value vals)
843 (($ <dynref> src fluid)
844 (make-dynref src (for-value fluid)))
845 (($ <dynset> src fluid exp)
846 (make-dynset src (for-value fluid) (for-value exp)))
847 (($ <toplevel-ref> src (? effect-free-primitive? name))
848 (if (local-toplevel? name)
850 (let ((exp (resolve-primitives! exp cenv)))
851 (if (primitive-ref? exp)
855 ;; todo: open private local bindings.
857 (($ <module-ref> src module (? effect-free-primitive? name) #f)
858 (let ((module (false-if-exception
859 (resolve-module module #:ensure #f))))
861 (let ((var (module-variable module name)))
862 (if (eq? var (module-variable the-scm-module name))
863 (make-primitive-ref src name)
868 (($ <module-set> src mod name public? exp)
869 (make-module-set src mod name public? (for-value exp)))
870 (($ <toplevel-define> src name exp)
871 (make-toplevel-define src name (for-value exp)))
872 (($ <toplevel-set> src name exp)
873 (make-toplevel-set src name (for-value exp)))
876 ((effect) (make-void #f))
877 ((test) (make-const #f #t))
879 (($ <conditional> src condition subsequent alternate)
880 (let ((condition (for-test condition)))
881 (if (const? condition)
882 (if (const-exp condition)
883 (for-tail subsequent)
884 (for-tail alternate))
885 (make-conditional src condition
886 (for-tail subsequent)
887 (for-tail alternate)))))
888 (($ <application> src
889 ($ <primitive-ref> _ '@call-with-values)
893 ;; No optional or kwargs.
895 _ req #f rest #f () gensyms body #f)))))
896 (for-tail (make-let-values src (make-application src producer '())
899 (($ <application> src orig-proc orig-args)
900 ;; todo: augment the global env with specialized functions
901 (let ((proc (visit orig-proc 'operator)))
903 (($ <primitive-ref> _ (? constructor-primitive? name))
905 ((and (memq ctx '(effect test))
906 (match (cons name orig-args)
911 ('make-prompt-tag ($ <const> _ (? string?))))
914 ;; Some expressions can be folded without visiting the
915 ;; arguments for value.
916 (let ((res (if (eq? ctx 'effect)
918 (make-const #f #t))))
919 (for-tail (make-sequence src (append orig-args (list res))))))
921 (match (cons name (map for-value orig-args))
925 (make-application src (make-primitive-ref #f 'list)
927 (($ <application> src ($ <primitive-ref> _ 'list) elts)
928 (make-application src (make-primitive-ref #f 'list)
930 (_ (make-application src proc
933 ;; FIXME: these for-tail recursions could take
934 ;; place outside an effort counter.
935 (('car ($ <application> src ($ <primitive-ref> _ 'cons) (head tail)))
936 (for-tail (make-sequence src (list tail head))))
937 (('cdr ($ <application> src ($ <primitive-ref> _ 'cons) (head tail)))
938 (for-tail (make-sequence src (list head tail))))
939 (('car ($ <application> src ($ <primitive-ref> _ 'list) (head . tail)))
940 (for-tail (make-sequence src (append tail (list head)))))
941 (('cdr ($ <application> src ($ <primitive-ref> _ 'list) (head . tail)))
942 (for-tail (make-sequence
946 src (make-primitive-ref #f 'list) tail)))))
948 (('car ($ <const> src (head . tail)))
949 (for-tail (make-const src head)))
950 (('cdr ($ <const> src (head . tail)))
951 (for-tail (make-const src tail)))
954 (make-application src proc args))))))
955 (($ <primitive-ref> _ (? effect-free-primitive? name))
956 (let ((args (map for-value orig-args)))
957 (if (every const? args) ; only simple constants
958 (let-values (((success? values)
959 (apply-primitive name (map const-exp args))))
960 (log 'fold success? values exp)
963 ((effect) (make-void #f))
965 ;; Values truncation: only take the first
968 (make-const #f (car values))
969 (make-values src '())))
971 (make-values src (map (cut make-const src <>)
973 (make-application src proc args)))
975 ((and (eq? ctx 'effect) (types-check? name args))
978 (make-application src proc args))))))
980 ($ <lambda-case> _ req opt #f #f inits gensyms body #f))
981 ;; Simple case: no rest, no keyword arguments.
982 ;; todo: handle the more complex cases
983 (let* ((nargs (length orig-args))
985 (nopt (if opt (length opt) 0))
986 (key (source-expression proc)))
988 ((or (< nargs nreq) (> nargs (+ nreq nopt)))
989 ;; An error, or effecting arguments.
990 (make-application src (for-call orig-proc)
991 (map for-value orig-args)))
992 ((or (and=> (find-counter key counter) counter-recursive?)
994 ;; A recursive call, or a lambda in the operator
995 ;; position of the source expression. Process again in
998 ;; In the recursive case, mark intervening counters as
999 ;; recursive, so we can handle a toplevel counter that
1000 ;; recurses mutually with some other procedure.
1001 ;; Otherwise, the next time we see the other procedure,
1002 ;; the effort limit would be clamped to 100.
1004 (let ((found (find-counter key counter)))
1005 (if (and found (counter-recursive? found))
1006 (let lp ((counter counter))
1007 (if (not (eq? counter found))
1009 (set-counter-recursive?! counter #t)
1010 (lp (counter-prev counter)))))))
1012 (log 'inline-recurse key)
1013 (loop (make-let src (append req (or opt '()))
1016 (drop inits (- nargs nreq)))
1020 ;; An integration at the top-level, the first
1021 ;; recursion of a recursive procedure, or a nested
1022 ;; integration of a procedure that hasn't been seen
1024 (log 'inline-begin exp)
1027 (log 'inline-abort exp)
1028 (k (make-application src (for-call orig-proc)
1029 (map for-value orig-args))))
1032 ;; These first two cases will transfer effort
1033 ;; from the current counter into the new
1035 ((find-counter key counter)
1037 (make-recursive-counter recursive-effort-limit
1041 (make-nested-counter abort key counter))
1042 ;; This case opens a new account, effectively
1043 ;; printing money. It should only do so once
1044 ;; for each call site in the source program.
1046 (make-top-counter effort-limit operand-size-limit
1049 (loop (make-let src (append req (or opt '()))
1052 (drop inits (- nargs nreq)))
1054 env new-counter ctx))
1057 ;; The nested inlining attempt succeeded.
1058 ;; Deposit the unspent effort and size back
1059 ;; into the current counter.
1060 (transfer! new-counter counter))
1062 (log 'inline-end result exp)
1065 (make-application src (for-call orig-proc)
1066 (map for-value orig-args))))))
1067 (($ <lambda> src meta body)
1069 ((effect) (make-void #f))
1070 ((test) (make-const #f #t))
1072 (else (record-source-expression!
1074 (make-lambda src meta (for-tail body))))))
1075 (($ <lambda-case> src req opt rest kw inits gensyms body alt)
1076 (let* ((vars (map lookup-var gensyms))
1077 (new (fresh-gensyms vars))
1078 (env (fold extend-env env gensyms
1079 (make-unbound-operands vars new)))
1080 (new-sym (lambda (old)
1081 (operand-sym (cdr (vhash-assq old env))))))
1082 (make-lambda-case src req opt rest
1084 ((aok? (kw name old) ...)
1085 (cons aok? (map list kw name (map new-sym old))))
1087 (map (cut loop <> env counter 'value) inits)
1089 (loop body env counter ctx)
1090 (and alt (for-tail alt)))))
1091 (($ <sequence> src exps)
1092 (let lp ((exps exps) (effects '()))
1099 (reverse (cons (for-tail last) effects)))))
1101 (let ((head (for-effect head)))
1104 (lp (append (sequence-exps head) rest) effects))
1108 (lp rest (cons head effects)))))))))
1109 (($ <prompt> src tag body handler)
1110 (define (singly-used-definition x)
1112 ((and (lexical-ref? x)
1113 ;; Only fetch definitions with single uses.
1114 (= (lexical-refcount (lexical-ref-gensym x)) 1)
1115 (lookup (lexical-ref-gensym x)))
1117 (singly-used-definition (visit-operand x counter 'value 10 10))))
1119 (match (singly-used-definition tag)
1120 (($ <application> _ ($ <primitive-ref> _ 'make-prompt-tag)
1121 (or () ((? constant-expression?))))
1122 ;; There is no way that an <abort> could know the tag
1123 ;; for this <prompt>, so we can elide the <prompt>
1127 (make-prompt src (for-value tag) (for-tail body)
1128 (for-value handler)))))
1129 (($ <abort> src tag args tail)
1130 (make-abort src (for-value tag) (map for-value args)
1131 (for-value tail))))))