@@ -23,29 +23,38 @@ namespace sycl {
2323inline namespace _V1 {
2424namespace detail {
2525
26+ std::function<void (void *)> jit_compiler::CustomDeleterForLibHandle =
27+ [](void *StoredPtr) {
28+ if (!StoredPtr)
29+ return ;
30+ std::ignore = sycl::detail::ur::unloadOsLibrary (StoredPtr);
31+ };
32+
2633static inline void printPerformanceWarning (const std::string &Message) {
2734 if (detail::SYCLConfig<detail::SYCL_RT_WARNING_LEVEL>::get () > 0 ) {
2835 std::cerr << " WARNING: " << Message << " \n " ;
2936 }
3037}
3138
32- jit_compiler::jit_compiler () {
39+ jit_compiler::jit_compiler ()
40+ : LibraryHandle(nullptr , CustomDeleterForLibHandle) {
3341 auto checkJITLibrary = [this ]() -> bool {
3442#ifdef _WIN32
3543 static const std::string dir = sycl::detail::OSUtil::getCurrentDSODir ();
3644 static const std::string JITLibraryName = dir + " \\ " + " sycl-jit.dll" ;
3745#else
3846 static const std::string JITLibraryName = " libsycl-jit.so" ;
3947#endif
40-
41- void *LibraryPtr = sycl::detail::ur::loadOsLibrary (JITLibraryName);
48+ std::unique_ptr<void , decltype (CustomDeleterForLibHandle)> LibraryPtr (
49+ sycl::detail::ur::loadOsLibrary (JITLibraryName),
50+ CustomDeleterForLibHandle);
4251 if (LibraryPtr == nullptr ) {
4352 printPerformanceWarning (" Could not find JIT library " + JITLibraryName);
4453 return false ;
4554 }
4655
4756 this ->AddToConfigHandle = reinterpret_cast <AddToConfigFuncT>(
48- sycl::detail::ur::getOsLibraryFuncAddress (LibraryPtr,
57+ sycl::detail::ur::getOsLibraryFuncAddress (LibraryPtr. get () ,
4958 " addToJITConfiguration" ));
5059 if (!this ->AddToConfigHandle ) {
5160 printPerformanceWarning (
@@ -54,7 +63,7 @@ jit_compiler::jit_compiler() {
5463 }
5564
5665 this ->ResetConfigHandle = reinterpret_cast <ResetConfigFuncT>(
57- sycl::detail::ur::getOsLibraryFuncAddress (LibraryPtr,
66+ sycl::detail::ur::getOsLibraryFuncAddress (LibraryPtr. get () ,
5867 " resetJITConfiguration" ));
5968 if (!this ->ResetConfigHandle ) {
6069 printPerformanceWarning (
@@ -63,7 +72,8 @@ jit_compiler::jit_compiler() {
6372 }
6473
6574 this ->FuseKernelsHandle = reinterpret_cast <FuseKernelsFuncT>(
66- sycl::detail::ur::getOsLibraryFuncAddress (LibraryPtr, " fuseKernels" ));
75+ sycl::detail::ur::getOsLibraryFuncAddress (LibraryPtr.get (),
76+ " fuseKernels" ));
6777 if (!this ->FuseKernelsHandle ) {
6878 printPerformanceWarning (
6979 " Cannot resolve JIT library function entry point" );
@@ -73,21 +83,22 @@ jit_compiler::jit_compiler() {
7383 this ->MaterializeSpecConstHandle =
7484 reinterpret_cast <MaterializeSpecConstFuncT>(
7585 sycl::detail::ur::getOsLibraryFuncAddress (
76- LibraryPtr, " materializeSpecConstants" ));
86+ LibraryPtr. get () , " materializeSpecConstants" ));
7787 if (!this ->MaterializeSpecConstHandle ) {
7888 printPerformanceWarning (
7989 " Cannot resolve JIT library function entry point" );
8090 return false ;
8191 }
8292
8393 this ->CompileSYCLHandle = reinterpret_cast <CompileSYCLFuncT>(
84- sycl::detail::ur::getOsLibraryFuncAddress (LibraryPtr, " compileSYCL" ));
94+ sycl::detail::ur::getOsLibraryFuncAddress (LibraryPtr.get (),
95+ " compileSYCL" ));
8596 if (!this ->CompileSYCLHandle ) {
8697 printPerformanceWarning (
8798 " Cannot resolve JIT library function entry point" );
8899 return false ;
89100 }
90-
101+ LibraryHandle = std::move (LibraryPtr);
91102 return true ;
92103 };
93104 Available = checkJITLibrary ();
0 commit comments