1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
//! See documentation on `CodegenCx::zombie` for a description of the zombie system.

use super::{get_name, get_names};
use crate::custom_decorations::{CustomDecoration, SpanRegenerator, ZombieDecoration};
use crate::custom_insts::{self, CustomOp};
use rspirv::dr::{Instruction, Module, Operand};
use rspirv::spirv::{Op, Word};
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap};
use rustc_errors::{DiagnosticBuilder, ErrorGuaranteed};
use rustc_session::Session;
use rustc_span::{Span, DUMMY_SP};
use smallvec::SmallVec;

#[derive(Copy, Clone)]
struct Zombie<'a> {
    id: Word,
    kind: &'a ZombieKind<'a>,
}

enum ZombieKind<'a> {
    /// Definition annotated with `ZombieDecoration`.
    Leaf,

    /// Transitively zombie'd by using other zombies, from an instruction.
    Uses(Vec<ZombieUse<'a>>),
}

struct ZombieUse<'a> {
    used_zombie_id: Word,

    /// Active debug "source location" instruction at the time of the use, if any
    /// (both `OpLine` and `CustomInst::SetDebugSrcLoc` are supported).
    use_debug_src_loc_inst: Option<&'a Instruction>,

    origin: UseOrigin,
}

enum UseOrigin {
    GlobalOperandOrResultType,
    IntraFuncOperandOrResultType { parent_func_id: Word },
    CallCalleeOperand { caller_func_id: Word },
}

struct Zombies<'a> {
    /// ID of `OpExtInstImport` for our custom "extended instruction set",
    /// if present (see `crate::custom_insts` for more details).
    custom_ext_inst_set_import: Option<Word>,

    id_to_zombie_kind: FxIndexMap<Word, ZombieKind<'a>>,
}

