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
//! The merged call graph.
//!
//! The driver emits one [`Artifact`] per crate. Merging them yields a single
//! graph in which every function has a dense index, which keeps the solver's
//! state in flat vectors.
use crate::{
model::{Artifact, Body, BuildConfig, FuncKey},
util::Map,
};
/// A dense index into [`Graph`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct FuncId(pub u32);
impl FuncId {
/// The index as a `usize`, for slicing.
#[must_use]
pub const fn index(self) -> usize {
self.0 as usize
}
/// A dense index turned into an identifier.
///
/// A graph that large cannot be built in the first place, so clamping is
/// only here to keep the conversion total.
#[must_use]
pub fn from_index(index: usize) -> Self {
Self(u32::try_from(index).unwrap_or(u32::MAX))
}
}
/// A merged, indexed call graph.
#[derive(Debug)]
pub struct Graph {
bodies: Vec<Body>,
by_key: Map<FuncKey, FuncId>,
callers: Vec<Vec<FuncId>>,
config: Option<BuildConfig>,
}
impl Graph {
/// Merges per-crate artifacts into one graph.
///
/// A function may be observed more than once, because a generic body is
/// instantiated in every crate that uses it. The richest record wins: a
/// body with MIR always replaces an opaque placeholder.
#[must_use]
pub fn from_artifacts(artifacts: Vec<Artifact>) -> Self {
let mut graph = Self {
bodies: Vec::new(),
by_key: Map::default(),
callers: Vec::new(),
config: None,
};
for artifact in artifacts {
if graph.config.is_none() {
graph.config = Some(artifact.config.clone());
}
for body in artifact.bodies {
graph.insert_body(body);
}
}
graph.materialize_missing_callees();
graph.build_reverse_edges();
graph
}
/// Adds or upgrades one body.
fn insert_body(&mut self, body: Body) {
if let Some(&id) = self.by_key.get(&body.key) {
let existing = &mut self.bodies[id.index()];
// A real body always beats a placeholder, whichever crate each
// was seen in, and between two of a kind a local definition
// beats a copy observed from a downstream crate.
let upgrade = match (existing.opaque, body.opaque) {
(true, false) => true,
(false, true) => false,
_ => !existing.local && body.local,
};
if upgrade {
*existing = body;
}
return;
}
let id = FuncId::from_index(self.bodies.len());
self.by_key.insert(body.key.clone(), id);
self.bodies.push(body);
}
/// Creates opaque placeholders for callees nothing ever defined.
fn materialize_missing_callees(&mut self) {
let mut missing: Vec<(FuncKey, String)> = Vec::new();
for body in &self.bodies {
for call in &body.calls {
if let Some(key) = &call.callee
&& !self.by_key.contains_key(key)
{
missing.push((key.clone(), call.callee_display.clone()));
}
}
}
for (key, display) in missing {
if self.by_key.contains_key(&key) {
continue;
}
let krate = display
.split_once("::")
.map_or_else(|| display.clone(), |(c, _)| c.to_owned());
self.insert_body(Body::opaque(key, display, krate));
}
}
/// Builds the caller index used to drive the solver's worklist.
fn build_reverse_edges(&mut self) {
self.callers = vec![Vec::new(); self.bodies.len()];
for (i, body) in self.bodies.iter().enumerate() {
let caller = FuncId::from_index(i);
for call in &body.calls {
let Some(key) = &call.callee else { continue };
let Some(&target) = self.by_key.get(key) else {
continue;
};
let list = &mut self.callers[target.index()];
if !list.contains(&caller) {
list.push(caller);
}
}
}
}
/// The number of functions in the graph.
#[must_use]
pub const fn len(&self) -> usize {
self.bodies.len()
}
/// Returns whether the graph holds no functions.
#[must_use]
pub const fn is_empty(&self) -> bool {
self.bodies.is_empty()
}
/// The body behind an index.
#[must_use]
pub fn body(&self, id: FuncId) -> &Body {
&self.bodies[id.index()]
}
/// Every body, with its index.
pub fn iter(&self) -> impl Iterator<Item = (FuncId, &Body)> {
self.bodies
.iter()
.enumerate()
.map(|(i, b)| (FuncId::from_index(i), b))
}
/// Looks a function up by key.
#[must_use]
pub fn id_of(&self, key: &FuncKey) -> Option<FuncId> {
self.by_key.get(key).copied()
}
/// The functions that call `id`.
#[must_use]
pub fn callers(&self, id: FuncId) -> &[FuncId] {
&self.callers[id.index()]
}
/// The build configuration the artifacts were produced under.
#[must_use]
pub const fn config(&self) -> Option<&BuildConfig> {
self.config.as_ref()
}
/// Finds functions whose display path contains `needle`.
///
/// Used to turn a user supplied name into an index without requiring the
/// full mangled symbol. The closest match comes first: a path equal to
/// the needle beats one that merely contains it, and a shorter path beats
/// a longer one, so asking about `parse` explains `parse` rather than the
/// closure inside it.
#[must_use]
pub fn find_by_display(&self, needle: &str) -> Vec<FuncId> {
let mut out: Vec<FuncId> = self
.iter()
.filter(|(_, b)| b.display.contains(needle))
.map(|(id, _)| id)
.collect();
out.sort_by(|a, b| {
let (a, b) = (&self.bodies[a.index()], &self.bodies[b.index()]);
(a.display != needle, a.display.len(), &a.display).cmp(&(
b.display != needle,
b.display.len(),
&b.display,
))
});
out
}
}