Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions src/FastExpressionCompiler/FastExpressionCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1602,12 +1602,8 @@ public static Result TryCollectInfo(ref CompilerContext context, Expression expr
return r;
}

var lastArgIndex = invokeArgCount - 1;
for (var i = 0; i < lastArgIndex; i++)
if ((r = TryCollectInfo(ref context, invokeArgs.GetArgument(i), nestedLambda, ref rootNestedLambdas, scopeIdx)) != Result.OK)
return r;
expr = invokeArgs.GetArgument(lastArgIndex);
continue;
return TryCollectArgsAsComplexOwner(ref context, invokeArgs, invokeExpr,
nestedLambda, ref rootNestedLambdas, scopeIdx);
}
case ExpressionType.Conditional:
var condExpr = (ConditionalExpression)expr;
Expand Down Expand Up @@ -2232,6 +2228,7 @@ private static Result TryCollectArgsAsComplexOwner(ref CompilerContext context,
NestedLambdaInfo nestedLambda, ref SmallList<NestedLambdaInfo> rootNestedLambdas, short scopeIdx)
{
var hasComplexExpression = false;
var precedingJumpCount = context.GotoTargets.Count;
var r = Result.OK;
foreach (var arg in args)
{
Expand All @@ -2240,7 +2237,7 @@ private static Result TryCollectArgsAsComplexOwner(ref CompilerContext context,
return r;
hasComplexExpression |= context.HasComplexExpression;
}
if (hasComplexExpression)
if (hasComplexExpression || context.GotoTargets.Count != precedingJumpCount)
{
context.HasComplexExpression = true;
context.MarkComplexArguments(owner);
Expand Down Expand Up @@ -6750,7 +6747,7 @@ private static bool TryEmitInvoke(InvocationExpression expr, ILGenerator il, ref
if (expr.GetArgCount() != 0)
{
var useResult = parent & ~(ParentFlags.IgnoreResult | ParentFlags.InstanceAccess | ParentFlags.InlinedLambdaInvoke);
var spillArguments = context.TryExpressionCount != 0 || deferCompile;
var spillArguments = context.TryExpressionCount != 0 || context.HasComplexArguments(expr) || deferCompile;
var receiver = spillArguments ? EmitStoreLocalVariable(il, deferCompile ? invokedExpr.Type : delegateType) : -1;
if (spillArguments)
{
Expand Down
2 changes: 2 additions & 0 deletions test/FastExpressionCompiler.TestsRunner.Net472/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ static void RunLightExpressionTests(object state)
t.Run(new LightExpression.IssueTests.Issue498_InvalidProgramException_when_using_loop());
t.Run(new LightExpression.IssueTests.Issue499_InvalidProgramException_for_Sorting_and_comparison_function());
t.Run(new LightExpression.UnitTests.ByRefConversionRegressionTests());
t.Run(new LightExpression.UnitTests.NestedJumpArgumentRegressionTests());
t.Run(new LightExpression.UnitTests.RefAssignmentExceptionTests());
t.Run(new LightExpression.UnitTests.PropertyGetterReceiverRegressionTests());
t.Run(new LightExpression.UnitTests.RuntimeTypeConstantRegressionTests());
Expand Down Expand Up @@ -217,6 +218,7 @@ static void RunLightExpressionTests(object state)
fecTests.Run(new Issue499_InvalidProgramException_for_Sorting_and_comparison_function());
fecTests.Run(new ReductionProgressRegressionTests());
fecTests.Run(new ByRefConversionRegressionTests());
fecTests.Run(new NestedJumpArgumentRegressionTests());
fecTests.Run(new RefAssignmentExceptionTests());
fecTests.Run(new PropertyGetterReceiverRegressionTests());
fecTests.Run(new RuntimeTypeConstantRegressionTests());
Expand Down
2 changes: 2 additions & 0 deletions test/FastExpressionCompiler.TestsRunner/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ public static void Main(string[] args)
lt.Run(new LightExpression.IssueTests.Issue499_InvalidProgramException_for_Sorting_and_comparison_function());
lt.Run(new LightExpression.IssueTests.Issue561_NonBooleanEqualityOperator());
lt.Run(new LightExpression.UnitTests.ByRefConversionRegressionTests());
lt.Run(new LightExpression.UnitTests.NestedJumpArgumentRegressionTests());
lt.Run(new LightExpression.UnitTests.RefAssignmentExceptionTests());
lt.Run(new LightExpression.UnitTests.PropertyGetterReceiverRegressionTests());
lt.Run(new LightExpression.UnitTests.RuntimeTypeConstantRegressionTests());
Expand Down Expand Up @@ -236,6 +237,7 @@ public static void Main(string[] args)
st.Run(new Issue561_NonBooleanEqualityOperator());
st.Run(new ReductionProgressRegressionTests());
st.Run(new ByRefConversionRegressionTests());
st.Run(new NestedJumpArgumentRegressionTests());
st.Run(new RefAssignmentExceptionTests());
st.Run(new PropertyGetterReceiverRegressionTests());
st.Run(new RuntimeTypeConstantRegressionTests());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
using System;
using S = System.Linq.Expressions.Expression;
#if LIGHT_EXPRESSION
namespace FastExpressionCompiler.LightExpression.UnitTests;
#else
namespace FastExpressionCompiler.UnitTests;
#endif

public struct NestedJumpArgumentRegressionTests : ITestX
{
public void Run(TestRun t)
{
Method_arguments_spill_across_nested_invocation_jump(t);
Constructor_arguments_spill_across_nested_invocation_jump(t);
Direct_jump_arguments_spill(t);
Byref_alias_survives_nested_jump_spilling(t);

Earlier_jump_in_argument_block_survives_later_call_collection(t);
}

private static string Trace;
public static int First() { Trace += "first;"; return 2; }
public static Func<int, int> Receiver() { Trace += "receiver;"; return Pass; }
public static int Pass(int value) { Trace += "pass;"; return value; }
public static int Combine(int first, int second) { Trace += "combine;"; return first + second; }
public static int Write(ref int first, int second) { Trace += "write;"; return first += second; }
public static void Finally() { Trace += "finally;"; }
public sealed class Pair
{
public Pair(int first, int second) { Trace += "ctor;"; }
}

private static System.Linq.Expressions.MethodCallExpression Call(string name, params System.Linq.Expressions.Expression[] args) =>
S.Call(typeof(NestedJumpArgumentRegressionTests).GetMethod(name), args);
private static System.Linq.Expressions.Expression Argument(System.Linq.Expressions.ParameterExpression leave, System.Linq.Expressions.LabelTarget target) =>
S.Condition(leave, S.Goto(target, S.Constant(7), typeof(int)), S.Constant(3));
private static System.Linq.Expressions.Expression InvokeArgument(System.Linq.Expressions.ParameterExpression leave, System.Linq.Expressions.LabelTarget target) =>
S.Invoke(Call(nameof(Receiver)), Argument(leave, target));
private static System.Linq.Expressions.Expression<Func<bool, int>> Tree(System.Linq.Expressions.ParameterExpression leave,
System.Linq.Expressions.LabelTarget target, System.Linq.Expressions.Expression body) =>
S.Lambda<Func<bool, int>>(S.Block(body, S.Label(target, S.Constant(8))), leave);

public void Method_arguments_spill_across_nested_invocation_jump(TestContext t)
{
var leave = S.Parameter(typeof(bool)); var target = S.Label(typeof(int));
Verify(t, Tree(leave, target, Call(nameof(Combine), Call(nameof(First)), InvokeArgument(leave, target))));
}
public void Constructor_arguments_spill_across_nested_invocation_jump(TestContext t)
{
var leave = S.Parameter(typeof(bool)); var target = S.Label(typeof(int));
Verify(t, Tree(leave, target, S.New(typeof(Pair).GetConstructor(new[] { typeof(int), typeof(int) }),
Call(nameof(First)), InvokeArgument(leave, target))));
}
public void Direct_jump_arguments_spill(TestContext t)
{
var leave = S.Parameter(typeof(bool)); var target = S.Label(typeof(int));
var argument = Argument(leave, target);
Verify(t, Tree(leave, target, Call(nameof(Combine), Call(nameof(First)), argument)));
Verify(t, Tree(leave, target, S.New(typeof(Pair).GetConstructor(new[] { typeof(int), typeof(int) }),
Call(nameof(First)), argument)));
}
public void Byref_alias_survives_nested_jump_spilling(TestContext t)
{
var leave = S.Parameter(typeof(bool)); var target = S.Label(typeof(int)); var value = S.Variable(typeof(int));
var tree = S.Lambda<Func<bool, int>>(S.Block(new[] { value }, S.Assign(value, S.Constant(5)),
Call(nameof(Write), value, InvokeArgument(leave, target)), S.Label(target, value)), leave);
VerifyByref(t, tree, false);
}

public void Earlier_jump_in_argument_block_survives_later_call_collection(TestContext t)
{
var leave = S.Parameter(typeof(bool)); var target = S.Label(typeof(int));
var argument = S.Block(Argument(leave, target), Call(nameof(Pass), S.Constant(3)));
Verify(t, Tree(leave, target, Call(nameof(Combine), Call(nameof(First)), argument)));
Verify(t, Tree(leave, target, S.New(typeof(Pair).GetConstructor(new[] { typeof(int), typeof(int) }),
Call(nameof(First)), argument)));
}
private static void VerifyByref(TestContext t, System.Linq.Expressions.Expression<Func<bool, int>> tree, bool hasFinally)
{
#if NETFRAMEWORK
// Framework's stack spiller rejects by-ref calls with jump/EH arguments.
// Keep the FEC value, alias write and evaluation-order assertions active.
t.Throws<NotSupportedException>(() => tree.Compile());
#else
Verify(t, tree);
#endif
foreach (var leave in new[] { true, false })
foreach (var flags in new[] { CompilerFlags.Default, CompilerFlags.NoInvocationLambdaInlining,
CompilerFlags.EnableDelegateDebugInfo | CompilerFlags.ThrowOnNotSupportedExpression })
{
var fast = tree.FromSysExpression().CompileFast(true, flags);
if (!t.IsNotNull(fast)) return;
Trace = "";
t.AreEqual(leave ? 7 : 8, fast(leave));
t.AreEqual(leave ? (hasFinally ? "receiver;finally;" : "receiver;") :
(hasFinally ? "receiver;pass;finally;write;" : "receiver;pass;write;"), Trace);
}
}
private static void Verify(TestContext t, System.Linq.Expressions.Expression<Func<bool, int>> tree)
{
var system = tree.Compile();
foreach (var leave in new[] { true, false })
{
Trace = ""; var expected = system(leave); var trace = Trace;
foreach (var flags in new[] { CompilerFlags.Default, CompilerFlags.NoInvocationLambdaInlining,
CompilerFlags.EnableDelegateDebugInfo | CompilerFlags.ThrowOnNotSupportedExpression })
{
var fast = tree.FromSysExpression().CompileFast(true, flags);
if (!t.IsNotNull(fast)) return;
Trace = ""; t.AreEqual(expected, fast(leave)); t.AreEqual(trace, Trace);
}
}
}
}
Loading