| 1 |
// RUN: %clang_cc1 %s -triple spir -cl-std=CL1.2 -fdeclare-opencl-builtins -finclude-default-header -emit-llvm-bc -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 -emit-opaque-pointers --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 |
// mem_fence OpenCL C 1.2 built-in function [1] into corresponding SPIR-V |
| 9 |
// instruction and vice-versa. |
| 10 |
|
| 11 |
// Strictly speaking, this flag is not supported by mem_fence in OpenCL 1.2 |
| 12 |
#define CLK_IMAGE_MEM_FENCE 0x04 |
| 13 |
|
| 14 |
__kernel void test_mem_fence_const_flags() { |
| 15 |
mem_fence(CLK_LOCAL_MEM_FENCE); |
| 16 |
mem_fence(CLK_GLOBAL_MEM_FENCE); |
| 17 |
mem_fence(CLK_IMAGE_MEM_FENCE); |
| 18 |
|
| 19 |
mem_fence(CLK_LOCAL_MEM_FENCE | CLK_GLOBAL_MEM_FENCE); |
| 20 |
mem_fence(CLK_LOCAL_MEM_FENCE | CLK_IMAGE_MEM_FENCE); |
| 21 |
mem_fence(CLK_GLOBAL_MEM_FENCE | CLK_LOCAL_MEM_FENCE | CLK_IMAGE_MEM_FENCE); |
| 22 |
|
| 23 |
read_mem_fence(CLK_LOCAL_MEM_FENCE); |
| 24 |
read_mem_fence(CLK_GLOBAL_MEM_FENCE); |
| 25 |
read_mem_fence(CLK_LOCAL_MEM_FENCE | CLK_GLOBAL_MEM_FENCE); |
| 26 |
|
| 27 |
write_mem_fence(CLK_LOCAL_MEM_FENCE); |
| 28 |
write_mem_fence(CLK_GLOBAL_MEM_FENCE); |
| 29 |
write_mem_fence(CLK_LOCAL_MEM_FENCE | CLK_GLOBAL_MEM_FENCE); |
| 30 |
} |
| 31 |
|
| 32 |
__kernel void test_mem_fence_non_const_flags(cl_mem_fence_flags flags) { |
| 33 |
// FIXME: OpenCL spec doesn't require flags to be compile-time known |
| 34 |
// mem_fence(flags); |
| 35 |
} |
| 36 |
|
| 37 |
// CHECK-SPIRV: Name [[TEST_CONST_FLAGS:[0-9]+]] "test_mem_fence_const_flags" |
| 38 |
// CHECK-SPIRV: TypeInt [[UINT:[0-9]+]] 32 0 |
| 39 |
// |
| 40 |
// In SPIR-V, mem_fence is represented as OpMemoryBarrier [2] and OpenCL |
| 41 |
// cl_mem_fence_flags are represented as part of Memory Semantics [3], which |
| 42 |
// also includes memory order constraints. The translator applies some default |
| 43 |
// memory order for OpMemoryBarrier and therefore, constants below include a |
| 44 |
// bit more information than original source |
| 45 |
// |
| 46 |
// 0x2 Workgroup |
| 47 |
// CHECK-SPIRV-DAG: Constant [[UINT]] [[WG:[0-9]+]] 2 |
| 48 |
// |
| 49 |
// 0x2 Acquire + 0x100 WorkgroupMemory |
| 50 |
// CHECK-SPIRV-DAG: Constant [[UINT]] [[ACQ_LOCAL:[0-9]+]] 258 |
| 51 |
// 0x2 Acquire + 0x200 CrossWorkgroupMemory |
| 52 |
// CHECK-SPIRV-DAG: Constant [[UINT]] [[ACQ_GLOBAL:[0-9]+]] 514 |
| 53 |
// 0x2 Acquire + 0x100 WorkgroupMemory + 0x200 CrossWorkgroupMemory |
| 54 |
// CHECK-SPIRV-DAG: Constant [[UINT]] [[ACQ_LOCAL_GLOBAL:[0-9]+]] 770 |
| 55 |
// |
| 56 |
// 0x4 Release + 0x100 WorkgroupMemory |
| 57 |
// CHECK-SPIRV-DAG: Constant [[UINT]] [[REL_LOCAL:[0-9]+]] 260 |
| 58 |
// 0x4 Release + 0x200 CrossWorkgroupMemory |
| 59 |
// CHECK-SPIRV-DAG: Constant [[UINT]] [[REL_GLOBAL:[0-9]+]] 516 |
| 60 |
// 0x4 Release + 0x100 WorkgroupMemory + 0x200 CrossWorkgroupMemory |
| 61 |
// CHECK-SPIRV-DAG: Constant [[UINT]] [[REL_LOCAL_GLOBAL:[0-9]+]] 772 |
| 62 |
// |
| 63 |
// 0x8 AcquireRelease + 0x100 WorkgroupMemory |
| 64 |
// CHECK-SPIRV-DAG: Constant [[UINT]] [[ACQ_REL_LOCAL:[0-9]+]] 264 |
| 65 |
// 0x8 AcquireRelease + 0x200 CrossWorkgroupMemory |
| 66 |
// CHECK-SPIRV-DAG: Constant [[UINT]] [[ACQ_REL_GLOBAL:[0-9]+]] 520 |
| 67 |
// 0x8 AcquireRelease + 0x800 ImageMemory |
| 68 |
// CHECK-SPIRV-DAG: Constant [[UINT]] [[ACQ_REL_IMAGE:[0-9]+]] 2056 |
| 69 |
// 0x8 AcquireRelease + 0x100 WorkgroupMemory + 0x200 CrossWorkgroupMemory |
| 70 |
// CHECK-SPIRV-DAG: Constant [[UINT]] [[ACQ_REL_LOCAL_GLOBAL:[0-9]+]] 776 |
| 71 |
// 0x8 AcquireRelease + 0x100 WorkgroupMemory + 0x800 ImageMemory |
| 72 |
// CHECK-SPIRV-DAG: Constant [[UINT]] [[ACQ_REL_LOCAL_IMAGE:[0-9]+]] 2312 |
| 73 |
// 0x8 AcquireRelease + 0x100 WorkgroupMemory + 0x200 CrossWorkgroupMemory + 0x800 ImageMemory |
| 74 |
// CHECK-SPIRV-DAG: Constant [[UINT]] [[ACQ_REL_LOCAL_GLOBAL_IMAGE:[0-9]+]] 2824 |
| 75 |
// |
| 76 |
// CHECK-SPIRV: Function {{[0-9]+}} [[TEST_CONST_FLAGS]] |
| 77 |
// CHECK-SPIRV: MemoryBarrier [[WG]] [[ACQ_REL_LOCAL]] |
| 78 |
// CHECK-SPIRV: MemoryBarrier [[WG]] [[ACQ_REL_GLOBAL]] |
| 79 |
// CHECK-SPIRV: MemoryBarrier [[WG]] [[ACQ_REL_IMAGE]] |
| 80 |
// CHECK-SPIRV: MemoryBarrier [[WG]] [[ACQ_REL_LOCAL_GLOBAL]] |
| 81 |
// CHECK-SPIRV: MemoryBarrier [[WG]] [[ACQ_REL_LOCAL_IMAGE]] |
| 82 |
// CHECK-SPIRV: MemoryBarrier [[WG]] [[ACQ_REL_LOCAL_GLOBAL_IMAGE]] |
| 83 |
// CHECK-SPIRV: MemoryBarrier [[WG]] [[ACQ_LOCAL]] |
| 84 |
// CHECK-SPIRV: MemoryBarrier [[WG]] [[ACQ_GLOBAL]] |
| 85 |
// CHECK-SPIRV: MemoryBarrier [[WG]] [[ACQ_LOCAL_GLOBAL]] |
| 86 |
// CHECK-SPIRV: MemoryBarrier [[WG]] [[REL_LOCAL]] |
| 87 |
// CHECK-SPIRV: MemoryBarrier [[WG]] [[REL_GLOBAL]] |
| 88 |
// CHECK-SPIRV: MemoryBarrier [[WG]] [[REL_LOCAL_GLOBAL]] |
| 89 |
// |
| 90 |
// CHECK-LLVM-LABEL: define spir_kernel void @test_mem_fence_const_flags |
| 91 |
// CHECK-LLVM: call spir_func void @_Z9mem_fencej(i32 1) |
| 92 |
// CHECK-LLVM: call spir_func void @_Z9mem_fencej(i32 2) |
| 93 |
// CHECK-LLVM: call spir_func void @_Z9mem_fencej(i32 4) |
| 94 |
// CHECK-LLVM: call spir_func void @_Z9mem_fencej(i32 3) |
| 95 |
// CHECK-LLVM: call spir_func void @_Z9mem_fencej(i32 5) |
| 96 |
// CHECK-LLVM: call spir_func void @_Z9mem_fencej(i32 7) |
| 97 |
// CHECK-LLVM: call spir_func void @_Z9mem_fencej(i32 1) |
| 98 |
// CHECK-LLVM: call spir_func void @_Z9mem_fencej(i32 2) |
| 99 |
// CHECK-LLVM: call spir_func void @_Z9mem_fencej(i32 3) |
| 100 |
// CHECK-LLVM: call spir_func void @_Z9mem_fencej(i32 1) |
| 101 |
// CHECK-LLVM: call spir_func void @_Z9mem_fencej(i32 2) |
| 102 |
// CHECK-LLVM: call spir_func void @_Z9mem_fencej(i32 3) |
| 103 |
|
| 104 |
// References: |
| 105 |
// [1]: https://www.khronos.org/registry/OpenCL/sdk/1.2/docs/man/xhtml/mem_fence.html |
| 106 |
// [2]: https://www.khronos.org/registry/spir-v/specs/unified1/SPIRV.html#OpMemoryBarrier |
| 107 |
// [3]: https://www.khronos.org/registry/spir-v/specs/unified1/SPIRV.html#_a_id_memory_semantics__id_a_memory_semantics_lt_id_gt |