Skip to content

Commit 8548fa0

Browse files
authored
[RISCV] Match fmaxnum and fminnum to reduction ops. (#159244)
This patch tries to match fmaxnum and fminnum to vector reductions.
1 parent 27f8f9e commit 8548fa0

File tree

2 files changed

+50
-7
lines changed

2 files changed

+50
-7
lines changed

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15144,6 +15144,10 @@ static unsigned getVecReduceOpcode(unsigned Opc) {
1514415144
case ISD::FADD:
1514515145
// Note: This is the associative form of the generic reduction opcode.
1514615146
return ISD::VECREDUCE_FADD;
15147+
case ISD::FMAXNUM:
15148+
return ISD::VECREDUCE_FMAX;
15149+
case ISD::FMINNUM:
15150+
return ISD::VECREDUCE_FMIN;
1514715151
}
1514815152
}
1514915153

@@ -15172,13 +15176,22 @@ combineBinOpOfExtractToReduceTree(SDNode *N, SelectionDAG &DAG,
1517215176
const EVT VT = N->getValueType(0);
1517315177
const unsigned Opc = N->getOpcode();
1517415178

15175-
// For FADD, we only handle the case with reassociation allowed. We
15176-
// could handle strict reduction order, but at the moment, there's no
15177-
// known reason to, and the complexity isn't worth it.
15178-
// TODO: Handle fminnum and fmaxnum here
15179-
if (!VT.isInteger() &&
15180-
(Opc != ISD::FADD || !N->getFlags().hasAllowReassociation()))
15181-
return SDValue();
15179+
if (!VT.isInteger()) {
15180+
switch (Opc) {
15181+
default:
15182+
return SDValue();
15183+
case ISD::FADD:
15184+
// For FADD, we only handle the case with reassociation allowed. We
15185+
// could handle strict reduction order, but at the moment, there's no
15186+
// known reason to, and the complexity isn't worth it.
15187+
if (!N->getFlags().hasAllowReassociation())
15188+
return SDValue();
15189+
break;
15190+
case ISD::FMAXNUM:
15191+
case ISD::FMINNUM:
15192+
break;
15193+
}
15194+
}
1518215195

1518315196
const unsigned ReduceOpc = getVecReduceOpcode(Opc);
1518415197
assert(Opc == ISD::getVecReduceBaseOpcode(ReduceOpc) &&

llvm/test/CodeGen/RISCV/rvv/fixed-vectors-reduction-formation.ll

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,3 +904,33 @@ define float @reduce_fadd_4xi32_non_associative2(ptr %p) {
904904
%fadd2 = fadd fast float %fadd1, %e3
905905
ret float %fadd2
906906
}
907+
908+
define float @reduce_fmaxnum_16xf32_prefix2(ptr %p) {
909+
; CHECK-LABEL: reduce_fmaxnum_16xf32_prefix2:
910+
; CHECK: # %bb.0:
911+
; CHECK-NEXT: vsetivli zero, 2, e32, mf2, ta, ma
912+
; CHECK-NEXT: vle32.v v8, (a0)
913+
; CHECK-NEXT: vfredmax.vs v8, v8, v8
914+
; CHECK-NEXT: vfmv.f.s fa0, v8
915+
; CHECK-NEXT: ret
916+
%v = load <16 x float>, ptr %p, align 256
917+
%e0 = extractelement <16 x float> %v, i32 0
918+
%e1 = extractelement <16 x float> %v, i32 1
919+
%fmax0 = call float @llvm.maxnum.f32(float %e0, float %e1)
920+
ret float %fmax0
921+
}
922+
923+
define float @reduce_fminnum_16xf32_prefix2(ptr %p) {
924+
; CHECK-LABEL: reduce_fminnum_16xf32_prefix2:
925+
; CHECK: # %bb.0:
926+
; CHECK-NEXT: vsetivli zero, 2, e32, mf2, ta, ma
927+
; CHECK-NEXT: vle32.v v8, (a0)
928+
; CHECK-NEXT: vfredmin.vs v8, v8, v8
929+
; CHECK-NEXT: vfmv.f.s fa0, v8
930+
; CHECK-NEXT: ret
931+
%v = load <16 x float>, ptr %p, align 256
932+
%e0 = extractelement <16 x float> %v, i32 0
933+
%e1 = extractelement <16 x float> %v, i32 1
934+
%fmax0 = call float @llvm.minnum.f32(float %e0, float %e1)
935+
ret float %fmax0
936+
}

0 commit comments

Comments
 (0)