Escape analysis

Sergey Gromov snake.scaly at gmail.com
Tue Oct 28 13:58:59 PDT 2008


Walter Bright wrote:
> The first step is, are function parameters considered to be escaping by 
> default or not by default? I.e.:
> 
> void bar(noscope int* p);    // p escapes
> void bar(scope int* p);      // p does not escape
> void bar(int* p);            // what should be the default?
> 
> What should be the default? The functional programmer would probably 
> choose scope as the default, and the OOP programmer noscope.

I'm for safe defaults.  Programs shouldn't crash for no reason.

Here are my thoughts on escape analysis.  Sorry if they're obvious.

I think it is possible to detect whether a reference escapes or not in
the absence of function calls by analyzing an expression graph.

Assigning to a global state variable is an ultimate escape.

In the worst case, when only the current function can be analyzed and no
meta-info is available about other functions, the compiler must assume a
reference escapes if it is passed as an argument to another function.
This is the current D2 behavior.

Pure functions provide some meta-info because any reference passed as an
argument can only escape via a reference return value or other mutable
reference arguments.  This makes escape analysis possible even after an
unknown pure function is called.

For any function in a tree of imported modules the compiler could keep
some meta-data about which argument escapes where, if at all.  This way
even regular functions can participate in escape analysis without
blowing it up.

An argument to a virtual function call always escapes by default.  It
may be possible to declare an argument as non-escaping (scope?) and
compiler should then enforce non-escaping contract upon any overriding
functions.

An argument to a function declared as a prototype always escapes by
default.  It may be possible for the compiler to export some meta-info
along with the prototype when a .di file is generated, whether an
argument is guaranteed to not escape, or maybe even detailed info about
which argument escapes where, to mimic the compile-time meta-info.

The expression graph analysis should be the first step towards safe
stack closures.



More information about the Digitalmars-d mailing list