Skip to content

Commit f7aa2bf

Browse files
authored
[SYCL][LIBCLC] Add memory/control barrier, sqrt, sin and cos builtins for AMDGCN (#4091)
1 parent 29f3a92 commit f7aa2bf

File tree

8 files changed

+105
-31
lines changed

8 files changed

+105
-31
lines changed

libclc/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,10 @@ foreach( t ${LIBCLC_TARGETS_TO_BUILD} )
343343
set( opt_flags -O3 )
344344
endif()
345345

346+
# Enable SPIR-V builtin function declarations, so they don't
347+
# have to be explicity declared in the soruce.
348+
list( APPEND flags -Xclang -fdeclare-spirv-builtins)
349+
346350
add_libclc_builtin_set(libspirv-${arch_suffix}
347351
TRIPLE ${t}
348352
TARGET_ENV libspirv

libclc/amdgcn-amdhsa/libspirv/SOURCES

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
workitem/get_global_size.cl
22
workitem/get_local_size.cl
33
workitem/get_num_groups.cl
4+
synchronization/barrier.cl
5+
math/cos.cl
6+
math/sin.cl
7+
math/sqrt.cl
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include <spirv/spirv.h>
10+
11+
_CLC_OVERLOAD _CLC_DECL _CLC_CONSTFN __clc_fp32_t
12+
__spirv_ocl_cos(__clc_fp32_t In) {
13+
return __builtin_amdgcn_cosf(In);
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include <spirv/spirv.h>
10+
11+
_CLC_OVERLOAD _CLC_DECL _CLC_CONSTFN __clc_fp32_t
12+
__spirv_ocl_sin(__clc_fp32_t In) {
13+
return __builtin_amdgcn_sinf(In);
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include <spirv/spirv.h>
10+
11+
_CLC_OVERLOAD _CLC_DECL _CLC_CONSTFN __clc_fp32_t
12+
__spirv_ocl_sqrt(__clc_fp32_t In) {
13+
return __builtin_amdgcn_sqrtf(In);
14+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include <spirv/spirv.h>
10+
11+
void __clc_amdgcn_s_waitcnt(unsigned flags);
12+
13+
// s_waitcnt takes 16bit argument with a combined number of maximum allowed
14+
// pending operations:
15+
// [12:8] LGKM -- LDS, GDS, Konstant (SMRD), Messages
16+
// [7] -- undefined
17+
// [6:4] -- exports, GDS, and mem write
18+
// [3:0] -- vector memory operations
19+
20+
// Newer clang supports __builtin_amdgcn_s_waitcnt
21+
#if __clang_major__ >= 5
22+
#define __waitcnt(x) __builtin_amdgcn_s_waitcnt(x)
23+
#else
24+
#define __waitcnt(x) __clc_amdgcn_s_waitcnt(x)
25+
_CLC_DEF void __clc_amdgcn_s_waitcnt(unsigned) __asm("llvm.amdgcn.s.waitcnt");
26+
#endif
27+
28+
_CLC_DEF _CLC_OVERLOAD void __mem_fence(cl_mem_fence_flags flags) {
29+
if (flags & CLK_GLOBAL_MEM_FENCE) {
30+
// scalar loads are counted with LGKM but we don't know whether
31+
// the compiler turned any loads to scalar
32+
__waitcnt(0);
33+
} else if (flags & CLK_LOCAL_MEM_FENCE)
34+
__waitcnt(0xff); // LGKM is [12:8]
35+
}
36+
#undef __waitcnt
37+
38+
_CLC_OVERLOAD _CLC_DEF void __spirv_MemoryBarrier(unsigned int memory,
39+
unsigned int semantics) {
40+
__mem_fence(memory);
41+
}
42+
43+
_CLC_OVERLOAD _CLC_DEF _CLC_CONVERGENT void
44+
__spirv_ControlBarrier(unsigned int scope, unsigned int memory,
45+
unsigned int semantics) {
46+
if (semantics) {
47+
__mem_fence(memory);
48+
}
49+
__builtin_amdgcn_s_barrier();
50+
}

libclc/amdgcn/lib/mem_fence/fence.cl

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,10 @@
11
#include <clc/clc.h>
22

3-
void __clc_amdgcn_s_waitcnt(unsigned flags);
4-
5-
// s_waitcnt takes 16bit argument with a combined number of maximum allowed
6-
// pending operations:
7-
// [12:8] LGKM -- LDS, GDS, Konstant (SMRD), Messages
8-
// [7] -- undefined
9-
// [6:4] -- exports, GDS, and mem write
10-
// [3:0] -- vector memory operations
11-
12-
// Newer clang supports __builtin_amdgcn_s_waitcnt
13-
#if __clang_major__ >= 5
14-
# define __waitcnt(x) __builtin_amdgcn_s_waitcnt(x)
15-
#else
16-
# define __waitcnt(x) __clc_amdgcn_s_waitcnt(x)
17-
_CLC_DEF void __clc_amdgcn_s_waitcnt(unsigned) __asm("llvm.amdgcn.s.waitcnt");
18-
#endif
19-
20-
_CLC_DEF _CLC_OVERLOAD void mem_fence(cl_mem_fence_flags flags) {
21-
if (flags & CLK_GLOBAL_MEM_FENCE) {
22-
// scalar loads are counted with LGKM but we don't know whether
23-
// the compiler turned any loads to scalar
24-
__waitcnt(0);
25-
} else if (flags & CLK_LOCAL_MEM_FENCE)
26-
__waitcnt(0xff); // LGKM is [12:8]
27-
}
28-
#undef __waitcnt
29-
303
// We don't have separate mechanism for read and write fences
314
_CLC_DEF _CLC_OVERLOAD void read_mem_fence(cl_mem_fence_flags flags) {
32-
mem_fence(flags);
5+
__spirv_MemoryBarrier(flags, 1);
336
}
347

358
_CLC_DEF _CLC_OVERLOAD void write_mem_fence(cl_mem_fence_flags flags) {
36-
mem_fence(flags);
9+
__spirv_MemoryBarrier(flags, 1);
3710
}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <clc/clc.h>
22

33
_CLC_DEF _CLC_OVERLOAD void barrier(cl_mem_fence_flags flags) {
4-
mem_fence(flags);
5-
__builtin_amdgcn_s_barrier();
4+
// Call spir-v implementation of the barrier.
5+
// Set semantics to not None, so it performs mem fence.
6+
__spirv_ControlBarrier(0, flags, 1);
67
}

0 commit comments

Comments
 (0)