Skip to content
Open
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
1 change: 1 addition & 0 deletions libclc/libspirv/lib/amdgpu/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ libclc_add_sources(${LIBCLC_LIBSPIRV_TARGET} FILES
group/group_ballot.cl
group/collectives.cl
group/collectives_helpers.cl
group/group_shuffle.cl
conversion/GenericCastToPtrExplicit.cl
synchronization/barrier.cl
images/image_common.cl
Expand Down
70 changes: 70 additions & 0 deletions libclc/libspirv/lib/amdgpu/group/group_shuffle.cl
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//===----------------------------------------------------------------------===//
//
// 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 <libspirv/spirv.h>

#pragma OPENCL EXTENSION cl_khr_fp16 : enable
#pragma OPENCL EXTENSION cl_khr_fp64 : enable

// The __spirv_GroupNonUniform*Shuffle* builtins are emitted directly by the
// SYCL headers (see sycl/detail/spirv.hpp) for sub-group shuffle, permute and
// scan operations, and are relied upon by oneDPL work-group algorithms. Unlike
// SPIR-V targets, the amdgcn target has no runtime translation for these
// instructions, so they must be provided by the device library.
//
// The SYCL headers scalarize every shuffle before reaching the intrinsic
// (vectors and marrays are handled element-wise, and bitcast/generic shuffles
// are lowered onto integer scalars), so only scalar overloads are required
// here - mirroring the scalar-only __spirv_GroupBroadcast definitions in
// group/collectives.cl.
//
// Each operation maps directly onto the corresponding, already validated,
// __spirv_SubgroupShuffle*INTEL primitive (see misc/sub_group_shuffle.cl),
// which lowers to the hardware ds_bpermute wavefront shuffle. The Shuffle Up
// and Down primitives take a pair of (previous/current) or (current/next)
// operands; for the single-operand SPIR-V form we pass the same value for both,
// which is correct within a single sub-group and preserves the SPIR-V contract
// that out-of-range indices produce an undefined result.
// The scope operand is unused: the delegated __spirv_SubgroupShuffle*INTEL
// primitives operate at sub-group (wavefront) granularity, matching the
// Subgroup path of __spirv_GroupBroadcast in group/collectives.cl.
#define __CLC_GROUP_NON_UNIFORM_SHUFFLE(TYPE) \
_CLC_DEF _CLC_OVERLOAD _CLC_CONVERGENT TYPE __spirv_GroupNonUniformShuffle( \
int scope, TYPE value, uint id) { \

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
int scope, TYPE value, uint id) { \
int execution, TYPE value, uint id) { \

(void)scope; \
return __spirv_SubgroupShuffleINTEL(value, id); \

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (execution == Subgroup)
  return __spirv_SubgroupShuffleINTEL(value, id);
return 0;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

current implementation is also fine since the spec requires Execution is .... . It must be Subgroup.

} \
_CLC_DEF _CLC_OVERLOAD _CLC_CONVERGENT TYPE \
__spirv_GroupNonUniformShuffleXor(int scope, TYPE value, uint mask) { \
(void)scope; \
return __spirv_SubgroupShuffleXorINTEL(value, mask); \
} \
_CLC_DEF _CLC_OVERLOAD _CLC_CONVERGENT TYPE \
__spirv_GroupNonUniformShuffleUp(int scope, TYPE value, uint delta) { \
(void)scope; \
return __spirv_SubgroupShuffleUpINTEL(value, value, delta); \
} \
_CLC_DEF _CLC_OVERLOAD _CLC_CONVERGENT TYPE \
__spirv_GroupNonUniformShuffleDown(int scope, TYPE value, uint delta) { \
(void)scope; \
return __spirv_SubgroupShuffleDownINTEL(value, value, delta); \
}

__CLC_GROUP_NON_UNIFORM_SHUFFLE(char)
__CLC_GROUP_NON_UNIFORM_SHUFFLE(uchar)
__CLC_GROUP_NON_UNIFORM_SHUFFLE(short)
__CLC_GROUP_NON_UNIFORM_SHUFFLE(ushort)
__CLC_GROUP_NON_UNIFORM_SHUFFLE(int)
__CLC_GROUP_NON_UNIFORM_SHUFFLE(uint)
__CLC_GROUP_NON_UNIFORM_SHUFFLE(long)
__CLC_GROUP_NON_UNIFORM_SHUFFLE(ulong)
__CLC_GROUP_NON_UNIFORM_SHUFFLE(half)
__CLC_GROUP_NON_UNIFORM_SHUFFLE(float)
__CLC_GROUP_NON_UNIFORM_SHUFFLE(double)

#undef __CLC_GROUP_NON_UNIFORM_SHUFFLE