Skip to content

Commit 6fca29a

Browse files
committed
static init hacks
1 parent 0b000cd commit 6fca29a

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

backends/cuda/runtime/cuda_backend.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,22 +371,38 @@ class ET_EXPERIMENTAL CudaBackend final
371371

372372
namespace executorch::backends {
373373
namespace {
374+
// Static backend instance and registration
374375
auto cls = cuda::CudaBackend();
375376
executorch::runtime::Backend backend{"CudaBackend", &cls};
377+
378+
#ifndef _WIN32
379+
// On non-Windows platforms, use static initialization
376380
static executorch::runtime::Error success_with_compiler =
377381
register_backend(backend);
382+
#endif
383+
378384
} // namespace
379385

380-
// Export an initialization function to ensure static initializers run on Windows
386+
// Export an initialization function for explicit backend registration
381387
#ifdef _WIN32
382388
#define CUDA_BACKEND_INIT_EXPORT __declspec(dllexport)
383389
#else
384390
#define CUDA_BACKEND_INIT_EXPORT __attribute__((visibility("default")))
385391
#endif
386392

387393
extern "C" CUDA_BACKEND_INIT_EXPORT void InitCudaBackend() {
388-
// Force the static initializer to run by referencing it
394+
#ifdef _WIN32
395+
// On Windows, explicitly register the backend since DLL static initializers
396+
// don't run reliably
397+
static bool initialized = false;
398+
if (!initialized) {
399+
register_backend(backend);
400+
initialized = true;
401+
}
402+
#else
403+
// On other platforms, static initialization already happened
389404
(void)success_with_compiler;
405+
#endif
390406
}
391407

392408
} // namespace executorch::backends

0 commit comments

Comments
 (0)