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

    <tr>
        <th>Summary</th>
        <td>
            [CUDA, NVPTX] LLVM crash with "Cannot cast between two non-generic address spaces"
        </td>
    </tr>

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

    <tr>
      <th>Assignees</th>
      <td>
            Artem-B
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          Artem-B
      </td>
    </tr>
</table>

<pre>
    Valid CUDA code results in LLVM crash due to an attempt to generate an impossible addrspacecast in a known-false conditional branch.

Reproducer: https://godbolt.org/z/Yjjsdvj1r

Source:
```
#include <stdint.h>

// No crash if the function is not inlined, and thus does not know 
// at compile time which pointer it will be handling
//__device__ uintptr_t f(void *p) __noinline__;

__device__ uintptr_t f(void *p) {
   if (__isGlobal(p))
    return __cvta_generic_to_global(p);
   if (__isShared(p))
     return __cvta_generic_to_shared(p);
   return (uintptr_t)p;
}

__shared__ int shared_data;
__device__ int global_data;
__constant__ int const_data = 3;

__global__ void square(uintptr_t* out, int n) {
  out[0] = f(&shared_data);
  out[1] = f(&global_data);
}
```

IR:
```
define dso_local void @square(unsigned long*, int)(ptr nocapture noundef writeonly %out, i32 noundef %n) local_unnamed_addr #1 {
entry:
  %0 = tail call i1 @llvm.nvvm.isspacep.global(ptr addrspacecast (ptr addrspace(3) @shared_data to ptr))
  %1 = tail call i1 @llvm.nvvm.isspacep.shared(ptr addrspacecast (ptr addrspace(3) @shared_data to ptr))
  %. = select i1 %1, i64 ptrtoint (ptr addrspace(3) @shared_data to i64), i64 ptrtoint (ptr addrspacecast (ptr addrspace(3) @shared_data to ptr) to i64)
  %retval.0.i = select i1 %0, i64 ptrtoint (ptr addrspace(1) addrspacecast (ptr addrspace(3) @shared_data to ptr addrspace(1)) to i64), i64 %.
  store i64 %retval.0.i, ptr %out, align 8, !tbaa !8
  ret void
}
```

The culprit is `addrspacecast (ptr addrspace(3) @shared_data to ptr addrspace(1))` here:
```
  %retval.0.i = select i1 %0, i64 ptrtoint (ptr addrspace(1) addrspacecast (ptr addrspace(3) @shared_data to ptr addrspace(1)) to i64), 
```

To add insult to injury, the impossible cast is still going to be executed, and will likely result in a runtime error trying to convert the pointer in the wrong address space.

We need to make sure that `__cvta_generic_to_global()` is never executed if `__isGlobal()` is false. Same for the conversions from shared and constant AS.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzUVk9v47YT_TT0ZRBBoiz_OfjgTX7-ocB2UWy22_YkUOTYYpYmVXJkN_30BWkllp1mm7Z7KWDYJjXz-N4MRT4Rgt5ZxBWr3jHO155wfxP_sepuInpqnV8Nk5PGqcfVZ2G0gtsf79YgnULwGHpDAbSF9-8_fw_Si9CC6hHIgbAgiHDfURzt0KIXhHFa7zsXgm4MglDKh05IlCJQxBHwxbqjvdkKExCks0qTdlYYaLywss1Yfsfy9en7I3beqV6iZ-UaWqIusHLN-Ibxzc6pxhnKnN8xvvmd8c0vDw9BHR4KP4a4d72XGLNOk7N8-JyGvNRWml4hsPI2kNKWspaV_xtDnNaDD27Qr7dALcK2tzJSBx3AuijOaIuK8VsQVgG1fQDl8PQwioYLOEEg3b7TBoH0HuHYatlC57Ql9KAJjtoYaBBaYZXRdjfOrmuFBy2xrqHXljryNcGW8cXBaQWMrzvGl1DX1p1Y1TUr3401vQ2AzYckgKia8UVd6_B_4xphGF_EmPh5CgGP1HsLdS0PJOq0JbSsydW7i5Tyz1DvW-Fj9V6ivg4bLnJGsEMG44tndYwvu3MR5neX1Tgh1TVoSzAMlCDxnDEqWAw5CboOkc4GEpaGoDRMMcDKOyhf9GBAqSFVPfzaC4-XnNfgeopbKuLZq57ER9W7nFV3CT-2j_HZmP1FVU7hxVX4WMko_FyhqzcmfX_38bU3SuFWWwQVXG2cFOYkjU3zszqbTiUFxtkd4-tBXer6oiMP1knRUe8RrOutwi0cvSZ01jwC49VTQUr-_JzxKtU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