rust-gpu icon indicating copy to clipboard operation
rust-gpu copied to clipboard

spirv-std inline `asm!` should use `MaybeUninit<T>` instead of `let mut result = T::default();`.

Open eddyb opened this issue 2 years ago • 1 comments

We have a few different ways to extract results from asm!:

  • old unsound approach (UB because of OpReturnValue, so it's getting replaced)
asm! {
    "%result = ...",
    "OpReturnValue %result",
    options(noreturn),
}
  • old approach (sound, but requires T: Default)
let mut result = T::default();
asm!(
    "%result = ...",
    "OpStore {result} %result",
    result = in(reg) &mut result,
);
result
  • new approach (using MaybeUninit<T>), from #1006
let mut result_slot = core::mem::MaybeUninit::uninit();
asm! {
    "%result = ...",
    "OpStore {result_slot} %result",
    result_slot = in(reg) result_slot.as_mut_ptr(),
}
result_slot.assume_init()

In #1006 only the unsound uses of asm!("OpReturnValue") were fixed (i.e. when dealing with opaque handles or &T/&mut T, neither of which implement Default).

However, it might be a good to transition everything uniformly, and maybe even provide a wrapper macro that allows uniformly using asm! with the result slot automatically handled (and even by-ref inputs as well?).

eddyb avatar Mar 17 '23 20:03 eddyb