<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=http://email.email.llvm.org/c/eJzFWFtzmzgU_jX4RWMP98uDH1wnaTPb7Ho2afroESBsGpAYSTRJf_2eI7ANJOkm23Q24xgjHX3nO0fngkhF_rhci5wRxcumYZroPdUko60q-Q5uGKFKMaktb2XZZ5a9skK7_5hbQiw3kC6xvDNSCZqTMvQtd40XQvNcqoZmzHLjwHITy12htOW6lgtCPpW7baPllmbZdifLfKs9e6vj2AisCa3KHSfxUI9n9JRcawELjZJOvRakjEcKnU7hcLVvVh-FMqr0C6t6ZQZ1AhIYENCuBfDo5nt0FAfrB8Lh83wDFM1Fm1YM0ccKopMvByLrkXwH_sRJk725LMg9I5LV4jsjOIos4Uooz2G4qcBms8WtYkQUI5H7Uu87L6AWEOKDYCgFJ0xKIUleKgphQ2VPwHzfMIUhBKCy5RzjCOQ_ijwVFcYR2WvdKAwo9wI-u25iIeQO7n7A_3n8_dsf-2_x1RD03OirmVJ0x0ghRU2qKnspLLspMOBCCqHhUlXf63kjxTeWHW7xUqbwfSMpV4WQtYKb64xWYI57cckLJlcQG6DyGsNDLbKmAdQwiBHbQIB6b3VLq5aZXYk-UC74Yy1aZUVn3exTnMu6qbq5rBKcXXKlZZuhV7-C1_9k90NpCMqTpoFoHxUtVxAALMcgw_tMgAgxjG6EuVzR5ubRcsPT7EayvMyoZjkqMlqGQid11zWtqlvwmJAd53UHcJL4okxEeueGTtIpQO-sjoECe3ItsznI7Ji-eWzQIhDtBzaYRWP_dPOYBZgIvTv6mYgUtKxYvui2efP5fHV9TlSb1qUmlKTtDgNbSI0ZNokz8G2bLjJRD_Z_HBWlUi3DIDAZUvKsavMuQzJJ1Z6kNLvTEoj06q81DJC8rZtjHNowlWyk2ElaEyhwbc24Vn0sQhGAb2DQgA1yzh6aSkiGwZZVlO_mx-xSc9hmfgcTackNz4zMBUJAtiFQq5tWLxSZP8QhLKvn6pFr-gAOQ3dW4Ly1Eq0El8HWG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ayhUjFe9_x1qSsj-kzJD8769x6K3OOLEDje4JkH9m70Lglfm6RM3zMQObzyAeuOr4rwQWBmjF4ai_8BG371zA>53665</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [InferAddressSpaces] asserts when adding `addrspacecast` between inttoptr/ptrtoint pair
        </td>
    </tr>

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

    <tr>
      <th>Assignees</th>
      <td>
            darkbuck,
            arsenm
      </td>
    </tr>

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

<pre>
    Code snippet that causing the assert:
```
  %r2 = load i64, i64 addrspace(5)* %"$$arg_ptr_acc_grid_t30_t88", align 8
  %r3 = inttoptr i64 %r2 to i8 addrspace(1)*
  %r4 = addrspacecast i8 addrspace(1)* %r3 to i8*
  %r5 = ptrtoint i8* %r4 to i64
  %r6 = inttoptr i64 %r5 to double**
  %r7 = load double*, double** %r6, align 8
```
If we remove `%r4` and replace the use of `%r4` with %r3, then the assertion error disappear

Test case running on Godbolt: https://godbolt.org/z/E8vjKhj8M

