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
21 changes: 21 additions & 0 deletions src/FastExpressionCompiler/FastExpressionCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7655,6 +7655,27 @@ private static bool IsAtomicArithmeticOperand(Expression operand)

private static bool TryEmitArithmetic(Expression left, Expression right, ExpressionType nodeType, Type exprType,
ILGenerator il, ref CompilerContext context, ParentFlags parent)
{
// Keep ordinary primitive arithmetic out of the lifted/spill emission frame.
if (right != null)
{
var leftType = left.Type;
var rightType = right.Type;
if (leftType.IsPrimitive && rightType.IsPrimitive &&
(context.TryExpressionCount == 0 || IsAtomicArithmeticOperand(right)) && !right.IsComplexExpression())
{
var flags = (parent & ~OperandParentFlagsToClear) | ParentFlags.Arithmetic;
if (!TryEmit(left, il, ref context, flags) || !TryEmit(right, il, ref context, flags) ||
!TryEmitArithmeticOperation(leftType, rightType, nodeType, exprType, il)) return false;
context.LastEmitIsAddress = false;
return il.EmitPopIfIgnoreResult(parent);
}
}
return TryEmitLiftedOrComplexArithmetic(left, right, nodeType, exprType, il, ref context, parent);
}

private static bool TryEmitLiftedOrComplexArithmetic(Expression left, Expression right, ExpressionType nodeType, Type exprType,
ILGenerator il, ref CompilerContext context, ParentFlags parent)
{
var flags = (parent
& ~OperandParentFlagsToClear)
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 @@ -35,6 +35,7 @@ static void RunLightExpressionTests(object state)

var t = (LightExpression.TestRun)state;
t.Run(new LightExpression.UnitTests.InvokeRefArgumentRegressionTests());
t.Run(new LightExpression.UnitTests.ScalarArithmeticFrameTests());
t.Run(new LightExpression.UnitTests.StructInterfaceSettersTests());
t.Run(new LightExpression.UnitTests.NullableBoxingRegressionTests());
t.Run(new LightExpression.UnitTests.LocalReturnTargetTests());
Expand Down Expand Up @@ -136,6 +137,7 @@ static void RunLightExpressionTests(object state)

var fecTests = new TestRun();
fecTests.Run(new InvokeRefArgumentRegressionTests());
fecTests.Run(new ScalarArithmeticFrameTests());
fecTests.Run(new StructInterfaceSettersTests());
fecTests.Run(new NullableBoxingRegressionTests());
fecTests.Run(new LocalReturnTargetTests());
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 @@ -64,6 +64,7 @@ public static void Main(string[] args)
{
var ltStopwatch = Stopwatch.StartNew();
lt.Run(new LightExpression.UnitTests.InvokeRefArgumentRegressionTests());
lt.Run(new LightExpression.UnitTests.ScalarArithmeticFrameTests());
lt.Run(new LightExpression.UnitTests.StructInterfaceSettersTests());
lt.Run(new LightExpression.UnitTests.NullableBoxingRegressionTests());
lt.Run(new LightExpression.UnitTests.LocalReturnTargetTests());
Expand Down Expand Up @@ -162,6 +163,7 @@ public static void Main(string[] args)

var stStopwatch = Stopwatch.StartNew();
st.Run(new InvokeRefArgumentRegressionTests());
st.Run(new ScalarArithmeticFrameTests());
st.Run(new StructInterfaceSettersTests());
st.Run(new NullableBoxingRegressionTests());
st.Run(new LocalReturnTargetTests());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using S = System.Linq.Expressions.Expression;
#if LIGHT_EXPRESSION
namespace FastExpressionCompiler.LightExpression.UnitTests;
#else
namespace FastExpressionCompiler.UnitTests;
#endif

public struct ScalarArithmeticFrameTests : ITestX
{
public void Run(TestRun t) { Scalar_and_lifted_paths_preserve_results(t); Earlier_finalizers_remain_observable(t); }
public void Scalar_and_lifted_paths_preserve_results(TestContext t)
{
var a=S.Parameter(typeof(int));var b=S.Parameter(typeof(int));
foreach(var operation in new[]{System.Linq.Expressions.ExpressionType.Add,System.Linq.Expressions.ExpressionType.Subtract,System.Linq.Expressions.ExpressionType.Multiply,System.Linq.Expressions.ExpressionType.Divide})
{
var tree=S.Lambda<Func<int,int,int>>(S.MakeBinary(operation,a,b),a,b);var fast=tree.FromSysExpression().CompileFast(true);if(!t.IsNotNull(fast))return;
foreach(var x in new[]{-7,0,7})t.AreEqual(tree.Compile()(x,3),fast(x,3));
}
var n=S.Parameter(typeof(int?));var lifted=S.Lambda<Func<int?,int?>>(S.Add(n,S.Constant((int?)3,typeof(int?))),n);var f=lifted.FromSysExpression().CompileFast(true);if(!t.IsNotNull(f))return;
foreach(var x in new int?[]{null,7})t.AreEqual(lifted.Compile()(x),f(x));
}
public void Earlier_finalizers_remain_observable(TestContext t)
{
var n=S.Parameter(typeof(int));var tree=S.Lambda<Func<int,int>>(S.Add(S.TryFinally(n,S.Assign(n,S.Constant(7))),n),n);
var fast=tree.FromSysExpression().CompileFast(true);if(t.IsNotNull(fast))t.AreEqual(tree.Compile()(3),fast(3));
}
}
Loading