[llvm] 08b20f2 - [ConstantFold] Use getFltSemantics instead of manually checking the type

Benjamin Kramer via llvm-commits llvm-commits at lists.llvm.org
Thu May 5 06:54:06 PDT 2022


Author: Benjamin Kramer
Date: 2022-05-05T15:52:19+02:00
New Revision: 08b20f20d2854377009822dfe597c78a4bf18de8

URL: https://github.com/llvm/llvm-project/commit/08b20f20d2854377009822dfe597c78a4bf18de8
DIFF: https://github.com/llvm/llvm-project/commit/08b20f20d2854377009822dfe597c78a4bf18de8.diff

LOG: [ConstantFold] Use getFltSemantics instead of manually checking the type

Simplifies the code and makes fpext/fptrunc constant folding not crash
when the result is bf16.

Added: 
    

Modified: 
    llvm/lib/IR/ConstantFold.cpp
    llvm/test/Transforms/InstSimplify/ConstProp/cast.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/IR/ConstantFold.cpp b/llvm/lib/IR/ConstantFold.cpp
index 9cbbe2bf5df8c..b032a3fea98f2 100644
--- a/llvm/lib/IR/ConstantFold.cpp
+++ b/llvm/lib/IR/ConstantFold.cpp
@@ -435,14 +435,8 @@ Constant *llvm::ConstantFoldCastInstruction(unsigned opc, Constant *V,
     if (ConstantFP *FPC = dyn_cast<ConstantFP>(V)) {
       bool ignored;
       APFloat Val = FPC->getValueAPF();
-      Val.convert(DestTy->isHalfTy() ? APFloat::IEEEhalf() :
-                  DestTy->isFloatTy() ? APFloat::IEEEsingle() :
-                  DestTy->isDoubleTy() ? APFloat::IEEEdouble() :
-                  DestTy->isX86_FP80Ty() ? APFloat::x87DoubleExtended() :
-                  DestTy->isFP128Ty() ? APFloat::IEEEquad() :
-                  DestTy->isPPC_FP128Ty() ? APFloat::PPCDoubleDouble() :
-                  APFloat::Bogus(),
-                  APFloat::rmNearestTiesToEven, &ignored);
+      Val.convert(DestTy->getFltSemantics(), APFloat::rmNearestTiesToEven,
+                  &ignored);
       return ConstantFP::get(V->getContext(), Val);
     }
     return nullptr; // Can't fold.

diff  --git a/llvm/test/Transforms/InstSimplify/ConstProp/cast.ll b/llvm/test/Transforms/InstSimplify/ConstProp/cast.ll
index 8e9fd5fbae300..14eb29e636c6f 100644
--- a/llvm/test/Transforms/InstSimplify/ConstProp/cast.ll
+++ b/llvm/test/Transforms/InstSimplify/ConstProp/cast.ll
@@ -62,3 +62,11 @@ define <3 x half> @nan_v3f64_trunc() {
   %f = fptrunc <3 x double> <double 0x7FF0020000000000, double 0x7FF003FFFFFFFFFF, double 0x7FF8000000000001> to <3 x half>
   ret <3 x half> %f
 }
+
+define bfloat @nan_bf16_trunc() {
+; CHECK-LABEL: @nan_bf16_trunc(
+; CHECK-NEXT:    ret bfloat 0xR7FC0
+;
+  %f = fptrunc double 0x7FF0000000000001 to bfloat
+  ret bfloat %f
+}


        


More information about the llvm-commits mailing list