Skip to content

Commit

Permalink
Remove RemoveUnneededReferences from LamdaRewriter (#21367)
Browse files Browse the repository at this point in the history
Currently, the lambda rewriter has an early optimization pass in
analysis that tries to find all local functions that only capture 'this'
and remove references to local functions that do the same. There are two
problems with this approach:

    1) Generally, removing information from the tree is a bad idea
    because it hurts further analysis passes that may have needed that
    information.

    2) The optimization strategy itself is very tricky and has a number
    of complex corner cases. This has lead to bugs, for example #19033.

This PR deletes the current method and adds a new optimization routine
at the end of the analysis, operating on assigned scopes and
environments rather than removing captured variable analysis. The new
optimization is as follows: if we end up with an environment containing
only 'this', the environment can be removed, all containing methods can
be moved to the top-level type, and all environments which capture the
'this' environment can instead directly capture the 'this' parameter.
This produces almost the same results as the previous optimization, but
is easier to validate as an algebraic equivalence.

The baseline changes come from the new optimization being less aggressive 
about moving functions which only capture 'this' to the top level. This
appears to be a wash -- some codegen gets slightly better, some gets
slightly worse.

Fixes #19033
Fixes #20577
  • Loading branch information
agocke committed Aug 11, 2017
1 parent 1c1fbc6 commit caa7830
Show file tree
Hide file tree
Showing 15 changed files with 608 additions and 375 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ internal MethodWithBody(MethodSymbol method, BoundStatement body, ImportChain im

public readonly CSharpCompilation Compilation;

public ClosureEnvironment StaticLambdaFrame;
public SynthesizedClosureEnvironment StaticLambdaFrame;

/// <summary>
/// A graph of method->method references for this(...) constructor initializers.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ private LambdaCapturedVariable(SynthesizedContainer frame, TypeSymbol type, stri
_isThis = isThisParameter;
}

