Skip to content

Commit 6bd2d84

Browse files
committed
Fix: fix ModuleJitOptions not working due to cursed reasons
1 parent d6d98b7 commit 6bd2d84

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

crates/cust/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
Notable changes to this project will be documented in this file.
44

5+
## 0.3.2 - 2/16/22
6+
7+
- Fixed `ModuleJitOptions` not working (due to cursed reasons).
8+
59
## 0.3.1 - 2/11/22
610

711
No changes, updated to cust_derive 0.2 to fix a bug.

crates/cust/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "cust"
3-
version = "0.3.1"
3+
version = "0.3.2"
44
# Big thanks to the original author of rustacuda <3
55
authors = [
66
"Riccardo D'Ambrosio <[email protected]>",

crates/cust/src/module.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,36 +92,46 @@ pub enum ModuleJitOption {
9292

9393
impl ModuleJitOption {
9494
pub fn into_raw(opts: &[Self]) -> (Vec<cuda::CUjit_option>, Vec<*mut c_void>) {
95+
// And here we stumble across one of the most horrific things i have ever seen in my entire
96+
// journey of working with many parts of CUDA. As a background, CUDA usually wants an array
97+
// of pointers to values when it takes void**, after all, this is what is expected by anyone.
98+
// However, there is a SINGLE exception in the entire driver API, and that is cuModuleLoadDataEx,
99+
// it actually wants you to pass values by value instead of by ref if they fit into pointer length.
100+
// Therefore something like MaxRegisters should be passed as `u32 as usize as *mut c_void`.
101+
// This is completely undocumented. I initially brought this up to an nvidia developer,
102+
// who eventually was able to figure out this issue, currently it appears to be labeled "not a bug",
103+
// however this will likely be changed in the future, or at least get documented better. (hopefully)
95104
let mut raw_opts = Vec::with_capacity(opts.len());
96105
let mut raw_vals = Vec::with_capacity(opts.len());
106+
97107
for opt in opts {
98108
match opt {
99109
Self::MaxRegisters(regs) => {
100110
raw_opts.push(cuda::CUjit_option::CU_JIT_MAX_REGISTERS);
101-
raw_vals.push(regs as *const u32 as *mut _);
111+
raw_vals.push(*regs as usize as *mut c_void);
102112
}
103113
Self::OptLevel(level) => {
104114
raw_opts.push(cuda::CUjit_option::CU_JIT_OPTIMIZATION_LEVEL);
105-
raw_vals.push(level as *const OptLevel as *mut _);
115+
raw_vals.push(*level as usize as *mut c_void);
106116
}
107117
Self::DetermineTargetFromContext => {
108118
raw_opts.push(cuda::CUjit_option::CU_JIT_TARGET_FROM_CUCONTEXT);
109119
}
110120
Self::Target(target) => {
111121
raw_opts.push(cuda::CUjit_option::CU_JIT_TARGET);
112-
raw_vals.push(target as *const JitTarget as *mut _);
122+
raw_vals.push(*target as usize as *mut c_void);
113123
}
114124
Self::Fallback(fallback) => {
115125
raw_opts.push(cuda::CUjit_option::CU_JIT_FALLBACK_STRATEGY);
116-
raw_vals.push(fallback as *const JitFallback as *mut _);
126+
raw_vals.push(*fallback as usize as *mut c_void);
117127
}
118128
Self::GenenerateDebugInfo(gen) => {
119129
raw_opts.push(cuda::CUjit_option::CU_JIT_GENERATE_DEBUG_INFO);
120-
raw_vals.push(gen as *const bool as *mut _);
130+
raw_vals.push(*gen as usize as *mut c_void);
121131
}
122132
Self::GenerateLineInfo(gen) => {
123133
raw_opts.push(cuda::CUjit_option::CU_JIT_GENERATE_LINE_INFO);
124-
raw_vals.push(gen as *const bool as *mut _)
134+
raw_vals.push(*gen as usize as *mut c_void)
125135
}
126136
}
127137
}

0 commit comments

Comments
 (0)