[RFC] [AMDGPU] [SelectionDAG] [GlobalIsel] select with constant combine into binaryOp with zext/sext
In both the ISEL under generic combines, various select with constants combine into binary ops with zext/sext operand like
select Cond, C1, C1-1 --> add (zext Cond), (C1-1)
select Cond, Pow2, 0 --> shl (zext Cond), log2(Pow2)
select Cond, C1, C1+1 --> add (sext Cond), (C1+1)
For various architecture, instruction materialization for zext/sext might be cheaper as compared to select, thus making sense for above combine optimization.
But in case of AMDGPU, both the zext/sext & select (for f32 with inline constants) materializes into v_cndmask_b32_e64. Thus the above optimization increases the cost by introducing an additional binary instruction.
If you look from different persepective, as in AMDGPU both the Zext/Sext and Select boils down to same machine instruction canonincally, thus really undoing the folding of binOp into Select. For example :
Select Cond, 7, 6 --> add ( zext Cond ), 6 materializes as :
v_cndmask_b32_e64 v0, 0, 1, vcc
v_add_u32_e32 v0, 6, v0
instead of
v_cndmask_b32_e64 v0, 6, 7, vcc
on which the binOp into Select combine is really missed, as Select is eliminated, but nevertheless (Zext cond) materializes as same as (Select cond 1, 0). So for AMDGPU : add ( zext Cond ), 6 <==> add ( Select 1, 0 ), 6 after the instruction selection is done. This really showcases that zext introduction (via select's combine) really caused the skip of BinOp fold into select, introducing the additional binary instruction.
It is the root cause of SWDEV-505394, as increases the code length.
@llvm/issue-subscribers-backend-amdgpu
Author: Vikash Gupta (vg0204)
select Cond, C1, C1-1 --> add (zext Cond), (C1-1)
select Cond, Pow2, 0 --> shl (zext Cond), log2(Pow2)
select Cond, C1, C1+1 --> add (sext Cond), (C1+1)
For various architecture, instruction materialization for zext/sext might be cheaper as compared to select, thus making sense for above combine optimization.
But in case of AMDGPU, both the zext/sext & select (for f32 with inline constants) materializes into v_cndmask_b32_e64. Thus the above optimization increases the cost by introducing an additional binary instruction.
If you look from different persepective, as in AMDGPU both the Zext/Sext and Select boils down to same machine instruction canonincally, thus really undoing the folding of binOp into Select. For example :
Select Cond, 7, 6 --> add ( zext Cond ), 6 materializes as :
v_cndmask_b32_e64 v0, 0, 1, vcc
v_add_u32_e32 v0, 6, v0
on which the binOp into Select combine is really missed, as Select is eliminated, but nevertheless (Zext cond)
materializes as same as (Select cond 1, 0). So for AMDGPU : add ( zext Cond ), 6 <==> add ( Select 1, 0 ), 6 after the instruction selection is done. This really showcases that zext introduction (via select's combine) really caused the skip of BinOp fold into select, introducing the additional binary instruction.
It is the root cause of SWDEV-505394, by increasing the code length.
Suggested Solutions :
Solution-0 : A target-specific combine to fold BinOp into Zext(in form of Select). This should be invoked after the completion of generic combines postDAGLegalization.
BinOp (zext (Cond), const0) -> select Cond, 1<BinOp>const0, 0<BinOp>const0
BinOp (const0, zext (Cond)) -> select Cond, const0<BinOp>1, const0<BinOp>0
Solution-1 : A target-specific hook which prevents the combine of select with constants which give rise to binOp with zext/sext operand for AMDGPU, really eliminating the problem from source. But, have shaky thoughts on its implementation in a cleaner way without affecting the other target's pipeline.
CC : @arsenm, @cdevadas , @aemerson , @jayfoad
In globalisel, all combines are explicitly opt-in. This doesn't need a new hook, and is already a generic combine in the DAG. We can just add it to the default sets
In globalisel, all combines are explicitly opt-in. This doesn't need a new hook, and is already a generic combine in the DAG. We can just add it to the default sets
Can you say specifically, what are you referring to be already in generic combine in the DAG; and can be just added to default set? Do you referring to an already exiting combine OR talking about the one I mentioned in the Solution-1 ?
Ping @arsenm !
Gentle Ping @arsenm , @jayfoad , @aemerson , @cdevadas
Can you say specifically, what are you referring to be already in generic combine in the DAG; and can be just added to default set? Do you referring to an already exiting combine OR talking about the one I mentioned in the Solution-1 ?
Add it as a generic combine and you shouldn't really need to think about any new target controls. It's already a generic DAG combine, you don't need anything new
It's already a generic DAG combine, you don't need anything new
Did you mean this combine already exists somewhere ;add ( zext Cond ), 6 <==> add ( Select 1, 0 ), 6; or should I write it and add it as a generic dag combine (not thinking of other target controls).
And a follow-up question if I write it as new combine, won't it be oscillating combine between push BinOp throgh Select & push binOp through Zext via Select? It might be a naive question but how combiner handles this scenario optimally?
The fold bin-op into select already exists generically, I'm less sure about the patterns involving the extensions. Do you have concrete IR examples? The basic IR pattern already folds, so this pattern would need to appear during legalization: https://godbolt.org/z/bPv4ME7Kb
The fold bin-op into select already exists generically, I'm less sure about the patterns involving the extensions. Do you have concrete IR examples? The basic IR pattern already folds, so this pattern would need to appear during legalization: https://godbolt.org/z/bPv4ME7Kb
This is IR example https://godbolt.org/z/b465rTf8Y, show what suggested!
After, digging bit more into it, found that foldSelectOfConstants function within generic combine in DAGCombiner is really not folding select into into maths/BinOp with zext (as it happens for only targets where this is beneficial, & AMDGPU is not one of them).
This is IR example https://godbolt.org/z/b465rTf8Y, show what suggested!
So, the IR folding happening above caused is just a specific case of folding fold select C, Pow2, 0 -> shl C, log2(Pow2) , where C is really a comparative cond (as can be understood by comapring 2nd and 3rd example). It is happening while handling SELECT_CC within SimplifySelectCC funtion.
So, now I think the possible solutions might include either writing a target-specific combine to undo it specifically or somehow prevent it to happen for AMDGPU at all (in SimplifySelectCC) via some target hook such as maybe shouldConvertSelectOfConstantsToMath or shouldAvoidTransformToShift or some new hook along the same line
FYI (in case of GlobalIsel) : As unlike the DAGCombiner (which uses shouldConvertSelectOfConstantsToMath to bypass generic SelectWithConstant combines where its not beneficial as for example in AMDGPU), global Isel don't use any such hooks yet, implying the issue addressessed in this ticket makes completely sense for globalIsel for all BinOps, not only shl (you can see the same on your own examples with globalIsel flag : https://godbolt.org/z/bPv4ME7Kb).
So, we need to think for that as well !!!
PS : Also how to handle SELECT_CC combine of type selectCC C, Pow2, 0 -> shl C, log2(Pow2) in global Isel, based on what decided in case of DAGCombiner.
Just found out that G_SELECT is just mapped from select & vselect. So, I think we need to port select_cc to GIsel in order to achieve this @arsenm
Gentle Ping @arsenm , if you could let me know what to proceed on immediately, as the major issue to be solved related to JIRA ticket(in desc) mentioned here is
So, the IR folding happening above caused is just a specific case of folding fold select C, Pow2, 0 -> shl C, log2(Pow2) , where C is really a comparative cond (as can be understood by comapring 2nd and 3rd example). It is happening while handling SELECT_CC within SimplifySelectCC funtion.
As for the globalIsel version, is it required now or not? As it might need port for SELECT_CC, followed by its handling the related combine described above !
A gentle ping @arsenm
Ping @pravinjagtap , @cdevadas !
As mentioned before mentioned unconditional dag combine [ fold select C, Pow2, 0 -> shl C, log2(Pow2) ] in SimplifySetCC() helper function is not beneficial for AMDGPU in general, in case the value Pow2 is inline value. As this fold is in conflict with BinOp push through Select. So in scenarios such as :
select C, 4, 0 -> shl C, 2 after ISEL becomes
v_cndMask v0, 0, 1, vcc
v_lshl v1 , v0, 2
rather than if we avoid mentioned folding here, the instructions generated after ISEL would be
v_cndMask v0, 0, 4, vc
As an experiment did try to comment out the mentioned folding & documented the failing LIT test here along with mir changes deduction results :