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

Assertion "Invalid SubReg for physical register" failed - Broken subreg SubRegIdxLists?

Open JonPsson opened this issue 3 years ago • 15 comments

llc -mtriple=s390x-linux-gnu -mcpu=z15 -O3 -o out.s -start-before=simple-register-coalescing ./tc_VR128_HH32.mir testcase.tar.gz

This test case starts out with separate fp32 / fp64 and vr128 bit regs:

%10:fp32bit = nofpexcept WFDSB killed %9:fp32bit, killed %8:fp32bit, implicit $fpc
undef %11.subreg_h32:fp64bit = COPY %10:fp32bit
%13:fp64bit = COPY killed %11:fp64bit
%13:fp64bit = nofpexcept MDEBR %13:fp64bit(tied-def 0), killed %10:fp32bit, implicit $fpc
undef %16.subreg_h64:vr128bit = COPY killed %13:fp64bit
%20:vr128bit = VPDI %16:vr128bit, killed %18:vr128bit, 0

The RegCoalescer produces a register def with the hh32 subreg:

288B      undef %16.subreg_hh32:vf128bit = nofpexcept WFDSB %9:fp32bit, %8:fp32bit, implicit $fpc
400B      %20:vr128bit = VPDI %16:vf128bit, %18:vr128bit, 0

On SystemZ, a VR128 reg has a "high" subreg FP64, which in turn has a "high" subreg FP32. So the FP32 subreg of VR128 has the HH32 subreg index.

This assertion fails:

VirtRegMap.cpp:603: void {anonymous}::VirtRegRewriter::rewrite(): Assertion PhysReg.isValid() && "Invalid SubReg for physical register"' failed.`

PhysReg = TRI->getSubReg(PhysReg, SubReg);

was called, where PhysReg is SystemZ::V1 (%16 above), and Subreg is SystemZ::subreg_hh32, as expected.

I suspect that the SubRegIdxLists is broken:

SubRegIndices for V1 is 0, meaning that *SRI is set to the first SubRegIndices list in MCRegisterInfo::getSubReg():

extern const uint16_t SystemZSubRegIdxLists[] = {
  /* 0 */ 2, 1, 0,
  /* 3 */ 5, 1, 0,
  /* 6 */ 6, 1, 2, 3, 0,
  /* 11 */ 6, 5, 1, 2, 4, 3, 0,
};

The subreg-indices here are 2, 1, which are h64 and h32, per the generated table:

enum : uint16_t {
  NoSubRegister,
  subreg_h32,   // 1
  subreg_h64,   // 2
  subreg_hh32,  // 3
  subreg_hl32,  // 4
  subreg_l32,   // 5
  subreg_l64,   // 6
  NUM_TARGET_SUBREGS
};

The subregs retrieved with MCSubRegIterator is F1D and F1S, which are the correct registers.

However, the h32 subreg index should actually be hh32 (3 above). This is the subreg index that the coalescer came up with (I suppose through composeSubRegIndices(), but it is not found in this list, so NoRegister is returned, and then the assert triggers.

Since the right subreg index is produced by the coalescer, it seems this should then also work with getSubReg(). I guess either something is missing in the SystemZ .td file, or there is a bug somewhere?

(This is a rare case which I ran into only recently during other experiments).

JonPsson avatar Mar 13 '23 20:03 JonPsson

Can reproduce with the provided testcase -- will try and have a quick look.

perlfu avatar Mar 14 '23 00:03 perlfu

From a quick look, tablegen has not inferred that hh32 is a valid subregister of VR128. There is no mapping of hh32 to register class, i.e. VR32Bit.

 { // VR128Bit
     2,  // VR128Bit:subreg_h32 -> VR32Bit
     12, // VR128Bit:subreg_h64 -> VR64Bit
     0,  // VR128Bit:subreg_hh32
     0,  // VR128Bit:subreg_hl32
     0,  // VR128Bit:subreg_l32
     0,  // VR128Bit:subreg_l64
   },

So it is no surprise that the subregister index lists would be incorrect.

I am not yet sure why this has happened. What stands out for now is that VR128 does not have full coverage from subregisters, so that is probably a factor.

perlfu avatar Mar 14 '23 09:03 perlfu

So the problem, somewhat alluded to by the code snipped above, is that subreg_h32 is inherited for VR128 class. Because only subreg_h64 exists, subreg_h32 uniquely addresses the same bits as subreg_hh32, but subreg_h32 is given priority. There can only be one name for a given subregister, e.g. V0 subregister F0S can only be addressed as subreg_h32, not both subreg_h32 and subreg_hh32. If VR128 had subreg_l64 then the issue would not occur as subreg_h32 would not be unique (technically subreg_h32 means subreg_lh32 in this case?).

It can see three possible solutions:

  1. Give VR128 subreg_l64. I don't know the architectural implications of this.
  2. Change Tablegen to give priority to composites when inheriting subregisters (perhaps behind a flag). A quick experiment showed this fixes the attached testcase, but throws up other lit failures in SystemZ tests.
  3. Root cause and fix RegCoalescer generating subreg_hh32 for VR128, so that it only generates valid subregisters, i.e. subreg_h32.

This seems like a decision/work for a SystemZ maintainer, so I won't be investigating further, but I am happy to assist with TableGen changes if appropriate.

perlfu avatar Mar 15 '23 02:03 perlfu

3 is probably the right solution. Optimizations need to take care to introduce valid subregisters with compatible classes

arsenm avatar Mar 15 '23 03:03 arsenm

Not sure how to best implement 3, however. The RegCoalescer determines the subreg to use by simply asking TableGen, i.e. by calling TRI.composeSubRegIndices. The problem is that this routine doesn't even get the register class - it simply assumes there is a unique composed subreg index for the two input indicies that is valid across all register classes.

In the SystemZ case, this doesn't appear to be true at the moment: when composing subreg_h64 + subreg_h32 on the FP128 class, we need to get the subreg_hh32 result. However, when composing subreg_h64 + subreg_h32 on the VR128 class, according to the discussion above, we should be getting subreg_h32.

This difference in register classes follows from SystemZ ISA properties, so I'm not sure this can be changed: The FP128 class consists of pairs of floating-point registers, where subreg_h64 and subreg_l64 refer to one regular (64-bit) floating-point register each, and we need to be able to access both of them. The VR128 class on the other hand consists of single 128-bit vector registers. Now, the upper half of those registers overlay the floating-point registers and there are ISA instructions to access those directly, so we need the subreg_h64 index here. However, the lower half does not overlay any named ISA register and there are no particular instructions directly operating on those (except for vector lane operations, I guess), so I don't think we should have a subreg_l64 index on this class.

Now, it seems that either the TRI.composeSubRegIndices assumption that composing indices can be done independently of the register class is correct, or it isn't. If it is not, then something needs to change here, e.g. by passing the register class as extra argument. If it is correct, then other parts of TableGen should also respect that assumption, which would seem to match solution 2 above. (If there are SystemZ specific failures here, I'd be happy to investigate those further.)

