Skip to content

refactor: use SpirvBuilder directly, just manage rustc_backend_spirv dylibs #69

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

Open
wants to merge 31 commits into
base: main
Choose a base branch
from

Conversation

Firestar99
Copy link
Member

@Firestar99 Firestar99 commented Apr 29, 2025

rust-gpu PR: Rust-GPU/rust-gpu#245

closes #55
closes #68
closes Rust-GPU/rust-gpu#208

The current version of cargo gpu roughly works like this, imagine it as stack frames:

  • cargo gpu
  • spawns spirv-builder-cli process
  • calls SpirvBuilder
  • spawns cargo process
  • spawns rustc process
  • loads rustc-codegen-spirv

And then we back-propagate the results:

  • rustc-codegen-spirv: writes spv files and json metadata of ShaderModules
  • Spirvbuilder: reads and parses json metadata and returns ShaderModules
  • spirv-builder-cli: converts rust types to version-independent ShaderModules and writes them to a json file
  • cargo gpu: reads json file, turns ShaderModules to Linkages (basically the same) and pretty writes a json metadata for the end user to parse

The core idea: Why not have cargo gpu directly call cargo with the required rustc-codegen-spirv flags attached, like Spirvbuilder does currently? And remove the complicated setup of spawning a new spirv-builder-cli process to call Spirvbuilder and parsing it all back out?

The key observation is that SpirvBuilder doesn't care at all what toolchain you use to build it, only it's dependency rustc_codegen_spirv has to be built by a specific toolchain. In Rust-GPU/rust-gpu#245 I make this dependency optional and hide it behind the feature rustc_codegen_spirv, which allows us to directly depend on SpirvBuilder and still build on stable. We just need to supply our SpirvBuilder with a librustc_codegen_spirv.so or rustc_codegen_spirv.dll prebuilt using the specific toolchain it requires, which significantly simplifies the build process.

This requires SpirvBuilder to stay be backwards compatible for all future versions, and we need to add support for past versions.