impl<'a> Zombies<'a> {
    // FIXME(eddyb) rename all the other methods to say `_inst` explicitly.
    fn get_zombie_by_id(&self, id: Word) -> Option<Zombie<'_>> {
        self.id_to_zombie_kind
            .get(&id)
            .map(|kind| Zombie { id, kind })
    }

    fn zombies_used_from_inst<'b>(
        &'b self,
        inst: &'b Instruction,
    ) -> impl Iterator<Item = Zombie<'b>> + 'b {
        inst.result_type
            .into_iter()
            .chain(inst.operands.iter().filter_map(|op| op.id_ref_any()))
            .filter_map(move |id| self.get_zombie_by_id(id))
    }

    fn spread(&mut self, module: &'a Module) -> bool {
        let mut any = false;
        // globals are easy
        {
            let mut debug_src_loc_inst = None;
            for inst in module.global_inst_iter() {
                match inst.class.opcode {
                    Op::Line => debug_src_loc_inst = Some(inst),
                    Op::NoLine => debug_src_loc_inst = None,
                    Op::ExtInst
                        if Some(inst.operands[0].unwrap_id_ref())
                            == self.custom_ext_inst_set_import =>
                    {
                        match CustomOp::decode_from_ext_inst(inst) {
                            CustomOp::SetDebugSrcLoc => debug_src_loc_inst = Some(inst),
                            CustomOp::ClearDebugSrcLoc => debug_src_loc_inst = None,
                            _ => {}
                        }
                    }
                    _ => {}
                }

                if let Some(result_id) = inst.result_id {
                    if self.id_to_zombie_kind.contains_key(&result_id) {
                        continue;
                    }
                    let zombie_uses: Vec<_> = self
                        .zombies_used_from_inst(inst)
                        .map(|zombie| ZombieUse {
                            used_zombie_id: zombie.id,
                            use_debug_src_loc_inst: debug_src_loc_inst,
                            origin: UseOrigin::GlobalOperandOrResultType,
                        })
                        .collect();
                    if !zombie_uses.is_empty() {
                        self.id_to_zombie_kind
                            .insert(result_id, ZombieKind::Uses(zombie_uses));
                        any = true;
                    }
                }
            }
        }
        // No need to zombie defs within a function: If any def within a function is zombied, then the
        // whole function is zombied. But, we don't have to mark the defs within a function as zombie,
        // because the defs can't escape the function.
        // HACK(eddyb) one exception to this is function-local variables, or the
        // `OpBitcast`s of pointer casts, either of which which may actually be
        // unused and as such cannot be allowed to always zombie the function.
        for func in &module.functions {
            let func_id = func.def_id().unwrap();
            if self.id_to_zombie_kind.contains_key(&func_id) {
                // Func is already zombie, no need to scan it again.
                continue;
            }

            let mut all_zombie_uses_in_func = vec![];
            let mut debug_src_loc_inst = None;
            for inst in func.all_inst_iter() {
                match inst.class.opcode {
                    Op::Line => debug_src_loc_inst = Some(inst),
                    // NOTE(eddyb) each block starts out with cleared debuginfo.
                    Op::Label | Op::NoLine => debug_src_loc_inst = None,
                    Op::ExtInst
                        if Some(inst.operands[0].unwrap_id_ref())
                            == self.custom_ext_inst_set_import =>
                    {
                        match CustomOp::decode_from_ext_inst(inst) {
                            CustomOp::SetDebugSrcLoc => debug_src_loc_inst = Some(inst),
                            CustomOp::ClearDebugSrcLoc => debug_src_loc_inst = None,
                            _ => {}
                        }
                    }
                    _ => {}
                }

                if [Op::Variable, Op::Bitcast].contains(&inst.class.opcode) {
                    let result_id = inst.result_id.unwrap();
                    if self.id_to_zombie_kind.contains_key(&result_id) {
                        continue;
                    }
                    let zombie_uses: Vec<_> = self
                        .zombies_used_from_inst(inst)
                        .map(|zombie| ZombieUse {
                            used_zombie_id: zombie.id,
                            use_debug_src_loc_inst: debug_src_loc_inst,
                            origin: UseOrigin::IntraFuncOperandOrResultType {
                                parent_func_id: func_id,
                            },
                        })
                        .collect();
                    if !zombie_uses.is_empty() {
                        self.id_to_zombie_kind
                            .insert(result_id, ZombieKind::Uses(zombie_uses));
                        any = true;
                    }
                    continue;
                }

                all_zombie_uses_in_func.extend(
                    inst.result_id
                        .and_then(|result_id| self.get_zombie_by_id(result_id))
                        .into_iter()
                        .chain(self.zombies_used_from_inst(inst))
                        .map(|zombie| {
                            let origin = if inst.class.opcode == Op::FunctionCall
                                && inst.operands[0] == Operand::IdRef(zombie.id)
                            {
                                UseOrigin::CallCalleeOperand {
                                    caller_func_id: func_id,
                                }
                            } else {
                                UseOrigin::IntraFuncOperandOrResultType {
                                    parent_func_id: func_id,
                                }
                            };
                            ZombieUse {
                                used_zombie_id: zombie.id,
                                use_debug_src_loc_inst: debug_src_loc_inst,
                                origin,
                            }
                        }),
                );
            }
            if !all_zombie_uses_in_func.is_empty() {
                self.id_to_zombie_kind
                    .insert(func_id, ZombieKind::Uses(all_zombie_uses_in_func));
                any = true;
            }
        }
        any
    }
}

struct ZombieReporter<'a> {
    sess: &'a Session,
    module: &'a Module,
    zombies: &'a Zombies<'a>,

    id_to_name: Option<FxHashMap<Word, &'a str>>,
    span_regen: SpanRegenerator<'a>,
}
impl<'a> ZombieReporter<'a> {
    fn new(sess: &'a Session, module: &'a Module, zombies: &'a Zombies<'a>) -> Self {
        Self {
            sess,
            module,
            zombies,

            id_to_name: None,
            span_regen: SpanRegenerator::new(sess.source_map(), module),
        }
    }

