Repository navigation
Expand file tree
/
Copy pathDirectILKernelGenerator.Where.Scalar.cs
More file actions
689 lines (573 loc) · 26.4 KB
/
Copy pathDirectILKernelGenerator.Where.Scalar.cs
File metadata and controls
689 lines (573 loc) · 26.4 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
using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;
using NumSharp.Utilities;
// =============================================================================
// DirectILKernelGenerator.Where.Scalar.cs - IL-emitted scalar-broadcast Where kernels
// =============================================================================
//
// MOTIVATION:
// np.where(cond, scalar, arr) and np.where(cond, arr, scalar) are very common
// patterns ("if cond then constant else value"). Today asanyarray(scalar)
// produces a 0-d array that broadcast_arrays expands to a stride-0 view —
// the contig-array gate in np.where_internal fails and we fall through to
// NDIter, which is ~6.5× slower than the IL contig WhereKernel.
//
// KERNELS (each IL-emitted via DynamicMethod, cached per T):
//
// WhereScalarXKernel<T>(bool* cond, T scalarX, T* y, T* result, long count)
// result[i] = cond[i] ? scalarX : y[i]
//
// WhereScalarYKernel<T>(bool* cond, T* x, T scalarY, T* result, long count)
// result[i] = cond[i] ? x[i] : scalarY
//
// WhereScalarXYKernel<T>(bool* cond, T scalarX, T scalarY, T* result, long count)
// result[i] = cond[i] ? scalarX : scalarY
//
// The scalar value is hoisted into a Vector256/Vector128.Create<T>(scalar)
// ONCE before the loop and stored in a local. Inside the SIMD body we Ldloc
// that pre-built vector instead of loading from memory each iteration.
// In the scalar tail we Ldarg the value directly.
//
// =============================================================================
namespace NumSharp.Backends.Kernels
{
public unsafe delegate void WhereScalarXKernel<T>(bool* cond, T scalarX, T* y, T* result, long count) where T : unmanaged;
public unsafe delegate void WhereScalarYKernel<T>(bool* cond, T* x, T scalarY, T* result, long count) where T : unmanaged;
public unsafe delegate void WhereScalarXYKernel<T>(bool* cond, T scalarX, T scalarY, T* result, long count) where T : unmanaged;
public static partial class DirectILKernelGenerator
{
#region Caches
internal static readonly ConcurrentDictionary<Type, Delegate> _whereScalarXCache = new();
internal static readonly ConcurrentDictionary<Type, Delegate> _whereScalarYCache = new();
internal static readonly ConcurrentDictionary<Type, Delegate> _whereScalarXYCache = new();
#endregion
#region Public API
public static WhereScalarXKernel<T> GetWhereScalarXKernel<T>() where T : unmanaged
{
if (!Enabled) return null;
var type = typeof(T);
if (_whereScalarXCache.TryGetValue(type, out var cached))
return (WhereScalarXKernel<T>)cached;
try
{
var kernel = GenerateWhereScalarXKernelIL<T>();
if (kernel == null) return null;
return (WhereScalarXKernel<T>)_whereScalarXCache.GetOrAdd(type, kernel);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"[ILKernel] GetWhereScalarXKernel<{type.Name}>: {ex.GetType().Name}: {ex.Message}");
return null;
}
}
public static WhereScalarYKernel<T> GetWhereScalarYKernel<T>() where T : unmanaged
{
if (!Enabled) return null;
var type = typeof(T);
if (_whereScalarYCache.TryGetValue(type, out var cached))
return (WhereScalarYKernel<T>)cached;
try
{
var kernel = GenerateWhereScalarYKernelIL<T>();
if (kernel == null) return null;
return (WhereScalarYKernel<T>)_whereScalarYCache.GetOrAdd(type, kernel);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"[ILKernel] GetWhereScalarYKernel<{type.Name}>: {ex.GetType().Name}: {ex.Message}");
return null;
}
}
public static WhereScalarXYKernel<T> GetWhereScalarXYKernel<T>() where T : unmanaged
{
if (!Enabled) return null;
var type = typeof(T);
if (_whereScalarXYCache.TryGetValue(type, out var cached))
return (WhereScalarXYKernel<T>)cached;
try
{
var kernel = GenerateWhereScalarXYKernelIL<T>();
if (kernel == null) return null;
return (WhereScalarXYKernel<T>)_whereScalarXYCache.GetOrAdd(type, kernel);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"[ILKernel] GetWhereScalarXYKernel<{type.Name}>: {ex.GetType().Name}: {ex.Message}");
return null;
}
}
#endregion
#region SIMD eligibility (mirrors GenerateWhereKernelIL)
// Same SIMD gate as the contig WhereKernel — V256 needs Avx2 for byte-lane
// sign-extend, V128 needs Sse41. 1-byte types skip the x86 requirement.
private static (bool emitSimd, bool useV256) WhereScalarSimdMode<T>() where T : unmanaged
{
int elementSize = Unsafe.SizeOf<T>();
bool canSimdDtype = elementSize <= 8 && IsSimdSupported<T>();
bool needsX86 = elementSize > 1;
bool useV256 = VectorBits >= 256 && (!needsX86 || Avx2.IsSupported);
bool useV128 = !useV256 && VectorBits >= 128 && (!needsX86 || Sse41.IsSupported);
return (canSimdDtype && (useV256 || useV128), useV256);
}
#endregion
#region IL Emission: WhereScalarX
/// <summary>
/// Emit: result[i] = cond[i] ? scalarX : y[i]
///
/// Layout (similar to the contig WhereKernel but x is a hoisted scalar):
/// Ldarg_0 = bool* cond
/// Ldarg_1 = T scalarX
/// Ldarg_2 = T* y
/// Ldarg_3 = T* result
/// Ldarg_S 4 = long count
///
/// Pre-loop:
/// scalarXVec = V<T>.Create(scalarX)
///
/// SIMD body (per vectorCount lanes):
/// mask = expand(cond + i)
/// yVec = V.Load(y + i*elemSize)
/// V.Store(result + i*elemSize, V.ConditionalSelect(mask, scalarXVec, yVec))
/// </summary>
private static WhereScalarXKernel<T> GenerateWhereScalarXKernelIL<T>() where T : unmanaged
{
int elementSize = Unsafe.SizeOf<T>();
var (emitSimd, useV256) = WhereScalarSimdMode<T>();
var dm = new DynamicMethod(
name: $"IL_WhereScalarX_{typeof(T).Name}",
returnType: typeof(void),
parameterTypes: new[] { typeof(bool*), typeof(T), typeof(T*), typeof(T*), typeof(long) },
owner: typeof(DirectILKernelGenerator),
skipVisibility: true);
var il = dm.GetILGenerator();
var locI = il.DeclareLocal(typeof(long));
// i = 0;
il.Emit(OpCodes.Ldc_I8, 0L);
il.Emit(OpCodes.Stloc, locI);
LocalBuilder locScalarXVec = null;
if (emitSimd)
{
// scalarXVec = V<T>.Create(scalarX) -- ONCE before the loop.
int simdBits = useV256 ? 256 : 128;
var vType = VectorMethodCache.CreateBroadcast(simdBits, typeof(T));
locScalarXVec = il.DeclareLocal(VectorMethodCache.V(simdBits, typeof(T)));
il.Emit(OpCodes.Ldarg_1); // scalarX
il.EmitCall(OpCodes.Call, vType, null);
il.Emit(OpCodes.Stloc, locScalarXVec);
EmitWhereScalarXSimdLoop<T>(il, locI, useV256, locScalarXVec);
}
// Scalar tail (also handles non-SIMD platforms / unsupported dtypes).
EmitWhereScalarXScalarTail<T>(il, locI);
il.Emit(OpCodes.Ret);
return (WhereScalarXKernel<T>)dm.CreateDelegate(typeof(WhereScalarXKernel<T>));
}
private static void EmitWhereScalarXSimdLoop<T>(ILGenerator il, LocalBuilder locI, bool useV256, LocalBuilder locScalarXVec) where T : unmanaged
{
long elementSize = Unsafe.SizeOf<T>();
long vectorCount = useV256 ? (32 / elementSize) : (16 / elementSize);
var locVectorEnd = il.DeclareLocal(typeof(long));
var lblVecHead = il.DefineLabel();
var lblVecEnd = il.DefineLabel();
il.Emit(OpCodes.Ldarg_S, (byte)4);
il.Emit(OpCodes.Ldc_I8, vectorCount);
il.Emit(OpCodes.Sub);
il.Emit(OpCodes.Stloc, locVectorEnd);
il.MarkLabel(lblVecHead);
il.Emit(OpCodes.Ldloc, locI);
il.Emit(OpCodes.Ldloc, locVectorEnd);
il.Emit(OpCodes.Bgt, lblVecEnd);
EmitWhereScalarXSimdBody<T>(il, locI, useV256, locScalarXVec, elementSize, 0);
il.Emit(OpCodes.Ldloc, locI);
il.Emit(OpCodes.Ldc_I8, vectorCount);
il.Emit(OpCodes.Add);
il.Emit(OpCodes.Stloc, locI);
il.Emit(OpCodes.Br, lblVecHead);
il.MarkLabel(lblVecEnd);
}
/// <summary>
/// One SIMD body iteration. <paramref name="laneOffset"/> shifts the base index by
/// that many elements (in element units, not bytes) — used to emit multiple bodies
/// in an unrolled outer loop without advancing <c>locI</c> between them.
/// </summary>
private static void EmitWhereScalarXSimdBody<T>(ILGenerator il, LocalBuilder locI, bool useV256, LocalBuilder locScalarXVec, long elementSize, long laneOffset) where T : unmanaged
{
int simdBits = useV256 ? 256 : 128;
var loadM = VectorMethodCache.Load(simdBits, typeof(T));
var storeM = VectorMethodCache.Store(simdBits, typeof(T));
var selectM = VectorMethodCache.ConditionalSelect(simdBits, typeof(T));
// Mask: cond + (i + laneOffset)
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldloc, locI);
if (laneOffset != 0)
{
il.Emit(OpCodes.Ldc_I8, laneOffset);
il.Emit(OpCodes.Add);
}
il.Emit(OpCodes.Conv_I);
il.Emit(OpCodes.Add);
if (useV256) EmitInlineMaskCreationV256(il, (int)elementSize);
else EmitInlineMaskCreationV128(il, (int)elementSize);
// scalarXVec
il.Emit(OpCodes.Ldloc, locScalarXVec);
// yVec = Load(y + (i + laneOffset)*elementSize)
il.Emit(OpCodes.Ldarg_2);
il.Emit(OpCodes.Ldloc, locI);
if (laneOffset != 0)
{
il.Emit(OpCodes.Ldc_I8, laneOffset);
il.Emit(OpCodes.Add);
}
il.Emit(OpCodes.Ldc_I8, elementSize);
il.Emit(OpCodes.Mul);
il.Emit(OpCodes.Conv_I);
il.Emit(OpCodes.Add);
il.EmitCall(OpCodes.Call, loadM, null);
// ConditionalSelect(mask, scalarXVec, yVec)
il.EmitCall(OpCodes.Call, selectM, null);
// Store at result + (i + laneOffset)*elementSize
il.Emit(OpCodes.Ldarg_3);
il.Emit(OpCodes.Ldloc, locI);
if (laneOffset != 0)
{
il.Emit(OpCodes.Ldc_I8, laneOffset);
il.Emit(OpCodes.Add);
}
il.Emit(OpCodes.Ldc_I8, elementSize);
il.Emit(OpCodes.Mul);
il.Emit(OpCodes.Conv_I);
il.Emit(OpCodes.Add);
il.EmitCall(OpCodes.Call, storeM, null);
}
private static void EmitWhereScalarXScalarTail<T>(ILGenerator il, LocalBuilder locI) where T : unmanaged
{
long elementSize = Unsafe.SizeOf<T>();
var typeCode = InfoOf<T>.NPTypeCode;
var lblHead = il.DefineLabel();
var lblEnd = il.DefineLabel();
var lblTakeY = il.DefineLabel();
var lblStore = il.DefineLabel();
il.MarkLabel(lblHead);
il.Emit(OpCodes.Ldloc, locI);
il.Emit(OpCodes.Ldarg_S, (byte)4);
il.Emit(OpCodes.Bge, lblEnd);
// result + i*elementSize (kept on stack for the Store)
il.Emit(OpCodes.Ldarg_3);
il.Emit(OpCodes.Ldloc, locI);
il.Emit(OpCodes.Ldc_I8, elementSize);
il.Emit(OpCodes.Mul);
il.Emit(OpCodes.Conv_I);
il.Emit(OpCodes.Add);
// bool c = cond[i]
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldloc, locI);
il.Emit(OpCodes.Conv_I);
il.Emit(OpCodes.Add);
il.Emit(OpCodes.Ldind_U1);
il.Emit(OpCodes.Brfalse, lblTakeY);
// True: load scalarX
il.Emit(OpCodes.Ldarg_1);
il.Emit(OpCodes.Br, lblStore);
// False: load y[i]
il.MarkLabel(lblTakeY);
il.Emit(OpCodes.Ldarg_2);
il.Emit(OpCodes.Ldloc, locI);
il.Emit(OpCodes.Ldc_I8, elementSize);
il.Emit(OpCodes.Mul);
il.Emit(OpCodes.Conv_I);
il.Emit(OpCodes.Add);
EmitLoadIndirect(il, typeCode);
il.MarkLabel(lblStore);
EmitStoreIndirect(il, typeCode);
// i++
il.Emit(OpCodes.Ldloc, locI);
il.Emit(OpCodes.Ldc_I8, 1L);
il.Emit(OpCodes.Add);
il.Emit(OpCodes.Stloc, locI);
il.Emit(OpCodes.Br, lblHead);
il.MarkLabel(lblEnd);
}
#endregion
#region IL Emission: WhereScalarY
/// <summary>
/// result[i] = cond[i] ? x[i] : scalarY
/// Ldarg_0 = bool* cond
/// Ldarg_1 = T* x
/// Ldarg_2 = T scalarY
/// Ldarg_3 = T* result
/// Ldarg_S 4 = long count
/// </summary>
private static WhereScalarYKernel<T> GenerateWhereScalarYKernelIL<T>() where T : unmanaged
{
int elementSize = Unsafe.SizeOf<T>();
var (emitSimd, useV256) = WhereScalarSimdMode<T>();
var dm = new DynamicMethod(
name: $"IL_WhereScalarY_{typeof(T).Name}",
returnType: typeof(void),
parameterTypes: new[] { typeof(bool*), typeof(T*), typeof(T), typeof(T*), typeof(long) },
owner: typeof(DirectILKernelGenerator),
skipVisibility: true);
var il = dm.GetILGenerator();
var locI = il.DeclareLocal(typeof(long));
il.Emit(OpCodes.Ldc_I8, 0L);
il.Emit(OpCodes.Stloc, locI);
LocalBuilder locScalarYVec = null;
if (emitSimd)
{
int simdBits = useV256 ? 256 : 128;
var createM = VectorMethodCache.CreateBroadcast(simdBits, typeof(T));
locScalarYVec = il.DeclareLocal(VectorMethodCache.V(simdBits, typeof(T)));
il.Emit(OpCodes.Ldarg_2); // scalarY
il.EmitCall(OpCodes.Call, createM, null);
il.Emit(OpCodes.Stloc, locScalarYVec);
EmitWhereScalarYSimdLoop<T>(il, locI, useV256, locScalarYVec);
}
EmitWhereScalarYScalarTail<T>(il, locI);
il.Emit(OpCodes.Ret);
return (WhereScalarYKernel<T>)dm.CreateDelegate(typeof(WhereScalarYKernel<T>));
}
private static void EmitWhereScalarYSimdLoop<T>(ILGenerator il, LocalBuilder locI, bool useV256, LocalBuilder locScalarYVec) where T : unmanaged
{
long elementSize = Unsafe.SizeOf<T>();
long vectorCount = useV256 ? (32 / elementSize) : (16 / elementSize);
var locVectorEnd = il.DeclareLocal(typeof(long));
var lblVecHead = il.DefineLabel();
var lblVecEnd = il.DefineLabel();
il.Emit(OpCodes.Ldarg_S, (byte)4);
il.Emit(OpCodes.Ldc_I8, vectorCount);
il.Emit(OpCodes.Sub);
il.Emit(OpCodes.Stloc, locVectorEnd);
il.MarkLabel(lblVecHead);
il.Emit(OpCodes.Ldloc, locI);
il.Emit(OpCodes.Ldloc, locVectorEnd);
il.Emit(OpCodes.Bgt, lblVecEnd);
EmitWhereScalarYSimdBody<T>(il, locI, useV256, locScalarYVec, elementSize, 0);
il.Emit(OpCodes.Ldloc, locI);
il.Emit(OpCodes.Ldc_I8, vectorCount);
il.Emit(OpCodes.Add);
il.Emit(OpCodes.Stloc, locI);
il.Emit(OpCodes.Br, lblVecHead);
il.MarkLabel(lblVecEnd);
}
private static void EmitWhereScalarYSimdBody<T>(ILGenerator il, LocalBuilder locI, bool useV256, LocalBuilder locScalarYVec, long elementSize, long laneOffset) where T : unmanaged
{
int simdBits = useV256 ? 256 : 128;
var loadM = VectorMethodCache.Load(simdBits, typeof(T));
var storeM = VectorMethodCache.Store(simdBits, typeof(T));
var selectM = VectorMethodCache.ConditionalSelect(simdBits, typeof(T));
// Mask
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldloc, locI);
if (laneOffset != 0) { il.Emit(OpCodes.Ldc_I8, laneOffset); il.Emit(OpCodes.Add); }
il.Emit(OpCodes.Conv_I);
il.Emit(OpCodes.Add);
if (useV256) EmitInlineMaskCreationV256(il, (int)elementSize);
else EmitInlineMaskCreationV128(il, (int)elementSize);
// xVec = Load(x + (i+laneOffset)*elementSize)
il.Emit(OpCodes.Ldarg_1);
il.Emit(OpCodes.Ldloc, locI);
if (laneOffset != 0) { il.Emit(OpCodes.Ldc_I8, laneOffset); il.Emit(OpCodes.Add); }
il.Emit(OpCodes.Ldc_I8, elementSize);
il.Emit(OpCodes.Mul);
il.Emit(OpCodes.Conv_I);
il.Emit(OpCodes.Add);
il.EmitCall(OpCodes.Call, loadM, null);
// scalarYVec
il.Emit(OpCodes.Ldloc, locScalarYVec);
// ConditionalSelect(mask, xVec, scalarYVec)
il.EmitCall(OpCodes.Call, selectM, null);
// Store at result + (i+laneOffset)*elementSize
il.Emit(OpCodes.Ldarg_3);
il.Emit(OpCodes.Ldloc, locI);
if (laneOffset != 0) { il.Emit(OpCodes.Ldc_I8, laneOffset); il.Emit(OpCodes.Add); }
il.Emit(OpCodes.Ldc_I8, elementSize);
il.Emit(OpCodes.Mul);
il.Emit(OpCodes.Conv_I);
il.Emit(OpCodes.Add);
il.EmitCall(OpCodes.Call, storeM, null);
}
private static void EmitWhereScalarYScalarTail<T>(ILGenerator il, LocalBuilder locI) where T : unmanaged
{
long elementSize = Unsafe.SizeOf<T>();
var typeCode = InfoOf<T>.NPTypeCode;
var lblHead = il.DefineLabel();
var lblEnd = il.DefineLabel();
var lblTakeY = il.DefineLabel();
var lblStore = il.DefineLabel();
il.MarkLabel(lblHead);
il.Emit(OpCodes.Ldloc, locI);
il.Emit(OpCodes.Ldarg_S, (byte)4);
il.Emit(OpCodes.Bge, lblEnd);
il.Emit(OpCodes.Ldarg_3);
il.Emit(OpCodes.Ldloc, locI);
il.Emit(OpCodes.Ldc_I8, elementSize);
il.Emit(OpCodes.Mul);
il.Emit(OpCodes.Conv_I);
il.Emit(OpCodes.Add);
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldloc, locI);
il.Emit(OpCodes.Conv_I);
il.Emit(OpCodes.Add);
il.Emit(OpCodes.Ldind_U1);
il.Emit(OpCodes.Brfalse, lblTakeY);
// True: load x[i]
il.Emit(OpCodes.Ldarg_1);
il.Emit(OpCodes.Ldloc, locI);
il.Emit(OpCodes.Ldc_I8, elementSize);
il.Emit(OpCodes.Mul);
il.Emit(OpCodes.Conv_I);
il.Emit(OpCodes.Add);
EmitLoadIndirect(il, typeCode);
il.Emit(OpCodes.Br, lblStore);
// False: load scalarY
il.MarkLabel(lblTakeY);
il.Emit(OpCodes.Ldarg_2);
il.MarkLabel(lblStore);
EmitStoreIndirect(il, typeCode);
il.Emit(OpCodes.Ldloc, locI);
il.Emit(OpCodes.Ldc_I8, 1L);
il.Emit(OpCodes.Add);
il.Emit(OpCodes.Stloc, locI);
il.Emit(OpCodes.Br, lblHead);
il.MarkLabel(lblEnd);
}
#endregion
#region IL Emission: WhereScalarXY
/// <summary>
/// result[i] = cond[i] ? scalarX : scalarY
/// Ldarg_0 = bool* cond
/// Ldarg_1 = T scalarX
/// Ldarg_2 = T scalarY
/// Ldarg_3 = T* result
/// Ldarg_S 4 = long count
/// </summary>
private static WhereScalarXYKernel<T> GenerateWhereScalarXYKernelIL<T>() where T : unmanaged
{
int elementSize = Unsafe.SizeOf<T>();
var (emitSimd, useV256) = WhereScalarSimdMode<T>();
var dm = new DynamicMethod(
name: $"IL_WhereScalarXY_{typeof(T).Name}",
returnType: typeof(void),
parameterTypes: new[] { typeof(bool*), typeof(T), typeof(T), typeof(T*), typeof(long) },
owner: typeof(DirectILKernelGenerator),
skipVisibility: true);
var il = dm.GetILGenerator();
var locI = il.DeclareLocal(typeof(long));
il.Emit(OpCodes.Ldc_I8, 0L);
il.Emit(OpCodes.Stloc, locI);
LocalBuilder locXVec = null;
LocalBuilder locYVec = null;
if (emitSimd)
{
int simdBits = useV256 ? 256 : 128;
var createM = VectorMethodCache.CreateBroadcast(simdBits, typeof(T));
var vecT = VectorMethodCache.V(simdBits, typeof(T));
locXVec = il.DeclareLocal(vecT);
locYVec = il.DeclareLocal(vecT);
il.Emit(OpCodes.Ldarg_1);
il.EmitCall(OpCodes.Call, createM, null);
il.Emit(OpCodes.Stloc, locXVec);
il.Emit(OpCodes.Ldarg_2);
il.EmitCall(OpCodes.Call, createM, null);
il.Emit(OpCodes.Stloc, locYVec);
EmitWhereScalarXYSimdLoop<T>(il, locI, useV256, locXVec, locYVec);
}
EmitWhereScalarXYScalarTail<T>(il, locI);
il.Emit(OpCodes.Ret);
return (WhereScalarXYKernel<T>)dm.CreateDelegate(typeof(WhereScalarXYKernel<T>));
}
private static void EmitWhereScalarXYSimdLoop<T>(ILGenerator il, LocalBuilder locI, bool useV256, LocalBuilder locXVec, LocalBuilder locYVec) where T : unmanaged
{
long elementSize = Unsafe.SizeOf<T>();
long vectorCount = useV256 ? (32 / elementSize) : (16 / elementSize);
var locVectorEnd = il.DeclareLocal(typeof(long));
var lblVecHead = il.DefineLabel();
var lblVecEnd = il.DefineLabel();
il.Emit(OpCodes.Ldarg_S, (byte)4);
il.Emit(OpCodes.Ldc_I8, vectorCount);
il.Emit(OpCodes.Sub);
il.Emit(OpCodes.Stloc, locVectorEnd);
il.MarkLabel(lblVecHead);
il.Emit(OpCodes.Ldloc, locI);
il.Emit(OpCodes.Ldloc, locVectorEnd);
il.Emit(OpCodes.Bgt, lblVecEnd);
EmitWhereScalarXYSimdBody<T>(il, locI, useV256, locXVec, locYVec, elementSize, 0);
il.Emit(OpCodes.Ldloc, locI);
il.Emit(OpCodes.Ldc_I8, vectorCount);
il.Emit(OpCodes.Add);
il.Emit(OpCodes.Stloc, locI);
il.Emit(OpCodes.Br, lblVecHead);
il.MarkLabel(lblVecEnd);
}
private static void EmitWhereScalarXYSimdBody<T>(ILGenerator il, LocalBuilder locI, bool useV256, LocalBuilder locXVec, LocalBuilder locYVec, long elementSize, long laneOffset) where T : unmanaged
{
int simdBits = useV256 ? 256 : 128;
var storeM = VectorMethodCache.Store(simdBits, typeof(T));
var selectM = VectorMethodCache.ConditionalSelect(simdBits, typeof(T));
// Mask
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldloc, locI);
if (laneOffset != 0) { il.Emit(OpCodes.Ldc_I8, laneOffset); il.Emit(OpCodes.Add); }
il.Emit(OpCodes.Conv_I);
il.Emit(OpCodes.Add);
if (useV256) EmitInlineMaskCreationV256(il, (int)elementSize);
else EmitInlineMaskCreationV128(il, (int)elementSize);
// ConditionalSelect(mask, xVec, yVec)
il.Emit(OpCodes.Ldloc, locXVec);
il.Emit(OpCodes.Ldloc, locYVec);
il.EmitCall(OpCodes.Call, selectM, null);
// Store at result + (i+laneOffset)*elementSize
il.Emit(OpCodes.Ldarg_3);
il.Emit(OpCodes.Ldloc, locI);
if (laneOffset != 0) { il.Emit(OpCodes.Ldc_I8, laneOffset); il.Emit(OpCodes.Add); }
il.Emit(OpCodes.Ldc_I8, elementSize);
il.Emit(OpCodes.Mul);
il.Emit(OpCodes.Conv_I);
il.Emit(OpCodes.Add);
il.EmitCall(OpCodes.Call, storeM, null);
}
private static void EmitWhereScalarXYScalarTail<T>(ILGenerator il, LocalBuilder locI) where T : unmanaged
{
long elementSize = Unsafe.SizeOf<T>();
var typeCode = InfoOf<T>.NPTypeCode;
var lblHead = il.DefineLabel();
var lblEnd = il.DefineLabel();
var lblTakeY = il.DefineLabel();
var lblStore = il.DefineLabel();
il.MarkLabel(lblHead);
il.Emit(OpCodes.Ldloc, locI);
il.Emit(OpCodes.Ldarg_S, (byte)4);
il.Emit(OpCodes.Bge, lblEnd);
il.Emit(OpCodes.Ldarg_3);
il.Emit(OpCodes.Ldloc, locI);
il.Emit(OpCodes.Ldc_I8, elementSize);
il.Emit(OpCodes.Mul);
il.Emit(OpCodes.Conv_I);
il.Emit(OpCodes.Add);
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldloc, locI);
il.Emit(OpCodes.Conv_I);
il.Emit(OpCodes.Add);
il.Emit(OpCodes.Ldind_U1);
il.Emit(OpCodes.Brfalse, lblTakeY);
il.Emit(OpCodes.Ldarg_1); // scalarX
il.Emit(OpCodes.Br, lblStore);
il.MarkLabel(lblTakeY);
il.Emit(OpCodes.Ldarg_2); // scalarY
il.MarkLabel(lblStore);
EmitStoreIndirect(il, typeCode);
il.Emit(OpCodes.Ldloc, locI);
il.Emit(OpCodes.Ldc_I8, 1L);
il.Emit(OpCodes.Add);
il.Emit(OpCodes.Stloc, locI);
il.Emit(OpCodes.Br, lblHead);
il.MarkLabel(lblEnd);
}
#endregion
}
}