-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[AMDGPU] Simplify API of matchFPExtFromF16. NFC. #108223
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
Conversation
@llvm/pr-subscribers-backend-amdgpu Author: Jay Foad (jayfoad) ChangesFull diff: https://github.com/llvm/llvm-project/pull/108223.diff 1 Files Affected:
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUInstCombineIntrinsic.cpp b/llvm/lib/Target/AMDGPU/AMDGPUInstCombineIntrinsic.cpp
index 4da3618357c420..a2135fbc254c11 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUInstCombineIntrinsic.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUInstCombineIntrinsic.cpp
@@ -353,23 +353,20 @@ bool GCNTTIImpl::canSimplifyLegacyMulToMul(const Instruction &I,
}
/// Match an fpext from half to float, or a constant we can convert.
-static bool matchFPExtFromF16(Value *Arg, Value *&FPExtSrc) {
- if (match(Arg, m_OneUse(m_FPExt(m_Value(FPExtSrc)))))
- return FPExtSrc->getType()->isHalfTy();
-
+static std::optional<Value *> matchFPExtFromF16(Value *Arg) {
+ Value *Src;
ConstantFP *CFP;
- if (match(Arg, m_ConstantFP(CFP))) {
+ if (match(Arg, m_OneUse(m_FPExt(m_Value(Src))))) {
+ if (Src->getType()->isHalfTy())
+ return Src;
+ } else if (match(Arg, m_ConstantFP(CFP))) {
bool LosesInfo;
APFloat Val(CFP->getValueAPF());
Val.convert(APFloat::IEEEhalf(), APFloat::rmNearestTiesToEven, &LosesInfo);
- if (LosesInfo)
- return false;
-
- FPExtSrc = ConstantFP::get(Type::getHalfTy(Arg->getContext()), Val);
- return true;
+ if (!LosesInfo)
+ return ConstantFP::get(Type::getHalfTy(Arg->getContext()), Val);
}
-
- return false;
+ return {};
}
// Trim all zero components from the end of the vector \p UseV and return
@@ -839,15 +836,16 @@ GCNTTIImpl::instCombineIntrinsic(InstCombiner &IC, IntrinsicInst &II) const {
if (!ST->hasMed3_16())
break;
- Value *X, *Y, *Z;
-
// Repeat floating-point width reduction done for minnum/maxnum.
// fmed3((fpext X), (fpext Y), (fpext Z)) -> fpext (fmed3(X, Y, Z))
- if (matchFPExtFromF16(Src0, X) && matchFPExtFromF16(Src1, Y) &&
- matchFPExtFromF16(Src2, Z)) {
- Value *NewCall = IC.Builder.CreateIntrinsic(IID, {X->getType()},
- {X, Y, Z}, &II, II.getName());
- return new FPExtInst(NewCall, II.getType());
+ if (auto X = matchFPExtFromF16(Src0)) {
+ if (auto Y = matchFPExtFromF16(Src1)) {
+ if (auto Z = matchFPExtFromF16(Src2)) {
+ Value *NewCall = IC.Builder.CreateIntrinsic(
+ IID, {(*X)->getType()}, {*X, *Y, *Z}, &II, II.getName());
+ return new FPExtInst(NewCall, II.getType());
+ }
+ }
}
break;
|
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.
Not sure what the real benefit is but it's fine.
} | ||
|
||
return false; | ||
return {}; |
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.
return {}; | |
return std::nullopt; |
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.
Done
return FPExtSrc->getType()->isHalfTy(); | ||
|
||
static std::optional<Value *> matchFPExtFromF16(Value *Arg) { | ||
Value *Src; |
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.
I understand here you just follow existing code, but I'd just initialize them to nullptr
.
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.
Done
I find it much cleaner to return things via the return value of a function, instead of modifying one of its arguments. |
I'd also be happy with returning |
Yeah, it seems like the boolean return value has the same semantics as |
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. Thanks.
No description provided.