    // If an entry point references a zombie'd value, then the entry point would normally get removed.
    // That's an absolutely horrible experience to debug, though, so instead, create a nice error
    // message containing the stack trace of how the entry point got to the zombie value.
    fn report_all(mut self) -> super::Result<()> {
        let mut result = Ok(());
        // FIXME(eddyb) this loop means that every entry-point can potentially
        // list out all the leaves, but that shouldn't be a huge issue.
        for root_id in super::dce::collect_roots(self.module) {
            if let Some(zombie) = self.zombies.get_zombie_by_id(root_id) {
                for (_, mut err) in self.build_errors_keyed_by_leaf_id(zombie) {
                    result = Err(err.emit());
                }
            }
        }
        result
    }

    fn add_use_note_to_err(
        &mut self,
        err: &mut DiagnosticBuilder<'a, ErrorGuaranteed>,
        span: Span,
        zombie: Zombie<'_>,
        zombie_use: &ZombieUse<'_>,
    ) {
        let mut id_to_name = |id, kind| {
            self.id_to_name
                .get_or_insert_with(|| get_names(self.module))
                .get(&id)
                .map_or_else(
                    || format!("unnamed {kind} (%{id})"),
                    |&name| format!("`{name}`"),
                )
        };
        let note = match zombie_use.origin {
            UseOrigin::GlobalOperandOrResultType => {
                format!("used by {}", id_to_name(zombie.id, "global"))
            }
            UseOrigin::IntraFuncOperandOrResultType { parent_func_id } => {
                format!(
                    "used from within {}",
                    id_to_name(parent_func_id, "function")
                )
            }
            UseOrigin::CallCalleeOperand { caller_func_id } => {
                format!("called by {}", id_to_name(caller_func_id, "function"))
            }
        };
        let span = zombie_use
            .use_debug_src_loc_inst
            .and_then(|inst| self.span_regen.src_loc_from_debug_inst(inst))
            .and_then(|src_loc| self.span_regen.src_loc_to_rustc(src_loc))
            .unwrap_or(span);
        err.span_note(span, note);
    }

    fn build_errors_keyed_by_leaf_id(
        &mut self,
        zombie: Zombie<'_>,
    ) -> FxIndexMap<Word, DiagnosticBuilder<'a, ErrorGuaranteed>> {
        // FIXME(eddyb) this is a bit inefficient, compared to some kind of
        // "small map", but this is the error path, and being correct is more
        // important here - in particular, we don't want to ignore *any* leaves.
        let mut errors_keyed_by_leaf_id = FxIndexMap::default();

        let span = self
            .span_regen
            .src_loc_for_id(zombie.id)
            .and_then(|src_loc| self.span_regen.src_loc_to_rustc(src_loc))
            .unwrap_or(DUMMY_SP);
        match zombie.kind {
            ZombieKind::Leaf => {
                let reason = self.span_regen.zombie_for_id(zombie.id).unwrap().reason;
                errors_keyed_by_leaf_id.insert(
                    zombie.id,
                    self.sess.struct_span_err(span, reason.to_string()),
                );
            }
            ZombieKind::Uses(zombie_uses) => {
                for zombie_use in zombie_uses {
                    let used_zombie = self
                        .zombies
                        .get_zombie_by_id(zombie_use.used_zombie_id)
                        .unwrap();
                    for (leaf_id, err) in self.build_errors_keyed_by_leaf_id(used_zombie) {
                        use rustc_data_structures::fx::IndexEntry as Entry;
                        match errors_keyed_by_leaf_id.entry(leaf_id) {
                            Entry::Occupied(_) => err.cancel(),
                            Entry::Vacant(entry) => {
                                self.add_use_note_to_err(
                                    entry.insert(err),
                                    span,
                                    zombie,
                                    zombie_use,
                                );
                            }
                        }
                    }
                }
            }
        }
        errors_keyed_by_leaf_id
    }
}

