| 1 |
// RUN: %clang_cc1 %s -triple spir -cl-std=CL1.2 -emit-llvm-bc -fdeclare-opencl-builtins -o %t.bc |
| 2 |
// RUN: llvm-spirv %t.bc -o %t.spv |
| 3 |
// RUN: spirv-val %t.spv |
| 4 |
// RUN: llvm-spirv %t.spv -to-text -o - | FileCheck %s --check-prefix=CHECK-SPIRV |
| 5 |
// RUN: llvm-spirv %t.spv -r --spirv-target-env=CL1.2 -o - | llvm-dis -o - | FileCheck %s --check-prefix=CHECK-LLVM |
| 6 |
|
| 7 |
// This test checks that the translator is capable to correctly translate |
| 8 |
// atomic_cmpxchg OpenCL C 1.2 built-in function [1] into corresponding SPIR-V |
| 9 |
// instruction and vice-versa. |
| 10 |
|
| 11 |
__kernel void test_atomic_cmpxchg(__global int *p, int cmp, int val) { |
| 12 |
atomic_cmpxchg(p, cmp, val); |
| 13 |
|
| 14 |
__global unsigned int *up = (__global unsigned int *)p; |
| 15 |
unsigned int ucmp = (unsigned int)cmp; |
| 16 |
unsigned int uval = (unsigned int)val; |
| 17 |
atomic_cmpxchg(up, ucmp, uval); |
| 18 |
} |
| 19 |
|
| 20 |
// CHECK-SPIRV: Name [[TEST:[0-9]+]] "test_atomic_cmpxchg" |
| 21 |
// CHECK-SPIRV-DAG: TypeInt [[UINT:[0-9]+]] 32 0 |
| 22 |
// CHECK-SPIRV-DAG: TypePointer [[UINT_PTR:[0-9]+]] 5 [[UINT]] |
| 23 |
// |
| 24 |
// In SPIR-V, atomic_cmpxchg is represented as OpAtomicCompareExchange [2], |
| 25 |
// which also includes memory scope and two memory semantic arguments. The |
| 26 |
// translator applies some default memory order for it and therefore, constants |
| 27 |
// below include a bit more information than original source |
| 28 |
// |
| 29 |
// 0x2 Workgroup |
| 30 |
// CHECK-SPIRV-DAG: Constant [[UINT]] [[WORKGROUP_SCOPE:[0-9]+]] 2 |
| 31 |
// |
| 32 |
// 0x0 Relaxed |
| 33 |
// TODO: do we need CrossWorkgroupMemory here as well? |
| 34 |
// CHECK-SPIRV-DAG: Constant [[UINT]] [[RELAXED:[0-9]+]] 0 |
| 35 |
// |
| 36 |
// CHECK-SPIRV: Function {{[0-9]+}} [[TEST]] |
| 37 |
// CHECK-SPIRV: FunctionParameter [[UINT_PTR]] [[PTR:[0-9]+]] |
| 38 |
// CHECK-SPIRV: FunctionParameter [[UINT]] [[CMP:[0-9]+]] |
| 39 |
// CHECK-SPIRV: FunctionParameter [[UINT]] [[VAL:[0-9]+]] |
| 40 |
// CHECK-SPIRV: AtomicCompareExchange [[UINT]] {{[0-9]+}} [[PTR]] [[WORKGROUP_SCOPE]] [[RELAXED]] [[RELAXED]] [[VAL]] [[CMP]] |
| 41 |
// CHECK-SPIRV: AtomicCompareExchange [[UINT]] {{[0-9]+}} [[PTR]] [[WORKGROUP_SCOPE]] [[RELAXED]] [[RELAXED]] [[VAL]] [[CMP]] |
| 42 |
// |
| 43 |
// |
| 44 |
// CHECK-LLVM-LABEL: define spir_kernel void @test_atomic_cmpxchg |
| 45 |
// CHECK-LLVM: call spir_func i32 @_Z14atomic_cmpxchgPU3AS1Viii( |
| 46 |
// TODO: is it an issue that we lost call to @_Z14atomic_cmpxchgPU3AS1jjj here? |
| 47 |
// CHECK-LLVM: call spir_func i32 @_Z14atomic_cmpxchgPU3AS1Viii( |
| 48 |
|
| 49 |
// References: |
| 50 |
// [1]: https://www.khronos.org/registry/OpenCL/sdk/2.0/docs/man/xhtml/atomic_cmpxchg.html |
| 51 |
// [2]: https://www.khronos.org/registry/spir-v/specs/unified1/SPIRV.html#OpAtomicCompareExchange |