From 8dbc7208de5a7aa1394904b1acbf4ee93f6f3249 Mon Sep 17 00:00:00 2001 From: Andrzej Janik Date: Mon, 28 Jul 2025 22:20:04 +0200 Subject: [PATCH] Try to make ZLUDA more robust on Windows (#442) On my machine ZLUDA seems to segfault when initializing LLVM's C++ statics in Blender. Blender ships with C++ runtime. It seems that compiling C++ runtime statically fixes the issue. Might be actually unrelated. Additionally, dtor crate on Windows seem to use a slightly dodgy method, so replace it with something more straightforward --- .cargo/config.toml | 5 ++++- llvm_zluda/build.rs | 4 ++-- zluda/Cargo.toml | 2 +- zluda/src/lib.rs | 8 +++++++- zluda/src/os_unix.rs | 0 zluda/src/os_win.rs | 13 +++++++++++++ 6 files changed, 27 insertions(+), 5 deletions(-) create mode 100644 zluda/src/os_unix.rs create mode 100644 zluda/src/os_win.rs diff --git a/.cargo/config.toml b/.cargo/config.toml index 1b4298a..6184c05 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,2 +1,5 @@ [alias] -xtask = "run --package xtask --" \ No newline at end of file +xtask = "run --package xtask --" + +[target.x86_64-pc-windows-msvc] +rustflags = ["-Ctarget-feature=+crt-static"] diff --git a/llvm_zluda/build.rs b/llvm_zluda/build.rs index 722dc42..9505a86 100644 --- a/llvm_zluda/build.rs +++ b/llvm_zluda/build.rs @@ -19,8 +19,8 @@ fn main() { cmake // It's not like we can do anything about the warnings .define("LLVM_ENABLE_WARNINGS", "OFF") - // For some reason Rust always links to release MSVCRT - .define("CMAKE_MSVC_RUNTIME_LIBRARY", "MultiThreadedDLL") + // For some reason Rust always links to release CRT + .define("CMAKE_MSVC_RUNTIME_LIBRARY", "MultiThreaded") .define("LLVM_ENABLE_TERMINFO", "OFF") .define("LLVM_ENABLE_LIBXML2", "OFF") .define("LLVM_ENABLE_LIBEDIT", "OFF") diff --git a/zluda/Cargo.toml b/zluda/Cargo.toml index 0c6b5de..61de140 100644 --- a/zluda/Cargo.toml +++ b/zluda/Cargo.toml @@ -22,13 +22,13 @@ lz4-sys = "1.9" tempfile = "3" paste = "1.0" rustc-hash = "1.1" -dtor = "0.0.6" [target.'cfg(windows)'.dependencies] winapi = { version = "0.3", features = ["heapapi", "std"] } [target.'cfg(not(windows))'.dependencies] libc = "0.2" +dtor = "0.0.6" [package.metadata.zluda] linux_symlinks = [ diff --git a/zluda/src/lib.rs b/zluda/src/lib.rs index 2da4c76..b6ced82 100644 --- a/zluda/src/lib.rs +++ b/zluda/src/lib.rs @@ -1,12 +1,18 @@ use cuda_types::cuda::CUerror; use std::sync::atomic::{AtomicBool, Ordering}; + + +#[cfg_attr(windows, path = "os_win.rs")] +#[cfg_attr(not(windows), path = "os_unix.rs")] +mod os; pub(crate) mod r#impl; static INITIALIZED: AtomicBool = AtomicBool::new(true); pub(crate) fn initialized() -> bool { INITIALIZED.load(Ordering::SeqCst) } -#[dtor::dtor] + +#[cfg_attr(not(windows), dtor::dtor)] fn deinitialize() { INITIALIZED.store(false, Ordering::SeqCst); } diff --git a/zluda/src/os_unix.rs b/zluda/src/os_unix.rs new file mode 100644 index 0000000..e69de29 diff --git a/zluda/src/os_win.rs b/zluda/src/os_win.rs new file mode 100644 index 0000000..a2936d3 --- /dev/null +++ b/zluda/src/os_win.rs @@ -0,0 +1,13 @@ +use std::ffi::c_void; + +const DLL_PROCESS_DETACH: u32 = 0; + +#[allow(non_snake_case, unused_variables)] +#[no_mangle] +extern "system" fn DllMain(_dll_module: *const c_void, call_reason: u32, _: *const c_void) -> bool { + if call_reason == DLL_PROCESS_DETACH { + super::deinitialize(); + } + + true +}