Skip to content

Commit 729d6f6

Browse files
[SYCL][E2E] Fix online compiler test for accelerator (#15270)
This commit fixes an issue where the online_compiler_OpenCL test would assume that the clCreateProgramWithIL function is supported by all OpenCL targets, while it was only supported with >=2.1 and had an extension in older versions. This also enables the test for accelerator. --------- Signed-off-by: Larsen, Steffen <[email protected]>
1 parent 098416a commit 729d6f6

File tree

3 files changed

+88
-6
lines changed

3 files changed

+88
-6
lines changed

sycl/test-e2e/OnlineCompiler/online_compiler_L0.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
// All Level-Zero specific code is kept here and the common part that can be
88
// re-used by other backends is kept in online_compiler_common.hpp file.
99

10-
#include <sycl/ext/intel/experimental/online_compiler.hpp>
1110
#include <sycl/detail/core.hpp>
11+
#include <sycl/ext/intel/experimental/online_compiler.hpp>
1212

1313
#include <vector>
1414

@@ -20,6 +20,10 @@
2020
using byte = unsigned char;
2121

2222
#ifdef RUN_KERNELS
23+
bool testSupported(sycl::queue &Queue) {
24+
return Queue.get_backend() == sycl::backend::ext_oneapi_level_zero;
25+
}
26+
2327
sycl::kernel getSYCLKernelWithIL(sycl::queue &Queue,
2428
const std::vector<byte> &IL) {
2529

sycl/test-e2e/OnlineCompiler/online_compiler_OpenCL.cpp

+76-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// REQUIRES: opencl, opencl_icd, cm-compiler
2-
// UNSUPPORTED: accelerator
32

43
// RUN: %{build} -Wno-error=deprecated-declarations -DRUN_KERNELS %opencl_lib -o %t.out
54
// RUN: %{run} %t.out
@@ -18,14 +17,86 @@
1817
using byte = unsigned char;
1918

2019
#ifdef RUN_KERNELS
20+
std::tuple<unsigned long, unsigned long> GetOCLVersion(sycl::device Device) {
21+
cl_int Err;
22+
cl_device_id ClDevice = sycl::get_native<sycl::backend::opencl>(Device);
23+
24+
size_t VersionSize = 0;
25+
Err = clGetDeviceInfo(ClDevice, CL_DEVICE_VERSION, 0, nullptr, &VersionSize);
26+
assert(Err == CL_SUCCESS);
27+
28+
std::string Version(VersionSize, '\0');
29+
Err = clGetDeviceInfo(ClDevice, CL_DEVICE_VERSION, VersionSize,
30+
Version.data(), nullptr);
31+
assert(Err == CL_SUCCESS);
32+
33+
std::string_view Prefix = "OpenCL ";
34+
size_t VersionBegin = Version.find_first_of(" ");
35+
size_t VersionEnd = Version.find_first_of(" ", VersionBegin + 1);
36+
size_t VersionSeparator = Version.find_first_of(".", VersionBegin + 1);
37+
38+
bool HaveOCLPrefix =
39+
std::equal(Prefix.begin(), Prefix.end(), Version.begin());
40+
41+
assert(HaveOCLPrefix && VersionBegin != std::string::npos &&
42+
VersionEnd != std::string::npos &&
43+
VersionSeparator != std::string::npos);
44+
45+
std::string VersionMajor{Version.begin() + VersionBegin + 1,
46+
Version.begin() + VersionSeparator};
47+
std::string VersionMinor{Version.begin() + VersionSeparator + 1,
48+
Version.begin() + VersionEnd};
49+
50+
unsigned long OCLMajor = strtoul(VersionMajor.c_str(), nullptr, 10);
51+
unsigned long OCLMinor = strtoul(VersionMinor.c_str(), nullptr, 10);
52+
53+
assert(OCLMajor > 0 && (OCLMajor > 2 || OCLMinor <= 2) &&
54+
OCLMajor != UINT_MAX && OCLMinor != UINT_MAX);
55+
56+
return std::make_tuple(OCLMajor, OCLMinor);
57+
}
58+
59+
bool testSupported(sycl::queue &Queue) {
60+
if (Queue.get_backend() != sycl::backend::opencl)
61+
return false;
62+
63+
sycl::device Device = Queue.get_device();
64+
auto [OCLMajor, OCLMinor] = GetOCLVersion(Device);
65+
66+
// Creating a program from IL is only supported on >=2.1 or if
67+
// cl_khr_il_program is supported on the device.
68+
return (OCLMajor == 2 && OCLMinor >= 1) || OCLMajor > 2 ||
69+
Device.has_extension("cl_khr_il_program");
70+
}
71+
2172
sycl::kernel getSYCLKernelWithIL(sycl::queue &Queue,
2273
const std::vector<byte> &IL) {
2374
sycl::context Context = Queue.get_context();
2475

25-
cl_int Err;
26-
cl_program ClProgram =
27-
clCreateProgramWithIL(sycl::get_native<sycl::backend::opencl>(Context),
28-
IL.data(), IL.size(), &Err);
76+
cl_int Err = 0;
77+
cl_program ClProgram = 0;
78+
79+
sycl::device Device = Queue.get_device();
80+
auto [OCLMajor, OCLMinor] = GetOCLVersion(Device);
81+
if ((OCLMajor == 2 && OCLMinor >= 1) || OCLMajor > 2) {
82+
// clCreateProgramWithIL is supported if OCL version >=2.1.
83+
ClProgram =
84+
clCreateProgramWithIL(sycl::get_native<sycl::backend::opencl>(Context),
85+
IL.data(), IL.size(), &Err);
86+
} else {
87+
// Fall back to using extension function for building IR.
88+
using ApiFuncT =
89+
cl_program(CL_API_CALL *)(cl_context, const void *, size_t, cl_int *);
90+
ApiFuncT FuncPtr =
91+
reinterpret_cast<ApiFuncT>(clGetExtensionFunctionAddressForPlatform(
92+
sycl::get_native<sycl::backend::opencl>(Context.get_platform()),
93+
"clCreateProgramWithILKHR"));
94+
95+
assert(FuncPtr != nullptr);
96+
97+
ClProgram = FuncPtr(sycl::get_native<sycl::backend::opencl>(Context),
98+
IL.data(), IL.size(), &Err);
99+
}
29100
assert(Err == CL_SUCCESS);
30101

31102
Err = clBuildProgram(ClProgram, 0, nullptr, nullptr, nullptr, nullptr);

sycl/test-e2e/OnlineCompiler/online_compiler_common.hpp

+7
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ int main(int argc, char **argv) {
5353
sycl::queue Q;
5454
sycl::device Device = Q.get_device();
5555

56+
#ifdef RUN_KERNELS
57+
if (!testSupported(Q)) {
58+
std::cout << "Building for IL is not supported. Skipping!" << std::endl;
59+
return 0;
60+
}
61+
#endif
62+
5663
{ // Compile and run a trivial OpenCL kernel.
5764
std::cout << "Test case1\n";
5865
sycl::ext::intel::experimental::online_compiler<

0 commit comments

Comments
 (0)