[llvm] [X86] Don't fold fp_round pairs into f64->f16 with F16C (PR #230079)

Aochang Liu via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 8 08:15:55 PDT 2026


https://github.com/HelloWorldU updated https://github.com/llvm/llvm-project/pull/230079

>From d9ce33a19bc303049132cea3c23a313b96cee984 Mon Sep 17 00:00:00 2001
From: HelloWorldU <asd001liu at gmail.com>
Date: Wed, 7 Oct 2026 23:03:40 +0800
Subject: [PATCH 1/5] [X86] Add tests for fptrunc from double to half with
 fast-math flags (NFC)

Baseline for #229669.

Assisted-by: Claude Opus 5.5
---
 .../X86/fastmath-float-half-conversion.ll     | 103 +++++++++++++++++-
 1 file changed, 101 insertions(+), 2 deletions(-)

diff --git a/llvm/test/CodeGen/X86/fastmath-float-half-conversion.ll b/llvm/test/CodeGen/X86/fastmath-float-half-conversion.ll
index 1bc94b106d398..15f0aacb39d20 100644
--- a/llvm/test/CodeGen/X86/fastmath-float-half-conversion.ll
+++ b/llvm/test/CodeGen/X86/fastmath-float-half-conversion.ll
@@ -1,6 +1,6 @@
 ; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
-; RUN: llc -mtriple=x86_64-unknown-unknown -mattr=+f16c < %s | FileCheck %s --check-prefix=ALL
-; RUN: llc -mtriple=x86_64-unknown-unknown -mattr=+avx < %s | FileCheck %s --check-prefix=ALL
+; RUN: llc -mtriple=x86_64-unknown-unknown -mattr=+f16c < %s | FileCheck %s --check-prefixes=ALL,F16C
+; RUN: llc -mtriple=x86_64-unknown-unknown -mattr=+avx < %s | FileCheck %s --check-prefixes=ALL,AVX
 
 define zeroext i16 @test1_fast(double %d) #0 {
 ; ALL-LABEL: test1_fast:
@@ -62,6 +62,105 @@ entry:
   ret i16 %0
 }
 
+define half @fptrunc_afn(double %d) #0 {
+; ALL-LABEL: fptrunc_afn:
+; ALL:       # %bb.0:
+; ALL-NEXT:    pushq %rax
+; ALL-NEXT:    callq __truncdfhf2 at PLT
+; ALL-NEXT:    popq %rax
+; ALL-NEXT:    retq
+  %r = fptrunc afn double %d to half
+  ret half %r
+}
+
+define half @fptrunc_afn_twostep(double %d) #0 {
+; ALL-LABEL: fptrunc_afn_twostep:
+; ALL:       # %bb.0:
+; ALL-NEXT:    pushq %rax
+; ALL-NEXT:    callq __truncdfhf2 at PLT
+; ALL-NEXT:    popq %rax
+; ALL-NEXT:    retq
+  %f = fptrunc contract afn double %d to float
+  %r = fptrunc contract afn float %f to half
+  ret half %r
+}
+
+define <4 x half> @fptrunc_afn_v4f64(<4 x double> %d) #0 {
+; F16C-LABEL: fptrunc_afn_v4f64:
+; F16C:       # %bb.0:
+; F16C-NEXT:    subq $72, %rsp
+; F16C-NEXT:    vmovups %ymm0, {{[-0-9]+}}(%r{{[sb]}}p) # 32-byte Spill
+; F16C-NEXT:    vextractf128 $1, %ymm0, %xmm0
+; F16C-NEXT:    vmovaps %xmm0, (%rsp) # 16-byte Spill
+; F16C-NEXT:    vzeroupper
+; F16C-NEXT:    callq __truncdfhf2 at PLT
+; F16C-NEXT:    vmovaps %xmm0, {{[-0-9]+}}(%r{{[sb]}}p) # 16-byte Spill
+; F16C-NEXT:    vpermilpd $1, (%rsp), %xmm0 # 16-byte Folded Reload
+; F16C-NEXT:    # xmm0 = mem[1,0]
+; F16C-NEXT:    callq __truncdfhf2 at PLT
+; F16C-NEXT:    vmovdqa {{[-0-9]+}}(%r{{[sb]}}p), %xmm1 # 16-byte Reload
+; F16C-NEXT:    vpunpcklwd {{.*#+}} xmm0 = xmm1[0],xmm0[0],xmm1[1],xmm0[1],xmm1[2],xmm0[2],xmm1[3],xmm0[3]
+; F16C-NEXT:    vmovdqa %xmm0, {{[-0-9]+}}(%r{{[sb]}}p) # 16-byte Spill
+; F16C-NEXT:    vmovups {{[-0-9]+}}(%r{{[sb]}}p), %ymm0 # 32-byte Reload
+; F16C-NEXT:    # kill: def $xmm0 killed $xmm0 killed $ymm0
+; F16C-NEXT:    vzeroupper
+; F16C-NEXT:    callq __truncdfhf2 at PLT
+; F16C-NEXT:    vmovaps %xmm0, (%rsp) # 16-byte Spill
+; F16C-NEXT:    vpermilpd $1, {{[-0-9]+}}(%r{{[sb]}}p), %xmm0 # 16-byte Folded Reload
+; F16C-NEXT:    # xmm0 = mem[1,0]
+; F16C-NEXT:    callq __truncdfhf2 at PLT
+; F16C-NEXT:    vmovdqa (%rsp), %xmm1 # 16-byte Reload
+; F16C-NEXT:    vpunpcklwd {{.*#+}} xmm0 = xmm1[0],xmm0[0],xmm1[1],xmm0[1],xmm1[2],xmm0[2],xmm1[3],xmm0[3]
+; F16C-NEXT:    vinsertps $28, {{[-0-9]+}}(%r{{[sb]}}p), %xmm0, %xmm0 # 16-byte Folded Reload
+; F16C-NEXT:    # xmm0 = xmm0[0],mem[0],zero,zero
+; F16C-NEXT:    addq $72, %rsp
+; F16C-NEXT:    retq
+;
+; AVX-LABEL: fptrunc_afn_v4f64:
+; AVX:       # %bb.0:
+; AVX-NEXT:    subq $88, %rsp
+; AVX-NEXT:    vmovupd %ymm0, (%rsp) # 32-byte Spill
+; AVX-NEXT:    vshufpd {{.*#+}} xmm0 = xmm0[1,0]
+; AVX-NEXT:    vzeroupper
+; AVX-NEXT:    callq __truncdfhf2 at PLT
+; AVX-NEXT:    vmovapd %xmm0, {{[-0-9]+}}(%r{{[sb]}}p) # 16-byte Spill
+; AVX-NEXT:    vmovups (%rsp), %ymm0 # 32-byte Reload
+; AVX-NEXT:    # kill: def $xmm0 killed $xmm0 killed $ymm0
+; AVX-NEXT:    vzeroupper
+; AVX-NEXT:    callq __truncdfhf2 at PLT
+; AVX-NEXT:    vmovaps %xmm0, {{[-0-9]+}}(%r{{[sb]}}p) # 16-byte Spill
+; AVX-NEXT:    vmovups (%rsp), %ymm0 # 32-byte Reload
+; AVX-NEXT:    vextractf128 $1, %ymm0, %xmm0
+; AVX-NEXT:    vmovapd %xmm0, (%rsp) # 16-byte Spill
+; AVX-NEXT:    vshufpd {{.*#+}} xmm0 = xmm0[1,0]
+; AVX-NEXT:    vzeroupper
+; AVX-NEXT:    callq __truncdfhf2 at PLT
+; AVX-NEXT:    vmovapd %xmm0, {{[-0-9]+}}(%r{{[sb]}}p) # 16-byte Spill
+; AVX-NEXT:    vmovdqa (%rsp), %xmm0 # 16-byte Reload
+; AVX-NEXT:    callq __truncdfhf2 at PLT
+; AVX-NEXT:    vpunpcklwd {{[-0-9]+}}(%r{{[sb]}}p), %xmm0, %xmm0 # 16-byte Folded Reload
+; AVX-NEXT:    # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1],xmm0[2],mem[2],xmm0[3],mem[3]
+; AVX-NEXT:    vmovdqa {{[-0-9]+}}(%r{{[sb]}}p), %xmm1 # 16-byte Reload
+; AVX-NEXT:    vpunpcklwd {{[-0-9]+}}(%r{{[sb]}}p), %xmm1, %xmm1 # 16-byte Folded Reload
+; AVX-NEXT:    # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1],xmm1[2],mem[2],xmm1[3],mem[3]
+; AVX-NEXT:    vinsertps {{.*#+}} xmm0 = xmm1[0],xmm0[0],zero,zero
+; AVX-NEXT:    addq $88, %rsp
+; AVX-NEXT:    retq
+  %r = fptrunc afn <4 x double> %d to <4 x half>
+  ret <4 x half> %r
+}
+
+define half @fptrunc_contract(double %d) #0 {
+; ALL-LABEL: fptrunc_contract:
+; ALL:       # %bb.0:
+; ALL-NEXT:    pushq %rax
+; ALL-NEXT:    callq __truncdfhf2 at PLT
+; ALL-NEXT:    popq %rax
+; ALL-NEXT:    retq
+  %r = fptrunc contract double %d to half
+  ret half %r
+}
+
 declare i16 @llvm.convert.to.fp16.f64(double)
 declare i16 @llvm.convert.to.fp16.f80(x86_fp80)
 

>From f5d9d78d43fe9c93920136dad86e8423def9dd14 Mon Sep 17 00:00:00 2001
From: HelloWorldU <asd001liu at gmail.com>
Date: Wed, 7 Oct 2026 23:08:35 +0800
Subject: [PATCH 2/5] [X86] Use F16C for afn fptrunc from double to half

D130151 stopped splitting f64->f16 fp_round into f64->f32->f16, since
rounding twice is not the same as rounding once. Since then f64->f16
always becomes a __truncdfhf2 libcall without AVX512-FP16, even with
fast-math flags.

Round through f32 when the fp_round has afn and F16C is available, as
AMDGPU does for f64->f16. DAGCombiner folds a contract f64->f32->f16
pair into a single fp_round but dropped the flags, so keep the flags
common to both, letting such a pair with afn reach this lowering too.

Fixes #229669

Assisted-by: Claude Opus 5.5
---
 llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp |  3 +-
 llvm/lib/Target/X86/X86ISelLowering.cpp       |  8 ++
 .../X86/fastmath-float-half-conversion.ll     | 75 ++++++++++---------
 3 files changed, 48 insertions(+), 38 deletions(-)

diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index ad24ea1c840b3..6527fb824f66f 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -20890,7 +20890,8 @@ SDValue DAGCombiner::visitFP_ROUND(SDNode *N) {
         N0IsTrunc)
       return DAG.getNode(
           ISD::FP_ROUND, DL, VT, N0.getOperand(0),
-          DAG.getIntPtrConstant(NIsTrunc && N0IsTrunc, DL, /*isTarget=*/true));
+          DAG.getIntPtrConstant(NIsTrunc && N0IsTrunc, DL, /*isTarget=*/true),
+          N->getFlags() & N0->getFlags());
   }
 
   // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp b/llvm/lib/Target/X86/X86ISelLowering.cpp
index dfb63c3b609e1..96289e160b764 100644
--- a/llvm/lib/Target/X86/X86ISelLowering.cpp
+++ b/llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -22840,6 +22840,14 @@ SDValue X86TargetLowering::LowerFP_ROUND(SDValue Op, SelectionDAG &DAG) const {
   if (SVT == MVT::f128 || (VT == MVT::f16 && SVT == MVT::f80))
     return SDValue();
 
+  // Rounding through f32 may round twice, which afn permits.
+  if (VT == MVT::f16 && SVT == MVT::f64 && !IsStrict && !Subtarget.hasFP16() &&
+      Subtarget.hasF16C() && Op->getFlags().hasApproximateFuncs()) {
+    In = DAG.getNode(ISD::FP_ROUND, DL, MVT::f32, In,
+                     DAG.getIntPtrConstant(0, DL, /*isTarget=*/true));
+    SVT = MVT::f32;
+  }
+
   if (VT == MVT::f16 && (SVT == MVT::f64 || SVT == MVT::f32) &&
       !Subtarget.hasFP16() && (SVT == MVT::f64 || !Subtarget.hasF16C())) {
     if (!Subtarget.getTargetTriple().isOSDarwin())
diff --git a/llvm/test/CodeGen/X86/fastmath-float-half-conversion.ll b/llvm/test/CodeGen/X86/fastmath-float-half-conversion.ll
index 15f0aacb39d20..1ecdfe2a25dc4 100644
--- a/llvm/test/CodeGen/X86/fastmath-float-half-conversion.ll
+++ b/llvm/test/CodeGen/X86/fastmath-float-half-conversion.ll
@@ -63,23 +63,35 @@ entry:
 }
 
 define half @fptrunc_afn(double %d) #0 {
-; ALL-LABEL: fptrunc_afn:
-; ALL:       # %bb.0:
-; ALL-NEXT:    pushq %rax
-; ALL-NEXT:    callq __truncdfhf2 at PLT
-; ALL-NEXT:    popq %rax
-; ALL-NEXT:    retq
+; F16C-LABEL: fptrunc_afn:
+; F16C:       # %bb.0:
+; F16C-NEXT:    vcvtsd2ss %xmm0, %xmm0, %xmm0
+; F16C-NEXT:    vcvtps2ph $4, %xmm0, %xmm0
+; F16C-NEXT:    retq
+;
+; AVX-LABEL: fptrunc_afn:
+; AVX:       # %bb.0:
+; AVX-NEXT:    pushq %rax
+; AVX-NEXT:    callq __truncdfhf2 at PLT
+; AVX-NEXT:    popq %rax
+; AVX-NEXT:    retq
   %r = fptrunc afn double %d to half
   ret half %r
 }
 
 define half @fptrunc_afn_twostep(double %d) #0 {
-; ALL-LABEL: fptrunc_afn_twostep:
-; ALL:       # %bb.0:
-; ALL-NEXT:    pushq %rax
-; ALL-NEXT:    callq __truncdfhf2 at PLT
-; ALL-NEXT:    popq %rax
-; ALL-NEXT:    retq
+; F16C-LABEL: fptrunc_afn_twostep:
+; F16C:       # %bb.0:
+; F16C-NEXT:    vcvtsd2ss %xmm0, %xmm0, %xmm0
+; F16C-NEXT:    vcvtps2ph $4, %xmm0, %xmm0
+; F16C-NEXT:    retq
+;
+; AVX-LABEL: fptrunc_afn_twostep:
+; AVX:       # %bb.0:
+; AVX-NEXT:    pushq %rax
+; AVX-NEXT:    callq __truncdfhf2 at PLT
+; AVX-NEXT:    popq %rax
+; AVX-NEXT:    retq
   %f = fptrunc contract afn double %d to float
   %r = fptrunc contract afn float %f to half
   ret half %r
@@ -88,32 +100,21 @@ define half @fptrunc_afn_twostep(double %d) #0 {
 define <4 x half> @fptrunc_afn_v4f64(<4 x double> %d) #0 {
 ; F16C-LABEL: fptrunc_afn_v4f64:
 ; F16C:       # %bb.0:
-; F16C-NEXT:    subq $72, %rsp
-; F16C-NEXT:    vmovups %ymm0, {{[-0-9]+}}(%r{{[sb]}}p) # 32-byte Spill
-; F16C-NEXT:    vextractf128 $1, %ymm0, %xmm0
-; F16C-NEXT:    vmovaps %xmm0, (%rsp) # 16-byte Spill
-; F16C-NEXT:    vzeroupper
-; F16C-NEXT:    callq __truncdfhf2 at PLT
-; F16C-NEXT:    vmovaps %xmm0, {{[-0-9]+}}(%r{{[sb]}}p) # 16-byte Spill
-; F16C-NEXT:    vpermilpd $1, (%rsp), %xmm0 # 16-byte Folded Reload
-; F16C-NEXT:    # xmm0 = mem[1,0]
-; F16C-NEXT:    callq __truncdfhf2 at PLT
-; F16C-NEXT:    vmovdqa {{[-0-9]+}}(%r{{[sb]}}p), %xmm1 # 16-byte Reload
-; F16C-NEXT:    vpunpcklwd {{.*#+}} xmm0 = xmm1[0],xmm0[0],xmm1[1],xmm0[1],xmm1[2],xmm0[2],xmm1[3],xmm0[3]
-; F16C-NEXT:    vmovdqa %xmm0, {{[-0-9]+}}(%r{{[sb]}}p) # 16-byte Spill
-; F16C-NEXT:    vmovups {{[-0-9]+}}(%r{{[sb]}}p), %ymm0 # 32-byte Reload
-; F16C-NEXT:    # kill: def $xmm0 killed $xmm0 killed $ymm0
+; F16C-NEXT:    vextractf128 $1, %ymm0, %xmm1
+; F16C-NEXT:    vcvtsd2ss %xmm1, %xmm1, %xmm2
+; F16C-NEXT:    vcvtps2ph $4, %xmm2, %xmm2
+; F16C-NEXT:    vshufpd {{.*#+}} xmm1 = xmm1[1,0]
+; F16C-NEXT:    vcvtsd2ss %xmm1, %xmm1, %xmm1
+; F16C-NEXT:    vcvtps2ph $4, %xmm1, %xmm1
+; F16C-NEXT:    vpunpcklwd {{.*#+}} xmm1 = xmm2[0],xmm1[0],xmm2[1],xmm1[1],xmm2[2],xmm1[2],xmm2[3],xmm1[3]
+; F16C-NEXT:    vcvtsd2ss %xmm0, %xmm0, %xmm2
+; F16C-NEXT:    vcvtps2ph $4, %xmm2, %xmm2
+; F16C-NEXT:    vshufpd {{.*#+}} xmm0 = xmm0[1,0]
+; F16C-NEXT:    vcvtsd2ss %xmm0, %xmm0, %xmm0
+; F16C-NEXT:    vcvtps2ph $4, %xmm0, %xmm0
+; F16C-NEXT:    vpunpcklwd {{.*#+}} xmm0 = xmm2[0],xmm0[0],xmm2[1],xmm0[1],xmm2[2],xmm0[2],xmm2[3],xmm0[3]
+; F16C-NEXT:    vinsertps {{.*#+}} xmm0 = xmm0[0],xmm1[0],zero,zero
 ; F16C-NEXT:    vzeroupper
-; F16C-NEXT:    callq __truncdfhf2 at PLT
-; F16C-NEXT:    vmovaps %xmm0, (%rsp) # 16-byte Spill
-; F16C-NEXT:    vpermilpd $1, {{[-0-9]+}}(%r{{[sb]}}p), %xmm0 # 16-byte Folded Reload
-; F16C-NEXT:    # xmm0 = mem[1,0]
-; F16C-NEXT:    callq __truncdfhf2 at PLT
-; F16C-NEXT:    vmovdqa (%rsp), %xmm1 # 16-byte Reload
-; F16C-NEXT:    vpunpcklwd {{.*#+}} xmm0 = xmm1[0],xmm0[0],xmm1[1],xmm0[1],xmm1[2],xmm0[2],xmm1[3],xmm0[3]
-; F16C-NEXT:    vinsertps $28, {{[-0-9]+}}(%r{{[sb]}}p), %xmm0, %xmm0 # 16-byte Folded Reload
-; F16C-NEXT:    # xmm0 = xmm0[0],mem[0],zero,zero
-; F16C-NEXT:    addq $72, %rsp
 ; F16C-NEXT:    retq
 ;
 ; AVX-LABEL: fptrunc_afn_v4f64:

>From 8273ba0849a845f602bacc8b7132a87a921e921d Mon Sep 17 00:00:00 2001
From: HelloWorldU <asd001liu at gmail.com>
Date: Thu, 8 Oct 2026 23:15:03 +0800
Subject: [PATCH 3/5] [X86] Return the second fp_round directly

Assisted-by: Claude Opus 5.5
---
 llvm/lib/Target/X86/X86ISelLowering.cpp | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp b/llvm/lib/Target/X86/X86ISelLowering.cpp
index 96289e160b764..08c851a7db51d 100644
--- a/llvm/lib/Target/X86/X86ISelLowering.cpp
+++ b/llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -22843,9 +22843,9 @@ SDValue X86TargetLowering::LowerFP_ROUND(SDValue Op, SelectionDAG &DAG) const {
   // Rounding through f32 may round twice, which afn permits.
   if (VT == MVT::f16 && SVT == MVT::f64 && !IsStrict && !Subtarget.hasFP16() &&
       Subtarget.hasF16C() && Op->getFlags().hasApproximateFuncs()) {
-    In = DAG.getNode(ISD::FP_ROUND, DL, MVT::f32, In,
-                     DAG.getIntPtrConstant(0, DL, /*isTarget=*/true));
-    SVT = MVT::f32;
+    SDValue Rnd = DAG.getIntPtrConstant(0, DL, /*isTarget=*/true);
+    SDValue F32 = DAG.getNode(ISD::FP_ROUND, DL, MVT::f32, In, Rnd);
+    return DAG.getNode(ISD::FP_ROUND, DL, VT, F32, Rnd);
   }
 
   if (VT == MVT::f16 && (SVT == MVT::f64 || SVT == MVT::f32) &&

>From ca3aaeb9f0a7d7159104ae1af7fc385e1093030a Mon Sep 17 00:00:00 2001
From: HelloWorldU <asd001liu at gmail.com>
Date: Thu, 8 Oct 2026 18:45:52 +0800
Subject: [PATCH 4/5] [X86] Add test for contract fptrunc from double to float
 to half (NFC)

Assisted-by: Claude Opus 5.5
---
 .../CodeGen/X86/fastmath-float-half-conversion.ll    | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/llvm/test/CodeGen/X86/fastmath-float-half-conversion.ll b/llvm/test/CodeGen/X86/fastmath-float-half-conversion.ll
index 1ecdfe2a25dc4..37a6266d148ed 100644
--- a/llvm/test/CodeGen/X86/fastmath-float-half-conversion.ll
+++ b/llvm/test/CodeGen/X86/fastmath-float-half-conversion.ll
@@ -162,6 +162,18 @@ define half @fptrunc_contract(double %d) #0 {
   ret half %r
 }
 
+define half @fptrunc_contract_twostep(double %d) #0 {
+; ALL-LABEL: fptrunc_contract_twostep:
+; ALL:       # %bb.0:
+; ALL-NEXT:    pushq %rax
+; ALL-NEXT:    callq __truncdfhf2 at PLT
+; ALL-NEXT:    popq %rax
+; ALL-NEXT:    retq
+  %f = fptrunc contract double %d to float
+  %r = fptrunc contract float %f to half
+  ret half %r
+}
+
 declare i16 @llvm.convert.to.fp16.f64(double)
 declare i16 @llvm.convert.to.fp16.f80(x86_fp80)
 

>From b0513d439240cc70a2edafaf33dc3dd950fb3323 Mon Sep 17 00:00:00 2001
From: HelloWorldU <asd001liu at gmail.com>
Date: Thu, 8 Oct 2026 19:35:31 +0800
Subject: [PATCH 5/5] [X86] Don't fold fp_round pairs into f64->f16 with F16C

DAGCombiner folds a contract f64->f32->f16 fp_round pair into a single
f64->f16 fp_round. Without AVX512-FP16 that becomes a __truncdfhf2
libcall on X86, while F16C handles the two steps natively.

Add a TLI hook to let the target reject the fold, and reject it for
f64->f16 on X86 with F16C and without FP16.

Assisted-by: Claude Opus 5.5
---
 llvm/include/llvm/CodeGen/TargetLowering.h     |  6 ++++++
 llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp  |  3 +++
 llvm/lib/Target/X86/X86ISelLowering.cpp        |  8 ++++++++
 llvm/lib/Target/X86/X86ISelLowering.h          |  2 ++
 .../X86/fastmath-float-half-conversion.ll      | 18 ++++++++++++------
 5 files changed, 31 insertions(+), 6 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/TargetLowering.h b/llvm/include/llvm/CodeGen/TargetLowering.h
index 7c44a4a830020..697c417bda69d 100644
--- a/llvm/include/llvm/CodeGen/TargetLowering.h
+++ b/llvm/include/llvm/CodeGen/TargetLowering.h
@@ -3461,6 +3461,12 @@ class LLVM_ABI TargetLoweringBase {
     return false;
   }
 
+  /// Return true if (fp_round (fp_round x)) should be folded into a single
+  /// fp_round from \p SrcVT to \p DestVT.
+  virtual bool shouldFoldFPRoundPair(EVT DestVT, EVT SrcVT) const {
+    return true;
+  }
+
   /// Return true if an fpext operation input to an \p Opcode operation is free
   /// (for instance, because half-precision floating-point numbers are
   /// implicitly extended to float-precision) for an FMA instruction.
diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index 6527fb824f66f..61c27d8e68024 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -20880,6 +20880,9 @@ SDValue DAGCombiner::visitFP_ROUND(SDNode *N) {
     if (N0.getOperand(0).getValueType() == MVT::f80 && VT == MVT::f16)
       return SDValue();
 
+    if (!TLI.shouldFoldFPRoundPair(VT, N0.getOperand(0).getValueType()))
+      return SDValue();
+
     // If the first fp_round isn't a value preserving truncation, it might
     // introduce a tie in the second fp_round, that wouldn't occur in the
     // single-step fp_round we want to fold to.
diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp b/llvm/lib/Target/X86/X86ISelLowering.cpp
index 08c851a7db51d..24222265e5e95 100644
--- a/llvm/lib/Target/X86/X86ISelLowering.cpp
+++ b/llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -36771,6 +36771,14 @@ bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
   return NumBits1 > NumBits2;
 }
 
+bool X86TargetLowering::shouldFoldFPRoundPair(EVT DestVT, EVT SrcVT) const {
+  // Without FP16, f64->f16 is a libcall, but F16C rounds f64->f32->f16 in two
+  // instructions.
+  return DestVT.getScalarType() != MVT::f16 ||
+         SrcVT.getScalarType() != MVT::f64 || !Subtarget.hasF16C() ||
+         Subtarget.hasFP16();
+}
+
 bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
   // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
   return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget.is64Bit();
diff --git a/llvm/lib/Target/X86/X86ISelLowering.h b/llvm/lib/Target/X86/X86ISelLowering.h
index ba04080573c42..9d2d265fdc56e 100644
--- a/llvm/lib/Target/X86/X86ISelLowering.h
+++ b/llvm/lib/Target/X86/X86ISelLowering.h
@@ -484,6 +484,8 @@ namespace llvm {
     bool isTruncateFree(Type *Ty1, Type *Ty2) const override;
     bool isTruncateFree(EVT VT1, EVT VT2) const override;
 
+    bool shouldFoldFPRoundPair(EVT DestVT, EVT SrcVT) const override;
+
     bool allowTruncateForTailCall(Type *Ty1, Type *Ty2) const override;
 
     /// Return true if any actual instruction that defines a
diff --git a/llvm/test/CodeGen/X86/fastmath-float-half-conversion.ll b/llvm/test/CodeGen/X86/fastmath-float-half-conversion.ll
index 37a6266d148ed..584ba8c1c1cea 100644
--- a/llvm/test/CodeGen/X86/fastmath-float-half-conversion.ll
+++ b/llvm/test/CodeGen/X86/fastmath-float-half-conversion.ll
@@ -163,12 +163,18 @@ define half @fptrunc_contract(double %d) #0 {
 }
 
 define half @fptrunc_contract_twostep(double %d) #0 {
-; ALL-LABEL: fptrunc_contract_twostep:
-; ALL:       # %bb.0:
-; ALL-NEXT:    pushq %rax
-; ALL-NEXT:    callq __truncdfhf2 at PLT
-; ALL-NEXT:    popq %rax
-; ALL-NEXT:    retq
+; F16C-LABEL: fptrunc_contract_twostep:
+; F16C:       # %bb.0:
+; F16C-NEXT:    vcvtsd2ss %xmm0, %xmm0, %xmm0
+; F16C-NEXT:    vcvtps2ph $4, %xmm0, %xmm0
+; F16C-NEXT:    retq
+;
+; AVX-LABEL: fptrunc_contract_twostep:
+; AVX:       # %bb.0:
+; AVX-NEXT:    pushq %rax
+; AVX-NEXT:    callq __truncdfhf2 at PLT
+; AVX-NEXT:    popq %rax
+; AVX-NEXT:    retq
   %f = fptrunc contract double %d to float
   %r = fptrunc contract float %f to half
   ret half %r



More information about the llvm-commits mailing list