<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/62391>62391</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Register coalescer crashes due to incorrect subregister live ranges
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            backend:AMDGPU,
            llvm:codegen,
            crash
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          nhaehnle
      </td>
    </tr>
</table>

<pre>
    When run through `llc -march=amdgcn -mcpu=gfx1100 -O1 -amdgpu-codegenprepare-break-large-phis=false`, the attached step4.ll(.txt) crashes in the register coalescer (in `LiveRange::MergeValueNumberInto` called from `RegisterCoalescer::adjustCopiesBackFrom`).

The test is reduced via bugpoint and unfortunately still relatively large, but even the tiniest changes I could think of make the crash go away.

The crash is related to incorrect subregister live ranges somehow. What I found:

* It happens when trying to merge %160 and %1382 in a basic block in which %1382 is copied to %160 and then directly back again
* At that point, only sub0 (lanemask 0x3) and sub1 (lanemask 0xC) are live
* This is reflected in the subregister live ranges of %1382, but *not* in %160 which does not have any subregister live ranges
* This is unexpected by the code leading to a null pointer dereference

I have far too little knowledge of this code to have the slightest idea of what *should* be happening. It seems like register coalescing somehow updates the subregister live ranges on the fly and that breaks down somewhere?
[step4.ll.txt](https://github.com/llvm/llvm-project/files/11337123/step4.ll.txt)

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyUVM2O4zYTfBr60rAhUbbHPujgHcMfBvg2CRab7LlFtkSuKVLgjz1--4CUvTPJYIPk4h-xVV1VXWwMQQ-WqGWbT2xzXGCKyvnWKiRlDS06J2_tN0UWfLIQlXdpUMC2lTECliN6oVhzxFEOwsJyFFNizXHoX-u6qmD5aw3LfDalpXCSBrKTpwk9LTtPeF4a9AMtJ6UDa449mkBsWzH-DFERYIwoFEkIkab1yhjGd6v4Ghnfg_AYFAXQtpR6GnSI5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