[compiler-rt] [llvm] Reland "[X86][BF16] Add libcall for F80 -> BF16 (#109116)" (PR #109143)

Phoebe Wang via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 18 23:41:20 PDT 2024


https://github.com/phoebewang updated https://github.com/llvm/llvm-project/pull/109143

>From 428c77b2329e1df7f89dff3268302828b3d69f43 Mon Sep 17 00:00:00 2001
From: "Wang, Phoebe" <phoebe.wang at intel.com>
Date: Wed, 18 Sep 2024 21:38:45 +0800
Subject: [PATCH 1/2] Reland "[X86][BF16] Add libcall for F80 -> BF16
 (#109116)"

This reverts commit ababfee78714313a0cad87591b819f0944b90d09.
---
 compiler-rt/lib/builtins/CMakeLists.txt  |  1 +
 compiler-rt/lib/builtins/truncxfbf2.c    | 16 ++++++++++++++++
 llvm/include/llvm/IR/RuntimeLibcalls.def |  1 +
 llvm/lib/CodeGen/TargetLoweringBase.cpp  |  2 ++
 llvm/test/CodeGen/X86/bfloat.ll          | 22 ++++++++++++++++++++++
 5 files changed, 42 insertions(+)
 create mode 100644 compiler-rt/lib/builtins/truncxfbf2.c

diff --git a/compiler-rt/lib/builtins/CMakeLists.txt b/compiler-rt/lib/builtins/CMakeLists.txt
index 2c3b0fa84a4782..9a0a50ee7003f1 100644
--- a/compiler-rt/lib/builtins/CMakeLists.txt
+++ b/compiler-rt/lib/builtins/CMakeLists.txt
@@ -192,6 +192,7 @@ set(GENERIC_SOURCES
 set(BF16_SOURCES
     extendbfsf2.c
     truncdfbf2.c
+    truncxfbf2.c
     truncsfbf2.c
 )
 
diff --git a/compiler-rt/lib/builtins/truncxfbf2.c b/compiler-rt/lib/builtins/truncxfbf2.c
new file mode 100644
index 00000000000000..aa4108c334eebc
--- /dev/null
+++ b/compiler-rt/lib/builtins/truncxfbf2.c
@@ -0,0 +1,16 @@
+//===-- lib/truncxfbf2.c - long double -> bfloat conversion -------*- C -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#if defined(CRT_HAS_TF_MODE) && __LDBL_MANT_DIG__ == 64 && defined(__x86_64__)
+#define SRC_80
+#define DST_BFLOAT
+#include "fp_trunc_impl.inc"
+
+COMPILER_RT_ABI dst_t __truncxfbf2(long double a) { return __truncXfYf2__(a); }
+
+#endif
diff --git a/llvm/include/llvm/IR/RuntimeLibcalls.def b/llvm/include/llvm/IR/RuntimeLibcalls.def
index c3d5ef9f4e4f82..69cf43140ad4bd 100644
--- a/llvm/include/llvm/IR/RuntimeLibcalls.def
+++ b/llvm/include/llvm/IR/RuntimeLibcalls.def
@@ -367,6 +367,7 @@ HANDLE_LIBCALL(FPROUND_F128_F16, "__trunctfhf2")
 HANDLE_LIBCALL(FPROUND_PPCF128_F16, "__trunctfhf2")
 HANDLE_LIBCALL(FPROUND_F32_BF16, "__truncsfbf2")
 HANDLE_LIBCALL(FPROUND_F64_BF16, "__truncdfbf2")
+HANDLE_LIBCALL(FPROUND_F80_BF16, "__truncxfbf2")
 HANDLE_LIBCALL(FPROUND_F64_F32, "__truncdfsf2")
 HANDLE_LIBCALL(FPROUND_F80_F32, "__truncxfsf2")
 HANDLE_LIBCALL(FPROUND_F128_F32, "__trunctfsf2")
diff --git a/llvm/lib/CodeGen/TargetLoweringBase.cpp b/llvm/lib/CodeGen/TargetLoweringBase.cpp
index eb3190c7cd247a..9fdde454559171 100644
--- a/llvm/lib/CodeGen/TargetLoweringBase.cpp
+++ b/llvm/lib/CodeGen/TargetLoweringBase.cpp
@@ -169,6 +169,8 @@ RTLIB::Libcall RTLIB::getFPROUND(EVT OpVT, EVT RetVT) {
       return FPROUND_F32_BF16;
     if (OpVT == MVT::f64)
       return FPROUND_F64_BF16;
+    if (OpVT == MVT::f80)
+      return FPROUND_F80_BF16;
   } else if (RetVT == MVT::f32) {
     if (OpVT == MVT::f64)
       return FPROUND_F64_F32;
diff --git a/llvm/test/CodeGen/X86/bfloat.ll b/llvm/test/CodeGen/X86/bfloat.ll
index 3759909a2ccc8e..3144fd56d9ccf3 100644
--- a/llvm/test/CodeGen/X86/bfloat.ll
+++ b/llvm/test/CodeGen/X86/bfloat.ll
@@ -1953,3 +1953,25 @@ define void @PR92471(ptr %0, ptr %1) nounwind {
   store <7 x float> %4, ptr %1, align 4
   ret void
 }
+
+define bfloat @PR108936(x86_fp80 %0) nounwind {
+; X86-LABEL: PR108936:
+; X86:       # %bb.0:
+; X86-NEXT:    subl $12, %esp
+; X86-NEXT:    fldt {{[0-9]+}}(%esp)
+; X86-NEXT:    fstpt (%esp)
+; X86-NEXT:    calll __truncxfbf2
+; X86-NEXT:    addl $12, %esp
+; X86-NEXT:    retl
+;
+; CHECK-LABEL: PR108936:
+; CHECK:       # %bb.0:
+; CHECK-NEXT:    subq $24, %rsp
+; CHECK-NEXT:    fldt {{[0-9]+}}(%rsp)
+; CHECK-NEXT:    fstpt (%rsp)
+; CHECK-NEXT:    callq __truncxfbf2 at PLT
+; CHECK-NEXT:    addq $24, %rsp
+; CHECK-NEXT:    retq
+  %2 = fptrunc x86_fp80 %0 to bfloat
+  ret bfloat %2
+}

>From c8d6142b5145f289329e4987f5c8c1bd54ec452d Mon Sep 17 00:00:00 2001
From: "Wang, Phoebe" <phoebe.wang at intel.com>
Date: Thu, 19 Sep 2024 14:41:05 +0800
Subject: [PATCH 2/2] Fix buildbot failures

---
 compiler-rt/lib/builtins/fp_trunc.h | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/compiler-rt/lib/builtins/fp_trunc.h b/compiler-rt/lib/builtins/fp_trunc.h
index 141fe63e132d96..a1bd881eb57c9e 100644
--- a/compiler-rt/lib/builtins/fp_trunc.h
+++ b/compiler-rt/lib/builtins/fp_trunc.h
@@ -35,6 +35,18 @@ static const int srcSigFracBits = 52;
 // srcBits - srcSigFracBits - 1
 static const int srcExpBits = 11;
 
+#elif defined SRC_80
+typedef xf_float src_t;
+typedef __uint128_t src_rep_t;
+#define SRC_REP_C (__uint128_t)
+// sign bit, exponent and significand occupy the lower 80 bits.
+static const int srcBits = 80;
+static const int srcSigFracBits = 63;
+// -1 accounts for the sign bit.
+// -1 accounts for the explicitly stored integer bit.
+// srcBits - srcSigFracBits - 1 - 1
+static const int srcExpBits = 15;
+
 #elif defined SRC_QUAD
 typedef tf_float src_t;
 typedef __uint128_t src_rep_t;



More information about the llvm-commits mailing list