Skip to content

Commit 6e2c84d

Browse files
committed
Replace printing codegen args with tracing.
This removes the following options and replaces them with `tracing`: * `--specializer-debug` => RUSTGPU_LOG=rustc_codegen_spirv::specializer=debug * `--print-zombie` => RUSTGPU_LOG=print_zombie=debug * `--print-all-zombie` => RUSTGPU_LOG=print_all_zombie=debug
1 parent d5754d0 commit 6e2c84d

File tree

7 files changed

+105
-137
lines changed

7 files changed

+105
-137
lines changed

crates/rustc_codegen_spirv/src/codegen_cx/mod.rs

-14
Original file line numberDiff line numberDiff line change
@@ -469,23 +469,12 @@ impl CodegenArgs {
469469
"spirt-keep-unstructured-cfg-in-dumps",
470470
"include initial unstructured CFG when dumping SPIR-T",
471471
);
472-
opts.optflag(
473-
"",
474-
"specializer-debug",
475-
"enable debug logging for the specializer",
476-
);
477472
opts.optopt(
478473
"",
479474
"specializer-dump-instances",
480475
"dump all instances inferred by the specializer, to FILE",
481476
"FILE",
482477
);
483-
opts.optflag("", "print-all-zombie", "prints all removed zombies");
484-
opts.optflag(
485-
"",
486-
"print-zombie",
487-
"prints everything removed (even transitively) due to zombies",
488-
);
489478
}
490479

491480
// NOTE(eddyb) these are debugging options that used to be env vars
@@ -639,10 +628,7 @@ impl CodegenArgs {
639628
.opt_present("spirt-keep-debug-sources-in-dumps"),
640629
spirt_keep_unstructured_cfg_in_dumps: matches
641630
.opt_present("spirt-keep-unstructured-cfg-in-dumps"),
642-
specializer_debug: matches.opt_present("specializer-debug"),
643631
specializer_dump_instances: matches_opt_path("specializer-dump-instances"),
644-
print_all_zombie: matches.opt_present("print-all-zombie"),
645-
print_zombie: matches.opt_present("print-zombie"),
646632
};
647633

648634
Ok(Self {

crates/rustc_codegen_spirv/src/linker/mod.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,7 @@ pub struct Options {
6363
pub spirt_strip_custom_debuginfo_from_dumps: bool,
6464
pub spirt_keep_debug_sources_in_dumps: bool,
6565
pub spirt_keep_unstructured_cfg_in_dumps: bool,
66-
pub specializer_debug: bool,
6766
pub specializer_dump_instances: Option<PathBuf>,
68-
pub print_all_zombie: bool,
69-
pub print_zombie: bool,
7067
}
7168

7269
pub enum LinkResult {
@@ -316,7 +313,7 @@ pub fn link(
316313
}
317314

318315
let _timer = sess.timer("link_report_and_remove_zombies");
319-
zombies::report_and_remove_zombies(sess, opts, &mut output)?;
316+
zombies::report_and_remove_zombies(sess, &mut output)?;
320317
}
321318

322319
if opts.infer_storage_classes {

crates/rustc_codegen_spirv/src/linker/specializer.rs

+61-86
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ use smallvec::SmallVec;
6060
use std::collections::{BTreeMap, VecDeque};
6161
use std::ops::{Range, RangeTo};
6262
use std::{fmt, io, iter, mem, slice};
63+
use tracing::{debug, error};
6364

6465
// FIXME(eddyb) move this elsewhere.
6566
struct FmtBy<F: Fn(&mut fmt::Formatter<'_>) -> fmt::Result>(F);
@@ -110,12 +111,10 @@ pub fn specialize(
110111
module: Module,
111112
specialization: impl Specialization,
112113
) -> Module {
113-
// FIXME(eddyb) use `log`/`tracing` instead.
114-
let debug = opts.specializer_debug;
115114
let dump_instances = &opts.specializer_dump_instances;
116115

117116
let mut debug_names = FxHashMap::default();
118-
if debug || dump_instances.is_some() {
117+
if dump_instances.is_some() {
119118
debug_names = module
120119
.debug_names
121120
.iter()
@@ -131,10 +130,7 @@ pub fn specialize(
131130

132131
let mut specializer = Specializer {
133132
specialization,
134-
135-
debug,
136133
debug_names,
137-
138134
generics: IndexMap::new(),
139135
int_consts: FxHashMap::default(),
140136
};
@@ -188,27 +184,21 @@ pub fn specialize(
188184
// For non-"generic" functions, we can apply `replacements` right away,
189185
// though not before finishing inference for all functions first
190186
// (because `expander` needs to borrow `specializer` immutably).
191-
if debug {
192-
eprintln!("non-generic replacements:");
193-
}
187+
debug!("non-generic replacements:");
194188
for (func_idx, replacements) in non_generic_replacements {
195189
let mut func = mem::replace(
196190
&mut expander.builder.module_mut().functions[func_idx],
197191
Function::new(),
198192
);
199-
if debug {
200-
let empty = replacements.with_instance.is_empty()
201-
&& replacements.with_concrete_or_param.is_empty();
202-
if !empty {
203-
eprintln!(" in %{}:", func.def_id().unwrap());
204-
}
193+
let empty =
194+
replacements.with_instance.is_empty() && replacements.with_concrete_or_param.is_empty();
195+
if !empty {
196+
debug!(" in %{}:", func.def_id().unwrap());
205197
}
206198
for (loc, operand) in
207199
replacements.to_concrete(&[], |instance| expander.alloc_instance_id(instance))
208200
{
209-
if debug {
210-
eprintln!(" {operand} -> {loc:?}");
211-
}
201+
debug!(" {operand} -> {loc:?}");
212202
func.index_set(loc, operand.into());
213203
}
214204
expander.builder.module_mut().functions[func_idx] = func;
@@ -537,9 +527,6 @@ struct Generic {
537527
struct Specializer<S: Specialization> {
538528
specialization: S,
539529

540-
// FIXME(eddyb) use `log`/`tracing` instead.
541-
debug: bool,
542-
543530
// HACK(eddyb) if debugging is requested, this is used to quickly get `OpName`s.
544531
debug_names: FxHashMap<Word, String>,
545532

@@ -1512,24 +1499,24 @@ impl InferError {
15121499
// FIXME(eddyb) better error reporting than this.
15131500
match self {
15141501
Self::Conflict(a, b) => {
1515-
eprintln!("inference conflict: {a:?} vs {b:?}");
1502+
error!("inference conflict: {a:?} vs {b:?}");
15161503
}
15171504
}
1518-
eprint!(" in ");
1505+
error!(" in ");
15191506
// FIXME(eddyb) deduplicate this with other instruction printing logic.
15201507
if let Some(result_id) = inst.result_id {
1521-
eprint!("%{result_id} = ");
1508+
error!("%{result_id} = ");
15221509
}
1523-
eprint!("Op{:?}", inst.class.opcode);
1510+
error!("Op{:?}", inst.class.opcode);
15241511
for operand in inst
15251512
.result_type
15261513
.map(Operand::IdRef)
15271514
.iter()
15281515
.chain(inst.operands.iter())
15291516
{
1530-
eprint!(" {operand}");
1517+
error!(" {operand}");
15311518
}
1532-
eprintln!();
1519+
error!("");
15331520

15341521
std::process::exit(1);
15351522
}
@@ -1937,38 +1924,36 @@ impl<'a, S: Specialization> InferCx<'a, S> {
19371924
};
19381925

19391926
let debug_dump_if_enabled = |cx: &Self, prefix| {
1940-
if cx.specializer.debug {
1941-
let result_type = match inst.class.opcode {
1942-
// HACK(eddyb) workaround for `OpFunction`, see earlier HACK comment.
1943-
Op::Function => Some(
1944-
InferOperand::from_operand_and_generic_args(
1945-
&Operand::IdRef(inst.result_type.unwrap()),
1946-
inputs_generic_args.clone(),
1947-
cx,
1948-
)
1949-
.0,
1950-
),
1951-
_ => type_of_result.clone(),
1952-
};
1953-
let inputs = InferOperandList {
1954-
operands: &inst.operands,
1955-
all_generic_args: inputs_generic_args.clone(),
1956-
transform: None,
1957-
};
1927+
let result_type = match inst.class.opcode {
1928+
// HACK(eddyb) workaround for `OpFunction`, see earlier HACK comment.
1929+
Op::Function => Some(
1930+
InferOperand::from_operand_and_generic_args(
1931+
&Operand::IdRef(inst.result_type.unwrap()),
1932+
inputs_generic_args.clone(),
1933+
cx,
1934+
)
1935+
.0,
1936+
),
1937+
_ => type_of_result.clone(),
1938+
};
1939+
let inputs = InferOperandList {
1940+
operands: &inst.operands,
1941+
all_generic_args: inputs_generic_args.clone(),
1942+
transform: None,
1943+
};
19581944

1959-
if inst_loc != InstructionLocation::Module {
1960-
eprint!(" ");
1961-
}
1962-
eprint!("{prefix}");
1963-
if let Some(result_id) = inst.result_id {
1964-
eprint!("%{result_id} = ");
1965-
}
1966-
eprint!("Op{:?}", inst.class.opcode);
1967-
for operand in result_type.into_iter().chain(inputs.iter(cx)) {
1968-
eprint!(" {}", operand.display_with_infer_cx(cx));
1969-
}
1970-
eprintln!();
1945+
if inst_loc != InstructionLocation::Module {
1946+
debug!(" ");
1947+
}
1948+
debug!("{prefix}");
1949+
if let Some(result_id) = inst.result_id {
1950+
debug!("%{result_id} = ");
1951+
}
1952+
debug!("Op{:?}", inst.class.opcode);
1953+
for operand in result_type.into_iter().chain(inputs.iter(cx)) {
1954+
debug!(" {}", operand.display_with_infer_cx(cx));
19711955
}
1956+
debug!("");
19721957
};
19731958

19741959
// If we have some instruction signatures for `inst`, enforce them.
@@ -1998,12 +1983,10 @@ impl<'a, S: Specialization> InferCx<'a, S> {
19981983
),
19991984
};
20001985

2001-
if self.specializer.debug {
2002-
if inst_loc != InstructionLocation::Module {
2003-
eprint!(" ");
2004-
}
2005-
eprintln!(" found {:?}", m.debug_with_infer_cx(self));
1986+
if inst_loc != InstructionLocation::Module {
1987+
debug!(" ");
20061988
}
1989+
debug!(" found {:?}", m.debug_with_infer_cx(self));
20071990

20081991
if let Err(e) = self.equate_match_findings(m) {
20091992
e.report(inst);
@@ -2032,24 +2015,20 @@ impl<'a, S: Specialization> InferCx<'a, S> {
20322015
fn instantiate_function(&mut self, func: &'a Function) {
20332016
let func_id = func.def_id().unwrap();
20342017

2035-
if self.specializer.debug {
2036-
eprintln!();
2037-
eprint!("specializer::instantiate_function(%{func_id}");
2038-
if let Some(name) = self.specializer.debug_names.get(&func_id) {
2039-
eprint!(" {name}");
2040-
}
2041-
eprintln!("):");
2018+
debug!("");
2019+
debug!("specializer::instantiate_function(%{func_id}");
2020+
if let Some(name) = self.specializer.debug_names.get(&func_id) {
2021+
debug!(" {name}");
20422022
}
2023+
debug!("):");
20432024

20442025
// Instantiate the defining `OpFunction` first, so that the first
20452026
// inference variables match the parameters from the `Generic`
20462027
// (if the `OpTypeFunction` is "generic", that is).
20472028
assert!(self.infer_var_values.is_empty());
20482029
self.instantiate_instruction(func.def.as_ref().unwrap(), InstructionLocation::Module);
20492030

2050-
if self.specializer.debug {
2051-
eprintln!("infer body {{");
2052-
}
2031+
debug!("infer body {{");
20532032

20542033
// If the `OpTypeFunction` is indeed "generic", we have to extract the
20552034
// return / parameter types for `OpReturnValue` and `OpFunctionParameter`.
@@ -2074,14 +2053,12 @@ impl<'a, S: Specialization> InferCx<'a, S> {
20742053
let (i, param) = params.next().unwrap();
20752054
assert_eq!(param.class.opcode, Op::FunctionParameter);
20762055

2077-
if self.specializer.debug {
2078-
eprintln!(
2079-
" %{} = Op{:?} {}",
2080-
param.result_id.unwrap(),
2081-
param.class.opcode,
2082-
param_ty.display_with_infer_cx(self)
2083-
);
2084-
}
2056+
debug!(
2057+
" %{} = Op{:?} {}",
2058+
param.result_id.unwrap(),
2059+
param.class.opcode,
2060+
param_ty.display_with_infer_cx(self)
2061+
);
20852062

20862063
self.record_instantiated_operand(
20872064
OperandLocation {
@@ -2133,13 +2110,11 @@ impl<'a, S: Specialization> InferCx<'a, S> {
21332110
}
21342111
}
21352112

2136-
if self.specializer.debug {
2137-
eprint!("}}");
2138-
if let Some(func_ty) = self.type_of_result.get(&func_id) {
2139-
eprint!(" -> %{}: {}", func_id, func_ty.display_with_infer_cx(self));
2140-
}
2141-
eprintln!();
2113+
debug!("}}");
2114+
if let Some(func_ty) = self.type_of_result.get(&func_id) {
2115+
debug!(" -> %{}: {}", func_id, func_ty.display_with_infer_cx(self));
21422116
}
2117+
debug!("");
21432118
}
21442119

21452120
/// Helper for `into_replacements`, that computes a single `ConcreteOrParam`.

crates/rustc_codegen_spirv/src/linker/zombies.rs

+17-13
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use rustc_errors::Diag;
1010
use rustc_session::Session;
1111
use rustc_span::{DUMMY_SP, Span};
1212
use smallvec::SmallVec;
13+
use tracing::{Level, debug};
1314

1415
#[derive(Copy, Clone)]
1516
struct Zombie<'a> {
@@ -321,11 +322,7 @@ impl<'a> ZombieReporter<'a> {
321322
}
322323
}
323324

324-
pub fn report_and_remove_zombies(
325-
sess: &Session,
326-
opts: &super::Options,
327-
module: &mut Module,
328-
) -> super::Result<()> {
325+
pub fn report_and_remove_zombies(sess: &Session, module: &mut Module) -> super::Result<()> {
329326
let mut zombies = Zombies {
330327
// FIXME(eddyb) avoid repeating this across different passes/helpers.
331328
custom_ext_inst_set_import: module
@@ -345,9 +342,7 @@ pub fn report_and_remove_zombies(
345342
while zombies.spread(module) {}
346343

347344
let result = ZombieReporter::new(sess, module, &zombies).report_all();
348-
349-
// FIXME(eddyb) use `log`/`tracing` instead.
350-
if opts.print_all_zombie {
345+
if tracing::enabled!(target: "print_all_zombie", Level::DEBUG) {
351346
let mut span_regen = SpanRegenerator::new(sess.source_map(), module);
352347
for &zombie_id in zombies.id_to_zombie_kind.keys() {
353348
let mut zombie_leaf_id = zombie_id;
@@ -362,15 +357,21 @@ pub fn report_and_remove_zombies(
362357
}
363358

364359
let reason = span_regen.zombie_for_id(zombie_leaf_id).unwrap().reason;
365-
eprint!("zombie'd %{zombie_id} because {reason}");
360+
debug!(
361+
target: "print_all_zombie",
362+
"zombie'd %{zombie_id} because {reason}"
363+
);
366364
if !infection_chain.is_empty() {
367-
eprint!(" (infected via {:?})", infection_chain);
365+
debug!(
366+
target: "print_all_zombie",
367+
" (infected via {:?})", infection_chain
368+
);
368369
}
369-
eprintln!();
370+
debug!(target: "print_all_zombie", "");
370371
}
371372
}
372373

373-
if opts.print_zombie {
374+
if tracing::enabled!(target: "print_zombie", Level::DEBUG) {
374375
let mut span_regen = SpanRegenerator::new(sess.source_map(), module);
375376
let names = get_names(module);
376377
for f in &module.functions {
@@ -386,7 +387,10 @@ pub fn report_and_remove_zombies(
386387

387388
let name = get_name(&names, f.def_id().unwrap());
388389
let reason = span_regen.zombie_for_id(zombie_leaf_id).unwrap().reason;
389-
eprintln!("function removed {name:?} because {reason:?}");
390+
debug!(
391+
target: "print_zombie",
392+
"function removed {name:?} because {reason:?}"
393+
);
390394
}
391395
}
392396
}

docs/src/SUMMARY.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
- [Contributing to Rust-GPU]()
55
- [Building](./building-rust-gpu.md)
66
- [Testing](./testing.md)
7+
- [Debugging](./tracing)
8+
- [Tracing](./tracing.md)
9+
- [Minimizing bugs in SPIR-V](./spirv-minimization.md)
710
- ["Codegen args" (flags/options) supported by the Rust-GPU codegen backend](./codegen-args.md)
8-
- [Minimizing bugs in SPIR-V](./spirv-minimization.md)
911
- [Publishing Rust-GPU on crates.io](./publishing-rust-gpu.md)
1012
- [Platform Support](./platform-support.md)
1113
- [Writing Shader Crates](./writing-shader-crates.md)

0 commit comments

Comments
 (0)