@@ -98,12 +98,18 @@ compileToSPIRV(string_view Src, sycl::info::device_type DeviceType,
9898#else
9999 static const std::string OclocLibraryName = " libocloc.so" ;
100100#endif
101- void *OclocLibrary = sycl::detail::ur::loadOsLibrary (OclocLibraryName);
101+ auto CustomDeleter = [](void *StoredPtr) {
102+ if (!StoredPtr)
103+ return ;
104+ std::ignore = sycl::detail::ur::unloadOsLibrary (StoredPtr);
105+ };
106+ std::unique_ptr<void , decltype (CustomDeleter)> OclocLibrary (
107+ sycl::detail::ur::loadOsLibrary (OclocLibraryName), CustomDeleter);
102108 if (!OclocLibrary)
103109 throw online_compile_error (" Cannot load ocloc library: " +
104110 OclocLibraryName);
105- void *OclocVersionHandle =
106- sycl::detail::ur::getOsLibraryFuncAddress ( OclocLibrary, " oclocVersion" );
111+ void *OclocVersionHandle = sycl::detail::ur::getOsLibraryFuncAddress (
112+ OclocLibrary. get () , " oclocVersion" );
107113 // The initial versions of ocloc library did not have the oclocVersion()
108114 // function. Those versions had the same API as the first version of ocloc
109115 // library having that oclocVersion() function.
@@ -129,18 +135,21 @@ compileToSPIRV(string_view Src, sycl::info::device_type DeviceType,
129135 std::to_string (CurrentVersionMajor) +
130136 " .N), where (N >= " + std::to_string (CurrentVersionMinor) + " )." );
131137
132- CompileToSPIRVHandle =
133- sycl::detail::ur::getOsLibraryFuncAddress ( OclocLibrary, " oclocInvoke" );
138+ CompileToSPIRVHandle = sycl::detail::ur::getOsLibraryFuncAddress (
139+ OclocLibrary. get () , " oclocInvoke" );
134140 if (!CompileToSPIRVHandle)
135141 throw online_compile_error (" Cannot load oclocInvoke() function" );
136142 FreeSPIRVOutputsHandle = sycl::detail::ur::getOsLibraryFuncAddress (
137- OclocLibrary, " oclocFreeOutput" );
138- if (!FreeSPIRVOutputsHandle)
143+ OclocLibrary.get (), " oclocFreeOutput" );
144+ if (!FreeSPIRVOutputsHandle) {
145+ CompileToSPIRVHandle = NULL ;
139146 throw online_compile_error (" Cannot load oclocFreeOutput() function" );
147+ }
148+ OclocLibrary.release ();
140149 }
141150
142151 std::string CombinedUserArgs;
143- for (auto UserArg : UserArgs) {
152+ for (const auto & UserArg : UserArgs) {
144153 if (UserArg == " " )
145154 continue ;
146155 if (CombinedUserArgs != " " )
0 commit comments