Skip to content

Allow instructions to explicitly specify StorageClasses#236

Draft
jwollen wants to merge 4 commits into
Rust-GPU:mainfrom
jwollen:explicit-storage-class
Draft

Allow instructions to explicitly specify StorageClasses#236
jwollen wants to merge 4 commits into
Rust-GPU:mainfrom
jwollen:explicit-storage-class

Conversation

@jwollen

@jwollen jwollen commented Apr 19, 2025

Copy link
Copy Markdown
Contributor

Summary

This adds an optional StorageClass to SpirvType::Pointer to allow instructions to explicitly specify it on their results.

Required for

Motivation

Currently storage classes are only inferred from interface variables. However, there is a number of instructions that have results with implicit storage classes (i.e. that "create provenance" on the fly).
These include

  • OpConvertUToPtr creates a PhysicalStorageBuffer pointer
  • OpBitcast can be used to create a PhysicalStorageBuffer pointer from u64 or uvec2 (if Int64 is not supported)
  • OpImageTexelPointer creates an Image pointer
  • future bindless extensions will need to create Uniform/StorageBuffer pointers

It should be possible to generate these both in the compiler (e.g. u64 as *const T) and through asm! for unknown instructions.

This is a requirement for the PhysicalStorageBuffer addressing model as well as upcoming more modern bindless extensions.

For example we can now write (or generate in inttoptr)

fn convert_u_to_ptr<T>(addr: u64) -> *const T {
    let result: *const T;
    unsafe {
        let dummy: T = core::mem::MaybeUninit::uninit().assume_init(); // :(
        asm!(
            "%ptr_type = OpTypePointer PhysicalStorageBuffer typeof*{dummy}",
            "{result} = OpConvertUToPtr %ptr_type {addr}",
            addr = in(reg) addr,
            dummy = in(reg) &dummy,
            result = out(reg) result,
        );
    }
    result
}

Implementation steps

  • Add an optional storage class to pointer types
  • Remove the Generic requirement in asm!. Generic is still used to indicate automatic inferrence.
  • Storage class inference
    • Initial hackish support
    • This works seemlessly qptr storage class inference.
    • Either generate InferVars for non-Generic storage classes or infer instances based on concrete types
  • Add API for OpImageTexelPointer
  • Add tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant