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

Enums MVP.

Open ElectronicRU opened this issue 3 years ago • 11 comments

Supported cases:

  • non-repr-C enums without non-null optimization;
  • repr-C enums that are dataless / have the same layout for every case
  • enums with non-null optimization that don't use pointers (e.g. Option<NonZeroUsize>).

Future work directions:

  • Option<&_> and such - currently emits zombies/invalid SPIR-V, likely requires adaptations in mem2reg
  • repr(C) enums that can be represented as (discriminant, union) - some of the groundwork is already there

ElectronicRU avatar Dec 07 '21 14:12 ElectronicRU

Would this mean that one can use things like Option<T> or algebraic enums, e.g.? As long as T is not a pointer?

I didn't get to play around with rust-gpu that much yet, my understanding was that so far only data-less enums work (#78).

joeftiger avatar Aug 25 '22 17:08 joeftiger

@joeftiger yes. With pointers, it can generate incorrect SPIR-V, because operations on pointers are heavily restricted and amending that would require basically a full-program analysis that I haven't come up with.

ElectronicRU avatar Oct 18 '22 11:10 ElectronicRU

This has been open for nearly a year. Is it still ready and just waiting for a review?

Keavon avatar Nov 12 '22 01:11 Keavon

i've roughly merged this into latest version here

i compiled this code

struct Nah {
    a: u32
}

enum Foo {
    A(Nah),
    B,
}

#[spirv(compute(threads(64)))]
pub fn main(
    #[spirv(global_invocation_id)] id: UVec3,
    #[spirv(storage_buffer, descriptor_set = 0, binding = 1)] output: &mut [u32],
) {
    let aa = Foo::A(Nah { a: 0xffffffff });

    if let Foo::A(a) = aa {
        output[id.x as usize] = a.a;
    } else {
        output[id.x as usize] = 99;
    }
}

it fails to compile

however if i revert providers.layout_of changes it works

need further research

djdisodo avatar Jan 16 '23 15:01 djdisodo

@djdisodo do you mean the deoverlay_enum_fields change etc? @Keavon there were some unresolved issues with this code, and ultimately, not much desire from Embark to do this. No-one wanted to do a massive code review, I guess.

@eddyb there seems to still be interest in this feature - I can probably bring it up to date, write some more tests, etc. Unfortunately I never did summon the courage required to make it work with pointers.

ElectronicRU avatar Feb 02 '23 20:02 ElectronicRU

@ElectronicRU that would be awesome if you could return to this and get it landed now that the project seems to be picking back up on Embark's end. Our project https://github.com/GraphiteEditor/Graphite needs it for our node graph compositing shaders, which are written in Rust, to work with (some degree of) generic types in our node graph system.

Keavon avatar Feb 02 '23 21:02 Keavon

This is fundamental Rust, we need some form of this feature; if not, this.

brynnbrancamp avatar Mar 17 '23 00:03 brynnbrancamp

What is the status on this feature? I'm running into issues with Option that I feel are related to this, specifically with Option<T> where T is a struct.

Lucky4Luuk avatar Apr 20 '23 08:04 Lucky4Luuk

@Lucky4Luuk sorry - life & work take up a lot of time, I don't have any to work on this and won't for the foreseeable future :( Someone else would need to push it through for now, sorry.

ElectronicRU avatar May 05 '23 09:05 ElectronicRU