Merge remote-tracking branch 'origin/stable-2.0'
[bpt/guile.git] / module / language / tree-il / peval.scm
1 ;;; Tree-IL partial evaluator
2
3 ;; Copyright (C) 2011, 2012 Free Software Foundation, Inc.
4
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.
9 ;;;;
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.
14 ;;;;
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
18
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)
28 #:export (peval))
29
30 ;;;
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.
35 ;;;
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].
39 ;;;
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.
43 ;;;
44 ;;; [0] http://www.cs.utexas.edu/~wcook/tutorial/.
45 ;;;
46
47 ;; First, some helpers.
48 ;;
49 (define-syntax *logging* (identifier-syntax #f))
50
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
53 ;; lines:
54 ;;
55 ;; (define %logging #f)
56 ;; (define-syntax *logging* (identifier-syntax %logging))
57 ;;
58 ;; Then you can change %logging at runtime.
59
60 (define-syntax log
61 (syntax-rules (quote)
62 ((log 'event arg ...)
63 (if (and *logging*
64 (or (eq? *logging* #t)
65 (memq 'event *logging*)))
66 (log* 'event arg ...)))))
67
68 (define (log* event . args)
69 (let ((pp (module-ref (resolve-interface '(ice-9 pretty-print))
70 'pretty-print)))
71 (pp `(log ,event . ,args))
72 (newline)
73 (values)))
74
75 (define-syntax-rule (let/ec k e e* ...)
76 (let ((tag (make-prompt-tag)))
77 (call-with-prompt
78 tag
79 (lambda ()
80 (let ((k (lambda args (apply abort-to-prompt tag args))))
81 e e* ...))
82 (lambda (_ res) res))))
83
84 (define (tree-il-any proc exp)
85 (let/ec k
86 (tree-il-fold (lambda (exp res)
87 (let ((res (proc exp)))
88 (if res (k res) #f)))
89 (lambda (exp res)
90 (let ((res (proc exp)))
91 (if res (k res) #f)))
92 (lambda (exp res) #f)
93 #f exp)))
94
95 (define (vlist-any proc vlist)
96 (let ((len (vlist-length vlist)))
97 (let lp ((i 0))
98 (and (< i len)
99 (or (proc (vlist-ref vlist i))
100 (lp (1+ i)))))))
101
102 (define (singly-valued-expression? exp)
103 (match exp
104 (($ <const>) #t)
105 (($ <lexical-ref>) #t)
106 (($ <void>) #t)
107 (($ <lexical-ref>) #t)
108 (($ <primitive-ref>) #t)
109 (($ <module-ref>) #t)
110 (($ <toplevel-ref>) #t)
111 (($ <primcall> _ (? singly-valued-primitive?)) #t)
112 (($ <primcall> _ 'values (val)) #t)
113 (($ <lambda>) #t)
114 (else #f)))
115
116 (define (truncate-values x)
117 "Discard all but the first value of X."
118 (if (singly-valued-expression? x)
119 x
120 (make-primcall (tree-il-src x) 'values (list x))))
121
122 ;; Peval will do a one-pass analysis on the source program to determine
123 ;; the set of assigned lexicals, and to identify unreferenced and
124 ;; singly-referenced lexicals.
125 ;;
126 (define-record-type <var>
127 (make-var name gensym refcount set?)
128 var?
129 (name var-name)
130 (gensym var-gensym)
131 (refcount var-refcount set-var-refcount!)
132 (set? var-set? set-var-set?!))
133
134 (define* (build-var-table exp #:optional (table vlist-null))
135 (tree-il-fold
136 (lambda (exp res)
137 (match exp
138 (($ <lexical-ref> src name gensym)
139 (let ((var (cdr (vhash-assq gensym res))))
140 (set-var-refcount! var (1+ (var-refcount var)))
141 res))
142 (_ res)))
143 (lambda (exp res)
144 (match exp
145 (($ <lambda-case> src req opt rest kw init gensyms body alt)
146 (fold (lambda (name sym res)
147 (vhash-consq sym (make-var name sym 0 #f) res))
148 res
149 (append req (or opt '()) (if rest (list rest) '())
150 (match kw
151 ((aok? (kw name sym) ...) name)
152 (_ '())))
153 gensyms))
154 (($ <let> src names gensyms vals body)
155 (fold (lambda (name sym res)
156 (vhash-consq sym (make-var name sym 0 #f) res))
157 res names gensyms))
158 (($ <letrec> src in-order? names gensyms vals body)
159 (fold (lambda (name sym res)
160 (vhash-consq sym (make-var name sym 0 #f) res))
161 res names gensyms))
162 (($ <fix> src names gensyms vals body)
163 (fold (lambda (name sym res)
164 (vhash-consq sym (make-var name sym 0 #f) res))
165 res names gensyms))
166 (($ <lexical-set> src name gensym exp)
167 (set-var-set?! (cdr (vhash-assq gensym res)) #t)
168 res)
169 (_ res)))
170 (lambda (exp res) res)
171 table exp))
172
173 ;; Counters are data structures used to limit the effort that peval
174 ;; spends on particular inlining attempts. Each call site in the source
175 ;; program is allocated some amount of effort. If peval exceeds the
176 ;; effort counter while attempting to inline a call site, it aborts the
177 ;; inlining attempt and residualizes a call instead.
178 ;;
179 ;; As there is a fixed number of call sites, that makes `peval' O(N) in
180 ;; the number of call sites in the source program.
181 ;;
182 ;; Counters should limit the size of the residual program as well, but
183 ;; currently this is not implemented.
184 ;;
185 ;; At the top level, before seeing any peval call, there is no counter,
186 ;; because inlining will terminate as there is no recursion. When peval
187 ;; sees a call at the top level, it will make a new counter, allocating
188 ;; it some amount of effort and size.
189 ;;
190 ;; This top-level effort counter effectively "prints money". Within a
191 ;; toplevel counter, no more effort is printed ex nihilo; for a nested
192 ;; inlining attempt to proceed, effort must be transferred from the
193 ;; toplevel counter to the nested counter.
194 ;;
195 ;; Via `data' and `prev', counters form a linked list, terminating in a
196 ;; toplevel counter. In practice `data' will be the a pointer to the
197 ;; source expression of the procedure being inlined.
198 ;;
199 ;; In this way peval can detect a recursive inlining attempt, by walking
200 ;; back on the `prev' links looking for matching `data'. Recursive
201 ;; counters receive a more limited effort allocation, as we don't want
202 ;; to spend all of the effort for a toplevel inlining site on loops.
203 ;; Also, recursive counters don't need a prompt at each inlining site:
204 ;; either the call chain folds entirely, or it will be residualized at
205 ;; its original call.
206 ;;
207 (define-record-type <counter>
208 (%make-counter effort size continuation recursive? data prev)
209 counter?
210 (effort effort-counter)
211 (size size-counter)
212 (continuation counter-continuation)
213 (recursive? counter-recursive? set-counter-recursive?!)
214 (data counter-data)
215 (prev counter-prev))
216
217 (define (abort-counter c)
218 ((counter-continuation c)))
219
220 (define (record-effort! c)
221 (let ((e (effort-counter c)))
222 (if (zero? (variable-ref e))
223 (abort-counter c)
224 (variable-set! e (1- (variable-ref e))))))
225
226 (define (record-size! c)
227 (let ((s (size-counter c)))
228 (if (zero? (variable-ref s))
229 (abort-counter c)
230 (variable-set! s (1- (variable-ref s))))))
231
232 (define (find-counter data counter)
233 (and counter
234 (if (eq? data (counter-data counter))
235 counter
236 (find-counter data (counter-prev counter)))))
237
238 (define* (transfer! from to #:optional
239 (effort (variable-ref (effort-counter from)))
240 (size (variable-ref (size-counter from))))
241 (define (transfer-counter! from-v to-v amount)
242 (let* ((from-balance (variable-ref from-v))
243 (to-balance (variable-ref to-v))
244 (amount (min amount from-balance)))
245 (variable-set! from-v (- from-balance amount))
246 (variable-set! to-v (+ to-balance amount))))
247
248 (transfer-counter! (effort-counter from) (effort-counter to) effort)
249 (transfer-counter! (size-counter from) (size-counter to) size))
250
251 (define (make-top-counter effort-limit size-limit continuation data)
252 (%make-counter (make-variable effort-limit)
253 (make-variable size-limit)
254 continuation
255 #t
256 data
257 #f))
258
259 (define (make-nested-counter continuation data current)
260 (let ((c (%make-counter (make-variable 0)
261 (make-variable 0)
262 continuation
263 #f
264 data
265 current)))
266 (transfer! current c)
267 c))
268
269 (define (make-recursive-counter effort-limit size-limit orig current)
270 (let ((c (%make-counter (make-variable 0)
271 (make-variable 0)
272 (counter-continuation orig)
273 #t
274 (counter-data orig)
275 current)))
276 (transfer! current c effort-limit size-limit)
277 c))
278
279 ;; Operand structures allow bindings to be processed lazily instead of
280 ;; eagerly. By doing so, hopefully we can get process them in a way
281 ;; appropriate to their use contexts. Operands also prevent values from
282 ;; being visited multiple times, wasting effort.
283 ;;
284 ;; TODO: Record value size in operand structure?
285 ;;
286 (define-record-type <operand>
287 (%make-operand var sym visit source visit-count residualize?
288 copyable? residual-value constant-value)
289 operand?
290 (var operand-var)
291 (sym operand-sym)
292 (visit %operand-visit)
293 (source operand-source)
294 (visit-count operand-visit-count set-operand-visit-count!)
295 (residualize? operand-residualize? set-operand-residualize?!)
296 (copyable? operand-copyable? set-operand-copyable?!)
297 (residual-value operand-residual-value %set-operand-residual-value!)
298 (constant-value operand-constant-value set-operand-constant-value!))
299
300 (define* (make-operand var sym #:optional source visit)
301 ;; Bind SYM to VAR, with value SOURCE. Bound operands are considered
302 ;; copyable until we prove otherwise. If we have a source expression,
303 ;; truncate it to one value. Copy propagation does not work on
304 ;; multiply-valued expressions.
305 (let ((source (and=> source truncate-values)))
306 (%make-operand var sym visit source 0 #f (and source #t) #f #f)))
307
308 (define (make-bound-operands vars syms sources visit)
309 (map (lambda (x y z) (make-operand x y z visit)) vars syms sources))
310
311 (define (make-unbound-operands vars syms)
312 (map make-operand vars syms))
313
314 (define (set-operand-residual-value! op val)
315 (%set-operand-residual-value!
316 op
317 (match val
318 (($ <primcall> src 'values (first))
319 ;; The continuation of a residualized binding does not need the
320 ;; introduced `values' node, so undo the effects of truncation.
321 first)
322 (else
323 val))))
324
325 (define* (visit-operand op counter ctx #:optional effort-limit size-limit)
326 ;; Peval is O(N) in call sites of the source program. However,
327 ;; visiting an operand can introduce new call sites. If we visit an
328 ;; operand outside a counter -- i.e., outside an inlining attempt --
329 ;; this can lead to divergence. So, if we are visiting an operand to
330 ;; try to copy it, and there is no counter, make a new one.
331 ;;
332 ;; This will only happen at most as many times as there are lexical
333 ;; references in the source program.
334 (and (zero? (operand-visit-count op))
335 (dynamic-wind
336 (lambda ()
337 (set-operand-visit-count! op (1+ (operand-visit-count op))))
338 (lambda ()
339 (and (operand-source op)
340 (if (or counter (and (not effort-limit) (not size-limit)))
341 ((%operand-visit op) (operand-source op) counter ctx)
342 (let/ec k
343 (define (abort) (k #f))
344 ((%operand-visit op)
345 (operand-source op)
346 (make-top-counter effort-limit size-limit abort op)
347 ctx)))))
348 (lambda ()
349 (set-operand-visit-count! op (1- (operand-visit-count op)))))))
350
351 ;; A helper for constant folding.
352 ;;
353 (define (types-check? primitive-name args)
354 (case primitive-name
355 ((values) #t)
356 ((not pair? null? list? symbol? vector? struct?)
357 (= (length args) 1))
358 ((eq? eqv? equal?)
359 (= (length args) 2))
360 ;; FIXME: add more cases?
361 (else #f)))
362
363 (define* (peval exp #:optional (cenv (current-module)) (env vlist-null)
364 #:key
365 (operator-size-limit 40)
366 (operand-size-limit 20)
367 (value-size-limit 10)
368 (effort-limit 500)
369 (recursive-effort-limit 100))
370 "Partially evaluate EXP in compilation environment CENV, with
371 top-level bindings from ENV and return the resulting expression."
372
373 ;; This is a simple partial evaluator. It effectively performs
374 ;; constant folding, copy propagation, dead code elimination, and
375 ;; inlining.
376
377 ;; TODO:
378 ;;
379 ;; Propagate copies across toplevel bindings, if we can prove the
380 ;; bindings to be immutable.
381 ;;
382 ;; Specialize lambda expressions with invariant arguments.
383
384 (define local-toplevel-env
385 ;; The top-level environment of the module being compiled.
386 (let ()
387 (define (env-folder x env)
388 (match x
389 (($ <toplevel-define> _ name)
390 (vhash-consq name #t env))
391 (($ <seq> _ head tail)
392 (env-folder tail (env-folder head env)))
393 (_ env)))
394 (env-folder exp vlist-null)))
395
396 (define (local-toplevel? name)
397 (vhash-assq name local-toplevel-env))
398
399 ;; gensym -> <var>
400 ;; renamed-term -> original-term
401 ;;
402 (define store (build-var-table exp))
403
404 (define (record-new-temporary! name sym refcount)
405 (set! store (vhash-consq sym (make-var name sym refcount #f) store)))
406
407 (define (lookup-var sym)
408 (let ((v (vhash-assq sym store)))
409 (if v (cdr v) (error "unbound var" sym (vlist->list store)))))
410
411 (define (fresh-gensyms vars)
412 (map (lambda (var)
413 (let ((new (gensym (string-append (symbol->string (var-name var))
414 "-"))))
415 (set! store (vhash-consq new var store))
416 new))
417 vars))
418
419 (define (assigned-lexical? sym)
420 (var-set? (lookup-var sym)))
421
422 (define (lexical-refcount sym)
423 (var-refcount (lookup-var sym)))
424
425 ;; ORIG has been alpha-renamed to NEW. Analyze NEW and record a link
426 ;; from it to ORIG.
427 ;;
428 (define (record-source-expression! orig new)
429 (set! store (vhash-consq new (source-expression orig) store))
430 new)
431
432 ;; Find the source expression corresponding to NEW. Used to detect
433 ;; recursive inlining attempts.
434 ;;
435 (define (source-expression new)
436 (let ((x (vhash-assq new store)))
437 (if x (cdr x) new)))
438
439 (define* (residualize-lexical op #:optional ctx val)
440 (log 'residualize op)
441 (set-operand-residualize?! op #t)
442 (if (eq? ctx 'value)
443 (set-operand-residual-value! op val))
444 (make-lexical-ref #f (var-name (operand-var op)) (operand-sym op)))
445
446 (define (fold-constants src name args ctx)
447 (define (apply-primitive name args)
448 ;; todo: further optimize commutative primitives
449 (catch #t
450 (lambda ()
451 (call-with-values
452 (lambda ()
453 (apply (module-ref the-scm-module name) args))
454 (lambda results
455 (values #t results))))
456 (lambda _
457 (values #f '()))))
458 (define (make-values src values)
459 (match values
460 ((single) single) ; 1 value
461 ((_ ...) ; 0, or 2 or more values
462 (make-primcall src 'values values))))
463 (define (residualize-call)
464 (make-primcall src name args))
465 (cond
466 ((every const? args)
467 (let-values (((success? values)
468 (apply-primitive name (map const-exp args))))
469 (log 'fold success? values name args)
470 (if success?
471 (case ctx
472 ((effect) (make-void src))
473 ((test)
474 ;; Values truncation: only take the first
475 ;; value.
476 (if (pair? values)
477 (make-const src (car values))
478 (make-values src '())))
479 (else
480 (make-values src (map (cut make-const src <>) values))))
481 (residualize-call))))
482 ((and (eq? ctx 'effect) (types-check? name args))
483 (make-void #f))
484 (else
485 (residualize-call))))
486
487 (define (inline-values exp src names gensyms body)
488 (let loop ((exp exp))
489 (match exp
490 ;; Some expression types are always singly-valued.
491 ((or ($ <const>)
492 ($ <void>)
493 ($ <lambda>)
494 ($ <lexical-ref>)
495 ($ <toplevel-ref>)
496 ($ <module-ref>)
497 ($ <primitive-ref>)
498 ($ <dynref>)
499 ($ <lexical-set>) ; FIXME: these set! expressions
500 ($ <toplevel-set>) ; could return zero values in
501 ($ <toplevel-define>) ; the future
502 ($ <module-set>) ;
503 ($ <dynset>)) ;
504 (and (= (length names) 1)
505 (make-let src names gensyms (list exp) body)))
506 (($ <primcall> src (? singly-valued-primitive? name))
507 (and (= (length names) 1)
508 (make-let src names gensyms (list exp) body)))
509
510 ;; Statically-known number of values.
511 (($ <primcall> src 'values vals)
512 (and (= (length names) (length vals))
513 (make-let src names gensyms vals body)))
514
515 ;; Not going to copy code into both branches.
516 (($ <conditional>) #f)
517
518 ;; Bail on other applications.
519 (($ <call>) #f)
520 (($ <primcall>) #f)
521
522 ;; Bail on prompt and abort.
523 (($ <prompt>) #f)
524 (($ <abort>) #f)
525
526 ;; Propagate to tail positions.
527 (($ <let> src names gensyms vals body)
528 (let ((body (loop body)))
529 (and body
530 (make-let src names gensyms vals body))))
531 (($ <letrec> src in-order? names gensyms vals body)
532 (let ((body (loop body)))
533 (and body
534 (make-letrec src in-order? names gensyms vals body))))
535 (($ <fix> src names gensyms vals body)
536 (let ((body (loop body)))
537 (and body
538 (make-fix src names gensyms vals body))))
539 (($ <let-values> src exp
540 ($ <lambda-case> src2 req opt rest kw inits gensyms body #f))
541 (let ((body (loop body)))
542 (and body
543 (make-let-values src exp
544 (make-lambda-case src2 req opt rest kw
545 inits gensyms body #f)))))
546 (($ <dynwind> src winder pre body post unwinder)
547 (let ((body (loop body)))
548 (and body
549 (make-dynwind src winder pre body post unwinder))))
550 (($ <dynlet> src fluids vals body)
551 (let ((body (loop body)))
552 (and body
553 (make-dynlet src fluids vals body))))
554 (($ <seq> src head tail)
555 (let ((tail (loop tail)))
556 (and tail (make-seq src head tail)))))))
557
558 (define (constant-expression? x)
559 ;; Return true if X is constant, for the purposes of copying or
560 ;; elision---i.e., if it is known to have no effects, does not
561 ;; allocate storage for a mutable object, and does not access
562 ;; mutable data (like `car' or toplevel references).
563 (let loop ((x x))
564 (match x
565 (($ <void>) #t)
566 (($ <const>) #t)
567 (($ <lambda>) #t)
568 (($ <lambda-case> _ req opt rest kw inits syms body alternate)
569 (and (not (any assigned-lexical? syms))
570 (every loop inits) (loop body)
571 (or (not alternate) (loop alternate))))
572 (($ <lexical-ref> _ _ gensym)
573 (not (assigned-lexical? gensym)))
574 (($ <primitive-ref>) #t)
575 (($ <conditional> _ condition subsequent alternate)
576 (and (loop condition) (loop subsequent) (loop alternate)))
577 (($ <primcall> _ 'values exps)
578 (and (not (null? exps))
579 (every loop exps)))
580 (($ <primcall> _ name args)
581 (and (effect-free-primitive? name)
582 (not (constructor-primitive? name))
583 (types-check? name args)
584 (if (accessor-primitive? name)
585 (every const? args)
586 (every loop args))))
587 (($ <call> _ ($ <lambda> _ _ body) args)
588 (and (loop body) (every loop args)))
589 (($ <seq> _ head tail)
590 (and (loop head) (loop tail)))
591 (($ <let> _ _ syms vals body)
592 (and (not (any assigned-lexical? syms))
593 (every loop vals) (loop body)))
594 (($ <letrec> _ _ _ syms vals body)
595 (and (not (any assigned-lexical? syms))
596 (every loop vals) (loop body)))
597 (($ <fix> _ _ _ vals body)
598 (and (every loop vals) (loop body)))
599 (($ <let-values> _ exp body)
600 (and (loop exp) (loop body)))
601 (($ <prompt> _ tag body handler)
602 (and (loop tag) (loop body) (loop handler)))
603 (_ #f))))
604
605 (define (prune-bindings ops in-order? body counter ctx build-result)
606 ;; This helper handles both `let' and `letrec'/`fix'. In the latter
607 ;; cases we need to make sure that if referenced binding A needs
608 ;; as-yet-unreferenced binding B, that B is processed for value.
609 ;; Likewise if C, when processed for effect, needs otherwise
610 ;; unreferenced D, then D needs to be processed for value too.
611 ;;
612 (define (referenced? op)
613 ;; When we visit lambdas in operator context, we just copy them,
614 ;; as we will process their body later. However this does have
615 ;; the problem that any free var referenced by the lambda is not
616 ;; marked as needing residualization. Here we hack around this
617 ;; and treat all bindings as referenced if we are in operator
618 ;; context.
619 (or (eq? ctx 'operator) (operand-residualize? op)))
620
621 ;; values := (op ...)
622 ;; effects := (op ...)
623 (define (residualize values effects)
624 ;; Note, values and effects are reversed.
625 (cond
626 (in-order?
627 (let ((values (filter operand-residual-value ops)))
628 (if (null? values)
629 body
630 (build-result (map (compose var-name operand-var) values)
631 (map operand-sym values)
632 (map operand-residual-value values)
633 body))))
634 (else
635 (let ((body
636 (if (null? effects)
637 body
638 (let ((effect-vals (map operand-residual-value effects)))
639 (list->seq #f (reverse (cons body effect-vals)))))))
640 (if (null? values)
641 body
642 (let ((values (reverse values)))
643 (build-result (map (compose var-name operand-var) values)
644 (map operand-sym values)
645 (map operand-residual-value values)
646 body)))))))
647
648 ;; old := (bool ...)
649 ;; values := (op ...)
650 ;; effects := ((op . value) ...)
651 (let prune ((old (map referenced? ops)) (values '()) (effects '()))
652 (let lp ((ops* ops) (values values) (effects effects))
653 (cond
654 ((null? ops*)
655 (let ((new (map referenced? ops)))
656 (if (not (equal? new old))
657 (prune new values '())
658 (residualize values
659 (map (lambda (op val)
660 (set-operand-residual-value! op val)
661 op)
662 (map car effects) (map cdr effects))))))
663 (else
664 (let ((op (car ops*)))
665 (cond
666 ((memq op values)
667 (lp (cdr ops*) values effects))
668 ((operand-residual-value op)
669 (lp (cdr ops*) (cons op values) effects))
670 ((referenced? op)
671 (set-operand-residual-value! op (visit-operand op counter 'value))
672 (lp (cdr ops*) (cons op values) effects))
673 (else
674 (lp (cdr ops*)
675 values
676 (let ((effect (visit-operand op counter 'effect)))
677 (if (void? effect)
678 effects
679 (acons op effect effects))))))))))))
680
681 (define (small-expression? x limit)
682 (let/ec k
683 (tree-il-fold
684 (lambda (x res) ; leaf
685 (1+ res))
686 (lambda (x res) ; down
687 (1+ res))
688 (lambda (x res) ; up
689 (if (< res limit)
690 res
691 (k #f)))
692 0 x)
693 #t))
694
695 (define (extend-env sym op env)
696 (vhash-consq (operand-sym op) op (vhash-consq sym op env)))
697
698 (let loop ((exp exp)
699 (env vlist-null) ; vhash of gensym -> <operand>
700 (counter #f) ; inlined call stack
701 (ctx 'values)) ; effect, value, values, test, operator, or call
702 (define (lookup var)
703 (cond
704 ((vhash-assq var env) => cdr)
705 (else (error "unbound var" var))))
706
707 (define (visit exp ctx)
708 (loop exp env counter ctx))
709
710 (define (for-value exp) (visit exp 'value))
711 (define (for-values exp) (visit exp 'values))
712 (define (for-test exp) (visit exp 'test))
713 (define (for-effect exp) (visit exp 'effect))
714 (define (for-call exp) (visit exp 'call))
715 (define (for-tail exp) (visit exp ctx))
716
717 (if counter
718 (record-effort! counter))
719
720 (log 'visit ctx (and=> counter effort-counter)
721 (unparse-tree-il exp))
722
723 (match exp
724 (($ <const>)
725 (case ctx
726 ((effect) (make-void #f))
727 (else exp)))
728 (($ <void>)
729 (case ctx
730 ((test) (make-const #f #t))
731 (else exp)))
732 (($ <lexical-ref> _ _ gensym)
733 (log 'begin-copy gensym)
734 (let ((op (lookup gensym)))
735 (cond
736 ((eq? ctx 'effect)
737 (log 'lexical-for-effect gensym)
738 (make-void #f))
739 ((eq? ctx 'call)
740 ;; Don't propagate copies if we are residualizing a call.
741 (log 'residualize-lexical-call gensym op)
742 (residualize-lexical op))
743 ((var-set? (operand-var op))
744 ;; Assigned lexicals don't copy-propagate.
745 (log 'assigned-var gensym op)
746 (residualize-lexical op))
747 ((not (operand-copyable? op))
748 ;; We already know that this operand is not copyable.
749 (log 'not-copyable gensym op)
750 (residualize-lexical op))
751 ((and=> (operand-constant-value op)
752 (lambda (x) (or (const? x) (void? x) (primitive-ref? x))))
753 ;; A cache hit.
754 (let ((val (operand-constant-value op)))
755 (log 'memoized-constant gensym val)
756 (for-tail val)))
757 ((visit-operand op counter (if (eq? ctx 'values) 'value ctx)
758 recursive-effort-limit operand-size-limit)
759 =>
760 ;; If we end up deciding to residualize this value instead of
761 ;; copying it, save that residualized value.
762 (lambda (val)
763 (cond
764 ((not (constant-expression? val))
765 (log 'not-constant gensym op)
766 ;; At this point, ctx is operator, test, or value. A
767 ;; value that is non-constant in one context will be
768 ;; non-constant in the others, so it's safe to record
769 ;; that here, and avoid future visits.
770 (set-operand-copyable?! op #f)
771 (residualize-lexical op ctx val))
772 ((or (const? val)
773 (void? val)
774 (primitive-ref? val))
775 ;; Always propagate simple values that cannot lead to
776 ;; code bloat.
777 (log 'copy-simple gensym val)
778 ;; It could be this constant is the result of folding.
779 ;; If that is the case, cache it. This helps loop
780 ;; unrolling get farther.
781 (if (or (eq? ctx 'value) (eq? ctx 'values))
782 (begin
783 (log 'memoize-constant gensym val)
784 (set-operand-constant-value! op val)))
785 val)
786 ((= 1 (var-refcount (operand-var op)))
787 ;; Always propagate values referenced only once.
788 (log 'copy-single gensym val)
789 val)
790 ;; FIXME: do demand-driven size accounting rather than
791 ;; these heuristics.
792 ((eq? ctx 'operator)
793 ;; A pure expression in the operator position. Inline
794 ;; if it's a lambda that's small enough.
795 (if (and (lambda? val)
796 (small-expression? val operator-size-limit))
797 (begin
798 (log 'copy-operator gensym val)
799 val)
800 (begin
801 (log 'too-big-for-operator gensym val)
802 (residualize-lexical op ctx val))))
803 (else
804 ;; A pure expression, processed for call or for value.
805 ;; Don't inline lambdas, because they will probably won't
806 ;; fold because we don't know the operator.
807 (if (and (small-expression? val value-size-limit)
808 (not (tree-il-any lambda? val)))
809 (begin
810 (log 'copy-value gensym val)
811 val)
812 (begin
813 (log 'too-big-or-has-lambda gensym val)
814 (residualize-lexical op ctx val)))))))
815 (else
816 ;; Visit failed. Either the operand isn't bound, as in
817 ;; lambda formal parameters, or the copy was aborted.
818 (log 'unbound-or-aborted gensym op)
819 (residualize-lexical op)))))
820 (($ <lexical-set> src name gensym exp)
821 (let ((op (lookup gensym)))
822 (if (zero? (var-refcount (operand-var op)))
823 (let ((exp (for-effect exp)))
824 (if (void? exp)
825 exp
826 (make-seq src exp (make-void #f))))
827 (begin
828 (set-operand-residualize?! op #t)
829 (make-lexical-set src name (operand-sym op) (for-value exp))))))
830 (($ <let> src names gensyms vals body)
831 (let* ((vars (map lookup-var gensyms))
832 (new (fresh-gensyms vars))
833 (ops (make-bound-operands vars new vals
834 (lambda (exp counter ctx)
835 (loop exp env counter ctx))))
836 (env (fold extend-env env gensyms ops))
837 (body (loop body env counter ctx)))
838 (cond
839 ((const? body)
840 (for-tail (list->seq src (append vals (list body)))))
841 ((and (lexical-ref? body)
842 (memq (lexical-ref-gensym body) new))
843 (let ((sym (lexical-ref-gensym body))
844 (pairs (map cons new vals)))
845 ;; (let ((x foo) (y bar) ...) x) => (begin bar ... foo)
846 (for-tail
847 (list->seq
848 src
849 (append (map cdr (alist-delete sym pairs eq?))
850 (list (assq-ref pairs sym)))))))
851 (else
852 ;; Only include bindings for which lexical references
853 ;; have been residualized.
854 (prune-bindings ops #f body counter ctx
855 (lambda (names gensyms vals body)
856 (if (null? names) (error "what!" names))
857 (make-let src names gensyms vals body)))))))
858 (($ <letrec> src in-order? names gensyms vals body)
859 ;; Note the difference from the `let' case: here we use letrec*
860 ;; so that the `visit' procedure for the new operands closes over
861 ;; an environment that includes the operands.
862 (letrec* ((visit (lambda (exp counter ctx)
863 (loop exp env* counter ctx)))
864 (vars (map lookup-var gensyms))
865 (new (fresh-gensyms vars))
866 (ops (make-bound-operands vars new vals visit))
867 (env* (fold extend-env env gensyms ops))
868 (body* (visit body counter ctx)))
869 (if (and (const? body*) (every constant-expression? vals))
870 ;; We may have folded a loop completely, even though there
871 ;; might be cyclical references between the bound values.
872 ;; Handle this degenerate case specially.
873 body*
874 (prune-bindings ops in-order? body* counter ctx
875 (lambda (names gensyms vals body)
876 (make-letrec src in-order?
877 names gensyms vals body))))))
878 (($ <fix> src names gensyms vals body)
879 (letrec* ((visit (lambda (exp counter ctx)
880 (loop exp env* counter ctx)))
881 (vars (map lookup-var gensyms))
882 (new (fresh-gensyms vars))
883 (ops (make-bound-operands vars new vals visit))
884 (env* (fold extend-env env gensyms ops))
885 (body* (visit body counter ctx)))
886 (if (const? body*)
887 body*
888 (prune-bindings ops #f body* counter ctx
889 (lambda (names gensyms vals body)
890 (make-fix src names gensyms vals body))))))
891 (($ <let-values> lv-src producer consumer)
892 ;; Peval the producer, then try to inline the consumer into
893 ;; the producer. If that succeeds, peval again. Otherwise
894 ;; reconstruct the let-values, pevaling the consumer.
895 (let ((producer (for-values producer)))
896 (or (match consumer
897 (($ <lambda-case> src req #f #f #f () gensyms body #f)
898 (cond
899 ((inline-values producer src req gensyms body)
900 => for-tail)
901 (else #f)))
902 (_ #f))
903 (make-let-values lv-src producer (for-tail consumer)))))
904 (($ <dynwind> src winder pre body post unwinder)
905 (make-dynwind src (for-value winder) (for-effect pre)
906 (for-tail body)
907 (for-effect post) (for-value unwinder)))
908 (($ <dynlet> src fluids vals body)
909 (make-dynlet src (map for-value fluids) (map for-value vals)
910 (for-tail body)))
911 (($ <dynref> src fluid)
912 (make-dynref src (for-value fluid)))
913 (($ <dynset> src fluid exp)
914 (make-dynset src (for-value fluid) (for-value exp)))
915 (($ <toplevel-ref> src (? effect-free-primitive? name))
916 exp)
917 (($ <toplevel-ref>)
918 ;; todo: open private local bindings.
919 exp)
920 (($ <module-ref> src module (? effect-free-primitive? name) #f)
921 (let ((module (false-if-exception
922 (resolve-module module #:ensure #f))))
923 (if (module? module)
924 (let ((var (module-variable module name)))
925 (if (eq? var (module-variable the-scm-module name))
926 (make-primitive-ref src name)
927 exp))
928 exp)))
929 (($ <module-ref>)
930 exp)
931 (($ <module-set> src mod name public? exp)
932 (make-module-set src mod name public? (for-value exp)))
933 (($ <toplevel-define> src name exp)
934 (make-toplevel-define src name (for-value exp)))
935 (($ <toplevel-set> src name exp)
936 (make-toplevel-set src name (for-value exp)))
937 (($ <primitive-ref>)
938 (case ctx
939 ((effect) (make-void #f))
940 ((test) (make-const #f #t))
941 (else exp)))
942 (($ <conditional> src condition subsequent alternate)
943 (let ((condition (for-test condition)))
944 (if (const? condition)
945 (if (const-exp condition)
946 (for-tail subsequent)
947 (for-tail alternate))
948 (make-conditional src condition
949 (for-tail subsequent)
950 (for-tail alternate)))))
951 (($ <primcall> src '@call-with-values
952 (producer
953 ($ <lambda> _ _
954 (and consumer
955 ;; No optional or kwargs.
956 ($ <lambda-case>
957 _ req #f rest #f () gensyms body #f)))))
958 (for-tail (make-let-values src (make-call src producer '())
959 consumer)))
960 (($ <primcall> src 'dynamic-wind (w thunk u))
961 (for-tail
962 (cond
963 ((not (constant-expression? w))
964 (cond
965 ((not (constant-expression? u))
966 (let ((w-sym (gensym "w ")) (u-sym (gensym "u ")))
967 (record-new-temporary! 'w w-sym 2)
968 (record-new-temporary! 'u u-sym 2)
969 (make-let src '(w u) (list w-sym u-sym) (list w u)
970 (make-dynwind
971 src
972 (make-lexical-ref #f 'w w-sym)
973 (make-call #f (make-lexical-ref #f 'w w-sym) '())
974 (make-call #f thunk '())
975 (make-call #f (make-lexical-ref #f 'u u-sym) '())
976 (make-lexical-ref #f 'u u-sym)))))
977 (else
978 (let ((w-sym (gensym "w ")))
979 (record-new-temporary! 'w w-sym 2)
980 (make-let src '(w) (list w-sym) (list w)
981 (make-dynwind
982 src
983 (make-lexical-ref #f 'w w-sym)
984 (make-call #f (make-lexical-ref #f 'w w-sym) '())
985 (make-call #f thunk '())
986 (make-call #f u '())
987 u))))))
988 ((not (constant-expression? u))
989 (let ((u-sym (gensym "u ")))
990 (record-new-temporary! 'u u-sym 2)
991 (make-let src '(u) (list u-sym) (list u)
992 (make-dynwind
993 src
994 w
995 (make-call #f w '())
996 (make-call #f thunk '())
997 (make-call #f (make-lexical-ref #f 'u u-sym) '())
998 (make-lexical-ref #f 'u u-sym)))))
999 (else
1000 (make-dynwind src w (make-call #f w '()) (make-call #f thunk '())
1001 (make-call #f u '()) u)))))
1002
1003 (($ <primcall> src 'values exps)
1004 (cond
1005 ((null? exps)
1006 (if (eq? ctx 'effect)
1007 (make-void #f)
1008 exp))
1009 (else
1010 (let ((vals (map for-value exps)))
1011 (if (and (case ctx
1012 ((value test effect) #t)
1013 (else (null? (cdr vals))))
1014 (every singly-valued-expression? vals))
1015 (for-tail (list->seq src (append (cdr vals) (list (car vals)))))
1016 (make-primcall src 'values vals))))))
1017
1018 (($ <primcall> src (? constructor-primitive? name) args)
1019 (cond
1020 ((and (memq ctx '(effect test))
1021 (match (cons name args)
1022 ((or ('cons _ _)
1023 ('list . _)
1024 ('vector . _)
1025 ('make-prompt-tag)
1026 ('make-prompt-tag ($ <const> _ (? string?))))
1027 #t)
1028 (_ #f)))
1029 ;; Some expressions can be folded without visiting the
1030 ;; arguments for value.
1031 (let ((res (if (eq? ctx 'effect)
1032 (make-void #f)
1033 (make-const #f #t))))
1034 (for-tail (list->seq src (append args (list res))))))
1035 (else
1036 (match (cons name (map for-value args))
1037 (('cons x ($ <const> _ (? (cut eq? <> '()))))
1038 (make-primcall src 'list (list x)))
1039 (('cons x ($ <primcall> _ 'list elts))
1040 (make-primcall src 'list (cons x elts)))
1041 ((name . args)
1042 (make-primcall src name args))))))
1043
1044 (($ <primcall> src (? accessor-primitive? name) args)
1045 (match (cons name (map for-value args))
1046 ;; FIXME: these for-tail recursions could take place outside
1047 ;; an effort counter.
1048 (('car ($ <primcall> src 'cons (head tail)))
1049 (for-tail (make-seq src tail head)))
1050 (('cdr ($ <primcall> src 'cons (head tail)))
1051 (for-tail (make-seq src head tail)))
1052 (('car ($ <primcall> src 'list (head . tail)))
1053 (for-tail (list->seq src (append tail (list head)))))
1054 (('cdr ($ <primcall> src 'list (head . tail)))
1055 (for-tail (make-seq src head (make-primcall #f 'list tail))))
1056
1057 (('car ($ <const> src (head . tail)))
1058 (for-tail (make-const src head)))
1059 (('cdr ($ <const> src (head . tail)))
1060 (for-tail (make-const src tail)))
1061 (((or 'memq 'memv) k ($ <const> _ (elts ...)))
1062 ;; FIXME: factor
1063 (case ctx
1064 ((effect)
1065 (for-tail
1066 (make-seq src k (make-void #f))))
1067 ((test)
1068 (cond
1069 ((const? k)
1070 ;; A shortcut. The `else' case would handle it, but
1071 ;; this way is faster.
1072 (let ((member (case name ((memq) memq) ((memv) memv))))
1073 (make-const #f (and (member (const-exp k) elts) #t))))
1074 ((null? elts)
1075 (for-tail
1076 (make-seq src k (make-const #f #f))))
1077 (else
1078 (let ((t (gensym "t "))
1079 (eq (if (eq? name 'memq) 'eq? 'eqv?)))
1080 (record-new-temporary! 't t (length elts))
1081 (for-tail
1082 (make-let
1083 src (list 't) (list t) (list k)
1084 (let lp ((elts elts))
1085 (define test
1086 (make-primcall #f eq
1087 (list (make-lexical-ref #f 't t)
1088 (make-const #f (car elts)))))
1089 (if (null? (cdr elts))
1090 test
1091 (make-conditional src test
1092 (make-const #f #t)
1093 (lp (cdr elts)))))))))))
1094 (else
1095 (cond
1096 ((const? k)
1097 (let ((member (case name ((memq) memq) ((memv) memv))))
1098 (make-const #f (member (const-exp k) elts))))
1099 ((null? elts)
1100 (for-tail (make-seq src k (make-const #f #f))))
1101 (else
1102 (make-primcall src name (list k (make-const #f elts))))))))
1103 ((name . args)
1104 (fold-constants src name args ctx))))
1105
1106 (($ <primcall> src (? effect-free-primitive? name) args)
1107 (fold-constants src name (map for-value args) ctx))
1108
1109 (($ <primcall> src name args)
1110 (make-primcall src name (map for-value args)))
1111
1112 (($ <call> src orig-proc orig-args)
1113 ;; todo: augment the global env with specialized functions
1114 (let ((proc (visit orig-proc 'operator)))
1115 (match proc
1116 (($ <primitive-ref> _ name)
1117 (for-tail (make-primcall src name orig-args)))
1118 (($ <lambda> _ _
1119 ($ <lambda-case> _ req opt #f #f inits gensyms body #f))
1120 ;; Simple case: no rest, no keyword arguments.
1121 ;; todo: handle the more complex cases
1122 (let* ((nargs (length orig-args))
1123 (nreq (length req))
1124 (nopt (if opt (length opt) 0))
1125 (key (source-expression proc)))
1126 (cond
1127 ((or (< nargs nreq) (> nargs (+ nreq nopt)))
1128 ;; An error, or effecting arguments.
1129 (make-call src (for-call orig-proc) (map for-value orig-args)))
1130 ((or (and=> (find-counter key counter) counter-recursive?)
1131 (lambda? orig-proc))
1132 ;; A recursive call, or a lambda in the operator
1133 ;; position of the source expression. Process again in
1134 ;; tail context.
1135 ;;
1136 ;; In the recursive case, mark intervening counters as
1137 ;; recursive, so we can handle a toplevel counter that
1138 ;; recurses mutually with some other procedure.
1139 ;; Otherwise, the next time we see the other procedure,
1140 ;; the effort limit would be clamped to 100.
1141 ;;
1142 (let ((found (find-counter key counter)))
1143 (if (and found (counter-recursive? found))
1144 (let lp ((counter counter))
1145 (if (not (eq? counter found))
1146 (begin
1147 (set-counter-recursive?! counter #t)
1148 (lp (counter-prev counter)))))))
1149
1150 (log 'inline-recurse key)
1151 (loop (make-let src (append req (or opt '()))
1152 gensyms
1153 (append orig-args
1154 (drop inits (- nargs nreq)))
1155 body)
1156 env counter ctx))
1157 (else
1158 ;; An integration at the top-level, the first
1159 ;; recursion of a recursive procedure, or a nested
1160 ;; integration of a procedure that hasn't been seen
1161 ;; yet.
1162 (log 'inline-begin exp)
1163 (let/ec k
1164 (define (abort)
1165 (log 'inline-abort exp)
1166 (k (make-call src (for-call orig-proc)
1167 (map for-value orig-args))))
1168 (define new-counter
1169 (cond
1170 ;; These first two cases will transfer effort
1171 ;; from the current counter into the new
1172 ;; counter.
1173 ((find-counter key counter)
1174 => (lambda (prev)
1175 (make-recursive-counter recursive-effort-limit
1176 operand-size-limit
1177 prev counter)))
1178 (counter
1179 (make-nested-counter abort key counter))
1180 ;; This case opens a new account, effectively
1181 ;; printing money. It should only do so once
1182 ;; for each call site in the source program.
1183 (else
1184 (make-top-counter effort-limit operand-size-limit
1185 abort key))))
1186 (define result
1187 (loop (make-let src (append req (or opt '()))
1188 gensyms
1189 (append orig-args
1190 (drop inits (- nargs nreq)))
1191 body)
1192 env new-counter ctx))
1193
1194 (if counter
1195 ;; The nested inlining attempt succeeded.
1196 ;; Deposit the unspent effort and size back
1197 ;; into the current counter.
1198 (transfer! new-counter counter))
1199
1200 (log 'inline-end result exp)
1201 result)))))
1202 (_
1203 (make-call src (for-call orig-proc) (map for-value orig-args))))))
1204 (($ <lambda> src meta body)
1205 (case ctx
1206 ((effect) (make-void #f))
1207 ((test) (make-const #f #t))
1208 ((operator) exp)
1209 (else (record-source-expression!
1210 exp
1211 (make-lambda src meta (for-values body))))))
1212 (($ <lambda-case> src req opt rest kw inits gensyms body alt)
1213 (let* ((vars (map lookup-var gensyms))
1214 (new (fresh-gensyms vars))
1215 (env (fold extend-env env gensyms
1216 (make-unbound-operands vars new)))
1217 (new-sym (lambda (old)
1218 (operand-sym (cdr (vhash-assq old env))))))
1219 (make-lambda-case src req opt rest
1220 (match kw
1221 ((aok? (kw name old) ...)
1222 (cons aok? (map list kw name (map new-sym old))))
1223 (_ #f))
1224 (map (cut loop <> env counter 'value) inits)
1225 new
1226 (loop body env counter ctx)
1227 (and alt (for-tail alt)))))
1228 (($ <seq> src head tail)
1229 (let ((head (for-effect head))
1230 (tail (for-tail tail)))
1231 (if (void? head)
1232 tail
1233 (make-seq src
1234 (if (and (seq? head)
1235 (void? (seq-tail head)))
1236 (seq-head head)
1237 head)
1238 tail))))
1239 (($ <prompt> src tag body handler)
1240 (define (singly-used-definition x)
1241 (cond
1242 ((and (lexical-ref? x)
1243 ;; Only fetch definitions with single uses.
1244 (= (lexical-refcount (lexical-ref-gensym x)) 1)
1245 (lookup (lexical-ref-gensym x)))
1246 => (lambda (x)
1247 (singly-used-definition (visit-operand x counter 'value 10 10))))
1248 (else x)))
1249 (match (singly-used-definition tag)
1250 (($ <primcall> _ 'make-prompt-tag (or () ((? constant-expression?))))
1251 ;; There is no way that an <abort> could know the tag
1252 ;; for this <prompt>, so we can elide the <prompt>
1253 ;; entirely.
1254 (for-tail body))
1255 (_
1256 (make-prompt src (for-value tag) (for-tail body)
1257 (for-value handler)))))
1258 (($ <abort> src tag args tail)
1259 (make-abort src (for-value tag) (map for-value args)
1260 (for-value tail))))))