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 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
27 changes: 20 additions & 7 deletions sycl/source/detail/jit_compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,20 @@ jit_compiler::jit_compiler() {
static const std::string JITLibraryName = "libsycl-jit.so";
#endif

void *LibraryPtr = sycl::detail::ur::loadOsLibrary(JITLibraryName);
auto CustomDeleter = [](void *StoredPtr) {
if (!StoredPtr)
return;
std::ignore = sycl::detail::ur::unloadOsLibrary(StoredPtr);
};
std::unique_ptr<void, decltype(CustomDeleter)> LibraryPtr(
sycl::detail::ur::loadOsLibrary(JITLibraryName), CustomDeleter);
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 +60,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 +69,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,26 +80,32 @@ 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;
}

MLibraryHandle = LibraryPtr.release();
return true;
};
Available = checkJITLibrary();
}

jit_compiler::~jit_compiler() {
if (MLibraryHandle)
std::ignore = sycl::detail::ur::unloadOsLibrary(MLibraryHandle);
}

static ::jit_compiler::BinaryFormat
translateBinaryImageFormat(ur::DeviceBinaryType Type) {
switch (Type) {
Expand Down
3 changes: 2 additions & 1 deletion sycl/source/detail/jit_compiler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class jit_compiler {

private:
jit_compiler();
~jit_compiler() = default;
~jit_compiler();
jit_compiler(const jit_compiler &) = delete;
jit_compiler(jit_compiler &&) = delete;
jit_compiler &operator=(const jit_compiler &) = delete;
Expand Down Expand Up @@ -98,6 +98,7 @@ class jit_compiler {
CompileSYCLFuncT CompileSYCLHandle = nullptr;
ResetConfigFuncT ResetConfigHandle = nullptr;
AddToConfigFuncT AddToConfigHandle = nullptr;
void *MLibraryHandle = nullptr;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be a unique pointer as well, reusing the custom deleter from the newly-added unique pointer in jit_compiler's constructor? Then we wouldn't need to duplicate the unload logic in the destructor.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: The other members here don't carry the M prefix.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in c86ef55

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

title is slightly updated too.

#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