User-facing changes

  • Renames:
    • capability -> capabilities
    • extension -> extensions
    • shader-target -> target
    • debug -> release (clap: remains --debug)
    • no_default_features -> default_features (clap: remains --no-default-features)
    • removed rust_toolchain (as you can't use a different toolchain than the one required anyway)
  • proper path support for rust-gpu
    • will build rustc_codegen_spirv in that local path, no need to compile twice
    • will always try to rebuild
  • faster install: removed slow rust-gpu repo cloning, instead reuse cargo's cache
  • faster build: fewer cmds need to be launched or things parsed

Internal changes, including to SpirvBuilder

  • removed spirv_builder_cli crate
  • split mod spirv_cli into spirv_source, install_toolchain and lockfile
  • moved most BuildArgs members to SpirvBuilder
    • made all fields pub on SpirvBuilder, replacing setters (but keeping them around)
    • made SpirvBuilder derive serde::Serialize, serde::Deserialize
    • moved clap macro args to SpirvBuilder (masked by clap feature)
    • BuildArgs has a new SpirvBuilder member with clap(flatten) and serde(flatten)
    • required some renames, see above
  • replaced cargo tree calls with cargo_metadata crate, cherry-picked from Switch to cargo metadata #66 by @tmvkrpxl0, required to "retrieve path to manifest" during install
  • new install procedure:
  * check spirv-std version
- * clone rust-gpu repo at revision (slow)
- * check `toolchain.toml` for toolchain
- * copy `spirv-builder-cli` to cache dir
+ * write blank `rustc_codegen_spirv_dummy` project
+   * only depends on `rustc_codegen_spirv`, version equal to `spirv-std`
+ * check metadata for project
+   * cargo creates `Cargo.lock`
+   * cargo clones repo into it's cache, if necessary
+   * retrieve path to manifest, either in crates cache or cloned repo cache
+ * check `build.rs` of `rustc_codegen_spirv` for toolchain version
+   * has a **string literal** with the exact same contents as the `toolchain.toml`
+ * delete `Cargo.lock` to prevent lockfile version issues
  * build dummy project with determined toolchain
  * retrieve `librustc_codegen_spirv.so` or `rustc_codegen_spirv.dll`
-   * retrieve `spirv-builder-cli` binary
  * copy target-specs jsons
  • new build procedure:
  * cargo gpu
- * spawns `spirv-builder-cli` process
+ * set `rustc_codegen_spirv_location` and `toolchain_overwrite` on SpirvBuilder
  * calls SpirvBuilder
(...)
  * SpirvBuilder returns
- * converts `CompileResult` into `ShaderModule`s, writes to disk
- * `spirv-builder-cli` process exits
- * load `ShaderModule`s from disk
  * convert to `Linkage`s
  * write `Linkage`s to file

Todo

@Firestar99
Copy link
Member Author

@schell @tombh cargo gpu install works! build still needs to be adjusted

@Firestar99
Copy link
Member Author

I would like to see the Switch to cargo metadata PR to be merged first (or integrated into here), as you can't just use it for more efficient version querying. It also tells you where cargo has checked out the repo / downloaded the crate, which we can use to retrieve the toolchain version we need without having to clone it ourselves.

Sample:

{
  "name": "rustc_codegen_spirv",
  "version": "0.9.0",
  "id": "git+https://github.com/Rust-GPU/rust-gpu?rev=82a0f69#[email protected]",
  "license": "MIT OR Apache-2.0",
  "license_file": null,
  "description": "SPIR-V code generator backend for rustc",
  "source": "git+https://github.com/Rust-GPU/rust-gpu?rev=82a0f69#82a0f69008414f51d59184763146caa6850ac588",
  ...
  "manifest_path": "/home/firestar99/.cargo/git/checkouts/rust-gpu-d06d15e2ba0f0ae2/82a0f69/crates/rustc_codegen_spirv/Cargo.toml",
  ...
},

tmvkrpxl0 and others added 9 commits April 30, 2025 00:44
… cargo gpu

This is bandaid fix, it now ignores RUSTC environment variable set from elsewhere, like when shader crate is part of a workspace. As of today, it fails to compile on workspace which uses nightly because an additional restriction was added to target specification json file on nightly. Even though enforcing specific rustc version seems like good idea for the purpose of cargo-gpu, target specification json file should be updated as well if we were to ever bump up required rustc version.
@tombh
Copy link
Collaborator

tombh commented Apr 30, 2025

This looks great! So let me see if I have a rough idea of what the change is here. In short, we're replacing spirv-builder-cli with rustc_codegen_spirv_dummy (which in turns builds the custom SPIRV codegen backend for rustc)? The main benefit of which is that we can now interact directly with rust-gpu, significantly simplifying and stabilising cargo-gpu?

@tombh
Copy link
Collaborator

tombh commented Apr 30, 2025

I think this PR would also fix this Rust-GPU/rust-gpu#208

@Firestar99
Copy link
Member Author

Ignore rustc_codegen_spirv_dummy that only exists to depend on rustc_codegen_spirv and have cargo build it for us. I'll try to write a better summary in the description of all the changes.

@tombh
Copy link
Collaborator

tombh commented Apr 30, 2025

Wow, great write up, thank you. This is actually awesome, there's so many improvements, it's going to make a big difference.

@schell
Copy link
Collaborator

schell commented Apr 30, 2025

I'm looking forward to this work landing @Firestar99, thanks!

@Firestar99
Copy link
Member Author

c1xx: fatal error C1081: 'C:\Users<user>\AppData\Local\rust-gpu\rustc_backend_spirv_install\https___github_com_Rust-GPU_rust-gpu+eca83dfbfc0aae5c72f1e3f53081f5ccb72c27a4\target\release\build\spirv-tools-sys-0a716808b3e410a8\out\942b8c9adc6a33b2-fold_spec_constant_op_and_composite_pass.o': file name too long

Oh f*ck off windows

@Firestar99 Firestar99 force-pushed the total_refactor branch 2 times, most recently from 4920a39 to 4aebc9a Compare May 1, 2025 10:52
@Firestar99
Copy link
Member Author

Firestar99 commented May 1, 2025

Watch works on Linux, but doesn't work on windows due to rust-gpu's depfile parsing being broken, see the missing \ in the key file path, first line?

\?\C:\Users\user\workspace\frameworks\cargo-gpu\crates\shader-crate-template\target\spirv-unknown-vulkan1.2\release\rust_gpu_shader_crate_template.spv.json: ["\\?\\C:\\Users\\user\\workspace\\frameworks\\cargo-gpu\\crates\\shader-crate-template\\src\\lib.rs", "\\?\\C:\\Users\\user\\workspace\\frameworks\\cargo-gpu\\crates\\shader-crate-template\\target\\release\\deps\\spirv_std_macros-fb14afbeddf119cb.dll", "\\?\\C:\\Users\\user\\workspace\\frameworks\\cargo-gpu\\crates\\shader-crate-template\\target\\spirv-unknown-vulkan1.2\\release\\deps\\libbitflags-9fbb05a69a74b591.rlib", "\\?\\C:\\Users\\user\\workspace\\frameworks\\cargo-gpu\\crates\\shader-crate-template\\target\\spirv-unknown-vulkan1.2\\release\\deps\\libbitflags-9fbb05a69a74b591.rmeta", "\\?\\C:\\Users\\user\\workspace\\frameworks\\cargo-gpu\\crates\\shader-crate-template\\target\\spirv-unknown-vulkan1.2\\release\\deps\\libcompiler_builtins-30c147eaedb4971d.rlib", "\\?\\C:\\Users\\user\\workspace\\frameworks\\cargo-gpu\\crates\\shader-crate-template\\target\\spirv-unknown-vulkan1.2\\release\\deps\\libcore-8ebebbd1746b7c70.rlib", "\\?\\C:\\Users\\user\\workspace\\frameworks\\cargo-gpu\\crates\\shader-crate-template\\target\\spirv-unknown-vulkan1.2\\release\\deps\\libglam-5df165d896175a1d.rlib", "\\?\\C:\\Users\\user\\workspace\\frameworks\\cargo-gpu\\crates\\shader-crate-template\\target\\spirv-unknown-vulkan1.2\\release\\deps\\liblibm-de1e5ee44605b253.rlib", "\\?\\C:\\Users\\user\\workspace\\frameworks\\cargo-gpu\\crates\\shader-crate-template\\target\\spirv-unknown-vulkan1.2\\release\\deps\\liblibm-de1e5ee44605b253.rmeta", "\\?\\C:\\Users\\user\\workspace\\frameworks\\cargo-gpu\\crates\\shader-crate-template\\target\\spirv-unknown-vulkan1.2\\release\\deps\\libnum_traits-325c0de23b9ff9b5.rlib", "\\?\\C:\\Users\\user\\workspace\\frameworks\\cargo-gpu\\crates\\shader-crate-template\\target\\spirv-unknown-vulkan1.2\\release\\deps\\libnum_traits-325c0de23b9ff9b5.rmeta", "\\?\\C:\\Users\\user\\workspace\\frameworks\\cargo-gpu\\crates\\shader-crate-template\\target\\spirv-unknown-vulkan1.2\\release\\deps\\librustc_std_workspace_core-9fb42a86c2d80d14.rlib", "\\?\\C:\\Users\\user\\workspace\\frameworks\\cargo-gpu\\crates\\shader-crate-template\\target\\spirv-unknown-vulkan1.2\\release\\deps\\librustc_std_workspace_core-9fb42a86c2d80d14.rmeta", "\\?\\C:\\Users\\user\\workspace\\frameworks\\cargo-gpu\\crates\\shader-crate-template\\target\\spirv-unknown-vulkan1.2\\release\\deps\\libspirv_std-bd942fd01083ffa9.rlib", "\\?\\C:\\Users\\user\\workspace\\frameworks\\cargo-gpu\\crates\\shader-crate-template\\target\\spirv-unknown-vulkan1.2\\release\\deps\\libspirv_std_types-ea31211670fb7009.rlib", "\\?\\C:\\Users\\user\\workspace\\frameworks\\cargo-gpu\\crates\\shader-crate-template\\target\\spirv-unknown-vulkan1.2\\release\\deps\\libspirv_std_types-ea31211670fb7009.rmeta", "C:\\Users\\user\\AppData\\Local\\rust-gpu\\codegen\\https___github_com_Rust-GPU_rust-gpu+82a0f690\\rustc_codegen_spirv.dll"]
recurse:
\\?\C:\Users\user\workspace\frameworks\cargo-gpu\crates\shader-crate-template\target\spirv-unknown-vulkan1.2\release\rust_gpu_shader_crate_template.spv.json

Has it ever worked before my refactor? Had other difficulties testing that.

@Firestar99
Copy link
Member Author

Firestar99 commented May 1, 2025

@tombh @schell @LegNeato I think this is ready for review. The merge procedure will be to first merge Rust-GPU/rust-gpu#245, then update this PR to point to rust-gpu's master and merge it as well (which is why I won't yet mark this PR as ready). I think the remaining todos can be done in followup PRs.

Please don't squash these commits on either PR, they change so much it would probably be better to keep them around.

@Firestar99 Firestar99 requested review from schell and tombh and removed request for schell May 1, 2025 13:47
Copy link
Collaborator

@tombh tombh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything looks great. Just a couple of minor suggestions.

@Firestar99 Firestar99 marked this pull request as ready for review May 6, 2025 10:55
@Firestar99
Copy link
Member Author

Firestar99 commented May 6, 2025

@tombh feel free to rereview and merge, but I got one followup question:

log::info!("installing cargo gpu artifacts");
cmd([
"cargo",
"gpu",
"install",
"--shader-crate",
SHADER_CRATE_PATH,
"--auto-install-rust-toolchain",
"--force-overwrite-lockfiles-v4-to-v3",
])
.unwrap();
let dir = tempdir::TempDir::new("test-shader-output").unwrap();
let mut overwriter = ShaderCrateTemplateCargoTomlWriter::new();
overwriter.replace_output_dir(dir.path()).unwrap();
if let Some(rust_gpu_version) = maybe_rust_gpu_version {
if rust_gpu_version != "latest" {
overwriter
.replace_spirv_std_version(rust_gpu_version)
.unwrap();
}
}
cmd([
"cargo",
"gpu",
"build",
"--shader-crate",
SHADER_CRATE_PATH,
"--auto-install-rust-toolchain",
"--force-spirv-cli-rebuild",
"--force-overwrite-lockfiles-v4-to-v3",
])
.unwrap();

Why are we installing one rust-gpu version (whatever is in the repo, currently a rev), then replace the version with what was specified in the cmdline and when building, reinstall a different rust-gpu version? I was wondering why our 0.9.0 and 0.8.0 builds were taking twice as much as the latest does. Any specific reason it has been done like that, or should we unify to testing only one version?

@tombh
Copy link
Collaborator

tombh commented May 7, 2025

Wow, that doesn't seem right. I can't think of any reason why that might be. I wonder if @schell does? I think as long as the tests pass then just a single cargo build ... should be enough?

@tmvkrpxl0
Copy link

I like how we are discovering what it does along the way

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants