Closure conversion eliminates self-references introduced by fixpoint
[bpt/guile.git] / module / language / cps / closure-conversion.scm
1 ;;; Continuation-passing style (CPS) intermediate language (IL)
2
3 ;; Copyright (C) 2013, 2014 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 ;;; Commentary:
20 ;;;
21 ;;; This pass converts a CPS term in such a way that no function has any
22 ;;; free variables. Instead, closures are built explicitly with
23 ;;; make-closure primcalls, and free variables are referenced through
24 ;;; the closure.
25 ;;;
26 ;;; Closure conversion also removes any $letrec forms that contification
27 ;;; did not handle. See (language cps) for a further discussion of
28 ;;; $letrec.
29 ;;;
30 ;;; Code:
31
32 (define-module (language cps closure-conversion)
33 #:use-module (ice-9 match)
34 #:use-module ((srfi srfi-1) #:select (fold
35 lset-union lset-difference
36 list-index))
37 #:use-module (srfi srfi-9)
38 #:use-module (srfi srfi-26)
39 #:use-module (language cps)
40 #:use-module (language cps dfg)
41 #:export (convert-closures))
42
43 ;; free := var ...
44
45 (define (analyze-closures exp dfg)
46 "Compute the set of free variables for all $fun instances in
47 @var{exp}."
48 (let ((bound-vars (make-hash-table))
49 (free-vars (make-hash-table))
50 (named-funs (make-hash-table))
51 (well-known-vars (make-bitvector (var-counter) #t)))
52 (define (add-named-fun! var cont)
53 (hashq-set! named-funs var cont)
54 (match cont
55 (($ $cont label ($ $kfun src meta self))
56 (unless (eq? var self)
57 (hashq-set! bound-vars label var)))))
58 (define (clear-well-known! var)
59 (bitvector-set! well-known-vars var #f))
60 (define (compute-well-known-labels)
61 (let ((bv (make-bitvector (label-counter) #f)))
62 (hash-for-each
63 (lambda (var cont)
64 (match cont
65 (($ $cont label ($ $kfun src meta self))
66 (unless (equal? var self)
67 (bitvector-set! bv label
68 (and (bitvector-ref well-known-vars var)
69 (bitvector-ref well-known-vars self)))))))
70 named-funs)
71 bv))
72 (define (union a b)
73 (lset-union eq? a b))
74 (define (difference a b)
75 (lset-difference eq? a b))
76 (define (visit-cont cont bound)
77 (match cont
78 (($ $cont label ($ $kargs names vars body))
79 (visit-term body (append vars bound)))
80 (($ $cont label ($ $kfun src meta self tail clause))
81 (add-named-fun! self cont)
82 (let ((free (if clause
83 (visit-cont clause (list self))
84 '())))
85 (hashq-set! free-vars label free)
86 (difference free bound)))
87 (($ $cont label ($ $kclause arity body alternate))
88 (let ((free (visit-cont body bound)))
89 (if alternate
90 (union (visit-cont alternate bound) free)
91 free)))
92 (($ $cont) '())))
93 (define (visit-term term bound)
94 (match term
95 (($ $letk conts body)
96 (fold (lambda (cont free)
97 (union (visit-cont cont bound) free))
98 (visit-term body bound)
99 conts))
100 (($ $letrec names vars (($ $fun () cont) ...) body)
101 (let ((bound (append vars bound)))
102 (for-each add-named-fun! vars cont)
103 (fold (lambda (cont free)
104 (union (visit-cont cont bound) free))
105 (visit-term body bound)
106 cont)))
107 (($ $continue k src ($ $fun () body))
108 (match (lookup-predecessors k dfg)
109 ((_) (match (lookup-cont k dfg)
110 (($ $kargs (name) (var))
111 (add-named-fun! var body))))
112 (_ #f))
113 (visit-cont body bound))
114 (($ $continue k src exp)
115 (visit-exp exp bound))))
116 (define (visit-exp exp bound)
117 (define (adjoin var free)
118 (if (or (memq var bound) (memq var free))
119 free
120 (cons var free)))
121 (match exp
122 ((or ($ $void) ($ $const) ($ $prim)) '())
123 (($ $call proc args)
124 (for-each clear-well-known! args)
125 (fold adjoin (adjoin proc '()) args))
126 (($ $primcall name args)
127 (for-each clear-well-known! args)
128 (fold adjoin '() args))
129 (($ $values args)
130 (for-each clear-well-known! args)
131 (fold adjoin '() args))
132 (($ $prompt escape? tag handler)
133 (clear-well-known! tag)
134 (adjoin tag '()))))
135
136 (let ((free (visit-cont exp '())))
137 (unless (null? free)
138 (error "Expected no free vars in toplevel thunk" free exp))
139 (values bound-vars free-vars named-funs (compute-well-known-labels)))))
140
141 (define (prune-free-vars free-vars named-funs well-known var-aliases)
142 (define (well-known? label)
143 (bitvector-ref well-known label))
144 (let ((eliminated (make-bitvector (label-counter) #f))
145 (label-aliases (make-vector (label-counter) #f)))
146 (let lp ((label 0))
147 (let ((label (bit-position #t well-known label)))
148 (when label
149 (match (hashq-ref free-vars label)
150 ;; Mark all well-known closures that have no free variables
151 ;; for elimination.
152 (() (bitvector-set! eliminated label #t))
153 ;; Replace well-known closures that have just one free
154 ;; variable by references to that free variable.
155 ((var)
156 (vector-set! label-aliases label var))
157 (_ #f))
158 (lp (1+ label)))))
159 ;; Iterative free variable elimination.
160 (let lp ()
161 (let ((recurse? #f))
162 (define (adjoin elt list)
163 ;; Normally you wouldn't see duplicates in a free variable
164 ;; list, but with aliases that is possible.
165 (if (memq elt list) list (cons elt list)))
166 (define (prune-free closure-label free)
167 (match free
168 (() '())
169 ((var . free)
170 (let lp ((var var) (alias-stack '()))
171 (match (hashq-ref named-funs var)
172 (($ $cont label)
173 (cond
174 ((bitvector-ref eliminated label)
175 (prune-free closure-label free))
176 ((vector-ref label-aliases label)
177 => (lambda (var)
178 (cond
179 ((memq label alias-stack)
180 ;; We have found a set of mutually recursive
181 ;; well-known procedures, each of which only
182 ;; closes over one of the others. Mark them
183 ;; all for elimination.
184 (for-each (lambda (label)
185 (bitvector-set! eliminated label #t)
186 (set! recurse? #t))
187 alias-stack)
188 (prune-free closure-label free))
189 (else
190 (lp var (cons label alias-stack))))))
191 ((eq? closure-label label)
192 ;; Eliminate self-reference.
193 (pk 'hi)
194 (prune-free closure-label free))
195 (else
196 (adjoin var (prune-free closure-label free)))))
197 (_ (adjoin var (prune-free closure-label free))))))))
198 (hash-for-each-handle
199 (lambda (pair)
200 (match pair
201 ((label . ()) #t)
202 ((label . free)
203 (let ((orig-nfree (length free))
204 (free (prune-free label free)))
205 (set-cdr! pair free)
206 ;; If we managed to eliminate one or more free variables
207 ;; from a well-known function, it could be that we can
208 ;; eliminate or alias this function as well.
209 (when (and (well-known? label)
210 (< (length free) orig-nfree))
211 (match free
212 (()
213 (bitvector-set! eliminated label #t)
214 (set! recurse? #t))
215 ((var)
216 (vector-set! label-aliases label var)
217 (set! recurse? #t))
218 (_ #t)))))))
219 free-vars)
220 ;; Iterate to fixed point.
221 (when recurse? (lp))))
222 ;; Populate var-aliases from label-aliases.
223 (hash-for-each (lambda (var cont)
224 (match cont
225 (($ $cont label)
226 (let ((alias (vector-ref label-aliases label)))
227 (when alias
228 (vector-set! var-aliases var alias))))))
229 named-funs)))
230
231 (define (convert-one bound label fun free-vars named-funs well-known aliases)
232 (define (well-known? label)
233 (bitvector-ref well-known label))
234
235 (let ((free (hashq-ref free-vars label))
236 (self-known? (well-known? label))
237 (self (match fun (($ $kfun _ _ self) self))))
238 (define (convert-free-var var k)
239 "Convert one possibly free variable reference to a bound reference.
240
241 If @var{var} is free, it is replaced by a closure reference via a
242 @code{free-ref} primcall, and @var{k} is called with the new var.
243 Otherwise @var{var} is bound, so @var{k} is called with @var{var}."
244 (cond
245 ((list-index (cut eq? <> var) free)
246 => (lambda (free-idx)
247 (match (cons self-known? free)
248 ;; A reference to the one free var of a well-known function.
249 ((#t _) (k self))
250 ;; A reference to one of the two free vars in a well-known
251 ;; function.
252 ((#t _ _)
253 (let-fresh (k*) (var*)
254 (build-cps-term
255 ($letk ((k* ($kargs (var*) (var*) ,(k var*))))
256 ($continue k* #f
257 ($primcall (match free-idx (0 'car) (1 'cdr)) (self)))))))
258 (_
259 (let-fresh (k* kidx) (idx var*)
260 (build-cps-term
261 ($letk ((kidx ($kargs ('idx) (idx)
262 ($letk ((k* ($kargs (var*) (var*) ,(k var*))))
263 ($continue k* #f
264 ($primcall
265 (cond
266 ((not self-known?) 'free-ref)
267 ((<= free-idx #xff) 'vector-ref/immediate)
268 (else 'vector-ref))
269 (self idx)))))))
270 ($continue kidx #f ($const free-idx)))))))))
271 ((eq? var bound) (k self))
272 (else (k var))))
273
274 (define (convert-free-vars vars k)
275 "Convert a number of possibly free references to bound references.
276 @var{k} is called with the bound references, and should return the
277 term."
278 (match vars
279 (() (k '()))
280 ((var . vars)
281 (convert-free-var var
282 (lambda (var)
283 (convert-free-vars vars
284 (lambda (vars)
285 (k (cons var vars)))))))))
286
287 (define (allocate-closure src name var label known? free body)
288 "Allocate a new closure."
289 (match (cons known? free)
290 ((#f . _)
291 (let-fresh (k*) ()
292 (build-cps-term
293 ($letk ((k* ($kargs (name) (var) ,body)))
294 ($continue k* src
295 ($closure label (length free)))))))
296 ((#t)
297 ;; Well-known closure with no free variables; elide the
298 ;; binding entirely.
299 body)
300 ((#t _)
301 ;; Well-known closure with one free variable; the free var is the
302 ;; closure, and no new binding need be made.
303 body)
304 ((#t _ _)
305 ;; Well-known closure with two free variables; the closure is a
306 ;; pair.
307 (let-fresh (kinit kfalse) (false)
308 (build-cps-term
309 ($letk ((kinit ($kargs (name) (var)
310 ,body))
311 (kfalse ($kargs ('false) (false)
312 ($continue kinit src
313 ($primcall 'cons (false false))))))
314 ($continue kfalse src ($const #f))))))
315 ;; Well-known callee with more than two free variables; the closure
316 ;; is a vector.
317 ((#t . _)
318 (let ((nfree (length free)))
319 (let-fresh (kinit klen kfalse) (false len-var)
320 (build-cps-term
321 ($letk ((kinit ($kargs (name) (var) ,body))
322 (kfalse
323 ($kargs ('false) (false)
324 ($letk ((klen
325 ($kargs ('len) (len-var)
326 ($continue kinit src
327 ($primcall (if (<= nfree #xff)
328 'make-vector/immediate
329 'make-vector)
330 (len-var false))))))
331 ($continue klen src ($const nfree))))))
332 ($continue kfalse src ($const #f)))))))))
333
334 (define (init-closure src var known? closure-free body)
335 "Initialize the free variables @var{closure-free} in a closure
336 bound to @var{var}, and continue with @var{body}."
337 (match (cons known? closure-free)
338 ;; Well-known callee with no free variables; no initialization
339 ;; necessary.
340 ((#t) body)
341 ;; Well-known callee with one free variable; no initialization
342 ;; necessary.
343 ((#t _) body)
344 ;; Well-known callee with two free variables; do a set-car! and
345 ;; set-cdr!.
346 ((#t v0 v1)
347 (let-fresh (kcar kcdr) ()
348 (convert-free-var
349 v0
350 (lambda (v0)
351 (build-cps-term
352 ($letk ((kcar ($kargs () ()
353 ,(convert-free-var
354 v1
355 (lambda (v1)
356 (build-cps-term
357 ($letk ((kcdr ($kargs () () ,body)))
358 ($continue kcdr src
359 ($primcall 'set-cdr! (var v1))))))))))
360 ($continue kcar src
361 ($primcall 'set-car! (var v0)))))))))
362 ;; Otherwise residualize a sequence of vector-set! or free-set!,
363 ;; depending on whether the callee is well-known or not.
364 (_
365 (fold (lambda (free idx body)
366 (let-fresh (k) (idxvar)
367 (build-cps-term
368 ($letk ((k ($kargs () () ,body)))
369 ,(convert-free-var
370 free
371 (lambda (free)
372 (build-cps-term
373 ($letconst (('idx idxvar idx))
374 ($continue k src
375 ($primcall (cond
376 ((not known?) 'free-set!)
377 ((<= idx #xff) 'vector-set!/immediate)
378 (else 'vector-set!))
379 (var idxvar free)))))))))))
380 body
381 closure-free
382 (iota (length closure-free))))))
383
384 ;; Load the closure for a known call. The callee may or may not be
385 ;; known at all call sites.
386 (define (convert-known-proc-call var label self self-known? free k)
387 ;; Well-known closures with one free variable are replaced at their
388 ;; use sites by uses of the one free variable. The use sites of a
389 ;; well-known closures are only in well-known proc calls, and in
390 ;; free lists of other closures. Here we handle the call case; the
391 ;; free list case is handled by prune-free-vars.
392 (define (rename var)
393 (let ((var* (vector-ref aliases var)))
394 (if var*
395 (rename var*)
396 var)))
397 (match (cons (well-known? label)
398 (hashq-ref free-vars label))
399 ((#t)
400 ;; Calling a well-known procedure with no free variables; pass #f
401 ;; as the closure.
402 (let-fresh (k*) (v*)
403 (build-cps-term
404 ($letk ((k* ($kargs (v*) (v*) ,(k v*))))
405 ($continue k* #f ($const #f))))))
406 ((#t _)
407 ;; Calling a well-known procedure with one free variable; pass
408 ;; the free variable as the closure.
409 (convert-free-var (rename var) k))
410 (_
411 (convert-free-var var k))))
412
413 (define (visit-cont cont)
414 (rewrite-cps-cont cont
415 (($ $cont label ($ $kargs names vars body))
416 (label ($kargs names vars ,(visit-term body))))
417 (($ $cont label ($ $kfun src meta self tail clause))
418 (label ($kfun src meta self ,tail
419 ,(and clause (visit-cont clause)))))
420 (($ $cont label ($ $kclause arity body alternate))
421 (label ($kclause ,arity ,(visit-cont body)
422 ,(and alternate (visit-cont alternate)))))
423 (($ $cont) ,cont)))
424 (define (visit-term term)
425 (match term
426 (($ $letk conts body)
427 (build-cps-term
428 ($letk ,(map visit-cont conts) ,(visit-term body))))
429
430 ;; Remove letrec.
431 (($ $letrec names vars funs body)
432 (let lp ((in (map list names vars funs))
433 (bindings (lambda (body) body))
434 (body (visit-term body)))
435 (match in
436 (() (bindings body))
437 (((name var ($ $fun ()
438 (and fun-body
439 ($ $cont kfun ($ $kfun src))))) . in)
440 (let ((fun-free (hashq-ref free-vars kfun)))
441 (lp in
442 (lambda (body)
443 (allocate-closure
444 src name var kfun (well-known? kfun) fun-free
445 (bindings body)))
446 (init-closure
447 src var (well-known? kfun) fun-free
448 body)))))))
449
450 (($ $continue k src (or ($ $void) ($ $const) ($ $prim)))
451 term)
452
453 (($ $continue k src ($ $fun () ($ $cont kfun)))
454 (let ((fun-free (hashq-ref free-vars kfun)))
455 (match (cons (well-known? kfun) fun-free)
456 ((known?)
457 (build-cps-term
458 ($continue k src ,(if known?
459 (build-cps-exp ($const #f))
460 (build-cps-exp ($closure kfun 0))))))
461 ((#t _)
462 ;; A well-known closure of one free variable is replaced
463 ;; at each use with the free variable itself, so we don't
464 ;; need a binding at all; and yet, the continuation
465 ;; expects one value, so give it something. DCE should
466 ;; clean up later.
467 (build-cps-term
468 ($continue k src ,(build-cps-exp ($const #f)))))
469 (_
470 (let-fresh () (var)
471 (allocate-closure
472 src #f var kfun (well-known? kfun) fun-free
473 (init-closure
474 src var (well-known? kfun) fun-free
475 (build-cps-term ($continue k src ($values (var)))))))))))
476
477 (($ $continue k src ($ $call proc args))
478 (match (hashq-ref named-funs proc)
479 (($ $cont kfun)
480 (convert-known-proc-call
481 proc kfun self self-known? free
482 (lambda (proc)
483 (convert-free-vars args
484 (lambda (args)
485 (build-cps-term
486 ($continue k src
487 ($callk kfun proc args))))))))
488 (#f
489 (convert-free-vars (cons proc args)
490 (match-lambda
491 ((proc . args)
492 (build-cps-term
493 ($continue k src
494 ($call proc args)))))))))
495
496 (($ $continue k src ($ $primcall name args))
497 (convert-free-vars args
498 (lambda (args)
499 (build-cps-term
500 ($continue k src ($primcall name args))))))
501
502 (($ $continue k src ($ $values args))
503 (convert-free-vars args
504 (lambda (args)
505 (build-cps-term
506 ($continue k src ($values args))))))
507
508 (($ $continue k src ($ $prompt escape? tag handler))
509 (convert-free-var tag
510 (lambda (tag)
511 (build-cps-term
512 ($continue k src
513 ($prompt escape? tag handler))))))))
514 (visit-cont (build-cps-cont (label ,fun)))))
515
516 (define (convert-closures fun)
517 "Convert free reference in @var{exp} to primcalls to @code{free-ref},
518 and allocate and initialize flat closures."
519 (let ((dfg (compute-dfg fun)))
520 (with-fresh-name-state-from-dfg dfg
521 (call-with-values (lambda () (analyze-closures fun dfg))
522 (lambda (bound-vars free-vars named-funs well-known)
523 (let ((labels (sort (hash-map->list (lambda (k v) k) free-vars) <))
524 (aliases (make-vector (var-counter) #f)))
525 (prune-free-vars free-vars named-funs well-known aliases)
526 (build-cps-term
527 ($program
528 ,(map (lambda (label)
529 (convert-one (hashq-ref bound-vars label) label
530 (lookup-cont label dfg)
531 free-vars named-funs well-known aliases))
532 labels)))))))))