Skip to content

[WebAssembly] Add pattern for relaxed nmadd #150684

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions llvm/include/llvm/Target/TargetSelectionDAG.td
Original file line number Diff line number Diff line change
Expand Up @@ -1147,6 +1147,9 @@ def fadd_contract : PatFrag<(ops node:$a, node:$b), (fadd node:$a, node:$b),[{
return N->getFlags().hasAllowContract();
}]>;

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

def not : PatFrag<(ops node:$in), (xor node:$in, -1)>;
def vnot : PatFrag<(ops node:$in), (xor node:$in, immAllOnesV)>;
Expand Down
2 changes: 2 additions & 0 deletions llvm/lib/Target/WebAssembly/WebAssemblyInstrSIMD.td
Original file line number Diff line number Diff line change
Expand Up @@ -1540,6 +1540,8 @@ multiclass SIMDMADD<Vec vec, bits<32> simdopA, bits<32> simdopS, list<Predicate>
def : Pat<(fadd_contract (vec.vt V128:$a), (fmul_contract (vec.vt V128:$b), (vec.vt V128:$c))),
(!cast<Instruction>("MADD_"#vec) V128:$a, V128:$b, V128:$c)>, Requires<[HasRelaxedSIMD]>;

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

defm "" : SIMDMADD<F32x4, 0x105, 0x106, [HasRelaxedSIMD]>;
Expand Down
145 changes: 145 additions & 0 deletions llvm/test/CodeGen/WebAssembly/simd-relaxed-fnma.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
; 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
; RUN: llc < %s -disable-wasm-fallthrough-return-opt -wasm-disable-explicit-locals -wasm-keep-registers -mattr=+fp16,+simd128, | FileCheck %s --check-prefix=STRICT

target triple = "wasm32"

define double @fsub_fmul_contract_f64(double %a, double %b, double %c) {
; RELAXED-LABEL: fsub_fmul_contract_f64:
; RELAXED: .functype fsub_fmul_contract_f64 (f64, f64, f64) -> (f64)
; RELAXED-NEXT: # %bb.0:
; RELAXED-NEXT: f64.mul $push0=, $1, $0
; RELAXED-NEXT: f64.sub $push1=, $2, $pop0
; RELAXED-NEXT: return $pop1
;
; STRICT-LABEL: fsub_fmul_contract_f64:
; STRICT: .functype fsub_fmul_contract_f64 (f64, f64, f64) -> (f64)
; STRICT-NEXT: # %bb.0:
; STRICT-NEXT: f64.mul $push0=, $1, $0
; STRICT-NEXT: f64.sub $push1=, $2, $pop0
; STRICT-NEXT: return $pop1
%mul = fmul contract double %b, %a
%sub = fsub contract double %c, %mul
ret double %sub
}

define <4 x float> @fsub_fmul_contract_4xf32(<4 x float> %a, <4 x float> %b, <4 x float> %c) {
; RELAXED-LABEL: fsub_fmul_contract_4xf32:
; RELAXED: .functype fsub_fmul_contract_4xf32 (v128, v128, v128) -> (v128)
; RELAXED-NEXT: # %bb.0:
; RELAXED-NEXT: f32x4.relaxed_nmadd $push0=, $2, $1, $0
; RELAXED-NEXT: return $pop0
;
; STRICT-LABEL: fsub_fmul_contract_4xf32:
; STRICT: .functype fsub_fmul_contract_4xf32 (v128, v128, v128) -> (v128)
; STRICT-NEXT: # %bb.0:
; STRICT-NEXT: f32x4.mul $push0=, $1, $0
; STRICT-NEXT: f32x4.sub $push1=, $2, $pop0
; STRICT-NEXT: return $pop1
%mul = fmul contract <4 x float> %b, %a
%sub = fsub contract <4 x float> %c, %mul
ret <4 x float> %sub
}


define <8 x half> @fsub_fmul_contract_8xf16(<8 x half> %a, <8 x half> %b, <8 x half> %c) {
; RELAXED-LABEL: fsub_fmul_contract_8xf16:
; RELAXED: .functype fsub_fmul_contract_8xf16 (v128, v128, v128) -> (v128)
; RELAXED-NEXT: # %bb.0:
; RELAXED-NEXT: f16x8.relaxed_nmadd $push0=, $2, $1, $0
; RELAXED-NEXT: return $pop0
;
; STRICT-LABEL: fsub_fmul_contract_8xf16:
; STRICT: .functype fsub_fmul_contract_8xf16 (v128, v128, v128) -> (v128)
; STRICT-NEXT: # %bb.0:
; STRICT-NEXT: f16x8.mul $push0=, $1, $0
; STRICT-NEXT: f16x8.sub $push1=, $2, $pop0
; STRICT-NEXT: return $pop1
%mul = fmul contract <8 x half> %b, %a
%sub = fsub contract <8 x half> %c, %mul
ret <8 x half> %sub
}


define <4 x float> @fsub_fmul_4xf32(<4 x float> %a, <4 x float> %b, <4 x float> %c) {
; RELAXED-LABEL: fsub_fmul_4xf32:
; RELAXED: .functype fsub_fmul_4xf32 (v128, v128, v128) -> (v128)
; RELAXED-NEXT: # %bb.0:
; RELAXED-NEXT: f32x4.mul $push0=, $1, $0
; RELAXED-NEXT: f32x4.sub $push1=, $2, $pop0
; RELAXED-NEXT: return $pop1
;
; STRICT-LABEL: fsub_fmul_4xf32:
; STRICT: .functype fsub_fmul_4xf32 (v128, v128, v128) -> (v128)
; STRICT-NEXT: # %bb.0:
; STRICT-NEXT: f32x4.mul $push0=, $1, $0
; STRICT-NEXT: f32x4.sub $push1=, $2, $pop0
; STRICT-NEXT: return $pop1
%mul = fmul <4 x float> %b, %a
%sub = fsub contract <4 x float> %c, %mul
ret <4 x float> %sub
}

define <8 x float> @fsub_fmul_contract_8xf32(<8 x float> %a, <8 x float> %b, <8 x float> %c) {
; RELAXED-LABEL: fsub_fmul_contract_8xf32:
; RELAXED: .functype fsub_fmul_contract_8xf32 (i32, v128, v128, v128, v128, v128, v128) -> ()
; RELAXED-NEXT: # %bb.0:
; RELAXED-NEXT: f32x4.relaxed_nmadd $push0=, $6, $4, $2
; RELAXED-NEXT: v128.store 16($0), $pop0
; RELAXED-NEXT: f32x4.relaxed_nmadd $push1=, $5, $3, $1
; RELAXED-NEXT: v128.store 0($0), $pop1
; RELAXED-NEXT: return
;
; STRICT-LABEL: fsub_fmul_contract_8xf32:
; STRICT: .functype fsub_fmul_contract_8xf32 (i32, v128, v128, v128, v128, v128, v128) -> ()
; STRICT-NEXT: # %bb.0:
; STRICT-NEXT: f32x4.mul $push0=, $4, $2
; STRICT-NEXT: f32x4.sub $push1=, $6, $pop0
; STRICT-NEXT: v128.store 16($0), $pop1
; STRICT-NEXT: f32x4.mul $push2=, $3, $1
; STRICT-NEXT: f32x4.sub $push3=, $5, $pop2
; STRICT-NEXT: v128.store 0($0), $pop3
; STRICT-NEXT: return
%mul = fmul contract <8 x float> %b, %a
%sub = fsub contract <8 x float> %c, %mul
ret <8 x float> %sub
}


define <2 x double> @fsub_fmul_contract_2xf64(<2 x double> %a, <2 x double> %b, <2 x double> %c) {
; RELAXED-LABEL: fsub_fmul_contract_2xf64:
; RELAXED: .functype fsub_fmul_contract_2xf64 (v128, v128, v128) -> (v128)
; RELAXED-NEXT: # %bb.0:
; RELAXED-NEXT: f64x2.relaxed_nmadd $push0=, $2, $1, $0
; RELAXED-NEXT: return $pop0
;
; STRICT-LABEL: fsub_fmul_contract_2xf64:
; STRICT: .functype fsub_fmul_contract_2xf64 (v128, v128, v128) -> (v128)
; STRICT-NEXT: # %bb.0:
; STRICT-NEXT: f64x2.mul $push0=, $1, $0
; STRICT-NEXT: f64x2.sub $push1=, $2, $pop0
; STRICT-NEXT: return $pop1
%mul = fmul contract <2 x double> %b, %a
%sub = fsub contract <2 x double> %c, %mul
ret <2 x double> %sub
}

define float @fsub_fmul_contract_f32(float %a, float %b, float %c) {
; RELAXED-LABEL: fsub_fmul_contract_f32:
; RELAXED: .functype fsub_fmul_contract_f32 (f32, f32, f32) -> (f32)
; RELAXED-NEXT: # %bb.0:
; RELAXED-NEXT: f32.mul $push0=, $1, $0
; RELAXED-NEXT: f32.sub $push1=, $2, $pop0
; RELAXED-NEXT: return $pop1
;
; STRICT-LABEL: fsub_fmul_contract_f32:
; STRICT: .functype fsub_fmul_contract_f32 (f32, f32, f32) -> (f32)
; STRICT-NEXT: # %bb.0:
; STRICT-NEXT: f32.mul $push0=, $1, $0
; STRICT-NEXT: f32.sub $push1=, $2, $pop0
; STRICT-NEXT: return $pop1
%mul = fmul contract float %b, %a
%sub = fsub contract float %c, %mul
ret float %sub
}