Skip to content

Commit 34bfed6

Browse files
authored
[ConstantFold] Fix result type when folding powi.f16 (#98681)
Fixes #98665.
1 parent 662c6fc commit 34bfed6

File tree

2 files changed

+29
-18
lines changed

2 files changed

+29
-18
lines changed

llvm/lib/Analysis/ConstantFolding.cpp

+19-18
Original file line numberDiff line numberDiff line change
@@ -2754,27 +2754,28 @@ static Constant *ConstantFoldIntrinsicCall2(Intrinsic::ID IntrinsicID, Type *Ty,
27542754
((Mask & fcPosInf) && Op1V.isPosInfinity());
27552755
return ConstantInt::get(Ty, Result);
27562756
}
2757+
case Intrinsic::powi: {
2758+
int Exp = static_cast<int>(Op2C->getSExtValue());
2759+
switch (Ty->getTypeID()) {
2760+
case Type::HalfTyID:
2761+
case Type::FloatTyID: {
2762+
APFloat Res(std::pow(Op1V.convertToFloat(), Exp));
2763+
if (Ty->isHalfTy()) {
2764+
bool Unused;
2765+
Res.convert(APFloat::IEEEhalf(), APFloat::rmNearestTiesToEven,
2766+
&Unused);
2767+
}
2768+
return ConstantFP::get(Ty->getContext(), Res);
2769+
}
2770+
case Type::DoubleTyID:
2771+
return ConstantFP::get(Ty, std::pow(Op1V.convertToDouble(), Exp));
2772+
default:
2773+
return nullptr;
2774+
}
2775+
}
27572776
default:
27582777
break;
27592778
}
2760-
2761-
if (!Ty->isHalfTy() && !Ty->isFloatTy() && !Ty->isDoubleTy())
2762-
return nullptr;
2763-
if (IntrinsicID == Intrinsic::powi && Ty->isHalfTy())
2764-
return ConstantFP::get(
2765-
Ty->getContext(),
2766-
APFloat((float)std::pow((float)Op1V.convertToDouble(),
2767-
(int)Op2C->getZExtValue())));
2768-
if (IntrinsicID == Intrinsic::powi && Ty->isFloatTy())
2769-
return ConstantFP::get(
2770-
Ty->getContext(),
2771-
APFloat((float)std::pow((float)Op1V.convertToDouble(),
2772-
(int)Op2C->getZExtValue())));
2773-
if (IntrinsicID == Intrinsic::powi && Ty->isDoubleTy())
2774-
return ConstantFP::get(
2775-
Ty->getContext(),
2776-
APFloat((double)std::pow(Op1V.convertToDouble(),
2777-
(int)Op2C->getZExtValue())));
27782779
}
27792780
return nullptr;
27802781
}

llvm/test/Transforms/EarlyCSE/math-2.ll

+10
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,14 @@ define double @i_powi() {
9898
ret double %res
9999
}
100100

101+
; Make sure that the type is correct after constant folding
102+
103+
define half @pr98665() {
104+
; CHECK-LABEL: @pr98665(
105+
; CHECK-NEXT: ret half 0xH3C00
106+
;
107+
%x = call half @llvm.powi.f16.i32(half 0xH3C00, i32 1)
108+
ret half %x
109+
}
110+
101111
attributes #0 = { nofree nounwind willreturn }

0 commit comments

Comments
 (0)