-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
simd.cpp
2479 lines (2277 loc) · 104 KB
/
simd.cpp
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
//
// SIMD Support
//
// IMPORTANT NOTES AND CAVEATS:
//
// This implementation is preliminary, and may change dramatically.
//
// New JIT types, TYP_SIMDxx, are introduced, and the SIMD intrinsics are created as GT_SIMD nodes.
// Nodes of SIMD types will be typed as TYP_SIMD* (e.g. TYP_SIMD8, TYP_SIMD16, etc.).
//
// Note that currently the "reference implementation" is the same as the runtime dll. As such, it is currently
// providing implementations for those methods not currently supported by the JIT as intrinsics.
//
// These are currently recognized using string compares, in order to provide an implementation in the JIT
// without taking a dependency on the VM.
// Furthermore, in the CTP, in order to limit the impact of doing these string compares
// against assembly names, we only look for the SIMDVector assembly if we are compiling a class constructor. This
// makes it somewhat more "pay for play" but is a significant usability compromise.
// This has been addressed for RTM by doing the assembly recognition in the VM.
// --------------------------------------------------------------------------------------
#include "jitpch.h"
#include "simd.h"
#ifdef _MSC_VER
#pragma hdrstop
#endif
#ifdef FEATURE_SIMD
// Intrinsic Id to intrinsic info map
const SIMDIntrinsicInfo simdIntrinsicInfoArray[] = {
#define SIMD_INTRINSIC(mname, inst, id, name, retType, argCount, arg1, arg2, arg3, t1, t2, t3, t4, t5, t6, t7, t8, t9, \
t10) \
{SIMDIntrinsic##id, mname, inst, retType, argCount, arg1, arg2, arg3, t1, t2, t3, t4, t5, t6, t7, t8, t9, t10},
#include "simdintrinsiclist.h"
};
//------------------------------------------------------------------------
// getSIMDVectorLength: Get the length (number of elements of base type) of
// SIMD Vector given its size and base (element) type.
//
// Arguments:
// simdSize - size of the SIMD vector
// baseType - type of the elements of the SIMD vector
//
// static
int Compiler::getSIMDVectorLength(unsigned simdSize, var_types baseType)
{
return simdSize / genTypeSize(baseType);
}
//------------------------------------------------------------------------
// Get the length (number of elements of base type) of SIMD Vector given by typeHnd.
//
// Arguments:
// typeHnd - type handle of the SIMD vector
//
int Compiler::getSIMDVectorLength(CORINFO_CLASS_HANDLE typeHnd)
{
unsigned sizeBytes = 0;
CorInfoType baseJitType = getBaseJitTypeAndSizeOfSIMDType(typeHnd, &sizeBytes);
var_types baseType = JitType2PreciseVarType(baseJitType);
return getSIMDVectorLength(sizeBytes, baseType);
}
//------------------------------------------------------------------------
// Get the preferred alignment of SIMD vector type for better performance.
//
// Arguments:
// typeHnd - type handle of the SIMD vector
//
int Compiler::getSIMDTypeAlignment(var_types simdType)
{
unsigned size = genTypeSize(simdType);
#ifdef TARGET_XARCH
// Fixed length vectors have the following alignment preference
// Vector2 = 8 byte alignment
// Vector3/4 = 16-byte alignment
// preferred alignment for SSE2 128-bit vectors is 16-bytes
if (size == 8)
{
return 8;
}
else if (size <= 16)
{
assert((size == 12) || (size == 16));
return 16;
}
else
{
assert(size == 32);
return 32;
}
#elif defined(TARGET_ARM64)
// preferred alignment for 64-bit vectors is 8-bytes.
// For everything else, 16-bytes.
return (size == 8) ? 8 : 16;
#else
assert(!"getSIMDTypeAlignment() unimplemented on target arch");
unreached();
#endif
}
//----------------------------------------------------------------------------------
// Return the base type and size of SIMD vector type given its type handle.
//
// Arguments:
// typeHnd - The handle of the type we're interested in.
// sizeBytes - out param
//
// Return Value:
// base type of SIMD vector.
// sizeBytes if non-null is set to size in bytes.
//
// Notes:
// If the size of the struct is already known call structSizeMightRepresentSIMDType
// to determine if this api needs to be called.
//
// TODO-Throughput: current implementation parses class name to find base type. Change
// this when we implement SIMD intrinsic identification for the final
// product.
CorInfoType Compiler::getBaseJitTypeAndSizeOfSIMDType(CORINFO_CLASS_HANDLE typeHnd, unsigned* sizeBytes /*= nullptr */)
{
assert(supportSIMDTypes());
if (m_simdHandleCache == nullptr)
{
if (impInlineInfo == nullptr)
{
m_simdHandleCache = new (this, CMK_Generic) SIMDHandlesCache();
}
else
{
// Steal the inliner compiler's cache (create it if not available).
if (impInlineInfo->InlineRoot->m_simdHandleCache == nullptr)
{
impInlineInfo->InlineRoot->m_simdHandleCache = new (this, CMK_Generic) SIMDHandlesCache();
}
m_simdHandleCache = impInlineInfo->InlineRoot->m_simdHandleCache;
}
}
if (typeHnd == nullptr)
{
return CORINFO_TYPE_UNDEF;
}
// fast path search using cached type handles of important types
CorInfoType simdBaseJitType = CORINFO_TYPE_UNDEF;
unsigned size = 0;
// TODO - Optimize SIMD type recognition by IntrinsicAttribute
if (isSIMDClass(typeHnd))
{
// The most likely to be used type handles are looked up first followed by
// less likely to be used type handles
if (typeHnd == m_simdHandleCache->SIMDFloatHandle)
{
simdBaseJitType = CORINFO_TYPE_FLOAT;
size = getSIMDVectorRegisterByteLength();
JITDUMP(" Known type SIMD Vector<Float>\n");
}
else if (typeHnd == m_simdHandleCache->SIMDIntHandle)
{
simdBaseJitType = CORINFO_TYPE_INT;
size = getSIMDVectorRegisterByteLength();
JITDUMP(" Known type SIMD Vector<Int>\n");
}
else if (typeHnd == m_simdHandleCache->SIMDVector2Handle)
{
simdBaseJitType = CORINFO_TYPE_FLOAT;
size = 2 * genTypeSize(TYP_FLOAT);
assert(size == roundUp(info.compCompHnd->getClassSize(typeHnd), TARGET_POINTER_SIZE));
JITDUMP(" Known type Vector2\n");
}
else if (typeHnd == m_simdHandleCache->SIMDVector3Handle)
{
simdBaseJitType = CORINFO_TYPE_FLOAT;
size = 3 * genTypeSize(TYP_FLOAT);
assert(size == info.compCompHnd->getClassSize(typeHnd));
JITDUMP(" Known type Vector3\n");
}
else if (typeHnd == m_simdHandleCache->SIMDVector4Handle)
{
simdBaseJitType = CORINFO_TYPE_FLOAT;
size = 4 * genTypeSize(TYP_FLOAT);
assert(size == roundUp(info.compCompHnd->getClassSize(typeHnd), TARGET_POINTER_SIZE));
JITDUMP(" Known type Vector4\n");
}
else if (typeHnd == m_simdHandleCache->SIMDVectorHandle)
{
size = getSIMDVectorRegisterByteLength();
JITDUMP(" Known type Vector\n");
}
else if (typeHnd == m_simdHandleCache->SIMDUShortHandle)
{
simdBaseJitType = CORINFO_TYPE_USHORT;
size = getSIMDVectorRegisterByteLength();
JITDUMP(" Known type SIMD Vector<ushort>\n");
}
else if (typeHnd == m_simdHandleCache->SIMDUByteHandle)
{
simdBaseJitType = CORINFO_TYPE_UBYTE;
size = getSIMDVectorRegisterByteLength();
JITDUMP(" Known type SIMD Vector<ubyte>\n");
}
else if (typeHnd == m_simdHandleCache->SIMDDoubleHandle)
{
simdBaseJitType = CORINFO_TYPE_DOUBLE;
size = getSIMDVectorRegisterByteLength();
JITDUMP(" Known type SIMD Vector<Double>\n");
}
else if (typeHnd == m_simdHandleCache->SIMDLongHandle)
{
simdBaseJitType = CORINFO_TYPE_LONG;
size = getSIMDVectorRegisterByteLength();
JITDUMP(" Known type SIMD Vector<Long>\n");
}
else if (typeHnd == m_simdHandleCache->SIMDShortHandle)
{
simdBaseJitType = CORINFO_TYPE_SHORT;
size = getSIMDVectorRegisterByteLength();
JITDUMP(" Known type SIMD Vector<short>\n");
}
else if (typeHnd == m_simdHandleCache->SIMDByteHandle)
{
simdBaseJitType = CORINFO_TYPE_BYTE;
size = getSIMDVectorRegisterByteLength();
JITDUMP(" Known type SIMD Vector<byte>\n");
}
else if (typeHnd == m_simdHandleCache->SIMDUIntHandle)
{
simdBaseJitType = CORINFO_TYPE_UINT;
size = getSIMDVectorRegisterByteLength();
JITDUMP(" Known type SIMD Vector<uint>\n");
}
else if (typeHnd == m_simdHandleCache->SIMDULongHandle)
{
simdBaseJitType = CORINFO_TYPE_ULONG;
size = getSIMDVectorRegisterByteLength();
JITDUMP(" Known type SIMD Vector<ulong>\n");
}
else if (typeHnd == m_simdHandleCache->SIMDNIntHandle)
{
simdBaseJitType = CORINFO_TYPE_NATIVEINT;
size = getSIMDVectorRegisterByteLength();
JITDUMP(" Known type SIMD Vector<nint>\n");
}
else if (typeHnd == m_simdHandleCache->SIMDNUIntHandle)
{
simdBaseJitType = CORINFO_TYPE_NATIVEUINT;
size = getSIMDVectorRegisterByteLength();
JITDUMP(" Known type SIMD Vector<nuint>\n");
}
// slow path search
if (simdBaseJitType == CORINFO_TYPE_UNDEF)
{
// Doesn't match with any of the cached type handles.
// Obtain base type by parsing fully qualified class name.
//
// TODO-Throughput: implement product shipping solution to query base type.
WCHAR className[256] = {0};
WCHAR* pbuf = &className[0];
int len = _countof(className);
info.compCompHnd->appendClassName((char16_t**)&pbuf, &len, typeHnd, TRUE, FALSE, FALSE);
noway_assert(pbuf < &className[256]);
JITDUMP("SIMD Candidate Type %S\n", className);
if (wcsncmp(className, W("System.Numerics."), 16) == 0)
{
if (wcsncmp(&(className[16]), W("Vector`1["), 9) == 0)
{
size = getSIMDVectorRegisterByteLength();
if (wcsncmp(&(className[25]), W("System.Single"), 13) == 0)
{
m_simdHandleCache->SIMDFloatHandle = typeHnd;
simdBaseJitType = CORINFO_TYPE_FLOAT;
JITDUMP(" Found type SIMD Vector<Float>\n");
}
else if (wcsncmp(&(className[25]), W("System.Int32"), 12) == 0)
{
m_simdHandleCache->SIMDIntHandle = typeHnd;
simdBaseJitType = CORINFO_TYPE_INT;
JITDUMP(" Found type SIMD Vector<Int>\n");
}
else if (wcsncmp(&(className[25]), W("System.UInt16"), 13) == 0)
{
m_simdHandleCache->SIMDUShortHandle = typeHnd;
simdBaseJitType = CORINFO_TYPE_USHORT;
JITDUMP(" Found type SIMD Vector<ushort>\n");
}
else if (wcsncmp(&(className[25]), W("System.Byte"), 11) == 0)
{
m_simdHandleCache->SIMDUByteHandle = typeHnd;
simdBaseJitType = CORINFO_TYPE_UBYTE;
JITDUMP(" Found type SIMD Vector<ubyte>\n");
}
else if (wcsncmp(&(className[25]), W("System.Double"), 13) == 0)
{
m_simdHandleCache->SIMDDoubleHandle = typeHnd;
simdBaseJitType = CORINFO_TYPE_DOUBLE;
JITDUMP(" Found type SIMD Vector<Double>\n");
}
else if (wcsncmp(&(className[25]), W("System.Int64"), 12) == 0)
{
m_simdHandleCache->SIMDLongHandle = typeHnd;
simdBaseJitType = CORINFO_TYPE_LONG;
JITDUMP(" Found type SIMD Vector<Long>\n");
}
else if (wcsncmp(&(className[25]), W("System.Int16"), 12) == 0)
{
m_simdHandleCache->SIMDShortHandle = typeHnd;
simdBaseJitType = CORINFO_TYPE_SHORT;
JITDUMP(" Found type SIMD Vector<short>\n");
}
else if (wcsncmp(&(className[25]), W("System.SByte"), 12) == 0)
{
m_simdHandleCache->SIMDByteHandle = typeHnd;
simdBaseJitType = CORINFO_TYPE_BYTE;
JITDUMP(" Found type SIMD Vector<byte>\n");
}
else if (wcsncmp(&(className[25]), W("System.UInt32"), 13) == 0)
{
m_simdHandleCache->SIMDUIntHandle = typeHnd;
simdBaseJitType = CORINFO_TYPE_UINT;
JITDUMP(" Found type SIMD Vector<uint>\n");
}
else if (wcsncmp(&(className[25]), W("System.UInt64"), 13) == 0)
{
m_simdHandleCache->SIMDULongHandle = typeHnd;
simdBaseJitType = CORINFO_TYPE_ULONG;
JITDUMP(" Found type SIMD Vector<ulong>\n");
}
else if (wcsncmp(&(className[25]), W("System.IntPtr"), 13) == 0)
{
m_simdHandleCache->SIMDNIntHandle = typeHnd;
simdBaseJitType = CORINFO_TYPE_NATIVEINT;
JITDUMP(" Found type SIMD Vector<nint>\n");
}
else if (wcsncmp(&(className[25]), W("System.UIntPtr"), 14) == 0)
{
m_simdHandleCache->SIMDNUIntHandle = typeHnd;
simdBaseJitType = CORINFO_TYPE_NATIVEUINT;
JITDUMP(" Found type SIMD Vector<nuint>\n");
}
else
{
JITDUMP(" Unknown SIMD Vector<T>\n");
}
}
else if (wcsncmp(&(className[16]), W("Vector2"), 8) == 0)
{
m_simdHandleCache->SIMDVector2Handle = typeHnd;
simdBaseJitType = CORINFO_TYPE_FLOAT;
size = 2 * genTypeSize(TYP_FLOAT);
assert(size == roundUp(info.compCompHnd->getClassSize(typeHnd), TARGET_POINTER_SIZE));
JITDUMP(" Found Vector2\n");
}
else if (wcsncmp(&(className[16]), W("Vector3"), 8) == 0)
{
m_simdHandleCache->SIMDVector3Handle = typeHnd;
simdBaseJitType = CORINFO_TYPE_FLOAT;
size = 3 * genTypeSize(TYP_FLOAT);
assert(size == info.compCompHnd->getClassSize(typeHnd));
JITDUMP(" Found Vector3\n");
}
else if (wcsncmp(&(className[16]), W("Vector4"), 8) == 0)
{
m_simdHandleCache->SIMDVector4Handle = typeHnd;
simdBaseJitType = CORINFO_TYPE_FLOAT;
size = 4 * genTypeSize(TYP_FLOAT);
assert(size == roundUp(info.compCompHnd->getClassSize(typeHnd), TARGET_POINTER_SIZE));
JITDUMP(" Found Vector4\n");
}
else if (wcsncmp(&(className[16]), W("Vector"), 6) == 0)
{
m_simdHandleCache->SIMDVectorHandle = typeHnd;
size = getSIMDVectorRegisterByteLength();
JITDUMP(" Found type Vector\n");
}
else
{
JITDUMP(" Unknown SIMD Type\n");
}
}
}
}
#ifdef FEATURE_HW_INTRINSICS
else if (isIntrinsicType(typeHnd))
{
const size_t Vector64SizeBytes = 64 / 8;
const size_t Vector128SizeBytes = 128 / 8;
const size_t Vector256SizeBytes = 256 / 8;
#if defined(TARGET_XARCH)
static_assert_no_msg(YMM_REGSIZE_BYTES == Vector256SizeBytes);
static_assert_no_msg(XMM_REGSIZE_BYTES == Vector128SizeBytes);
if (typeHnd == m_simdHandleCache->Vector256FloatHandle)
{
simdBaseJitType = CORINFO_TYPE_FLOAT;
size = Vector256SizeBytes;
JITDUMP(" Known type Vector256<float>\n");
}
else if (typeHnd == m_simdHandleCache->Vector256DoubleHandle)
{
simdBaseJitType = CORINFO_TYPE_DOUBLE;
size = Vector256SizeBytes;
JITDUMP(" Known type Vector256<double>\n");
}
else if (typeHnd == m_simdHandleCache->Vector256IntHandle)
{
simdBaseJitType = CORINFO_TYPE_INT;
size = Vector256SizeBytes;
JITDUMP(" Known type Vector256<int>\n");
}
else if (typeHnd == m_simdHandleCache->Vector256UIntHandle)
{
simdBaseJitType = CORINFO_TYPE_UINT;
size = Vector256SizeBytes;
JITDUMP(" Known type Vector256<uint>\n");
}
else if (typeHnd == m_simdHandleCache->Vector256ShortHandle)
{
simdBaseJitType = CORINFO_TYPE_SHORT;
size = Vector256SizeBytes;
JITDUMP(" Known type Vector256<short>\n");
}
else if (typeHnd == m_simdHandleCache->Vector256UShortHandle)
{
simdBaseJitType = CORINFO_TYPE_USHORT;
size = Vector256SizeBytes;
JITDUMP(" Known type Vector256<ushort>\n");
}
else if (typeHnd == m_simdHandleCache->Vector256ByteHandle)
{
simdBaseJitType = CORINFO_TYPE_BYTE;
size = Vector256SizeBytes;
JITDUMP(" Known type Vector256<sbyte>\n");
}
else if (typeHnd == m_simdHandleCache->Vector256UByteHandle)
{
simdBaseJitType = CORINFO_TYPE_UBYTE;
size = Vector256SizeBytes;
JITDUMP(" Known type Vector256<byte>\n");
}
else if (typeHnd == m_simdHandleCache->Vector256LongHandle)
{
simdBaseJitType = CORINFO_TYPE_LONG;
size = Vector256SizeBytes;
JITDUMP(" Known type Vector256<long>\n");
}
else if (typeHnd == m_simdHandleCache->Vector256ULongHandle)
{
simdBaseJitType = CORINFO_TYPE_ULONG;
size = Vector256SizeBytes;
JITDUMP(" Known type Vector256<ulong>\n");
}
else
#endif // defined(TARGET_XARCH)
if (typeHnd == m_simdHandleCache->Vector128FloatHandle)
{
simdBaseJitType = CORINFO_TYPE_FLOAT;
size = Vector128SizeBytes;
JITDUMP(" Known type Vector128<float>\n");
}
else if (typeHnd == m_simdHandleCache->Vector128DoubleHandle)
{
simdBaseJitType = CORINFO_TYPE_DOUBLE;
size = Vector128SizeBytes;
JITDUMP(" Known type Vector128<double>\n");
}
else if (typeHnd == m_simdHandleCache->Vector128IntHandle)
{
simdBaseJitType = CORINFO_TYPE_INT;
size = Vector128SizeBytes;
JITDUMP(" Known type Vector128<int>\n");
}
else if (typeHnd == m_simdHandleCache->Vector128UIntHandle)
{
simdBaseJitType = CORINFO_TYPE_UINT;
size = Vector128SizeBytes;
JITDUMP(" Known type Vector128<uint>\n");
}
else if (typeHnd == m_simdHandleCache->Vector128ShortHandle)
{
simdBaseJitType = CORINFO_TYPE_SHORT;
size = Vector128SizeBytes;
JITDUMP(" Known type Vector128<short>\n");
}
else if (typeHnd == m_simdHandleCache->Vector128UShortHandle)
{
simdBaseJitType = CORINFO_TYPE_USHORT;
size = Vector128SizeBytes;
JITDUMP(" Known type Vector128<ushort>\n");
}
else if (typeHnd == m_simdHandleCache->Vector128ByteHandle)
{
simdBaseJitType = CORINFO_TYPE_BYTE;
size = Vector128SizeBytes;
JITDUMP(" Known type Vector128<sbyte>\n");
}
else if (typeHnd == m_simdHandleCache->Vector128UByteHandle)
{
simdBaseJitType = CORINFO_TYPE_UBYTE;
size = Vector128SizeBytes;
JITDUMP(" Known type Vector128<byte>\n");
}
else if (typeHnd == m_simdHandleCache->Vector128LongHandle)
{
simdBaseJitType = CORINFO_TYPE_LONG;
size = Vector128SizeBytes;
JITDUMP(" Known type Vector128<long>\n");
}
else if (typeHnd == m_simdHandleCache->Vector128ULongHandle)
{
simdBaseJitType = CORINFO_TYPE_ULONG;
size = Vector128SizeBytes;
JITDUMP(" Known type Vector128<ulong>\n");
}
else
#if defined(TARGET_ARM64)
if (typeHnd == m_simdHandleCache->Vector64FloatHandle)
{
simdBaseJitType = CORINFO_TYPE_FLOAT;
size = Vector64SizeBytes;
JITDUMP(" Known type Vector64<float>\n");
}
else if (typeHnd == m_simdHandleCache->Vector64DoubleHandle)
{
simdBaseJitType = CORINFO_TYPE_DOUBLE;
size = Vector64SizeBytes;
JITDUMP(" Known type Vector64<double>\n");
}
else if (typeHnd == m_simdHandleCache->Vector64IntHandle)
{
simdBaseJitType = CORINFO_TYPE_INT;
size = Vector64SizeBytes;
JITDUMP(" Known type Vector64<int>\n");
}
else if (typeHnd == m_simdHandleCache->Vector64UIntHandle)
{
simdBaseJitType = CORINFO_TYPE_UINT;
size = Vector64SizeBytes;
JITDUMP(" Known type Vector64<uint>\n");
}
else if (typeHnd == m_simdHandleCache->Vector64ShortHandle)
{
simdBaseJitType = CORINFO_TYPE_SHORT;
size = Vector64SizeBytes;
JITDUMP(" Known type Vector64<short>\n");
}
else if (typeHnd == m_simdHandleCache->Vector64UShortHandle)
{
simdBaseJitType = CORINFO_TYPE_USHORT;
size = Vector64SizeBytes;
JITDUMP(" Known type Vector64<ushort>\n");
}
else if (typeHnd == m_simdHandleCache->Vector64ByteHandle)
{
simdBaseJitType = CORINFO_TYPE_BYTE;
size = Vector64SizeBytes;
JITDUMP(" Known type Vector64<sbyte>\n");
}
else if (typeHnd == m_simdHandleCache->Vector64UByteHandle)
{
simdBaseJitType = CORINFO_TYPE_UBYTE;
size = Vector64SizeBytes;
JITDUMP(" Known type Vector64<byte>\n");
}
else if (typeHnd == m_simdHandleCache->Vector64LongHandle)
{
simdBaseJitType = CORINFO_TYPE_LONG;
size = Vector64SizeBytes;
JITDUMP(" Known type Vector64<long>\n");
}
else if (typeHnd == m_simdHandleCache->Vector64ULongHandle)
{
simdBaseJitType = CORINFO_TYPE_ULONG;
size = Vector64SizeBytes;
JITDUMP(" Known type Vector64<ulong>\n");
}
#endif // defined(TARGET_ARM64)
// slow path search
if (simdBaseJitType == CORINFO_TYPE_UNDEF)
{
// Doesn't match with any of the cached type handles.
const char* className = getClassNameFromMetadata(typeHnd, nullptr);
CORINFO_CLASS_HANDLE baseTypeHnd = getTypeInstantiationArgument(typeHnd, 0);
if (baseTypeHnd != nullptr)
{
CorInfoType type = info.compCompHnd->getTypeForPrimitiveNumericClass(baseTypeHnd);
JITDUMP("HW Intrinsic SIMD Candidate Type %s with Base Type %s\n", className,
getClassNameFromMetadata(baseTypeHnd, nullptr));
#if defined(TARGET_XARCH)
if (strcmp(className, "Vector256`1") == 0)
{
size = Vector256SizeBytes;
switch (type)
{
case CORINFO_TYPE_FLOAT:
m_simdHandleCache->Vector256FloatHandle = typeHnd;
simdBaseJitType = CORINFO_TYPE_FLOAT;
JITDUMP(" Found type Hardware Intrinsic SIMD Vector256<float>\n");
break;
case CORINFO_TYPE_DOUBLE:
m_simdHandleCache->Vector256DoubleHandle = typeHnd;
simdBaseJitType = CORINFO_TYPE_DOUBLE;
JITDUMP(" Found type Hardware Intrinsic SIMD Vector256<double>\n");
break;
case CORINFO_TYPE_INT:
m_simdHandleCache->Vector256IntHandle = typeHnd;
simdBaseJitType = CORINFO_TYPE_INT;
JITDUMP(" Found type Hardware Intrinsic SIMD Vector256<int>\n");
break;
case CORINFO_TYPE_UINT:
m_simdHandleCache->Vector256UIntHandle = typeHnd;
simdBaseJitType = CORINFO_TYPE_UINT;
JITDUMP(" Found type Hardware Intrinsic SIMD Vector256<uint>\n");
break;
case CORINFO_TYPE_SHORT:
m_simdHandleCache->Vector256ShortHandle = typeHnd;
simdBaseJitType = CORINFO_TYPE_SHORT;
JITDUMP(" Found type Hardware Intrinsic SIMD Vector256<short>\n");
break;
case CORINFO_TYPE_USHORT:
m_simdHandleCache->Vector256UShortHandle = typeHnd;
simdBaseJitType = CORINFO_TYPE_USHORT;
JITDUMP(" Found type Hardware Intrinsic SIMD Vector256<ushort>\n");
break;
case CORINFO_TYPE_LONG:
m_simdHandleCache->Vector256LongHandle = typeHnd;
simdBaseJitType = CORINFO_TYPE_LONG;
JITDUMP(" Found type Hardware Intrinsic SIMD Vector256<long>\n");
break;
case CORINFO_TYPE_ULONG:
m_simdHandleCache->Vector256ULongHandle = typeHnd;
simdBaseJitType = CORINFO_TYPE_ULONG;
JITDUMP(" Found type Hardware Intrinsic SIMD Vector256<ulong>\n");
break;
case CORINFO_TYPE_UBYTE:
m_simdHandleCache->Vector256UByteHandle = typeHnd;
simdBaseJitType = CORINFO_TYPE_UBYTE;
JITDUMP(" Found type Hardware Intrinsic SIMD Vector256<byte>\n");
break;
case CORINFO_TYPE_BYTE:
m_simdHandleCache->Vector256ByteHandle = typeHnd;
simdBaseJitType = CORINFO_TYPE_BYTE;
JITDUMP(" Found type Hardware Intrinsic SIMD Vector256<sbyte>\n");
break;
default:
JITDUMP(" Unknown Hardware Intrinsic SIMD Type Vector256<T>\n");
}
}
else
#endif // defined(TARGET_XARCH)
if (strcmp(className, "Vector128`1") == 0)
{
size = Vector128SizeBytes;
switch (type)
{
case CORINFO_TYPE_FLOAT:
m_simdHandleCache->Vector128FloatHandle = typeHnd;
simdBaseJitType = CORINFO_TYPE_FLOAT;
JITDUMP(" Found type Hardware Intrinsic SIMD Vector128<float>\n");
break;
case CORINFO_TYPE_DOUBLE:
m_simdHandleCache->Vector128DoubleHandle = typeHnd;
simdBaseJitType = CORINFO_TYPE_DOUBLE;
JITDUMP(" Found type Hardware Intrinsic SIMD Vector128<double>\n");
break;
case CORINFO_TYPE_INT:
m_simdHandleCache->Vector128IntHandle = typeHnd;
simdBaseJitType = CORINFO_TYPE_INT;
JITDUMP(" Found type Hardware Intrinsic SIMD Vector128<int>\n");
break;
case CORINFO_TYPE_UINT:
m_simdHandleCache->Vector128UIntHandle = typeHnd;
simdBaseJitType = CORINFO_TYPE_UINT;
JITDUMP(" Found type Hardware Intrinsic SIMD Vector128<uint>\n");
break;
case CORINFO_TYPE_SHORT:
m_simdHandleCache->Vector128ShortHandle = typeHnd;
simdBaseJitType = CORINFO_TYPE_SHORT;
JITDUMP(" Found type Hardware Intrinsic SIMD Vector128<short>\n");
break;
case CORINFO_TYPE_USHORT:
m_simdHandleCache->Vector128UShortHandle = typeHnd;
simdBaseJitType = CORINFO_TYPE_USHORT;
JITDUMP(" Found type Hardware Intrinsic SIMD Vector128<ushort>\n");
break;
case CORINFO_TYPE_LONG:
m_simdHandleCache->Vector128LongHandle = typeHnd;
simdBaseJitType = CORINFO_TYPE_LONG;
JITDUMP(" Found type Hardware Intrinsic SIMD Vector128<long>\n");
break;
case CORINFO_TYPE_ULONG:
m_simdHandleCache->Vector128ULongHandle = typeHnd;
simdBaseJitType = CORINFO_TYPE_ULONG;
JITDUMP(" Found type Hardware Intrinsic SIMD Vector128<ulong>\n");
break;
case CORINFO_TYPE_UBYTE:
m_simdHandleCache->Vector128UByteHandle = typeHnd;
simdBaseJitType = CORINFO_TYPE_UBYTE;
JITDUMP(" Found type Hardware Intrinsic SIMD Vector128<byte>\n");
break;
case CORINFO_TYPE_BYTE:
m_simdHandleCache->Vector128ByteHandle = typeHnd;
simdBaseJitType = CORINFO_TYPE_BYTE;
JITDUMP(" Found type Hardware Intrinsic SIMD Vector128<sbyte>\n");
break;
default:
JITDUMP(" Unknown Hardware Intrinsic SIMD Type Vector128<T>\n");
}
}
#if defined(TARGET_ARM64)
else if (strcmp(className, "Vector64`1") == 0)
{
size = Vector64SizeBytes;
switch (type)
{
case CORINFO_TYPE_FLOAT:
m_simdHandleCache->Vector64FloatHandle = typeHnd;
simdBaseJitType = CORINFO_TYPE_FLOAT;
JITDUMP(" Found type Hardware Intrinsic SIMD Vector64<float>\n");
break;
case CORINFO_TYPE_DOUBLE:
m_simdHandleCache->Vector64DoubleHandle = typeHnd;
simdBaseJitType = CORINFO_TYPE_DOUBLE;
JITDUMP(" Found type Hardware Intrinsic SIMD Vector64<double>\n");
break;
case CORINFO_TYPE_INT:
m_simdHandleCache->Vector64IntHandle = typeHnd;
simdBaseJitType = CORINFO_TYPE_INT;
JITDUMP(" Found type Hardware Intrinsic SIMD Vector64<int>\n");
break;
case CORINFO_TYPE_UINT:
m_simdHandleCache->Vector64UIntHandle = typeHnd;
simdBaseJitType = CORINFO_TYPE_UINT;
JITDUMP(" Found type Hardware Intrinsic SIMD Vector64<uint>\n");
break;
case CORINFO_TYPE_SHORT:
m_simdHandleCache->Vector64ShortHandle = typeHnd;
simdBaseJitType = CORINFO_TYPE_SHORT;
JITDUMP(" Found type Hardware Intrinsic SIMD Vector64<short>\n");
break;
case CORINFO_TYPE_USHORT:
m_simdHandleCache->Vector64UShortHandle = typeHnd;
simdBaseJitType = CORINFO_TYPE_USHORT;
JITDUMP(" Found type Hardware Intrinsic SIMD Vector64<ushort>\n");
break;
case CORINFO_TYPE_LONG:
m_simdHandleCache->Vector64LongHandle = typeHnd;
simdBaseJitType = CORINFO_TYPE_LONG;
JITDUMP(" Found type Hardware Intrinsic SIMD Vector64<long>\n");
break;
case CORINFO_TYPE_ULONG:
m_simdHandleCache->Vector64ULongHandle = typeHnd;
simdBaseJitType = CORINFO_TYPE_ULONG;
JITDUMP(" Found type Hardware Intrinsic SIMD Vector64<ulong>\n");
break;
case CORINFO_TYPE_UBYTE:
m_simdHandleCache->Vector64UByteHandle = typeHnd;
simdBaseJitType = CORINFO_TYPE_UBYTE;
JITDUMP(" Found type Hardware Intrinsic SIMD Vector64<byte>\n");
break;
case CORINFO_TYPE_BYTE:
m_simdHandleCache->Vector64ByteHandle = typeHnd;
simdBaseJitType = CORINFO_TYPE_BYTE;
JITDUMP(" Found type Hardware Intrinsic SIMD Vector64<sbyte>\n");
break;
default:
JITDUMP(" Unknown Hardware Intrinsic SIMD Type Vector64<T>\n");
}
}
#endif // defined(TARGET_ARM64)
}
}
#if defined(TARGET_XARCH)
// Even though Vector256 is TYP_SIMD32, if AVX isn't supported, then it must
// be treated as a regular struct
if (size == YMM_REGSIZE_BYTES && (simdBaseJitType != CORINFO_TYPE_UNDEF) &&
!compExactlyDependsOn(InstructionSet_AVX))
{
simdBaseJitType = CORINFO_TYPE_UNDEF;
}
#endif // TARGET_XARCH
}
#endif // FEATURE_HW_INTRINSICS
if (sizeBytes != nullptr)
{
*sizeBytes = size;
}
if (simdBaseJitType != CORINFO_TYPE_UNDEF)
{
setUsesSIMDTypes(true);
}
return simdBaseJitType;
}
//--------------------------------------------------------------------------------------
// getSIMDIntrinsicInfo: get SIMD intrinsic info given the method handle.
//
// Arguments:
// inOutTypeHnd - The handle of the type on which the method is invoked. This is an in-out param.
// methodHnd - The handle of the method we're interested in.
// sig - method signature info
// isNewObj - whether this call represents a newboj constructor call
// argCount - argument count - out pram
// simdBaseJitType - base JIT type of the intrinsic - out param
// sizeBytes - size of SIMD vector type on which the method is invoked - out param
//
// Return Value:
// SIMDIntrinsicInfo struct initialized corresponding to methodHnd.
// Sets SIMDIntrinsicInfo.id to SIMDIntrinsicInvalid if methodHnd doesn't correspond
// to any SIMD intrinsic. Also, sets the out params inOutTypeHnd, argCount, baseType and
// sizeBytes.
//
// Note that VectorMath class doesn't have a base type and first argument of the method
// determines the SIMD vector type on which intrinsic is invoked. In such a case inOutTypeHnd
// is modified by this routine.
//
// TODO-Throughput: The current implementation is based on method name string parsing.
// Although we now have type identification from the VM, the parsing of intrinsic names
// could be made more efficient.
//
const SIMDIntrinsicInfo* Compiler::getSIMDIntrinsicInfo(CORINFO_CLASS_HANDLE* inOutTypeHnd,
CORINFO_METHOD_HANDLE methodHnd,
CORINFO_SIG_INFO* sig,
bool isNewObj,
unsigned* argCount,
CorInfoType* simdBaseJitType,
unsigned* sizeBytes)
{
assert(featureSIMD);
assert(simdBaseJitType != nullptr);
assert(sizeBytes != nullptr);
// get simdBaseJitType and size of the type
CORINFO_CLASS_HANDLE typeHnd = *inOutTypeHnd;
*simdBaseJitType = getBaseJitTypeAndSizeOfSIMDType(typeHnd, sizeBytes);
if (typeHnd == m_simdHandleCache->SIMDVectorHandle)
{
// All of the supported intrinsics on this static class take a first argument that's a vector,
// which determines the simdBaseJitType.
// The exception is the IsHardwareAccelerated property, which is handled as a special case.
assert(*simdBaseJitType == CORINFO_TYPE_UNDEF);
if (sig->numArgs == 0)
{
const SIMDIntrinsicInfo* hwAccelIntrinsicInfo = &(simdIntrinsicInfoArray[SIMDIntrinsicHWAccel]);
if ((strcmp(eeGetMethodName(methodHnd, nullptr), hwAccelIntrinsicInfo->methodName) == 0) &&
JITtype2varType(sig->retType) == hwAccelIntrinsicInfo->retType)
{
// Sanity check
assert(hwAccelIntrinsicInfo->argCount == 0 && hwAccelIntrinsicInfo->isInstMethod == false);
return hwAccelIntrinsicInfo;
}
return nullptr;
}
else
{
typeHnd = info.compCompHnd->getArgClass(sig, sig->args);
*inOutTypeHnd = typeHnd;
*simdBaseJitType = getBaseJitTypeAndSizeOfSIMDType(typeHnd, sizeBytes);
}
}
if (*simdBaseJitType == CORINFO_TYPE_UNDEF)
{
JITDUMP("NOT a SIMD Intrinsic: unsupported baseType\n");
return nullptr;
}
var_types simdBaseType = JitType2PreciseVarType(*simdBaseJitType);
// account for implicit "this" arg
*argCount = sig->numArgs;
if (sig->hasThis())
{
*argCount += 1;
}
// Get the Intrinsic Id by parsing method name.
//
// TODO-Throughput: replace sequential search by binary search by arranging entries
// sorted by method name.
SIMDIntrinsicID intrinsicId = SIMDIntrinsicInvalid;
const char* methodName = eeGetMethodName(methodHnd, nullptr);
for (int i = SIMDIntrinsicNone + 1; i < SIMDIntrinsicInvalid; ++i)
{
if (strcmp(methodName, simdIntrinsicInfoArray[i].methodName) == 0)
{
// Found an entry for the method; further check whether it is one of
// the supported base types.
bool found = false;
for (int j = 0; j < SIMD_INTRINSIC_MAX_BASETYPE_COUNT; ++j)
{
// Convention: if there are fewer base types supported than MAX_BASETYPE_COUNT,
// the end of the list is marked by TYP_UNDEF.
if (simdIntrinsicInfoArray[i].supportedBaseTypes[j] == TYP_UNDEF)
{
break;
}
if (simdIntrinsicInfoArray[i].supportedBaseTypes[j] == simdBaseType)
{
found = true;
break;
}
}
if (!found)
{
continue;
}
// Now, check the arguments.
unsigned int fixedArgCnt = simdIntrinsicInfoArray[i].argCount;
unsigned int expectedArgCnt = fixedArgCnt;
// First handle SIMDIntrinsicInitN, where the arg count depends on the type.
// The listed arg types include the vector and the first two init values, which is the expected number
// for Vector2. For other cases, we'll check their types here.
if (*argCount > expectedArgCnt)
{
if (i == SIMDIntrinsicInitN)
{
if (*argCount == 3 && typeHnd == m_simdHandleCache->SIMDVector2Handle)
{
expectedArgCnt = 3;
}
else if (*argCount == 4 && typeHnd == m_simdHandleCache->SIMDVector3Handle)
{
expectedArgCnt = 4;
}
else if (*argCount == 5 && typeHnd == m_simdHandleCache->SIMDVector4Handle)
{
expectedArgCnt = 5;
}
}
else if (i == SIMDIntrinsicInitFixed)
{
if (*argCount == 4 && typeHnd == m_simdHandleCache->SIMDVector4Handle)
{
expectedArgCnt = 4;
}
}
}
if (*argCount != expectedArgCnt)
{
continue;
}
// Validate the types of individual args passed are what is expected of.
// If any of the types don't match with what is expected, don't consider
// as an intrinsic. This will make an older JIT with SIMD capabilities
// resilient to breaking changes to SIMD managed API.
//
// Note that from IL type stack, args get popped in right to left order
// whereas args get listed in method signatures in left to right order.
int stackIndex = (expectedArgCnt - 1);
// Track the arguments from the signature - we currently only use this to distinguish
// integral and pointer types, both of which will by TYP_I_IMPL on the importer stack.
CORINFO_ARG_LIST_HANDLE argLst = sig->args;
CORINFO_CLASS_HANDLE argClass;
for (unsigned int argIndex = 0; found == true && argIndex < expectedArgCnt; argIndex++)
{
bool isThisPtr = ((argIndex == 0) && sig->hasThis());
// In case of "newobj SIMDVector<T>(T val)", thisPtr won't be present on type stack.
// We don't check anything in that case.
if (!isThisPtr || !isNewObj)