[llvm] [X86] Promote approximate v8f16 FMA to v8f32 (PR #228900)
KIM MYEONG SEOK via llvm-commits
llvm-commits at lists.llvm.org
Sun Oct 4 08:03:33 PDT 2026
https://github.com/mangsgi created https://github.com/llvm/llvm-project/pull/228900
On X86 targets with F16C and FMA, but without native FP16 arithmetic, an
`afn` `v8f16` FMA is currently scalarized through `f64`. On Alder Lake,
this produces 115 instructions and eight `__truncdfhf2` calls.
Promote the complete approximate FMA operation to `v8f32` before operation
legalization:
- extend the three `v8f16` operands to `v8f32`;
- evaluate one `v8f32` FMA;
- round the final result back to `v8f16`.
Promoting the complete FMA avoids the intermediate `v8f16` rounding that
would be introduced if the operation were expanded to separately promoted
`FMUL` and `FADD` nodes.
The transform is restricted to nodes with `afn`, targets with F16C and FMA,
and targets without native FP16 arithmetic. Promoting an exact `f16` FMA
through `f32` can produce a different result because of double rounding, so
exact and `contract`-only FMA retain the existing legalization path. Handling
exact FMA would require a separate rounding-correction scheme. Targets with
native FP16 arithmetic continue to select `vfmadd*ph`.
On Alder Lake, the resulting function contains seven instructions and no
libcalls.
Tests cover approximate, exact, `contract`, and `fast` FMA on Alder Lake,
Ivy Bridge, and Sapphire Rapids.
Proof: https://godbolt.org/z/ffxfsrMsc
Fixes #228091
>From daa9223f436115173043d88848addf28b0083406 Mon Sep 17 00:00:00 2001
From: mangsgi <aodrntjr0901 at naver.com>
Date: Sun, 4 Oct 2026 20:20:31 +0900
Subject: [PATCH 1/2] [X86] Add coverage for approximate v8f16 FMA lowering
Record the existing lowering for approximate, exact, contract, and fast v8f16 FMA on F16C, non-FMA, and native FP16 targets.
---
llvm/test/CodeGen/X86/afn-fma-v8f16.ll | 95 ++++++++++++++++++++++++++
1 file changed, 95 insertions(+)
create mode 100644 llvm/test/CodeGen/X86/afn-fma-v8f16.ll
diff --git a/llvm/test/CodeGen/X86/afn-fma-v8f16.ll b/llvm/test/CodeGen/X86/afn-fma-v8f16.ll
new file mode 100644
index 0000000000000..dbf1b14ce1c82
--- /dev/null
+++ b/llvm/test/CodeGen/X86/afn-fma-v8f16.ll
@@ -0,0 +1,95 @@
+; RUN: llc < %s -mtriple=x86_64-unknown-linux-gnu -mcpu=alderlake | FileCheck %s --check-prefix=FMA
+; RUN: llc < %s -mtriple=x86_64-unknown-linux-gnu -mcpu=ivybridge | FileCheck %s --check-prefix=NOFMA
+; RUN: llc < %s -mtriple=x86_64-unknown-linux-gnu -mcpu=sapphirerapids | FileCheck %s --check-prefix=FP16
+
+; Check v8f16 FMA lowering with approximate, exact, and native FP16 paths.
+
+define <8 x half> @afn_fma(<8 x half> %a, <8 x half> %b, <8 x half> %c) {
+; FMA-LABEL: afn_fma:
+; FMA: vfmadd213sd
+; FMA-COUNT-8: callq __truncdfhf2 at PLT
+; FMA: retq
+;
+; NOFMA-LABEL: afn_fma:
+; NOFMA: callq fma at PLT
+; NOFMA-NEXT: callq __truncdfhf2 at PLT
+; NOFMA: retq
+;
+; FP16-LABEL: afn_fma:
+; FP16: vfmadd213ph %xmm2, %xmm1, %xmm0
+; FP16-NEXT: retq
+ %r = call afn <8 x half> @llvm.fma.v8f16(<8 x half> %a, <8 x half> %b, <8 x half> %c)
+ ret <8 x half> %r
+}
+
+define <8 x half> @exact_fma(<8 x half> %a, <8 x half> %b, <8 x half> %c) {
+; FMA-LABEL: exact_fma:
+; FMA: vfmadd213sd
+; FMA-COUNT-8: callq __truncdfhf2 at PLT
+; FMA: retq
+;
+; NOFMA-LABEL: exact_fma:
+; NOFMA: callq fma at PLT
+; NOFMA-NEXT: callq __truncdfhf2 at PLT
+; NOFMA: retq
+;
+; FP16-LABEL: exact_fma:
+; FP16: vfmadd213ph %xmm2, %xmm1, %xmm0
+; FP16-NEXT: retq
+ %r = call <8 x half> @llvm.fma.v8f16(<8 x half> %a, <8 x half> %b, <8 x half> %c)
+ ret <8 x half> %r
+}
+
+define <8 x half> @contract_fma(<8 x half> %a, <8 x half> %b, <8 x half> %c) {
+; FMA-LABEL: contract_fma:
+; FMA: vfmadd213sd
+; FMA-COUNT-8: callq __truncdfhf2 at PLT
+; FMA: retq
+;
+; NOFMA-LABEL: contract_fma:
+; NOFMA: callq fma at PLT
+; NOFMA-NEXT: callq __truncdfhf2 at PLT
+; NOFMA: retq
+;
+; FP16-LABEL: contract_fma:
+; FP16: vfmadd213ph %xmm2, %xmm1, %xmm0
+; FP16-NEXT: retq
+ %r = call contract <8 x half> @llvm.fma.v8f16(<8 x half> %a, <8 x half> %b, <8 x half> %c)
+ ret <8 x half> %r
+}
+
+define <8 x half> @fast_fma(<8 x half> %a, <8 x half> %b, <8 x half> %c) {
+; FMA-LABEL: fast_fma:
+; FMA: # %bb.0:
+; FMA-NEXT: vcvtph2ps %xmm1, %ymm1
+; FMA-NEXT: vcvtph2ps %xmm0, %ymm0
+; FMA-NEXT: vmulps %ymm1, %ymm0, %ymm0
+; FMA-NEXT: vcvtps2ph $4, %ymm0, %xmm0
+; FMA-NEXT: vcvtph2ps %xmm0, %ymm0
+; FMA-NEXT: vcvtph2ps %xmm2, %ymm1
+; FMA-NEXT: vaddps %ymm1, %ymm0, %ymm0
+; FMA-NEXT: vcvtps2ph $4, %ymm0, %xmm0
+; FMA-NEXT: vzeroupper
+; FMA-NEXT: retq
+;
+; NOFMA-LABEL: fast_fma:
+; NOFMA: # %bb.0:
+; NOFMA-NEXT: vcvtph2ps %xmm1, %ymm1
+; NOFMA-NEXT: vcvtph2ps %xmm0, %ymm0
+; NOFMA-NEXT: vmulps %ymm1, %ymm0, %ymm0
+; NOFMA-NEXT: vcvtps2ph $4, %ymm0, %xmm0
+; NOFMA-NEXT: vcvtph2ps %xmm0, %ymm0
+; NOFMA-NEXT: vcvtph2ps %xmm2, %ymm1
+; NOFMA-NEXT: vaddps %ymm1, %ymm0, %ymm0
+; NOFMA-NEXT: vcvtps2ph $4, %ymm0, %xmm0
+; NOFMA-NEXT: vzeroupper
+; NOFMA-NEXT: retq
+;
+; FP16-LABEL: fast_fma:
+; FP16: vfmadd213ph %xmm2, %xmm1, %xmm0
+; FP16-NEXT: retq
+ %r = call fast <8 x half> @llvm.fma.v8f16(<8 x half> %a, <8 x half> %b, <8 x half> %c)
+ ret <8 x half> %r
+}
+
+declare <8 x half> @llvm.fma.v8f16(<8 x half>, <8 x half>, <8 x half>)
>From 3fbde64970f6caf73fcf6b35a73dbe9ac7669205 Mon Sep 17 00:00:00 2001
From: mangsgi <aodrntjr0901 at naver.com>
Date: Sun, 4 Oct 2026 20:22:47 +0900
Subject: [PATCH 2/2] [X86] Promote approximate v8f16 FMA to v8f32
On X86 targets with F16C and FMA but without native FP16 arithmetic, approximate v8f16 FMA is currently scalarized through f64 and emits scalar conversion libcalls.
Promote the complete FMA to v8f32 before operation legalization and round the final result back to v8f16. This avoids the intermediate f16 rounding introduced when FMUL and FADD are promoted separately.
Exact and contract-only FMA retain the existing legalization path to avoid double rounding.
Proof: https://godbolt.org/z/ffxfsrMsc
Fixes #228091
---
llvm/lib/Target/X86/X86ISelLowering.cpp | 13 +++++++++++++
llvm/test/CodeGen/X86/afn-fma-v8f16.ll | 22 ++++++++++++----------
2 files changed, 25 insertions(+), 10 deletions(-)
diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp b/llvm/lib/Target/X86/X86ISelLowering.cpp
index c29b09ae846bb..247d15d4b64bb 100644
--- a/llvm/lib/Target/X86/X86ISelLowering.cpp
+++ b/llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -58416,6 +58416,19 @@ static SDValue combineFMA(SDNode *N, SelectionDAG &DAG,
const X86Subtarget &Subtarget) {
SDLoc dl(N);
EVT VT = N->getValueType(0);
+ if (N->getOpcode() == ISD::FMA && VT == MVT::v8f16 &&
+ DCI.isBeforeLegalizeOps() && N->getFlags().hasApproximateFuncs() &&
+ Subtarget.hasF16C() && Subtarget.hasAnyFMA() && !Subtarget.hasFP16()) {
+ // Promote approximate FMA as a whole to avoid the intermediate f16
+ // rounding introduced when FMUL and FADD are promoted separately.
+ // Exact half FMA must retain the existing legalization path.
+ SDValue A = DAG.getNode(ISD::FP_EXTEND, dl, MVT::v8f32, N->getOperand(0));
+ SDValue B = DAG.getNode(ISD::FP_EXTEND, dl, MVT::v8f32, N->getOperand(1));
+ SDValue C = DAG.getNode(ISD::FP_EXTEND, dl, MVT::v8f32, N->getOperand(2));
+ SDValue Res = DAG.getNode(ISD::FMA, dl, MVT::v8f32, A, B, C, N->getFlags());
+ return DAG.getNode(ISD::FP_ROUND, dl, VT, Res,
+ DAG.getIntPtrConstant(0, dl, /*isTarget=*/true));
+ }
const SelectionDAGTargetInfo &TSI = DAG.getSelectionDAGInfo();
bool IsStrict = N->isTargetOpcode()
? TSI.isTargetStrictFPOpcode(N->getOpcode())
diff --git a/llvm/test/CodeGen/X86/afn-fma-v8f16.ll b/llvm/test/CodeGen/X86/afn-fma-v8f16.ll
index dbf1b14ce1c82..35be11d2826f9 100644
--- a/llvm/test/CodeGen/X86/afn-fma-v8f16.ll
+++ b/llvm/test/CodeGen/X86/afn-fma-v8f16.ll
@@ -6,9 +6,14 @@
define <8 x half> @afn_fma(<8 x half> %a, <8 x half> %b, <8 x half> %c) {
; FMA-LABEL: afn_fma:
-; FMA: vfmadd213sd
-; FMA-COUNT-8: callq __truncdfhf2 at PLT
-; FMA: retq
+; FMA: # %bb.0:
+; FMA-NEXT: vcvtph2ps %xmm2, %ymm2
+; FMA-NEXT: vcvtph2ps %xmm0, %ymm0
+; FMA-NEXT: vcvtph2ps %xmm1, %ymm1
+; FMA-NEXT: vfmadd213ps {{.*#+}} ymm1 = (ymm0 * ymm1) + ymm2
+; FMA-NEXT: vcvtps2ph $4, %ymm1, %xmm0
+; FMA-NEXT: vzeroupper
+; FMA-NEXT: retq
;
; NOFMA-LABEL: afn_fma:
; NOFMA: callq fma at PLT
@@ -61,14 +66,11 @@ define <8 x half> @contract_fma(<8 x half> %a, <8 x half> %b, <8 x half> %c) {
define <8 x half> @fast_fma(<8 x half> %a, <8 x half> %b, <8 x half> %c) {
; FMA-LABEL: fast_fma:
; FMA: # %bb.0:
-; FMA-NEXT: vcvtph2ps %xmm1, %ymm1
+; FMA-NEXT: vcvtph2ps %xmm2, %ymm2
; FMA-NEXT: vcvtph2ps %xmm0, %ymm0
-; FMA-NEXT: vmulps %ymm1, %ymm0, %ymm0
-; FMA-NEXT: vcvtps2ph $4, %ymm0, %xmm0
-; FMA-NEXT: vcvtph2ps %xmm0, %ymm0
-; FMA-NEXT: vcvtph2ps %xmm2, %ymm1
-; FMA-NEXT: vaddps %ymm1, %ymm0, %ymm0
-; FMA-NEXT: vcvtps2ph $4, %ymm0, %xmm0
+; FMA-NEXT: vcvtph2ps %xmm1, %ymm1
+; FMA-NEXT: vfmadd213ps {{.*#+}} ymm1 = (ymm0 * ymm1) + ymm2
+; FMA-NEXT: vcvtps2ph $4, %ymm1, %xmm0
; FMA-NEXT: vzeroupper
; FMA-NEXT: retq
;
More information about the llvm-commits
mailing list