Skip to content

Allow instructions to explicitly specify StorageClasses #236

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

jwollen
Copy link
Contributor

@jwollen jwollen commented Apr 19, 2025

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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant