[InstCombine] Potential regression by blocking narrowing of double to float.
I noticed a performance regression in the linked benchmark when comparing an LLVM build from yesterday against the previous day's build: https://cc-perf.igalia.com/db_default/v4/nts/profile/260/402/4
The following screenshots show the basic block responsible for the cycle increase. I believe the regression stems from the compiler generating an fdiv.d operation where it previously generated an fdiv.s. Looking at the output of llvm-mca, I interpret this as a meaningful difference since fdiv.d has a latency of 33 whereas fdiv.s has a latency of 19.
LLVM (NEW)
LLVM (OLD)
I believe this is caused by this patch https://github.com/llvm/llvm-project/pull/190235.
For more context, this is also the relevant IR beginninng of the pipeline:
%conv = sitofp i64 %5 to float
%conv2 = fpext float %conv to double
%div3 = fdiv double %conv2, 7.438300e+04
%conv4 = fptrunc double %div3 to float
In the prior version, when visitFPExt called isKnownExactCastIntToFP, it returned false. This allowed visitFPTrunc to recognize the fptrunc(fdiv double (fpext(sitofp))) pattern and narrow the operation to a float.
Following the patch, isKnownExactCastIntToFP now returns true in visitFPExt. This allows the optimizer to simplify the fpext(sitofp) into a uitofp (with the nneg flag):
%conv2 = uitofp nneg i64 %0 to double
%div3 = fdiv double %conv2, 7.438300e+04
%conv4 = fptrunc double %div3 to float
However, because the fpext is gone, visitFPTrunc cannot reduce this into a float and use fdiv.s, despite the constants in the source code being able to fit within a float.
I do have a patch that addresses this, in which I modify getMinimumFPType called by visitFPTrunc to essentially check if a source of uoitfp can be converted into a float instead. Not exactly sure if getMinimumFPType is the most appropriate place to put this.
@SavchenkoValeriy, apologies if this ping is inappropriate. I suspect the improved logic in isKnownExactCastIntToFP might be causing this unintended effect? I'd appreciate your thoughts on whether my proposed fix to getMinimumFPType is the right approach, or if there's a better place to address this.
I see! Thanks for explaining the issue very clearly. I was thinking that we actually can reuse the function that caused the problems. It looks like the fptrunc fold can use this function but a variant where you can pass in a different type, ie float in this case. Maybe we can refactor it to have two variants one implemented using the other. My reasoning being that your suggested function will pretty much use the same logic.
Ah, I agree, your approach here would be better :sweat_smile:. Instead of a variant, could we instead modify the function signature in the header like so?
bool isKnownExactCastIntToFP(CastInst &I, Type *DestTy = nullptr) const;
This would allow us to refactor only the definition, and set FPTy to DestTy if defined, else keep it to I.getTye(). As you stated, we can then have visitFPTrunc call this with Ty passed in. All the other calls to this function don't have the second parameter so it'll use I.getType() as they currently do.
What about having a function like:
bool canBeCastedExactlyIntToFP(Value &V, Type *FP) const;
Because we actually don't even need to be a cast there, it's just a predicate to see if that int value can fit into a particular float.
And isKnownExactCastIntToFP will basically get a source of cast and the target of cast and pass it into it.
How does this sound?
Sounds good! I'll try giving it a whirl.