Physical subregister liveness

(Cross posting from discourse)

Hi,

MachineVerifier allows a use of a physical register iff any of its
subregisters is defined:

This means that a wide COPY like this is legal, even if only some of
the individual subregsters ($sgpr0, $sgpr1, $sgpr2) are defined:

$vgpr1_vgpr2_vgpr3 = COPY killed $sgpr0_sgpr1_sgpr2

But if the target’s copyPhysReg splits it into multiple word-sized
copies like this, then some of them may no longer be legal, because
the corresponding source is completely undefined:

$vgpr3 = V_MOV_B32_e32 $sgpr2
$vgpr2 = V_MOV_B32_e32 $sgpr1
$vgpr1 = V_MOV_B32_e32 $sgpr0

Various targets (AMDGPU, Sparc, SystemZ) work around this in
copyPhysReg by adding extra implicit operands to the word-sized copy
instructions to satisfy MachineVerifier. But these extra operands make
it look like there are dependencies between instructions that have
none. This restricts post-RA scheduling freedom and confuses other
late codegen passes. For example, AMDGPU actually adds all of these
implicit operands for the example above [edited slightly to remove
irrelevant stuff]:

$vgpr3 = V_MOV_B32_e32 $sgpr2, implicit-def $vgpr1_vgpr2_vgpr3,
implicit $sgpr0_sgpr1_sgpr2
$vgpr2 = V_MOV_B32_e32 $sgpr1, implicit $sgpr0_sgpr1_sgpr2
$vgpr1 = V_MOV_B32_e32 $sgpr0, implicit killed $sgpr0_sgpr1_sgpr2

Because of this, I would like to find a better solution. I can think of three:

1. Use subreg liveness information in copyPhysReg to only copy the
parts of the wide register that are live. I tried that in D113017
"[AMDGPU] Avoid copying dead subregisters in copyPhysReg" and it seems
to work, but it also has a measurable (0.7%) compile time cost, which
seems unfortunate.

2. Change the physical subreg liveness rules to say that all parts of
a physical register have to be defined. I’m not sure how to implement
this, but I suppose it would mean we would need more IMPLICIT_DEF
instructions to satisfy the verifier.

3. Change the physical subreg liveness rules to say that no part of a
physical register has to be defined. I guess this would be unpopular
because it means we end up with no liveness verification at all.

Thoughts?

Thanks,
Jay.

Hi Jayfoad!

I restarted working on this to get rid of usage of implicit & implicit-def. As I looked into its various aspects such as how the PhysRegTuples really formed & selected by COPY during RegisterAllocator & later assigned by virtRegRewriter, how laneMask is used to maintain liveness of SubRegs, etc.

Even after having significant background study, I looked upon your prototype solution as well, which is more or less I come to eventually also. And on Matt’s suggestion & my understanding even thought of the solution mentioned below:

  1. Since, its unavoidable to solve this issue without having the liveness info (at instructions while iterating MI), the point is to do it as early as possible where pre-computed liveness info can be used as it is, leading me to the VirtRegRewriter pass.

  2. Now, once we have subreg’s liveness info (in laneMask) form, the next point is how to preserve (if needed update) upto the point of its usage in CopyPhysReg. Either it can be done via maintaing some sort of internal analysis structure OR use explicit RegMask operand to represent it (Matt’s suggestion, but needs to thought deliberately as it will make machine operand bigger).

But, after spending such a long time on it, the question which really intrigued me is why MachineVerifier allows use of such Physical registers, as being new in this, its difficult for me understand how such a COPY would practically created in compiler’s pipeline (my scope for this doubt came after studying RA’s allocation & VirtRegRewriter’ assignment of RegTuples)

Any Comments!

Thanks,
Vikash

PS : You can refer this JIRA ticket SWDEV-498533 where, this would be tracked

For the record I guess you mean this suggestion:

I think we should migrate to having explicit operands carrying regmasks.

I don’t really understand what that would look like or what problem it would solve.

Also I’m still quite fond of my original solution #2:

Change the physical subreg liveness rules to say that all parts of a physical register have to be defined.

