Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
24 changes: 15 additions & 9 deletions sycl/include/sycl/detail/os_util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,27 +106,33 @@ void fileTreeWalk(const std::string Path,
std::function<void(const std::string)> Func,
bool ignoreErrors = false);

void *dynLookup(const char *WinName, const char *LinName, const char *FunName);
void *dynLookup(const char *WinName, const char *LinName,
const char *LinuxFallbackLibName, const char *FunName);

// Look up a function name that was dynamically linked
// This is used by the runtime where it needs to manipulate native handles (e.g.
// retaining OpenCL handles). On Windows, the symbol name is looked up in
// `WinName`. In Linux, it uses `LinName`.
// `WinName`. In Linux, it uses `LinName` or `LinuxFallbackLibName`.
//
// The library must already have been loaded (perhaps by UR), otherwise this
// function throws a SYCL runtime exception.
template <typename fn>
fn *dynLookupFunction(const char *WinName, const char *LinName,
const char *FunName) {
return reinterpret_cast<fn *>(dynLookup(WinName, LinName, FunName));
const char *LinuxFallbackLibName, const char *FunName) {
return reinterpret_cast<fn *>(
dynLookup(WinName, LinName, LinuxFallbackLibName, FunName));
}
// On Linux, the name of OpenCL that was used to link against may be either
// `OpenCL.so`, `OpenCL.so.1` or possibly anything else.
// `libur_adapter_opencl.so` is a more stable name, since it is hardcoded into
// the loader.

// On Linux, first try to load from libur_adapter_opencl.so, then
// libur_adapter_opencl.so.0 if the first is not found. libur_adapter_opencl.so
// and libur_adapter_opencl.so.0 might be different libraries if they are not
// symlinked, which is the case with PyPi compiler distribution package.
// We can't load libur_adapter_opencl.so.0 always as the first choice because
// that would break SYCL unittests, which rely on mocking libur_adapter_opencl.
#define __SYCL_OCL_CALL(FN, ...) \
(sycl::_V1::detail::dynLookupFunction<decltype(FN)>( \
"OpenCL", "libur_adapter_opencl.so", #FN)(__VA_ARGS__))
"OpenCL", "libur_adapter_opencl.so", "libur_adapter_opencl.so.0", \
#FN)(__VA_ARGS__))

} // namespace detail
} // namespace _V1
Expand Down
14 changes: 11 additions & 3 deletions sycl/source/detail/os_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,9 @@ size_t getDirectorySize(const std::string &Path, bool ignoreErrors) {
// The library must already have been loaded (perhaps by UR), otherwise this
// function throws a SYCL runtime exception.
void *dynLookup([[maybe_unused]] const char *WinName,
[[maybe_unused]] const char *LinName, const char *FunName) {
[[maybe_unused]] const char *LinName,
[[maybe_unused]] const char *LinuxFallbackLibName,
const char *FunName) {
#ifdef __SYCL_RT_OS_WINDOWS
auto handle = GetModuleHandleA(WinName);
if (!handle) {
Expand All @@ -310,8 +312,14 @@ void *dynLookup([[maybe_unused]] const char *WinName,
#else
auto handle = dlopen(LinName, RTLD_LAZY | RTLD_NOLOAD);
if (!handle) {
throw sycl::exception(make_error_code(errc::runtime),
std::string(LinName) + " library is not loaded");

// Try to open fallback library if provided.
if (LinuxFallbackLibName)
handle = dlopen(LinuxFallbackLibName, RTLD_LAZY | RTLD_NOLOAD);

if (!handle)
throw sycl::exception(make_error_code(errc::runtime),
std::string(LinName) + " library is not loaded");
}
auto *retVal = dlsym(handle, FunName);
dlclose(handle);
Expand Down
Loading