Skip to content

Commit c18be32

Browse files
authored
Reland "[X86][BF16] Add libcall for F80 -> BF16 (#109116)" (#109143)
This reverts commit ababfee78714313a0cad87591b819f0944b90d09. Add X86 FP80 check.
1 parent dc6876f commit c18be32

File tree

6 files changed

+54
-0
lines changed

6 files changed

+54
-0
lines changed

compiler-rt/lib/builtins/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ set(GENERIC_SOURCES
192192
set(BF16_SOURCES
193193
extendbfsf2.c
194194
truncdfbf2.c
195+
truncxfbf2.c
195196
truncsfbf2.c
196197
)
197198

compiler-rt/lib/builtins/fp_trunc.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@ static const int srcSigFracBits = 52;
3535
// srcBits - srcSigFracBits - 1
3636
static const int srcExpBits = 11;
3737

38+
#elif defined SRC_80
39+
typedef xf_float src_t;
40+
typedef __uint128_t src_rep_t;
41+
#define SRC_REP_C (__uint128_t)
42+
// sign bit, exponent and significand occupy the lower 80 bits.
43+
static const int srcBits = 80;
44+
static const int srcSigFracBits = 63;
45+
// -1 accounts for the sign bit.
46+
// -1 accounts for the explicitly stored integer bit.
47+
// srcBits - srcSigFracBits - 1 - 1
48+
static const int srcExpBits = 15;
49+
3850
#elif defined SRC_QUAD
3951
typedef tf_float src_t;
4052
typedef __uint128_t src_rep_t;

compiler-rt/lib/builtins/truncxfbf2.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//===-- lib/truncxfbf2.c - long double -> bfloat conversion -------*- C -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#if defined(CRT_HAS_TF_MODE) && __LDBL_MANT_DIG__ == 64 && defined(__x86_64__)
10+
#define SRC_80
11+
#define DST_BFLOAT
12+
#include "fp_trunc_impl.inc"
13+
14+
COMPILER_RT_ABI dst_t __truncxfbf2(long double a) { return __truncXfYf2__(a); }
15+
16+
#endif

llvm/include/llvm/IR/RuntimeLibcalls.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ HANDLE_LIBCALL(FPROUND_F128_F16, "__trunctfhf2")
367367
HANDLE_LIBCALL(FPROUND_PPCF128_F16, "__trunctfhf2")
368368
HANDLE_LIBCALL(FPROUND_F32_BF16, "__truncsfbf2")
369369
HANDLE_LIBCALL(FPROUND_F64_BF16, "__truncdfbf2")
370+
HANDLE_LIBCALL(FPROUND_F80_BF16, "__truncxfbf2")
370371
HANDLE_LIBCALL(FPROUND_F64_F32, "__truncdfsf2")
371372
HANDLE_LIBCALL(FPROUND_F80_F32, "__truncxfsf2")
372373
HANDLE_LIBCALL(FPROUND_F128_F32, "__trunctfsf2")

llvm/lib/CodeGen/TargetLoweringBase.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ RTLIB::Libcall RTLIB::getFPROUND(EVT OpVT, EVT RetVT) {
169169
return FPROUND_F32_BF16;
170170
if (OpVT == MVT::f64)
171171
return FPROUND_F64_BF16;
172+
if (OpVT == MVT::f80)
173+
return FPROUND_F80_BF16;
172174
} else if (RetVT == MVT::f32) {
173175
if (OpVT == MVT::f64)
174176
return FPROUND_F64_F32;

llvm/test/CodeGen/X86/bfloat.ll

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1953,3 +1953,25 @@ define void @PR92471(ptr %0, ptr %1) nounwind {
19531953
store <7 x float> %4, ptr %1, align 4
19541954
ret void
19551955
}
1956+
1957+
define bfloat @PR108936(x86_fp80 %0) nounwind {
1958+
; X86-LABEL: PR108936:
1959+
; X86: # %bb.0:
1960+
; X86-NEXT: subl $12, %esp
1961+
; X86-NEXT: fldt {{[0-9]+}}(%esp)
1962+
; X86-NEXT: fstpt (%esp)
1963+
; X86-NEXT: calll __truncxfbf2
1964+
; X86-NEXT: addl $12, %esp
1965+
; X86-NEXT: retl
1966+
;
1967+
; CHECK-LABEL: PR108936:
1968+
; CHECK: # %bb.0:
1969+
; CHECK-NEXT: subq $24, %rsp
1970+
; CHECK-NEXT: fldt {{[0-9]+}}(%rsp)
1971+
; CHECK-NEXT: fstpt (%rsp)
1972+
; CHECK-NEXT: callq __truncxfbf2@PLT
1973+
; CHECK-NEXT: addq $24, %rsp
1974+
; CHECK-NEXT: retq
1975+
%2 = fptrunc x86_fp80 %0 to bfloat
1976+
ret bfloat %2
1977+
}

0 commit comments

Comments
 (0)