Skip to content

Commit 931f924

Browse files
committed
[SYCL] Added a new test case for sub-group attributes
Signed-off-by: Tian, Shilei <[email protected]>
1 parent ecedb43 commit 931f924

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed

sycl/test/sub_group/attributes.cpp

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// RUN: %clangxx -fsycl %s -o %t.out -lOpenCL
2+
// RUN: env SYCL_DEVICE_TYPE=HOST %t.out
3+
// RUN: %CPU_RUN_PLACEHOLDER %t.out
4+
// RUN: %GPU_RUN_PLACEHOLDER %t.out
5+
// RUN: %ACC_RUN_PLACEHOLDER %t.out
6+
//==------- attributes.cpp - SYCL sub_group attributes test ----*- C++ -*---==//
7+
//
8+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
9+
// See https://llvm.org/LICENSE.txt for license information.
10+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#include "helper.hpp"
15+
#include <CL/sycl.hpp>
16+
17+
#define KERNEL_FUNCTOR_WITH_SIZE(SIZE) \
18+
class KernelFunctor##SIZE { \
19+
public: \
20+
[[cl::intel_reqd_sub_group_size(SIZE)]] void \
21+
operator()(cl::sycl::nd_item<1> Item) { \
22+
const auto GID = Item.get_global_id(); \
23+
} \
24+
};
25+
26+
KERNEL_FUNCTOR_WITH_SIZE(1);
27+
KERNEL_FUNCTOR_WITH_SIZE(2);
28+
KERNEL_FUNCTOR_WITH_SIZE(4);
29+
KERNEL_FUNCTOR_WITH_SIZE(8);
30+
KERNEL_FUNCTOR_WITH_SIZE(16);
31+
32+
#undef KERNEL_FUNCTOR_WITH_SIZE
33+
34+
inline uint32_t flp2(uint32_t X) {
35+
X = X | (X >> 1);
36+
X = X | (X >> 2);
37+
X = X | (X >> 4);
38+
X = X | (X >> 8);
39+
X = X | (X >> 16);
40+
return X - (X >> 1);
41+
}
42+
43+
int main() {
44+
queue Queue;
45+
device Device = Queue.get_device();
46+
47+
// According to specification, this kernel query requires `cl_khr_subgroups`
48+
// or `cl_intel_subgroups`, and also `cl_intel_required_subgroup_size`
49+
if ((!Device.has_extension("cl_intel_subgroups") &&
50+
!Device.has_extension("cl_khr_subgroups")) ||
51+
!Device.has_extension("cl_intel_required_subgroup_size")) {
52+
std::cout << "Skipping test\n";
53+
return 0;
54+
}
55+
56+
try {
57+
const auto SGSizes = Device.get_info<info::device::sub_group_sizes>();
58+
59+
for (const auto SGSize : SGSizes) {
60+
// Get the previous power of 2
61+
auto ReqdSize = flp2(SGSize);
62+
63+
cl::sycl::program Prog(Queue.get_context());
64+
65+
// Hack to store the `cl::sycl::kernel` because `cl::sycl::kernel` doesn't
66+
// have default constructor
67+
cl::sycl::vector_class<cl::sycl::kernel> TheKernel;
68+
69+
switch (ReqdSize) {
70+
case 16:
71+
Prog.build_with_kernel_type<KernelFunctor16>();
72+
TheKernel.push_back(Prog.get_kernel<KernelFunctor16>());
73+
break;
74+
case 8:
75+
Prog.build_with_kernel_type<KernelFunctor8>();
76+
TheKernel.push_back(Prog.get_kernel<KernelFunctor8>());
77+
break;
78+
case 4:
79+
Prog.build_with_kernel_type<KernelFunctor4>();
80+
TheKernel.push_back(Prog.get_kernel<KernelFunctor4>());
81+
break;
82+
case 2:
83+
Prog.build_with_kernel_type<KernelFunctor2>();
84+
TheKernel.push_back(Prog.get_kernel<KernelFunctor2>());
85+
break;
86+
case 1:
87+
Prog.build_with_kernel_type<KernelFunctor1>();
88+
TheKernel.push_back(Prog.get_kernel<KernelFunctor1>());
89+
break;
90+
default:
91+
throw feature_not_supported("sub-group size is not supported");
92+
}
93+
94+
auto Kernel = TheKernel[0];
95+
96+
auto Res = Kernel.get_sub_group_info<
97+
cl::sycl::info::kernel_sub_group::compile_sub_group_size>(Device);
98+
99+
exit_if_not_equal<size_t>(Res, ReqdSize, "compile_sub_group_size");
100+
}
101+
} catch (exception e) {
102+
std::cout << "SYCL exception caught: " << e.what();
103+
return 1;
104+
}
105+
106+
std::cout << "Test passed.\n";
107+
return 0;
108+
}

0 commit comments

Comments
 (0)