Escape Analysis on reddit
Steven Schveighoffer
schveiguy at yahoo.com
Sat Nov 1 08:44:09 PDT 2008
"Walter Bright" wrote
> Steven Schveighoffer wrote:
>> So how does foo know what scope return means? Consider a min function:
>>
>> scope T min(T)(scope T v1, scope T v2);
>>
>> Now, let's try it out:
>>
>> scope int *foo(scope int *p)
>> {
>> int x;
>> return min(&x, p);
>> }
>>
>> Shouldn't this not compile? If you say 'well, the compiler can take the
>> most conservative approach, and assume scope means the most inner scope.'
>
> Assume the compiler keeps track of, for every expression, a list of
> references that it is composed of. For:
> min(&x, p)
> the compiler knows that min() returns some combination of v1 and v2, so
> the expression min(&x, p) is tagged with the list [&x,p]. Now it examines
> the return, and sees that [&x,p] is escaping the scope of the function.
> Although p is a scope parameter, the scope on the foo() return value says
> that's ok, so that passes. The &x, however, fails because it's a reference
> to a local going out of scope.
>
> So, I think we've got this case covered properly.
OK, I assumed that was an easy one, but I was establishing how it's hard to
solve this and the other example.
> But I don't like the syntax. For one thing, if the return value might
> refer to parameter p1 but not p2, we need to express that.
This is part of it, you also can have return values through parameters which
need to be tracked.
> So, something like:
>
> int* foo(return scope int* p1, scope int* p2, return scope int* p3);
>
> Now, the caller tags a call to foo() with [arg1,arg3] for its escape
> analysis, omitting arg2.
This is better functionally, but notice how verbose we are getting. Now add
in tagging parameters to depend on other parameters (like for instance in a
swap function).
> The fly in this is I'd like to combine it somehow with the const
> propagation issue of argument type to return type.
Note that the const propogation issue isn't as complex, because you can't
change const parameters, so you only have to do return value analysis.
-Steve
More information about the Digitalmars-d
mailing list