Skip to content

Commit 88457ae

Browse files
committed
custom_insts: group all debugPrintf-like inputs of Abort together.
1 parent 603f989 commit 88457ae

File tree

4 files changed

+50
-38
lines changed

4 files changed

+50
-38
lines changed

crates/rustc_codegen_spirv/src/builder/builder_methods.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2878,11 +2878,7 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> {
28782878

28792879
// HACK(eddyb) redirect any possible panic call to an abort, to avoid
28802880
// needing to materialize `&core::panic::Location` or `format_args!`.
2881-
self.abort_with_message_and_debug_printf_args(
2882-
// HACK(eddyb) `|` is an ad-hoc convention of `linker::spirt_passes::controlflow`.
2883-
format!("panicked|{message}"),
2884-
debug_printf_args,
2885-
);
2881+
self.abort_with_kind_and_message_debug_printf("panic", message, debug_printf_args);
28862882
self.undef(result_type)
28872883
} else if let Some(mode) = buffer_load_intrinsic {
28882884
self.codegen_buffer_load_intrinsic(result_type, args, mode)

crates/rustc_codegen_spirv/src/builder/intrinsics.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -339,11 +339,7 @@ impl<'a, 'tcx> IntrinsicCallMethods<'tcx> for Builder<'a, 'tcx> {
339339
}
340340

341341
fn abort(&mut self) {
342-
// HACK(eddyb) `|` is an ad-hoc convention of `linker::spirt_passes::controlflow`.
343-
self.abort_with_message_and_debug_printf_args(
344-
"aborted|intrinsics::abort() called".into(),
345-
[],
346-
);
342+
self.abort_with_kind_and_message_debug_printf("abort", "intrinsics::abort() called", []);
347343
}
348344

349345
fn assume(&mut self, _val: Self::Value) {
@@ -378,24 +374,31 @@ impl<'a, 'tcx> IntrinsicCallMethods<'tcx> for Builder<'a, 'tcx> {
378374
}
379375

