[bitc-dev] Re: Closure Conversion

Swaroop Sridhar swaroop at cs.jhu.edu
Fri Jul 8 16:34:59 EDT 2005


Jonathan S. Shapiro wrote:
> Swaroop:
>  
> If the lambda does not escape then the closure can be stack-allocated.
> 
> Closure conversion certainly should NOT be done blindly -- this
> transform should only be done for procedures that require it.
> 

Determining whether a function needs to be closure converted is easy. 
But, I think, to determine whether its closure can be stack allocated or 
not is hard.

Cases like:

(define F (lambda (x) ((lambda (y) x) 2)))

are easy. The inner-lambda's closure can be safely stack allocated.

But consider:
   (define G (lambda (f) (let ((p 3)) (f (lambda (x) p)))))

the inner lambda is apparently not escaping downward. But it is not 
clear whether its closure can be stack allocated or not.

For example, stack allocation is safe in cases like:

   (define P (G (lambda (x) ())))

but not safe in cases like:

   (define H (mutable (lambda (x) 5)))
   (define I (lambda (m) (begin (set! H m) ())))
   (define J (G I))
   (define K ((immutable H) ())) ; K is now 3

This is why I had suggested that there should be some data-flow analysis 
as in [1]. Otherwise, I think the choices are:

i) Escaping will come to mean escaping upwards or downwards, and most 
lambdas will have their closures heap allocated

ii) The language run-time will have to be very smart in order to 
construct closures appropriately for each case.

Am I missing something?

Swaroop.

[1] P. A. Steckler and M. Wand. Lightweight closure conversion. 
(TOPLAS), pages 48--86, January 1997.


More information about the bitc-dev mailing list