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
//! 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 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, and a local definition
// beats a copy observed from a downstream crate.
let upgrade = (existing.opaque && !body.opaque)
|| (!existing.local && body.local);
if upgrade {
*existing = body;
}
return;
}
let id = FuncId(u32::try_from(self.bodies.len()).unwrap_or(u32::MAX));
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(u32::try_from(i).unwrap_or(u32::MAX));
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(u32::try_from(i).unwrap_or(u32::MAX)), 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.
#[must_use]
pub fn find_by_display(&self, needle: &str) -> Vec<FuncId> {
self.iter()
.filter(|(_, b)| b.display.contains(needle))
.map(|(id, _)| id)
.collect()
}
}