pub fn report_and_remove_zombies(
    sess: &Session,
    opts: &super::Options,
    module: &mut Module,
) -> super::Result<()> {
    let mut zombies = Zombies {
        // FIXME(eddyb) avoid repeating this across different passes/helpers.
        custom_ext_inst_set_import: module
            .ext_inst_imports
            .iter()
            .find(|inst| {
                assert_eq!(inst.class.opcode, Op::ExtInstImport);
                inst.operands[0].unwrap_literal_string() == &custom_insts::CUSTOM_EXT_INST_SET[..]
            })
            .map(|inst| inst.result_id.unwrap()),

        id_to_zombie_kind: ZombieDecoration::decode_all(module)
            .map(|(id, _)| (id, ZombieKind::Leaf))
            .collect(),
    };
    // Note: This is O(n^2).
    while zombies.spread(module) {}

    let result = ZombieReporter::new(sess, module, &zombies).report_all();

    // FIXME(eddyb) use `log`/`tracing` instead.
    if opts.print_all_zombie {
        let mut span_regen = SpanRegenerator::new(sess.source_map(), module);
        for &zombie_id in zombies.id_to_zombie_kind.keys() {
            let mut zombie_leaf_id = zombie_id;
            let mut infection_chain = SmallVec::<[_; 4]>::new();
            loop {
                zombie_leaf_id = match zombies.get_zombie_by_id(zombie_leaf_id).unwrap().kind {
                    ZombieKind::Leaf => break,
                    // FIXME(eddyb) this is all very lossy and should probably go away.
                    ZombieKind::Uses(zombie_uses) => zombie_uses[0].used_zombie_id,
                };
                infection_chain.push(zombie_leaf_id);
            }

            let reason = span_regen.zombie_for_id(zombie_leaf_id).unwrap().reason;
            eprint!("zombie'd %{zombie_id} because {reason}");
            if !infection_chain.is_empty() {
                eprint!(" (infected via {:?})", infection_chain);
            }
            eprintln!();
        }
    }

    if opts.print_zombie {
        let mut span_regen = SpanRegenerator::new(sess.source_map(), module);
        let names = get_names(module);
        for f in &module.functions {
            if let Some(zombie) = zombies.get_zombie_by_id(f.def_id().unwrap()) {
                let mut zombie_leaf_id = zombie.id;
                loop {
                    zombie_leaf_id = match zombies.get_zombie_by_id(zombie_leaf_id).unwrap().kind {
                        ZombieKind::Leaf => break,
                        // FIXME(eddyb) this is all very lossy and should probably go away.
                        ZombieKind::Uses(zombie_uses) => zombie_uses[0].used_zombie_id,
                    };
                }

                let name = get_name(&names, f.def_id().unwrap());
                let reason = span_regen.zombie_for_id(zombie_leaf_id).unwrap().reason;
                eprintln!("function removed {name:?} because {reason:?}");
            }
        }
    }

    // FIXME(eddyb) this should be unnecessary, either something is unused, and
    // it will get DCE'd *anyway*, or it caused an error.
    {
        // HACK(eddyb) cannot use the original map because it borrows the `Module`.
        let all_zombies: FxHashSet<_> = zombies.id_to_zombie_kind.into_keys().collect();
        let keep = |inst: &Instruction| {
            if let Some(result_id) = inst.result_id {
                !all_zombies.contains(&result_id)
            } else {
                let mut inst_ids = inst
                    .result_type
                    .into_iter()
                    .chain(inst.operands.iter().filter_map(|op| op.id_ref_any()));
                !inst_ids.any(|id| all_zombies.contains(&id))
            }
        };
        module.capabilities.retain(keep);
        module.extensions.retain(keep);
        module.ext_inst_imports.retain(keep);
        module.memory_model = module.memory_model.take().filter(keep);
        module.entry_points.retain(keep);
        module.execution_modes.retain(keep);
        module.debug_string_source.retain(keep);
        module.debug_names.retain(keep);
        module.debug_module_processed.retain(keep);
        module.annotations.retain(keep);
        module.types_global_values.retain(keep);
        module.functions.retain(|f| keep(f.def.as_ref().unwrap()));
    }

    result
}