1 ; RUN: llc -O0 -mtriple=spirv32-unknown-unknown %s -o - | FileCheck %s --check-prefix=CHECK-SPIRV
3 ;; This test checks that the backend is capable to correctly translate
4 ;; sub_group_barrier built-in function [1] from cl_khr_subgroups extension into
5 ;; corresponding SPIR-V instruction.
7 ;; __kernel void test_barrier_const_flags() {
8 ;; work_group_barrier(CLK_LOCAL_MEM_FENCE);
9 ;; work_group_barrier(CLK_GLOBAL_MEM_FENCE);
10 ;; work_group_barrier(CLK_IMAGE_MEM_FENCE);
12 ;; work_group_barrier(CLK_LOCAL_MEM_FENCE | CLK_GLOBAL_MEM_FENCE);
13 ;; work_group_barrier(CLK_LOCAL_MEM_FENCE | CLK_IMAGE_MEM_FENCE);
14 ;; work_group_barrier(CLK_GLOBAL_MEM_FENCE | CLK_LOCAL_MEM_FENCE | CLK_IMAGE_MEM_FENCE);
16 ;; work_group_barrier(CLK_LOCAL_MEM_FENCE, memory_scope_work_item);
17 ;; work_group_barrier(CLK_LOCAL_MEM_FENCE, memory_scope_work_group);
18 ;; work_group_barrier(CLK_LOCAL_MEM_FENCE, memory_scope_device);
19 ;; work_group_barrier(CLK_LOCAL_MEM_FENCE, memory_scope_all_svm_devices);
20 ;; work_group_barrier(CLK_LOCAL_MEM_FENCE, memory_scope_sub_group);
22 ;; barrier should also work (preserved for backward compatibility)
23 ;; barrier(CLK_GLOBAL_MEM_FENCE);
26 ;; __kernel void test_barrier_non_const_flags(cl_mem_fence_flags flags, memory_scope scope) {
27 ;; FIXME: OpenCL spec doesn't require flags to be compile-time known
28 ;; work_group_barrier(flags);
29 ;; work_group_barrier(flags, scope);
32 ; CHECK-SPIRV: OpName %[[#TEST_CONST_FLAGS:]] "test_barrier_const_flags"
33 ; CHECK-SPIRV: %[[#UINT:]] = OpTypeInt 32 0
35 ;; In SPIR-V, barrier is represented as OpControlBarrier [2] and OpenCL
36 ;; cl_mem_fence_flags are represented as part of Memory Semantics [3], which
37 ;; also includes memory order constraints. The backend applies some default
38 ;; memory order for OpControlBarrier and therefore, constants below include a
39 ;; bit more information than original source
41 ;; 0x10 SequentiallyConsistent + 0x100 WorkgroupMemory
42 ; CHECK-SPIRV-DAG: %[[#LOCAL:]] = OpConstant %[[#UINT]] 272
43 ;; 0x10 SequentiallyConsistent + 0x200 CrossWorkgroupMemory
44 ; CHECK-SPIRV-DAG: %[[#GLOBAL:]] = OpConstant %[[#UINT]] 528
45 ;; 0x10 SequentiallyConsistent + 0x800 ImageMemory
46 ; CHECK-SPIRV-DAG: %[[#IMAGE:]] = OpConstant %[[#UINT]] 2064
47 ;; 0x10 SequentiallyConsistent + 0x100 WorkgroupMemory + 0x200 CrossWorkgroupMemory
48 ; CHECK-SPIRV-DAG: %[[#LOCAL_GLOBAL:]] = OpConstant %[[#UINT]] 784
49 ;; 0x10 SequentiallyConsistent + 0x100 WorkgroupMemory + 0x800 ImageMemory
50 ; CHECK-SPIRV-DAG: %[[#LOCAL_IMAGE:]] = OpConstant %[[#UINT]] 2320
51 ;; 0x10 SequentiallyConsistent + 0x100 WorkgroupMemory + 0x200 CrossWorkgroupMemory + 0x800 ImageMemory
52 ; CHECK-SPIRV-DAG: %[[#LOCAL_GLOBAL_IMAGE:]] = OpConstant %[[#UINT]] 2832
56 ; CHECK-SPIRV-DAG: %[[#SCOPE_WORK_GROUP:]] = OpConstant %[[#UINT]] 2
58 ; CHECK-SPIRV-DAG: %[[#SCOPE_INVOCATION:]] = OpConstant %[[#UINT]] 4
60 ; CHECK-SPIRV-DAG: %[[#SCOPE_DEVICE:]] = OpConstant %[[#UINT]] 1
62 ; CHECK-SPIRV-DAG: %[[#SCOPE_CROSS_DEVICE:]] = OpConstant %[[#UINT]] 0
64 ; CHECK-SPIRV-DAG: %[[#SCOPE_SUBGROUP:]] = OpConstant %[[#UINT]] 3
66 ; CHECK-SPIRV: %[[#TEST_CONST_FLAGS]] = OpFunction %[[#]]
67 ; CHECK-SPIRV: OpControlBarrier %[[#SCOPE_WORK_GROUP]] %[[#SCOPE_WORK_GROUP]] %[[#LOCAL]]
68 ; CHECK-SPIRV: OpControlBarrier %[[#SCOPE_WORK_GROUP]] %[[#SCOPE_WORK_GROUP]] %[[#GLOBAL]]
69 ; CHECK-SPIRV: OpControlBarrier %[[#SCOPE_WORK_GROUP]] %[[#SCOPE_WORK_GROUP]] %[[#IMAGE]]
70 ; CHECK-SPIRV: OpControlBarrier %[[#SCOPE_WORK_GROUP]] %[[#SCOPE_WORK_GROUP]] %[[#LOCAL_GLOBAL]]
71 ; CHECK-SPIRV: OpControlBarrier %[[#SCOPE_WORK_GROUP]] %[[#SCOPE_WORK_GROUP]] %[[#LOCAL_IMAGE]]
72 ; CHECK-SPIRV: OpControlBarrier %[[#SCOPE_WORK_GROUP]] %[[#SCOPE_WORK_GROUP]] %[[#LOCAL_GLOBAL_IMAGE]]
73 ; CHECK-SPIRV: OpControlBarrier %[[#SCOPE_WORK_GROUP]] %[[#SCOPE_INVOCATION]] %[[#LOCAL]]
74 ; CHECK-SPIRV: OpControlBarrier %[[#SCOPE_WORK_GROUP]] %[[#SCOPE_WORK_GROUP]] %[[#LOCAL]]
75 ; CHECK-SPIRV: OpControlBarrier %[[#SCOPE_WORK_GROUP]] %[[#SCOPE_DEVICE]] %[[#LOCAL]]
76 ; CHECK-SPIRV: OpControlBarrier %[[#SCOPE_WORK_GROUP]] %[[#SCOPE_CROSS_DEVICE]] %[[#LOCAL]]
77 ; CHECK-SPIRV: OpControlBarrier %[[#SCOPE_WORK_GROUP]] %[[#SCOPE_SUBGROUP]] %[[#LOCAL]]
78 ; CHECK-SPIRV: OpControlBarrier %[[#SCOPE_WORK_GROUP]] %[[#SCOPE_WORK_GROUP]] %[[#GLOBAL]]
80 define dso_local spir_kernel void @test_barrier_const_flags() local_unnamed_addr {
82 tail call spir_func void @_Z18work_group_barrierj(i32 noundef 1)
83 tail call spir_func void @_Z18work_group_barrierj(i32 noundef 2)
84 tail call spir_func void @_Z18work_group_barrierj(i32 noundef 4)
85 tail call spir_func void @_Z18work_group_barrierj(i32 noundef 3)
86 tail call spir_func void @_Z18work_group_barrierj(i32 noundef 5)
87 tail call spir_func void @_Z18work_group_barrierj(i32 noundef 7)
88 tail call spir_func void @_Z18work_group_barrierj12memory_scope(i32 noundef 1, i32 noundef 0)
89 tail call spir_func void @_Z18work_group_barrierj12memory_scope(i32 noundef 1, i32 noundef 1)
90 tail call spir_func void @_Z18work_group_barrierj12memory_scope(i32 noundef 1, i32 noundef 2)
91 tail call spir_func void @_Z18work_group_barrierj12memory_scope(i32 noundef 1, i32 noundef 3)
92 tail call spir_func void @_Z18work_group_barrierj12memory_scope(i32 noundef 1, i32 noundef 4)
93 tail call spir_func void @_Z7barrierj(i32 noundef 2)
97 declare spir_func void @_Z18work_group_barrierj(i32 noundef) local_unnamed_addr
99 declare spir_func void @_Z18work_group_barrierj12memory_scope(i32 noundef, i32 noundef) local_unnamed_addr
101 declare spir_func void @_Z7barrierj(i32 noundef) local_unnamed_addr
103 define dso_local spir_kernel void @test_barrier_non_const_flags(i32 noundef %flags, i32 noundef %scope) local_unnamed_addr {
109 ;; [1]: https://www.khronos.org/registry/OpenCL/sdk/2.0/docs/man/xhtml/work_group_barrier.html
110 ;; [2]: https://www.khronos.org/registry/spir-v/specs/unified1/SPIRV.html#OpControlBarrier
111 ;; [3]: https://www.khronos.org/registry/spir-v/specs/unified1/SPIRV.html#_a_id_memory_semantics__id_a_memory_semantics_lt_id_gt