[llvm] 55a3382 - [X86] Add DAG combine to fold promoted f32 sequences for f16 fneg and fabs (#189395)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Apr 7 02:11:09 PDT 2026
Author: Tharun V K
Date: 2026-04-07T17:11:03+08:00
New Revision: 55a3382be485148a3ab29c8942a07f13e9ba036a
URL: https://github.com/llvm/llvm-project/commit/55a3382be485148a3ab29c8942a07f13e9ba036a
DIFF: https://github.com/llvm/llvm-project/commit/55a3382be485148a3ab29c8942a07f13e9ba036a.diff
LOG: [X86] Add DAG combine to fold promoted f32 sequences for f16 fneg and fabs (#189395)
This patch optimizes f16 fneg and fabs on X86 targets by introducing
a DAG combine to identify and collapse fpext -> fneg/fabs -> fptrunc.
Generally f16 operations are promoted to f32. For bitwise-equivalent
operations like fneg and fabs, this results in unnecessary and
expensive f32 library calls (__extendhfsf2 / __truncsfhf2) or
hardware conversions (vcvtph2ps / vcvtps2ph) at -O0.
Fixes: https://github.com/llvm/llvm-project/issues/188201
---------
Co-authored-by: Phoebe Wang <phoebe.wang at intel.com>
Added:
llvm/test/CodeGen/X86/half-fneg-fabs.ll
Modified:
llvm/lib/Target/X86/X86ISelLowering.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp b/llvm/lib/Target/X86/X86ISelLowering.cpp
index 67d25e4eabbac..ebc8766969251 100644
--- a/llvm/lib/Target/X86/X86ISelLowering.cpp
+++ b/llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -61982,20 +61982,30 @@ static SDValue combineBROADCAST_LOAD(SDNode *N, SelectionDAG &DAG,
static SDValue combineFP_ROUND(SDNode *N, SelectionDAG &DAG,
const X86Subtarget &Subtarget) {
- if (!Subtarget.hasF16C() || Subtarget.useSoftFloat())
- return SDValue();
bool IsStrict = N->isStrictFPOpcode();
EVT VT = N->getValueType(0);
SDValue Src = N->getOperand(IsStrict ? 1 : 0);
+ SDLoc dl(N);
+
+ if (!IsStrict && VT == MVT::f16 &&
+ (Src.getOpcode() == ISD::FNEG || Src.getOpcode() == ISD::FABS)) {
+ SDValue Inner = Src.getOperand(0);
+ if (Inner.getOpcode() == ISD::FP_EXTEND &&
+ Inner.getOperand(0).getValueType() == MVT::f16) {
+ return DAG.getNode(Src.getOpcode(), dl, MVT::f16, Inner.getOperand(0));
+ }
+ }
+
+ if (!Subtarget.hasF16C() || Subtarget.useSoftFloat())
+ return SDValue();
+
EVT SrcVT = Src.getValueType();
if (!VT.isVector() || VT.getVectorElementType() != MVT::f16 ||
SrcVT.getVectorElementType() != MVT::f32)
return SDValue();
- SDLoc dl(N);
-
SDValue Cvt, Chain;
unsigned NumElts = VT.getVectorNumElements();
if (Subtarget.hasFP16()) {
diff --git a/llvm/test/CodeGen/X86/half-fneg-fabs.ll b/llvm/test/CodeGen/X86/half-fneg-fabs.ll
new file mode 100644
index 0000000000000..ea461a0bc741a
--- /dev/null
+++ b/llvm/test/CodeGen/X86/half-fneg-fabs.ll
@@ -0,0 +1,79 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
+; RUN: llc < %s -mtriple=x86_64-unknown-linux-gnu -mattr=+sse2 | FileCheck %s --check-prefixes=SSE2
+; RUN: llc < %s -mtriple=x86_64-unknown-linux-gnu -mattr=+sse2 -fast-isel | FileCheck %s --check-prefixes=SSE2-FAST
+; RUN: llc < %s -mtriple=x86_64-unknown-linux-gnu -mattr=+avx | FileCheck %s --check-prefixes=AVX
+; RUN: llc < %s -mtriple=x86_64-unknown-linux-gnu -mattr=+avx -fast-isel | FileCheck %s --check-prefixes=AVX-FAST
+; RUN: llc < %s -mtriple=x86_64-unknown-linux-gnu -mattr=+f16c | FileCheck %s --check-prefixes=F16C
+; RUN: llc < %s -mtriple=x86_64-unknown-linux-gnu -mattr=+f16c -fast-isel | FileCheck %s --check-prefixes=F16C-FAST
+
+define half @test_fneg(half %a) nounwind {
+; SSE2-LABEL: test_fneg:
+; SSE2: # %bb.0:
+; SSE2-NEXT: xorps {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %xmm0
+; SSE2-NEXT: retq
+;
+; SSE2-FAST-LABEL: test_fneg:
+; SSE2-FAST: # %bb.0:
+; SSE2-FAST-NEXT: xorps {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %xmm0
+; SSE2-FAST-NEXT: retq
+;
+; AVX-LABEL: test_fneg:
+; AVX: # %bb.0:
+; AVX-NEXT: vxorps {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %xmm0, %xmm0
+; AVX-NEXT: retq
+;
+; AVX-FAST-LABEL: test_fneg:
+; AVX-FAST: # %bb.0:
+; AVX-FAST-NEXT: vxorps {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %xmm0, %xmm0
+; AVX-FAST-NEXT: retq
+;
+; F16C-LABEL: test_fneg:
+; F16C: # %bb.0:
+; F16C-NEXT: vxorps {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %xmm0, %xmm0
+; F16C-NEXT: retq
+;
+; F16C-FAST-LABEL: test_fneg:
+; F16C-FAST: # %bb.0:
+; F16C-FAST-NEXT: vxorps {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %xmm0, %xmm0
+; F16C-FAST-NEXT: retq
+ %ext = fpext half %a to float
+ %neg = fneg float %ext
+ %res = fptrunc float %neg to half
+ ret half %res
+}
+
+define half @test_fabs(half %a) nounwind {
+; SSE2-LABEL: test_fabs:
+; SSE2: # %bb.0:
+; SSE2-NEXT: andps {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %xmm0
+; SSE2-NEXT: retq
+;
+; SSE2-FAST-LABEL: test_fabs:
+; SSE2-FAST: # %bb.0:
+; SSE2-FAST-NEXT: andps {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %xmm0
+; SSE2-FAST-NEXT: retq
+;
+; AVX-LABEL: test_fabs:
+; AVX: # %bb.0:
+; AVX-NEXT: vandps {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %xmm0, %xmm0
+; AVX-NEXT: retq
+;
+; AVX-FAST-LABEL: test_fabs:
+; AVX-FAST: # %bb.0:
+; AVX-FAST-NEXT: vandps {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %xmm0, %xmm0
+; AVX-FAST-NEXT: retq
+;
+; F16C-LABEL: test_fabs:
+; F16C: # %bb.0:
+; F16C-NEXT: vandps {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %xmm0, %xmm0
+; F16C-NEXT: retq
+;
+; F16C-FAST-LABEL: test_fabs:
+; F16C-FAST: # %bb.0:
+; F16C-FAST-NEXT: vandps {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %xmm0, %xmm0
+; F16C-FAST-NEXT: retq
+ %ext = fpext half %a to float
+ %abs = call float @llvm.fabs.f32(float %ext)
+ %res = fptrunc float %abs to half
+ ret half %res
+}
More information about the llvm-commits
mailing list