1 ;;; Continuation-passing style (CPS) intermediate language (IL)
3 ;; Copyright (C) 2013, 2014 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
21 ;;; Contification is a pass that turns $fun instances into $cont
22 ;;; instances if all calls to the $fun return to the same continuation.
23 ;;; This is a more rigorous variant of our old "fixpoint labels
24 ;;; allocation" optimization.
26 ;;; See Kennedy's "Compiling with Continuations, Continued", and Fluet
27 ;;; and Weeks's "Contification using Dominators".
31 (define-module (language cps contification)
32 #:use-module (ice-9 match)
33 #:use-module ((srfi srfi-1) #:select (concatenate filter-map))
34 #:use-module (srfi srfi-26)
35 #:use-module (language cps)
36 #:use-module (language cps dfg)
37 #:use-module (language cps primitives)
38 #:use-module (language bytecode)
41 (define (compute-contification fun)
42 (let* ((dfg (compute-dfg fun))
43 (scope-table (make-hash-table))
47 (cont-splices (make-hash-table)))
48 (define (subst-call! sym arities body-ks)
49 (set! call-substs (acons sym (map cons arities body-ks) call-substs)))
50 (define (subst-return! old-tail new-tail)
51 (set! cont-substs (acons old-tail new-tail cont-substs)))
52 (define (elide-function! k cont)
53 (set! fun-elisions (acons k cont fun-elisions)))
54 (define (splice-conts! scope conts)
55 (for-each (match-lambda
56 (($ $cont k) (hashq-set! scope-table k scope)))
58 (hashq-set! cont-splices scope
59 (append conts (hashq-ref cont-splices scope '()))))
61 (define (lookup-return-cont k)
62 (match (assq-ref cont-substs k)
64 (k (lookup-return-cont k))))
66 ;; If K is a continuation that binds one variable, and it has only
67 ;; one predecessor, return that variable.
68 (define (bound-symbol k)
69 (match (lookup-cont k dfg)
71 (match (lookup-predecessors k dfg)
73 ;; K has one predecessor, the one that defined SYM.
78 (define (extract-arities clause)
80 (($ $cont _ ($ $kclause arity body alternate))
81 (cons arity (extract-arities alternate)))
83 (define (extract-bodies clause)
85 (($ $cont _ ($ $kclause arity body alternate))
86 (cons body (extract-bodies alternate)))
89 (define (contify-fun term-k sym self tail arities bodies)
91 (list sym) (list self) (list tail)
92 (list arities) (list bodies)))
94 ;; Given a set of mutually recursive functions bound to local
95 ;; variables SYMS, with self symbols SELFS, tail continuations
96 ;; TAILS, arities ARITIES, and bodies BODIES, all bound in TERM-K,
97 ;; contify them if we can prove that they all return to the same
98 ;; continuation. Returns a true value on success, and false
100 (define (contify-funs term-k syms selfs tails arities bodies)
101 (define (unused? sym)
102 (null? (lookup-uses sym dfg)))
104 ;; Are the given args compatible with any of the arities?
105 (define (applicable? proc args)
106 (let lp ((arities (assq-ref (map cons syms arities) proc)))
108 ((($ $arity req () #f () #f) . arities)
109 (or (= (length args) (length req))
111 ;; If we reached the end of the arities, fail. Also fail if
112 ;; the next arity in the list has optional, keyword, or rest
116 ;; If the use of PROC in continuation USE is a call to PROC that
117 ;; is compatible with one of the procedure's arities, return the
118 ;; target continuation. Otherwise return #f.
119 (define (call-target use proc)
120 (match (find-call (lookup-cont use dfg))
121 (($ $continue k src ($ $call proc* args))
122 (and (eq? proc proc*) (not (memq proc args)) (applicable? proc args)
123 ;; Converge more quickly by resolving already-contified
125 (lookup-return-cont k)))
128 ;; If this set of functions is always called with one
129 ;; continuation, not counting tail calls between the functions,
130 ;; return that continuation.
131 (define (find-common-continuation)
132 (let visit-syms ((syms syms) (k #f))
136 (let visit-uses ((uses (lookup-uses sym dfg)) (k k))
138 (() (visit-syms syms k))
140 (and=> (call-target use sym)
143 ((memq k* tails) (visit-uses uses k))
144 ((not k) (visit-uses uses k*))
145 ((eq? k k*) (visit-uses uses k))
148 ;; Given that the functions are called with the common
149 ;; continuation K, determine the scope at which to contify the
150 ;; functions. If K is in scope in the term, we go ahead and
151 ;; contify them there. Otherwise the scope is inside the letrec
152 ;; body, and so choose the scope in which the continuation is
153 ;; defined, whose free variables are a superset of the free
154 ;; variables of the functions.
156 ;; There is some slight trickiness here. Call-target already uses
157 ;; the information we compute within this pass. Previous
158 ;; contifications may cause functions to be contified not at their
159 ;; point of definition but at their point of non-recursive use.
160 ;; That will cause the scope nesting to change. (It may
161 ;; effectively push a function deeper down the tree -- the second
162 ;; case above, a call within the letrec body.) What if we contify
163 ;; to the tail of a previously contified function? We have to
164 ;; track what the new scope tree will be when asking whether K
165 ;; will be bound in TERM-K's scope, not the scope tree that
166 ;; existed when we started the pass.
168 ;; FIXME: Does this choose the right scope for contified let-bound
170 (define (find-contification-scope k)
171 (define (scope-contains? scope k)
172 (let ((k-scope (or (hashq-ref scope-table k)
173 (let ((k-scope (lookup-block-scope k dfg)))
174 (hashq-set! scope-table k k-scope)
176 (or (eq? scope k-scope)
177 (and k-scope (scope-contains? scope k-scope)))))
179 ;; Find the scope of K.
180 (define (continuation-scope k)
181 (or (hashq-ref scope-table k)
182 (let ((scope (lookup-block-scope k dfg)))
183 (hashq-set! scope-table k scope)
186 (let ((k-scope (continuation-scope k)))
187 (if (scope-contains? k-scope term-k)
189 (match (lookup-cont k-scope dfg)
190 (($ $kentry self tail clause)
191 ;; K is the tail of some function. If that function
192 ;; has just one clause, return that clause. Otherwise
195 (($ $cont _ ($ $kclause arity ($ $cont kargs) #f))
200 ;; We are going to contify. Mark all SYMs for replacement in
201 ;; calls, and mark the tail continuations for replacement by K.
202 ;; Arrange for the continuations to be spliced into SCOPE.
203 (define (enqueue-contification! k scope)
204 (for-each (lambda (sym tail arities bodies)
206 ((($ $cont body-k) ...)
207 (subst-call! sym arities body-k)))
208 (subst-return! tail k))
209 syms tails arities bodies)
210 (splice-conts! scope (concatenate bodies))
214 (and (and-map unused? selfs)
215 (and=> (find-common-continuation)
217 (and=> (find-contification-scope k)
218 (cut enqueue-contification! k <>))))))
220 (define (visit-fun term)
222 (($ $fun src meta free body)
224 (define (visit-cont cont)
226 (($ $cont sym ($ $kargs _ _ body))
227 (visit-term body sym))
228 (($ $cont sym ($ $kentry self tail clause))
229 (when clause (visit-cont clause)))
230 (($ $cont sym ($ $kclause arity body alternate))
232 (when alternate (visit-cont alternate)))
235 (define (visit-term term term-k)
237 (($ $letk conts body)
238 (for-each visit-cont conts)
239 (visit-term body term-k))
240 (($ $letrec names syms funs body)
241 (define (split-components nsf)
242 ;; FIXME: Compute strongly-connected components. Currently
243 ;; we just put non-recursive functions in their own
244 ;; components, and lump everything else in the remaining
246 (define (recursive? k)
247 (or-map (cut variable-free-in? <> k dfg) syms))
248 (let lp ((nsf nsf) (rec '()))
254 (((and elt (n s ($ $fun src meta free ($ $cont kentry))))
256 (if (recursive? kentry)
257 (lp nsf (cons elt rec))
258 (cons (list elt) (lp nsf rec)))))))
259 (define (extract-arities+bodies clauses)
260 (values (map extract-arities clauses)
261 (map extract-bodies clauses)))
262 (define (visit-component component)
264 (((name sym fun) ...)
266 ((($ $fun src meta free
268 ($ $kentry self ($ $cont tail-k ($ $ktail)) clause)))
270 (call-with-values (lambda () (extract-arities+bodies clause))
271 (lambda (arities bodies)
272 (if (contify-funs term-k sym self tail-k arities bodies)
273 (for-each (cut for-each visit-cont <>) bodies)
274 (for-each visit-fun fun)))))))))
275 (visit-term body term-k)
276 (for-each visit-component
277 (split-components (map list names syms funs))))
278 (($ $continue k src exp)
280 (($ $fun src meta free
282 ($ $kentry self ($ $cont tail-k ($ $ktail)) clause)))
283 (if (and=> (bound-symbol k)
285 (contify-fun term-k sym self tail-k
286 (extract-arities clause)
287 (extract-bodies clause))))
289 (elide-function! k (lookup-cont k dfg))
290 (for-each visit-cont (extract-bodies clause)))
295 (values call-substs cont-substs fun-elisions cont-splices)))
297 (define (apply-contification fun call-substs cont-substs fun-elisions cont-splices)
298 (define (contify-call src proc args)
299 (and=> (assq-ref call-substs proc)
301 (let lp ((clauses clauses))
303 (() (error "invalid contification"))
304 (((($ $arity req () #f () #f) . k) . clauses)
305 (if (= (length req) (length args))
310 ((_ . clauses) (lp clauses)))))))
311 (define (continue k src exp)
312 (define (lookup-return-cont k)
313 (match (assq-ref cont-substs k)
315 (k (lookup-return-cont k))))
316 (let ((k* (lookup-return-cont k)))
317 ;; We are contifying this return. It must be a call or a
318 ;; primcall to values, return, or return-values.
320 (build-cps-term ($continue k src ,exp))
321 (rewrite-cps-term exp
322 (($ $primcall 'return (val))
323 ($continue k* src ($primcall 'values (val))))
325 ($continue k* src ($primcall 'values vals)))
326 (_ ($continue k* src ,exp))))))
327 (define (splice-continuations term-k term)
328 (match (hashq-ref cont-splices term-k)
331 (let lp ((term term))
332 (rewrite-cps-term term
333 (($ $letrec names syms funs body)
334 ($letrec names syms funs ,(lp body)))
335 (($ $letk conts* body)
336 ($letk ,(append conts* (filter-map visit-cont cont))
339 ($letk ,(filter-map visit-cont cont)
341 (define (visit-fun term)
342 (rewrite-cps-exp term
343 (($ $fun src meta free body)
344 ($fun src meta free ,(visit-cont body)))))
345 (define (visit-cont cont)
346 (rewrite-cps-cont cont
347 (($ $cont (? (cut assq <> fun-elisions)))
348 ;; This cont gets inlined in place of the $fun.
350 (($ $cont sym ($ $kargs names syms body))
351 (sym ($kargs names syms ,(visit-term body sym))))
352 (($ $cont sym ($ $kentry self tail clause))
353 (sym ($kentry self ,tail ,(and clause (visit-cont clause)))))
354 (($ $cont sym ($ $kclause arity body alternate))
355 (sym ($kclause ,arity ,(visit-cont body)
356 ,(and alternate (visit-cont alternate)))))
359 (define (visit-term term term-k)
361 (($ $letk conts body)
362 ;; Visit the body first, so we rewrite depth-first.
363 (let lp ((body (visit-term body term-k)))
364 ;; Because we attach contified functions on a particular
365 ;; term-k, and one term-k can correspond to an arbitrarily
366 ;; nested sequence of $letrec and $letk instances, normalize
367 ;; so that all continuations are bound by one $letk --
368 ;; guaranteeing that they are in the same scope.
369 (rewrite-cps-term body
370 (($ $letrec names syms funs body)
371 ($letrec names syms funs ,(lp body)))
372 (($ $letk conts* body)
373 ($letk ,(append conts* (filter-map visit-cont conts))
376 ($letk ,(filter-map visit-cont conts)
378 (($ $letrec names syms funs body)
379 (rewrite-cps-term (filter (match-lambda
380 ((n s f) (not (assq s call-substs))))
381 (map list names syms funs))
382 (((names syms funs) ...)
383 ($letrec names syms (map visit-fun funs)
384 ,(visit-term body term-k)))))
385 (($ $continue k src exp)
386 (splice-continuations
391 ((assq-ref fun-elisions k)
393 (($ $kargs (_) (_) body)
394 (visit-term body k))))
396 (continue k src (visit-fun exp)))))
398 (or (contify-call src proc args)
399 (continue k src exp)))
400 (_ (continue k src exp)))))))
403 (define (contify fun)
404 (call-with-values (lambda () (compute-contification fun))
405 (lambda (call-substs cont-substs fun-elisions cont-splices)
406 (if (null? call-substs)
408 ;; Iterate to fixed point.
410 (apply-contification fun call-substs cont-substs fun-elisions cont-splices))))))