uweigand avatar Mar 17 '23 16:03 uweigand

Not sure how to best implement 3, however. The RegCoalescer determines the subreg to use by simply asking TableGen, i.e. by calling TRI.composeSubRegIndices. The problem is that this routine doesn't even get the register class - it simply assumes there is a unique composed subreg index for the two input indicies that is valid across all register classes.

In the SystemZ case, this doesn't appear to be true at the moment: when composing subreg_h64 + subreg_h32 on the FP128 class, we need to get the subreg_hh32 result. However, when composing subreg_h64 + subreg_h32 on the VR128 class, according to the discussion above, we should be getting subreg_h32.

This difference in register classes follows from SystemZ ISA properties, so I'm not sure this can be changed: The FP128 class consists of pairs of floating-point registers, where subreg_h64 and subreg_l64 refer to one regular (64-bit) floating-point register each, and we need to be able to access both of them. The VR128 class on the other hand consists of single 128-bit vector registers. Now, the upper half of those registers overlay the floating-point registers and there are ISA instructions to access those directly, so we need the subreg_h64 index here. However, the lower half does not overlay any named ISA register and there are no particular instructions directly operating on those (except for vector lane operations, I guess), so I don't think we should have a subreg_l64 index on this class.

Now, it seems that either the TRI.composeSubRegIndices assumption that composing indices can be done independently of the register class is correct, or it isn't. If it is not, then something needs to change here, e.g. by passing the register class as extra argument. If it is correct, then other parts of TableGen should also respect that assumption, which would seem to match solution 2 above. (If there are SystemZ specific failures here, I'd be happy to investigate those further.)

With the current API I think you're supposed to check the class actually supports the subregister by checking getSubClassWithSubReg

arsenm avatar Mar 17 '23 16:03 arsenm

Now, it seems that either the TRI.composeSubRegIndices assumption that composing indices can be done independently of the register class is correct, or it isn't. If it is not, then something needs to change here, e.g. by passing the register class as extra argument. If it is correct, then other parts of TableGen should also respect that assumption, which would seem to match solution 2 above. (If there are SystemZ specific failures here, I'd be happy to investigate those further.)

With the current API I think you're supposed to check the class actually supports the subregister by checking getSubClassWithSubReg

This should be done in the RegisterCoalescer common code, not target-specific code, right?

But even if this is done, while this would fix the invalid code generation by rejecting this transformation, this seems an unnecessary pessimisation to me: we actually do support access to this particular subreg, it's just that the index is named differently.

uweigand avatar Mar 17 '23 17:03 uweigand

we actually do support access to this particular subreg, it's just that the index is named differently.

I think this is broken and tablegen shouldn't allow it. I would hope and assume subregister indexed can't be exactly aliases

arsenm avatar Mar 17 '23 17:03 arsenm

we actually do support access to this particular subreg, it's just that the index is named differently.

I think this is broken and tablegen shouldn't allow it. I would hope and assume subregister indexed can't be exactly aliases