Error message from llc:
```
llc: /root/llvm-project/llvm/lib/Transforms/Scalar/InferAddressSpaces.cpp:658: llvm::Value* {anonymous}::InferAddressSpacesImpl::cloneInstructionWithNewAddressSpace(llvm::Instruction*, unsigned int, const ValueToValueMapTy&, const PredicatedAddrSpaceMapTy&, llvm::SmallVectorImpl<const llvm::Use*>*) const: Assertion `Src->getType()->getPointerAddressSpace() == NewAddrSpace' 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 Pass Manager' on module '<source>'.
2.      Running pass 'Infer address spaces' on function '@func'
 #0 0x00005628b552e29f PrintStackTraceSignalHandler(void*) Signals.cpp:0:0
 #1 0x00005628b552ba5d SignalHandler(int) Signals.cpp:0:0
 #2 0x00007fae42ceb3c0 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x153c0)
 #3 0x00007fae427ba18b raise (/lib/x86_64-linux-gnu/libc.so.6+0x4618b)
 #4 0x00007fae42799859 abort (/lib/x86_64-linux-gnu/libc.so.6+0x25859)
 #5 0x00007fae42799729 (/lib/x86_64-linux-gnu/libc.so.6+0x25729)
 #6 0x00007fae427aaf36 (/lib/x86_64-linux-gnu/libc.so.6+0x36f36)
 #7 0x00005628b4f0bf4e (anonymous namespace)::InferAddressSpacesImpl::cloneInstructionWithNewAddressSpace(llvm::Instruction*, unsigned int, llvm::ValueMap<llvm::Value const*, llvm::WeakTrackingVH, llvm::ValueMapConfig<llvm::Value const*, llvm::sys::SmartMutex<false> > > const&, llvm::DenseMap<std::pair<llvm::Value const*, llvm::Value const*>, unsigned int, llvm::DenseMapInfo<std::pair<llvm::Value const*, llvm::Value const*>, void>, llvm::detail::DenseMapPair<std::pair<llvm::Value const*, llvm::Value const*>, unsigned int> > const&, llvm::SmallVectorImpl<llvm::Use const*>*) const (.isra.413) InferAddressSpaces.cpp:0:0
 #8 0x00005628b4f0ed6c (anonymous namespace)::InferAddressSpacesImpl::run(llvm::Function&) (.constprop.518) InferAddressSpaces.cpp:0:0
 #9 0x00005628b4f110f6 (anonymous namespace)::InferAddressSpaces::runOnFunction(llvm::Function&) InferAddressSpaces.cpp:0:0
#10 0x00005628b4d32816 llvm::FPPassManager::runOnFunction(llvm::Function&) (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x2932816)
#11 0x00005628b4d32b69 llvm::FPPassManager::runOnModule(llvm::Module&) (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x2932b69)
#12 0x00005628b4d334a9 llvm::legacy::PassManagerImpl::run(llvm::Module&) (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0x29334a9)
#13 0x00005628b2f56578 compileModule(char**, llvm::LLVMContext&) llc.cpp:0:0
#14 0x00005628b2e94adf main (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0xa94adf)
#15 0x00007fae4279b0b3 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b3)
#16 0x00005628b2f4d03a _start (/opt/compiler-explorer/clang-assertions-trunk/bin/llc+0xb4d03a)
Compiler returned: 139
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzFWFtzmzgU_jX4RWMP5s6DH1wnaTPb7Ho2afroEULYNCAxkmiS_vo9R2AbnKSbbNNZjw1GOvrOp6Nzkchk_rhYyZwTLcqm4YaYHTWE0VaXYgsPnFCtuTKOv3TcM8ddOpHbf-0jIY4XKo84_hmpJM1JGQWOt8IboXmudEMZd7wkdLzU8ZYo7Xie44FQQNV20xi1oYxttqrMN8Z3NyZJrMCK0KrcCpIM9fhWTymMkTDQKunUG0nKZKRw3ikcjg7s6IMQo9q8MKpXZlFPQEILAtqNBB5df4-O4jD7gXD0PN8QRXPZZhVH9LGC-GjLgchqJN-BPzHSydpcFuSeE8Vr-Z0TbEWWcCdU5NDcVDBnu8St5kQWI5H70uw6K6AWEBIDZyilIFwpqUheagpuQ1VPwF5vuEYXAlDVCoF-BPIfZZ7JCv2I7IxpNDqUdwHfbdcxk2oLTz_gd558__bH7ltyNQQ9t_pqrjXdclIoWZOqYi-5ZdcFE7hQUhq4VdX3etoo-Y2z_SPeygyuN4oKXUhVa3i4ZrSC6XgXl6Lgagm-ASqv0T30jDUNoEZhgtgWAtT7y1tatdyuSvyBCikea9lqJz7rep_iXNZN1fWxSgp-KbRRLUOrfgWr_8nvh9LglEdNA9HeK1qhwQF4jk6Gz0yCCLGMbqS9XdHm5tHxomPvWvG8ZNTwHBVZLUOho7rrmlbVLVhMqo7zqgM4SnzR1iP9c0sn7RSgdZYHR4E1uVZsCjJbbm4eG5wRiPYNa4yisX26fowCDITeHH1PTApaVjyfdcu8_ny-vD4nus3q0hBKsnaLji2VwQg78TOwbZvNmKwH6z_2ilLrlqMT2AgpBavavIs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