MaikKlein / rlsl

Rust to SPIR-V compiler
Apache License 2.0
557 stars 14 forks source link

Allow functions to return references #43

Closed MaikKlein closed 6 years ago

MaikKlein commented 6 years ago

SPIR-V only allowes to return values by copy. #[inline(always)] in Rust doesn't seem to force inlining.

This is a needed feature because a lot of things in Rust like ops::Index and ops::Deref return a reference.

A temporary workaround would be to implement an intrinsic for accessing elements in a storage buffer.

A possible implementation could be to transform functions from

fn index(&self, idx: usize) -> &Foo{..}

to

fn index(&self, idx: usize, output: &mut Foo){..}

Possibly those functions would need to be annotated with something like

#[spirv(transform_ref)]
fn index(&self, idx: usize) -> &Foo{..}

Another possibility would be to force inlining, by inserting those functions directly into the mir where they are used.

MaikKlein commented 6 years ago

Might be easier than expected if we can use the mir::transoform::Inline to always inline functions that return a reference.

MaikKlein commented 6 years ago

Almost fully implemented. Currently requires #[inline(always)] on top of functions that return a reference. Those functions are then inlined manually and removed.

https://github.com/MaikKlein/rlsl/issues/45