Skip to content
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

Merged
merged 4 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 19 additions & 18 deletions llvm/lib/Analysis/ConstantFolding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2754,27 +2754,28 @@ static Constant *ConstantFoldIntrinsicCall2(Intrinsic::ID IntrinsicID, Type *Ty,
((Mask & fcPosInf) && Op1V.isPosInfinity());
return ConstantInt::get(Ty, Result);
}
case Intrinsic::powi: {
int exp = static_cast<int>(Op2C->getZExtValue());
dtcxzyw marked this conversation as resolved.
Show resolved Hide resolved
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, 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())
return ConstantFP::get(
Ty->getContext(),
APFloat((float)std::pow((float)Op1V.convertToDouble(),
(int)Op2C->getZExtValue())));
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;
}
Expand Down
10 changes: 10 additions & 0 deletions llvm/test/Transforms/EarlyCSE/math-2.ll
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Loading