From 5ac500d5b96145d72da6fe2a1059bd4e2a34b5e8 Mon Sep 17 00:00:00 2001 From: Eduard-Mihai Burtescu Date: Wed, 30 Mar 2022 10:07:14 +0300 Subject: [PATCH] Split out a new `-types` crate so `spirv-builder` stops loading LLVM via dylibs. (#856) * Split out a new `-types` crate so `spirv-builder` stops loading LLVM via dylibs. * example-wgpu-runner: halve `max_push_constant_size` so it works on RADV/Fiji. --- Cargo.lock | 10 +++++++ Cargo.toml | 1 + crates/rustc_codegen_spirv-types/Cargo.toml | 12 ++++++++ .../src/compile_result.rs | 24 +++++++-------- crates/rustc_codegen_spirv-types/src/lib.rs | 6 ++++ crates/rustc_codegen_spirv/Cargo.toml | 1 + crates/rustc_codegen_spirv/src/lib.rs | 3 -- crates/rustc_codegen_spirv/src/link.rs | 30 +++++++++++-------- crates/spirv-builder/Cargo.toml | 1 + crates/spirv-builder/src/lib.rs | 4 +-- crates/spirv-builder/src/watch.rs | 2 +- examples/runners/wgpu/src/graphics.rs | 2 +- 12 files changed, 63 insertions(+), 33 deletions(-) create mode 100644 crates/rustc_codegen_spirv-types/Cargo.toml rename crates/{rustc_codegen_spirv => rustc_codegen_spirv-types}/src/compile_result.rs (86%) create mode 100644 crates/rustc_codegen_spirv-types/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 1e46fd577e..de1fa33cba 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2006,6 +2006,7 @@ dependencies = [ "pretty_assertions", "rspirv", "rustc-demangle", + "rustc_codegen_spirv-types", "sanitize-filename", "serde", "serde_json", @@ -2015,6 +2016,14 @@ dependencies = [ "tempfile", ] +[[package]] +name = "rustc_codegen_spirv-types" +version = "0.4.0-alpha.12" +dependencies = [ + "rspirv", + "serde", +] + [[package]] name = "rustfix" version = "0.5.1" @@ -2223,6 +2232,7 @@ dependencies = [ "notify", "raw-string", "rustc_codegen_spirv", + "rustc_codegen_spirv-types", "serde", "serde_json", ] diff --git a/Cargo.toml b/Cargo.toml index 602dfed743..85de7fc82b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,7 @@ members = [ "examples/multibuilder", "crates/rustc_codegen_spirv", + "crates/rustc_codegen_spirv-types", "crates/spirv-builder", "crates/spirv-std", "crates/spirv-std/shared", diff --git a/crates/rustc_codegen_spirv-types/Cargo.toml b/crates/rustc_codegen_spirv-types/Cargo.toml new file mode 100644 index 0000000000..1cde4e5105 --- /dev/null +++ b/crates/rustc_codegen_spirv-types/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "rustc_codegen_spirv-types" +description = "SPIR-V backend types shared between rustc_codegen_spirv and spirv-builder" +version = "0.4.0-alpha.12" +authors = ["Embark "] +edition = "2018" +license = "MIT OR Apache-2.0" +repository = "https://github.com/EmbarkStudios/rust-gpu" + +[dependencies] +rspirv = "0.11" +serde = { version = "1.0", features = ["derive"] } diff --git a/crates/rustc_codegen_spirv/src/compile_result.rs b/crates/rustc_codegen_spirv-types/src/compile_result.rs similarity index 86% rename from crates/rustc_codegen_spirv/src/compile_result.rs rename to crates/rustc_codegen_spirv-types/src/compile_result.rs index 5bdc984fff..90eb948e02 100644 --- a/crates/rustc_codegen_spirv/src/compile_result.rs +++ b/crates/rustc_codegen_spirv-types/src/compile_result.rs @@ -1,5 +1,5 @@ -use rustc_data_structures::fx::FxHashMap; use serde::{Deserialize, Serialize}; +use std::collections::BTreeMap; use std::fmt::Write; use std::path::{Path, PathBuf}; @@ -7,7 +7,7 @@ use std::path::{Path, PathBuf}; #[serde(untagged)] pub enum ModuleResult { SingleModule(PathBuf), - MultiModule(FxHashMap), + MultiModule(BTreeMap), } impl ModuleResult { @@ -20,7 +20,7 @@ impl ModuleResult { } } - pub fn unwrap_multi(&self) -> &FxHashMap { + pub fn unwrap_multi(&self) -> &BTreeMap { match self { ModuleResult::MultiModule(result) => result, ModuleResult::SingleModule(_) => { @@ -46,28 +46,24 @@ impl CompileResult { } #[derive(Default)] -struct Trie { +struct Trie<'a> { present: bool, - children: FxHashMap>, + children: BTreeMap<&'a str, Trie<'a>>, } -impl Trie { - fn create_from<'a>(entry_points: impl IntoIterator) -> Self { +impl<'a> Trie<'a> { + fn create_from(entry_points: impl IntoIterator) -> Self { let mut result = Trie::default(); for entry in entry_points { - result.insert(entry.split("::").map(|x| x.to_owned())); + result.insert(entry.split("::")); } result } - fn insert(&mut self, mut sequence: impl Iterator) { + fn insert(&mut self, mut sequence: impl Iterator) { match sequence.next() { None => self.present = true, - Some(next) => self - .children - .entry(next) - .or_insert_with(Default::default) - .insert(sequence), + Some(next) => self.children.entry(next).or_default().insert(sequence), } } diff --git a/crates/rustc_codegen_spirv-types/src/lib.rs b/crates/rustc_codegen_spirv-types/src/lib.rs new file mode 100644 index 0000000000..0f507c900d --- /dev/null +++ b/crates/rustc_codegen_spirv-types/src/lib.rs @@ -0,0 +1,6 @@ +//! Types used by both `rustc_codegen_spirv` and `spirv-builder`. + +pub use rspirv::spirv::Capability; + +mod compile_result; +pub use compile_result::*; diff --git a/crates/rustc_codegen_spirv/Cargo.toml b/crates/rustc_codegen_spirv/Cargo.toml index 983c43932d..4b5648254c 100644 --- a/crates/rustc_codegen_spirv/Cargo.toml +++ b/crates/rustc_codegen_spirv/Cargo.toml @@ -45,6 +45,7 @@ serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" smallvec = "1.6.1" spirv-tools = { version = "0.8", default-features = false } +rustc_codegen_spirv-types = { path = "../rustc_codegen_spirv-types", version = "0.4.0-alpha.12" } [dev-dependencies] pipe = "0.4" diff --git a/crates/rustc_codegen_spirv/src/lib.rs b/crates/rustc_codegen_spirv/src/lib.rs index ca1ea5b8a5..898edcfd6f 100644 --- a/crates/rustc_codegen_spirv/src/lib.rs +++ b/crates/rustc_codegen_spirv/src/lib.rs @@ -137,7 +137,6 @@ mod attr; mod builder; mod builder_spirv; mod codegen_cx; -mod compile_result; mod decorations; mod link; mod linker; @@ -149,8 +148,6 @@ mod target_feature; use builder::Builder; use codegen_cx::CodegenCx; -pub use compile_result::*; -pub use rspirv; use rspirv::binary::Assemble; use rustc_ast::expand::allocator::AllocatorKind; use rustc_codegen_ssa::back::lto::{LtoModuleCodegen, SerializedModule, ThinModule}; diff --git a/crates/rustc_codegen_spirv/src/link.rs b/crates/rustc_codegen_spirv/src/link.rs index fb427d255e..73a8e397a4 100644 --- a/crates/rustc_codegen_spirv/src/link.rs +++ b/crates/rustc_codegen_spirv/src/link.rs @@ -1,13 +1,12 @@ use crate::codegen_cx::{CodegenArgs, ModuleOutputType, SpirvMetadata}; -use crate::{ - linker, CompileResult, ModuleResult, SpirvCodegenBackend, SpirvModuleBuffer, SpirvThinBuffer, -}; +use crate::{linker, SpirvCodegenBackend, SpirvModuleBuffer, SpirvThinBuffer}; use ar::{Archive, GnuBuilder, Header}; use rspirv::binary::Assemble; +use rustc_codegen_spirv_types::{CompileResult, ModuleResult}; use rustc_codegen_ssa::back::lto::{LtoModuleCodegen, SerializedModule, ThinModule, ThinShared}; use rustc_codegen_ssa::back::write::CodegenContext; use rustc_codegen_ssa::{CodegenResults, NativeLib, METADATA_FILENAME}; -use rustc_data_structures::fx::{FxHashMap, FxHashSet}; +use rustc_data_structures::fx::FxHashSet; use rustc_errors::FatalError; use rustc_middle::bug; use rustc_middle::dep_graph::WorkProduct; @@ -156,15 +155,22 @@ fn link_exe( } } linker::LinkResult::MultipleModules(map) => { - let mut hashmap = FxHashMap::default(); let entry_points = map.keys().cloned().collect(); - for (name, spv_binary) in map { - let mut module_filename = out_dir.clone(); - module_filename.push(sanitize_filename::sanitize(&name)); - post_link_single_module(sess, &cg_args, spv_binary.assemble(), &module_filename); - hashmap.insert(name, module_filename); - } - let module_result = ModuleResult::MultiModule(hashmap); + let map = map + .into_iter() + .map(|(name, spv_binary)| { + let mut module_filename = out_dir.clone(); + module_filename.push(sanitize_filename::sanitize(&name)); + post_link_single_module( + sess, + &cg_args, + spv_binary.assemble(), + &module_filename, + ); + (name, module_filename) + }) + .collect(); + let module_result = ModuleResult::MultiModule(map); CompileResult { module: module_result, entry_points, diff --git a/crates/spirv-builder/Cargo.toml b/crates/spirv-builder/Cargo.toml index 9e2f7a5788..f8eaa5b758 100644 --- a/crates/spirv-builder/Cargo.toml +++ b/crates/spirv-builder/Cargo.toml @@ -17,6 +17,7 @@ memchr = "2.4" raw-string = "0.3.5" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" +rustc_codegen_spirv-types = { path = "../rustc_codegen_spirv-types" } # See comment in lib.rs invoke_rustc for why this is here rustc_codegen_spirv = { path = "../rustc_codegen_spirv", default-features = false } diff --git a/crates/spirv-builder/src/lib.rs b/crates/spirv-builder/src/lib.rs index b333a0f82c..998c662a1b 100644 --- a/crates/spirv-builder/src/lib.rs +++ b/crates/spirv-builder/src/lib.rs @@ -86,8 +86,8 @@ use std::io::BufReader; use std::path::{Path, PathBuf}; use std::process::{Command, Stdio}; -pub use rustc_codegen_spirv::rspirv::spirv::Capability; -pub use rustc_codegen_spirv::{CompileResult, ModuleResult}; +pub use rustc_codegen_spirv_types::Capability; +pub use rustc_codegen_spirv_types::{CompileResult, ModuleResult}; #[derive(Debug)] #[non_exhaustive] diff --git a/crates/spirv-builder/src/watch.rs b/crates/spirv-builder/src/watch.rs index 479c6b7bb7..ca4624cb2b 100644 --- a/crates/spirv-builder/src/watch.rs +++ b/crates/spirv-builder/src/watch.rs @@ -1,7 +1,7 @@ use std::{collections::HashSet, sync::mpsc::sync_channel}; use notify::{Event, RecursiveMode, Watcher}; -use rustc_codegen_spirv::CompileResult; +use rustc_codegen_spirv_types::CompileResult; use crate::{leaf_deps, SpirvBuilder, SpirvBuilderError}; diff --git a/examples/runners/wgpu/src/graphics.rs b/examples/runners/wgpu/src/graphics.rs index 25eeef4b59..0f5a09036d 100644 --- a/examples/runners/wgpu/src/graphics.rs +++ b/examples/runners/wgpu/src/graphics.rs @@ -59,7 +59,7 @@ async fn run( let features = wgpu::Features::PUSH_CONSTANTS; let limits = wgpu::Limits { - max_push_constant_size: 256, + max_push_constant_size: 128, ..Default::default() };