[llvm] [X86] Do not lower f16 to i128 conversions as i64 on Win64 (PR #230712)

via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 9 16:53:12 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-x86

Author: Aochang Liu (HelloWorldU)

<details>
<summary>Changes</summary>

The f16 case of the i64 vector conversion path in ReplaceNodeResults did not check the result type. On Win64, f16 to i128 conversions took this path. As a result, only the low 64 bits were converted, and assertions builds hit "i64 should be legal".

Check VT == MVT::i64 for f16 too, so i128 uses the Win64 libcall like f32 and f64.

Fixes #<!-- -->230703

Assisted-by: Claude Opus 5.5

---
Full diff: https://github.com/llvm/llvm-project/pull/230712.diff


2 Files Affected:

- (modified) llvm/lib/Target/X86/X86ISelLowering.cpp (+3-3) 
- (added) llvm/test/CodeGen/X86/i128-fpconv-win64-fp16.ll (+55) 


``````````diff
diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp b/llvm/lib/Target/X86/X86ISelLowering.cpp
index a265c163cf276..e57b783e8f53b 100644
--- a/llvm/lib/Target/X86/X86ISelLowering.cpp
+++ b/llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -36020,9 +36020,9 @@ void X86TargetLowering::ReplaceNodeResults(SDNode *N,
 
     assert(!VT.isVector() && "Vectors should have been handled above!");
 
-    if ((Subtarget.hasDQI() && VT == MVT::i64 &&
-         (SrcVT == MVT::f32 || SrcVT == MVT::f64)) ||
-        (Subtarget.hasFP16() && SrcVT == MVT::f16)) {
+    if (VT == MVT::i64 &&
+        ((Subtarget.hasDQI() && (SrcVT == MVT::f32 || SrcVT == MVT::f64)) ||
+         (Subtarget.hasFP16() && SrcVT == MVT::f16))) {
       assert(!Subtarget.is64Bit() && "i64 should be legal");
       unsigned NumElts = Subtarget.hasVLX() ? 2 : 8;
       // If we use a 128-bit result we might need to use a target specific node.
diff --git a/llvm/test/CodeGen/X86/i128-fpconv-win64-fp16.ll b/llvm/test/CodeGen/X86/i128-fpconv-win64-fp16.ll
new file mode 100644
index 0000000000000..d7cf99f783a34
--- /dev/null
+++ b/llvm/test/CodeGen/X86/i128-fpconv-win64-fp16.ll
@@ -0,0 +1,55 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
+; RUN: llc < %s -mtriple=x86_64-win32 -mattr=+avx512fp16 | FileCheck %s -check-prefix=WIN64
+; RUN: llc < %s -mtriple=x86_64-mingw32 -mattr=+avx512fp16 | FileCheck %s -check-prefix=WIN64
+
+define i128 @half_to_i128(half %x) nounwind {
+; WIN64-LABEL: half_to_i128:
+; WIN64:       # %bb.0:
+; WIN64-NEXT:    subq $40, %rsp
+; WIN64-NEXT:    callq __fixhfti
+; WIN64-NEXT:    vmovq %xmm0, %rax
+; WIN64-NEXT:    vpextrq $1, %xmm0, %rdx
+; WIN64-NEXT:    addq $40, %rsp
+; WIN64-NEXT:    retq
+  %r = fptosi half %x to i128
+  ret i128 %r
+}
+
+define i128 @half_to_ui128(half %x) nounwind {
+; WIN64-LABEL: half_to_ui128:
+; WIN64:       # %bb.0:
+; WIN64-NEXT:    subq $40, %rsp
+; WIN64-NEXT:    callq __fixunshfti
+; WIN64-NEXT:    vmovq %xmm0, %rax
+; WIN64-NEXT:    vpextrq $1, %xmm0, %rdx
+; WIN64-NEXT:    addq $40, %rsp
+; WIN64-NEXT:    retq
+  %r = fptoui half %x to i128
+  ret i128 %r
+}
+
+define i128 @half_to_i128_strict(half %x) nounwind strictfp {
+; WIN64-LABEL: half_to_i128_strict:
+; WIN64:       # %bb.0:
+; WIN64-NEXT:    subq $40, %rsp
+; WIN64-NEXT:    callq __fixhfti
+; WIN64-NEXT:    vmovq %xmm0, %rax
+; WIN64-NEXT:    vpextrq $1, %xmm0, %rdx
+; WIN64-NEXT:    addq $40, %rsp
+; WIN64-NEXT:    retq
+  %r = call i128 @llvm.experimental.constrained.fptosi.i128.f16(half %x, metadata !"fpexcept.strict") strictfp
+  ret i128 %r
+}
+
+define i128 @half_to_ui128_strict(half %x) nounwind strictfp {
+; WIN64-LABEL: half_to_ui128_strict:
+; WIN64:       # %bb.0:
+; WIN64-NEXT:    subq $40, %rsp
+; WIN64-NEXT:    callq __fixunshfti
+; WIN64-NEXT:    vmovq %xmm0, %rax
+; WIN64-NEXT:    vpextrq $1, %xmm0, %rdx
+; WIN64-NEXT:    addq $40, %rsp
+; WIN64-NEXT:    retq
+  %r = call i128 @llvm.experimental.constrained.fptoui.i128.f16(half %x, metadata !"fpexcept.strict") strictfp
+  ret i128 %r
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/230712


More information about the llvm-commits mailing list