llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[RFC] [AMDGPU] [SelectionDAG] [GlobalIsel] select with constant combine into binaryOp with zext/sext

Open vg0204 opened this issue 1 year ago • 19 comments

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.

vg0204 avatar Dec 26 '24 08:12 vg0204

@llvm/issue-subscribers-backend-amdgpu

Author: Vikash Gupta (vg0204)

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 --&gt; add (zext Cond), (C1-1)
select Cond, Pow2, 0  --&gt;  shl  (zext Cond), log2(Pow2)  
select Cond, C1, C1+1 --&gt; 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 --&gt; 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 &lt;==&gt; 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.

llvmbot avatar Dec 26 '24 10:12 llvmbot

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.

vg0204 avatar Dec 26 '24 11:12 vg0204

CC : @arsenm, @cdevadas , @aemerson , @jayfoad

vg0204 avatar Dec 26 '24 11:12 vg0204

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

arsenm avatar Dec 26 '24 12:12 arsenm

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 ?

vg0204 avatar Dec 26 '24 14:12 vg0204

Ping @arsenm !

vg0204 avatar Dec 30 '24 07:12 vg0204

Gentle Ping @arsenm , @jayfoad , @aemerson , @cdevadas

vg0204 avatar Jan 03 '25 07:01 vg0204

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

arsenm avatar Jan 04 '25 11:01 arsenm

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?

vg0204 avatar Jan 06 '25 06:01 vg0204

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

arsenm avatar Jan 06 '25 06:01 arsenm

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!

vg0204 avatar Jan 06 '25 11:01 vg0204

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.

vg0204 avatar Jan 06 '25 11:01 vg0204

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

vg0204 avatar Jan 06 '25 12:01 vg0204

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.

vg0204 avatar Jan 06 '25 12:01 vg0204

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

vg0204 avatar Jan 07 '25 06:01 vg0204

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 !

vg0204 avatar Jan 13 '25 06:01 vg0204

A gentle ping @arsenm

vg0204 avatar Jan 20 '25 06:01 vg0204

Ping @pravinjagtap , @cdevadas !

vg0204 avatar Jan 23 '25 07:01 vg0204

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 :

LITTestFailure.txt

vg0204 avatar Feb 13 '25 14:02 vg0204