Skip to content

Commit

Permalink
Reduce code size of error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
kornelski authored and teoxoy committed Aug 19, 2024
1 parent a87c8d7 commit 222f1ea
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 8 deletions.
2 changes: 2 additions & 0 deletions naga-cli/src/bin/naga.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,8 @@ trait PrettyResult {
fn unwrap_pretty(self) -> Self::Target;
}

#[cold]
#[inline(never)]
fn print_err(error: &dyn Error) {
eprint!("{error}");

Expand Down
2 changes: 2 additions & 0 deletions naga/src/front/wgsl/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
10 changes: 6 additions & 4 deletions wgpu-core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ use std::{error::Error, sync::Arc};

use thiserror::Error;

#[cfg(send_sync)]
pub type ContextErrorSource = Box<dyn Error + Send + Sync + 'static>;
#[cfg(not(send_sync))]
pub type ContextErrorSource = Box<dyn Error + 'static>;

#[derive(Debug, Error)]
#[error(
"In {fn_ident}{}{}{}",
Expand All @@ -13,10 +18,7 @@ use thiserror::Error;
pub struct ContextError {
pub fn_ident: &'static str,
#[source]
#[cfg(send_sync)]
pub source: Box<dyn Error + Send + Sync + 'static>,
#[cfg(not(send_sync))]
pub source: Box<dyn Error + 'static>,
pub source: ContextErrorSource,
pub label: String,
}

Expand Down
1 change: 1 addition & 0 deletions wgpu-hal/src/vulkan/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1296,6 +1296,7 @@ fn get_lost_err() -> crate::DeviceError {
crate::DeviceError::Lost
}

#[cold]
fn hal_usage_error<T: fmt::Display>(txt: T) -> ! {
panic!("wgpu-hal invariant was violated (usage error): {txt}")
}
25 changes: 21 additions & 4 deletions wgpu/src/backend/wgpu_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -267,16 +268,18 @@ impl ContextWgpuCore {
self.0.generate_report()
}

fn handle_error(
#[cold]
#[inline(never)]
fn handle_error_inner(
&self,
sink_mutex: &Mutex<ErrorSinkRaw>,
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();
Expand All @@ -299,16 +302,29 @@ impl ContextWgpuCore {
});
}

#[inline]
fn handle_error(
&self,
sink_mutex: &Mutex<ErrorSinkRaw>,
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<ErrorSinkRaw>,
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,
Expand All @@ -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;
Expand Down

0 comments on commit 222f1ea

Please sign in to comment.