-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathCodeGen_X86.cpp
More file actions
1929 lines (1674 loc) · 79.8 KB
/
Copy pathCodeGen_X86.cpp
File metadata and controls
1929 lines (1674 loc) · 79.8 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
#include "CodeGen_CPU.h"
#include "CodeGen_Internal.h"
#include "ConciseCasts.h"
#include "ConstantBounds.h"
#include "Debug.h"
#include "IRMatch.h"
#include "IRMutator.h"
#include "IROperator.h"
#include "LLVM_Headers.h"
#include "Simplify.h"
#include "Substitute.h"
#include "Util.h"
#include <algorithm>
namespace Halide {
namespace Internal {
using std::pair;
using std::string;
using std::vector;
using namespace Halide::ConciseCasts;
using namespace llvm;
#if defined(WITH_X86)
namespace {
/** A code generator that emits x86 code from a given Halide stmt. */
class CodeGen_X86 : public CodeGen_CPU {
public:
/** Create an x86 code generator. Processor features can be
* enabled using the appropriate flags in the target struct. */
CodeGen_X86(Target);
protected:
string mcpu_target() const override;
string mcpu_tune() const override;
string mattrs() const override;
bool use_soft_float_abi() const override;
int native_vector_bits() const override;
int vector_lanes_for_slice(const Type &t) const;
using CodeGen_CPU::visit;
void init_module() override;
void emit_streaming_store_fence() override;
/** Nodes for which we want to emit specific sse/avx intrinsics */
// @{
void visit(const Add *) override;
void visit(const Sub *) override;
void visit(const Cast *) override;
void visit(const Call *) override;
void visit(const GT *) override;
void visit(const LT *) override;
void visit(const LE *) override;
void visit(const GE *) override;
void visit(const EQ *) override;
void visit(const NE *) override;
void visit(const Select *) override;
void visit(const Allocate *) override;
void visit(const Load *) override;
void visit(const Store *) override;
void codegen_vector_reduce(const VectorReduce *, const Expr &init) override;
// @}
std::vector<llvm::Value *> deinterleave_vector(llvm::Value *, int) override;
llvm::Value *interleave_vectors(const std::vector<llvm::Value *> &) override;
private:
Scope<MemoryType> mem_type;
};
CodeGen_X86::CodeGen_X86(Target t)
: CodeGen_CPU(t) {
}
const int max_intrinsic_args = 6;
struct x86Intrinsic {
const char *intrin_name;
Type ret_type;
const char *name;
Type arg_types[max_intrinsic_args];
Target::Feature feature = Target::FeatureEnd;
uint32_t flags = 0;
enum Options {
AccessesMemory = 1 << 0,
};
};
const x86Intrinsic intrinsic_defs[] = {
// AVX2/SSSE3 LLVM intrinsics for pabs fail in JIT. The integer wrappers
// just call `llvm.abs` (which requires a second argument).
// AVX512BW's pabs instructions aren't directly exposed by LLVM.
{"abs_i8x64", UInt(8, 64), "abs", {Int(8, 64)}, Target::AVX512_Skylake},
{"abs_i16x32", UInt(16, 32), "abs", {Int(16, 32)}, Target::AVX512_Skylake},
{"abs_i32x16", UInt(32, 16), "abs", {Int(32, 16)}, Target::AVX512_Skylake},
{"abs_i8x32", UInt(8, 32), "abs", {Int(8, 32)}, Target::AVX2},
{"abs_i16x16", UInt(16, 16), "abs", {Int(16, 16)}, Target::AVX2},
{"abs_i32x8", UInt(32, 8), "abs", {Int(32, 8)}, Target::AVX2},
{"abs_f32x8", Float(32, 8), "abs", {Float(32, 8)}, Target::AVX2},
{"abs_i8x16", UInt(8, 16), "abs", {Int(8, 16)}, Target::SSE41},
{"abs_i16x8", UInt(16, 8), "abs", {Int(16, 8)}, Target::SSE41},
{"abs_i32x4", UInt(32, 4), "abs", {Int(32, 4)}, Target::SSE41},
{"abs_f32x4", Float(32, 4), "abs", {Float(32, 4)}},
{"round_f32x4", Float(32, 4), "round", {Float(32, 4)}, Target::SSE41},
{"round_f64x2", Float(64, 2), "round", {Float(64, 2)}, Target::SSE41},
{"round_f32x8", Float(32, 8), "round", {Float(32, 8)}, Target::AVX},
{"round_f64x4", Float(64, 4), "round", {Float(64, 4)}, Target::AVX},
{"llvm.sadd.sat.v64i8", Int(8, 64), "saturating_add", {Int(8, 64), Int(8, 64)}, Target::AVX512_Skylake},
{"llvm.sadd.sat.v32i8", Int(8, 32), "saturating_add", {Int(8, 32), Int(8, 32)}, Target::AVX2},
{"llvm.sadd.sat.v16i8", Int(8, 16), "saturating_add", {Int(8, 16), Int(8, 16)}},
{"llvm.sadd.sat.v8i8", Int(8, 8), "saturating_add", {Int(8, 8), Int(8, 8)}},
{"llvm.ssub.sat.v64i8", Int(8, 64), "saturating_sub", {Int(8, 64), Int(8, 64)}, Target::AVX512_Skylake},
{"llvm.ssub.sat.v32i8", Int(8, 32), "saturating_sub", {Int(8, 32), Int(8, 32)}, Target::AVX2},
{"llvm.ssub.sat.v16i8", Int(8, 16), "saturating_sub", {Int(8, 16), Int(8, 16)}},
{"llvm.ssub.sat.v8i8", Int(8, 8), "saturating_sub", {Int(8, 8), Int(8, 8)}},
{"llvm.sadd.sat.v32i16", Int(16, 32), "saturating_add", {Int(16, 32), Int(16, 32)}, Target::AVX512_Skylake},
{"llvm.sadd.sat.v16i16", Int(16, 16), "saturating_add", {Int(16, 16), Int(16, 16)}, Target::AVX2},
{"llvm.sadd.sat.v8i16", Int(16, 8), "saturating_add", {Int(16, 8), Int(16, 8)}},
{"llvm.ssub.sat.v32i16", Int(16, 32), "saturating_sub", {Int(16, 32), Int(16, 32)}, Target::AVX512_Skylake},
{"llvm.ssub.sat.v16i16", Int(16, 16), "saturating_sub", {Int(16, 16), Int(16, 16)}, Target::AVX2},
{"llvm.ssub.sat.v8i16", Int(16, 8), "saturating_sub", {Int(16, 8), Int(16, 8)}},
// Sum of absolute differences
{"llvm.x86.sse2.psad.bw", UInt(64, 2), "sum_of_absolute_differences", {UInt(8, 16), UInt(8, 16)}},
{"llvm.x86.avx2.psad.bw", UInt(64, 4), "sum_of_absolute_differences", {UInt(8, 32), UInt(8, 32)}, Target::AVX2},
{"llvm.x86.avx512.psad.bw.512", UInt(64, 8), "sum_of_absolute_differences", {UInt(8, 64), UInt(8, 64)}, Target::AVX512_Skylake},
// Some of the instructions referred to below only appear with
// AVX2, but LLVM generates better AVX code if you give it
// full 256-bit vectors and let it do the slicing up into
// individual instructions itself. This is why we use
// Target::AVX instead of Target::AVX2 as the feature flag
// requirement.
// TODO: Just use llvm.*add/*sub.sat, and verify the above comment?
{"llvm.uadd.sat.v64i8", UInt(8, 64), "saturating_add", {UInt(8, 64), UInt(8, 64)}, Target::AVX512_Skylake},
{"paddusbx32", UInt(8, 32), "saturating_add", {UInt(8, 32), UInt(8, 32)}, Target::AVX},
{"paddusbx16", UInt(8, 16), "saturating_add", {UInt(8, 16), UInt(8, 16)}},
{"llvm.usub.sat.v64i8", UInt(8, 64), "saturating_sub", {UInt(8, 64), UInt(8, 64)}, Target::AVX512_Skylake},
{"psubusbx32", UInt(8, 32), "saturating_sub", {UInt(8, 32), UInt(8, 32)}, Target::AVX},
{"psubusbx16", UInt(8, 16), "saturating_sub", {UInt(8, 16), UInt(8, 16)}},
{"llvm.uadd.sat.v32i16", UInt(16, 32), "saturating_add", {UInt(16, 32), UInt(16, 32)}, Target::AVX512_Skylake},
{"padduswx16", UInt(16, 16), "saturating_add", {UInt(16, 16), UInt(16, 16)}, Target::AVX},
{"padduswx8", UInt(16, 8), "saturating_add", {UInt(16, 8), UInt(16, 8)}},
{"llvm.usub.sat.v32i16", UInt(16, 32), "saturating_sub", {UInt(16, 32), UInt(16, 32)}, Target::AVX512_Skylake},
{"psubuswx16", UInt(16, 16), "saturating_sub", {UInt(16, 16), UInt(16, 16)}, Target::AVX},
{"psubuswx8", UInt(16, 8), "saturating_sub", {UInt(16, 8), UInt(16, 8)}},
{"llvm.x86.avx512.pavg.b.512", UInt(8, 64), "rounding_halving_add", {UInt(8, 64), UInt(8, 64)}, Target::AVX512_Skylake},
{"llvm.x86.avx2.pavg.b", UInt(8, 32), "rounding_halving_add", {UInt(8, 32), UInt(8, 32)}, Target::AVX2},
{"llvm.x86.sse2.pavg.b", UInt(8, 16), "rounding_halving_add", {UInt(8, 16), UInt(8, 16)}},
{"llvm.x86.avx512.pavg.w.512", UInt(16, 32), "rounding_halving_add", {UInt(16, 32), UInt(16, 32)}, Target::AVX512_Skylake},
{"llvm.x86.avx2.pavg.w", UInt(16, 16), "rounding_halving_add", {UInt(16, 16), UInt(16, 16)}, Target::AVX2},
{"llvm.x86.sse2.pavg.w", UInt(16, 8), "rounding_halving_add", {UInt(16, 8), UInt(16, 8)}},
{"packssdwx16", Int(16, 16), "saturating_narrow", {Int(32, 16)}, Target::AVX2},
{"packssdwx8", Int(16, 8), "saturating_narrow", {Int(32, 8)}},
{"packsswbx32", Int(8, 32), "saturating_narrow", {Int(16, 32)}, Target::AVX2},
{"packsswbx16", Int(8, 16), "saturating_narrow", {Int(16, 16)}},
{"packusdwx16", UInt(16, 16), "saturating_narrow", {Int(32, 16)}, Target::AVX2},
{"packusdwx8", UInt(16, 8), "saturating_narrow", {Int(32, 8)}, Target::SSE41},
{"packuswbx32", UInt(8, 32), "saturating_narrow", {Int(16, 32)}, Target::AVX2},
{"packuswbx16", UInt(8, 16), "saturating_narrow", {Int(16, 16)}},
// Widening multiplies that use (v)pmaddwd
{"wmul_pmaddwd_avx512", Int(32, 16), "widening_mul", {Int(16, 16), Int(16, 16)}, Target::AVX512_Skylake},
{"wmul_pmaddwd_avx2", Int(32, 8), "widening_mul", {Int(16, 8), Int(16, 8)}, Target::AVX2},
{"wmul_pmaddwd_sse2", Int(32, 4), "widening_mul", {Int(16, 4), Int(16, 4)}},
// Multiply keep high half
{"llvm.x86.avx512.pmulh.w.512", Int(16, 32), "pmulh", {Int(16, 32), Int(16, 32)}, Target::AVX512_Skylake},
{"llvm.x86.avx2.pmulh.w", Int(16, 16), "pmulh", {Int(16, 16), Int(16, 16)}, Target::AVX2},
{"llvm.x86.avx512.pmulhu.w.512", UInt(16, 32), "pmulh", {UInt(16, 32), UInt(16, 32)}, Target::AVX512_Skylake},
{"llvm.x86.avx2.pmulhu.w", UInt(16, 16), "pmulh", {UInt(16, 16), UInt(16, 16)}, Target::AVX2},
{"llvm.x86.avx512.pmul.hr.sw.512", Int(16, 32), "pmulhrs", {Int(16, 32), Int(16, 32)}, Target::AVX512_Skylake},
{"llvm.x86.avx2.pmul.hr.sw", Int(16, 16), "pmulhrs", {Int(16, 16), Int(16, 16)}, Target::AVX2},
{"llvm.x86.sse2.pmulh.w", Int(16, 8), "pmulh", {Int(16, 8), Int(16, 8)}},
{"llvm.x86.sse2.pmulhu.w", UInt(16, 8), "pmulh", {UInt(16, 8), UInt(16, 8)}},
{"llvm.x86.ssse3.pmul.hr.sw.128", Int(16, 8), "pmulhrs", {Int(16, 8), Int(16, 8)}, Target::SSE41},
// As of LLVM main September 5 2023, LLVM only has partial handling of
// bfloat16. The below rules will match fine for simple examples, but bfloat
// conversion will get folded through any nearby shuffles and cause
// unimplemented errors in llvm's x86 instruction selection for the shuffle
// node. Disabling them for now. See https://github.com/halide/Halide/issues/7219
/*
// Convert FP32 to BF16
{"vcvtne2ps2bf16x32", BFloat(16, 32), "f32_to_bf16", {Float(32, 32)}, Target::AVX512_Zen4},
{"llvm.x86.avx512bf16.cvtneps2bf16.512", BFloat(16, 16), "f32_to_bf16", {Float(32, 16)}, Target::AVX512_Zen4},
{"llvm.x86.avx512bf16.cvtneps2bf16.256", BFloat(16, 8), "f32_to_bf16", {Float(32, 8)}, Target::AVX512_Zen4},
// LLVM does not provide an unmasked 128bit cvtneps2bf16 intrinsic, so provide a wrapper around the masked version.
{"vcvtneps2bf16x4", BFloat(16, 4), "f32_to_bf16", {Float(32, 4)}, Target::AVX512_Zen4},
*/
// 2-way dot products
{"llvm.x86.avx2.pmadd.ub.sw", Int(16, 16), "saturating_dot_product", {UInt(8, 32), Int(8, 32)}, Target::AVX2},
{"llvm.x86.ssse3.pmadd.ub.sw.128", Int(16, 8), "saturating_dot_product", {UInt(8, 16), Int(8, 16)}, Target::SSE41},
// Horizontal widening adds using 2-way dot products.
{"hadd_pmadd_u8_avx512", UInt(16, 32), "horizontal_widening_add", {UInt(8, 64)}, Target::AVX512_Skylake},
{"hadd_pmadd_u8_avx512", Int(16, 32), "horizontal_widening_add", {UInt(8, 64)}, Target::AVX512_Skylake},
{"hadd_pmadd_i8_avx512", Int(16, 32), "horizontal_widening_add", {Int(8, 64)}, Target::AVX512_Skylake},
{"hadd_pmadd_u8_avx2", UInt(16, 16), "horizontal_widening_add", {UInt(8, 32)}, Target::AVX2},
{"hadd_pmadd_u8_avx2", Int(16, 16), "horizontal_widening_add", {UInt(8, 32)}, Target::AVX2},
{"hadd_pmadd_i8_avx2", Int(16, 16), "horizontal_widening_add", {Int(8, 32)}, Target::AVX2},
{"hadd_pmadd_u8_sse3", UInt(16, 8), "horizontal_widening_add", {UInt(8, 16)}, Target::SSE41},
{"hadd_pmadd_u8_sse3", Int(16, 8), "horizontal_widening_add", {UInt(8, 16)}, Target::SSE41},
{"hadd_pmadd_i8_sse3", Int(16, 8), "horizontal_widening_add", {Int(8, 16)}, Target::SSE41},
{"hadd_pmadd_i16_avx512", Int(32, 16), "horizontal_widening_add", {Int(16, 32)}, Target::AVX512_Skylake},
{"hadd_pmadd_i16_avx2", Int(32, 8), "horizontal_widening_add", {Int(16, 16)}, Target::AVX2},
{"hadd_pmadd_i16_sse2", Int(32, 4), "horizontal_widening_add", {Int(16, 8)}},
{"llvm.x86.avx512.pmaddw.d.512", Int(32, 16), "dot_product", {Int(16, 32), Int(16, 32)}, Target::AVX512_Skylake},
{"llvm.x86.avx2.pmadd.wd", Int(32, 8), "dot_product", {Int(16, 16), Int(16, 16)}, Target::AVX2},
{"llvm.x86.sse2.pmadd.wd", Int(32, 4), "dot_product", {Int(16, 8), Int(16, 8)}},
// 4-way dot product vector reduction
// The LLVM intrinsics combine the bf16 pairs into i32, so provide a wrapper to correctly call the intrinsic.
// Currently, all targets which support avx_vnni inherit AVX512_Zen4, which also implies avx512vl.
// This means AVX512_Zen4 can cover all 128, 256, 512 bit vectors of bf16 and vnni.
{"dpbf16psx16", Float(32, 16), "dot_product", {Float(32, 16), BFloat(16, 32), BFloat(16, 32)}, Target::AVX512_Zen4},
{"dpbf16psx8", Float(32, 8), "dot_product", {Float(32, 8), BFloat(16, 16), BFloat(16, 16)}, Target::AVX512_Zen4},
{"dpbf16psx4", Float(32, 4), "dot_product", {Float(32, 4), BFloat(16, 8), BFloat(16, 8)}, Target::AVX512_Zen4},
{"dpbusdx16", Int(32, 16), "dot_product", {Int(32, 16), UInt(8, 64), Int(8, 64)}, Target::AVX512_Zen4},
{"dpbusdx8", Int(32, 8), "dot_product", {Int(32, 8), UInt(8, 32), Int(8, 32)}, Target::AVX512_Zen4},
{"dpbusdx4", Int(32, 4), "dot_product", {Int(32, 4), UInt(8, 16), Int(8, 16)}, Target::AVX512_Zen4},
{"dpwssdx16", Int(32, 16), "dot_product", {Int(32, 16), Int(16, 32), Int(16, 32)}, Target::AVX512_Zen4},
{"dpwssdx8", Int(32, 8), "dot_product", {Int(32, 8), Int(16, 16), Int(16, 16)}, Target::AVX512_Zen4},
{"dpwssdx4", Int(32, 4), "dot_product", {Int(32, 4), Int(16, 8), Int(16, 8)}, Target::AVX512_Zen4},
{"dpbusdsx16", Int(32, 16), "saturating_dot_product", {Int(32, 16), UInt(8, 64), Int(8, 64)}, Target::AVX512_Zen4},
{"dpbusdsx8", Int(32, 8), "saturating_dot_product", {Int(32, 8), UInt(8, 32), Int(8, 32)}, Target::AVX512_Zen4},
{"dpbusdsx4", Int(32, 4), "saturating_dot_product", {Int(32, 4), UInt(8, 16), Int(8, 16)}, Target::AVX512_Zen4},
{"dpwssdsx16", Int(32, 16), "saturating_dot_product", {Int(32, 16), Int(16, 32), Int(16, 32)}, Target::AVX512_Zen4},
{"dpwssdsx8", Int(32, 8), "saturating_dot_product", {Int(32, 8), Int(16, 16), Int(16, 16)}, Target::AVX512_Zen4},
{"dpwssdsx4", Int(32, 4), "saturating_dot_product", {Int(32, 4), Int(16, 8), Int(16, 8)}, Target::AVX512_Zen4},
{"tileloadd64_i8", Int(8, 1024), "tile_load", {Int(16), Int(16), Handle(), Int(64), Int(64)}, Target::AVX512_SapphireRapids, x86Intrinsic::AccessesMemory},
{"tileloadd64_i8", UInt(8, 1024), "tile_load", {Int(16), Int(16), Handle(), Int(64), Int(64)}, Target::AVX512_SapphireRapids, x86Intrinsic::AccessesMemory},
{"tileloadd64_bf16", BFloat(16, 512), "tile_load", {Int(16), Int(16), Handle(), Int(64), Int(64)}, Target::AVX512_SapphireRapids, x86Intrinsic::AccessesMemory},
{"tdpbssd", Int(32, 256), "tile_matmul", {Int(16), Int(16), Int(16), Int(32, 256), Int(8, 1024), Int(8, 1024)}, Target::AVX512_SapphireRapids},
{"tdpbsud", Int(32, 256), "tile_matmul", {Int(16), Int(16), Int(16), Int(32, 256), Int(8, 1024), UInt(8, 1024)}, Target::AVX512_SapphireRapids},
{"tdpbusd", Int(32, 256), "tile_matmul", {Int(16), Int(16), Int(16), Int(32, 256), UInt(8, 1024), Int(8, 1024)}, Target::AVX512_SapphireRapids},
{"tdpbuud", Int(32, 256), "tile_matmul", {Int(16), Int(16), Int(16), Int(32, 256), UInt(8, 1024), UInt(8, 1024)}, Target::AVX512_SapphireRapids},
{"tdpbf16ps", Float(32, 256), "tile_matmul", {Int(16), Int(16), Int(16), Float(32, 256), BFloat(16, 512), BFloat(16, 512)}, Target::AVX512_SapphireRapids},
{"tilezero_i32", Int(32, 256), "tile_zero", {Int(16), Int(16)}, Target::AVX512_SapphireRapids},
{"tilezero_f32", Float(32, 256), "tile_zero", {Int(16), Int(16)}, Target::AVX512_SapphireRapids},
{"tilestored64_i32", Int(32), "tile_store", {Int(16), Int(16), Handle(), Int(64), Int(64), Int(32, 256)}, Target::AVX512_SapphireRapids, x86Intrinsic::AccessesMemory},
{"tilestored64_f32", Int(32), "tile_store", {Int(16), Int(16), Handle(), Int(64), Int(64), Float(32, 256)}, Target::AVX512_SapphireRapids, x86Intrinsic::AccessesMemory},
};
void CodeGen_X86::init_module() {
CodeGen_CPU::init_module();
for (const x86Intrinsic &i : intrinsic_defs) {
if (i.feature != Target::FeatureEnd && !target.has_feature(i.feature)) {
continue;
}
Type ret_type = i.ret_type;
vector<Type> arg_types;
arg_types.reserve(max_intrinsic_args);
for (const Type &j : i.arg_types) {
if (j.bits() == 0) {
break;
}
arg_types.emplace_back(j);
}
auto *fn = declare_intrin_overload(i.name, ret_type, i.intrin_name, std::move(arg_types));
if ((i.flags & x86Intrinsic::AccessesMemory) == 0) {
function_does_not_access_memory(fn);
}
fn->addFnAttr(llvm::Attribute::NoUnwind);
}
}
void CodeGen_X86::emit_streaming_store_fence() {
llvm::Function *sfence = llvm::Intrinsic::getOrInsertDeclaration(module.get(), llvm::Intrinsic::x86_sse_sfence);
builder->CreateCall(sfence);
}
// i32(i16_a)*i32(i16_b) +/- i32(i16_c)*i32(i16_d) can be done by
// interleaving a, c, and b, d, and then using dot_product.
bool should_use_dot_product(const Expr &a, const Expr &b, vector<Expr> &result) {
Type t = a.type();
internal_assert(b.type() == t);
if (!(t.is_int() && t.bits() == 32 && t.lanes() >= 4)) {
return false;
}
const Call *ma = Call::as_intrinsic(a, {Call::widening_mul});
const Call *mb = Call::as_intrinsic(b, {Call::widening_mul});
// dot_product can't handle mixed type widening muls.
if (ma && ma->args[0].type() != ma->args[1].type()) {
return false;
}
if (mb && mb->args[0].type() != mb->args[1].type()) {
return false;
}
// If the operands are widening shifts, we might be able to treat these as
// multiplies.
const Call *sa = Call::as_intrinsic(a, {Call::widening_shift_left});
const Call *sb = Call::as_intrinsic(b, {Call::widening_shift_left});
if (sa && !is_const(sa->args[1])) {
sa = nullptr;
}
if (sb && !is_const(sb->args[1])) {
sb = nullptr;
}
if ((ma || sa) && (mb || sb)) {
Expr a0 = ma ? ma->args[0] : sa->args[0];
Expr a1 = ma ? ma->args[1] : lossless_cast(sa->args[0].type(), simplify(make_const(sa->type, 1) << sa->args[1]));
Expr b0 = mb ? mb->args[0] : sb->args[0];
Expr b1 = mb ? mb->args[1] : lossless_cast(sb->args[0].type(), simplify(make_const(sb->type, 1) << sb->args[1]));
if (a1.defined() && b1.defined()) {
std::vector<Expr> args = {a0, a1, b0, b1};
result.swap(args);
return true;
}
}
return false;
}
void CodeGen_X86::visit(const Add *op) {
vector<Expr> matches;
if (should_use_dot_product(op->a, op->b, matches)) {
Expr ac = Shuffle::make_interleave({matches[0], matches[2]});
Expr bd = Shuffle::make_interleave({matches[1], matches[3]});
value = call_overloaded_intrin(op->type, "dot_product", {ac, bd});
if (value) {
return;
}
}
CodeGen_CPU::visit(op);
}
void CodeGen_X86::visit(const Sub *op) {
vector<Expr> matches;
if (should_use_dot_product(op->a, op->b, matches)) {
// Negate one of the factors in the second expression
Expr negative_2 = lossless_negate(matches[2]);
Expr negative_3 = lossless_negate(matches[3]);
if (negative_2.defined() || negative_3.defined()) {
if (negative_2.defined()) {
matches[2] = negative_2;
} else {
matches[3] = negative_3;
}
Expr ac = Shuffle::make_interleave({matches[0], matches[2]});
Expr bd = Shuffle::make_interleave({matches[1], matches[3]});
value = call_overloaded_intrin(op->type, "dot_product", {ac, bd});
if (value) {
return;
}
}
}
CodeGen_CPU::visit(op);
}
void CodeGen_X86::visit(const GT *op) {
Type t = op->a.type();
if (t.is_vector() &&
upgrade_type_for_arithmetic(t) == t) {
// Non-native vector widths get legalized poorly by llvm. We
// split it up ourselves.
int slice_size = vector_lanes_for_slice(t);
Value *a = codegen(op->a), *b = codegen(op->b);
vector<Value *> result;
for (int i = 0; i < op->type.lanes(); i += slice_size) {
Value *sa = slice_vector(a, i, slice_size);
Value *sb = slice_vector(b, i, slice_size);
Value *slice_value;
if (t.is_float()) {
ScopedFastMath guard(this);
slice_value = builder->CreateFCmpOGT(sa, sb);
} else if (t.is_int()) {
slice_value = builder->CreateICmpSGT(sa, sb);
} else {
slice_value = builder->CreateICmpUGT(sa, sb);
}
result.push_back(slice_value);
}
value = concat_vectors(result);
value = slice_vector(value, 0, t.lanes());
} else {
CodeGen_CPU::visit(op);
}
}
void CodeGen_X86::visit(const EQ *op) {
Type t = op->a.type();
if (t.is_vector() &&
upgrade_type_for_arithmetic(t) == t) {
// Non-native vector widths get legalized poorly by llvm. We
// split it up ourselves.
int slice_size = vector_lanes_for_slice(t);
Value *a = codegen(op->a), *b = codegen(op->b);
vector<Value *> result;
for (int i = 0; i < op->type.lanes(); i += slice_size) {
Value *sa = slice_vector(a, i, slice_size);
Value *sb = slice_vector(b, i, slice_size);
Value *slice_value;
if (t.is_float()) {
ScopedFastMath guard(this);
slice_value = builder->CreateFCmpOEQ(sa, sb);
} else {
slice_value = builder->CreateICmpEQ(sa, sb);
}
result.push_back(slice_value);
}
value = concat_vectors(result);
value = slice_vector(value, 0, t.lanes());
} else {
CodeGen_CPU::visit(op);
}
}
void CodeGen_X86::visit(const LT *op) {
codegen(op->b > op->a);
}
void CodeGen_X86::visit(const LE *op) {
codegen(!(op->a > op->b));
}
void CodeGen_X86::visit(const GE *op) {
codegen(!(op->b > op->a));
}
void CodeGen_X86::visit(const NE *op) {
codegen(!(op->a == op->b));
}
void CodeGen_X86::visit(const Select *op) {
if (op->type.is_vector()) {
// LLVM handles selects on vector conditions much better at native width
Value *cond = codegen(op->condition);
Value *true_val = codegen(op->true_value);
Value *false_val = codegen(op->false_value);
Type t = op->true_value.type();
int slice_size = vector_lanes_for_slice(t);
vector<Value *> result;
for (int i = 0; i < t.lanes(); i += slice_size) {
Value *st = slice_vector(true_val, i, slice_size);
Value *sf = slice_vector(false_val, i, slice_size);
Value *sc = slice_vector(cond, i, slice_size);
Value *slice_value = builder->CreateSelect(sc, st, sf);
result.push_back(slice_value);
}
value = concat_vectors(result);
value = slice_vector(value, 0, t.lanes());
} else {
CodeGen_CPU::visit(op);
}
}
void CodeGen_X86::visit(const Cast *op) {
Type src = op->value.type();
Type dst = op->type;
if (target.has_feature(Target::F16C) &&
dst.code() == Type::Float &&
src.code() == Type::Float &&
(dst.bits() == 16 || src.bits() == 16) &&
src.bits() <= 32) { // Don't use for narrowing casts from double - it results in a libm call
// Node we use code() == Type::Float instead of is_float(), because we
// don't want to catch bfloat casts.
// This target doesn't support full float16 arithmetic, but it *does*
// support float16 casts, so we emit a vanilla LLVM cast node.
value = codegen(op->value);
ScopedFastMath guard(this);
value = builder->CreateFPCast(value, llvm_type_of(dst));
return;
}
if (!dst.is_vector()) {
// We only have peephole optimizations for vectors after this point.
CodeGen_CPU::visit(op);
return;
}
struct Pattern {
string intrin;
Expr pattern;
};
static Pattern patterns[] = {
// This isn't rounding_mul_shift_right(i16, i16, 15) because it doesn't
// saturate the result.
{"pmulhrs", i16(rounding_shift_right(widening_mul(wild_i16x_, wild_i16x_), 15))},
{"f32_to_bf16", bf16(wild_f32x_)},
};
vector<Expr> matches;
for (const Pattern &p : patterns) {
if (expr_match(p.pattern, op, matches)) {
value = call_overloaded_intrin(dst, p.intrin, matches);
if (value) {
return;
}
}
}
if (const Call *widening_op = Call::as_intrinsic(op->value, {Call::widening_mul, Call::widening_add, Call::widening_sub})) {
bool should_upcast_args_to_dst_type =
dst.can_represent(widening_op->args[0].type()) &&
dst.can_represent(widening_op->args[1].type()) &&
// LLVM/x86 really doesn't like 8 -> 16 bit multiplication. If we're
// widening to 32-bits after a widening multiply, LLVM prefers to see a
// widening multiply directly to 32-bits. This may result in extra
// casts, so simplify to remove them.
((widening_op->is_intrinsic(Call::widening_mul) &&
src.bits() < dst.bits() &&
dst.bits() <= 32) ||
// X86 doesn't have uint to float conversions before avx512
(!target.has_feature(Target::AVX512) &&
src.is_uint() &&
src.bits() >= 32 &&
dst.is_float()));
if (should_upcast_args_to_dst_type) {
Expr arg0 = Cast::make(dst, widening_op->args[0]);
Expr arg1 = Cast::make(dst, widening_op->args[1]);
Expr equiv;
if (widening_op->is_intrinsic(Call::widening_mul)) {
equiv = arg0 * arg1;
} else if (widening_op->is_intrinsic(Call::widening_add)) {
equiv = arg0 + arg1;
} else {
internal_assert(widening_op->is_intrinsic(Call::widening_sub));
equiv = arg0 - arg1;
}
value = codegen(simplify(equiv));
return;
}
}
CodeGen_CPU::visit(op);
}
void CodeGen_X86::visit(const Call *op) {
if (!op->type.is_vector()) {
// We only have peephole optimizations for vectors beyond this point.
CodeGen_CPU::visit(op);
return;
}
// A 16-bit mul-shift-right of less than 16 can sometimes be rounded up to a
// full 16 to use pmulh(u)w by left-shifting one of the operands. This is
// handled here instead of in the lowering of mul_shift_right because it's
// unlikely to be a good idea on platforms other than x86, as it adds an
// extra shift in the fully-lowered case.
if ((op->type.element_of() == UInt(16) ||
op->type.element_of() == Int(16)) &&
op->is_intrinsic(Call::mul_shift_right)) {
internal_assert(op->args.size() == 3);
auto shift = as_const_uint(op->args[2]);
if (shift && *shift < 16 && *shift >= 8) {
Type narrow = op->type.with_bits(8);
Expr narrow_a = lossless_cast(narrow, op->args[0]);
Expr narrow_b = narrow_a.defined() ? Expr() : lossless_cast(narrow, op->args[1]);
int shift_left = 16 - (int)(*shift);
if (narrow_a.defined()) {
codegen(mul_shift_right(op->args[0] << shift_left, op->args[1], 16));
return;
} else if (narrow_b.defined()) {
codegen(mul_shift_right(op->args[0], op->args[1] << shift_left, 16));
return;
}
}
} else if (op->type.is_int() &&
op->type.bits() <= 16 &&
op->is_intrinsic(Call::rounding_halving_add)) {
// We can redirect signed rounding halving add to unsigned rounding
// halving add by adding 128 / 32768 to the result if the sign of the
// args differs.
internal_assert(op->args.size() == 2);
Type t = op->type.with_code(halide_type_uint);
Expr a = cast(t, op->args[0]);
Expr b = cast(t, op->args[1]);
codegen(cast(op->type, rounding_halving_add(a, b) + ((a ^ b) & (1 << (t.bits() - 1)))));
return;
} else if (op->is_intrinsic(Call::absd)) {
internal_assert(op->args.size() == 2);
if (op->args[0].type().is_uint()) {
// On x86, there are many 3-instruction sequences to compute absd of
// unsigned integers. This one consists solely of instructions with
// throughput of 3 ops per cycle on Cannon Lake.
//
// Solution due to Wojciech Mula:
// http://0x80.pl/notesen/2018-03-11-sse-abs-unsigned.html
codegen(saturating_sub(op->args[0], op->args[1]) | saturating_sub(op->args[1], op->args[0]));
return;
} else if (op->args[0].type().is_int()) {
// In the signed case, we take the min/max, cast them to unsigned,
// and subtract. The cast to unsigned may wrap, but if it does, so
// will the subtract.
codegen(
cast(op->type, Max::make(op->args[0], op->args[1])) -
cast(op->type, Min::make(op->args[0], op->args[1])));
return;
}
}
struct Pattern {
string intrin;
Expr pattern;
};
static const Pattern patterns[] = {
{"pmulh", mul_shift_right(wild_i16x_, wild_i16x_, 16)},
{"pmulh", mul_shift_right(wild_u16x_, wild_u16x_, 16)},
{"saturating_narrow", i16_sat(wild_i32x_)},
{"saturating_narrow", u16_sat(wild_i32x_)},
{"saturating_narrow", i8_sat(wild_i16x_)},
{"saturating_narrow", u8_sat(wild_i16x_)},
};
vector<Expr> matches;
for (const auto &pattern : patterns) {
if (expr_match(pattern.pattern, op, matches)) {
value = call_overloaded_intrin(op->type, pattern.intrin, matches);
if (value) {
return;
}
}
}
if (op->is_intrinsic(Call::saturating_cast)) {
static const Pattern reinterpret_patterns[] = {
{"saturating_narrow", i16_sat(wild_u32x_)},
{"saturating_narrow", u16_sat(wild_u32x_)},
{"saturating_narrow", i8_sat(wild_u16x_)},
{"saturating_narrow", u8_sat(wild_u16x_)},
};
// Search for saturating casts where the inner value can be
// reinterpreted to signed, so that we can use existing
// saturating_narrow instructions.
for (const auto &pattern : reinterpret_patterns) {
if (expr_match(pattern.pattern, op, matches)) {
const Type signed_type = matches[0].type().with_code(halide_type_int);
Expr e = lossless_cast(signed_type, matches[0]);
if (e.defined()) {
// Can safely reinterpret to signed integer.
matches[0] = e;
value = call_overloaded_intrin(op->type, pattern.intrin, matches);
if (value) {
return;
}
}
// No reinterpret patterns match the same input, so stop matching.
break;
}
}
static const vector<pair<Expr, Expr>> cast_rewrites = {
// Some double-narrowing saturating casts can be better expressed as
// combinations of single-narrowing saturating casts.
{u8_sat(wild_i32x_), u8_sat(i16_sat(wild_i32x_))},
{i8_sat(wild_i32x_), i8_sat(i16_sat(wild_i32x_))},
{i8_sat(wild_u32x_), i8_sat(i16_sat(wild_u32x_))},
};
for (const auto &i : cast_rewrites) {
if (expr_match(i.first, op, matches)) {
Expr replacement = substitute("*", matches[0], with_lanes(i.second, op->type.lanes()));
value = codegen(replacement);
return;
}
}
}
// Check for saturating_pmulhrs. On x86, pmulhrs is truncating, but it's still faster
// to use pmulhrs than to lower (producing widening multiplication), and have a check
// for the singular overflow case.
static Expr saturating_pmulhrs = rounding_mul_shift_right(wild_i16x_, wild_i16x_, 15);
if (expr_match(saturating_pmulhrs, op, matches)) {
// Rewrite so that we can take advantage of pmulhrs.
internal_assert(matches.size() == 2);
internal_assert(op->type.element_of() == Int(16));
const Expr &a = matches[0];
const Expr &b = matches[1];
Expr pmulhrs = i16(rounding_shift_right(widening_mul(a, b), 15));
Expr i16_min = op->type.min();
Expr i16_max = op->type.max();
// Handle edge case of possible overflow.
// See https://github.com/halide/Halide/pull/7129/files#r1008331426
// On AVX512 (and with enough lanes) we can use a mask register.
ConstantInterval ca = constant_integer_bounds(a);
ConstantInterval cb = constant_integer_bounds(b);
if (!ca.contains(-32768) || !cb.contains(-32768)) {
// Overflow isn't possible
pmulhrs.accept(this);
} else if (target.has_feature(Target::AVX512) && op->type.lanes() >= 32) {
Expr expr = select((a == i16_min) && (b == i16_min), i16_max, pmulhrs);
expr.accept(this);
} else {
Expr mask = select(max(a, b) == i16_min, cast(op->type, -1), cast(op->type, 0));
Expr expr = mask ^ pmulhrs;
expr.accept(this);
}
return;
}
CodeGen_CPU::visit(op);
}
void CodeGen_X86::codegen_vector_reduce(const VectorReduce *op, const Expr &init) {
if (op->op != VectorReduce::Add && op->op != VectorReduce::SaturatingAdd) {
CodeGen_CPU::codegen_vector_reduce(op, init);
return;
}
const int factor = op->value.type().lanes() / op->type.lanes();
struct Pattern {
VectorReduce::Operator reduce_op;
int factor;
Expr pattern;
const char *intrin;
Type narrow_type;
uint32_t flags = 0;
enum {
CombineInit = 1 << 0,
SwapOperands = 1 << 1,
SingleArg = 1 << 2,
};
};
// These patterns are roughly sorted "best to worst", in case there are two
// patterns that match the expression.
static const Pattern patterns[] = {
// 4-way dot products
{VectorReduce::Add, 4, i32(widening_mul(wild_u8x_, wild_i8x_)), "dot_product", {}, Pattern::CombineInit},
{VectorReduce::Add, 4, i32(widening_mul(wild_i8x_, wild_u8x_)), "dot_product", {}, Pattern::CombineInit | Pattern::SwapOperands},
{VectorReduce::SaturatingAdd, 4, i32(widening_mul(wild_u8x_, wild_i8x_)), "saturating_dot_product", {}, Pattern::CombineInit},
{VectorReduce::SaturatingAdd, 4, i32(widening_mul(wild_i8x_, wild_u8x_)), "saturating_dot_product", {}, Pattern::CombineInit | Pattern::SwapOperands},
// 2-way dot products
{VectorReduce::Add, 2, i32(widening_mul(wild_i8x_, wild_i8x_)), "dot_product", Int(16)},
{VectorReduce::Add, 2, i32(widening_mul(wild_i8x_, wild_u8x_)), "dot_product", Int(16)},
{VectorReduce::Add, 2, i32(widening_mul(wild_u8x_, wild_i8x_)), "dot_product", Int(16)},
{VectorReduce::Add, 2, i32(widening_mul(wild_u8x_, wild_u8x_)), "dot_product", Int(16)},
{VectorReduce::SaturatingAdd, 2, i32(widening_mul(wild_u8x_, wild_i8x_)), "saturating_dot_product", {}, Pattern::CombineInit},
{VectorReduce::SaturatingAdd, 2, i32(widening_mul(wild_i8x_, wild_u8x_)), "saturating_dot_product", {}, Pattern::CombineInit | Pattern::SwapOperands},
{VectorReduce::SaturatingAdd, 2, widening_mul(wild_u8x_, wild_i8x_), "saturating_dot_product"},
{VectorReduce::SaturatingAdd, 2, widening_mul(wild_i8x_, wild_u8x_), "saturating_dot_product", {}, Pattern::SwapOperands},
{VectorReduce::Add, 2, i32(widening_mul(wild_i16x_, wild_i16x_)), "dot_product", {}, Pattern::CombineInit},
{VectorReduce::Add, 2, i32(widening_mul(wild_i16x_, wild_i16x_)), "dot_product", Int(16)},
{VectorReduce::SaturatingAdd, 2, i32(widening_mul(wild_i16x_, wild_i16x_)), "saturating_dot_product", {}, Pattern::CombineInit},
{VectorReduce::Add, 2, widening_mul(wild_bf16x_, wild_bf16x_), "dot_product", {}, Pattern::CombineInit},
{VectorReduce::Add, 2, wild_f32x_ * wild_f32x_, "dot_product", BFloat(16), Pattern::CombineInit},
// Horizontal widening addition using a dot_product against a vector of ones.
{VectorReduce::Add, 2, u16(wild_u8x_), "horizontal_widening_add", {}, Pattern::SingleArg},
{VectorReduce::Add, 2, i16(wild_u8x_), "horizontal_widening_add", {}, Pattern::SingleArg},
{VectorReduce::Add, 2, i16(wild_i8x_), "horizontal_widening_add", {}, Pattern::SingleArg},
{VectorReduce::Add, 2, i32(wild_i16x_), "horizontal_widening_add", {}, Pattern::SingleArg},
// Sum of absolute differences
{VectorReduce::Add, 8, u64(absd(wild_u8x_, wild_u8x_)), "sum_of_absolute_differences", {}},
};
std::vector<Expr> matches;
for (const Pattern &p : patterns) {
if (op->op != p.reduce_op || p.factor != factor) {
continue;
}
if (expr_match(p.pattern, op->value, matches)) {
if (p.flags & Pattern::SingleArg) {
Expr a = matches[0];
if (p.narrow_type.bits() > 0) {
a = lossless_cast(p.narrow_type.with_lanes(a.type().lanes()), a);
}
if (!a.defined()) {
continue;
}
if (init.defined() && (p.flags & Pattern::CombineInit)) {
value = call_overloaded_intrin(op->type, p.intrin, {init, a});
if (value) {
return;
}
} else {
value = call_overloaded_intrin(op->type, p.intrin, {a});
if (value) {
if (init.defined()) {
Value *x = value;
Value *y = codegen(init);
value = builder->CreateAdd(x, y);
}
return;
}
}
} else {
Expr a = matches[0];
Expr b = matches[1];
if (p.flags & Pattern::SwapOperands) {
std::swap(a, b);
}
if (p.narrow_type.bits() > 0) {
a = lossless_cast(p.narrow_type.with_lanes(a.type().lanes()), a);
b = lossless_cast(p.narrow_type.with_lanes(b.type().lanes()), b);
}
if (!a.defined() || !b.defined()) {
continue;
}
if (init.defined() && (p.flags & Pattern::CombineInit)) {
value = call_overloaded_intrin(op->type, p.intrin, {init, a, b});
if (value) {
return;
}
} else {
value = call_overloaded_intrin(op->type, p.intrin, {a, b});
if (value) {
if (init.defined()) {
Value *x = value;
Value *y = codegen(init);
value = builder->CreateAdd(x, y);
}
return;
}
}
}
}
}
// Rewrite non-native sum-of-absolute-difference variants to the native
// op. We support reducing to various types. We could consider supporting
// multiple reduction factors too, but in general we don't handle non-native
// reduction factors for VectorReduce nodes (yet?).
if (op->op == VectorReduce::Add &&
factor == 8) {
const Cast *cast = op->value.as<Cast>();
const Call *call = cast ? cast->value.as<Call>() : nullptr;
if (call &&
call->is_intrinsic(Call::absd) &&
cast->type.element_of().can_represent(UInt(8)) &&
(cast->type.is_int() || cast->type.is_uint()) &&
call->args[0].type().element_of() == UInt(8)) {
internal_assert(cast->type.element_of() != UInt(64)) << "Should have pattern-matched above\n";
// Cast to uint64 instead
Expr equiv = Cast::make(UInt(64, cast->value.type().lanes()), cast->value);
// Reduce on that to hit psadbw
equiv = VectorReduce::make(VectorReduce::Add, equiv, op->type.lanes());
// Then cast that to the desired type
equiv = Cast::make(cast->type.with_lanes(equiv.type().lanes()), equiv);
codegen(equiv);
return;
}
}
CodeGen_CPU::codegen_vector_reduce(op, init);
}
std::vector<Value *> CodeGen_X86::deinterleave_vector(Value *vec, int num_vecs) {
int vec_elements = get_vector_num_elements(vec->getType()) / num_vecs;
const size_t element_bits = vec->getType()->getScalarSizeInBits();
if (target.has_feature(Target::AVX) &&
is_power_of_two(num_vecs) &&
is_power_of_two(vec_elements) &&
(int)(vec_elements * num_vecs * element_bits) > native_vector_bits()) {
// Our interleaving logic below supports this case
std::vector<Value *> slices(vec_elements);
for (int i = 0; i < vec_elements; i++) {
slices[i] = slice_vector(vec, i * num_vecs, num_vecs);
}
vec = interleave_vectors(slices);
std::vector<Value *> result(num_vecs);
for (int i = 0; i < num_vecs; i++) {
result[i] = slice_vector(vec, i * vec_elements, vec_elements);
}
return result;
} else {
return CodeGen_CPU::deinterleave_vector(vec, num_vecs);
}
}
Value *CodeGen_X86::interleave_vectors(const std::vector<Value *> &vecs) {
// Only use x86-specific interleaving for AVX and above
if (vecs.empty() || !target.has_feature(Target::AVX)) {
return CodeGen_CPU::interleave_vectors(vecs);
}
if (vecs.size() == 1) {
return vecs[0];
}
// Get the element type and vector properties
llvm::Type *vec_type = vecs[0]->getType();
llvm::Type *element_type = get_vector_element_type(vec_type);
int vec_elements = get_vector_num_elements(vec_type);
const size_t element_bits = element_type->getScalarSizeInBits();
const size_t elems_per_native_vec = native_vector_bits() / element_bits;
const size_t elems_per_slice = 128 / element_bits;
// Only apply special x86 logic for power-of-two interleaves for avx and
// above where we're going to end up with multiple native vectors.
if (!is_power_of_two(vec_elements) &&
vec_elements % elems_per_native_vec == 0) {
// It's not a power of two, but it's a multiple of the native vector
// length, so slice it and recurse.
std::vector<Value *> results;
for (int i = 0; i < vec_elements; i += elems_per_native_vec) {
std::vector<Value *> slices;
slices.reserve(vecs.size());
for (auto *v : vecs) {
slices.push_back(slice_vector(v, i, (int)elems_per_native_vec));
}
results.push_back(interleave_vectors(slices));
}
return concat_vectors(results);
}
if (!is_power_of_two(vec_elements) ||
!is_power_of_two(vecs.size()) ||
(vecs.size() * vec_elements * element_bits) <= (size_t)native_vector_bits()) {
return CodeGen_CPU::interleave_vectors(vecs);
}
/*
x86 has a weird set of vector shuffle instructions due to historical
baggage, and the strategy in the base class for interleaving vectors
works poorly. Here we have a somewhat complex algorithm for generating
better sequences of shuffle instructions for avx and avx-512.
Consider the location of one of the elements of one of the vectors. It has
a vector index, which says which vector it's in, and a vector lane index,
which gives the lane. x86 shuffles work in terms of 128-bit subvectors,
which we will call slices. So we'll decompose that lane index into a slice
index, to identify the 128-bit slice within a vector, and the lane index
within that slice. For avx the slice index is either zero or one, and for
avx-512 it can be zero through three. Because we have limited everything
to be a power of two, we can write out these indices in binary. We'll use
v for the vector index, s for the slice index, and l for the lane
index. For an avx-512 interleave of 16 vectors of 32 elements each
(i.e. uint16s), a location could thus be written as:
[l0 l1 l2] [s0 s1] [v0 v1 v2 v3]
where l0 is the least-significant bit of the lane index, and so on.
An interleave takes the bits that give the vector index and moves them to
be the least significant bits, shifting everything else over. So the
indices of our vectors after the interleave should be:
[v0 v1 v2] [v3 l0] [l1 l2 s0 s1]
Assigning numbers to each according to their final location, we start with:
[4 5 6] [7 8] [0 1 2 3]
and we want to issue some sequence of instructions to get us to:
[0 1 2] [3 4] [5 6 7 8]
Now let's consider the instructions we have available. These generally