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

    <tr>
        <th>Summary</th>
        <td>
            Unsupported calling convention: AMDGPU_KERNEL encountered when codegen some amdgpu (opencl) bitcode
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
      </td>
    </tr>

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

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

<pre>
    I'm not sure whether this is a bug or a missing feature, but please look at https://github.com/littlewu2508/LLVMAMDGPUcodegenbug, which is the reproducing method.

The issue is, during codegen phase, some simple opencl program meets "Unsupported calling convention: AMDGPU_KERNEL", while using the ROCm-forked llvm, the issue does not exists. Using gdb, I found that with vanilla llvm, one of the instructions `I` in https://github.com/llvm/llvm-project/blob/c12386ae247c0d46e1d513942e322e3a0510b126/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp#L1136 has `OpCode=56` and `SubclassData=365`, which leads to 91 (CallingConv::AMDGPU_KERNEL) in followed steps (`LowerCallTo`, `LowerCall`, `CCAssignFnForCall`, etc), and finally get Unsupported calling convention issue. Gdb trace: 
[llvm-14.0.5-dbg-cmdline.log.gz](https://github.com/llvm/llvm-project/files/10510584/llvm-14.0.5-dbg-cmdline.log.gz)

For [ROCm forked llvm](https://github.com/RadeonOpenCompute/llvm-project/), there's no issue. My guess is that `source1.cl` is calling amdgpu kernel functions, but somehow it no `I` with 91 (CallingConv::AMDGPU_KERNEL) appearing, only 0 (CallingConv::C) is seen, so no unsupported calling convention issues (gdb trace: 
[ROCm-llvm-5.1.3-dbg-cmdline.log.gz](https://github.com/llvm/llvm-project/files/10510588/ROCm-llvm-5.1.3-dbg-cmdline.log.gz)). But I failed to find out which change they use to get things worked, due to lack of compiler knowledge, and inability to bisect [ROCm forked llvm](https://github.com/RadeonOpenCompute/llvm-project/) (encountered many build issues when bisecting).

Forgive me of using llvm-14 because I built the debug version of llvm/clang to start investigating th issue half a year earlier (original issue: https://github.com/RadeonOpenCompute/ROCm-CompilerSupport/issues/45). I can confirm that this situation still remains now for both llvm-15.0.7 and llvm-16
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy0Vktz2zgS_jXQpcssEhQp6aCDLUUu1zqbrTz2OgUCLRJjEGDhYY3m1081KDmuTCYTH3KRTbLR3ejvAYgQdG8Rt6y5Y81-IVIcnN8aHaPBU-JNuV50Tp23D4yvRrAuQkge4TRgHNBDHHQAHUBAl3pwHgSMOgRteziiiMkj4zvoUoTJoAgIxrknEBGGGKfA6lvGD4wfeh2H1BXSjYwfXhdn_PD4-P_3t-_39__7Ip3CHm2Xekp6GrQcqHYcEDxO3qkkqfCIcXCqYOWelbfz7-cBQYeQ6JfWquQp8pIQpkGE3GhwI0LQ42QQ3IRWGpi8670YYUSMARjnX2xI0-R8RAVSGDMnss9oo3aW1bcwd_vbf959_O-7R8b5pVuDkPJkqOGPH3bjzdH5J1RgzPNIMfGlS-Uw5GHjHzrEUMCXvLBXHcU9wNElqyAOIsJJxwGehdXGiJdMziK445zQhuiTpN4CsLZ8YG0J2v4QgJwl_7mZvPsdZWT80BnXMX6QFa_XrUC-XMlSLVusVFPVmyXHmnOsRdlUZVfx9lUeTet2TuE9WsYPn9Bg7md_e__N413SRqEv5DQxXj9WVd3CIHLfHybKwOp909IOhFX09lPqpBEh7EUUrN7XbcPa8is7DAoVIDrYVMD4ejfDtXP2mXZe336D1IYGc3TGuBMqCBEnQnzN2vLRndDT8s_uUuD1y6-vdrvbrKiDPbjXnzBKxjf0HzV-1FYYc4YeI_yYTjMfCrhXHUQvJBK_Lsxu7jJC1bIoi-ZGdf2NHJXRFgvj-qL_kzV7xtdvxvmoDQbGDxUh2ayX14h_rsM3r7V2cB5Yc0cEh9cE_7duPgqFzn6Y0O7cOKWIf2_tMkJyHmR8RRK5Duj9GfqEIcyGICKBEVzyEqtCmsz58D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