Skip to content
Closed
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
2 changes: 1 addition & 1 deletion src/FastExpressionCompiler/FastExpressionCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6970,7 +6970,7 @@ private static bool TryEmitArithmeticOperation(Type leftType, Type rightType, Ex
ExpressionType.SubtractChecked => exprType.IsUnsigned() ? OpCodes.Sub_Ovf_Un : OpCodes.Sub_Ovf,
ExpressionType.Multiply => OpCodes.Mul,
ExpressionType.MultiplyChecked => exprType.IsUnsigned() ? OpCodes.Mul_Ovf_Un : OpCodes.Mul_Ovf,
ExpressionType.Divide => OpCodes.Div,
ExpressionType.Divide => exprType.IsUnsigned() ? OpCodes.Div_Un : OpCodes.Div,
ExpressionType.Modulo => exprType.IsUnsigned() ? OpCodes.Rem_Un : OpCodes.Rem,
ExpressionType.And => OpCodes.And,
ExpressionType.Or => OpCodes.Or,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Use unsigned division for uint and ulong expressions

Emit div.un instead of div for unsigned arithmetic. Values above the signed range currently produce incorrect quotients or overflow. The existing arithmetic dispatch and closure layout are unchanged.

One production line. Three regression methods per expression flavor cover uint/ulong, signed-boundary values, divide-by-zero, nullable operands, and compound assignment.

Independent extraction from #558, based directly on `dadhi/master` `6702eab6`. It requires neither #558 nor either of the other standalone fixes. All six merge orders of the three standalone fixes were checked without conflicts. #558 is unchanged.

## Validation

Focused regressions: **6 methods passed per runtime**, covering Standard and LightExpression on .NET 8, .NET 10, and .NET Framework 4.7.2. Loading the untouched master compiler with the same regression assembly reproduces the failure on .NET 8. All three Release solution builds pass with zero warnings/errors.

Full .NET 8/.NET 10 native runners were also executed. Each reports the same existing failure in `LightExpressionTests.Binary_operators_with_methods_and_nullable_operands_are_the_same_as_system` (two nullable MultiplyChecked rows produce OverflowException). The identical failure reproduces on untouched master; no new full-run failures were observed. No tests are skipped or weakened to obtain this result.

## Performance and allocations

Windows ARM64, Release, SDK 10.0.301. Before and after compiler assemblies are loaded in separate AssemblyLoadContexts in the same process. Nine alternating-order samples per phase, common operation counts, approximately 35 ms per side, warmed delegates, tiering disabled, and the process pinned to one available logical CPU. GC runs outside samples and a no-GC region isolates allocation throughput. Compilation reuses prebuilt expression trees; execution reuses compiled delegates. This measures CompileFast and delegate invocation, not tree construction. Ratios for previously incorrect/invalid cases are intentionally not claimed.

Allocation counts are unchanged on every shared workload measured, for both compilation and invocation. The raw timing samples and all unchanged controls are retained: small timing differences and code-placement/host variation do not establish universal throughput parity. These are correctness fixes with no new runtime helper calls or storage allocations; no broad performance improvement is claimed.

| Runtime / flavor / workload | Compile ns, master → fix | Invoke ns, master → fix | Compile B/op | Invoke B/op |
| --- | ---: | ---: | ---: | ---: |
| net8.0 / Standard / uint_divide | 1131.1 → 1170.4 | 2.10 → 1.89 | 880 → 880 | 0 → 0 |
| net8.0 / Standard / ulong_divide | 1317.4 → 1287.6 | 2.63 → 2.26 | 880 → 880 | 0 → 0 |
| net8.0 / Light / uint_divide | 1108.9 → 1091.1 | 2.16 → 1.90 | 880 → 880 | 0 → 0 |
| net8.0 / Light / ulong_divide | 1028.7 → 1119.7 | 1.78 → 1.63 | 880 → 880 | 0 → 0 |
| net10.0 / Standard / uint_divide | 1244.6 → 1207.0 | 2.18 → 1.93 | 880 → 880 | 0 → 0 |
| net10.0 / Standard / ulong_divide | 1336.8 → 1241.1 | 1.87 → 1.56 | 880 → 880 | 0 → 0 |
| net10.0 / Light / uint_divide | 1085.7 → 1034.5 | 2.02 → 1.86 | 880 → 880 | 0 → 0 |
| net10.0 / Light / ulong_divide | 1142.4 → 1061.5 | 1.92 → 1.55 | 880 → 880 | 0 → 0 |

No affected row has a paired-sample bootstrap interval wholly above parity. These descriptive intervals, stored with the raw data, are based on nine temporal samples and are not a universal performance guarantee.

Raw samples, harness sources, source hashes and validation logs are committed in `test/FastExpressionCompiler.Benchmarks/results/standalone-unsigned-division/evidence.zip`. Earlier separate-process timing attempts had unstable unchanged controls; their raw results are retained as exploratory evidence and are not the final paired comparison.
Binary file not shown.
2 changes: 2 additions & 0 deletions test/FastExpressionCompiler.TestsRunner.Net472/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ void Run(Func<int> run, string name = null)

var unitTests = Task.Run(() =>
{
Run(new UnsignedDivisionRegressionTests().Run);
Run(new ArithmeticOperationsTests().Run);
Run(new LightExpression.UnitTests.UnsignedDivisionRegressionTests().Run);
Run(new LightExpression.UnitTests.ArithmeticOperationsTests().Run);
Run(new AssignTests().Run);
Run(new LightExpression.UnitTests.AssignTests().Run);
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 @@ -119,7 +119,9 @@ void Run(Func<int> run, string name = null)

var unitTests = Task.Run(() =>
{
Run(new UnsignedDivisionRegressionTests().Run);
Run(new ArithmeticOperationsTests().Run);
Run(new LightExpression.UnitTests.UnsignedDivisionRegressionTests().Run);
Run(new LightExpression.UnitTests.ArithmeticOperationsTests().Run);
Run(new AssignTests().Run);
Run(new LightExpression.UnitTests.AssignTests().Run);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using System.Reflection;
using SysExpr = System.Linq.Expressions.Expression;

#if LIGHT_EXPRESSION
namespace FastExpressionCompiler.LightExpression.UnitTests;
#else
namespace FastExpressionCompiler.UnitTests;
#endif

public struct UnsignedDivisionRegressionTests : ITest
{
public int Run()
{
Unsigned_division_uses_unsigned_operands();
Lifted_unsigned_division_uses_unsigned_operands();
Unsigned_division_assignment_uses_unsigned_operands();
return 3;
}

public void Unsigned_division_uses_unsigned_operands() => Check(false, false);
public void Lifted_unsigned_division_uses_unsigned_operands() => Check(true, false);
public void Unsigned_division_assignment_uses_unsigned_operands() => Check(false, true);

private static void Check(bool nullable, bool assign)
{
Type[] types = { typeof(uint), typeof(ulong) };
object[][] values = { new object[] { 0u, 1u, 2u, (uint)int.MaxValue + 1, uint.MaxValue },
new object[] { 0UL, 1UL, 2UL, (ulong)long.MaxValue + 1, ulong.MaxValue } };
for (var i = 0; i < types.Length; ++i)
{
var type = nullable ? typeof(Nullable<>).MakeGenericType(types[i]) : types[i];
var left = SysExpr.Parameter(type);
var right = SysExpr.Parameter(type);
var body = assign ? SysExpr.DivideAssign(left, right) : SysExpr.Divide(left, right);
var expression = SysExpr.Lambda(body, left, right);
var system = expression.Compile();
var fast = expression.FromSysExpression().CompileFast(true);
Asserts.IsNotNull(fast);
foreach (var a in values[i])
foreach (var b in values[i]) Asserts.AreEqual(Invoke(system, a, b), Invoke(fast, a, b));
if (nullable)
foreach (var value in values[i])
{
Asserts.AreEqual(Invoke(system, value, null), Invoke(fast, value, null));
Asserts.AreEqual(Invoke(system, null, value), Invoke(fast, null, value));
}
}
}

private static object Invoke(Delegate function, object a, object b)
{
try { return function.DynamicInvoke(new[] { a, b }); }
catch (TargetInvocationException e) { return e.InnerException.GetType(); }
}
}
Loading