Skip to content

Commit 4e936bb

Browse files
[LLVM][InstCombine] Preserve vector types when shrinking FP constants.
While my original objective was to make the shrinkfp path safe for ConstantFP based splats I discovered the following issues for ConstantVector based splats: 1. PreferBFloat is not set for bfloat vectors. 2. getMinimumFPType() returns a scalar type for vector constants where getSplatValue() is successful. Please let me know if you rather me upstream those fixes seperate from the use-constant-fp-for-fixed-length-splat support.
1 parent 7275256 commit 4e936bb

File tree

2 files changed

+54
-22
lines changed

2 files changed

+54
-22
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1643,33 +1643,43 @@ Instruction *InstCombinerImpl::visitSExt(SExtInst &Sext) {
16431643

16441644
/// Return a Constant* for the specified floating-point constant if it fits
16451645
/// in the specified FP type without changing its value.
1646-
static bool fitsInFPType(ConstantFP *CFP, const fltSemantics &Sem) {
1646+
static bool fitsInFPType(APFloat F, const fltSemantics &Sem) {
16471647
bool losesInfo;
1648-
APFloat F = CFP->getValueAPF();
16491648
(void)F.convert(Sem, APFloat::rmNearestTiesToEven, &losesInfo);
16501649
return !losesInfo;
16511650
}
16521651

1653-
static Type *shrinkFPConstant(ConstantFP *CFP, bool PreferBFloat) {
1654-
if (CFP->getType() == Type::getPPC_FP128Ty(CFP->getContext()))
1655-
return nullptr; // No constant folding of this.
1652+
static Type *shrinkFPConstant(LLVMContext &Ctx, APFloat F, bool PreferBFloat) {
16561653
// See if the value can be truncated to bfloat and then reextended.
1657-
if (PreferBFloat && fitsInFPType(CFP, APFloat::BFloat()))
1658-
return Type::getBFloatTy(CFP->getContext());
1654+
if (PreferBFloat && fitsInFPType(F, APFloat::BFloat()))
1655+
return Type::getBFloatTy(Ctx);
16591656
// See if the value can be truncated to half and then reextended.
1660-
if (!PreferBFloat && fitsInFPType(CFP, APFloat::IEEEhalf()))
1661-
return Type::getHalfTy(CFP->getContext());
1657+
if (!PreferBFloat && fitsInFPType(F, APFloat::IEEEhalf()))
1658+
return Type::getHalfTy(Ctx);
16621659
// See if the value can be truncated to float and then reextended.
1663-
if (fitsInFPType(CFP, APFloat::IEEEsingle()))
1664-
return Type::getFloatTy(CFP->getContext());
1665-
if (CFP->getType()->isDoubleTy())
1666-
return nullptr; // Won't shrink.
1667-
if (fitsInFPType(CFP, APFloat::IEEEdouble()))
1668-
return Type::getDoubleTy(CFP->getContext());
1669-
// Don't try to shrink to various long double types.
1660+
if (fitsInFPType(F, APFloat::IEEEsingle()))
1661+
return Type::getFloatTy(Ctx);
1662+
// See if the value can be truncated to double and then reextended.
1663+
if (fitsInFPType(F, APFloat::IEEEdouble()))
1664+
return Type::getDoubleTy(Ctx);
1665+
// Does not shrink.
16701666
return nullptr;
16711667
}
16721668

1669+
static Type *shrinkFPConstant(ConstantFP *CFP, bool PreferBFloat) {
1670+
Type *Ty = CFP->getType();
1671+
if (Ty->getScalarType() == Type::getPPC_FP128Ty(CFP->getContext()))
1672+
return nullptr; // No constant folding of this.
1673+
1674+
Type *ShrinkTy =
1675+
shrinkFPConstant(CFP->getContext(), CFP->getValueAPF(), PreferBFloat);
1676+
if (auto *VecTy = dyn_cast<VectorType>(Ty))
1677+
ShrinkTy = VectorType::get(ShrinkTy, VecTy);
1678+
1679+
// Does it shrink?
1680+
return ShrinkTy != Ty ? ShrinkTy : nullptr;
1681+
}
1682+
16731683
// Determine if this is a vector of ConstantFPs and if so, return the minimal
16741684
// type we can safely truncate all elements to.
16751685
static Type *shrinkFPConstantVector(Value *V, bool PreferBFloat) {
@@ -1720,10 +1730,10 @@ static Type *getMinimumFPType(Value *V, bool PreferBFloat) {
17201730

17211731
// Try to shrink scalable and fixed splat vectors.
17221732
if (auto *FPC = dyn_cast<Constant>(V))
1723-
if (isa<VectorType>(V->getType()))
1733+
if (auto *VTy = dyn_cast<VectorType>(V->getType()))
17241734
if (auto *Splat = dyn_cast_or_null<ConstantFP>(FPC->getSplatValue()))
17251735
if (Type *T = shrinkFPConstant(Splat, PreferBFloat))
1726-
return T;
1736+
return VectorType::get(T, VTy);
17271737

17281738
// Try to shrink a vector of FP constants. This returns nullptr on scalable
17291739
// vectors
@@ -1796,10 +1806,9 @@ Instruction *InstCombinerImpl::visitFPTrunc(FPTruncInst &FPT) {
17961806
Type *Ty = FPT.getType();
17971807
auto *BO = dyn_cast<BinaryOperator>(FPT.getOperand(0));
17981808
if (BO && BO->hasOneUse()) {
1799-
Type *LHSMinType =
1800-
getMinimumFPType(BO->getOperand(0), /*PreferBFloat=*/Ty->isBFloatTy());
1801-
Type *RHSMinType =
1802-
getMinimumFPType(BO->getOperand(1), /*PreferBFloat=*/Ty->isBFloatTy());
1809+
bool PreferBFloat = Ty->getScalarType()->isBFloatTy();
1810+
Type *LHSMinType = getMinimumFPType(BO->getOperand(0), PreferBFloat);
1811+
Type *RHSMinType = getMinimumFPType(BO->getOperand(1), PreferBFloat);
18031812
unsigned OpWidth = BO->getType()->getFPMantissaWidth();
18041813
unsigned LHSWidth = LHSMinType->getFPMantissaWidth();
18051814
unsigned RHSWidth = RHSMinType->getFPMantissaWidth();

llvm/test/Transforms/InstCombine/fpextend.ll

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
22
; RUN: opt < %s -passes=instcombine -S | FileCheck %s
3+
; RUN: opt < %s -passes=instcombine --use-constant-fp-for-fixed-length-splat -S | FileCheck %s
34

45
define float @test(float %x) nounwind {
56
; CHECK-LABEL: @test(
@@ -449,6 +450,28 @@ define bfloat @bf16_frem(bfloat %x) {
449450
ret bfloat %t3
450451
}
451452

453+
define <4 x bfloat> @v4bf16_frem_x_const(<4 x bfloat> %x) {
454+
; CHECK-LABEL: @v4bf16_frem_x_const(
455+
; CHECK-NEXT: [[TMP1:%.*]] = frem <4 x bfloat> [[X:%.*]], splat (bfloat 0xR40C9)
456+
; CHECK-NEXT: ret <4 x bfloat> [[TMP1]]
457+
;
458+
%t1 = fpext <4 x bfloat> %x to <4 x float>
459+
%t2 = frem <4 x float> %t1, splat(float 6.281250e+00)
460+
%t3 = fptrunc <4 x float> %t2 to <4 x bfloat>
461+
ret <4 x bfloat> %t3
462+
}
463+
464+
define <4 x bfloat> @v4bf16_frem_const_x(<4 x bfloat> %x) {
465+
; CHECK-LABEL: @v4bf16_frem_const_x(
466+
; CHECK-NEXT: [[TMP1:%.*]] = frem <4 x bfloat> splat (bfloat 0xR40C9), [[X:%.*]]
467+
; CHECK-NEXT: ret <4 x bfloat> [[TMP1]]
468+
;
469+
%t1 = fpext <4 x bfloat> %x to <4 x float>
470+
%t2 = frem <4 x float> splat(float 6.281250e+00), %t1
471+
%t3 = fptrunc <4 x float> %t2 to <4 x bfloat>
472+
ret <4 x bfloat> %t3
473+
}
474+
452475
define <4 x float> @v4f32_fadd(<4 x float> %a) {
453476
; CHECK-LABEL: @v4f32_fadd(
454477
; CHECK-NEXT: [[TMP1:%.*]] = fadd <4 x float> [[A:%.*]], splat (float -1.000000e+00)

0 commit comments

Comments
 (0)