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

Allow keeping linkage annotation

Open SiebenCorgie opened this issue 2 years ago • 2 comments

SpirV allows defining import and export [0] for linking separate Shader modules [1] post compilation.

Apparently SpirV linking is already used within rust-gpu, but not exposed. Therefore we can't use extern "C" as we usually would.

This functionality would be useful, for instance when building shader permutation systems.

The enhancement would basically be allowing extern C, #[no_mangle] etc. to map to (possibly named) import/export decorated values whenever the Linkage capability on spirv-builder is enabled.

SiebenCorgie avatar Apr 21 '23 16:04 SiebenCorgie

Note that extern "C" exports in Rust only affect ABI. #[no_mangle] + #[export_name = "..."] is how you control symbol names for exports.

Anyway, Linkage is really the only prerequisite, we remove our annotations today because it's not really supported anywhere I can think of. This is the relevant code: https://github.com/EmbarkStudios/rust-gpu/blob/c1b0b5de0054eb82ff817a45406f2fc0ca3e9edf/crates/rustc_codegen_spirv/src/linker/import_export_link.rs#L234-L247

Oh, right, that's kind of funny, there's already a keep_link_exports meant for testing.

You'll have to be careful tho, to not search for the Capability::Linkage in the SPIR-V Module (since it sounds like that one is always present, in the interest of keeping the module valid?), but rather determine "did the user ask for the Linkage capability" from however target_features (from Session) turn into capabilities.

eddyb avatar Apr 24 '23 06:04 eddyb