Repository navigation
Expand file tree
/
Copy pathNDExpr.cs
More file actions
1320 lines (1155 loc) · 59.7 KB
/
Copy pathNDExpr.cs
File metadata and controls
1320 lines (1155 loc) · 59.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Reflection.Emit;
using System.Text;
using NumSharp.Backends.Kernels;
// =============================================================================
// NDExpr.cs — Expression DSL (Tier 3C of the custom-op API)
// =============================================================================
//
// A small algebraic AST over NDIter operands. Compiles to an
// NDInnerLoopFunc by emitting (scalarBody, vectorBody) pairs that
// DirectILKernelGenerator.CompileInnerLoop wraps in the standard 4× unroll shell.
//
// TYPE DISCIPLINE — TWO MODES
// ---------------------------
// Legacy mode (Compile): all intermediate computation happens in the output
// dtype. Input loads auto-promote to output dtype; constants are pushed as
// output dtype. Simple, but does NOT match NumPy for mixed-dtype trees
// (NumPy computes each ufunc node at its own result_type — int32*int32
// wraps in int32 even when the final result is float64).
//
// NumPy mode (CompileNumPy, used by np.evaluate): a typing pass resolves
// every node to its NumPy 2.x result_type (NEP50, incl. weak python-scalar
// semantics for constants); emission computes each node at its resolved
// dtype and converts at node edges. See NDExpr.Typing.cs.
//
// For fine-grained type control, use ExecuteElementWise directly (Tier 3B).
//
// SIMD
// ----
// The vector path is enabled iff every input type equals the output type
// (in NumPy mode: the whole tree resolves homogeneous) AND every node's op
// supports SIMD. Otherwise the compiled kernel carries a scalar-only body;
// the factory's strided fallback handles all cases.
//
// =============================================================================
namespace NumSharp.Backends.Iteration
{
/// <summary>
/// Abstract expression node. Subclasses describe computations over
/// NDIter operands; Compile() produces an NDInnerLoopFunc.
/// </summary>
public abstract partial class NDExpr
{
// ----- Contract (internal API used by the compiler) -----
/// <summary>
/// Emit scalar code. On exit, the evaluation stack must have exactly
/// one value of dtype <c>ctx.OutputType</c>.
/// </summary>
public abstract void EmitScalar(ILGenerator il, NDExprCompileContext ctx);
/// <summary>
/// Emit vector code. On exit, the evaluation stack must have exactly
/// one <c>Vector{W}<T></c> of element type <c>ctx.OutputType</c>.
/// Called only when <see cref="SupportsSimd"/> is true and all input
/// types equal the output type.
/// </summary>
public abstract void EmitVector(ILGenerator il, NDExprCompileContext ctx);
/// <summary>
/// True if this node and its entire sub-tree have a SIMD emit path. Structural and
/// type-independent — prefer <see cref="SupportsSimdAt"/> for the compile decision, since
/// some ops only vectorize at certain element types/runtimes.
/// </summary>
public abstract bool SupportsSimd { get; }
/// <summary>
/// Whether this node and its sub-tree have a SIMD emit path WHEN every operand and the
/// output share element type <paramref name="t"/> (the compiler only vectorizes a tree when
/// all its types are equal — see <see cref="Compile"/>). Defaults to the structural
/// <see cref="SupportsSimd"/>; nodes whose SIMD-ability is type- or runtime-dependent
/// override this. The key case: rounding ops (Floor/Ceil/Round/Truncate) bind a per-type
/// <c>Vector{N}</c> BCL method that exists only for float/double on a capable runtime, so
/// vectorizing them at an integer type (or on a runtime without the method) would hit the
/// emitter's "Could not find ..." throw — this gate routes those to scalar instead.
/// </summary>
public virtual bool SupportsSimdAt(NPTypeCode t) => SupportsSimd;
/// <summary>
/// Stable structural signature. Used to derive a cache key when the
/// user doesn't supply one.
/// </summary>
public abstract void AppendSignature(StringBuilder sb);
// ----- Compilation -----
/// <summary>
/// Compile the tree to an <see cref="NDInnerLoopFunc"/>.
/// </summary>
public NDInnerLoopFunc Compile(
NPTypeCode[] inputTypes, NPTypeCode outputType, string? cacheKey)
{
if (inputTypes is null) throw new ArgumentNullException(nameof(inputTypes));
string key = cacheKey ?? DeriveCacheKey(inputTypes, outputType);
int nIn = inputTypes.Length;
// Type-aware: a tree only vectorizes when every operand and the output share a type
// (AllEqual), so SupportsSimdAt is evaluated at that single shared type. This catches
// ops that vectorize only at certain element types/runtimes (e.g. rounding ops, which
// have no Vector{N} method for integers or on pre-.NET-9 runtimes).
bool wantSimd = AllEqual(inputTypes, outputType) && SupportsSimdAt(outputType);
Action<ILGenerator> scalarBody = il =>
{
// Shell delivers N inputs on stack: stack[bottom]=in0, stack[top]=inN-1.
// Stash each into a local (reverse order since we pop top first).
var scalarLocals = new LocalBuilder[nIn];
for (int i = nIn - 1; i >= 0; i--)
{
scalarLocals[i] = il.DeclareLocal(DirectILKernelGenerator.GetClrType(inputTypes[i]));
il.Emit(OpCodes.Stloc, scalarLocals[i]);
}
var ctx = new NDExprCompileContext(inputTypes, outputType, scalarLocals, vectorMode: false);
EmitScalar(il, ctx);
// Stack now: [result : outputType] — factory stores it.
};
Action<ILGenerator>? vectorBody = null;
if (wantSimd)
{
vectorBody = il =>
{
var vectorLocals = new LocalBuilder[nIn];
var vecType = DirectILKernelGenerator.GetVectorType(DirectILKernelGenerator.GetClrType(inputTypes[0]));
for (int i = nIn - 1; i >= 0; i--)
{
vectorLocals[i] = il.DeclareLocal(vecType);
il.Emit(OpCodes.Stloc, vectorLocals[i]);
}
var ctx = new NDExprCompileContext(inputTypes, outputType, vectorLocals, vectorMode: true);
EmitVector(il, ctx);
};
}
var operandTypes = new NPTypeCode[nIn + 1];
Array.Copy(inputTypes, operandTypes, nIn);
operandTypes[nIn] = outputType;
return DirectILKernelGenerator.CompileInnerLoop(operandTypes, scalarBody, vectorBody, key);
}
private protected string DeriveCacheKey(NPTypeCode[] inputTypes, NPTypeCode outputType)
{
var sb = new StringBuilder("NDExpr:");
AppendSignature(sb);
sb.Append(":in=");
for (int i = 0; i < inputTypes.Length; i++)
{
if (i > 0) sb.Append(',');
sb.Append(inputTypes[i]);
}
sb.Append(":out=").Append(outputType);
return sb.ToString();
}
private static bool AllEqual(NPTypeCode[] inputs, NPTypeCode output)
{
foreach (var t in inputs) if (t != output) return false;
return true;
}
// ===================================================================
// Leaf factories
// ===================================================================
/// <summary>Reference the i-th operand of the iterator (0-based input index).</summary>
public static NDExpr Input(int index) => new InputNode(index);
/// <summary>Push a constant of the given .NET type. Value is converted to the output dtype when evaluated.</summary>
public static NDExpr Const(double value) => new ConstNode(value);
public static NDExpr Const(float value) => new ConstNode(value);
public static NDExpr Const(long value) => new ConstNode(value);
public static NDExpr Const(int value) => new ConstNode(value);
// ===================================================================
// Binary factories
// ===================================================================
// Arithmetic
public static NDExpr Add(NDExpr a, NDExpr b) => new BinaryNode(BinaryOp.Add, a, b);
public static NDExpr Subtract(NDExpr a, NDExpr b) => new BinaryNode(BinaryOp.Subtract, a, b);
public static NDExpr Multiply(NDExpr a, NDExpr b) => new BinaryNode(BinaryOp.Multiply, a, b);
public static NDExpr Divide(NDExpr a, NDExpr b) => new BinaryNode(BinaryOp.Divide, a, b);
public static NDExpr Mod(NDExpr a, NDExpr b) => new BinaryNode(BinaryOp.Mod, a, b);
public static NDExpr Power(NDExpr a, NDExpr b) => new BinaryNode(BinaryOp.Power, a, b);
public static NDExpr FloorDivide(NDExpr a, NDExpr b) => new BinaryNode(BinaryOp.FloorDivide, a, b);
public static NDExpr ATan2(NDExpr y, NDExpr x) => new BinaryNode(BinaryOp.ATan2, y, x);
// Bitwise
public static NDExpr BitwiseAnd(NDExpr a, NDExpr b) => new BinaryNode(BinaryOp.BitwiseAnd, a, b);
public static NDExpr BitwiseOr(NDExpr a, NDExpr b) => new BinaryNode(BinaryOp.BitwiseOr, a, b);
public static NDExpr BitwiseXor(NDExpr a, NDExpr b) => new BinaryNode(BinaryOp.BitwiseXor, a, b);
// Scalar-branchy combinators compiled to IL
public static NDExpr Min(NDExpr a, NDExpr b) => new MinMaxNode(isMin: true, a, b);
public static NDExpr Max(NDExpr a, NDExpr b) => new MinMaxNode(isMin: false, a, b);
public static NDExpr Clamp(NDExpr x, NDExpr lo, NDExpr hi) => Min(Max(x, lo), hi);
public static NDExpr Where(NDExpr cond, NDExpr a, NDExpr b) => new WhereNode(cond, a, b);
// ===================================================================
// Unary factories
// ===================================================================
// Core arithmetic
public static NDExpr Sqrt(NDExpr x) => new UnaryNode(UnaryOp.Sqrt, x);
public static NDExpr Abs(NDExpr x) => new UnaryNode(UnaryOp.Abs, x);
public static NDExpr Negate(NDExpr x) => new UnaryNode(UnaryOp.Negate, x);
public static NDExpr Square(NDExpr x) => new UnaryNode(UnaryOp.Square, x);
public static NDExpr Reciprocal(NDExpr x) => new UnaryNode(UnaryOp.Reciprocal, x);
public static NDExpr Sign(NDExpr x) => new UnaryNode(UnaryOp.Sign, x);
public static NDExpr Cbrt(NDExpr x) => new UnaryNode(UnaryOp.Cbrt, x);
// Exp / Log family
public static NDExpr Exp(NDExpr x) => new UnaryNode(UnaryOp.Exp, x);
public static NDExpr Exp2(NDExpr x) => new UnaryNode(UnaryOp.Exp2, x);
public static NDExpr Expm1(NDExpr x) => new UnaryNode(UnaryOp.Expm1, x);
public static NDExpr Log(NDExpr x) => new UnaryNode(UnaryOp.Log, x);
public static NDExpr Log2(NDExpr x) => new UnaryNode(UnaryOp.Log2, x);
public static NDExpr Log10(NDExpr x) => new UnaryNode(UnaryOp.Log10, x);
public static NDExpr Log1p(NDExpr x) => new UnaryNode(UnaryOp.Log1p, x);
// Trigonometric
public static NDExpr Sin(NDExpr x) => new UnaryNode(UnaryOp.Sin, x);
public static NDExpr Cos(NDExpr x) => new UnaryNode(UnaryOp.Cos, x);
public static NDExpr Tan(NDExpr x) => new UnaryNode(UnaryOp.Tan, x);
public static NDExpr Sinh(NDExpr x) => new UnaryNode(UnaryOp.Sinh, x);
public static NDExpr Cosh(NDExpr x) => new UnaryNode(UnaryOp.Cosh, x);
public static NDExpr Tanh(NDExpr x) => new UnaryNode(UnaryOp.Tanh, x);
public static NDExpr ASin(NDExpr x) => new UnaryNode(UnaryOp.ASin, x);
public static NDExpr ACos(NDExpr x) => new UnaryNode(UnaryOp.ACos, x);
public static NDExpr ATan(NDExpr x) => new UnaryNode(UnaryOp.ATan, x);
public static NDExpr Asinh(NDExpr x) => new UnaryNode(UnaryOp.Asinh, x);
public static NDExpr Acosh(NDExpr x) => new UnaryNode(UnaryOp.Acosh, x);
public static NDExpr Atanh(NDExpr x) => new UnaryNode(UnaryOp.Atanh, x);
public static NDExpr Deg2Rad(NDExpr x) => new UnaryNode(UnaryOp.Deg2Rad, x);
public static NDExpr Rad2Deg(NDExpr x) => new UnaryNode(UnaryOp.Rad2Deg, x);
// Rounding
public static NDExpr Floor(NDExpr x) => new UnaryNode(UnaryOp.Floor, x);
public static NDExpr Ceil(NDExpr x) => new UnaryNode(UnaryOp.Ceil, x);
public static NDExpr Round(NDExpr x) => new UnaryNode(UnaryOp.Round, x);
public static NDExpr Truncate(NDExpr x) => new UnaryNode(UnaryOp.Truncate, x);
// Bitwise / logical
public static NDExpr BitwiseNot(NDExpr x) => new UnaryNode(UnaryOp.BitwiseNot, x);
public static NDExpr LogicalNot(NDExpr x) => new UnaryNode(UnaryOp.LogicalNot, x);
// Predicates (returns numeric 0/1 at output dtype — NumPy-compatible)
public static NDExpr IsNaN(NDExpr x) => new UnaryNode(UnaryOp.IsNan, x);
public static NDExpr IsFinite(NDExpr x) => new UnaryNode(UnaryOp.IsFinite, x);
public static NDExpr IsInf(NDExpr x) => new UnaryNode(UnaryOp.IsInf, x);
// ===================================================================
// Comparison factories (produce 0/1 at output dtype)
// ===================================================================
public static NDExpr Equal(NDExpr a, NDExpr b) => new ComparisonNode(ComparisonOp.Equal, a, b);
public static NDExpr NotEqual(NDExpr a, NDExpr b) => new ComparisonNode(ComparisonOp.NotEqual, a, b);
public static NDExpr Less(NDExpr a, NDExpr b) => new ComparisonNode(ComparisonOp.Less, a, b);
public static NDExpr LessEqual(NDExpr a, NDExpr b) => new ComparisonNode(ComparisonOp.LessEqual, a, b);
public static NDExpr Greater(NDExpr a, NDExpr b) => new ComparisonNode(ComparisonOp.Greater, a, b);
public static NDExpr GreaterEqual(NDExpr a, NDExpr b) => new ComparisonNode(ComparisonOp.GreaterEqual, a, b);
// ===================================================================
// Call — invoke an arbitrary .NET delegate or MethodInfo per element.
// ===================================================================
//
// Three entry points:
// (a) Typed Func<...> overloads — allow passing method groups
// (e.g. `Math.Sqrt`, `Math.Pow`) without an explicit cast.
// C# overload resolution picks these when the compiler can infer
// the delegate signature from the method group.
//
// (b) `Call(Delegate func, params NDExpr[] args)` — catch-all for
// any pre-constructed delegate. Method groups will NOT bind to
// this directly (the C# compiler needs a specific delegate
// target type). Cast or use a typed Func<...> overload.
//
// (c) `Call(MethodInfo, ...)` and `Call(MethodInfo, object target, ...)`
// — bypass the delegate layer entirely. Static and instance methods
// respectively. Useful when reflecting over types at runtime.
//
// Implementation notes:
// * Static methods with no target are emitted as a direct `call`
// opcode to the underlying `MethodInfo` — no indirection.
// * Instance methods or delegates with captured state are stored in a
// process-wide slot dictionary (`DelegateSlots`). The emitted IL
// loads the delegate via an integer ID and invokes it through
// `Delegate.Invoke` (callvirt).
// * SIMD is always disabled for trees containing a CallNode.
// * Argument values are auto-converted from `ctx.OutputType` to each
// parameter's dtype; the return value is converted back to
// `ctx.OutputType` before leaving the node.
/// <summary>Invoke a static method (no target).</summary>
public static NDExpr Call(System.Reflection.MethodInfo method, params NDExpr[] args)
=> new CallNode(method, target: null, args);
/// <summary>Invoke an instance method on a target object.</summary>
public static NDExpr Call(System.Reflection.MethodInfo method, object target, params NDExpr[] args)
=> new CallNode(method, target, args);
/// <summary>Invoke any delegate. Method-group arguments need a typed Func overload; use a cast or the typed overloads below.</summary>
public static NDExpr Call(Delegate func, params NDExpr[] args)
=> new CallNode(func, args);
// Typed Func<...> overloads — enable `NDExpr.Call(Math.Sqrt, x)` without cast.
public static NDExpr Call<TR>(Func<TR> func)
=> new CallNode(func, Array.Empty<NDExpr>());
public static NDExpr Call<T1, TR>(Func<T1, TR> func, NDExpr a1)
=> new CallNode(func, new[] { a1 });
public static NDExpr Call<T1, T2, TR>(Func<T1, T2, TR> func, NDExpr a1, NDExpr a2)
=> new CallNode(func, new[] { a1, a2 });
public static NDExpr Call<T1, T2, T3, TR>(Func<T1, T2, T3, TR> func, NDExpr a1, NDExpr a2, NDExpr a3)
=> new CallNode(func, new[] { a1, a2, a3 });
public static NDExpr Call<T1, T2, T3, T4, TR>(Func<T1, T2, T3, T4, TR> func, NDExpr a1, NDExpr a2, NDExpr a3, NDExpr a4)
=> new CallNode(func, new[] { a1, a2, a3, a4 });
// ===================================================================
// Operator overloads (syntactic sugar)
// ===================================================================
public static NDExpr operator +(NDExpr a, NDExpr b) => Add(a, b);
public static NDExpr operator -(NDExpr a, NDExpr b) => Subtract(a, b);
public static NDExpr operator *(NDExpr a, NDExpr b) => Multiply(a, b);
public static NDExpr operator /(NDExpr a, NDExpr b) => Divide(a, b);
public static NDExpr operator %(NDExpr a, NDExpr b) => Mod(a, b);
public static NDExpr operator &(NDExpr a, NDExpr b) => BitwiseAnd(a, b);
public static NDExpr operator |(NDExpr a, NDExpr b) => BitwiseOr(a, b);
public static NDExpr operator ^(NDExpr a, NDExpr b) => BitwiseXor(a, b);
public static NDExpr operator -(NDExpr a) => Negate(a);
public static NDExpr operator ~(NDExpr a) => BitwiseNot(a);
public static NDExpr operator !(NDExpr a) => LogicalNot(a);
}
// =========================================================================
// Compile-time context shared with each node
// =========================================================================
public sealed class NDExprCompileContext
{
public NPTypeCode[] InputTypes { get; }
public NPTypeCode OutputType { get; }
public LocalBuilder[] InputLocals { get; }
public bool VectorMode { get; }
/// <summary>
/// Per-node resolved dtypes (NumPy result_type mode, produced by the
/// typing pass for <see cref="NDExpr.CompileNumPy"/>). Null in legacy
/// mode, where every node computes at <see cref="OutputType"/>.
/// Keyed by node reference.
/// </summary>
public IReadOnlyDictionary<NDExpr, NPTypeCode>? NodeTypes { get; }
public NDExprCompileContext(
NPTypeCode[] inputTypes, NPTypeCode outputType,
LocalBuilder[] inputLocals, bool vectorMode)
: this(inputTypes, outputType, inputLocals, vectorMode, null)
{
}
public NDExprCompileContext(
NPTypeCode[] inputTypes, NPTypeCode outputType,
LocalBuilder[] inputLocals, bool vectorMode,
IReadOnlyDictionary<NDExpr, NPTypeCode>? nodeTypes)
{
InputTypes = inputTypes;
OutputType = outputType;
InputLocals = inputLocals;
VectorMode = vectorMode;
NodeTypes = nodeTypes;
}
/// <summary>
/// The dtype this node computes at: its typing-pass entry in NumPy
/// mode, or the uniform <see cref="OutputType"/> in legacy mode.
/// </summary>
internal NPTypeCode TypeOf(NDExpr node)
=> NodeTypes is null
? OutputType
: NodeTypes.TryGetValue(node, out var t)
? t
: throw new InvalidOperationException(
$"NDExpr typing pass produced no dtype for node {node.GetType().Name} — typing/emission mismatch.");
}
// =========================================================================
// Node: Input(i) — reference operand i
// =========================================================================
public sealed partial class InputNode : NDExpr
{
private readonly int _index;
public InputNode(int index)
{
if (index < 0) throw new ArgumentOutOfRangeException(nameof(index));
_index = index;
}
internal int Index => _index;
public override bool SupportsSimd => true;
public override void EmitScalar(ILGenerator il, NDExprCompileContext ctx)
{
if (_index >= ctx.InputTypes.Length)
throw new InvalidOperationException(
$"Input({_index}) out of range; compile provided {ctx.InputTypes.Length} inputs.");
il.Emit(OpCodes.Ldloc, ctx.InputLocals[_index]);
// Leave this node's resolved dtype on the stack. Legacy mode
// resolves every node to OutputType (auto-promote at load);
// NumPy mode resolves to the operand's native dtype, so parents
// convert at their edges instead.
var inType = ctx.InputTypes[_index];
DirectILKernelGenerator.EmitConvertTo(il, inType, ctx.TypeOf(this));
}
public override void EmitVector(ILGenerator il, NDExprCompileContext ctx)
{
if (_index >= ctx.InputTypes.Length)
throw new InvalidOperationException(
$"Input({_index}) out of range; compile provided {ctx.InputTypes.Length} inputs.");
// Vector mode is only used when all input types == output type
// (enforced by Compile), so no conversion is needed here.
il.Emit(OpCodes.Ldloc, ctx.InputLocals[_index]);
}
public override void AppendSignature(StringBuilder sb)
=> sb.Append("In[").Append(_index).Append(']');
}
// =========================================================================
// Node: Constant
// =========================================================================
public sealed partial class ConstNode : NDExpr
{
// Store as double — widest scalar; convert down to outputType on emit.
// Also preserve an exact-int path for integer-typed outputs.
private readonly double _valueFp;
private readonly long _valueInt;
private readonly bool _isIntegerLiteral;
public ConstNode(double v) { _valueFp = v; _valueInt = 0; _isIntegerLiteral = false; }
public ConstNode(float v) { _valueFp = v; _valueInt = 0; _isIntegerLiteral = false; }
public ConstNode(long v) { _valueInt = v; _valueFp = v; _isIntegerLiteral = true; }
public ConstNode(int v) { _valueInt = v; _valueFp = v; _isIntegerLiteral = true; }
internal bool IsIntegerLiteral => _isIntegerLiteral;
internal long IntegerValue => _valueInt;
internal double FloatValue => _valueFp;
public override bool SupportsSimd => true;
public override void EmitScalar(ILGenerator il, NDExprCompileContext ctx)
{
EmitLoadTyped(il, ctx.TypeOf(this));
}
public override void EmitVector(ILGenerator il, NDExprCompileContext ctx)
{
EmitLoadTyped(il, ctx.TypeOf(this));
DirectILKernelGenerator.EmitVectorCreate(il, ctx.TypeOf(this));
}
private void EmitLoadTyped(ILGenerator il, NPTypeCode target)
{
switch (target)
{
case NPTypeCode.Single:
il.Emit(OpCodes.Ldc_R4, (float)_valueFp);
return;
case NPTypeCode.Double:
il.Emit(OpCodes.Ldc_R8, _valueFp);
return;
case NPTypeCode.Int64:
case NPTypeCode.UInt64:
il.Emit(OpCodes.Ldc_I8, _isIntegerLiteral ? _valueInt : (long)_valueFp);
return;
case NPTypeCode.Byte:
case NPTypeCode.SByte:
case NPTypeCode.Int16:
case NPTypeCode.UInt16:
case NPTypeCode.Int32:
case NPTypeCode.UInt32:
case NPTypeCode.Char:
case NPTypeCode.Boolean:
il.Emit(OpCodes.Ldc_I4, _isIntegerLiteral ? (int)_valueInt : (int)_valueFp);
return;
case NPTypeCode.Half:
// No Ldc for Half — load double then convert (Half consts
// arise from NEP50 weak adoption: f2_array + 2.5 stays f2).
il.Emit(OpCodes.Ldc_R8, _isIntegerLiteral ? _valueInt : _valueFp);
DirectILKernelGenerator.EmitConvertTo(il, NPTypeCode.Double, NPTypeCode.Half);
return;
case NPTypeCode.Decimal:
if (_isIntegerLiteral)
{
il.Emit(OpCodes.Ldc_I8, _valueInt);
DirectILKernelGenerator.EmitConvertTo(il, NPTypeCode.Int64, NPTypeCode.Decimal);
}
else
{
il.Emit(OpCodes.Ldc_R8, _valueFp);
DirectILKernelGenerator.EmitConvertTo(il, NPTypeCode.Double, NPTypeCode.Decimal);
}
return;
case NPTypeCode.Complex:
il.Emit(OpCodes.Ldc_R8, _isIntegerLiteral ? _valueInt : _valueFp);
DirectILKernelGenerator.EmitConvertTo(il, NPTypeCode.Double, NPTypeCode.Complex);
return;
default:
throw new NotSupportedException(
$"ConstNode cannot emit for output dtype {target}.");
}
}
public override void AppendSignature(StringBuilder sb)
{
sb.Append("Const[");
if (_isIntegerLiteral) sb.Append(_valueInt); else sb.Append(_valueFp);
sb.Append(']');
}
}
// =========================================================================
// Node: Binary op
// =========================================================================
public sealed partial class BinaryNode : NDExpr
{
private readonly BinaryOp _op;
private readonly NDExpr _left;
private readonly NDExpr _right;
public BinaryNode(BinaryOp op, NDExpr left, NDExpr right)
{
_op = op;
_left = left ?? throw new ArgumentNullException(nameof(left));
_right = right ?? throw new ArgumentNullException(nameof(right));
}
public override bool SupportsSimd
=> _left.SupportsSimd && _right.SupportsSimd && IsSimdOp(_op);
public override bool SupportsSimdAt(NPTypeCode t)
=> _left.SupportsSimdAt(t) && _right.SupportsSimdAt(t) && IsSimdOp(_op);
// Must match DirectILKernelGenerator.EmitVectorOperation's supported set.
// Mod, Power, FloorDivide, ATan2 are scalar-only.
private static bool IsSimdOp(BinaryOp op)
=> op == BinaryOp.Add || op == BinaryOp.Subtract ||
op == BinaryOp.Multiply || op == BinaryOp.Divide ||
op == BinaryOp.BitwiseAnd || op == BinaryOp.BitwiseOr ||
op == BinaryOp.BitwiseXor;
public override void EmitScalar(ILGenerator il, NDExprCompileContext ctx)
{
var my = ctx.TypeOf(this);
_left.EmitScalar(il, ctx);
DirectILKernelGenerator.EmitConvertTo(il, ctx.TypeOf(_left), my);
_right.EmitScalar(il, ctx);
DirectILKernelGenerator.EmitConvertTo(il, ctx.TypeOf(_right), my);
// NumPy's bool loops: add(bool,bool) is logical OR, multiply is
// logical AND. The generic Add opcode would store byte 2 for
// True+True, which is not a valid bool.
if (my == NPTypeCode.Boolean && _op == BinaryOp.Add)
{
il.Emit(OpCodes.Or);
return;
}
if (my == NPTypeCode.Boolean && _op == BinaryOp.Multiply)
{
il.Emit(OpCodes.And);
return;
}
DirectILKernelGenerator.EmitScalarOperation(il, _op, my);
}
public override void EmitVector(ILGenerator il, NDExprCompileContext ctx)
{
_left.EmitVector(il, ctx);
_right.EmitVector(il, ctx);
DirectILKernelGenerator.EmitVectorOperation(il, _op, ctx.OutputType);
}
public override void AppendSignature(StringBuilder sb)
{
sb.Append(_op).Append('(');
_left.AppendSignature(sb);
sb.Append(',');
_right.AppendSignature(sb);
sb.Append(')');
}
}
// =========================================================================
// Node: Unary op
// =========================================================================
public sealed partial class UnaryNode : NDExpr
{
private readonly UnaryOp _op;
private readonly NDExpr _child;
public UnaryNode(UnaryOp op, NDExpr child)
{
_op = op;
_child = child ?? throw new ArgumentNullException(nameof(child));
}
public override bool SupportsSimd
=> _child.SupportsSimd && IsSimdUnary(_op);
public override bool SupportsSimdAt(NPTypeCode t)
=> _child.SupportsSimdAt(t) && IsSimdUnaryAt(_op, t);
// Type/runtime-aware refinement of IsSimdUnary. The rounding family binds a per-type
// Vector{N} BCL method that exists only for float/double on a capable runtime (Floor/Ceil
// .NET 7+, Round/Truncate .NET 9+), so its SIMD-ability depends on the element type AND the
// running runtime — defer to the shared capability gate. This is what lets the fused path
// vectorize Round/Truncate on .NET 9+ while (a) staying scalar on net8.0 and (b) NOT
// crashing on integer rounding, where the typing keeps the dtype and there is no
// Vector{N}.Floor(int) (the gate returns false -> scalar identity emit).
// exp/log/sin/cos are likewise type+host-gated: NDFloatMath's vector overloads are float32-only and need
// hardware FMA, so the fused path must defer to the same capability gate the direct kernel
// uses (at f8/f2 or without FMA it stays scalar — same bits, one lane at a time).
private static bool IsSimdUnaryAt(UnaryOp op, NPTypeCode t)
=> IsRoundingOp(op)
? DirectILKernelGenerator.RoundingVectorSimdAvailable(op, t)
: (op == UnaryOp.Exp || op == UnaryOp.Log ||
op == UnaryOp.Sin || op == UnaryOp.Cos || op == UnaryOp.Tanh)
? DirectILKernelGenerator.NumPyFloatKernelSimdAvailable(op, t)
: IsSimdUnary(op);
// Structural SIMD set used by the type-independent SupportsSimd. The rounding family
// (Floor/Ceil/Round/Truncate) is gated per type+runtime by IsSimdUnaryAt instead, so it is
// omitted here — SupportsSimdAt is the gate the compiler actually consults.
private static bool IsSimdUnary(UnaryOp op)
=> op == UnaryOp.Negate || op == UnaryOp.Abs || op == UnaryOp.Sqrt ||
op == UnaryOp.Square || op == UnaryOp.Reciprocal ||
op == UnaryOp.Deg2Rad || op == UnaryOp.Rad2Deg || op == UnaryOp.BitwiseNot;
// Predicates leave a bool (I4 0/1) on the stack — not outputType. The wrapper
// below converts to outputType so the factory's Stind matches.
private static bool IsPredicateResult(UnaryOp op)
=> op == UnaryOp.IsNan || op == UnaryOp.IsFinite || op == UnaryOp.IsInf;
// NumPy preserves integer dtypes through floor/ceil/round/trunc — the op
// is an identity there (and Math.Floor has no integer overloads to call).
private static bool IsRoundingOp(UnaryOp op)
=> op == UnaryOp.Floor || op == UnaryOp.Ceil ||
op == UnaryOp.Round || op == UnaryOp.Truncate;
private static bool IsIntegerKind(NPTypeCode t)
=> t == NPTypeCode.Boolean || t == NPTypeCode.Byte || t == NPTypeCode.SByte ||
t == NPTypeCode.Int16 || t == NPTypeCode.UInt16 || t == NPTypeCode.Char ||
t == NPTypeCode.Int32 || t == NPTypeCode.UInt32 ||
t == NPTypeCode.Int64 || t == NPTypeCode.UInt64;
public override void EmitScalar(ILGenerator il, NDExprCompileContext ctx)
{
var my = ctx.TypeOf(this);
var childType = ctx.TypeOf(_child);
// LogicalNot needs a special path. DirectILKernelGenerator's emit uses Ldc_I4_0+Ceq
// which is only correct when the input value fits in I4 (Int32 and narrower).
// For Int64/Single/Double/Decimal the types mismatch on the stack. Rewrite
// as (x == 0) using the comparison emit, which handles all types correctly.
// The test runs at the CHILD's dtype (NumPy: logical_not(f8) inspects f8).
if (_op == UnaryOp.LogicalNot)
{
_child.EmitScalar(il, ctx);
WhereNode.EmitPushZeroPublic(il, childType);
DirectILKernelGenerator.EmitComparisonOperation(il, ComparisonOp.Equal, childType);
DirectILKernelGenerator.EmitConvertTo(il, NPTypeCode.Int32, my);
return;
}
// Predicates also run at the child's dtype (NumPy: isnan(int) is
// all-False without promoting the input).
if (IsPredicateResult(_op))
{
_child.EmitScalar(il, ctx);
DirectILKernelGenerator.EmitUnaryScalarOperation(il, _op, childType);
DirectILKernelGenerator.EmitConvertTo(il, NPTypeCode.Int32, my);
return;
}
_child.EmitScalar(il, ctx);
DirectILKernelGenerator.EmitConvertTo(il, childType, my);
// Identity cases at integer/bool dtypes (NumPy preserves the value).
if (IsRoundingOp(_op) && IsIntegerKind(my))
return;
if (_op == UnaryOp.Abs && my == NPTypeCode.Boolean)
return;
// NumPy invert on bool is logical not; the raw Not opcode on the
// I4 0/1 would produce -1/-2.
if (_op == UnaryOp.BitwiseNot && my == NPTypeCode.Boolean)
{
il.Emit(OpCodes.Ldc_I4_1);
il.Emit(OpCodes.Xor);
return;
}
DirectILKernelGenerator.EmitUnaryScalarOperation(il, _op, my);
}
public override void EmitVector(ILGenerator il, NDExprCompileContext ctx)
{
_child.EmitVector(il, ctx);
DirectILKernelGenerator.EmitUnaryVectorOperation(il, _op, ctx.OutputType);
}
public override void AppendSignature(StringBuilder sb)
{
sb.Append(_op).Append('(');
_child.AppendSignature(sb);
sb.Append(')');
}
}
// =========================================================================
// Node: Comparison op (produces numeric 0/1 at output dtype)
//
// Comparisons in NumPy return bool arrays, but NDExpr's single-output-dtype
// model collapses that to "0 or 1 at output dtype", which composes cleanly
// with arithmetic (e.g. (x > 0) * x for ReLU). The I4 0/1 produced by
// EmitComparisonOperation is converted to the output dtype after emission.
//
// Scalar-only — SIMD would require writing bool output and rerouting through
// the Comparison kernel pipeline, which is beyond this tier.
// =========================================================================
public sealed partial class ComparisonNode : NDExpr
{
private readonly ComparisonOp _op;
private readonly NDExpr _left;
private readonly NDExpr _right;
public ComparisonNode(ComparisonOp op, NDExpr left, NDExpr right)
{
_op = op;
_left = left ?? throw new ArgumentNullException(nameof(left));
_right = right ?? throw new ArgumentNullException(nameof(right));
}
public override bool SupportsSimd => false;
public override void EmitScalar(ILGenerator il, NDExprCompileContext ctx)
{
var my = ctx.TypeOf(this); // Boolean in NumPy mode; OutputType legacy
var lT = ctx.TypeOf(_left);
var rT = ctx.TypeOf(_right);
// NumPy compares at the operands' common dtype (result_type of the
// two children), then yields bool. Legacy mode compares at
// OutputType, where both children already sit.
var cmpType = ctx.NodeTypes is null
? ctx.OutputType
: NDExprTypeRules.PromoteStrong(lT, rT);
_left.EmitScalar(il, ctx);
DirectILKernelGenerator.EmitConvertTo(il, lT, cmpType);
_right.EmitScalar(il, ctx);
DirectILKernelGenerator.EmitConvertTo(il, rT, cmpType);
DirectILKernelGenerator.EmitComparisonOperation(il, _op, cmpType);
// EmitComparisonOperation leaves an I4 (0 or 1) on the stack.
DirectILKernelGenerator.EmitConvertTo(il, NPTypeCode.Int32, my);
}
public override void EmitVector(ILGenerator il, NDExprCompileContext ctx)
{
throw new InvalidOperationException("ComparisonNode has no vector path.");
}
public override void AppendSignature(StringBuilder sb)
{
sb.Append("Cmp").Append(_op).Append('(');
_left.AppendSignature(sb);
sb.Append(',');
_right.AppendSignature(sb);
sb.Append(')');
}
}
// =========================================================================
// Node: Min/Max — scalar-only branchy select
//
// Min(a, b) = a < b ? a : b
// Max(a, b) = a > b ? a : b
// NaN handling: matches NumPy's minimum/maximum — if either operand is NaN,
// result is NaN (because the C# compare opcodes on NaN return 0).
//
// Branch-free equivalent via Math.Min/Math.Max would handle NaN differently
// (returns the non-NaN operand) — NumPy's np.minimum/np.maximum return NaN,
// so the branchy lowering matches NumPy exactly. For NumPy's np.fmin/np.fmax
// (NaN-skipping) users can compose with IsNaN + Where.
// =========================================================================
public sealed partial class MinMaxNode : NDExpr
{
private readonly bool _isMin;
private readonly NDExpr _left;
private readonly NDExpr _right;
public MinMaxNode(bool isMin, NDExpr left, NDExpr right)
{
_isMin = isMin;
_left = left ?? throw new ArgumentNullException(nameof(left));
_right = right ?? throw new ArgumentNullException(nameof(right));
}
public override bool SupportsSimd => false;
public override void EmitScalar(ILGenerator il, NDExprCompileContext ctx)
{
// Prefer Math.Min/Max — they propagate NaN per IEEE 754, matching NumPy's
// np.minimum/np.maximum. Fall back to a branchy select for dtypes without
// a Math.Min/Max overload (Char, Boolean, Half, Complex).
EmitBranchy(il, ctx);
}
private void EmitBranchy(ILGenerator il, NDExprCompileContext ctx)
{
var my = ctx.TypeOf(this);
var clrType = DirectILKernelGenerator.GetClrType(my);
var locL = il.DeclareLocal(clrType);
var locR = il.DeclareLocal(clrType);
_left.EmitScalar(il, ctx);
DirectILKernelGenerator.EmitConvertTo(il, ctx.TypeOf(_left), my);
il.Emit(OpCodes.Stloc, locL);
_right.EmitScalar(il, ctx);
DirectILKernelGenerator.EmitConvertTo(il, ctx.TypeOf(_right), my);
il.Emit(OpCodes.Stloc, locR);
// Prefer Math.Min/Max if available (NaN-propagating for floats).
// ScalarMethodCache.Get throws on missing; fall back to the manual ldloc/branch
// path below for types without a Math overload (e.g. Char).
string methodName = _isMin ? "Min" : "Max";
System.Reflection.MethodInfo method = null;
try { method = ScalarMethodCache.Get(typeof(Math), methodName, clrType, clrType); }
catch (MissingMethodException) { /* fall through */ }
if (method != null)
{
il.Emit(OpCodes.Ldloc, locL);
il.Emit(OpCodes.Ldloc, locR);
il.EmitCall(OpCodes.Call, method, null);
return;
}
// Fallback: branchy select via comparison (for Char / Boolean / Half).
var lblElse = il.DefineLabel();
var lblEnd = il.DefineLabel();
il.Emit(OpCodes.Ldloc, locL);
il.Emit(OpCodes.Ldloc, locR);
DirectILKernelGenerator.EmitComparisonOperation(
il,
_isMin ? ComparisonOp.LessEqual : ComparisonOp.GreaterEqual,
my);
il.Emit(OpCodes.Brfalse, lblElse);
il.Emit(OpCodes.Ldloc, locL);
il.Emit(OpCodes.Br, lblEnd);
il.MarkLabel(lblElse);
il.Emit(OpCodes.Ldloc, locR);
il.MarkLabel(lblEnd);
}
public override void EmitVector(ILGenerator il, NDExprCompileContext ctx)
{
throw new InvalidOperationException("MinMaxNode has no vector path.");
}
public override void AppendSignature(StringBuilder sb)
{
sb.Append(_isMin ? "Min(" : "Max(");
_left.AppendSignature(sb);
sb.Append(',');
_right.AppendSignature(sb);
sb.Append(')');
}
}
// =========================================================================
// Node: Where(cond, a, b) — scalar-only ternary
//
// cond is evaluated at the output dtype. Non-zero means "true".
// Equivalent to np.where(cond, a, b), with cond coerced to bool.
// =========================================================================
public sealed partial class WhereNode : NDExpr
{
private readonly NDExpr _cond;
private readonly NDExpr _a;
private readonly NDExpr _b;
public WhereNode(NDExpr cond, NDExpr a, NDExpr b)
{
_cond = cond ?? throw new ArgumentNullException(nameof(cond));
_a = a ?? throw new ArgumentNullException(nameof(a));
_b = b ?? throw new ArgumentNullException(nameof(b));
}
public override bool SupportsSimd => false;
public override void EmitScalar(ILGenerator il, NDExprCompileContext ctx)
{
var my = ctx.TypeOf(this);
var condType = ctx.TypeOf(_cond);
var lblElse = il.DefineLabel();
var lblEnd = il.DefineLabel();
// Evaluate cond at its own dtype (NumPy nonzero-tests the
// condition without promoting it), then compare to zero so we
// have a verifiable I4 0/1 on the stack before brfalse.
_cond.EmitScalar(il, ctx);
EmitPushZero(il, condType);
DirectILKernelGenerator.EmitComparisonOperation(il, ComparisonOp.NotEqual, condType);
il.Emit(OpCodes.Brfalse, lblElse);
_a.EmitScalar(il, ctx);
DirectILKernelGenerator.EmitConvertTo(il, ctx.TypeOf(_a), my);
il.Emit(OpCodes.Br, lblEnd);
il.MarkLabel(lblElse);
_b.EmitScalar(il, ctx);
DirectILKernelGenerator.EmitConvertTo(il, ctx.TypeOf(_b), my);
il.MarkLabel(lblEnd);
}
private static void EmitPushZero(ILGenerator il, NPTypeCode type)
=> EmitPushZeroPublic(il, type);
public static void EmitPushZeroPublic(ILGenerator il, NPTypeCode type)
{
switch (type)
{
case NPTypeCode.Single:
il.Emit(OpCodes.Ldc_R4, 0f);
break;
case NPTypeCode.Double:
il.Emit(OpCodes.Ldc_R8, 0d);
break;
case NPTypeCode.Int64:
case NPTypeCode.UInt64:
il.Emit(OpCodes.Ldc_I8, 0L);
break;
case NPTypeCode.Boolean:
case NPTypeCode.Byte:
case NPTypeCode.Int16:
case NPTypeCode.UInt16:
case NPTypeCode.Int32:
case NPTypeCode.UInt32:
case NPTypeCode.Char:
il.Emit(OpCodes.Ldc_I4_0);
break;
case NPTypeCode.Decimal:
var fld = typeof(decimal).GetField(nameof(decimal.Zero));
il.Emit(OpCodes.Ldsfld, fld!);
break;
case NPTypeCode.Complex:
// System.Numerics.Complex.Zero is a static readonly field (0 + 0i). Needed when
// a mixed where(cond, complex, real) promotes the real operand to complex.
var cZero = typeof(System.Numerics.Complex).GetField(nameof(System.Numerics.Complex.Zero));
il.Emit(OpCodes.Ldsfld, cZero!);
break;
case NPTypeCode.Half:
// Half has no Zero constant; push float 0 and convert via the explicit operator.
il.Emit(OpCodes.Ldc_R4, 0f);
il.Emit(OpCodes.Call, typeof(Half).GetMethod("op_Explicit", new[] { typeof(float) })
?? throw new MissingMethodException(typeof(Half).FullName, "op_Explicit(float)"));
break;
default:
throw new NotSupportedException($"Zero-push unsupported for {type}");
}
}
public override void EmitVector(ILGenerator il, NDExprCompileContext ctx)
{
throw new InvalidOperationException("WhereNode has no vector path.");
}
public override void AppendSignature(StringBuilder sb)
{
sb.Append("Where(");
_cond.AppendSignature(sb);