Skip to content

Commit 29f3a92

Browse files
author
Alexander Batashev
authored
[SYCL] Allow overriding plugin libraries (#4067)
Motivation for this change is to provide ability to replay PI traces: a fake plugin opens a file with trace recording and replies to PI calls with info, acquired from that file. This can be used for debugging SYCL applications runtime when target hardware is not available. Such design allows the fake plugin rely on the fact that the library is only loaded once, which makes such a plugin stateless.
1 parent 4c7a172 commit 29f3a92

File tree

4 files changed

+90
-14
lines changed

4 files changed

+90
-14
lines changed

sycl/source/detail/config.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,7 @@ CONFIG(SYCL_CACHE_THRESHOLD, 16, __SYCL_CACHE_THRESHOLD)
3131
CONFIG(SYCL_CACHE_MIN_DEVICE_IMAGE_SIZE, 16, __SYCL_CACHE_MIN_DEVICE_IMAGE_SIZE)
3232
CONFIG(SYCL_CACHE_MAX_DEVICE_IMAGE_SIZE, 16, __SYCL_CACHE_MAX_DEVICE_IMAGE_SIZE)
3333
CONFIG(INTEL_ENABLE_OFFLOAD_ANNOTATIONS, 1, __SYCL_INTEL_ENABLE_OFFLOAD_ANNOTATIONS)
34+
CONFIG(SYCL_OVERRIDE_PI_OPENCL, 1024, __SYCL_OVERRIDE_PI_OPENCL)
35+
CONFIG(SYCL_OVERRIDE_PI_LEVEL_ZERO, 1024, __SYCL_OVERRIDE_PI_LEVEL_ZERO)
36+
CONFIG(SYCL_OVERRIDE_PI_CUDA, 1024, __SYCL_OVERRIDE_PI_CUDA)
37+
CONFIG(SYCL_OVERRIDE_PI_ROCM, 1024, __SYCL_OVERRIDE_PI_ROCM)

sycl/source/detail/pi.cpp

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -263,19 +263,34 @@ std::string memFlagsToString(pi_mem_flags Flags) {
263263
std::shared_ptr<plugin> GlobalPlugin;
264264

265265
// Find the plugin at the appropriate location and return the location.
266-
bool findPlugins(std::vector<std::pair<std::string, backend>> &PluginNames) {
266+
std::vector<std::pair<std::string, backend>> findPlugins() {
267+
std::vector<std::pair<std::string, backend>> PluginNames;
268+
267269
// TODO: Based on final design discussions, change the location where the
268270
// plugin must be searched; how to identify the plugins etc. Currently the
269271
// search is done for libpi_opencl.so/pi_opencl.dll file in LD_LIBRARY_PATH
270272
// env only.
271273
//
274+
const char *OpenCLPluginName =
275+
SYCLConfig<SYCL_OVERRIDE_PI_OPENCL>::get()
276+
? SYCLConfig<SYCL_OVERRIDE_PI_OPENCL>::get()
277+
: __SYCL_OPENCL_PLUGIN_NAME;
278+
const char *L0PluginName =
279+
SYCLConfig<SYCL_OVERRIDE_PI_LEVEL_ZERO>::get()
280+
? SYCLConfig<SYCL_OVERRIDE_PI_LEVEL_ZERO>::get()
281+
: __SYCL_LEVEL_ZERO_PLUGIN_NAME;
282+
const char *CUDAPluginName = SYCLConfig<SYCL_OVERRIDE_PI_CUDA>::get()
283+
? SYCLConfig<SYCL_OVERRIDE_PI_CUDA>::get()
284+
: __SYCL_CUDA_PLUGIN_NAME;
285+
const char *ROCMPluginName = SYCLConfig<SYCL_OVERRIDE_PI_ROCM>::get()
286+
? SYCLConfig<SYCL_OVERRIDE_PI_ROCM>::get()
287+
: __SYCL_ROCM_PLUGIN_NAME;
272288
device_filter_list *FilterList = SYCLConfig<SYCL_DEVICE_FILTER>::get();
273289
if (!FilterList) {
274-
PluginNames.emplace_back(__SYCL_OPENCL_PLUGIN_NAME, backend::opencl);
275-
PluginNames.emplace_back(__SYCL_LEVEL_ZERO_PLUGIN_NAME,
276-
backend::level_zero);
277-
PluginNames.emplace_back(__SYCL_CUDA_PLUGIN_NAME, backend::cuda);
278-
PluginNames.emplace_back(__SYCL_ROCM_PLUGIN_NAME, backend::rocm);
290+
PluginNames.emplace_back(OpenCLPluginName, backend::opencl);
291+
PluginNames.emplace_back(L0PluginName, backend::level_zero);
292+
PluginNames.emplace_back(CUDAPluginName, backend::cuda);
293+
PluginNames.emplace_back(ROCMPluginName, backend::rocm);
279294
} else {
280295
std::vector<device_filter> Filters = FilterList->get();
281296
bool OpenCLFound = false;
@@ -286,26 +301,25 @@ bool findPlugins(std::vector<std::pair<std::string, backend>> &PluginNames) {
286301
backend Backend = Filter.Backend;
287302
if (!OpenCLFound &&
288303
(Backend == backend::opencl || Backend == backend::all)) {
289-
PluginNames.emplace_back(__SYCL_OPENCL_PLUGIN_NAME, backend::opencl);
304+
PluginNames.emplace_back(OpenCLPluginName, backend::opencl);
290305
OpenCLFound = true;
291306
}
292307
if (!LevelZeroFound &&
293308
(Backend == backend::level_zero || Backend == backend::all)) {
294-
PluginNames.emplace_back(__SYCL_LEVEL_ZERO_PLUGIN_NAME,
295-
backend::level_zero);
309+
PluginNames.emplace_back(L0PluginName, backend::level_zero);
296310
LevelZeroFound = true;
297311
}
298312
if (!CudaFound && (Backend == backend::cuda || Backend == backend::all)) {
299-
PluginNames.emplace_back(__SYCL_CUDA_PLUGIN_NAME, backend::cuda);
313+
PluginNames.emplace_back(CUDAPluginName, backend::cuda);
300314
CudaFound = true;
301315
}
302316
if (!RocmFound && (Backend == backend::rocm || Backend == backend::all)) {
303-
PluginNames.emplace_back(__SYCL_ROCM_PLUGIN_NAME, backend::rocm);
317+
PluginNames.emplace_back(ROCMPluginName, backend::rocm);
304318
RocmFound = true;
305319
}
306320
}
307321
}
308-
return true;
322+
return PluginNames;
309323
}
310324

311325
// Load the Plugin by calling the OS dependent library loading call.
@@ -359,8 +373,7 @@ const std::vector<plugin> &initialize() {
359373
}
360374

361375
static void initializePlugins(std::vector<plugin> *Plugins) {
362-
std::vector<std::pair<std::string, backend>> PluginNames;
363-
findPlugins(PluginNames);
376+
std::vector<std::pair<std::string, backend>> PluginNames = findPlugins();
364377

365378
if (PluginNames.empty() && trace(PI_TRACE_ALL))
366379
std::cerr << "SYCL_PI_TRACE[all]: "
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// RUN: %clangxx -fsycl %s -o %t.out
2+
// RUN: env SYCL_OVERRIDE_PI_OPENCL=opencl_test env SYCL_OVERRIDE_PI_LEVEL_ZERO=l0_test env SYCL_OVERRIDE_PI_CUDA=cuda_test env SYCL_OVERRIDE_PI_ROCM=rocm_test env SYCL_PI_TRACE=-1 %t.out > %t.log 2>&1
3+
// RUN: FileCheck %s --input-file %t.log
4+
5+
#include <sycl/sycl.hpp>
6+
7+
int main() {
8+
sycl::queue Q;
9+
10+
return 0;
11+
}
12+
13+
// CHECK: SYCL_PI_TRACE[all]: Check if plugin is present. Failed to load plugin: opencl_test
14+
// CHECK: SYCL_PI_TRACE[all]: Check if plugin is present. Failed to load plugin: l0_test
15+
// CHECK: SYCL_PI_TRACE[all]: Check if plugin is present. Failed to load plugin: cuda_test
16+
// CHECK: SYCL_PI_TRACE[all]: Check if plugin is present. Failed to load plugin: rocm_test
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// RUN: %clangxx -fsycl -DFAKE_PLUGIN -shared %s -o %t_fake_plugin.so
2+
// RUN: %clangxx -fsycl %s -o %t.out
3+
// RUN: env SYCL_OVERRIDE_PI_OPENCL=%t_fake_plugin.so env SYCL_OVERRIDE_PI_LEVEL_ZERO=%t_fake_plugin.so env SYCL_OVERRIDE_PI_CUDA=%t_fake_plugin.so env SYCL_OVERRIDE_PI_ROCM=%t_fake_plugin.so env SYCL_PI_TRACE=-1 %t.out > %t.log 2>&1
4+
// RUN: FileCheck %s --input-file %t.log
5+
// REQUIRES: linux
6+
7+
#ifdef FAKE_PLUGIN
8+
9+
#include <CL/sycl/detail/pi.h>
10+
11+
pi_result piPlatformsGet(pi_uint32 NumEntries, pi_platform *Platforms,
12+
pi_uint32 *NumPlatforms) {
13+
return PI_INVALID_OPERATION;
14+
}
15+
16+
pi_result piTearDown(void *) { return PI_SUCCESS; }
17+
18+
pi_result piPluginInit(pi_plugin *PluginInit) {
19+
PluginInit->PiFunctionTable.piPlatformsGet = piPlatformsGet;
20+
PluginInit->PiFunctionTable.piTearDown = piTearDown;
21+
return PI_SUCCESS;
22+
}
23+
24+
#else
25+
26+
#include <sycl/sycl.hpp>
27+
28+
int main() {
29+
try {
30+
sycl::platform P{sycl::default_selector{}};
31+
} catch (...) {
32+
// NOP
33+
}
34+
35+
return 0;
36+
}
37+
38+
#endif
39+
40+
// CHECK: SYCL_PI_TRACE[basic]: Plugin found and successfully loaded: {{[0-9a-zA-Z_\/\.-]+}}_fake_plugin.so
41+
// CHECK: SYCL_PI_TRACE[basic]: Plugin found and successfully loaded: {{[0-9a-zA-Z_\/\.-]+}}_fake_plugin.so
42+
// CHECK: SYCL_PI_TRACE[basic]: Plugin found and successfully loaded: {{[0-9a-zA-Z_\/\.-]+}}_fake_plugin.so
43+
// CHECK: SYCL_PI_TRACE[basic]: Plugin found and successfully loaded: {{[0-9a-zA-Z_\/\.-]+}}_fake_plugin.so

0 commit comments

Comments
 (0)