From 222f1ea733fec9840ce3f2451bc5b8e386e59695 Mon Sep 17 00:00:00 2001 From: Kornel Date: Sun, 18 Aug 2024 13:44:59 +0100 Subject: [PATCH] Reduce code size of error handling --- naga-cli/src/bin/naga.rs | 2 ++ naga/src/front/wgsl/error.rs | 2 ++ wgpu-core/src/error.rs | 10 ++++++---- wgpu-hal/src/vulkan/mod.rs | 1 + wgpu/src/backend/wgpu_core.rs | 25 +++++++++++++++++++++---- 5 files changed, 32 insertions(+), 8 deletions(-) diff --git a/naga-cli/src/bin/naga.rs b/naga-cli/src/bin/naga.rs index 002c6dd664..d97d96de76 100644 --- a/naga-cli/src/bin/naga.rs +++ b/naga-cli/src/bin/naga.rs @@ -326,6 +326,8 @@ trait PrettyResult { fn unwrap_pretty(self) -> Self::Target; } +#[cold] +#[inline(never)] fn print_err(error: &dyn Error) { eprint!("{error}"); diff --git a/naga/src/front/wgsl/error.rs b/naga/src/front/wgsl/error.rs index febcd9a4e0..bfaba48946 100644 --- a/naga/src/front/wgsl/error.rs +++ b/naga/src/front/wgsl/error.rs @@ -278,6 +278,8 @@ pub enum Error<'a> { } impl<'a> Error<'a> { + #[cold] + #[inline(never)] pub(crate) fn as_parse_error(&self, source: &'a str) -> ParseError { match *self { Error::Unexpected(unexpected_span, expected) => { diff --git a/wgpu-core/src/error.rs b/wgpu-core/src/error.rs index aa1f11df0f..519a5f930c 100644 --- a/wgpu-core/src/error.rs +++ b/wgpu-core/src/error.rs @@ -3,6 +3,11 @@ use std::{error::Error, sync::Arc}; use thiserror::Error; +#[cfg(send_sync)] +pub type ContextErrorSource = Box; +#[cfg(not(send_sync))] +pub type ContextErrorSource = Box; + #[derive(Debug, Error)] #[error( "In {fn_ident}{}{}{}", @@ -13,10 +18,7 @@ use thiserror::Error; pub struct ContextError { pub fn_ident: &'static str, #[source] - #[cfg(send_sync)] - pub source: Box, - #[cfg(not(send_sync))] - pub source: Box, + pub source: ContextErrorSource, pub label: String, } diff --git a/wgpu-hal/src/vulkan/mod.rs b/wgpu-hal/src/vulkan/mod.rs index 3f3bd557e4..26186d5fa8 100644 --- a/wgpu-hal/src/vulkan/mod.rs +++ b/wgpu-hal/src/vulkan/mod.rs @@ -1296,6 +1296,7 @@ fn get_lost_err() -> crate::DeviceError { crate::DeviceError::Lost } +#[cold] fn hal_usage_error(txt: T) -> ! { panic!("wgpu-hal invariant was violated (usage error): {txt}") } diff --git a/wgpu/src/backend/wgpu_core.rs b/wgpu/src/backend/wgpu_core.rs index 413524ab0d..7c085ec476 100644 --- a/wgpu/src/backend/wgpu_core.rs +++ b/wgpu/src/backend/wgpu_core.rs @@ -24,6 +24,7 @@ use std::{ slice, sync::Arc, }; +use wgc::error::ContextErrorSource; use wgc::{ command::bundle_ffi::*, device::DeviceLostClosure, id::CommandEncoderId, id::TextureViewId, pipeline::CreateShaderModuleError, @@ -267,16 +268,18 @@ impl ContextWgpuCore { self.0.generate_report() } - fn handle_error( + #[cold] + #[inline(never)] + fn handle_error_inner( &self, sink_mutex: &Mutex, - source: impl Error + WasmNotSendSync + 'static, + source: ContextErrorSource, label: Label<'_>, fn_ident: &'static str, ) { let error = wgc::error::ContextError { fn_ident, - source: Box::new(source), + source, label: label.unwrap_or_default().to_string(), }; let mut sink = sink_mutex.lock(); @@ -299,16 +302,29 @@ impl ContextWgpuCore { }); } + #[inline] + fn handle_error( + &self, + sink_mutex: &Mutex, + source: impl Error + WasmNotSendSync + 'static, + label: Label<'_>, + fn_ident: &'static str, + ) { + self.handle_error_inner(sink_mutex, Box::new(source), label, fn_ident) + } + + #[inline] fn handle_error_nolabel( &self, sink_mutex: &Mutex, source: impl Error + WasmNotSendSync + 'static, fn_ident: &'static str, ) { - self.handle_error(sink_mutex, source, None, fn_ident) + self.handle_error_inner(sink_mutex, Box::new(source), None, fn_ident) } #[track_caller] + #[cold] fn handle_error_fatal( &self, cause: impl Error + WasmNotSendSync + 'static, @@ -317,6 +333,7 @@ impl ContextWgpuCore { panic!("Error in {operation}: {f}", f = self.format_error(&cause)); } + #[inline(never)] fn format_error(&self, err: &(impl Error + 'static)) -> String { let mut output = String::new(); let mut level = 1;