|
| 1 | +//! Passes that pertain to `OpEntryPoint`'s "interface variables". |
| 2 | +
|
| 3 | +use crate::linker::ipo::CallGraph; |
| 4 | +use indexmap::{IndexMap, IndexSet}; |
| 5 | +use rspirv::dr::{Module, Operand}; |
| 6 | +use rspirv::spirv::{Op, StorageClass, Word}; |
| 7 | +use std::mem; |
| 8 | + |
| 9 | +type Id = Word; |
| 10 | + |
| 11 | +/// Update `OpEntryPoint`s to contain all of the `OpVariable`s they reference, |
| 12 | +/// whether directly or through some function in their call graph. |
| 13 | +/// |
| 14 | +/// This is needed for (arguably-not-interface) `Private` in SPIR-V >= 1.4, |
| 15 | +/// but also any interface variables declared "out of band" (e.g. via `asm!`). |
| 16 | +pub fn gather_all_interface_vars_from_uses(module: &mut Module) { |
| 17 | + // Start by mapping out which global (i.e. `OpVariable` or constants) IDs |
| 18 | + // can be used to access any interface-relevant `OpVariable`s |
| 19 | + // (where "interface-relevant" depends on the version, see comments below). |
| 20 | + let mut used_vars_per_global_id: IndexMap<Id, IndexSet<Id>> = IndexMap::new(); |
| 21 | + let version = module.header.as_ref().unwrap().version(); |
| 22 | + for inst in &module.types_global_values { |
| 23 | + let mut used_vars = IndexSet::new(); |
| 24 | + |
| 25 | + // Base case: the global itself is an interface-relevant `OpVariable`. |
| 26 | + let interface_relevant_var = inst.class.opcode == Op::Variable && { |
| 27 | + if version > (1, 3) { |
| 28 | + // SPIR-V >= v1.4 includes all OpVariables in the interface. |
| 29 | + true |
| 30 | + } else { |
| 31 | + let storage_class = inst.operands[0].unwrap_storage_class(); |
| 32 | + // SPIR-V <= v1.3 only includes Input and Output in the interface. |
| 33 | + storage_class == StorageClass::Input || storage_class == StorageClass::Output |
| 34 | + } |
| 35 | + }; |
| 36 | + if interface_relevant_var { |
| 37 | + used_vars.insert(inst.result_id.unwrap()); |
| 38 | + } |
| 39 | + |
| 40 | + // Nested constant refs (e.g. `&&&0`) can create chains of `OpVariable`s |
| 41 | + // where only the outer-most `OpVariable` may be accessed directly, |
| 42 | + // but the interface variables need to include all the nesting levels. |
| 43 | + used_vars.extend( |
| 44 | + inst.operands |
| 45 | + .iter() |
| 46 | + .filter_map(|operand| operand.id_ref_any()) |
| 47 | + .filter_map(|id| used_vars_per_global_id.get(&id)) |
| 48 | + .flatten(), |
| 49 | + ); |
| 50 | + |
| 51 | + if !used_vars.is_empty() { |
| 52 | + used_vars_per_global_id.insert(inst.result_id.unwrap(), used_vars); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + // Initial uses come from functions directly referencing global instructions. |
| 57 | + let mut used_vars_per_fn_idx: Vec<IndexSet<Id>> = module |
| 58 | + .functions |
| 59 | + .iter() |
| 60 | + .map(|func| { |
| 61 | + func.all_inst_iter() |
| 62 | + .flat_map(|inst| &inst.operands) |
| 63 | + .filter_map(|operand| operand.id_ref_any()) |
| 64 | + .filter_map(|id| used_vars_per_global_id.get(&id)) |
| 65 | + .flatten() |
| 66 | + .copied() |
| 67 | + .collect() |
| 68 | + }) |
| 69 | + .collect(); |
| 70 | + |
| 71 | + // Uses can then be propagated through the call graph, from callee to caller. |
| 72 | + let call_graph = CallGraph::collect(module); |
| 73 | + for caller_idx in call_graph.post_order() { |
| 74 | + let mut used_vars = mem::take(&mut used_vars_per_fn_idx[caller_idx]); |
| 75 | + for &callee_idx in &call_graph.callees[caller_idx] { |
| 76 | + used_vars.extend(&used_vars_per_fn_idx[callee_idx]); |
| 77 | + } |
| 78 | + used_vars_per_fn_idx[caller_idx] = used_vars; |
| 79 | + } |
| 80 | + |
| 81 | + // All transitive uses are available, add them to `OpEntryPoint`s. |
| 82 | + for (i, entry) in module.entry_points.iter_mut().enumerate() { |
| 83 | + assert_eq!(entry.class.opcode, Op::EntryPoint); |
| 84 | + let &entry_func_idx = call_graph.entry_points.get_index(i).unwrap(); |
| 85 | + assert_eq!( |
| 86 | + module.functions[entry_func_idx].def_id().unwrap(), |
| 87 | + entry.operands[1].unwrap_id_ref() |
| 88 | + ); |
| 89 | + |
| 90 | + // NOTE(eddyb) it might be better to remove any unused vars, or warn |
| 91 | + // the user about their presence, but for now this keeps them around. |
| 92 | + let mut interface_vars: IndexSet<Id> = entry.operands[3..] |
| 93 | + .iter() |
| 94 | + .map(|operand| operand.unwrap_id_ref()) |
| 95 | + .collect(); |
| 96 | + |
| 97 | + interface_vars.extend(&used_vars_per_fn_idx[entry_func_idx]); |
| 98 | + |
| 99 | + entry.operands.truncate(3); |
| 100 | + entry |
| 101 | + .operands |
| 102 | + .extend(interface_vars.iter().map(|&id| Operand::IdRef(id))); |
| 103 | + } |
| 104 | +} |
0 commit comments