xref: /llvm-project/llvm/test/CodeGen/SPIRV/transcoding/OpenCL/atomic_cmpxchg.ll (revision 0a443f13b49b3f392461a0bb60b0146cfc4607c7)
1; RUN: llc -O0 -mtriple=spirv32-unknown-unknown %s -o - | FileCheck %s --check-prefix=CHECK-SPIRV
2; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv64-unknown-unknown %s -o - -filetype=obj | spirv-val %}
3
4;; This test checks that the backend is capable to correctly translate
5;; atomic_cmpxchg OpenCL C 1.2 built-in function [1] into corresponding SPIR-V
6;; instruction.
7
8;; __kernel void test_atomic_cmpxchg(__global int *p, int cmp, int val) {
9;;   atomic_cmpxchg(p, cmp, val);
10;;
11;;   __global unsigned int *up = (__global unsigned int *)p;
12;;   unsigned int ucmp = (unsigned int)cmp;
13;;   unsigned int uval = (unsigned int)val;
14;;   atomic_cmpxchg(up, ucmp, uval);
15;; }
16
17; CHECK-SPIRV:     OpName %[[#TEST:]] "test_atomic_cmpxchg"
18; CHECK-SPIRV-DAG: %[[#UINT:]] = OpTypeInt 32 0
19; CHECK-SPIRV-DAG: %[[#UINT_PTR:]] = OpTypePointer CrossWorkgroup %[[#UINT]]
20
21;; In SPIR-V, atomic_cmpxchg is represented as OpAtomicCompareExchange [2],
22;; which also includes memory scope and two memory semantic arguments. The
23;; backend applies some default memory order for it and therefore, constants
24;; below include a bit more information than original source
25
26;; 0x2 Workgroup
27; CHECK-SPIRV-DAG: %[[#WORKGROUP_SCOPE:]] = OpConstant %[[#UINT]] 2
28
29;; 0x0 Relaxed
30;; TODO: do we need CrossWorkgroupMemory here as well?
31; CHECK-SPIRV-DAG: %[[#RELAXED:]] = OpConstant %[[#UINT]] 0
32
33; CHECK-SPIRV:     %[[#TEST]] = OpFunction %[[#]]
34; CHECK-SPIRV:     %[[#PTR:]] = OpFunctionParameter %[[#UINT_PTR]]
35; CHECK-SPIRV:     %[[#CMP:]] = OpFunctionParameter %[[#UINT]]
36; CHECK-SPIRV:     %[[#VAL:]] = OpFunctionParameter %[[#UINT]]
37; CHECK-SPIRV:     %[[#]] = OpAtomicCompareExchange %[[#UINT]] %[[#PTR]] %[[#WORKGROUP_SCOPE]] %[[#RELAXED]] %[[#RELAXED]] %[[#VAL]] %[[#CMP]]
38; CHECK-SPIRV:     %[[#]] = OpAtomicCompareExchange %[[#UINT]] %[[#PTR]] %[[#WORKGROUP_SCOPE]] %[[#RELAXED]] %[[#RELAXED]] %[[#VAL]] %[[#CMP]]
39
40define dso_local spir_kernel void @test_atomic_cmpxchg(i32 addrspace(1)* noundef %p, i32 noundef %cmp, i32 noundef %val) local_unnamed_addr {
41entry:
42  %call = tail call spir_func i32 @_Z14atomic_cmpxchgPU3AS1Viii(i32 addrspace(1)* noundef %p, i32 noundef %cmp, i32 noundef %val)
43  %call1 = tail call spir_func i32 @_Z14atomic_cmpxchgPU3AS1Vjjj(i32 addrspace(1)* noundef %p, i32 noundef %cmp, i32 noundef %val)
44  ret void
45}
46
47declare spir_func i32 @_Z14atomic_cmpxchgPU3AS1Viii(i32 addrspace(1)* noundef, i32 noundef, i32 noundef) local_unnamed_addr
48
49declare spir_func i32 @_Z14atomic_cmpxchgPU3AS1Vjjj(i32 addrspace(1)* noundef, i32 noundef, i32 noundef) local_unnamed_addr
50
51;; References:
52;; [1]: https://www.khronos.org/registry/OpenCL/sdk/2.0/docs/man/xhtml/atomic_cmpxchg.html
53;; [2]: https://www.khronos.org/registry/spir-v/specs/unified1/SPIRV.html#OpAtomicCompareExchange
54