From 0581f8580e1deff3272d056ff41b941e8b2efe2c Mon Sep 17 00:00:00 2001 From: Eduard-Mihai Burtescu Date: Fri, 20 Nov 2020 15:54:26 +0200 Subject: [PATCH] Use a demangled symbol name for OpName. (#266) * Use a demangled symbol name for OpName. * Rename my_smoothstep back to smoothstep. * Update SPIR-V tests to look for the absolute fn path. --- Cargo.lock | 7 +++++++ crates/rustc_codegen_spirv/Cargo.toml | 1 + .../src/codegen_cx/declare.rs | 18 ++++++++++++++---- crates/spirv-builder/src/lib.rs | 2 +- crates/spirv-builder/src/test/mod.rs | 3 ++- examples/shaders/shared/src/lib.rs | 3 +-- examples/shaders/sky-shader/src/lib.rs | 2 +- 7 files changed, 27 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8f3984bf0c..1c8b69e6dc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1963,6 +1963,12 @@ dependencies = [ "spirv_headers 1.5.0 (git+https://github.com/gfx-rs/rspirv.git?rev=01ca0d2e5b667a0e4ff1bc1804511e38f9a08759)", ] +[[package]] +name = "rustc-demangle" +version = "0.1.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e3bad0ee36814ca07d7968269dd4b7ec89ec2da10c4bb613928d3077083c232" + [[package]] name = "rustc_codegen_spirv" version = "0.1.0" @@ -1971,6 +1977,7 @@ dependencies = [ "pipe", "pretty_assertions", "rspirv", + "rustc-demangle", "spirv-tools", "tar", "tempfile", diff --git a/crates/rustc_codegen_spirv/Cargo.toml b/crates/rustc_codegen_spirv/Cargo.toml index 258341bf7b..187ae51673 100644 --- a/crates/rustc_codegen_spirv/Cargo.toml +++ b/crates/rustc_codegen_spirv/Cargo.toml @@ -29,6 +29,7 @@ use-compiled-tools = ["spirv-tools/use-compiled-tools"] [dependencies] bimap = "0.5" rspirv = { git = "https://github.com/gfx-rs/rspirv.git", rev = "01ca0d2e5b667a0e4ff1bc1804511e38f9a08759" } +rustc-demangle = "0.1.18" spirv-tools = { version = "0.1.0", default-features = false } tar = "0.4.30" topological-sort = "0.1" diff --git a/crates/rustc_codegen_spirv/src/codegen_cx/declare.rs b/crates/rustc_codegen_spirv/src/codegen_cx/declare.rs index 78f8f883d7..cae9016b0a 100644 --- a/crates/rustc_codegen_spirv/src/codegen_cx/declare.rs +++ b/crates/rustc_codegen_spirv/src/codegen_cx/declare.rs @@ -91,11 +91,20 @@ impl<'tcx> CodegenCx<'tcx> { } emit.end_function().unwrap(); - let human_name = format!("{}", instance); - emit.name(fn_id, &human_name); + // HACK(eddyb) this is a bit roundabout, but the easiest way to get a + // fully absolute path that contains at least as much information as + // `instance.to_string()` (at least with `-Z symbol-mangling-version=v0`). + // While we could use the mangled symbol insyead, like we do for linkage, + // `OpName` is more of a debugging aid, so not having to separately + // demangle the SPIR-V can help. However, if some tools assume `OpName` + // is always a valid identifier, we may have to offer the mangled name + // (as some sort of opt-in, or toggled based on the platform, etc.). + let symbol_name = self.tcx.symbol_name(instance).name; + let demangled_symbol_name = format!("{:#}", rustc_demangle::demangle(symbol_name)); + emit.name(fn_id, &demangled_symbol_name); + drop(emit); // set_linkage uses emit if let Some(linkage) = linkage { - let symbol_name = self.tcx.symbol_name(instance).name; self.set_linkage(fn_id, symbol_name.to_owned(), linkage); } @@ -104,7 +113,8 @@ impl<'tcx> CodegenCx<'tcx> { for attr in parse_attrs(self, self.tcx.get_attrs(instance.def_id())) { match attr { SpirvAttribute::Entry(entry) => { - self.entry_stub(&instance, &fn_abi, declared, human_name.clone(), entry) + let crate_relative_name = instance.to_string(); + self.entry_stub(&instance, &fn_abi, declared, crate_relative_name, entry) } SpirvAttribute::ReallyUnsafeIgnoreBitcasts => { self.really_unsafe_ignore_bitcasts diff --git a/crates/spirv-builder/src/lib.rs b/crates/spirv-builder/src/lib.rs index fe12897890..f1730baf5b 100644 --- a/crates/spirv-builder/src/lib.rs +++ b/crates/spirv-builder/src/lib.rs @@ -143,7 +143,7 @@ fn invoke_rustc(builder: &SpirvBuilder) -> Result { format!(" -C target-feature={}", target_features.join(",")) }; let rustflags = format!( - "-Z codegen-backend={}{}", + "-Z codegen-backend={} -Z symbol-mangling-version=v0{}", rustc_codegen_spirv.display(), feature_flag, ); diff --git a/crates/spirv-builder/src/test/mod.rs b/crates/spirv-builder/src/test/mod.rs index fadef73a2c..a08b1a597a 100644 --- a/crates/spirv-builder/src/test/mod.rs +++ b/crates/spirv-builder/src/test/mod.rs @@ -113,12 +113,13 @@ fn assert_str_eq(expected: &str, result: &str) { fn dis_fn(src: &str, func: &str, expect: &str) { let _lock = global_lock(); let module = read_module(&build(src)).unwrap(); + let abs_func_path = format!("test_project::{}", func); let id = module .debugs .iter() .find(|inst| { inst.class.opcode == rspirv::spirv::Op::Name - && inst.operands[1].unwrap_literal_string() == func + && inst.operands[1].unwrap_literal_string() == abs_func_path }) .expect("No function with that name found") .operands[0] diff --git a/examples/shaders/shared/src/lib.rs b/examples/shaders/shared/src/lib.rs index 0f74013b2f..7496bd27f4 100644 --- a/examples/shaders/shared/src/lib.rs +++ b/examples/shaders/shared/src/lib.rs @@ -39,8 +39,7 @@ pub fn acos_approx(v: f32) -> f32 { } } -/// renamed because of cross-compilation issues with spirv-cross/ moltenvk -pub fn my_smoothstep(edge0: f32, edge1: f32, x: f32) -> f32 { +pub fn smoothstep(edge0: f32, edge1: f32, x: f32) -> f32 { // Scale, bias and saturate x to 0..1 range let x = ((x - edge0) / (edge1 - edge0)).saturate(); // Evaluate polynomial diff --git a/examples/shaders/sky-shader/src/lib.rs b/examples/shaders/sky-shader/src/lib.rs index 6cb80106db..42875466bb 100644 --- a/examples/shaders/sky-shader/src/lib.rs +++ b/examples/shaders/sky-shader/src/lib.rs @@ -97,7 +97,7 @@ fn sky(dir: Vec3, sun_position: Vec3) -> Vec3 { // Composition + solar disc let sun_angular_diameter_cos = SUN_ANGULAR_DIAMETER_DEGREES.cos(); - let sundisk = my_smoothstep( + let sundisk = smoothstep( sun_angular_diameter_cos, sun_angular_diameter_cos + 0.00002, cos_theta,