forked from intel/llvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SYCL][NFC] Detach library loading helpers from UR
This is a by-product of intel#14145. Functions to dynamically load a library and query a symbol out of it should not be attached to UR, because they are used for other libraries as well. Moved them from `detail::ur` into `detail` namespace, outlined into a separate header and removed declarations from public SYCL headers. Fixes intel#14923
- Loading branch information
1 parent
3e98b3a
commit ef9ae8e
Showing
10 changed files
with
172 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
//==-------------------------- load_library.hpp ----------------------------==// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Collection of helper OS-agnostic functions to dynamically load libraries and | ||
// query their symbols. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#pragma once | ||
|
||
#include <string> | ||
|
||
namespace sycl { | ||
inline namespace _V1 { | ||
namespace detail { | ||
|
||
// Function to load a shared library | ||
// Implementation is OS dependent | ||
void *loadOsLibrary(const std::string &Library); | ||
|
||
// Function to unload a shared library | ||
// Implementation is OS dependent (see posix-ur.cpp and windows-ur.cpp) | ||
int unloadOsLibrary(void *Library); | ||
|
||
// Function to get Address of a symbol defined in the shared | ||
// library, implementation is OS dependent. | ||
void *getOsLibraryFuncAddress(void *Library, const std::string &FunctionName); | ||
|
||
} // namespace detail | ||
} // namespace _V1 | ||
} // namespace sycl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
//==-------------------- load_library_posix.cpp ----------------------------==// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include <detail/load_library.hpp> | ||
#include <sycl/detail/defines_elementary.hpp> | ||
#include <sycl/detail/iostream_proxy.hpp> | ||
#include <sycl/detail/ur.hpp> | ||
|
||
#include <dlfcn.h> | ||
#include <string> | ||
|
||
namespace sycl { | ||
inline namespace _V1 { | ||
namespace detail { | ||
|
||
void *loadOsLibrary(const std::string &LibraryPath) { | ||
// TODO: Check if the option RTLD_NOW is correct. Explore using | ||
// RTLD_DEEPBIND option when there are multiple adapters. | ||
void *so = dlopen(LibraryPath.c_str(), RTLD_NOW); | ||
if (!so && ur::trace(ur::TraceLevel::TRACE_ALL)) { | ||
char *Error = dlerror(); | ||
std::cerr << "SYCL_UR_TRACE: dlopen(" << LibraryPath << ") failed with <" | ||
<< (Error ? Error : "unknown error") << ">" << std::endl; | ||
} | ||
return so; | ||
} | ||
|
||
int unloadOsLibrary(void *Library) { return dlclose(Library); } | ||
|
||
void *getOsLibraryFuncAddress(void *Library, const std::string &FunctionName) { | ||
return dlsym(Library, FunctionName.c_str()); | ||
} | ||
|
||
} // namespace detail | ||
} // namespace _V1 | ||
} // namespace sycl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
//==-------------------- load_library_lin.cpp ------------------------------==// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include <detail/load_library.hpp> | ||
#include <detail/windows_os_utils.hpp> | ||
|
||
#include <filesystem> | ||
#include <cassert> | ||
#include <string> | ||
#include <windows.h> | ||
#include <winreg.h> | ||
|
||
namespace sycl { | ||
inline namespace _V1 { | ||
namespace detail { | ||
|
||
void *loadOsLibrary(const std::string &LibraryPath) { | ||
// Tells the system to not display the critical-error-handler message box. | ||
// Instead, the system sends the error to the calling process. | ||
// This is crucial for graceful handling of shared libs that can't be | ||
// loaded, e.g. due to missing native run-times. | ||
|
||
UINT SavedMode = SetErrorMode(SEM_FAILCRITICALERRORS); | ||
// Exclude current directory from DLL search path | ||
if (!SetDllDirectoryA("")) { | ||
assert(false && "Failed to update DLL search path"); | ||
} | ||
|
||
auto Result = (void *)LoadLibraryExA(LibraryPath.c_str(), NULL, NULL); | ||
(void)SetErrorMode(SavedMode); | ||
if (!SetDllDirectoryA(nullptr)) { | ||
assert(false && "Failed to restore DLL search path"); | ||
} | ||
|
||
return Result; | ||
} | ||
|
||
int unloadOsLibrary(void *Library) { | ||
return (int)FreeLibrary((HMODULE)Library); | ||
} | ||
|
||
void *getOsLibraryFuncAddress(void *Library, const std::string &FunctionName) { | ||
return reinterpret_cast<void *>( | ||
GetProcAddress((HMODULE)Library, FunctionName.c_str())); | ||
} | ||
|
||
static std::filesystem::path getCurrentDSODirPath() { | ||
wchar_t Path[MAX_PATH]; | ||
auto Handle = | ||
getOSModuleHandle(reinterpret_cast<void *>(&getCurrentDSODirPath)); | ||
DWORD Ret = GetModuleFileName( | ||
reinterpret_cast<HMODULE>(ExeModuleHandle == Handle ? 0 : Handle), Path, | ||
MAX_PATH); | ||
assert(Ret < MAX_PATH && "Path is longer than MAX_PATH?"); | ||
assert(Ret > 0 && "GetModuleFileName failed"); | ||
(void)Ret; | ||
|
||
BOOL RetCode = PathRemoveFileSpec(Path); | ||
assert(RetCode && "PathRemoveFileSpec failed"); | ||
(void)RetCode; | ||
|
||
return std::filesystem::path(Path); | ||
} | ||
|
||
} // namespace detail | ||
} // namespace _V1 | ||
} // namespace sycl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.