Well, as I mentioned above, for the VR128 class, we can access a subreg_h64 + subreg_h32 as simply subreg_h32 at the moment, this is not aliased with anything.

It's simply that TRI.composeSubRegIndices of subreg_h64 + subreg_h32 returns subreg_hh32, because that is what is necessary for the FP128 class, and there's currently no way to distinguish between classes. subreg_hh32 is not supported on VS128 which is why the getSubClassWithSubReg check would fail.

But simply failing that check would be the pessimization: we should actually use subreg_h32 instead.

(Or else, allow subreg_hh32 on the VR128 class, and then forbid subreg_h32 - the latter would make sense as this would normally match subreg_l64 + subreg_h32, which we don't support on VR128 as we don't support subreg_l64 on that class.)

uweigand avatar Mar 17 '23 17:03 uweigand

Hmm. We currently define this list of subreg indices:

def subreg_l32   : SubRegIndex<32, 0>;  // Also acts as subreg_ll32.
def subreg_h32   : SubRegIndex<32, 32>; // Also acts as subreg_lh32.
def subreg_l64   : SubRegIndex<64, 0>;
def subreg_h64   : SubRegIndex<64, 64>;
def subreg_hh32  : ComposedSubRegIndex<subreg_h64, subreg_h32>;
def subreg_hl32  : ComposedSubRegIndex<subreg_h64, subreg_l32>;

used in (among others) these two register classes:

// 8 pairs of FPR64s, with a one-register gap inbetween.
class FPR128<bits<16> num, string n, FPR64 low, FPR64 high>
 : SystemZRegWithSubregs<n, [low, high]> {
  let HWEncoding = num;
  let SubRegIndices = [subreg_l64, subreg_h64];
  let CoveredBySubRegs = 1;
}

and

// A full 128-bit vector register, with an FPR64 as its high part.
class VR128<bits<16> num, string n, FPR64 high>
  : SystemZRegWithSubregs<n, [high]> {
  let HWEncoding = num;
  let SubRegIndices = [subreg_h64];
}

From what I understand, the problem is that the "also acts as" comments here:

def subreg_l32   : SubRegIndex<32, 0>;  // Also acts as subreg_ll32.
def subreg_h32   : SubRegIndex<32, 32>; // Also acts as subreg_lh32.

are only true because subreg_l64 is listed as the first subreg in this list:

  let SubRegIndices = [subreg_l64, subreg_h64];

so it is only true for FP128.

Would it make sense to swap this list to:

  let SubRegIndices = [subreg_h64, subreg_l64];

so that subreg_h32 always acts as subreg_hh32 - which would work for both FP128 and VR128. And then define an extra subreg_lh32 as combination of subreg_l64 + subreg_h32, which would be only defined for FP128?

Not sure if there are any further consequences of swapping the order in the SubRegIndices list ...

uweigand avatar Mar 17 '23 17:03 uweigand

That makes sense to me but I know nothing about SystemZ. In the very early days of AMDGPU we used to have differently named subregisters for different classes which really should have all been the same; IIRC this was due to some tablegen issue which was later fixed. This might be a holdover from then

arsenm avatar Mar 19 '23 15:03 arsenm

It seems to be workable, or at least it passes LIT tests and the original test case.

Diff attached in case anyone wants to pick this up: systemz-vr128-h32-l32.diff.zip

perlfu avatar Mar 20 '23 06:03 perlfu

Thanks for providing that diff, @perlfu, much appreciated! @JonPsson can you try whether this has any other effect on codegen?

uweigand avatar Mar 20 '23 15:03 uweigand

I can confirm that this patch fixes my original test case, as the COPY is coalesced into %16.subreg_h32:vf128bit instead of into hh32 subreg.

On SPEC, I see one file change (f526.blender_r/build/sequencer.s):

main <> patched

adbr           :                 6175                 6166       -9
ldr            :                51013                51004       -9
wfadb          :                 6947                 6956       +9

I reduced this and found that SimpleRegisterCoalescer can now coalesce one more COPY:

< 288B    undef %13.subreg_h32:fp64bit = COPY %6.subreg_h32:vf128bit
< 320B    %13:fp64bit = nofpexcept MDEBR %13:fp64bit(tied-def 0), %6.subreg_h32:vf128bit, implicit $fpc
---
> 320B    %6.subreg_h64:vf128bit = nofpexcept MDEBR %6.subreg_h64:vf128bit(tied-def 0), %6.subreg_h32:vf128bit, implicit $fpc

So this small change seems fine and actually an improvement on that file.

ninja check passes, so it all seems ok.

JonPsson avatar Mar 21 '23 14:03 JonPsson

Thanks @JonPsson! Please also update the "Also acts as ..." comments in SystemZRegisterInfo.td:

def subreg_l32   : SubRegIndex<32, 0>;  // Also acts as subreg_ll32.
def subreg_h32   : SubRegIndex<32, 32>; // Also acts as subreg_lh32.

Otherwise, the patch LGTM.

uweigand avatar Mar 21 '23 14:03 uweigand