Skip to content

Commit 522ac23

Browse files
authored
[WebAssembly] Add pattern for relaxed nmadd (#150684)
Following footstep of #147487 (support for madd), this PR adds support for nmadd. #55932 tracks this
1 parent 2368be3 commit 522ac23

File tree

3 files changed

+150
-0
lines changed

3 files changed

+150
-0
lines changed

llvm/include/llvm/Target/TargetSelectionDAG.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,6 +1147,9 @@ def fadd_contract : PatFrag<(ops node:$a, node:$b), (fadd node:$a, node:$b),[{
11471147
return N->getFlags().hasAllowContract();
11481148
}]>;
11491149

1150+
def fsub_contract : PatFrag<(ops node:$a, node:$b), (fsub node:$a, node:$b),[{
1151+
return N->getFlags().hasAllowContract();
1152+
}]>;
11501153

11511154
def not : PatFrag<(ops node:$in), (xor node:$in, -1)>;
11521155
def vnot : PatFrag<(ops node:$in), (xor node:$in, immAllOnesV)>;

llvm/lib/Target/WebAssembly/WebAssemblyInstrSIMD.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1540,6 +1540,8 @@ multiclass SIMDMADD<Vec vec, bits<32> simdopA, bits<32> simdopS, list<Predicate>
15401540
def : Pat<(fadd_contract (vec.vt V128:$a), (fmul_contract (vec.vt V128:$b), (vec.vt V128:$c))),
15411541
(!cast<Instruction>("MADD_"#vec) V128:$a, V128:$b, V128:$c)>, Requires<[HasRelaxedSIMD]>;
15421542

1543+
def : Pat<(fsub_contract (vec.vt V128:$a), (fmul_contract (vec.vt V128:$b), (vec.vt V128:$c))),
1544+
(!cast<Instruction>("NMADD_"#vec) V128:$a, V128:$b, V128:$c)>, Requires<[HasRelaxedSIMD]>;
15431545
}
15441546

15451547
defm "" : SIMDMADD<F32x4, 0x105, 0x106, [HasRelaxedSIMD]>;
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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 --check-prefix=RELAXED
3+
; RUN: llc < %s -disable-wasm-fallthrough-return-opt -wasm-disable-explicit-locals -wasm-keep-registers -mattr=+fp16,+simd128, | FileCheck %s --check-prefix=STRICT
4+
5+
target triple = "wasm32"
6+
7+
define double @fsub_fmul_contract_f64(double %a, double %b, double %c) {
8+
; RELAXED-LABEL: fsub_fmul_contract_f64:
9+
; RELAXED: .functype fsub_fmul_contract_f64 (f64, f64, f64) -> (f64)
10+
; RELAXED-NEXT: # %bb.0:
11+
; RELAXED-NEXT: f64.mul $push0=, $1, $0
12+
; RELAXED-NEXT: f64.sub $push1=, $2, $pop0
13+
; RELAXED-NEXT: return $pop1
14+
;
15+
; STRICT-LABEL: fsub_fmul_contract_f64:
16+
; STRICT: .functype fsub_fmul_contract_f64 (f64, f64, f64) -> (f64)
17+
; STRICT-NEXT: # %bb.0:
18+
; STRICT-NEXT: f64.mul $push0=, $1, $0
19+
; STRICT-NEXT: f64.sub $push1=, $2, $pop0
20+
; STRICT-NEXT: return $pop1
21+
%mul = fmul contract double %b, %a
22+
%sub = fsub contract double %c, %mul
23+
ret double %sub
24+
}
25+
26+
define <4 x float> @fsub_fmul_contract_4xf32(<4 x float> %a, <4 x float> %b, <4 x float> %c) {
27+
; RELAXED-LABEL: fsub_fmul_contract_4xf32:
28+
; RELAXED: .functype fsub_fmul_contract_4xf32 (v128, v128, v128) -> (v128)
29+
; RELAXED-NEXT: # %bb.0:
30+
; RELAXED-NEXT: f32x4.relaxed_nmadd $push0=, $2, $1, $0
31+
; RELAXED-NEXT: return $pop0
32+
;
33+
; STRICT-LABEL: fsub_fmul_contract_4xf32:
34+
; STRICT: .functype fsub_fmul_contract_4xf32 (v128, v128, v128) -> (v128)
35+
; STRICT-NEXT: # %bb.0:
36+
; STRICT-NEXT: f32x4.mul $push0=, $1, $0
37+
; STRICT-NEXT: f32x4.sub $push1=, $2, $pop0
38+
; STRICT-NEXT: return $pop1
39+
%mul = fmul contract <4 x float> %b, %a
40+
%sub = fsub contract <4 x float> %c, %mul
41+
ret <4 x float> %sub
42+
}
43+
44+
45+
define <8 x half> @fsub_fmul_contract_8xf16(<8 x half> %a, <8 x half> %b, <8 x half> %c) {
46+
; RELAXED-LABEL: fsub_fmul_contract_8xf16:
47+
; RELAXED: .functype fsub_fmul_contract_8xf16 (v128, v128, v128) -> (v128)
48+
; RELAXED-NEXT: # %bb.0:
49+
; RELAXED-NEXT: f16x8.relaxed_nmadd $push0=, $2, $1, $0
50+
; RELAXED-NEXT: return $pop0
51+
;
52+
; STRICT-LABEL: fsub_fmul_contract_8xf16:
53+
; STRICT: .functype fsub_fmul_contract_8xf16 (v128, v128, v128) -> (v128)
54+
; STRICT-NEXT: # %bb.0:
55+
; STRICT-NEXT: f16x8.mul $push0=, $1, $0
56+
; STRICT-NEXT: f16x8.sub $push1=, $2, $pop0
57+
; STRICT-NEXT: return $pop1
58+
%mul = fmul contract <8 x half> %b, %a
59+
%sub = fsub contract <8 x half> %c, %mul
60+
ret <8 x half> %sub
61+
}
62+
63+
64+
define <4 x float> @fsub_fmul_4xf32(<4 x float> %a, <4 x float> %b, <4 x float> %c) {
65+
; RELAXED-LABEL: fsub_fmul_4xf32:
66+
; RELAXED: .functype fsub_fmul_4xf32 (v128, v128, v128) -> (v128)
67+
; RELAXED-NEXT: # %bb.0:
68+
; RELAXED-NEXT: f32x4.mul $push0=, $1, $0
69+
; RELAXED-NEXT: f32x4.sub $push1=, $2, $pop0
70+
; RELAXED-NEXT: return $pop1
71+
;
72+
; STRICT-LABEL: fsub_fmul_4xf32:
73+
; STRICT: .functype fsub_fmul_4xf32 (v128, v128, v128) -> (v128)
74+
; STRICT-NEXT: # %bb.0:
75+
; STRICT-NEXT: f32x4.mul $push0=, $1, $0
76+
; STRICT-NEXT: f32x4.sub $push1=, $2, $pop0
77+
; STRICT-NEXT: return $pop1
78+
%mul = fmul <4 x float> %b, %a
79+
%sub = fsub contract <4 x float> %c, %mul
80+
ret <4 x float> %sub
81+
}
82+
83+
define <8 x float> @fsub_fmul_contract_8xf32(<8 x float> %a, <8 x float> %b, <8 x float> %c) {
84+
; RELAXED-LABEL: fsub_fmul_contract_8xf32:
85+
; RELAXED: .functype fsub_fmul_contract_8xf32 (i32, v128, v128, v128, v128, v128, v128) -> ()
86+
; RELAXED-NEXT: # %bb.0:
87+
; RELAXED-NEXT: f32x4.relaxed_nmadd $push0=, $6, $4, $2
88+
; RELAXED-NEXT: v128.store 16($0), $pop0
89+
; RELAXED-NEXT: f32x4.relaxed_nmadd $push1=, $5, $3, $1
90+
; RELAXED-NEXT: v128.store 0($0), $pop1
91+
; RELAXED-NEXT: return
92+
;
93+
; STRICT-LABEL: fsub_fmul_contract_8xf32:
94+
; STRICT: .functype fsub_fmul_contract_8xf32 (i32, v128, v128, v128, v128, v128, v128) -> ()
95+
; STRICT-NEXT: # %bb.0:
96+
; STRICT-NEXT: f32x4.mul $push0=, $4, $2
97+
; STRICT-NEXT: f32x4.sub $push1=, $6, $pop0
98+
; STRICT-NEXT: v128.store 16($0), $pop1
99+
; STRICT-NEXT: f32x4.mul $push2=, $3, $1
100+
; STRICT-NEXT: f32x4.sub $push3=, $5, $pop2
101+
; STRICT-NEXT: v128.store 0($0), $pop3
102+
; STRICT-NEXT: return
103+
%mul = fmul contract <8 x float> %b, %a
104+
%sub = fsub contract <8 x float> %c, %mul
105+
ret <8 x float> %sub
106+
}
107+
108+
109+
define <2 x double> @fsub_fmul_contract_2xf64(<2 x double> %a, <2 x double> %b, <2 x double> %c) {
110+
; RELAXED-LABEL: fsub_fmul_contract_2xf64:
111+
; RELAXED: .functype fsub_fmul_contract_2xf64 (v128, v128, v128) -> (v128)
112+
; RELAXED-NEXT: # %bb.0:
113+
; RELAXED-NEXT: f64x2.relaxed_nmadd $push0=, $2, $1, $0
114+
; RELAXED-NEXT: return $pop0
115+
;
116+
; STRICT-LABEL: fsub_fmul_contract_2xf64:
117+
; STRICT: .functype fsub_fmul_contract_2xf64 (v128, v128, v128) -> (v128)
118+
; STRICT-NEXT: # %bb.0:
119+
; STRICT-NEXT: f64x2.mul $push0=, $1, $0
120+
; STRICT-NEXT: f64x2.sub $push1=, $2, $pop0
121+
; STRICT-NEXT: return $pop1
122+
%mul = fmul contract <2 x double> %b, %a
123+
%sub = fsub contract <2 x double> %c, %mul
124+
ret <2 x double> %sub
125+
}
126+
127+
define float @fsub_fmul_contract_f32(float %a, float %b, float %c) {
128+
; RELAXED-LABEL: fsub_fmul_contract_f32:
129+
; RELAXED: .functype fsub_fmul_contract_f32 (f32, f32, f32) -> (f32)
130+
; RELAXED-NEXT: # %bb.0:
131+
; RELAXED-NEXT: f32.mul $push0=, $1, $0
132+
; RELAXED-NEXT: f32.sub $push1=, $2, $pop0
133+
; RELAXED-NEXT: return $pop1
134+
;
135+
; STRICT-LABEL: fsub_fmul_contract_f32:
136+
; STRICT: .functype fsub_fmul_contract_f32 (f32, f32, f32) -> (f32)
137+
; STRICT-NEXT: # %bb.0:
138+
; STRICT-NEXT: f32.mul $push0=, $1, $0
139+
; STRICT-NEXT: f32.sub $push1=, $2, $pop0
140+
; STRICT-NEXT: return $pop1
141+
%mul = fmul contract float %b, %a
142+
%sub = fsub contract float %c, %mul
143+
ret float %sub
144+
}
145+

0 commit comments

Comments
 (0)