Skip to content

Commit cf7fe37

Browse files
tuStromaigcbot
authored andcommitted
Fix BFloat trunc in LLVM
Fix BFloat trunc by adding BFLoat to convert function in instruction casting
1 parent 0a29f25 commit cf7fe37

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*========================== begin_copyright_notice ============================
2+
3+
Copyright (C) 2024 Intel Corporation
4+
5+
SPDX-License-Identifier: MIT
6+
7+
============================= end_copyright_notice ===========================*/
8+
9+
/*========================== begin_copyright_notice ============================
10+
11+
Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
12+
See https://llvm.org/LICENSE.txt for license information.
13+
SPDX-License-Identifier: Apache-2.0 with LLVM-exception
14+
15+
============================= end_copyright_notice ===========================*/
16+
17+
From 08b20f20d2854377009822dfe597c78a4bf18de8 Mon Sep 17 00:00:00 2001
18+
From: Benjamin Kramer <[email protected]>
19+
Date: Thu, 5 May 2022 15:50:33 +0200
20+
Subject: [PATCH] [ConstantFold] Use getFltSemantics instead of manually
21+
checking the type
22+
23+
Simplifies the code and makes fpext/fptrunc constant folding not crash
24+
when the result is bf16.
25+
---
26+
llvm/lib/IR/ConstantFold.cpp | 10 ++--------
27+
llvm/test/Transforms/InstSimplify/ConstProp/cast.ll | 8 ++++++++
28+
2 files changed, 10 insertions(+), 8 deletions(-)
29+
30+
diff --git a/llvm/lib/IR/ConstantFold.cpp b/llvm/lib/IR/ConstantFold.cpp
31+
index 9cbbe2bf5df8..b032a3fea98f 100644
32+
--- a/llvm/lib/IR/ConstantFold.cpp
33+
+++ b/llvm/lib/IR/ConstantFold.cpp
34+
@@ -435,14 +435,8 @@ Constant *llvm::ConstantFoldCastInstruction(unsigned opc, Constant *V,
35+
if (ConstantFP *FPC = dyn_cast<ConstantFP>(V)) {
36+
bool ignored;
37+
APFloat Val = FPC->getValueAPF();
38+
- Val.convert(DestTy->isHalfTy() ? APFloat::IEEEhalf() :
39+
- DestTy->isFloatTy() ? APFloat::IEEEsingle() :
40+
- DestTy->isDoubleTy() ? APFloat::IEEEdouble() :
41+
- DestTy->isX86_FP80Ty() ? APFloat::x87DoubleExtended() :
42+
- DestTy->isFP128Ty() ? APFloat::IEEEquad() :
43+
- DestTy->isPPC_FP128Ty() ? APFloat::PPCDoubleDouble() :
44+
- APFloat::Bogus(),
45+
- APFloat::rmNearestTiesToEven, &ignored);
46+
+ Val.convert(DestTy->getFltSemantics(), APFloat::rmNearestTiesToEven,
47+
+ &ignored);
48+
return ConstantFP::get(V->getContext(), Val);
49+
}
50+
return nullptr; // Can't fold.
51+
diff --git a/llvm/test/Transforms/InstSimplify/ConstProp/cast.ll b/llvm/test/Transforms/InstSimplify/ConstProp/cast.ll
52+
index 8e9fd5fbae30..14eb29e636c6 100644
53+
--- a/llvm/test/Transforms/InstSimplify/ConstProp/cast.ll
54+
+++ b/llvm/test/Transforms/InstSimplify/ConstProp/cast.ll
55+
@@ -62,3 +62,11 @@ define <3 x half> @nan_v3f64_trunc() {
56+
%f = fptrunc <3 x double> <double 0x7FF0020000000000, double 0x7FF003FFFFFFFFFF, double 0x7FF8000000000001> to <3 x half>
57+
ret <3 x half> %f
58+
}
59+
+
60+
+define bfloat @nan_bf16_trunc() {
61+
+; CHECK-LABEL: @nan_bf16_trunc(
62+
+; CHECK-NEXT: ret bfloat 0xR7FC0
63+
+;
64+
+ %f = fptrunc double 0x7FF0000000000001 to bfloat
65+
+ ret bfloat %f
66+
+}
67+
--
68+
2.39.1.windows.1
69+

0 commit comments

Comments
 (0)