Skip to content

[WebAssembly] Add patterns for relaxed madd #147487

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 7 commits into from
Jul 14, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions llvm/include/llvm/CodeGen/SelectionDAGNodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ struct SDNodeFlags {
bool hasAllowReassociation() const { return Flags & AllowReassociation; }
bool hasNoFPExcept() const { return Flags & NoFPExcept; }
bool hasUnpredictable() const { return Flags & Unpredictable; }
bool hasFastMath() const { return Flags & FastMathFlags; }

bool operator==(const SDNodeFlags &Other) const {
return Flags == Other.Flags;
Expand Down
41 changes: 41 additions & 0 deletions llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,12 @@ WebAssemblyTargetLowering::WebAssemblyTargetLowering(
// SIMD-specific configuration
if (Subtarget->hasSIMD128()) {

// Enable fma optimization for wasm relaxed simd
if (Subtarget->hasRelaxedSIMD()) {
setTargetDAGCombine(ISD::FADD);
setTargetDAGCombine(ISD::FMA);
}

// Combine partial.reduce.add before legalization gets confused.
setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);

Expand Down Expand Up @@ -3412,6 +3418,37 @@ static SDValue performSETCCCombine(SDNode *N,
return SDValue();
}

static SDValue performFAddCombine(SDNode *N, SelectionDAG &DAG) {
assert(N->getOpcode() == ISD::FADD);
using namespace llvm::SDPatternMatch;
if (!N->getFlags().hasFastMath())
return SDValue();

SDLoc DL(N);
SDValue A, B, C;
EVT VecVT = N->getValueType(0);
if (sd_match(N, m_FAdd(m_Value(A), m_FMul(m_Value(B), m_Value(C)))))
return DAG.getNode(
ISD::INTRINSIC_WO_CHAIN, DL, VecVT,
{DAG.getConstant(Intrinsic::wasm_relaxed_madd, DL, MVT::i32), A, B, C});

return SDValue();
}

static SDValue performFMACombine(SDNode *N, SelectionDAG &DAG) {
assert(N->getOpcode() == ISD::FMA);
if (!N->getFlags().hasFastMath())
return SDValue();

SDLoc DL(N);
SDValue A = N->getOperand(0), B = N->getOperand(1), C = N->getOperand(2);
EVT VecVT = N->getValueType(0);

return DAG.getNode(
ISD::INTRINSIC_WO_CHAIN, DL, VecVT,
{DAG.getConstant(Intrinsic::wasm_relaxed_madd, DL, MVT::i32), A, B, C});
}

static SDValue performMulCombine(SDNode *N, SelectionDAG &DAG) {
assert(N->getOpcode() == ISD::MUL);
EVT VT = N->getValueType(0);
Expand Down Expand Up @@ -3529,6 +3566,10 @@ WebAssemblyTargetLowering::PerformDAGCombine(SDNode *N,
return AnyAllCombine;
return performLowerPartialReduction(N, DCI.DAG);
}
case ISD::FADD:
return performFAddCombine(N, DCI.DAG);
case ISD::FMA:
return performFMACombine(N, DCI.DAG);
case ISD::MUL:
return performMulCombine(N, DCI.DAG);
}
Expand Down
43 changes: 43 additions & 0 deletions llvm/test/CodeGen/WebAssembly/simd-relaxed-fma.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5

; RUN: llc < %s -verify-machineinstrs -disable-wasm-fallthrough-return-opt -wasm-disable-explicit-locals -wasm-keep-registers -mattr=+simd128,+relaxed-simd | FileCheck %s
target triple = "wasm32"
define void @fma_seperate(ptr %a, ptr %b, ptr %c, ptr %dest) {
; CHECK-LABEL: fma_seperate:
; CHECK: .functype fma_seperate (i32, i32, i32, i32) -> ()
; CHECK-NEXT: # %bb.0: # %entry
; CHECK-NEXT: v128.load $push2=, 0($2):p2align=0
; CHECK-NEXT: v128.load $push1=, 0($1):p2align=0
; CHECK-NEXT: v128.load $push0=, 0($0):p2align=0
; CHECK-NEXT: f32x4.relaxed_madd $push3=, $pop2, $pop1, $pop0
; CHECK-NEXT: v128.store 0($3):p2align=0, $pop3
; CHECK-NEXT: return
entry:
%0 = load <4 x float>, ptr %a, align 1
%1 = load <4 x float>, ptr %b, align 1
%2 = load <4 x float>, ptr %c, align 1
%mul.i = fmul fast <4 x float> %1, %0
%add.i = fadd fast <4 x float> %mul.i, %2
store <4 x float> %add.i, ptr %dest, align 1
ret void
}

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: readwrite)
define void @fma_llvm(ptr %a, ptr %b, ptr %c, ptr %dest) {
; CHECK-LABEL: fma_llvm:
; CHECK: .functype fma_llvm (i32, i32, i32, i32) -> ()
; CHECK-NEXT: # %bb.0: # %entry
; CHECK-NEXT: v128.load $push2=, 0($0):p2align=0
; CHECK-NEXT: v128.load $push1=, 0($1):p2align=0
; CHECK-NEXT: v128.load $push0=, 0($2):p2align=0
; CHECK-NEXT: f32x4.relaxed_madd $push3=, $pop2, $pop1, $pop0
; CHECK-NEXT: v128.store 0($3):p2align=0, $pop3
; CHECK-NEXT: return
entry:
%0 = load <4 x float>, ptr %a, align 1
%1 = load <4 x float>, ptr %b, align 1
%2 = load <4 x float>, ptr %c, align 1
%fma = tail call fast <4 x float> @llvm.fma.v4f32(<4 x float> %0, <4 x float> %1, <4 x float> %2)
store <4 x float> %fma, ptr %dest, align 1
ret void
}
Loading