Skip to content

Commit 74c42b3

Browse files
committed
[SYCL] Lookup versioned OpenCL adapter library as fallback
1 parent 1846390 commit 74c42b3

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

sycl/include/sycl/detail/os_util.hpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,27 +106,33 @@ void fileTreeWalk(const std::string Path,
106106
std::function<void(const std::string)> Func,
107107
bool ignoreErrors = false);
108108

109-
void *dynLookup(const char *WinName, const char *LinName, const char *FunName);
109+
void *dynLookup(const char *WinName, const char *LinName,
110+
const char *LinuxFallbackLibName, const char *FunName);
110111

111112
// Look up a function name that was dynamically linked
112113
// This is used by the runtime where it needs to manipulate native handles (e.g.
113114
// retaining OpenCL handles). On Windows, the symbol name is looked up in
114-
// `WinName`. In Linux, it uses `LinName`.
115+
// `WinName`. In Linux, it uses `LinName` or `LinuxFallbackLibName`.
115116
//
116117
// The library must already have been loaded (perhaps by UR), otherwise this
117118
// function throws a SYCL runtime exception.
118119
template <typename fn>
119120
fn *dynLookupFunction(const char *WinName, const char *LinName,
120-
const char *FunName) {
121-
return reinterpret_cast<fn *>(dynLookup(WinName, LinName, FunName));
121+
const char *LinuxFallbackLibName, const char *FunName) {
122+
return reinterpret_cast<fn *>(
123+
dynLookup(WinName, LinName, LinuxFallbackLibName, FunName));
122124
}
123-
// On Linux, the name of OpenCL that was used to link against may be either
124-
// `OpenCL.so`, `OpenCL.so.1` or possibly anything else.
125-
// `libur_adapter_opencl.so` is a more stable name, since it is hardcoded into
126-
// the loader.
125+
126+
// On Linux, first try to load from libur_adapter_opencl.so, then
127+
// libur_adapter_opencl.so.0 if the first is not found. libur_adapter_opencl.so
128+
// and libur_adapter_opencl.so.0 might be different libraries if they are not
129+
// symlinked, which is the case with PyPi compiler distribution package.
130+
// We can't load libur_adapter_opencl.so.0 always as the first choice because
131+
// that would break SYCL unittests, which rely on mocking libur_adapter_opencl.
127132
#define __SYCL_OCL_CALL(FN, ...) \
128133
(sycl::_V1::detail::dynLookupFunction<decltype(FN)>( \
129-
"OpenCL", "libur_adapter_opencl.so", #FN)(__VA_ARGS__))
134+
"OpenCL", "libur_adapter_opencl.so", "libur_adapter_opencl.so.0", \
135+
#FN)(__VA_ARGS__))
130136

131137
} // namespace detail
132138
} // namespace _V1

sycl/source/detail/os_util.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,9 @@ size_t getDirectorySize(const std::string &Path, bool ignoreErrors) {
299299
// The library must already have been loaded (perhaps by UR), otherwise this
300300
// function throws a SYCL runtime exception.
301301
void *dynLookup([[maybe_unused]] const char *WinName,
302-
[[maybe_unused]] const char *LinName, const char *FunName) {
302+
[[maybe_unused]] const char *LinName,
303+
[[maybe_unused]] const char *LinuxFallbackLibName,
304+
const char *FunName) {
303305
#ifdef __SYCL_RT_OS_WINDOWS
304306
auto handle = GetModuleHandleA(WinName);
305307
if (!handle) {
@@ -310,8 +312,14 @@ void *dynLookup([[maybe_unused]] const char *WinName,
310312
#else
311313
auto handle = dlopen(LinName, RTLD_LAZY | RTLD_NOLOAD);
312314
if (!handle) {
313-
throw sycl::exception(make_error_code(errc::runtime),
314-
std::string(LinName) + " library is not loaded");
315+
316+
// Try to open fallback library if provided.
317+
if (LinuxFallbackLibName)
318+
handle = dlopen(LinuxFallbackLibName, RTLD_LAZY | RTLD_NOLOAD);
319+
320+
if (!handle)
321+
throw sycl::exception(make_error_code(errc::runtime),
322+
std::string(LinName) + " library is not loaded");
315323
}
316324
auto *retVal = dlsym(handle, FunName);
317325
dlclose(handle);

0 commit comments

Comments
 (0)