77 */
88
99#include < cuda_runtime.h>
10- #include < dlfcn.h>
1110#include < executorch/runtime/backend/interface.h>
1211#include < executorch/runtime/core/error.h>
1312#include < executorch/runtime/core/evalue.h>
1413#include < executorch/runtime/core/exec_aten/util/tensor_util.h>
15- #include < unistd.h>
1614#include < cstdio>
1715
1816#include < filesystem>
2321// Include our shim layer headers
2422#include < executorch/backends/aoti/aoti_delegate_handle.h>
2523#include < executorch/backends/aoti/common_shims.h>
24+ #include < executorch/backends/cuda/runtime/platform/platform.h>
2625#include < executorch/backends/cuda/runtime/shims/memory.h>
2726#include < executorch/backends/cuda/runtime/utils.h>
2827
2928namespace executorch ::backends::cuda {
3029
31- #define LOAD_SYMBOL (handle, member, name, so_handle ) \
32- do { \
33- handle->member = reinterpret_cast <name##Func>(dlsym (so_handle, #name)); \
34- ET_CHECK_OR_RETURN_ERROR ( \
35- handle->member != nullptr , AccessFailed, " Failed to load " #name); \
30+ #define LOAD_SYMBOL (handle, member, name, so_handle ) \
31+ do { \
32+ auto symbol_res = get_function (so_handle, #name); \
33+ if (!symbol_res.ok ()) { \
34+ return symbol_res.error (); \
35+ } \
36+ handle->member = reinterpret_cast <name##Func>(symbol_res.get ()); \
3637 } while (0 )
3738
3839using namespace std ;
@@ -122,10 +123,10 @@ class ET_EXPERIMENTAL CudaBackend final
122123 // Generate dynamic temporary file path
123124 filesystem::path temp_dir = filesystem::temp_directory_path ();
124125 filesystem::path so_path =
125- temp_dir / (so_blob_key + to_string (getpid ()) + " .so" );
126+ temp_dir / (so_blob_key + to_string (get_process_id ()) + " .so" );
126127
127128 // Create a temporary file
128- ofstream outfile (so_path. c_str () , ios::binary);
129+ ofstream outfile (so_path, ios::binary);
129130
130131 // Write the ELF buffer to the temporary file
131132 ET_LOG (
@@ -144,24 +145,23 @@ class ET_EXPERIMENTAL CudaBackend final
144145 // Finish writing the file to disk
145146 outfile.close ();
146147
147- // Load the ELF using dlopen
148- void * so_handle = dlopen (so_path.c_str (), RTLD_LAZY | RTLD_LOCAL);
149- ET_CHECK_OR_RETURN_ERROR (
150- so_handle != nullptr ,
151- AccessFailed,
152- " Failed to load shared library: %s" ,
153- dlerror ());
148+ // Load the lib
149+ Result<void *> lib_handle_res = load_library (so_path);
150+ if (!lib_handle_res.ok ()) {
151+ return lib_handle_res.error ();
152+ }
153+ void * lib_handle = lib_handle_res.get ();
154154
155155 processed->Free ();
156156
157157 // Create handle and load function pointers into it
158158 AOTIDelegateHandle* handle = new AOTIDelegateHandle ();
159- handle->so_handle = so_handle ;
159+ handle->so_handle = lib_handle ;
160160 handle->so_path = so_path.string ();
161161
162162 // Load function pointers specific to this handle's shared library
163163 ET_CHECK_OK_OR_RETURN_ERROR (
164- load_function_pointers_into_handle (so_handle , handle));
164+ load_function_pointers_into_handle (lib_handle , handle));
165165
166166 AOTInductorModelContainerHandle container_handle = nullptr ;
167167
@@ -332,8 +332,9 @@ class ET_EXPERIMENTAL CudaBackend final
332332 // AOTInductorModelContainerDelete(handle->container_handle);
333333
334334 // Now close the shared library
335+ auto err = Error::Ok;
335336 if (handle->so_handle != nullptr ) {
336- dlclose (handle->so_handle );
337+ err = close_library (handle->so_handle );
337338 }
338339
339340 // Remove the temporary shared library file
0 commit comments