For the record I guess you mean this suggestion:

Yes!

Also I’m still quite fond of my original solution #2:

Change the physical subreg liveness rules to say that all parts of a physical register have to be defined.

It makes sense, though it would require implementation changes at lot of places within compiler, where SuperRegTuples (partially defined) are spawned by diffierent mechanism. And later need used by some psuedo instruction which would need its expansion, breaking down it into smaller subregs (maybe dead or defined).

But, after spending such a long time on it, the question which really intrigued me is why MachineVerifier allows use of such Physical registers, as being new in this, its difficult for me understand how such a COPY would practically created in compiler’s pipeline (my scope for this doubt came after studying RA’s allocation & VirtRegRewriter’ assignment of RegTuples)

Can you put some light on this! Let me know if you did not understood my query.

Hi Jayfoad,

I have a question, lets assume that somehow we have the knowledge of defined dword-sized subregs, so we would only allow copies for those. But following it could be the case that dest SupsrReg of that COPY might be used entirely, which would now be only partially-defined owing to selective dword-sized COPY. You can understand it from below example :

bb.0 (%ir-block.0):
INLINEASM &“v_mov_b32_e32 v5, 5” [sideeffect] [attdialect], $0:[regdef], implicit-def $vgpr5
INLINEASM &“v_mov_b32_e32 v6, 6” [sideeffect] [attdialect], $0:[regdef], implicit-def $vgpr6
renamable $vgpr0_vgpr1_vgpr2_vgpr3 = COPY renamable $vgpr5_vgpr6_vgpr7_vgpr8, implicit $exec
renamable $sgpr3 = S_MOV_B32 61440
renamable $sgpr2 = S_MOV_B32 -1
renamable $vgpr0 = COPY killed renamable $vgpr6, implicit $exec
BUFFER_STORE_DWORDX4_OFFSET killed renamable $vgpr0_vgpr1_vgpr2_vgpr3, renamable $sgpr0_sgpr1_sgpr2_sgpr3, 0, 0, 0, 0, implicit $exec :: (volatile store (s128) into ptr addrspace(1) undef, addrspace 1)
INLINEASM &v_nop [sideeffect] [attdialect], $0:[reguse:VReg_128], renamable $vgpr5_vgpr6_vgpr7_vgpr8
S_ENDPGM 0

In above IR, it can be seen COPY is needed only for $vgpr5 & $vgpr6, but that would left $vgpr2 & $vgpr3 undefined, leading to issue in usage of $vgpr0_vgpr1_vgpr2_vgpr3 by BUFFER_STORE_DWORDX4_OFFSET

Now, the question is how to take care of this scenario, one way is to use implicit-def for dstSuperReg of COPY (defeating the purpose), but then I am very curious to know how your solution D113017
“[AMDGPU] Avoid copying dead subregisters in copyPhysReg” took care of this scenario as it eliminates both implicit(for SrcSuperReg) & implicit-def(for DstSuperReg) which holds in contradiction to my findings as mentioned here. Am I missing something ??

Thanks!
Vikash

Hi Guys!

@arsenm , @cdevadas , @pravinjagtap

As I restarted working on it (mentioned the JIRA ticket), would be grateful if you could give your thoughts on whatever posted recently.

Thanks!
Vikash

I don’t recall exactly where partially defined physical registers first appear. You could try changing MachineVerifier to require physical registers to be fully defined, and see where it starts failing?

In above IR, it can be seen COPY is needed only for $vgpr5 & $vgpr6, but that would left $vgpr2 & $vgpr3 undefined, leading to issue in usage of $vgpr0_vgpr1_vgpr2_vgpr3 by BUFFER_STORE_DWORDX4_OFFSET

Right, but you would only get that problem (BUFFER_STORE_DWORDX4_OFFSET using partially defined vgpr0_vgpr1_vgpr2_vgpr3) if the original MIR had an equivalent problem (COPY using partially defined vgpr5_vgpr6_vgpr7_vgpr8). So copyPhysReg would not introduce new undefinedness problems, so I think it is OK.