1 ;;; Continuation-passing style (CPS) intermediate language (IL)
3 ;; Copyright (C) 2013 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 rtl)
41 (define (compute-contification fun)
42 (let* ((dfg (compute-dfg fun))
43 (cont-table (dfg-cont-table dfg))
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 (hashq-set! cont-splices scope
56 (append conts (hashq-ref cont-splices scope '()))))
58 ;; If K is a continuation that binds one variable, and it has only
59 ;; one predecessor, return that variable.
60 (define (bound-symbol k)
61 (match (lookup-cont k cont-table)
63 (match (lookup-predecessors k dfg)
65 ;; K has one predecessor, the one that defined SYM.
70 (define (contify-fun term-k sym self tail arities bodies)
72 (list sym) (list self) (list tail)
73 (list arities) (list bodies)))
75 ;; Given a set of mutually recursive functions bound to local
76 ;; variables SYMS, with self symbols SELFS, tail continuations
77 ;; TAILS, arities ARITIES, and bodies BODIES, all bound in TERM-K,
78 ;; contify them if we can prove that they all return to the same
79 ;; continuation. Returns a true value on success, and false
81 (define (contify-funs term-k syms selfs tails arities bodies)
83 (null? (lookup-uses sym dfg)))
85 ;; Are the given args compatible with any of the arities?
86 (define (applicable? proc args)
87 (let lp ((arities (assq-ref (map cons syms arities) proc)))
89 ((($ $arity req () #f () #f) . arities)
90 (or (= (length args) (length req))
92 ;; If we reached the end of the arities, fail. Also fail if
93 ;; the next arity in the list has optional, keyword, or rest
97 ;; If the use of PROC in continuation USE is a call to PROC that
98 ;; is compatible with one of the procedure's arities, return the
99 ;; target continuation. Otherwise return #f.
100 (define (call-target use proc)
101 (match (find-call (lookup-cont use cont-table))
102 (($ $continue k src ($ $call proc* args))
103 (and (eq? proc proc*) (not (memq proc args)) (applicable? proc args)
107 ;; If this set of functions is always called with one
108 ;; continuation, not counting tail calls between the functions,
109 ;; return that continuation.
110 (define (find-common-continuation)
111 (let visit-syms ((syms syms) (k #f))
115 (let visit-uses ((uses (lookup-uses sym dfg)) (k k))
117 (() (visit-syms syms k))
119 (and=> (call-target use sym)
122 ((memq k* tails) (visit-uses uses k))
123 ((not k) (visit-uses uses k*))
124 ((eq? k k*) (visit-uses uses k))
127 ;; Given that the functions are called with the common
128 ;; continuation K, determine the scope at which to contify the
129 ;; functions. If K is in scope in the term, we go ahead and
130 ;; contify them there. Otherwise the scope is inside the letrec
131 ;; body, and so choose the scope in which the continuation is
132 ;; defined, whose free variables are a superset of the free
133 ;; variables of the functions.
135 ;; FIXME: Does this choose the right scope for contified let-bound
137 (define (find-contification-scope k)
138 (if (continuation-bound-in? k term-k dfg)
140 (let ((scope (lookup-block-scope k dfg)))
141 (match (lookup-cont scope cont-table)
142 ;; The common continuation was the tail of some function
143 ;; inside the letrec body. If that function has just
144 ;; one clause, contify into that clause. Otherwise
146 (($ $kentry self tail clauses)
148 ((($ $cont _ ($ $kclause arity ($ $cont kargs))))
153 ;; We are going to contify. Mark all SYMs for replacement in
154 ;; calls, and mark the tail continuations for replacement by K.
155 ;; Arrange for the continuations to be spliced into SCOPE.
156 (define (enqueue-contification! k scope)
157 (for-each (lambda (sym tail arities bodies)
159 ((($ $cont body-k) ...)
160 (subst-call! sym arities body-k)))
161 (subst-return! tail k))
162 syms tails arities bodies)
163 (splice-conts! scope (concatenate bodies))
167 (and (and-map unused? selfs)
168 (and=> (find-common-continuation)
170 (and=> (find-contification-scope k)
171 (cut enqueue-contification! k <>))))))
173 (define (visit-fun term)
175 (($ $fun src meta free body)
177 (define (visit-cont cont)
179 (($ $cont sym ($ $kargs _ _ body))
180 (visit-term body sym))
181 (($ $cont sym ($ $kentry self tail clauses))
182 (for-each visit-cont clauses))
183 (($ $cont sym ($ $kclause arity body))
187 (define (visit-term term term-k)
189 (($ $letk conts body)
190 (for-each visit-cont conts)
191 (visit-term body term-k))
192 (($ $letrec names syms funs body)
193 (define (split-components nsf)
194 ;; FIXME: Compute strongly-connected components. Currently
195 ;; we just put non-recursive functions in their own
196 ;; components, and lump everything else in the remaining
198 (define (recursive? k)
199 (or-map (cut variable-free-in? <> k dfg) syms))
200 (let lp ((nsf nsf) (rec '()))
206 (((and elt (n s ($ $fun src meta free ($ $cont kentry))))
208 (if (recursive? kentry)
209 (lp nsf (cons elt rec))
210 (cons (list elt) (lp nsf rec)))))))
211 (define (visit-component component)
213 (((name sym fun) ...)
215 ((($ $fun src meta free
218 ($ $cont tail-k ($ $ktail))
219 (($ $cont _ ($ $kclause arity body))
222 (unless (contify-funs term-k sym self tail-k arity body)
223 (for-each visit-fun fun)))))))
224 (visit-term body term-k)
225 (for-each visit-component
226 (split-components (map list names syms funs))))
227 (($ $continue k src exp)
229 (($ $fun src meta free
232 ($ $cont tail-k ($ $ktail))
233 (($ $cont _ ($ $kclause arity body)) ...))))
234 (if (and=> (bound-symbol k)
236 (contify-fun term-k sym self tail-k arity body)))
237 (elide-function! k (lookup-cont k cont-table))
242 (values call-substs cont-substs fun-elisions cont-splices)))
244 (define (apply-contification fun call-substs cont-substs fun-elisions cont-splices)
245 (define (contify-call src proc args)
246 (and=> (assq-ref call-substs proc)
248 (let lp ((clauses clauses))
250 (() (error "invalid contification"))
251 (((($ $arity req () #f () #f) . k) . clauses)
252 (if (= (length req) (length args))
257 ((_ . clauses) (lp clauses)))))))
258 (define (continue k src exp)
259 (define (lookup-return-cont k)
260 (match (assq-ref cont-substs k)
262 (k (lookup-return-cont k))))
263 (let ((k* (lookup-return-cont k)))
264 ;; We are contifying this return. It must be a call or a
265 ;; primcall to values, return, or return-values.
267 (build-cps-term ($continue k src ,exp))
268 (rewrite-cps-term exp
269 (($ $primcall 'return (val))
270 ($continue k* src ($primcall 'values (val))))
272 ($continue k* src ($primcall 'values vals)))
273 (_ ($continue k* src ,exp))))))
274 (define (splice-continuations term-k term)
275 (match (hashq-ref cont-splices term-k)
278 (let lp ((term term))
279 (rewrite-cps-term term
280 (($ $letrec names syms funs body)
281 ($letrec names syms funs ,(lp body)))
282 (($ $letk conts* body)
283 ($letk ,(append conts* (filter-map visit-cont cont))
286 ($letk ,(filter-map visit-cont cont)
288 (define (visit-fun term)
289 (rewrite-cps-exp term
290 (($ $fun src meta free body)
291 ($fun src meta free ,(visit-cont body)))))
292 (define (visit-cont cont)
293 (rewrite-cps-cont cont
294 (($ $cont (? (cut assq <> fun-elisions)))
295 ;; This cont gets inlined in place of the $fun.
297 (($ $cont sym ($ $kargs names syms body))
298 (sym ($kargs names syms ,(visit-term body sym))))
299 (($ $cont sym ($ $kentry self tail clauses))
300 (sym ($kentry self ,tail ,(map visit-cont clauses))))
301 (($ $cont sym ($ $kclause arity body))
302 (sym ($kclause ,arity ,(visit-cont body))))
305 (define (visit-term term term-k)
307 (($ $letk conts body)
308 ;; Visit the body first, so we rewrite depth-first.
309 (let lp ((body (visit-term body term-k)))
310 ;; Because we attach contified functions on a particular
311 ;; term-k, and one term-k can correspond to an arbitrarily
312 ;; nested sequence of $letrec and $letk instances, normalize
313 ;; so that all continuations are bound by one $letk --
314 ;; guaranteeing that they are in the same scope.
315 (rewrite-cps-term body
316 (($ $letrec names syms funs body)
317 ($letrec names syms funs ,(lp body)))
318 (($ $letk conts* body)
319 ($letk ,(append conts* (filter-map visit-cont conts))
322 ($letk ,(filter-map visit-cont conts)
324 (($ $letrec names syms funs body)
325 (rewrite-cps-term (filter (match-lambda
326 ((n s f) (not (assq s call-substs))))
327 (map list names syms funs))
328 (((names syms funs) ...)
329 ($letrec names syms (map visit-fun funs)
330 ,(visit-term body term-k)))))
331 (($ $continue k src exp)
332 (splice-continuations
337 ((assq-ref fun-elisions k)
339 (($ $kargs (_) (_) body)
340 (visit-term body k))))
342 (continue k src (visit-fun exp)))))
344 (or (contify-call src proc args)
345 (continue k src exp)))
346 (_ (continue k src exp)))))))
349 (define (contify fun)
350 (call-with-values (lambda () (compute-contification fun))
351 (lambda (call-substs cont-substs fun-elisions cont-splices)
352 (if (null? call-substs)
354 ;; Iterate to fixed point.
356 (apply-contification fun call-substs cont-substs fun-elisions cont-splices))))))