This is an archive of the discontinued LLVM Phabricator instance.

[MCP] Do not try forward non-existent sub-register of a copy
ClosedPublic

Authored by barannikov88 on Mar 26 2023, 9:09 PM.

Details

Summary

In this example:

$d14 = COPY killed $d18
$s0 = MI $s28

$s28 is a sub-register of $d14. However, $d18 does not have
sub-registers and thus cannot be forwarded. Previously, this resulted
in $noreg being substituted in place of the use of $s28, which later
led to an assertion failure.

Fixes https://github.com/llvm/llvm-project/issues/60908, a regression
that was introduced in D141747.

Diff Detail

Event Timeline

barannikov88 created this revision.Mar 26 2023, 9:09 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 26 2023, 9:09 PM
barannikov88 requested review of this revision.Mar 26 2023, 9:09 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 26 2023, 9:09 PM

Add a positive test

Strip changes that are not necessary for fixing the bug.
They will be in a separate commit.

barannikov88 retitled this revision from [MCP] Properly handle sub-register forwarding to [MCP] Do not try forward non-existent sub-register of a copy.Mar 26 2023, 11:59 PM
barannikov88 edited the summary of this revision. (Show Details)
barannikov88 added reviewers: arsenm, glandium, dim.
barannikov88 edited the summary of this revision. (Show Details)

Drop 'renamable' on the registers that are not really renamable.

glandium resigned from this revision.Mar 27 2023, 7:46 PM

I'm not a reviewer, but I can confirm the patch fixes the issue as it was filed.

arsenm accepted this revision.Mar 27 2023, 7:49 PM
arsenm added inline comments.
llvm/lib/CodeGen/MachineCopyPropagation.cpp
651

Don't really understand the assert message here (plus I thought getSubRegIndex could deal with the 0 case anyway)

This revision is now accepted and ready to land.Mar 27 2023, 7:49 PM
barannikov88 added inline comments.Mar 27 2023, 9:03 PM
llvm/lib/CodeGen/MachineCopyPropagation.cpp
651

findAvailCopy returns non-null only if isSubRegisterEq(CopyDstReg, MOUse.getReg()) returns true, i.e. if CopyDstReg is the same as MOUse.getReg() or is a sub-register of MOUse.getReg(). If it is a sub-register, it must have a valid sub-register index, and this is checked by this assertion.
Could you suggest how to rephrase the assert message? My English is bad at times.

barannikov88 added inline comments.Mar 27 2023, 9:09 PM
llvm/lib/CodeGen/MachineCopyPropagation.cpp
651

I messed up operands of isSubRegisterEq. It should read:

"if isSubRegisterEq(CopyDstReg, MOUse.getReg()) returns true, i.e. if MOUse.getReg() is the same as CopyDstReg or is a sub-register of CopyDstReg."

renamable $d14 = COPY killed $d15
$s0 = COPY killed renamable $s28

Here, $d14 is CopyDstReg, $s28 is MOUse.getReg(), and $d15 is CopySrcReg.

xiangzhangllvm added inline comments.
llvm/lib/CodeGen/MachineCopyPropagation.cpp
653

I think there is unsafe to use the SubRegIdx when CopySrcReg and CopyDstReg reg are not same regclass or even different size.
I happen to meet this bug in our tests.
Pls refer to https://reviews.llvm.org/D147031

barannikov88 added inline comments.Mar 28 2023, 9:25 PM
llvm/lib/CodeGen/MachineCopyPropagation.cpp
653

Why do you think it is unsafe?

The test contains:

renamable $rcx = COPY killed renamable $k0
renamable $k1 = COPY renamable $ecx

Suppose it is a valid MIR, despite of the fact that $k0, $ecx and $rcx are all of different sizes.

With D147031 it is converted to (according to CHECK lines):

renamable $rcx = COPY renamable $k0
renamable $k1 = COPY $k0

Which, I think is a change of semantics. (What's the semantics of a COPY to a differently-sized register?)

With this patch it remains untouched:

renamable $rcx = COPY killed renamable $k0
renamable $k1 = COPY renamable $ecx

This patch does not try to optimize code, it only fixes the reported issue. But it does not generate invalid code (unless proven otherwise :).

@xiangzhangllvm
If you don't mind, I'd land this patch as it fixes the original issue. It would be nice if you could check it on your codebase though.
Feel free to update your patch if this one wouldn't fix your issue, I'd gladly review it.

xiangzhangllvm added inline comments.Mar 29 2023, 6:04 PM
llvm/lib/CodeGen/MachineCopyPropagation.cpp
653

1st, sorry for I am off line yesterday (for some family errands).
2nd, I think your patch can also fix my test fail. So I +1 for you to commit this patch first. Any other problems, we can go on refine it.
3rd, For different reg class, the following convert has risk.

B= COPY C
A= COPY SubB

==>
A = COPY SubC

Because the SubRegIdx of B (for SubB) may not match the SubRegIdx of C (for SubC).
(for example B has 2 subreg but C only has 4)

Anyway, this can be refine later. We can let this patch in first.

Try to refine assertion message.

This revision was landed with ongoing or failed builds.Mar 29 2023, 8:11 PM
This revision was automatically updated to reflect the committed changes.
llvm/lib/CodeGen/MachineCopyPropagation.cpp
653

@barannikov88 I enhance it at D147031
Pls help review.