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
12 changes: 9 additions & 3 deletions src/FastExpressionCompiler/FastExpressionCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3641,7 +3641,7 @@ public static bool TryEmitParameter(ParameterExpression paramExpr, ILGenerator i
// Load the variable from the NonPassedParams array
EmitLoadConstantInt(il, varIndexInNonPassedParams);
il.Demit(OpCodes.Ldelem_Ref);
return TryEmitCapturedParameterValue(il, paramType, isPassedRef, ref context);
return TryEmitCapturedParameterValue(il, paramType, isPassedRef, parent, ref context);
}
}
}
Expand Down Expand Up @@ -3779,11 +3779,17 @@ public static bool TryEmitParameter(ParameterExpression paramExpr, ILGenerator i
il.Demit(OpCodes.Ldfld, ArrayClosureWithNonPassedParamsField);
EmitLoadConstantInt(il, nonPassedParamIndex);
il.Demit(OpCodes.Ldelem_Ref);
return TryEmitCapturedParameterValue(il, paramType, isPassedRef, ref context);
return TryEmitCapturedParameterValue(il, paramType, isPassedRef, parent, ref context);
}

private static bool TryEmitCapturedParameterValue(ILGenerator il, Type type, bool byRef, ref CompilerContext context)
private static bool TryEmitCapturedParameterValue(ILGenerator il, Type type, bool byRef, ParentFlags parent, ref CompilerContext context)
{
// Captured nonnullable structs already live in a writable box. Member and
// instance access must use that storage rather than a temporary copy.
byRef |= type.IsValueType && !type.IsNullable() &&
(parent & (ParentFlags.MemberAccess | ParentFlags.InstanceAccess)) != 0 &&
((parent & ParentFlags.IndexAccess) == 0 ||
(parent & (ParentFlags.Call | ParentFlags.MemberAccess)) == 0);
if (byRef)
{
// Nullable boxes and reference slots do not expose writable storage of this type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,7 @@ public void Test_struct_parameter_in_closure_of_the_nested_lambda()
OpCodes.Ldfld, // ArrayClosureWithNonPassedParams.NonPassedParams
OpCodes.Ldc_I4_0,
OpCodes.Ldelem_Ref,
OpCodes.Unbox_Any, // NotifyModel
OpCodes.Stloc_0,
OpCodes.Ldloca_S, // 0
OpCodes.Unbox, // address of the captured NotifyModel
OpCodes.Call, // NotifyModel.get_Number1
OpCodes.Ret
);
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 @@ -49,6 +49,7 @@ static void RunLightExpressionTests(object state)
t.Run(new LightExpression.UnitTests.DelegateExceptionArgumentsTests());
t.Run(new LightExpression.UnitTests.MultidimensionalGetStorageRegressionTests());
t.Run(new LightExpression.UnitTests.AbstractConstructorRegressionTests());
t.Run(new LightExpression.UnitTests.ArithmeticValueOperandTests());
t.Run(new LightExpression.UnitTests.AssignmentNullCheckTests());
t.Run(new LightExpression.UnitTests.DirectLogicalFactoryRegressionTests());
t.Run(new LightExpression.UnitTests.IntrinsicConstructorSpillsTests());
Expand Down Expand Up @@ -151,6 +152,7 @@ static void RunLightExpressionTests(object state)
fecTests.Run(new DelegateExceptionArgumentsTests());
fecTests.Run(new MultidimensionalGetStorageRegressionTests());
fecTests.Run(new AbstractConstructorRegressionTests());
fecTests.Run(new ArithmeticValueOperandTests());
fecTests.Run(new AssignmentNullCheckTests());
fecTests.Run(new DirectLogicalFactoryRegressionTests());
fecTests.Run(new IntrinsicConstructorSpillsTests());
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 @@ -78,6 +78,7 @@ public static void Main(string[] args)
lt.Run(new LightExpression.UnitTests.DelegateExceptionArgumentsTests());
lt.Run(new LightExpression.UnitTests.MultidimensionalGetStorageRegressionTests());
lt.Run(new LightExpression.UnitTests.AbstractConstructorRegressionTests());
lt.Run(new LightExpression.UnitTests.ArithmeticValueOperandTests());
lt.Run(new LightExpression.UnitTests.AssignmentNullCheckTests());
lt.Run(new LightExpression.UnitTests.DirectLogicalFactoryRegressionTests());
lt.Run(new LightExpression.UnitTests.IntrinsicConstructorSpillsTests());
Expand Down Expand Up @@ -177,6 +178,7 @@ public static void Main(string[] args)
st.Run(new DelegateExceptionArgumentsTests());
st.Run(new MultidimensionalGetStorageRegressionTests());
st.Run(new AbstractConstructorRegressionTests());
st.Run(new ArithmeticValueOperandTests());
st.Run(new AssignmentNullCheckTests());
st.Run(new DirectLogicalFactoryRegressionTests());
st.Run(new IntrinsicConstructorSpillsTests());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using S = System.Linq.Expressions.Expression;
#if LIGHT_EXPRESSION
namespace FastExpressionCompiler.LightExpression.UnitTests;
#else
namespace FastExpressionCompiler.UnitTests;
#endif

public struct ArithmeticValueOperandTests : ITestX
{
public struct Number { public int Value; public static Number operator +(Number a, Number b) => new Number { Value = a.Value + b.Value }; }
public void Run(TestRun t) { Operator_temporaries_keep_value_operands(t); Writable_capture_field_keeps_its_address(t); }
public void Operator_temporaries_keep_value_operands(TestContext t)
{
var value=S.Parameter(typeof(Number));
var tree=S.Lambda<Func<Number,Func<int>>>(S.Lambda<Func<int>>(S.Assign(
S.Field(S.Add(value,value),nameof(Number.Value)),S.Constant(17))),value);
foreach(var flags in new[]{CompilerFlags.Default,CompilerFlags.NoInvocationLambdaInlining,CompilerFlags.EnableDelegateDebugInfo})
{
var fast=tree.FromSysExpression().CompileFast(true,flags);if(!t.IsNotNull(fast))return;
t.AreEqual(17,fast(new Number{Value=7})());t.AreEqual(17,tree.Compile()(new Number{Value=7})());
}
}
public void Writable_capture_field_keeps_its_address(TestContext t)
{
var value=S.Parameter(typeof(Number));var field=S.Field(value,nameof(Number.Value));
var right=S.Field(S.Add(value,value),nameof(Number.Value));
var tree=S.Lambda<Func<Number,Func<int>>>(S.Lambda<Func<int>>(S.AddAssign(field,right)),value);
var control=S.Lambda<Func<Number,Func<int>>>(S.Lambda<Func<int>>(S.Assign(field,S.Add(field,right))),value);
foreach(var flags in new[]{CompilerFlags.Default,CompilerFlags.NoInvocationLambdaInlining,CompilerFlags.EnableDelegateDebugInfo})
{
var fast=tree.FromSysExpression().CompileFast(true,flags);if(!t.IsNotNull(fast))return;
var actual=fast(new Number{Value=7});var expected=control.Compile()(new Number{Value=7});
t.AreEqual(expected(),actual());t.AreEqual(expected(),actual());
}
}
}
Loading