Skip to content

Commit 5e23a73

Browse files
committed
Move CallGraph into a new ipo module.
1 parent f951203 commit 5e23a73

File tree

3 files changed

+93
-83
lines changed

3 files changed

+93
-83
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
//! Tools for interprocedural optimizations (aka "IPO"s).
2+
3+
// FIXME(eddyb) perhaps make all IPOs sub-modules of this module?
4+
5+
use indexmap::IndexSet;
6+
use rspirv::dr::Module;
7+
use rspirv::spirv::Op;
8+
use rustc_data_structures::fx::FxHashMap;
9+
10+
// FIXME(eddyb) use newtyped indices and `IndexVec`.
11+
type FuncIdx = usize;
12+
13+
pub struct CallGraph {
14+
entry_points: IndexSet<FuncIdx>,
15+
16+
/// `callees[i].contains(j)` implies `functions[i]` calls `functions[j]`.
17+
callees: Vec<IndexSet<FuncIdx>>,
18+
}
19+
20+
impl CallGraph {
21+
pub fn collect(module: &Module) -> Self {
22+
let func_id_to_idx: FxHashMap<_, _> = module
23+
.functions
24+
.iter()
25+
.enumerate()
26+
.map(|(i, func)| (func.def_id().unwrap(), i))
27+
.collect();
28+
let entry_points = module
29+
.entry_points
30+
.iter()
31+
.map(|entry| {
32+
assert_eq!(entry.class.opcode, Op::EntryPoint);
33+
func_id_to_idx[&entry.operands[1].unwrap_id_ref()]
34+
})
35+
.collect();
36+
let callees = module
37+
.functions
38+
.iter()
39+
.map(|func| {
40+
func.all_inst_iter()
41+
.filter(|inst| inst.class.opcode == Op::FunctionCall)
42+
.map(|inst| func_id_to_idx[&inst.operands[0].unwrap_id_ref()])
43+
.collect()
44+
})
45+
.collect();
46+
Self {
47+
entry_points,
48+
callees,
49+
}
50+
}
51+
52+
/// Order functions using a post-order traversal, i.e. callees before callers.
53+
// FIXME(eddyb) replace this with `rustc_data_structures::graph::iterate`
54+
// (or similar).
55+
pub fn post_order(&self) -> Vec<FuncIdx> {
56+
let num_funcs = self.callees.len();
57+
58+
// FIXME(eddyb) use a proper bitset.
59+
let mut visited = vec![false; num_funcs];
60+
let mut post_order = Vec::with_capacity(num_funcs);
61+
62+
// Visit the call graph with entry points as roots.
63+
for &entry in &self.entry_points {
64+
self.post_order_step(entry, &mut visited, &mut post_order);
65+
}
66+
67+
// Also visit any functions that were not reached from entry points
68+
// (they might be dead but they should be processed nonetheless).
69+
for func in 0..num_funcs {
70+
if !visited[func] {
71+
self.post_order_step(func, &mut visited, &mut post_order);
72+
}
73+
}
74+
75+
post_order
76+
}
77+
78+
fn post_order_step(&self, func: FuncIdx, visited: &mut [bool], post_order: &mut Vec<FuncIdx>) {
79+
if visited[func] {
80+
return;
81+
}
82+
visited[func] = true;
83+
84+
for &callee in &self.callees[func] {
85+
self.post_order_step(callee, visited, post_order);
86+
}
87+
88+
post_order.push(func);
89+
}
90+
}

crates/rustc_codegen_spirv/src/linker/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ mod destructure_composites;
66
mod duplicates;
77
mod import_export_link;
88
mod inline;
9+
mod ipo;
910
mod mem2reg;
1011
mod peephole_opts;
1112
mod simple_passes;

crates/rustc_codegen_spirv/src/linker/specializer.rs

Lines changed: 2 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@
4949
//! expand_params: Option<Vec<usize>>,
5050
//! ```
5151
52+
use crate::linker::ipo::CallGraph;
5253
use crate::spirv_type_constraints::{self, InstSig, StorageClassPat, TyListPat, TyPat};
53-
use indexmap::{IndexMap, IndexSet};
54+
use indexmap::IndexMap;
5455
use rspirv::dr::{Builder, Function, Instruction, Module, Operand};
5556
use rspirv::spirv::{Op, StorageClass, Word};
5657
use rustc_data_structures::captures::Captures;
@@ -186,88 +187,6 @@ pub fn specialize(module: Module, specialization: impl Specialization) -> Module
186187
expander.expand_module()
187188
}
188189

189-
// FIXME(eddyb) use newtyped indices and `IndexVec`.
190-
type FuncIdx = usize;
191-
192-
struct CallGraph {
193-
entry_points: IndexSet<FuncIdx>,
194-
195-
/// `callees[i].contains(j)` implies `functions[i]` calls `functions[j]`.
196-
callees: Vec<IndexSet<FuncIdx>>,
197-
}
198-
199-
impl CallGraph {
200-
fn collect(module: &Module) -> Self {
201-
let func_id_to_idx: FxHashMap<_, _> = module
202-
.functions
203-
.iter()
204-
.enumerate()
205-
.map(|(i, func)| (func.def_id().unwrap(), i))
206-
.collect();
207-
let entry_points = module
208-
.entry_points
209-
.iter()
210-
.map(|entry| {
211-
assert_eq!(entry.class.opcode, Op::EntryPoint);
212-
func_id_to_idx[&entry.operands[1].unwrap_id_ref()]
213-
})
214-
.collect();
215-
let callees = module
216-
.functions
217-
.iter()
218-
.map(|func| {
219-
func.all_inst_iter()
220-
.filter(|inst| inst.class.opcode == Op::FunctionCall)
221-
.map(|inst| func_id_to_idx[&inst.operands[0].unwrap_id_ref()])
222-
.collect()
223-
})
224-
.collect();
225-
Self {
226-
entry_points,
227-
callees,
228-
}
229-
}
230-
231-
/// Order functions using a post-order traversal, i.e. callees before callers.
232-
// FIXME(eddyb) replace this with `rustc_data_structures::graph::iterate`
233-
// (or similar).
234-
fn post_order(&self) -> Vec<FuncIdx> {
235-
let num_funcs = self.callees.len();
236-
237-
// FIXME(eddyb) use a proper bitset.
238-
let mut visited = vec![false; num_funcs];
239-
let mut post_order = Vec::with_capacity(num_funcs);
240-
241-
// Visit the call graph with entry points as roots.
242-
for &entry in &self.entry_points {
243-
self.post_order_step(entry, &mut visited, &mut post_order);
244-
}
245-
246-
// Also visit any functions that were not reached from entry points
247-
// (they might be dead but they should be processed nonetheless).
248-
for func in 0..num_funcs {
249-
if !visited[func] {
250-
self.post_order_step(func, &mut visited, &mut post_order);
251-
}
252-
}
253-
254-
post_order
255-
}
256-
257-
fn post_order_step(&self, func: FuncIdx, visited: &mut [bool], post_order: &mut Vec<FuncIdx>) {
258-
if visited[func] {
259-
return;
260-
}
261-
visited[func] = true;
262-
263-
for &callee in &self.callees[func] {
264-
self.post_order_step(callee, visited, post_order);
265-
}
266-
267-
post_order.push(func);
268-
}
269-
}
270-
271190
// HACK(eddyb) `Copy` version of `Operand` that only includes the cases that
272191
// are relevant to the inference algorithm (and is also smaller).
273192
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]

0 commit comments

Comments
 (0)