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

    <tr>
        <th>Summary</th>
        <td>
            [SPIR-V] Incorrect pointer type is deduced for OpAtomic* instructions when a pointer argument is created by an `inttoptr .. to` instruction
        </td>
    </tr>

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

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

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

<pre>
    A GPUToLLVMSPV user of the SPIR-V backend reports about a case when incorrect pointer type is deduced for OpAtomic* instructions. The reproducer is

```
define dso_local spir_func void @test4(i64 noundef %arg, float %val) local_unnamed_addr {
entry:
  %ptr1 = inttoptr i64 %arg to ptr addrspace(1)
  %v1 = atomicrmw fadd ptr addrspace(1) %ptr1, float %val seq_cst, align 4
  ret void
}
```

while triaging shows that the problem may be also reproduced for a wider set of instructions and SPIR-V op-codes, for example:

```
define dso_local spir_func void @test4(i64 noundef %arg, float %val) local_unnamed_addr {
entry:
  %ptr1 = inttoptr i64 %arg to ptr addrspace(1)
  %v1 = atomicrmw fadd ptr addrspace(1) %ptr1, float %val seq_cst, align 4
  %ptr2 = inttoptr i64 %arg to float addrspace(1)*
  %v2 = atomicrmw fsub ptr addrspace(1) %ptr2, float %val seq_cst, align 4
  %ptr3 = inttoptr i64 %arg to float addrspace(1)*
  %v3 = tail call spir_func float @_Z21__spirv_AtomicFAddEXT(ptr addrspace(1) %ptr3, i32 1, i32 16, float %val)
  %ptr4 = inttoptr i64 %arg to float addrspace(1)*
  %v4 = tail call spir_func float @_Z25atomic_fetch_add_explicitPU3AS1VU7_Atomicff12memory_order(ptr addrspace(1) %ptr4, float %val, i32 0)
  %ptr5 = inttoptr i64 %arg to float addrspace(1)*
  %v5 = tail call spir_func float @_Z25atomic_fetch_sub_explicitPU3AS1VU7_Atomicff12memory_order(ptr addrspace(1) %ptr5, float %val, i32 0)
  ret void
}

declare dso_local spir_func float @_Z21__spirv_AtomicFAddEXT(ptr addrspace(1), i32, i32, float)
declare spir_func float @_Z25atomic_fetch_add_explicitPU3AS1VU7_Atomicff12memory_order(ptr addrspace(1), float, i32)
declare spir_func float @_Z25atomic_fetch_sub_explicitPU3AS1VU7_Atomicff12memory_order(ptr addrspace(1), float, i32)
```

and also for OpAtomicLoad/OpAtomicStore:

```
define spir_func void @test_types1(ptr addrspace(1) %ptr, float %val) {
entry:
  %r = call spir_func float @atomic_load(ptr addrspace(1) %ptr)
  ret void
}

define spir_func void @test_types2(ptr addrspace(1) %ptr, float %val) {
entry:
  call spir_func void @atomic_store(ptr addrspace(1) %ptr, float %val)
  ret void
}

define spir_func void @test_types3(i64 noundef %arg, float %val) {
entry:
  %ptr1 = inttoptr i64 %arg to float addrspace(1)*
  %r = call spir_func float @atomic_load(ptr addrspace(1) %ptr1)
  ret void
}

define spir_func void @test_types4(i64 noundef %arg, float %val) {
entry:
  %ptr2 = inttoptr i64 %arg to float addrspace(1)*
  call spir_func void @atomic_store(ptr addrspace(1) %ptr2, float %val)
  ret void
}

declare spir_func float @atomic_load(ptr addrspace(1))
declare spir_func void @atomic_store(ptr addrspace(1), float)
```

Incorrect pointer type is deduced for OpAtomic* instructions when a pointer argument is created by an `inttoptr .. to` instruction, with a root cause is that information about pointee type is ignored unless this argument is a pointer as well. A solution could be to account for a pointee type using the Value argument type of the corresponding LLVM IR instruction/SPIR-V op-code, depending on the context of the case.

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