-
Notifications
You must be signed in to change notification settings - Fork 11.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ConstantFold] Fix result type when folding powi.f16 #98681
Conversation
@llvm/pr-subscribers-llvm-analysis Author: Yingwei Zheng (dtcxzyw) ChangesFixes #98665. Full diff: https://github.com/llvm/llvm-project/pull/98681.diff 2 Files Affected:
diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index 962880f68f076..b86f91edeb5b9 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 0000000000000..8b7d361840936
--- /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
+}
|
@llvm/pr-subscribers-llvm-transforms Author: Yingwei Zheng (dtcxzyw) ChangesFixes #98665. Full diff: https://github.com/llvm/llvm-project/pull/98681.diff 2 Files Affected:
diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index 962880f68f076..b86f91edeb5b9 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 0000000000000..8b7d361840936
--- /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
+}
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, though this code could really do with some cleanup. Why are there three different if
s checking for IntrinsicID == Intrinsic::powi
when there is a perfectly good switch
just before it?
dtcxzyw/llvm-opt-benchmark#961 |
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/51/builds/1315 Here is the relevant piece of the build log for the reference:
|
See #98860. |
Fixes #98665.