From a0097cbfd275d08831ee02b354fce7011d57eb19 Mon Sep 17 00:00:00 2001 From: Yingwei Zheng Date: Sat, 13 Jul 2024 03:05:37 +0800 Subject: [PATCH 1/4] [ConstantFold] Fix result type when folding powi.f16 --- llvm/lib/Analysis/ConstantFolding.cpp | 12 +++++++----- .../Transforms/InstSimplify/ConstProp/pr98665.ll | 11 +++++++++++ 2 files changed, 18 insertions(+), 5 deletions(-) create mode 100644 llvm/test/Transforms/InstSimplify/ConstProp/pr98665.ll diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp index 962880f68f0767..b86f91edeb5b92 100644 --- a/llvm/lib/Analysis/ConstantFolding.cpp +++ b/llvm/lib/Analysis/ConstantFolding.cpp @@ -2760,11 +2760,13 @@ static Constant *ConstantFoldIntrinsicCall2(Intrinsic::ID IntrinsicID, Type *Ty, if (!Ty->isHalfTy() && !Ty->isFloatTy() && !Ty->isDoubleTy()) return nullptr; - if (IntrinsicID == Intrinsic::powi && Ty->isHalfTy()) - return ConstantFP::get( - Ty->getContext(), - APFloat((float)std::pow((float)Op1V.convertToDouble(), - (int)Op2C->getZExtValue()))); + if (IntrinsicID == Intrinsic::powi && Ty->isHalfTy()) { + APFloat Res((float)std::pow((float)Op1V.convertToDouble(), + (int)Op2C->getZExtValue())); + bool unused; + Res.convert(APFloat::IEEEhalf(), APFloat::rmNearestTiesToEven, &unused); + return ConstantFP::get(Ty->getContext(), Res); + } if (IntrinsicID == Intrinsic::powi && Ty->isFloatTy()) return ConstantFP::get( Ty->getContext(), diff --git a/llvm/test/Transforms/InstSimplify/ConstProp/pr98665.ll b/llvm/test/Transforms/InstSimplify/ConstProp/pr98665.ll new file mode 100644 index 00000000000000..8b7d3618409360 --- /dev/null +++ b/llvm/test/Transforms/InstSimplify/ConstProp/pr98665.ll @@ -0,0 +1,11 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5 +; RUN: opt < %s -passes=instsimplify -S | FileCheck %s +; Make sure that the type is correct after constant folding + +define half @pr98665() { +; CHECK-LABEL: define half @pr98665() { +; CHECK-NEXT: ret half 0xH3C00 +; + %x = call half @llvm.powi.f16.i32(half 0xH3C00, i32 1) + ret half %x +} From b0a1393e34290aa1b4db61c5c842306ab62ec231 Mon Sep 17 00:00:00 2001 From: Yingwei Zheng Date: Sat, 13 Jul 2024 04:23:07 +0800 Subject: [PATCH 2/4] [ConstandFold] Address review comments. NFC. --- llvm/lib/Analysis/ConstantFolding.cpp | 40 +++++++++---------- llvm/test/Transforms/EarlyCSE/math-2.ll | 10 +++++ .../InstSimplify/ConstProp/pr98665.ll | 11 ----- 3 files changed, 30 insertions(+), 31 deletions(-) delete mode 100644 llvm/test/Transforms/InstSimplify/ConstProp/pr98665.ll diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp index b86f91edeb5b92..73278ea6b2a977 100644 --- a/llvm/lib/Analysis/ConstantFolding.cpp +++ b/llvm/lib/Analysis/ConstantFolding.cpp @@ -2754,29 +2754,29 @@ static Constant *ConstantFoldIntrinsicCall2(Intrinsic::ID IntrinsicID, Type *Ty, ((Mask & fcPosInf) && Op1V.isPosInfinity()); return ConstantInt::get(Ty, Result); } + case Intrinsic::powi: { + int exp = static_cast(Op2C->getZExtValue()); + switch (Ty->getTypeID()) { + case Type::HalfTyID: + case Type::FloatTyID: { + APFloat Res(std::pow(Op1V.convertToFloat(), exp)); + if (Ty->isHalfTy()) { + bool Unused; + Res.convert(APFloat::IEEEhalf(), APFloat::rmNearestTiesToEven, + &Unused); + } + return ConstantFP::get(Ty->getContext(), Res); + } + case Type::DoubleTyID: + return ConstantFP::get( + Ty->getContext(), APFloat(std::pow(Op1V.convertToDouble(), exp))); + default: + return nullptr; + } + } default: break; } - - if (!Ty->isHalfTy() && !Ty->isFloatTy() && !Ty->isDoubleTy()) - return nullptr; - if (IntrinsicID == Intrinsic::powi && Ty->isHalfTy()) { - APFloat Res((float)std::pow((float)Op1V.convertToDouble(), - (int)Op2C->getZExtValue())); - bool unused; - Res.convert(APFloat::IEEEhalf(), APFloat::rmNearestTiesToEven, &unused); - return ConstantFP::get(Ty->getContext(), Res); - } - if (IntrinsicID == Intrinsic::powi && Ty->isFloatTy()) - return ConstantFP::get( - Ty->getContext(), - APFloat((float)std::pow((float)Op1V.convertToDouble(), - (int)Op2C->getZExtValue()))); - if (IntrinsicID == Intrinsic::powi && Ty->isDoubleTy()) - return ConstantFP::get( - Ty->getContext(), - APFloat((double)std::pow(Op1V.convertToDouble(), - (int)Op2C->getZExtValue()))); } return nullptr; } diff --git a/llvm/test/Transforms/EarlyCSE/math-2.ll b/llvm/test/Transforms/EarlyCSE/math-2.ll index d9f7c619fa0137..60a2f19084c836 100644 --- a/llvm/test/Transforms/EarlyCSE/math-2.ll +++ b/llvm/test/Transforms/EarlyCSE/math-2.ll @@ -98,4 +98,14 @@ define double @i_powi() { ret double %res } +; Make sure that the type is correct after constant folding + +define half @pr98665() { +; CHECK-LABEL: @pr98665( +; CHECK-NEXT: ret half 0xH3C00 +; + %x = call half @llvm.powi.f16.i32(half 0xH3C00, i32 1) + ret half %x +} + attributes #0 = { nofree nounwind willreturn } diff --git a/llvm/test/Transforms/InstSimplify/ConstProp/pr98665.ll b/llvm/test/Transforms/InstSimplify/ConstProp/pr98665.ll deleted file mode 100644 index 8b7d3618409360..00000000000000 --- a/llvm/test/Transforms/InstSimplify/ConstProp/pr98665.ll +++ /dev/null @@ -1,11 +0,0 @@ -; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5 -; RUN: opt < %s -passes=instsimplify -S | FileCheck %s -; Make sure that the type is correct after constant folding - -define half @pr98665() { -; CHECK-LABEL: define half @pr98665() { -; CHECK-NEXT: ret half 0xH3C00 -; - %x = call half @llvm.powi.f16.i32(half 0xH3C00, i32 1) - ret half %x -} From 00fce823e5d7e1b1ecd5d9f1697f83aecca8c1d5 Mon Sep 17 00:00:00 2001 From: Yingwei Zheng Date: Sat, 13 Jul 2024 04:25:16 +0800 Subject: [PATCH 3/4] [ConstantFold] Use `ConstantFP::get(Ty, double)` --- llvm/lib/Analysis/ConstantFolding.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp index 73278ea6b2a977..b8b82d35780588 100644 --- a/llvm/lib/Analysis/ConstantFolding.cpp +++ b/llvm/lib/Analysis/ConstantFolding.cpp @@ -2768,8 +2768,7 @@ static Constant *ConstantFoldIntrinsicCall2(Intrinsic::ID IntrinsicID, Type *Ty, return ConstantFP::get(Ty->getContext(), Res); } case Type::DoubleTyID: - return ConstantFP::get( - Ty->getContext(), APFloat(std::pow(Op1V.convertToDouble(), exp))); + return ConstantFP::get(Ty, std::pow(Op1V.convertToDouble(), exp)); default: return nullptr; } From 0d11d5ae27f8aea340be96240271eb53ecb21d0a Mon Sep 17 00:00:00 2001 From: Yingwei Zheng Date: Sat, 13 Jul 2024 16:29:52 +0800 Subject: [PATCH 4/4] [ConstantFold] Address review comments. --- llvm/lib/Analysis/ConstantFolding.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp index b8b82d35780588..6c52091cd5d758 100644 --- a/llvm/lib/Analysis/ConstantFolding.cpp +++ b/llvm/lib/Analysis/ConstantFolding.cpp @@ -2755,11 +2755,11 @@ static Constant *ConstantFoldIntrinsicCall2(Intrinsic::ID IntrinsicID, Type *Ty, return ConstantInt::get(Ty, Result); } case Intrinsic::powi: { - int exp = static_cast(Op2C->getZExtValue()); + int Exp = static_cast(Op2C->getSExtValue()); switch (Ty->getTypeID()) { case Type::HalfTyID: case Type::FloatTyID: { - APFloat Res(std::pow(Op1V.convertToFloat(), exp)); + APFloat Res(std::pow(Op1V.convertToFloat(), Exp)); if (Ty->isHalfTy()) { bool Unused; Res.convert(APFloat::IEEEhalf(), APFloat::rmNearestTiesToEven, @@ -2768,7 +2768,7 @@ static Constant *ConstantFoldIntrinsicCall2(Intrinsic::ID IntrinsicID, Type *Ty, return ConstantFP::get(Ty->getContext(), Res); } case Type::DoubleTyID: - return ConstantFP::get(Ty, std::pow(Op1V.convertToDouble(), exp)); + return ConstantFP::get(Ty, std::pow(Op1V.convertToDouble(), Exp)); default: return nullptr; }