380376
impl Builder<'_, '_> {
381-
pub fn abort_with_message_and_debug_printf_args(
377+
pub fn abort_with_kind_and_message_debug_printf(
382378
&mut self,
383-
message: String,
384-
debug_printf_args: impl IntoIterator<Item = SpirvValue>,
379+
kind: &str,
380+
message_debug_printf_fmt_str: impl Into<String>,
381+
message_debug_printf_args: impl IntoIterator<Item = SpirvValue>,
385382
) {
386383
// FIXME(eddyb) this should be cached more efficiently.
387384
let void_ty = SpirvType::Void.def(rustc_span::DUMMY_SP, self);
388385

389386
// HACK(eddyb) there is no `abort` or `trap` instruction in SPIR-V,
390387
// so the best thing we can do is use our own custom instruction.
391-
let message_id = self.emit().string(message);
388+
let kind_id = self.emit().string(kind);
389+
let message_debug_printf_fmt_str_id = self.emit().string(message_debug_printf_fmt_str);
392390
self.custom_inst(
393391
void_ty,
394392
CustomInst::Abort {
395-
message: Operand::IdRef(message_id),
396-
debug_printf_args: debug_printf_args
393+
kind: Operand::IdRef(kind_id),
394+
message_debug_printf: [message_debug_printf_fmt_str_id]
397395
.into_iter()
398-
.map(|arg| Operand::IdRef(arg.def(self)))
396+
.chain(
397+
message_debug_printf_args
398+
.into_iter()
399+
.map(|arg| arg.def(self)),
400+
)
401+
.map(Operand::IdRef)
399402
.collect(),
400403
},
401404
);

crates/rustc_codegen_spirv/src/custom_insts.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,10 @@ def_custom_insts! {
151151
// users to do `catch_unwind` at the top-level of their shader to handle
152152
// panics specially (e.g. by appending to a custom buffer, or using some
153153
// specific color in a fragment shader, to indicate a panic happened).
154-
// NOTE(eddyb) the `message` string follows `debugPrintf` rules, with remaining
155-
// operands (collected into `debug_printf_args`) being its formatting arguments.
156-
4 => Abort { message, ..debug_printf_args },
154+
// NOTE(eddyb) `message_debug_printf` operands form a complete `debugPrintf`
155+
// invocation (format string followed by inputs) for the "message", while
156+
// `kind` only distinguishes broad categories like `"abort"` vs `"panic"`.
157+
4 => Abort { kind, ..message_debug_printf },
157158
}
158159

159160
impl CustomOp {

crates/rustc_codegen_spirv/src/linker/spirt_passes/controlflow.rs

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,8 @@ pub fn convert_custom_aborts_to_unstructured_returns_in_entry_points(
244244
if let Some((
245245
func_at_abort_inst,
246246
CustomInst::Abort {
247-
message,
248-
debug_printf_args: message_debug_printf_args,
247+
kind: abort_kind,
248+
message_debug_printf,
249249
},
250250
)) = custom_terminator_inst
251251
{
@@ -335,26 +335,38 @@ pub fn convert_custom_aborts_to_unstructured_returns_in_entry_points(
335335

336336
let mut fmt = String::new();
337337

338+
let (message_debug_printf_fmt_str, message_debug_printf_args) =
339+
message_debug_printf
340+
.split_first()
341+
.map(|(&fmt_str, args)| (&cx[const_str(fmt_str)], args))
342+
.unwrap_or_default();
343+
344+
let fmt_dbg_src_loc = |(file, line, col)| {
345+
format!("{file}:{line}:{col}").replace('%', "%%")
346+
};
347+
338348
// HACK(eddyb) this improves readability w/ very verbose Vulkan loggers.
339349
fmt += "\n";
340350

341-
fmt += "[";
351+
fmt += "[Rust ";
342352

343-
// NB: `message` has its own `message_debug_printf_args`
344-
// it formats, and as such any escaping is already done.
345-
let message = &cx[const_str(message)];
346-
let (message_kind, message) =
347-
message.split_once('|').unwrap_or(("", message));
353+
// HACK(eddyb) turn "panic" into "panicked", while the
354+
// general case looks like "abort" -> "aborted".
355+
match &cx[const_str(abort_kind)] {
356+
"panic" => fmt += "panicked",
357+
verb => {
358+
fmt += verb;
359+
fmt += "en";
360+
}
361+
};
348362

349-
if let Some((file, line, col)) = current_debug_src_loc.take() {
350-
fmt += &format!("Rust {message_kind} at {file}:{line}:{col}")
351-
.replace('%', "%%");
352-
} else {
353-
fmt += message_kind;
363+
if let Some(loc) = current_debug_src_loc.take() {
364+
fmt += " at ";
365+
fmt += &fmt_dbg_src_loc(loc);
354366
}
355367

356368
fmt += "]\n ";
357-
fmt += &message.replace('\n', "\n ");
369+
fmt += &message_debug_printf_fmt_str.replace('\n', "\n ");
358370

359371
let mut innermost = true;
360372
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(
368380
fmt += "\n called by ";
369381
}
370382
fmt += callee;
371-
if let Some((file, line, col)) = callsite_debug_src_loc {
372-
fmt += &format!("\n called at {file}:{line}:{col}")
373-
.replace('%', "%%");
383+
if let Some(loc) = callsite_debug_src_loc {
384+
fmt += "\n called at ";
385+
fmt += &fmt_dbg_src_loc(loc);
374386
}
375387
current_debug_src_loc = callsite_debug_src_loc;
376388
};
@@ -391,7 +403,7 @@ pub fn convert_custom_aborts_to_unstructured_returns_in_entry_points(
391403
});
392404
abort_inst_def.inputs = [Value::Const(mk_const_str(cx.intern(fmt)))]
393405
.into_iter()
394-
.chain(message_debug_printf_args)
406+
.chain(message_debug_printf_args.iter().copied())
395407
.chain(debug_printf_context_inputs.iter().copied())
396408
.collect();
397409

0 commit comments

Comments
 (0)