public static LambdaCapturedVariable Create(ClosureEnvironment frame, Symbol captured, ref int uniqueId)
public static LambdaCapturedVariable Create(SynthesizedClosureEnvironment frame, Symbol captured, ref int uniqueId)
{
Debug.Assert(captured is LocalSymbol || captured is ParameterSymbol);

Expand Down Expand Up @@ -87,7 +87,7 @@ private static TypeSymbol GetCapturedVariableFieldType(SynthesizedContainer fram
if ((object)local != null)
{
// if we're capturing a generic frame pointer, construct it with the new frame's type parameters
var lambdaFrame = local.Type.OriginalDefinition as ClosureEnvironment;
var lambdaFrame = local.Type.OriginalDefinition as SynthesizedClosureEnvironment;
if ((object)lambdaFrame != null)
{
// lambdaFrame may have less generic type parameters than frame, so trim them down (the first N will always match)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,23 @@ public sealed class Closure

public ClosureEnvironment ContainingEnvironmentOpt;

private bool _capturesThis;

/// <summary>
/// True if this closure directly or transitively captures 'this' (captures
/// a local function which directly or indirectly captures 'this').
/// Calculated in <see cref="MakeAndAssignEnvironments"/>.
/// </summary>
public bool CapturesThis
{
get => _capturesThis;
set
{
Debug.Assert(value);
_capturesThis = value;
}
}

public Closure(MethodSymbol symbol)
{
Debug.Assert(symbol != null);
Expand All @@ -132,81 +149,29 @@ public void Free()
}
}

/// <summary>
/// Optimizes local functions such that if a local function only references other local functions
/// that capture no variables, we don't need to create capture environments for any of them.
/// </summary>
private void RemoveUnneededReferences(ParameterSymbol thisParam)
public sealed class ClosureEnvironment
{
var methodGraph = new MultiDictionary<MethodSymbol, MethodSymbol>();
var capturesThis = new HashSet<MethodSymbol>();
var capturesVariable = new HashSet<MethodSymbol>();
var visitStack = new Stack<MethodSymbol>();
VisitClosures(ScopeTree, (scope, closure) =>
{
foreach (var capture in closure.CapturedVariables)
{
if (capture is MethodSymbol localFunc)
{
methodGraph.Add(localFunc, closure.OriginalMethodSymbol);
}
else if (capture == thisParam)
{
if (capturesThis.Add(closure.OriginalMethodSymbol))
{
visitStack.Push(closure.OriginalMethodSymbol);
}
}
else if (capturesVariable.Add(closure.OriginalMethodSymbol) &&
!capturesThis.Contains(closure.OriginalMethodSymbol))
{
visitStack.Push(closure.OriginalMethodSymbol);
}
}
});

while (visitStack.Count > 0)
{
var current = visitStack.Pop();
var setToAddTo = capturesVariable.Contains(current) ? capturesVariable : capturesThis;
foreach (var capturesCurrent in methodGraph[current])
{
if (setToAddTo.Add(capturesCurrent))
{
visitStack.Push(capturesCurrent);
}
}
}
public readonly SetWithInsertionOrder<Symbol> CapturedVariables;

/// <summary>
/// Represents a <see cref="SynthesizedEnvironment"/> that had its environment
/// pointer (a local pointing to the environment) captured like a captured
/// variable. Assigned in
/// <see cref="ComputeLambdaScopesAndFrameCaptures(ParameterSymbol)"/>
/// </summary>
public bool CapturesParent;

// True if there are any closures in the tree which
// capture 'this' and another variable
bool captureMoreThanThis = false;
public readonly bool IsStruct;
internal SynthesizedClosureEnvironment SynthesizedEnvironment;

VisitClosures(ScopeTree, (scope, closure) =>
public ClosureEnvironment(IEnumerable<Symbol> capturedVariables, bool isStruct)
{
if (!capturesVariable.Contains(closure.OriginalMethodSymbol))
{
closure.CapturedVariables.Clear();
}
if (capturesThis.Contains(closure.OriginalMethodSymbol))
CapturedVariables = new SetWithInsertionOrder<Symbol>();
foreach (var item in capturedVariables)
{
closure.CapturedVariables.Add(thisParam);
if (closure.CapturedVariables.Count > 1)
{
captureMoreThanThis |= true;
}
CapturedVariables.Add(item);
}
});

if (!captureMoreThanThis && capturesThis.Count > 0)
{
// If we have closures which capture 'this', and nothing else, we can
// remove 'this' from the declared variables list, since we don't need
// to create an environment to hold 'this' (since we can emit the
// lowered methods directly onto the containing class)
bool removed = ScopeTree.DeclaredVariables.Remove(thisParam);
Debug.Assert(removed);
IsStruct = isStruct;
}
}

Expand All @@ -226,6 +191,31 @@ public static void VisitClosures(Scope scope, Action<Scope, Closure> action)
}
}

/// <summary>
/// Visit all the closures and return true when the <paramref name="func"/> returns
/// true. Otherwise, returns false.
/// </summary>
public static bool CheckClosures(Scope scope, Func<Scope, Closure, bool> func)
{
foreach (var closure in scope.Closures)
{
if (func(scope, closure))
{
return true;
}
}

foreach (var nested in scope.NestedScopes)
{
if (CheckClosures(nested, func))
{
return true;
}
}

return false;
}

/// <summary>
/// Visit the tree with the given root and run the <paramref name="action"/>
/// </summary>
Expand Down Expand Up @@ -491,6 +481,13 @@ private void AddIfCaptured(Symbol symbol, SyntaxNode syntax)
return;
}

if (symbol is MethodSymbol method &&
_currentClosure.OriginalMethodSymbol == method)
{
// Is this recursion? If so there's no capturing
return;
}

if (symbol.ContainingSymbol != _currentClosure.OriginalMethodSymbol)
{
// Restricted types can't be hoisted, so they are not permitted to be captured
Expand Down

0 comments on commit caa7830

Please sign in to comment.