Skip to content

[SYCL][LIBCLC] Add subgroup builtins for AMDGCN #4208

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions libclc/amdgcn-amdhsa/libspirv/SOURCES
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ math/sin.cl
math/sqrt.cl
math/atan.cl
math/cbrt.cl
workitem/get_max_sub_group_size.cl
workitem/get_num_sub_groups.cl
workitem/get_sub_group_id.cl
workitem/get_sub_group_size.cl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include <spirv/spirv.h>

// FIXME: Remove the following workaround once the clang change is released.
// This is for backward compatibility with older clang which does not define
// __AMDGCN_WAVEFRONT_SIZE. It does not consider -mwavefrontsize64.
// See:
// https://github.com/intel/llvm/blob/sycl/clang/lib/Basic/Targets/AMDGPU.h#L414
// and:
// https://github.com/intel/llvm/blob/sycl/clang/lib/Basic/Targets/AMDGPU.cpp#L421
#ifndef __AMDGCN_WAVEFRONT_SIZE
#if __gfx1010__ || __gfx1011__ || __gfx1012__ || __gfx1030__ || __gfx1031__
#define __AMDGCN_WAVEFRONT_SIZE 32
#else
#define __AMDGCN_WAVEFRONT_SIZE 64
#endif
#endif

_CLC_DEF _CLC_OVERLOAD uint __spirv_SubgroupMaxSize() {
return __AMDGCN_WAVEFRONT_SIZE;
}
18 changes: 18 additions & 0 deletions libclc/amdgcn-amdhsa/libspirv/workitem/get_num_sub_groups.cl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include <spirv/spirv.h>

_CLC_DEF _CLC_OVERLOAD uint __spirv_NumSubgroups() {
size_t size_x = __spirv_WorkgroupSize_x();
size_t size_y = __spirv_WorkgroupSize_y();
size_t size_z = __spirv_WorkgroupSize_z();
uint sg_size = __spirv_SubgroupMaxSize();
size_t linear_size = size_z * size_y * size_x;
return (uint)((linear_size + sg_size - 1) / sg_size);
}
20 changes: 20 additions & 0 deletions libclc/amdgcn-amdhsa/libspirv/workitem/get_sub_group_id.cl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include <spirv/spirv.h>

_CLC_DEF _CLC_OVERLOAD uint __spirv_SubgroupId() {
size_t id_x = __spirv_LocalInvocationId_x();
size_t id_y = __spirv_LocalInvocationId_y();
size_t id_z = __spirv_LocalInvocationId_z();
size_t size_x = __spirv_WorkgroupSize_x();
size_t size_y = __spirv_WorkgroupSize_y();
size_t size_z = __spirv_WorkgroupSize_z();
uint sg_size = __spirv_SubgroupMaxSize();
return (id_z * size_y * size_x + id_y * size_x + id_x) / sg_size;
}
22 changes: 22 additions & 0 deletions libclc/amdgcn-amdhsa/libspirv/workitem/get_sub_group_size.cl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include <spirv/spirv.h>

_CLC_DEF _CLC_OVERLOAD uint __spirv_SubgroupSize() {
if (__spirv_SubgroupId() != __spirv_NumSubgroups() - 1) {
return __spirv_SubgroupMaxSize();
}
size_t size_x = __spirv_WorkgroupSize_x();
size_t size_y = __spirv_WorkgroupSize_y();
size_t size_z = __spirv_WorkgroupSize_z();
size_t linear_size = size_z * size_y * size_x;
size_t uniform_groups = __spirv_NumSubgroups() - 1;
size_t uniform_size = __spirv_SubgroupMaxSize() * uniform_groups;
return linear_size - uniform_size;
}