Skip to content

Commit 08b20f2

Browse files
committed
[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.
1 parent 47bdea3 commit 08b20f2

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

llvm/lib/IR/ConstantFold.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -435,14 +435,8 @@ Constant *llvm::ConstantFoldCastInstruction(unsigned opc, Constant *V,
435435
if (ConstantFP *FPC = dyn_cast<ConstantFP>(V)) {
436436
bool ignored;
437437
APFloat Val = FPC->getValueAPF();
438-
Val.convert(DestTy->isHalfTy() ? APFloat::IEEEhalf() :
439-
DestTy->isFloatTy() ? APFloat::IEEEsingle() :
440-
DestTy->isDoubleTy() ? APFloat::IEEEdouble() :
441-
DestTy->isX86_FP80Ty() ? APFloat::x87DoubleExtended() :
442-
DestTy->isFP128Ty() ? APFloat::IEEEquad() :
443-
DestTy->isPPC_FP128Ty() ? APFloat::PPCDoubleDouble() :
444-
APFloat::Bogus(),
445-
APFloat::rmNearestTiesToEven, &ignored);
438+
Val.convert(DestTy->getFltSemantics(), APFloat::rmNearestTiesToEven,
439+
&ignored);
446440
return ConstantFP::get(V->getContext(), Val);
447441
}
448442
return nullptr; // Can't fold.

llvm/test/Transforms/InstSimplify/ConstProp/cast.ll

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,11 @@ define <3 x half> @nan_v3f64_trunc() {
6262
%f = fptrunc <3 x double> <double 0x7FF0020000000000, double 0x7FF003FFFFFFFFFF, double 0x7FF8000000000001> to <3 x half>
6363
ret <3 x half> %f
6464
}
65+
66+
define bfloat @nan_bf16_trunc() {
67+
; CHECK-LABEL: @nan_bf16_trunc(
68+
; CHECK-NEXT: ret bfloat 0xR7FC0
69+
;
70+
%f = fptrunc double 0x7FF0000000000001 to bfloat
71+
ret bfloat %f
72+
}

0 commit comments

Comments
 (0)