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