Skip to content

Commit 893b1d4

Browse files
authored
[WebAssembly] [Codegen] Add patterns for relaxed dot (#163266)
The pattern I added for `relaxed dot` similar to normal dot @ #151775. For `relaxed dot add`, i noticed that in the proposal the portion of dot implementation is similar to `relaxed dot`, so I think we can add a pattern where after we do relaxed dot and do extadd pairwise, we can do `relaxed dot add`. One current obstacles is I don't think there is any pattern to singly create a extadd pairwise from other instructions so the `relaxed dot add` pattern would not cover a wide range of instructions. related to #55932
1 parent 05cbcf1 commit 893b1d4

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed

llvm/lib/Target/WebAssembly/WebAssemblyInstrSIMD.td

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1763,6 +1763,26 @@ defm RELAXED_DOT :
17631763
"i16x8.relaxed_dot_i8x16_i7x16_s\t$dst, $lhs, $rhs",
17641764
"i16x8.relaxed_dot_i8x16_i7x16_s", 0x112>;
17651765

1766+
def : Pat<
1767+
(v8i16 (add
1768+
(wasm_shuffle
1769+
(v8i16 (extmul_low_s v16i8:$lhs, v16i8:$rhs)),
1770+
(v8i16 (extmul_high_s v16i8:$lhs, v16i8:$rhs)),
1771+
(i32 0), (i32 1), (i32 4), (i32 5),
1772+
(i32 8), (i32 9), (i32 12), (i32 13),
1773+
(i32 16), (i32 17), (i32 20), (i32 21),
1774+
(i32 24), (i32 25), (i32 28), (i32 29)),
1775+
(wasm_shuffle
1776+
(v8i16 (extmul_low_s v16i8:$lhs, v16i8:$rhs)),
1777+
(v8i16 (extmul_high_s v16i8:$lhs, v16i8:$rhs)),
1778+
(i32 2), (i32 3), (i32 6), (i32 7),
1779+
(i32 10), (i32 11), (i32 14), (i32 15),
1780+
(i32 18), (i32 19), (i32 22), (i32 23),
1781+
(i32 26), (i32 27), (i32 30), (i32 31)))
1782+
),
1783+
(v8i16 (RELAXED_DOT v16i8:$lhs, v16i8:$rhs))
1784+
>;
1785+
17661786
defm RELAXED_DOT_ADD :
17671787
RELAXED_I<(outs V128:$dst), (ins V128:$lhs, V128:$rhs, V128:$acc),
17681788
(outs), (ins),
@@ -1771,6 +1791,14 @@ defm RELAXED_DOT_ADD :
17711791
"i32x4.relaxed_dot_i8x16_i7x16_add_s\t$dst, $lhs, $rhs, $acc",
17721792
"i32x4.relaxed_dot_i8x16_i7x16_add_s", 0x113>;
17731793

1794+
def : Pat<
1795+
(v4i32 (add
1796+
(v4i32 (int_wasm_extadd_pairwise_signed
1797+
(v8i16 (int_wasm_relaxed_dot_i8x16_i7x16_signed v16i8:$lhs, v16i8:$rhs)))),
1798+
(v4i32 V128:$acc))),
1799+
(v4i32 (RELAXED_DOT_ADD v16i8:$lhs, v16i8:$rhs, (v4i32 V128:$acc)))
1800+
>;
1801+
17741802
def : Pat<(v4i32 (partial_reduce_smla (v4i32 V128:$acc), (v16i8 V128:$lhs),
17751803
(v16i8 V128:$rhs))),
17761804
(RELAXED_DOT_ADD $lhs, $rhs, $acc)>, Requires<[HasRelaxedSIMD]>;
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
2+
; RUN: llc < %s -disable-wasm-fallthrough-return-opt -wasm-disable-explicit-locals -wasm-keep-registers -mattr=+fp16,+simd128,+relaxed-simd | FileCheck %s
3+
4+
target triple = "wasm32"
5+
; relaxed_dot stands for relaxed_dot_i8x16_i7x16_s, as in td
6+
; relaxed_dot_add stands for i32x4.relaxed_dot_i8x16_i7x16_add_s, as in td
7+
8+
define <8 x i16> @relaxed_dot_sext_1(<16 x i8> %a, <16 x i8> %b) {
9+
; CHECK-LABEL: relaxed_dot_sext_1:
10+
; CHECK: .functype relaxed_dot_sext_1 (v128, v128) -> (v128)
11+
; CHECK-NEXT: # %bb.0:
12+
; CHECK-NEXT: i16x8.relaxed_dot_i8x16_i7x16_s $push0=, $0, $1
13+
; CHECK-NEXT: return $pop0
14+
%sext1 = sext <16 x i8> %a to <16 x i16>
15+
%sext2 = sext <16 x i8> %b to <16 x i16>
16+
%mul = mul <16 x i16> %sext1, %sext2
17+
%shuffle1 = shufflevector <16 x i16> %mul, <16 x i16> poison, <8 x i32> <i32 0, i32 2, i32 4, i32 6, i32 8, i32 10, i32 12, i32 14>
18+
%shuffle2 = shufflevector <16 x i16> %mul, <16 x i16> poison, <8 x i32> <i32 1, i32 3, i32 5, i32 7, i32 9, i32 11, i32 13, i32 15>
19+
%res = add <8 x i16> %shuffle1, %shuffle2
20+
ret <8 x i16> %res
21+
}
22+
23+
24+
define <8 x i16> @relaxed_dot_sext_2(<16 x i8> %a, <16 x i8> %b) {
25+
; CHECK-LABEL: relaxed_dot_sext_2:
26+
; CHECK: .functype relaxed_dot_sext_2 (v128, v128) -> (v128)
27+
; CHECK-NEXT: # %bb.0:
28+
; CHECK-NEXT: i16x8.relaxed_dot_i8x16_i7x16_s $push0=, $0, $1
29+
; CHECK-NEXT: return $pop0
30+
%sext1 = sext <16 x i8> %a to <16 x i16>
31+
%sext2 = sext <16 x i8> %b to <16 x i16>
32+
%mul = mul <16 x i16> %sext1, %sext2
33+
%shuffle1 = shufflevector <16 x i16> %mul, <16 x i16> poison, <8 x i32> <i32 0, i32 2, i32 4, i32 6, i32 8, i32 10, i32 12, i32 14>
34+
%shuffle2 = shufflevector <16 x i16> %mul, <16 x i16> poison, <8 x i32> <i32 1, i32 3, i32 5, i32 7, i32 9, i32 11, i32 13, i32 15>
35+
%res = add <8 x i16> %shuffle2, %shuffle1
36+
ret <8 x i16> %res
37+
}
38+
39+
define <8 x i16> @relaxed_dot_sext_self(<16 x i8> %v) {
40+
; CHECK-LABEL: relaxed_dot_sext_self:
41+
; CHECK: .functype relaxed_dot_sext_self (v128) -> (v128)
42+
; CHECK-NEXT: # %bb.0:
43+
; CHECK-NEXT: i16x8.relaxed_dot_i8x16_i7x16_s $push0=, $0, $0
44+
; CHECK-NEXT: return $pop0
45+
%sext = sext <16 x i8> %v to <16 x i16>
46+
%mul = mul <16 x i16> %sext, %sext
47+
%shuffle1 = shufflevector <16 x i16> %mul, <16 x i16> poison, <8 x i32> <i32 0, i32 2, i32 4, i32 6, i32 8, i32 10, i32 12, i32 14>
48+
%shuffle2 = shufflevector <16 x i16> %mul, <16 x i16> poison, <8 x i32> <i32 1, i32 3, i32 5, i32 7, i32 9, i32 11, i32 13, i32 15>
49+
%res = add <8 x i16> %shuffle1, %shuffle2
50+
ret <8 x i16> %res
51+
}
52+
53+
define <4 x i32> @relaxed_dot_add_from_relaxed_dot(<16 x i8> %a, <16 x i8> %b, <4 x i32> %c) {
54+
; CHECK-LABEL: relaxed_dot_add_from_relaxed_dot:
55+
; CHECK: .functype relaxed_dot_add_from_relaxed_dot (v128, v128, v128) -> (v128)
56+
; CHECK-NEXT: # %bb.0:
57+
; CHECK-NEXT: i32x4.relaxed_dot_i8x16_i7x16_add_s $push0=, $0, $1, $2
58+
; CHECK-NEXT: return $pop0
59+
%relaxed_dot_call = call <8 x i16> @llvm.wasm.relaxed.dot.i8x16.i7x16.signed(<16 x i8> %a, <16 x i8> %b)
60+
%sext = call <4 x i32> @llvm.wasm.extadd.pairwise.signed.v4i32(<8 x i16> %relaxed_dot_call)
61+
%res = add <4 x i32> %sext, %c
62+
ret <4 x i32> %res
63+
}
64+
65+
; INFO: Negative test
66+
define <8 x i16> @relaxed_dot_zext(<16 x i8> %a, <16 x i8> %b) {
67+
; CHECK-LABEL: relaxed_dot_zext:
68+
; CHECK: .functype relaxed_dot_zext (v128, v128) -> (v128)
69+
; CHECK-NEXT: # %bb.0:
70+
; CHECK-NEXT: i16x8.extmul_low_i8x16_u $push6=, $0, $1
71+
; CHECK-NEXT: local.tee $push5=, $2=, $pop6
72+
; CHECK-NEXT: i16x8.extmul_high_i8x16_u $push4=, $0, $1
73+
; CHECK-NEXT: local.tee $push3=, $1=, $pop4
74+
; CHECK-NEXT: i8x16.shuffle $push1=, $pop5, $pop3, 0, 1, 4, 5, 8, 9, 12, 13, 16, 17, 20, 21, 24, 25, 28, 29
75+
; CHECK-NEXT: i8x16.shuffle $push0=, $2, $1, 2, 3, 6, 7, 10, 11, 14, 15, 18, 19, 22, 23, 26, 27, 30, 31
76+
; CHECK-NEXT: i16x8.add $push2=, $pop1, $pop0
77+
; CHECK-NEXT: return $pop2
78+
%zext1 = zext <16 x i8> %a to <16 x i16>
79+
%zext2 = zext <16 x i8> %b to <16 x i16>
80+
%mul = mul <16 x i16> %zext1, %zext2
81+
%shuffle1 = shufflevector <16 x i16> %mul, <16 x i16> poison, <8 x i32> <i32 0, i32 2, i32 4, i32 6, i32 8, i32 10, i32 12, i32 14>
82+
%shuffle2 = shufflevector <16 x i16> %mul, <16 x i16> poison, <8 x i32> <i32 1, i32 3, i32 5, i32 7, i32 9, i32 11, i32 13, i32 15>
83+
%res = add <8 x i16> %shuffle1, %shuffle2
84+
ret <8 x i16> %res
85+
86+
}
87+
88+
; INFO: Negative test
89+
define <8 x i16> @relaxed_dot_wrong_shuffle(<16 x i8> %a, <16 x i8> %b) {
90+
; CHECK-LABEL: relaxed_dot_wrong_shuffle:
91+
; CHECK: .functype relaxed_dot_wrong_shuffle (v128, v128) -> (v128)
92+
; CHECK-NEXT: # %bb.0:
93+
; CHECK-NEXT: i16x8.extmul_low_i8x16_s $push1=, $0, $1
94+
; CHECK-NEXT: i16x8.extmul_high_i8x16_s $push0=, $0, $1
95+
; CHECK-NEXT: i16x8.add $push2=, $pop1, $pop0
96+
; CHECK-NEXT: return $pop2
97+
%sext1 = sext <16 x i8> %a to <16 x i16>
98+
%sext2 = sext <16 x i8> %b to <16 x i16>
99+
%mul = mul <16 x i16> %sext1, %sext2
100+
%shuffle1 = shufflevector <16 x i16> %mul, <16 x i16> poison, <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7>
101+
%shuffle2 = shufflevector <16 x i16> %mul, <16 x i16> poison, <8 x i32> <i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15>
102+
%res = add <8 x i16> %shuffle1, %shuffle2
103+
ret <8 x i16> %res
104+
}

0 commit comments

Comments
 (0)