Skip to content

Commit dfa4f02

Browse files
authored
[SYCL][LIBCLC] Add subgroup builtins for AMDGCN (#4208)
This patch brings in support for the following builtins: * `__spirv_SubgroupMaxSize` * `__spirv_NumSubgroups` * `__spirv_SubgroupId` * `__spirv_SubgroupSize` The implementation follows that of HIP.
1 parent 98a339d commit dfa4f02

File tree

5 files changed

+92
-0
lines changed

5 files changed

+92
-0
lines changed

libclc/amdgcn-amdhsa/libspirv/SOURCES

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@ math/sin.cl
77
math/sqrt.cl
88
math/atan.cl
99
math/cbrt.cl
10+
workitem/get_max_sub_group_size.cl
11+
workitem/get_num_sub_groups.cl
12+
workitem/get_sub_group_id.cl
13+
workitem/get_sub_group_size.cl
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
// FIXME: Remove the following workaround once the clang change is released.
12+
// This is for backward compatibility with older clang which does not define
13+
// __AMDGCN_WAVEFRONT_SIZE. It does not consider -mwavefrontsize64.
14+
// See:
15+
// https://github.com/intel/llvm/blob/sycl/clang/lib/Basic/Targets/AMDGPU.h#L414
16+
// and:
17+
// https://github.com/intel/llvm/blob/sycl/clang/lib/Basic/Targets/AMDGPU.cpp#L421
18+
#ifndef __AMDGCN_WAVEFRONT_SIZE
19+
#if __gfx1010__ || __gfx1011__ || __gfx1012__ || __gfx1030__ || __gfx1031__
20+
#define __AMDGCN_WAVEFRONT_SIZE 32
21+
#else
22+
#define __AMDGCN_WAVEFRONT_SIZE 64
23+
#endif
24+
#endif
25+
26+
_CLC_DEF _CLC_OVERLOAD uint __spirv_SubgroupMaxSize() {
27+
return __AMDGCN_WAVEFRONT_SIZE;
28+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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_DEF _CLC_OVERLOAD uint __spirv_NumSubgroups() {
12+
size_t size_x = __spirv_WorkgroupSize_x();
13+
size_t size_y = __spirv_WorkgroupSize_y();
14+
size_t size_z = __spirv_WorkgroupSize_z();
15+
uint sg_size = __spirv_SubgroupMaxSize();
16+
size_t linear_size = size_z * size_y * size_x;
17+
return (uint)((linear_size + sg_size - 1) / sg_size);
18+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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_DEF _CLC_OVERLOAD uint __spirv_SubgroupId() {
12+
size_t id_x = __spirv_LocalInvocationId_x();
13+
size_t id_y = __spirv_LocalInvocationId_y();
14+
size_t id_z = __spirv_LocalInvocationId_z();
15+
size_t size_x = __spirv_WorkgroupSize_x();
16+
size_t size_y = __spirv_WorkgroupSize_y();
17+
size_t size_z = __spirv_WorkgroupSize_z();
18+
uint sg_size = __spirv_SubgroupMaxSize();
19+
return (id_z * size_y * size_x + id_y * size_x + id_x) / sg_size;
20+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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_DEF _CLC_OVERLOAD uint __spirv_SubgroupSize() {
12+
if (__spirv_SubgroupId() != __spirv_NumSubgroups() - 1) {
13+
return __spirv_SubgroupMaxSize();
14+
}
15+
size_t size_x = __spirv_WorkgroupSize_x();
16+
size_t size_y = __spirv_WorkgroupSize_y();
17+
size_t size_z = __spirv_WorkgroupSize_z();
18+
size_t linear_size = size_z * size_y * size_x;
19+
size_t uniform_groups = __spirv_NumSubgroups() - 1;
20+
size_t uniform_size = __spirv_SubgroupMaxSize() * uniform_groups;
21+
return linear_size - uniform_size;
22+
}

0 commit comments

Comments
 (0)