EmbarkStudios / rust-gpu

🐉 Making Rust a first-class language and ecosystem for GPU shaders 🚧
https://shader.rs
Apache License 2.0
7.31k stars 246 forks source link

Optimize `spirv-builder` clean build wall time #858

Open repi opened 2 years ago

repi commented 2 years ago

We should look into improve our clean build times of when building spriv-builder & rustc_codegen_spirv as that is the main scenario in CI and also useful for users if our crates rebuild fast.

Right now it looks like we have a couple of quite unfortunate serial dependencies that significantly increases the wall time. Here is the current results with cargo build --release -p spirv-builder -Z timings on my AMD 5950x (32 vCPU):

image

Potential optimizations


If we do get rid of serde and manage to make sure rustc_codegen_spirv doesn't have to wait for the spirv-tools-sys build, we could get get a 15+ second wall time improvement here as rustc_codegen_spirv could start as soon as frontend section of rspirv has been built:

repi commented 2 years ago

Did an experiment to switch to nanoserde and it seems to work fine, there are some contraints and type/format output changes that one have to figure out (no PathBuf support), and does get rid of all the long serde compilation, 20 sec -> 1.7 sec.

Wall time benefit was just ~1 second though, but would enable real gains if one also finds a way not have rustc_codegen_spirv stall and wait for spirv-tools-sys build.

image

repi commented 2 years ago

Determine why rustc_codegen_spirv is not split in frontend/codegen sections in the profile report. This would enable spriv-builder itself to start building earlier and before the codegen of rustc_codegen_spirv is done.

Found the source of this, rustc_codegen_spirv was set as a dylib crate instead of standard lib.

Fixing this, together with the above experimental nanoserde switch does reduce build time from original 46.3s to 41.9s, one can see in the report that rustc_codegen_spirv is now split in frontend and codegen sections which enables spirv-builder to build earlier.

image

bjorn3 commented 2 years ago

You could split rustc_codegen_spirv into an rlib containing the actual implementation and a dylib which only depends on the rlib. The rlib will participate in pipelined compilation and the dylib will wait for all dependencies to have fully compiled before doing the final link. It may also be possible to move just the code using spirv-tools to the dylib, allowing most of the code of cg_spirv to be compiled at the same time as spirv-tools-sys.

repi commented 2 years ago

ah that is an interesting idea and approach, what do you think of the feasibility of that @eddyb ?

eddyb commented 2 years ago

Ahh I always forget about how dylibs behave just like executables, and the current state of pipelining - it makes sense in the grand scheme of there being no good way (yet) to block rustc invoking the linker, on the linker inputs actually being present, but it's basically a Cargo<->rustc communicative deficiency AFAIK, not even something limited by platform tooling.

What @bjorn3 is suggesting is basically "emulate what should happen, with today's tools" and we should probably do that.

If @oisyn wants to tackle this, a first approximation could be as simple as:

For @bjorn3's complete suggestion, I'm guessing this needs to go in the dylib: https://github.com/EmbarkStudios/rust-gpu/blob/105cbcc6184ac78fcb954a525895d49f39733159/crates/rustc_codegen_spirv/src/lib.rs#L515-L561

With that Box::new(SpirvCodegenBackend) at the very end being changed to Box::new(SpirvCodegenBackend { spirv_tools: ... }) (i.e. rustc_codegen_spirv-impl's definition of SpirvCodegenBackend needs a new field that holds some kind of trait object, function pointers, etc. - some mechanism to dynamically get access to the functionality we need from spirv_tools without it being statically accessed from rustc_codegen_spirv-impl).