Skip to content

Commit c3f51fd

Browse files
committed
linker: early zombie reporting doesn't need to mutate the module anymore (thanks to DCE).
1 parent 6aeec0b commit c3f51fd

File tree

3 files changed

+8
-96
lines changed

3 files changed

+8
-96
lines changed

crates/rustc_codegen_spirv/src/linker/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -308,17 +308,17 @@ pub fn link(
308308
simple_passes::check_fragment_insts(sess, &output)?;
309309
}
310310

311-
// HACK(eddyb) this has to run before the `report_and_remove_zombies` pass,
312-
// so that any zombies that are passed as call arguments, but eventually unused,
311+
// HACK(eddyb) this has to run before the `report_zombies` pass, so that
312+
// any zombies that are passed as call arguments, but eventually unused,
313313
// won't be (incorrectly) considered used.
314314
{
315315
let _timer = sess.timer("link_remove_unused_params");
316316
output = param_weakening::remove_unused_params(output);
317317
}
318318

319319
if opts.early_report_zombies {
320-
let _timer = sess.timer("link_report_and_remove_zombies");
321-
zombies::report_and_remove_zombies(sess, &mut output)?;
320+
let _timer = sess.timer("link_report_zombies");
321+
zombies::report_zombies(sess, &output)?;
322322
}
323323

324324
if opts.infer_storage_classes {

crates/rustc_codegen_spirv/src/linker/zombies.rs

Lines changed: 4 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
//! See documentation on `CodegenCx::zombie` for a description of the zombie system.
22
3-
use super::{get_name, get_names};
3+
use super::get_names;
44
use crate::custom_decorations::{CustomDecoration, SpanRegenerator, ZombieDecoration};
55
use crate::custom_insts::{self, CustomOp};
66
use rspirv::dr::{Instruction, Module, Operand};
77
use rspirv::spirv::{Op, Word};
8-
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap};
8+
use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
99
use rustc_errors::Diag;
1010
use rustc_session::Session;
1111
use rustc_span::{DUMMY_SP, Span};
12-
use smallvec::SmallVec;
13-
use tracing::{Level, debug};
1412

1513
#[derive(Copy, Clone)]
1614
struct Zombie<'a> {
@@ -322,7 +320,7 @@ impl<'a> ZombieReporter<'a> {
322320
}
323321
}
324322

325-
pub fn report_and_remove_zombies(sess: &Session, module: &mut Module) -> super::Result<()> {
323+
pub fn report_zombies(sess: &Session, module: &Module) -> super::Result<()> {
326324
let mut zombies = Zombies {
327325
// FIXME(eddyb) avoid repeating this across different passes/helpers.
328326
custom_ext_inst_set_import: module
@@ -341,89 +339,5 @@ pub fn report_and_remove_zombies(sess: &Session, module: &mut Module) -> super::
341339
// Note: This is O(n^2).
342340
while zombies.spread(module) {}
343341

344-
let result = ZombieReporter::new(sess, module, &zombies).report_all();
345-
if tracing::enabled!(target: "print_all_zombie", Level::DEBUG) {
346-
let mut span_regen = SpanRegenerator::new(sess.source_map(), module);
347-
for &zombie_id in zombies.id_to_zombie_kind.keys() {
348-
let mut zombie_leaf_id = zombie_id;
349-
let mut infection_chain = SmallVec::<[_; 4]>::new();
350-
loop {
351-
zombie_leaf_id = match zombies.get_zombie_by_id(zombie_leaf_id).unwrap().kind {
352-
ZombieKind::Leaf => break,
353-
// FIXME(eddyb) this is all very lossy and should probably go away.
354-
ZombieKind::Uses(zombie_uses) => zombie_uses[0].used_zombie_id,
355-
};
356-
infection_chain.push(zombie_leaf_id);
357-
}
358-
359-
let reason = span_regen.zombie_for_id(zombie_leaf_id).unwrap().reason;
360-
debug!(
361-
target: "print_all_zombie",
362-
"zombie'd %{zombie_id} because {reason}"
363-
);
364-
if !infection_chain.is_empty() {
365-
debug!(
366-
target: "print_all_zombie",
367-
" (infected via {:?})", infection_chain
368-
);
369-
}
370-
debug!(target: "print_all_zombie", "");
371-
}
372-
}
373-
374-
if tracing::enabled!(target: "print_zombie", Level::DEBUG) {
375-
let mut span_regen = SpanRegenerator::new(sess.source_map(), module);
376-
let names = get_names(module);
377-
for f in &module.functions {
378-
if let Some(zombie) = zombies.get_zombie_by_id(f.def_id().unwrap()) {
379-
let mut zombie_leaf_id = zombie.id;
380-
loop {
381-
zombie_leaf_id = match zombies.get_zombie_by_id(zombie_leaf_id).unwrap().kind {
382-
ZombieKind::Leaf => break,
383-
// FIXME(eddyb) this is all very lossy and should probably go away.
384-
ZombieKind::Uses(zombie_uses) => zombie_uses[0].used_zombie_id,
385-
};
386-
}
387-
388-
let name = get_name(&names, f.def_id().unwrap());
389-
let reason = span_regen.zombie_for_id(zombie_leaf_id).unwrap().reason;
390-
debug!(
391-
target: "print_zombie",
392-
"function removed {name:?} because {reason:?}"
393-
);
394-
}
395-
}
396-
}
397-
398-
// FIXME(eddyb) this should be unnecessary, either something is unused, and
399-
// it will get DCE'd *anyway*, or it caused an error.
400-
{
401-
// HACK(eddyb) cannot use the original map because it borrows the `Module`.
402-
let all_zombies: FxHashSet<_> = zombies.id_to_zombie_kind.into_keys().collect();
403-
let keep = |inst: &Instruction| {
404-
if let Some(result_id) = inst.result_id {
405-
!all_zombies.contains(&result_id)
406-
} else {
407-
let mut inst_ids = inst
408-
.result_type
409-
.into_iter()
410-
.chain(inst.operands.iter().filter_map(|op| op.id_ref_any()));
411-
!inst_ids.any(|id| all_zombies.contains(&id))
412-
}
413-
};
414-
module.capabilities.retain(keep);
415-
module.extensions.retain(keep);
416-
module.ext_inst_imports.retain(keep);
417-
module.memory_model = module.memory_model.take().filter(keep);
418-
module.entry_points.retain(keep);
419-
module.execution_modes.retain(keep);
420-
module.debug_string_source.retain(keep);
421-
module.debug_names.retain(keep);
422-
module.debug_module_processed.retain(keep);
423-
module.annotations.retain(keep);
424-
module.types_global_values.retain(keep);
425-
module.functions.retain(|f| keep(f.def.as_ref().unwrap()));
426-
}
427-
428-
result
342+
ZombieReporter::new(sess, module, &zombies).report_all()
429343
}

docs/src/tracing.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,3 @@ arguments](./codegen-args.md) to aid observability. As of
1919
have been removed and replaced with the following:
2020

2121
- `--specializer-debug` &rarr; `RUSTGPU_LOG=rustc_codegen_spirv::specializer=debug`
22-
- `--print-zombie` &rarr; `RUSTGPU_LOG=print_zombie=debug`
23-
- `--print-all-zombie` &rarr; `RUSTGPU_LOG=print_all_zombie=debug`

0 commit comments

Comments
 (0)