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

    <tr>
        <th>Summary</th>
        <td>
            [AMDGPU] Alias to alias crashes 'AMDGPUResourceUsageAnalysis' with an invalid cast
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

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

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

<pre>
    The following LLVM-IR using nested aliases crashes the AMDGPU backend during resource assignment. This is caused by an invalid cast between incompatible types. See the godbolt link https://godbolt.org/z/Mrv7a1PrP or below.

```llvm
target triple = "amdgcn-amd-amdhsa"

@foo_a = alias void (ptr), ptr @foo
@bar_a = alias void (ptr), ptr @foo_a

define void @foo() {
entry:
  ret void
}

define void @bad() {
entry:
  call void @bar_a(ptr null)
  ret void
}
```
Simply invoking the backend will reproduce the issue.
```console
$ llc bug.bc
llc: /root/llvm-project/llvm/include/llvm/Support/Casting.h:578: decltype(auto) llvm::cast(From*) [with To = llvm::Function; From = llvm::Constant]: Assertion `isa<To>(Val) && "cast<Ty>() argument of incompatible type!"' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.      Program arguments: /opt/compiler-explorer/clang-assertions-trunk/bin/llc -o /app/output.s -x86-asm-syntax=intel <source>
1.      Running pass 'Function register usage analysis' on module '<source>'.
 #0 0x0000000003a18df8 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x3a18df8)
 #1 0x0000000003a1655c SignalHandler(int) Signals.cpp:0:0
 #2 0x000077b059042520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #3 0x000077b0590969fc pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x969fc)
 #4 0x000077b059042476 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x42476)
 #5 0x000077b0590287f3 abort (/lib/x86_64-linux-gnu/libc.so.6+0x287f3)
 #6 0x000077b05902871b (/lib/x86_64-linux-gnu/libc.so.6+0x2871b)
 #7 0x000077b059039e96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
 #8 0x0000000000d3c5ad llvm::AMDGPUResourceUsageAnalysis::analyzeResourceUsage(llvm::MachineFunction const&, llvm::TargetMachine const&, unsigned int, unsigned int) const (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0xd3c5ad)
 #9 0x0000000000d3d673 llvm::AMDGPUResourceUsageAnalysis::runOnModule(llvm::Module&) (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0xd3d673)
#10 0x0000000002fce7f5 llvm::legacy::PassManagerImpl::run(llvm::Module&) (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x2fce7f5)
#11 0x0000000000835c2c compileModule(char**, llvm::LLVMContext&) llc.cpp:0:0
#12 0x000000000072b37e main (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x72b37e)
#13 0x000077b059029d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#14 0x000077b059029e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#15 0x000000000082c74e _start (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x82c74e)
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy0V11v6zYS_TX0y8CGTH0_-MGJr3cL3GCDm7SvxoikZTYUKZBUEvfXL0jJSeQLtM1uGsRBRA3PHJ758BCdk60WYkPyG5LvFjj4k7Gb309DI2yxaAw_bx5PAo5GKfMidQvfv_92t_zlBwwuPGnhvOCASqITDphFdxIO_EnA9m73r_tfoUH2JDQHPtiwwQpnBssEjJ47of0KHk_SgXTAcHCCQ3MG1CD1MyrJgaHz0Aj_IkRYZKbr0ctGCfDnXrgVPAgRHbaGN0Z5UFI_wcn73pF0S-ie0P30amVsS-j-D0L3d_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