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

    <tr>
        <th>Summary</th>
        <td>
            Clang crashes by bitcasting pointers of different opencl address spaces
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang,
            OpenCL
      </td>
    </tr>

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

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

<pre>
    In targets that hold a different address space for each of the 7 possible opencl address spaces, when running 
[lifetime.cl](https://github.com/llvm/llvm-project/blob/f92fa7e2cf38341211af262b21c568bef4d76b10/clang/test/CodeGenOpenCL/lifetime.cl#L4) test, it breaks by performing a bitcast instead of a addresSpaceCast between two pointers of different address spaces.
Although "fixing" it in [CGCall.cpp](https://github.com/llvm/llvm-project/blob/4d177435bae03551245ffdc4dfcee5345323121d/clang/lib/CodeGen/CGCall.cpp#L5304) to handle addresSpaceCasts is rather easy, it seems rather fragile as solution. Shouldn't there be a way to tell clang that an address space of opencl subsumes another one (such as opencl_generic subsumes .... all other opencl ?) and such casts wouldn't required, that is, even if the ptr is not at the same address space as the function argument, if the latter includes the first, it shouldn't require any sort of cast.

Generated IR after fixing it to perform address space cast:
```
define dso_local void @helper_no_markers() local_unnamed_addr #0 {
entry:
  %a = alloca i8, align 1
  call void @llvm.lifetime.start.p0(i64 1, ptr nonnull %a) #4
 %0 = addrspacecast ptr %a to ptr addrspace(4)         <<=== here it creates a bitcast and it breaks
  call void @use(ptr addrspace(4) noundef %0) #5
 call void @llvm.lifetime.end.p0(i64 1, ptr nonnull %a) #4
  ret void
}
```

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJykVdtu4zYQ_Rr6ZVBBInXzgx82DrxYYIEC3Q8IRuRIYpcmVZJKmr8vSMnxNk0LLGoIvnF45szhmSGGoCdLdGLNA2seD7jG2fmTsgGvC2p3GJx6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