mirror of
https://github.com/EmbarkStudios/rust-gpu.git
synced 2025-02-16 08:54:56 +00:00
custom_insts: group all debugPrintf
-like inputs of Abort
together.
This commit is contained in:
parent
603f9894d6
commit
88457ae249
@ -2878,11 +2878,7 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
|
|||||||
|
|
||||||
// HACK(eddyb) redirect any possible panic call to an abort, to avoid
|
// HACK(eddyb) redirect any possible panic call to an abort, to avoid
|
||||||
// needing to materialize `&core::panic::Location` or `format_args!`.
|
// needing to materialize `&core::panic::Location` or `format_args!`.
|
||||||
self.abort_with_message_and_debug_printf_args(
|
self.abort_with_kind_and_message_debug_printf("panic", message, debug_printf_args);
|
||||||
// HACK(eddyb) `|` is an ad-hoc convention of `linker::spirt_passes::controlflow`.
|
|
||||||
format!("panicked|{message}"),
|
|
||||||
debug_printf_args,
|
|
||||||
);
|
|
||||||
self.undef(result_type)
|
self.undef(result_type)
|
||||||
} else if let Some(mode) = buffer_load_intrinsic {
|
} else if let Some(mode) = buffer_load_intrinsic {
|
||||||
self.codegen_buffer_load_intrinsic(result_type, args, mode)
|
self.codegen_buffer_load_intrinsic(result_type, args, mode)
|
||||||
|
@ -339,11 +339,7 @@ impl<'a, 'tcx> IntrinsicCallMethods<'tcx> for Builder<'a, 'tcx> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn abort(&mut self) {
|
fn abort(&mut self) {
|
||||||
// HACK(eddyb) `|` is an ad-hoc convention of `linker::spirt_passes::controlflow`.
|
self.abort_with_kind_and_message_debug_printf("abort", "intrinsics::abort() called", []);
|
||||||
self.abort_with_message_and_debug_printf_args(
|
|
||||||
"aborted|intrinsics::abort() called".into(),
|
|
||||||
[],
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn assume(&mut self, _val: Self::Value) {
|
fn assume(&mut self, _val: Self::Value) {
|
||||||
@ -378,24 +374,31 @@ impl<'a, 'tcx> IntrinsicCallMethods<'tcx> for Builder<'a, 'tcx> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Builder<'_, '_> {
|
impl Builder<'_, '_> {
|
||||||
pub fn abort_with_message_and_debug_printf_args(
|
pub fn abort_with_kind_and_message_debug_printf(
|
||||||
&mut self,
|
&mut self,
|
||||||
message: String,
|
kind: &str,
|
||||||
debug_printf_args: impl IntoIterator<Item = SpirvValue>,
|
message_debug_printf_fmt_str: impl Into<String>,
|
||||||
|
message_debug_printf_args: impl IntoIterator<Item = SpirvValue>,
|
||||||
) {
|
) {
|
||||||
// FIXME(eddyb) this should be cached more efficiently.
|
// FIXME(eddyb) this should be cached more efficiently.
|
||||||
let void_ty = SpirvType::Void.def(rustc_span::DUMMY_SP, self);
|
let void_ty = SpirvType::Void.def(rustc_span::DUMMY_SP, self);
|
||||||
|
|
||||||
// HACK(eddyb) there is no `abort` or `trap` instruction in SPIR-V,
|
// HACK(eddyb) there is no `abort` or `trap` instruction in SPIR-V,
|
||||||
// so the best thing we can do is use our own custom instruction.
|
// so the best thing we can do is use our own custom instruction.
|
||||||
let message_id = self.emit().string(message);
|
let kind_id = self.emit().string(kind);
|
||||||
|
let message_debug_printf_fmt_str_id = self.emit().string(message_debug_printf_fmt_str);
|
||||||
self.custom_inst(
|
self.custom_inst(
|
||||||
void_ty,
|
void_ty,
|
||||||
CustomInst::Abort {
|
CustomInst::Abort {
|
||||||
message: Operand::IdRef(message_id),
|
kind: Operand::IdRef(kind_id),
|
||||||
debug_printf_args: debug_printf_args
|
message_debug_printf: [message_debug_printf_fmt_str_id]
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|arg| Operand::IdRef(arg.def(self)))
|
.chain(
|
||||||
|
message_debug_printf_args
|
||||||
|
.into_iter()
|
||||||
|
.map(|arg| arg.def(self)),
|
||||||
|
)
|
||||||
|
.map(Operand::IdRef)
|
||||||
.collect(),
|
.collect(),
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
@ -151,9 +151,10 @@ def_custom_insts! {
|
|||||||
// users to do `catch_unwind` at the top-level of their shader to handle
|
// users to do `catch_unwind` at the top-level of their shader to handle
|
||||||
// panics specially (e.g. by appending to a custom buffer, or using some
|
// panics specially (e.g. by appending to a custom buffer, or using some
|
||||||
// specific color in a fragment shader, to indicate a panic happened).
|
// specific color in a fragment shader, to indicate a panic happened).
|
||||||
// NOTE(eddyb) the `message` string follows `debugPrintf` rules, with remaining
|
// NOTE(eddyb) `message_debug_printf` operands form a complete `debugPrintf`
|
||||||
// operands (collected into `debug_printf_args`) being its formatting arguments.
|
// invocation (format string followed by inputs) for the "message", while
|
||||||
4 => Abort { message, ..debug_printf_args },
|
// `kind` only distinguishes broad categories like `"abort"` vs `"panic"`.
|
||||||
|
4 => Abort { kind, ..message_debug_printf },
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CustomOp {
|
impl CustomOp {
|
||||||
|
@ -244,8 +244,8 @@ pub fn convert_custom_aborts_to_unstructured_returns_in_entry_points(
|
|||||||
if let Some((
|
if let Some((
|
||||||
func_at_abort_inst,
|
func_at_abort_inst,
|
||||||
CustomInst::Abort {
|
CustomInst::Abort {
|
||||||
message,
|
kind: abort_kind,
|
||||||
debug_printf_args: message_debug_printf_args,
|
message_debug_printf,
|
||||||
},
|
},
|
||||||
)) = custom_terminator_inst
|
)) = custom_terminator_inst
|
||||||
{
|
{
|
||||||
@ -335,26 +335,38 @@ pub fn convert_custom_aborts_to_unstructured_returns_in_entry_points(
|
|||||||
|
|
||||||
let mut fmt = String::new();
|
let mut fmt = String::new();
|
||||||
|
|
||||||
|
let (message_debug_printf_fmt_str, message_debug_printf_args) =
|
||||||
|
message_debug_printf
|
||||||
|
.split_first()
|
||||||
|
.map(|(&fmt_str, args)| (&cx[const_str(fmt_str)], args))
|
||||||
|
.unwrap_or_default();
|
||||||
|
|
||||||
|
let fmt_dbg_src_loc = |(file, line, col)| {
|
||||||
|
format!("{file}:{line}:{col}").replace('%', "%%")
|
||||||
|
};
|
||||||
|
|
||||||
// HACK(eddyb) this improves readability w/ very verbose Vulkan loggers.
|
// HACK(eddyb) this improves readability w/ very verbose Vulkan loggers.
|
||||||
fmt += "\n";
|
fmt += "\n";
|
||||||
|
|
||||||
fmt += "[";
|
fmt += "[Rust ";
|
||||||
|
|
||||||
// NB: `message` has its own `message_debug_printf_args`
|
// HACK(eddyb) turn "panic" into "panicked", while the
|
||||||
// it formats, and as such any escaping is already done.
|
// general case looks like "abort" -> "aborted".
|
||||||
let message = &cx[const_str(message)];
|
match &cx[const_str(abort_kind)] {
|
||||||
let (message_kind, message) =
|
"panic" => fmt += "panicked",
|
||||||
message.split_once('|').unwrap_or(("", message));
|
verb => {
|
||||||
|
fmt += verb;
|
||||||
|
fmt += "en";
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
if let Some((file, line, col)) = current_debug_src_loc.take() {
|
if let Some(loc) = current_debug_src_loc.take() {
|
||||||
fmt += &format!("Rust {message_kind} at {file}:{line}:{col}")
|
fmt += " at ";
|
||||||
.replace('%', "%%");
|
fmt += &fmt_dbg_src_loc(loc);
|
||||||
} else {
|
|
||||||
fmt += message_kind;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt += "]\n ";
|
fmt += "]\n ";
|
||||||
fmt += &message.replace('\n', "\n ");
|
fmt += &message_debug_printf_fmt_str.replace('\n', "\n ");
|
||||||
|
|
||||||
let mut innermost = true;
|
let mut innermost = true;
|
||||||
let mut append_call = |callsite_debug_src_loc, callee: &str| {
|
let mut append_call = |callsite_debug_src_loc, callee: &str| {
|
||||||
@ -368,9 +380,9 @@ pub fn convert_custom_aborts_to_unstructured_returns_in_entry_points(
|
|||||||
fmt += "\n called by ";
|
fmt += "\n called by ";
|
||||||
}
|
}
|
||||||
fmt += callee;
|
fmt += callee;
|
||||||
if let Some((file, line, col)) = callsite_debug_src_loc {
|
if let Some(loc) = callsite_debug_src_loc {
|
||||||
fmt += &format!("\n called at {file}:{line}:{col}")
|
fmt += "\n called at ";
|
||||||
.replace('%', "%%");
|
fmt += &fmt_dbg_src_loc(loc);
|
||||||
}
|
}
|
||||||
current_debug_src_loc = callsite_debug_src_loc;
|
current_debug_src_loc = callsite_debug_src_loc;
|
||||||
};
|
};
|
||||||
@ -391,7 +403,7 @@ pub fn convert_custom_aborts_to_unstructured_returns_in_entry_points(
|
|||||||
});
|
});
|
||||||
abort_inst_def.inputs = [Value::Const(mk_const_str(cx.intern(fmt)))]
|
abort_inst_def.inputs = [Value::Const(mk_const_str(cx.intern(fmt)))]
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.chain(message_debug_printf_args)
|
.chain(message_debug_printf_args.iter().copied())
|
||||||
.chain(debug_printf_context_inputs.iter().copied())
|
.chain(debug_printf_context_inputs.iter().copied())
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user