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
|
// |jit-test| skip-if: !wasmSimdEnabled() || wasmCompileMode() != "ion" || !this.wasmSimdAnalysis
// White-box tests for SIMD optimizations. These are sensitive to internal
// details of the front-end and lowering logic, which is partly platform-dependent.
//
// In DEBUG builds, the testing function wasmSimdAnalysis() returns a string
// describing the last decision made by the SIMD lowering code: to perform an
// optimized lowering or the default byte shuffle+blend for i8x16.shuffle; to
// shift by a constant or a variable for the various shifts; and so on.
//
// We test that the expected transformation applies, and that the machine code
// generates the expected result.
var isArm64 = getBuildConfiguration("arm64");
// 32-bit permutation that is not a rotation.
let perm32x4_pattern = [4, 5, 6, 7, 12, 13, 14, 15, 8, 9, 10, 11, 0, 1, 2, 3];
// Operands the same, dword permutation
{
let ins = wasmCompile(`
(module
(memory (export "mem") 1 1)
(func (export "run")
(v128.store (i32.const 0) (call $f (v128.load (i32.const 16)))))
(func $f (param v128) (result v128)
(i8x16.shuffle ${perm32x4_pattern.join(' ')} (local.get 0) (local.get 0))))`);
assertEq(wasmSimdAnalysis(), "shuffle -> permute 32x4");
let mem = new Int8Array(ins.exports.mem.buffer);
set(mem, 16, iota(16));
ins.exports.run();
assertSame(get(mem, 0, 16), perm32x4_pattern);
}
// Right operand ignored, dword permutation
{
let ins = wasmCompile(`
(module
(memory (export "mem") 1 1)
(func (export "run")
(v128.store (i32.const 0) (call $f (v128.load (i32.const 16)) (v128.load (i32.const 32)))))
(func $f (param v128) (param v128) (result v128)
(i8x16.shuffle ${perm32x4_pattern.join(' ')} (local.get 0) (local.get 1))))`);
assertEq(wasmSimdAnalysis(), "shuffle -> permute 32x4");
let mem = new Int8Array(ins.exports.mem.buffer);
set(mem, 16, iota(16));
set(mem, 32, iota(16).map(x => x+16));
ins.exports.run();
assertSame(get(mem, 0, 16), perm32x4_pattern);
}
// Left operand ignored, dword permutation
{
let ins = wasmCompile(`
(module
(memory (export "mem") 1 1)
(func (export "run")
(v128.store (i32.const 0) (call $f (v128.load (i32.const 16)) (v128.load (i32.const 32)))))
(func $f (param v128) (param v128) (result v128)
(i8x16.shuffle ${perm32x4_pattern.map(x => x+16).join(' ')} (local.get 0) (local.get 1))))`);
assertEq(wasmSimdAnalysis(), "shuffle -> permute 32x4");
let mem = new Int8Array(ins.exports.mem.buffer);
set(mem, 16, iota(16).map(x => x+16));
set(mem, 32, iota(16));
ins.exports.run();
assertSame(get(mem, 0, 16), perm32x4_pattern);
}
// Operands the same, word permutation on both sides of the qword divide, with a qword swap
{
let perm16x8_pattern = [12, 13, 14, 15, 10, 11, 8, 9,
6, 7, 4, 5, 2, 3, 0, 1];
let ins = wasmCompile(`
(module
(memory (export "mem") 1 1)
(func (export "run")
(v128.store (i32.const 0) (call $f (v128.load (i32.const 16)))))
(func $f (param v128) (result v128)
(i8x16.shuffle ${perm16x8_pattern.join(' ')} (local.get 0) (local.get 0))))`);
assertEq(wasmSimdAnalysis(), "shuffle -> permute 16x8");
let mem = new Int8Array(ins.exports.mem.buffer);
set(mem, 16, iota(16));
ins.exports.run();
assertSame(get(mem, 0, 16), perm16x8_pattern);
}
// Operands the same, word permutation on both sides of the qword divide, no qword swap
{
let perm16x8_pattern = [ 6, 7, 4, 5, 2, 3, 0, 1,
12, 13, 14, 15, 10, 11, 8, 9];
let ins = wasmCompile(`
(module
(memory (export "mem") 1 1)
(func (export "run")
(v128.store (i32.const 0) (call $f (v128.load (i32.const 16)))))
(func $f (param v128) (result v128)
(i8x16.shuffle ${perm16x8_pattern.join(' ')} (local.get 0) (local.get 0))))`);
assertEq(wasmSimdAnalysis(), "shuffle -> permute 16x8");
let mem = new Int8Array(ins.exports.mem.buffer);
set(mem, 16, iota(16));
ins.exports.run();
assertSame(get(mem, 0, 16), perm16x8_pattern);
}
// Operands the same, word permutation on low side of the qword divide, no qword swap
{
let perm16x8_pattern = [ 6, 7, 4, 5, 2, 3, 0, 1,
8, 9, 10, 11, 12, 13, 14, 15];
let ins = wasmCompile(`
(module
(memory (export "mem") 1 1)
(func (export "run")
(v128.store (i32.const 0) (call $f (v128.load (i32.const 16)))))
(func $f (param v128) (result v128)
(i8x16.shuffle ${perm16x8_pattern.join(' ')} (local.get 0) (local.get 0))))`);
assertEq(wasmSimdAnalysis(), "shuffle -> permute 16x8");
let mem = new Int8Array(ins.exports.mem.buffer);
set(mem, 16, iota(16));
ins.exports.run();
assertSame(get(mem, 0, 16), perm16x8_pattern);
}
// Operands the same, word permutation on high side of the qword divide, no qword swap
{
let perm16x8_pattern = [ 0, 1, 2, 3, 4, 5, 6, 7,
12, 13, 14, 15, 10, 11, 8, 9];
let ins = wasmCompile(`
(module
(memory (export "mem") 1 1)
(func (export "run")
(v128.store (i32.const 0) (call $f (v128.load (i32.const 16)))))
(func $f (param v128) (result v128)
(i8x16.shuffle ${perm16x8_pattern.join(' ')} (local.get 0) (local.get 0))))`);
assertEq(wasmSimdAnalysis(), "shuffle -> permute 16x8");
let mem = new Int8Array(ins.exports.mem.buffer);
set(mem, 16, iota(16));
ins.exports.run();
assertSame(get(mem, 0, 16), perm16x8_pattern);
}
// Same operands, byte rotate
{
// 8-bit permutation that is a rotation
let rot8x16_pattern = [5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4];
let ins = wasmCompile(`
(module
(memory (export "mem") 1 1)
(func (export "run")
(v128.store (i32.const 0) (call $f (v128.load (i32.const 16)))))
(func $f (param v128) (result v128)
(i8x16.shuffle ${rot8x16_pattern.join(' ')} (local.get 0) (local.get 0))))`);
assertEq(wasmSimdAnalysis(), "shuffle -> rotate-right 8x16");
let mem = new Int8Array(ins.exports.mem.buffer);
set(mem, 16, iota(16));
ins.exports.run();
assertSame(get(mem, 0, 16), rot8x16_pattern);
}
// Operands the same, random jumble => byte permutation
{
// 8-bit permutation that is not a rotation
let perm8x16_pattern = [5, 7, 6, 8, 9, 10, 11, 4, 13, 14, 15, 0, 1, 2, 3, 12];
let ins = wasmCompile(`
(module
(memory (export "mem") 1 1)
(func (export "run")
(v128.store (i32.const 0) (call $f (v128.load (i32.const 16)))))
(func $f (param v128) (result v128)
(i8x16.shuffle ${perm8x16_pattern.join(' ')} (local.get 0) (local.get 0))))`);
assertEq(wasmSimdAnalysis(), "shuffle -> permute 8x16");
let mem = new Int8Array(ins.exports.mem.buffer);
set(mem, 16, iota(16));
ins.exports.run();
assertSame(get(mem, 0, 16), perm8x16_pattern);
}
// Operands differ, both accessed, rhs is constant zero, left-shift pattern
{
// 8-bit shift with zeroes shifted in at the right end
let shift8x16_pattern = [16, 16, 16, 16, 16, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let ins = wasmCompile(`
(module
(memory (export "mem") 1 1)
(func (export "run")
(v128.store (i32.const 0) (call $f (v128.load (i32.const 16)))))
(func $f (param v128) (result v128)
(i8x16.shuffle ${shift8x16_pattern.join(' ')} (local.get 0) (v128.const i32x4 0 0 0 0))))`);
assertEq(wasmSimdAnalysis(), "shuffle -> shift-left 8x16");
let mem = new Int8Array(ins.exports.mem.buffer);
set(mem, 16, iota(16));
ins.exports.run();
assertSame(get(mem, 0, 16), shift8x16_pattern.map(x => x >= 16 ? 0 : x));
}
// The same as above but the constant is lhs.
{
// 8-bit shift with zeroes shifted in at the right end
let shift8x16_pattern = [16, 16, 16, 16, 16, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10].map(x => x ^ 16);
let ins = wasmCompile(`
(module
(memory (export "mem") 1 1)
(func (export "run")
(v128.store (i32.const 0) (call $f (v128.load (i32.const 16)))))
(func $f (param v128) (result v128)
(i8x16.shuffle ${shift8x16_pattern.join(' ')} (v128.const i32x4 0 0 0 0) (local.get 0))))`);
assertEq(wasmSimdAnalysis(), "shuffle -> shift-left 8x16");
let mem = new Int8Array(ins.exports.mem.buffer);
set(mem, 16, iota(16));
ins.exports.run();
assertSame(get(mem, 0, 16), shift8x16_pattern.map(x => x < 16 ? 0 : x - 16));
}
// Operands differ, both accessed, rhs is constant zero, left-shift pattern that
// does not start properly.
{
// 8-bit shift with zeroes shifted in at the right end
let shift8x16_pattern = [16, 16, 16, 16, 16, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let ins = wasmCompile(`
(module
(memory (export "mem") 1 1)
(func (export "run")
(v128.store (i32.const 0) (call $f (v128.load (i32.const 16)))))
(func $f (param v128) (result v128)
(i8x16.shuffle ${shift8x16_pattern.join(' ')} (local.get 0) (v128.const i32x4 0 0 0 0))))`);
assertEq(wasmSimdAnalysis(), "shuffle -> shuffle+blend 8x16");
let mem = new Int8Array(ins.exports.mem.buffer);
set(mem, 16, iota(16));
ins.exports.run();
assertSame(get(mem, 0, 16), shift8x16_pattern.map(x => x >= 16 ? 0 : x));
}
// Operands differ, both accessed, rhs is constant zero, right-shift pattern
{
// 8-bit shift with zeroes shifted in at the right end
let shift8x16_pattern = [6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 20, 20, 20, 20, 20, 20];
let ins = wasmCompile(`
(module
(memory (export "mem") 1 1)
(func (export "run")
(v128.store (i32.const 0) (call $f (v128.load (i32.const 16)))))
(func $f (param v128) (result v128)
(i8x16.shuffle ${shift8x16_pattern.join(' ')} (local.get 0) (v128.const i32x4 0 0 0 0))))`);
assertEq(wasmSimdAnalysis(), "shuffle -> shift-right 8x16");
let mem = new Int8Array(ins.exports.mem.buffer);
set(mem, 16, iota(16));
ins.exports.run();
assertSame(get(mem, 0, 16), shift8x16_pattern.map(x => x >= 16 ? 0 : x));
}
// Operands differ, both accessed, rhs is constant zero, right-shift pattern
// that does not end properly.
{
// 8-bit shift with zeroes shifted in at the right end
let shift8x16_pattern = [6, 7, 8, 9, 10, 11, 12, 13, 14, 20, 20, 20, 20, 20, 20, 20];
let ins = wasmCompile(`
(module
(memory (export "mem") 1 1)
(func (export "run")
(v128.store (i32.const 0) (call $f (v128.load (i32.const 16)))))
(func $f (param v128) (result v128)
(i8x16.shuffle ${shift8x16_pattern.join(' ')} (local.get 0) (v128.const i32x4 0 0 0 0))))`);
assertEq(wasmSimdAnalysis(), "shuffle -> shuffle+blend 8x16");
let mem = new Int8Array(ins.exports.mem.buffer);
set(mem, 16, iota(16));
ins.exports.run();
assertSame(get(mem, 0, 16), shift8x16_pattern.map(x => x >= 16 ? 0 : x));
}
// Operands differ and are variable, both accessed, (lhs ++ rhs) >> k
{
let concat8x16_pattern = [27, 28, 29, 30, 31, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let ins = wasmCompile(`
(module
(memory (export "mem") 1 1)
(func (export "run")
(v128.store (i32.const 0) (call $f (v128.load (i32.const 16)) (v128.load (i32.const 32)))))
(func $f (param v128) (param v128) (result v128)
(i8x16.shuffle ${concat8x16_pattern.join(' ')} (local.get 0) (local.get 1))))`);
assertEq(wasmSimdAnalysis(), "shuffle -> concat+shift-right 8x16");
let mem = new Int8Array(ins.exports.mem.buffer);
set(mem, 16, iota(16));
set(mem, 32, iota(16).map(k => k+16));
ins.exports.run();
assertSame(get(mem, 0, 16), concat8x16_pattern);
}
// Operands differ and are variable, both accessed, (rhs ++ lhs) >> k
{
let concat8x16_pattern = [11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26];
let ins = wasmCompile(`
(module
(memory (export "mem") 1 1)
(func (export "run")
(v128.store (i32.const 0) (call $f (v128.load (i32.const 16)) (v128.load (i32.const 32)))))
(func $f (param v128) (param v128) (result v128)
(i8x16.shuffle ${concat8x16_pattern.join(' ')} (local.get 0) (local.get 1))))`);
assertEq(wasmSimdAnalysis(), "shuffle -> concat+shift-right 8x16");
let mem = new Int8Array(ins.exports.mem.buffer);
set(mem, 16, iota(16));
set(mem, 32, iota(16).map(k => k+16));
ins.exports.run();
assertSame(get(mem, 0, 16), concat8x16_pattern);
}
// Operands differ, both accessed, but inputs stay in their lanes => byte blend
{
let blend8x16_pattern = iota(16).map(x => (x % 3 == 0) ? x + 16 : x);
let ins = wasmCompile(`
(module
(memory (export "mem") 1 1)
(func (export "run")
(v128.store (i32.const 0) (call $f (v128.load (i32.const 16)) (v128.load (i32.const 32)))))
(func $f (param v128) (param v128) (result v128)
(i8x16.shuffle ${blend8x16_pattern.join(' ')} (local.get 0) (local.get 1))))`);
assertEq(wasmSimdAnalysis(), "shuffle -> blend 8x16");
let mem = new Int8Array(ins.exports.mem.buffer);
let lhs = iota(16);
let rhs = iota(16).map(x => x+16);
set(mem, 16, lhs);
set(mem, 32, rhs);
ins.exports.run();
assertSame(get(mem, 0, 16), blend8x16_pattern);
}
// Operands differ, both accessed, but inputs stay in their lanes => word blend
{
let blend16x8_pattern = iota(16).map(x => (x & 2) ? x + 16 : x);
let ins = wasmCompile(`
(module
(memory (export "mem") 1 1)
(func (export "run")
(v128.store (i32.const 0) (call $f (v128.load (i32.const 16)) (v128.load (i32.const 32)))))
(func $f (param v128) (param v128) (result v128)
(i8x16.shuffle ${blend16x8_pattern.join(' ')} (local.get 0) (local.get 1))))`);
assertEq(wasmSimdAnalysis(), "shuffle -> blend 16x8");
let mem = new Int8Array(ins.exports.mem.buffer);
let lhs = iota(16);
let rhs = iota(16).map(x => x+16);
set(mem, 16, lhs);
set(mem, 32, rhs);
ins.exports.run();
assertSame(get(mem, 0, 16), blend16x8_pattern);
}
// Interleave i32x4s
for ( let [lhs, rhs, expected] of
[[[0, 1], [4, 5], "shuffle -> interleave-low 32x4"],
[[2, 3], [6, 7], "shuffle -> interleave-high 32x4"]] ) {
for (let swap of [false, true]) {
if (swap)
[lhs, rhs] = [rhs, lhs];
let interleave_pattern = i32ToI8(interleave(lhs, rhs));
let ins = wasmCompile(`
(module
(memory (export "mem") 1 1)
(func (export "run")
(v128.store (i32.const 0) (call $f (v128.load (i32.const 16)) (v128.load (i32.const 32)))))
(func $f (param v128) (param v128) (result v128)
(i8x16.shuffle ${interleave_pattern.join(' ')} (local.get 0) (local.get 1))))`);
assertEq(wasmSimdAnalysis(), expected);
let mem = new Int8Array(ins.exports.mem.buffer);
let lhsval = iota(16);
let rhsval = iota(16).map(x => x+16);
set(mem, 16, lhsval);
set(mem, 32, rhsval);
ins.exports.run();
assertSame(get(mem, 0, 16), interleave_pattern);
}
}
// Interleave i64x2s
for ( let [lhs, rhs, expected] of
[[[0], [2], "shuffle -> interleave-low 64x2"],
[[1], [3], "shuffle -> interleave-high 64x2"]] ) {
for (let swap of [false, true]) {
if (swap)
[lhs, rhs] = [rhs, lhs];
let interleave_pattern = i64ToI2(interleave(lhs, rhs));
let ins = wasmCompile(`
(module
(memory (export "mem") 1 1)
(func (export "run")
(v128.store (i32.const 0) (call $f (v128.load (i32.const 16)) (v128.load (i32.const 32)))))
(func $f (param v128) (param v128) (result v128)
(i8x16.shuffle ${interleave_pattern.join(' ')} (local.get 0) (local.get 1))))`);
assertEq(wasmSimdAnalysis(), expected);
let mem = new Int8Array(ins.exports.mem.buffer);
let lhsval = iota(16);
let rhsval = iota(16).map(x => x+16);
set(mem, 16, lhsval);
set(mem, 32, rhsval);
ins.exports.run();
assertSame(get(mem, 0, 16), interleave_pattern);
}
}
// Interleave i16x8s
for ( let [lhs, rhs, expected] of
[[[0, 1, 2, 3], [8, 9, 10, 11], "shuffle -> interleave-low 16x8"],
[[4, 5, 6, 7], [12, 13, 14, 15], "shuffle -> interleave-high 16x8"]] ) {
for (let swap of [false, true]) {
if (swap)
[lhs, rhs] = [rhs, lhs];
let interleave_pattern = i16ToI8(interleave(lhs, rhs));
let ins = wasmCompile(`
(module
(memory (export "mem") 1 1)
(func (export "run")
(v128.store (i32.const 0) (call $f (v128.load (i32.const 16)) (v128.load (i32.const 32)))))
(func $f (param v128) (param v128) (result v128)
(i8x16.shuffle ${interleave_pattern.join(' ')} (local.get 0) (local.get 1))))`);
assertEq(wasmSimdAnalysis(), expected);
let mem = new Int8Array(ins.exports.mem.buffer);
let lhsval = iota(16);
let rhsval = iota(16).map(x => x+16);
set(mem, 16, lhsval);
set(mem, 32, rhsval);
ins.exports.run();
assertSame(get(mem, 0, 16), interleave_pattern);
}
}
// Interleave i8x16s
for ( let [lhs, rhs, expected] of
[[[0, 1, 2, 3, 4, 5, 6, 7], [16, 17, 18, 19, 20, 21, 22, 23], "shuffle -> interleave-low 8x16"],
[[8, 9, 10, 11, 12, 13, 14, 15],[24, 25, 26, 27, 28, 29, 30, 31], "shuffle -> interleave-high 8x16"]] ) {
for (let swap of [false, true]) {
if (swap)
[lhs, rhs] = [rhs, lhs];
let interleave_pattern = interleave(lhs, rhs);
let ins = wasmCompile(`
(module
(memory (export "mem") 1 1)
(func (export "run")
(v128.store (i32.const 0) (call $f (v128.load (i32.const 16)) (v128.load (i32.const 32)))))
(func $f (param v128) (param v128) (result v128)
(i8x16.shuffle ${interleave_pattern.join(' ')} (local.get 0) (local.get 1))))`);
assertEq(wasmSimdAnalysis(), expected);
let mem = new Int8Array(ins.exports.mem.buffer);
let lhsval = iota(16);
let rhsval = iota(16).map(x => x+16);
set(mem, 16, lhsval);
set(mem, 32, rhsval);
ins.exports.run();
assertSame(get(mem, 0, 16), interleave_pattern);
}
}
// Operands differ, both accessed, random jumble => byte shuffle+blend
{
let blend_perm8x16_pattern = [5, 23, 6, 24, 9, 10, 11, 7, 7, 14, 15, 19, 1, 2, 3, 12];
let ins = wasmCompile(`
(module
(memory (export "mem") 1 1)
(func (export "run")
(v128.store (i32.const 0) (call $f (v128.load (i32.const 16)) (v128.load (i32.const 32)))))
(func $f (param v128) (param v128) (result v128)
(i8x16.shuffle ${blend_perm8x16_pattern.join(' ')} (local.get 0) (local.get 1))))`);
assertEq(wasmSimdAnalysis(), "shuffle -> shuffle+blend 8x16");
let mem = new Int8Array(ins.exports.mem.buffer);
let lhs = iota(16).map(x => x+16);
let rhs = iota(16);
set(mem, 16, lhs);
set(mem, 32, rhs);
ins.exports.run();
assertSame(get(mem, 0, 16),
blend_perm8x16_pattern.map(x => x < 16 ? lhs[x] : rhs[x-16]));
}
// No-op, ignoring right operand, should turn into a move.
{
let nop8x16_pattern = iota(16);
let ins = wasmCompile(`
(module
(memory (export "mem") 1 1)
(func (export "run")
(v128.store (i32.const 0) (call $f (v128.load (i32.const 16)) (v128.load (i32.const 32)))))
(func $f (param v128) (param v128) (result v128)
(i8x16.shuffle ${nop8x16_pattern.join(' ')} (local.get 0) (local.get 1))))`);
assertEq(wasmSimdAnalysis(), "shuffle -> move");
let mem = new Int8Array(ins.exports.mem.buffer);
set(mem, 16, iota(16));
set(mem, 32, iota(16).map(x => x+16));
ins.exports.run();
assertSame(get(mem, 0, 16), nop8x16_pattern);
}
// No-op, ignoring left operand, should turn into a move.
{
let nop8x16_pattern = iota(16).map(x => x+16);
let ins = wasmCompile(`
(module
(memory (export "mem") 1 1)
(func (export "run")
(v128.store (i32.const 0) (call $f (v128.load (i32.const 16)) (v128.load (i32.const 32)))))
(func $f (param v128) (param v128) (result v128)
(i8x16.shuffle ${nop8x16_pattern.join(' ')} (local.get 0) (local.get 1))))`);
assertEq(wasmSimdAnalysis(), "shuffle -> move");
let mem = new Int8Array(ins.exports.mem.buffer);
set(mem, 16, iota(16));
set(mem, 32, iota(16).map(x => x+16));
ins.exports.run();
assertSame(get(mem, 0, 16), nop8x16_pattern);
}
// Broadcast byte
for ( let byte of [3, 11, 8, 2] ) {
let broadcast8x16_pattern = iota(16).map(_ => byte);
let ins = wasmCompile(`
(module
(memory (export "mem") 1 1)
(func (export "run")
(v128.store (i32.const 0) (call $f (v128.load (i32.const 16)))))
(func $f (param v128) (result v128)
(i8x16.shuffle ${broadcast8x16_pattern.join(' ')} (local.get 0) (local.get 0))))`);
assertEq(wasmSimdAnalysis(), "shuffle -> broadcast 8x16");
let mem = new Int8Array(ins.exports.mem.buffer);
set(mem, 16, iota(16));
ins.exports.run();
assertSame(get(mem, 0, 16), broadcast8x16_pattern);
}
// Broadcast word from high quadword
{
let broadcast16x8_pattern = [10, 11, 10, 11, 10, 11, 10, 11, 10, 11, 10, 11, 10, 11, 10, 11];
let ins = wasmCompile(`
(module
(memory (export "mem") 1 1)
(func (export "run")
(v128.store (i32.const 0) (call $f (v128.load (i32.const 16)))))
(func $f (param v128) (result v128)
(i8x16.shuffle ${broadcast16x8_pattern.join(' ')} (local.get 0) (local.get 0))))`);
assertEq(wasmSimdAnalysis(), "shuffle -> broadcast 16x8");
let mem = new Int8Array(ins.exports.mem.buffer);
set(mem, 16, iota(16));
ins.exports.run();
assertSame(get(mem, 0, 16), broadcast16x8_pattern);
}
// Broadcast word from low quadword
{
let broadcast16x8_pattern = [4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5];
let ins = wasmCompile(`
(module
(memory (export "mem") 1 1)
(func (export "run")
(v128.store (i32.const 0) (call $f (v128.load (i32.const 16)))))
(func $f (param v128) (result v128)
(i8x16.shuffle ${broadcast16x8_pattern.join(' ')} (local.get 0) (local.get 0))))`);
assertEq(wasmSimdAnalysis(), "shuffle -> broadcast 16x8");
let mem = new Int8Array(ins.exports.mem.buffer);
set(mem, 16, iota(16));
ins.exports.run();
assertSame(get(mem, 0, 16), broadcast16x8_pattern);
}
// Broadcast dword from low quadword should turn into a dword permute
{
let broadcast32x4_pattern = [4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7];
let ins = wasmCompile(`
(module
(memory (export "mem") 1 1)
(func (export "run")
(v128.store (i32.const 0) (call $f (v128.load (i32.const 16)))))
(func $f (param v128) (result v128)
(i8x16.shuffle ${broadcast32x4_pattern.join(' ')} (local.get 0) (local.get 0))))`);
assertEq(wasmSimdAnalysis(), "shuffle -> permute 32x4");
let mem = new Int8Array(ins.exports.mem.buffer);
set(mem, 16, iota(16));
ins.exports.run();
assertSame(get(mem, 0, 16), broadcast32x4_pattern);
}
// Broadcast high qword should turn into a dword permute
{
let broadcast64x2_pattern = [8, 9, 10, 11, 12, 13, 14, 15, 8, 9, 10, 11, 12, 13, 14, 15]
let ins = wasmCompile(`
(module
(memory (export "mem") 1 1)
(func (export "run")
(v128.store (i32.const 0) (call $f (v128.load (i32.const 16)))))
(func $f (param v128) (result v128)
(i8x16.shuffle ${broadcast64x2_pattern.join(' ')} (local.get 0) (local.get 0))))`);
assertEq(wasmSimdAnalysis(), "shuffle -> permute 32x4");
let mem = new Int8Array(ins.exports.mem.buffer);
set(mem, 16, iota(16));
ins.exports.run();
assertSame(get(mem, 0, 16), broadcast64x2_pattern);
}
// Byte reversal should be a byte permute
{
let rev8x16_pattern = iota(16).reverse();
let ins = wasmCompile(`
(module
(memory (export "mem") 1 1)
(func (export "run")
(v128.store (i32.const 0) (call $f (v128.load (i32.const 16)))))
(func $f (param v128) (result v128)
(i8x16.shuffle ${rev8x16_pattern.join(' ')} (local.get 0) (local.get 0))))`);
assertEq(wasmSimdAnalysis(), "shuffle -> permute 8x16");
let mem = new Int8Array(ins.exports.mem.buffer);
set(mem, 16, iota(16));
ins.exports.run();
assertSame(get(mem, 0, 16), rev8x16_pattern);
}
// Byteswap of half-word, word and quad-word groups should be
// reverse bytes analysis
for (let k of [2, 4, 8]) {
let rev8_pattern = iota(16).map(i => i ^ (k - 1));
let ins = wasmCompile(`
(module
(memory (export "mem") 1 1)
(func (export "run")
(v128.store (i32.const 0) (call $f (v128.load (i32.const 16)))))
(func $f (param v128) (result v128)
(i8x16.shuffle ${rev8_pattern.join(' ')} (local.get 0) (local.get 0))))`);
assertEq(wasmSimdAnalysis(), `shuffle -> reverse bytes in ${8 * k}-bit lanes`);
let mem = new Int8Array(ins.exports.mem.buffer);
set(mem, 16, iota(16));
ins.exports.run();
assertSame(get(mem, 0, 16), rev8_pattern);
}
// Word reversal should be a word permute
{
let rev16x8_pattern = i16ToI8(iota(8).reverse());
let ins = wasmCompile(`
(module
(memory (export "mem") 1 1)
(func (export "run")
(v128.store (i32.const 0) (call $f (v128.load (i32.const 16)))))
(func $f (param v128) (result v128)
(i8x16.shuffle ${rev16x8_pattern.join(' ')} (local.get 0) (local.get 0))))`);
assertEq(wasmSimdAnalysis(), "shuffle -> permute 16x8");
let mem = new Int8Array(ins.exports.mem.buffer);
set(mem, 16, iota(16));
ins.exports.run();
assertSame(get(mem, 0, 16), rev16x8_pattern);
}
// Dword reversal should be a dword permute
{
let rev32x4_pattern = i32ToI8([3, 2, 1, 0]);
let ins = wasmCompile(`
(module
(memory (export "mem") 1 1)
(func (export "run")
(v128.store (i32.const 0) (call $f (v128.load (i32.const 16)))))
(func $f (param v128) (result v128)
(i8x16.shuffle ${rev32x4_pattern.join(' ')} (local.get 0) (local.get 0))))`);
assertEq(wasmSimdAnalysis(), "shuffle -> permute 32x4");
let mem = new Int8Array(ins.exports.mem.buffer);
set(mem, 16, iota(16));
ins.exports.run();
assertSame(get(mem, 0, 16), rev32x4_pattern);
}
// Qword reversal should be a dword permute
{
let rev64x2_pattern = i32ToI8([2, 3, 0, 1]);
let ins = wasmCompile(`
(module
(memory (export "mem") 1 1)
(func (export "run")
(v128.store (i32.const 0) (call $f (v128.load (i32.const 16)))))
(func $f (param v128) (result v128)
(i8x16.shuffle ${rev64x2_pattern.join(' ')} (local.get 0) (local.get 0))))`);
assertEq(wasmSimdAnalysis(), "shuffle -> permute 32x4");
let mem = new Int8Array(ins.exports.mem.buffer);
set(mem, 16, iota(16));
ins.exports.run();
assertSame(get(mem, 0, 16), rev64x2_pattern);
}
// In the case of shifts, we have separate tests that constant shifts work
// correctly, so no such testing is done here.
for ( let lanes of ['i8x16', 'i16x8', 'i32x4', 'i64x2'] ) {
for ( let shift of ['shl', 'shr_s', 'shr_u'] ) {
for ( let [count, result] of [['(i32.const 5)', /shift -> constant shift/],
['(local.get 1)', /shift -> variable(?: scalarized)? shift/]] ) {
wasmCompile(`(module (func (param v128) (param i32) (result v128) (${lanes}.${shift} (local.get 0) ${count})))`);
assertEq(wasmSimdAnalysis().match(result).length, 1);
}
}
}
// Zero extending int values.
{
const zeroExtTypes = [
{ty: '8x16', size: 1, ch: 'b'},
{ty: '16x8', size: 2, ch: 'w'},
{ty: '32x4', size: 4, ch: 'd'},
{ty: '64x2', size: 8, ch: 'q'}];
function generateZeroExtend (src, dest, inv) {
const ar = new Array(16);
for (let i = 0, j = 0; i < ar.length; i++) {
if ((i % dest) >= src) {
ar[i] = (inv ? 0 : 16) + (i % 16);
continue;
}
ar[i] = j++ + (inv ? 16 : 0);
}
return ar.join(' ');
}
for (let i = 0; i < 3; i++) {
for (let j = i + 1; j < 4; j++) {
const result = `shuffle -> zero-extend ${zeroExtTypes[i].ty} to ${zeroExtTypes[j].ty}`;
const pat = generateZeroExtend(zeroExtTypes[i].size, zeroExtTypes[j].size, false);
wasmCompile(`(module (func (param v128) (result v128) (i8x16.shuffle ${pat} (local.get 0) (v128.const i32x4 0 0 0 0))))`);
assertEq(wasmSimdAnalysis(), result);
const patInv = generateZeroExtend(zeroExtTypes[i].size, zeroExtTypes[j].size, true);
wasmCompile(`(module (func (param v128) (result v128) (i8x16.shuffle ${patInv} (v128.const i32x4 0 0 0 0) (local.get 0))))`);
assertEq(wasmSimdAnalysis(), result);
// Test in wasm by "hidding" zero constant as an argument.
const ins = wasmEvalText(`(module
(func $t (param v128) (result v128) (i8x16.shuffle ${pat} (local.get 0) (v128.const i32x4 0 0 0 0)))
(func $check (param v128) (param v128) (result v128) (i8x16.shuffle ${pat} (local.get 0) (local.get 1)))
(func (export "test") (result i32)
v128.const i32x4 0xff01ee02 0xdd03cc04 0xaa059906 0x88776655
call $t
v128.const i32x4 0xff01ee02 0xdd03cc04 0xaa059906 0x88776655
v128.const i32x4 0 0 0 0
call $check
i8x16.eq
i8x16.bitmask
))`);
assertEq(ins.exports.test(), 0xffff);
}
}
// Some patterns that look like zero extend.
for (let pat of ["0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30"]) {
wasmCompile(`(module (func (param v128) (result v128) (i8x16.shuffle ${pat} (local.get 0) (v128.const i32x4 0 0 0 0))))`);
const res = wasmSimdAnalysis();
assertEq(!res.includes("shuffle -> zero-extend"), true);
}
}
// Constant folding scalar->simd. There are functional tests for all these in
// ad-hack.js so here we only check that the transformation is triggered.
for ( let [ty128, ty] of [['i8x16', 'i32'], ['i16x8', 'i32'], ['i32x4', 'i32'],
['i64x2', 'i64'], ['f32x4', 'f32'], ['f64x2', 'f64']] )
{
wasmCompile(`(module (func (result v128) (${ty128}.splat (${ty}.const 37))))`);
assertEq(wasmSimdAnalysis(), "scalar-to-simd128 -> constant folded");
}
// Ditto simd->scalar.
for ( let [ty128, suffix] of [['i8x16', '_s'], ['i8x16', '_u'], ['i16x8','_s'], ['i16x8','_u'], ['i32x4', '']] ) {
for ( let op of ['any_true', 'all_true', 'bitmask', `extract_lane${suffix} 0`] ) {
let operation = op == 'any_true' ? 'v128.any_true' : `${ty128}.${op}`;
wasmCompile(`(module (func (result i32) (${operation} (v128.const i64x2 0 0))))`);
assertEq(wasmSimdAnalysis(), "simd128-to-scalar -> constant folded");
}
}
for ( let ty128 of ['f32x4','f64x2','i64x2'] ) {
wasmCompile(`(module (func (result ${ty128.match(/(...)x.*/)[1]}) (${ty128}.extract_lane 0 (v128.const i64x2 0 0))))`);
assertEq(wasmSimdAnalysis(), "simd128-to-scalar -> constant folded");
}
// Optimizing all_true, any_true, and bitmask that are used for control flow, also when negated.
for ( let [ty128,size] of [['i8x16',1], ['i16x8',2], ['i32x4',4]] ) {
let all = iota(16/size).map(n => n*n);
let some = iota(16/size).map(n => n*(n % 3));
let none = iota(16/size).map(n => 0);
let inputs = [all, some, none];
let ops = { all_true: allTrue, any_true: anyTrue, bitmask };
for ( let op of ['any_true', 'all_true', 'bitmask'] ) {
let folded = op != 'bitmask' || (size == 2 && !isArm64);
let operation = op == 'any_true' ? 'v128.any_true' : `${ty128}.${op}`;
let positive =
wasmCompile(
`(module
(memory (export "mem") 1 1)
(func $f (param v128) (result i32)
(if (result i32) (${operation} (local.get 0))
(then (i32.const 42))
(else (i32.const 37))))
(func (export "run") (result i32)
(call $f (v128.load (i32.const 16)))))`);
assertEq(wasmSimdAnalysis(), folded ? "simd128-to-scalar-and-branch -> folded" : "none");
let negative =
wasmCompile(
`(module
(memory (export "mem") 1 1)
(func $f (param v128) (result i32)
(if (result i32) (i32.eqz (${operation} (local.get 0)))
(then (i32.const 42))
(else (i32.const 37))))
(func (export "run") (result i32)
(call $f (v128.load (i32.const 16)))))`);
assertEq(wasmSimdAnalysis(), folded ? "simd128-to-scalar-and-branch -> folded" : "none");
for ( let inp of inputs ) {
let mem = new this[`Int${8*size}Array`](positive.exports.mem.buffer);
set(mem, 16/size, inp);
assertEq(positive.exports.run(), ops[op](inp) ? 42 : 37);
mem = new this[`Int${8*size}Array`](negative.exports.mem.buffer);
set(mem, 16/size, inp);
assertEq(negative.exports.run(), ops[op](inp) ? 37 : 42);
}
}
}
// Constant folding
{
// Swizzle-with-constant rewritten as shuffle, and then further optimized
// into a dword permute. Correctness is tested in ad-hack.js.
wasmCompile(`
(module (func (param v128) (result v128)
(i8x16.swizzle (local.get 0) (v128.const i8x16 4 5 6 7 0 1 2 3 12 13 14 15 8 9 10 11))))
`);
assertEq(wasmSimdAnalysis(), "shuffle -> permute 32x4");
}
// Bitselect with constant mask folded into shuffle operation
if (!isArm64) {
wasmCompile(`
(module (func (param v128) (param v128) (result v128)
(v128.bitselect (local.get 0) (local.get 1) (v128.const i8x16 0 -1 -1 0 0 0 0 0 -1 -1 -1 -1 -1 -1 0 0))))
`);
assertEq(wasmSimdAnalysis(), "shuffle -> blend 8x16");
}
// Library
function wasmCompile(text) {
return new WebAssembly.Instance(new WebAssembly.Module(wasmTextToBinary(text)))
}
function get(arr, loc, len) {
let res = [];
for ( let i=0; i < len; i++ ) {
res.push(arr[loc+i]);
}
return res;
}
function set(arr, loc, vals) {
for ( let i=0; i < vals.length; i++ ) {
arr[loc+i] = vals[i];
}
}
function i32ToI8(xs) {
return xs.map(x => [x*4, x*4+1, x*4+2, x*4+3]).flat();
}
function i64ToI2(xs) {
return xs.map(x => [x*8, x*8+1, x*8+2, x*8+3,
x*8+4, x*8+5, x*8+6, x*8+7]).flat();
}
function i16ToI8(xs) {
return xs.map(x => [x*2, x*2+1]).flat();
}
function allTrue(xs) {
return xs.every(v => v != 0);
}
function anyTrue(xs) {
return xs.some(v => v != 0);
}
function bitmask(xs) {
let shift = 128/xs.length - 1;
let res = 0;
let k = 0;
xs.forEach(v => { res |= ((v >>> shift) & 1) << k; k++; });
return res;
}
|