Skip to content

[SYCL] Unload libraries in jit_compiler and kernel_compiler_opencl destructors #16517

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
29 changes: 20 additions & 9 deletions sycl/source/detail/jit_compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,38 @@ namespace sycl {
inline namespace _V1 {
namespace detail {

std::function<void(void *)> jit_compiler::CustomDeleterForLibHandle =
[](void *StoredPtr) {
if (!StoredPtr)
return;
std::ignore = sycl::detail::ur::unloadOsLibrary(StoredPtr);
};

static inline void printPerformanceWarning(const std::string &Message) {
if (detail::SYCLConfig<detail::SYCL_RT_WARNING_LEVEL>::get() > 0) {
std::cerr << "WARNING: " << Message << "\n";
}
}

jit_compiler::jit_compiler() {
jit_compiler::jit_compiler()
: LibraryHandle(nullptr, CustomDeleterForLibHandle) {
auto checkJITLibrary = [this]() -> bool {
#ifdef _WIN32
static const std::string dir = sycl::detail::OSUtil::getCurrentDSODir();
static const std::string JITLibraryName = dir + "\\" + "sycl-jit.dll";
#else
static const std::string JITLibraryName = "libsycl-jit.so";
#endif

void *LibraryPtr = sycl::detail::ur::loadOsLibrary(JITLibraryName);
std::unique_ptr<void, decltype(CustomDeleterForLibHandle)> LibraryPtr(
sycl::detail::ur::loadOsLibrary(JITLibraryName),
CustomDeleterForLibHandle);
if (LibraryPtr == nullptr) {
printPerformanceWarning("Could not find JIT library " + JITLibraryName);
return false;
}

this->AddToConfigHandle = reinterpret_cast<AddToConfigFuncT>(
sycl::detail::ur::getOsLibraryFuncAddress(LibraryPtr,
sycl::detail::ur::getOsLibraryFuncAddress(LibraryPtr.get(),
"addToJITConfiguration"));
if (!this->AddToConfigHandle) {
printPerformanceWarning(
Expand All @@ -54,7 +63,7 @@ jit_compiler::jit_compiler() {
}

this->ResetConfigHandle = reinterpret_cast<ResetConfigFuncT>(
sycl::detail::ur::getOsLibraryFuncAddress(LibraryPtr,
sycl::detail::ur::getOsLibraryFuncAddress(LibraryPtr.get(),
"resetJITConfiguration"));
if (!this->ResetConfigHandle) {
printPerformanceWarning(
Expand All @@ -63,7 +72,8 @@ jit_compiler::jit_compiler() {
}

this->FuseKernelsHandle = reinterpret_cast<FuseKernelsFuncT>(
sycl::detail::ur::getOsLibraryFuncAddress(LibraryPtr, "fuseKernels"));
sycl::detail::ur::getOsLibraryFuncAddress(LibraryPtr.get(),
"fuseKernels"));
if (!this->FuseKernelsHandle) {
printPerformanceWarning(
"Cannot resolve JIT library function entry point");
Expand All @@ -73,21 +83,22 @@ jit_compiler::jit_compiler() {
this->MaterializeSpecConstHandle =
reinterpret_cast<MaterializeSpecConstFuncT>(
sycl::detail::ur::getOsLibraryFuncAddress(
LibraryPtr, "materializeSpecConstants"));
LibraryPtr.get(), "materializeSpecConstants"));
if (!this->MaterializeSpecConstHandle) {
printPerformanceWarning(
"Cannot resolve JIT library function entry point");
return false;
}

this->CompileSYCLHandle = reinterpret_cast<CompileSYCLFuncT>(
sycl::detail::ur::getOsLibraryFuncAddress(LibraryPtr, "compileSYCL"));
sycl::detail::ur::getOsLibraryFuncAddress(LibraryPtr.get(),
"compileSYCL"));
if (!this->CompileSYCLHandle) {
printPerformanceWarning(
"Cannot resolve JIT library function entry point");
return false;
}

LibraryHandle = std::move(LibraryPtr);
return true;
};
Available = checkJITLibrary();
Expand Down
5 changes: 4 additions & 1 deletion sycl/source/detail/jit_compiler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <KernelFusion.h>
#endif // SYCL_EXT_JIT_ENABLE

#include <functional>
#include <unordered_map>

namespace jit_compiler {
Expand Down Expand Up @@ -80,7 +81,7 @@ class jit_compiler {
const ::jit_compiler::SYCLKernelAttribute &Attr) const;

// Indicate availability of the JIT compiler
bool Available;
bool Available = false;

// Manages the lifetime of the UR structs for device binaries.
std::vector<DeviceBinariesCollection> JITDeviceBinaries;
Expand All @@ -98,6 +99,8 @@ class jit_compiler {
CompileSYCLFuncT CompileSYCLHandle = nullptr;
ResetConfigFuncT ResetConfigHandle = nullptr;
AddToConfigFuncT AddToConfigHandle = nullptr;
static std::function<void(void *)> CustomDeleterForLibHandle;
std::unique_ptr<void, decltype(CustomDeleterForLibHandle)> LibraryHandle;
#endif // SYCL_EXT_JIT_ENABLE
};

Expand Down
26 changes: 15 additions & 11 deletions sycl/source/detail/kernel_compiler/kernel_compiler_opencl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
#include "../online_compiler/ocloc_api.h"
#include "../split_string.hpp"

#include <cstring> // strlen
#include <numeric> // for std::accumulate
#include <cstring> // strlen
#include <functional> // for std::function
#include <numeric> // for std::accumulate
#include <regex>
#include <sstream>

Expand Down Expand Up @@ -56,16 +57,21 @@ void checkOclocLibrary(void *OclocLibrary) {
}
}

static void *OclocLibrary = nullptr;
static std::unique_ptr<void, std::function<void(void *)>>
OclocLibrary(nullptr, [](void *StoredPtr) {
if (!StoredPtr)
return;
std::ignore = sycl::detail::ur::unloadOsLibrary(StoredPtr);
});

// load the ocloc shared library, check it.
void *loadOclocLibrary() {
void loadOclocLibrary() {
#ifdef __SYCL_RT_OS_WINDOWS
static const std::string OclocLibraryName = "ocloc64.dll";
#else
static const std::string OclocLibraryName = "libocloc.so";
#endif
void *tempPtr = OclocLibrary;
void *tempPtr = OclocLibrary.get();
if (tempPtr == nullptr) {
tempPtr = sycl::detail::ur::loadOsLibrary(OclocLibraryName);

Expand All @@ -75,10 +81,8 @@ void *loadOclocLibrary() {

checkOclocLibrary(tempPtr);

OclocLibrary = tempPtr;
OclocLibrary.reset(tempPtr);
}

return OclocLibrary;
}

bool OpenCLC_Compilation_Available() {
Expand All @@ -103,13 +107,13 @@ void SetupLibrary(voidPtr &oclocInvokeHandle, voidPtr &oclocFreeOutputHandle,
if (OclocLibrary == nullptr)
loadOclocLibrary();

oclocInvokeHandle =
sycl::detail::ur::getOsLibraryFuncAddress(OclocLibrary, "oclocInvoke");
oclocInvokeHandle = sycl::detail::ur::getOsLibraryFuncAddress(
OclocLibrary.get(), "oclocInvoke");
if (!oclocInvokeHandle)
throw sycl::exception(the_errc, "Cannot load oclocInvoke() function");

oclocFreeOutputHandle = sycl::detail::ur::getOsLibraryFuncAddress(
OclocLibrary, "oclocFreeOutput");
OclocLibrary.get(), "oclocFreeOutput");
if (!oclocFreeOutputHandle)
throw sycl::exception(the_errc, "Cannot load oclocFreeOutput() function");
}
Expand Down
Loading