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
//! Byte-neutral responsibility split of the former monolith impl; see the parent module.
use super::*;
impl LlModule {
pub(in crate::native) fn infer_pointer_pointees(&mut self) {
for f in &self.functions {
let params = f
.params
.iter()
.map(|(param, _)| param.clone())
.collect::<HashSet<_>>();
let pointer_params = f
.params
.iter()
.filter(|&(_param, ty)| matches!(ty, LlType::Ptr(_)))
.map(|(param, _ty)| param.clone())
.collect::<HashSet<_>>();
let mut pointer_select_arms: HashMap<String, Vec<String>> = HashMap::new();
for inst in f.carrier_insts() {
let Some(result) = &inst.result else {
continue;
};
let Some((true_value, false_value)) = &inst.select_arms else {
continue;
};
if !matches!(true_value.ty, LlType::Ptr(_))
|| !matches!(false_value.ty, LlType::Ptr(_))
{
continue;
}
let (LlValue::Local(true_name), LlValue::Local(false_name)) =
(&true_value.value, &false_value.value)
else {
continue;
};
pointer_select_arms
.insert(result.clone(), vec![true_name.clone(), false_name.clone()]);
}
for inst in f.carrier_insts() {
if let Some(gep) = &inst.gep {
if let LlValue::Local(name) = &gep.base.value {
if params.contains(name) {
let key = (f.name.clone(), name.clone());
if self.gep_source_should_override(&key, &gep.source_ty) {
self.metadata_pointee_params.remove(&key);
self.ptr_pointees.insert(key, gep.source_ty.clone());
} else {
self.ptr_pointees
.entry(key)
.or_insert_with(|| gep.source_ty.clone());
}
}
if let Some(arms) = pointer_select_arms.get(name) {
for arm in arms {
if params.contains(arm) {
let key = (f.name.clone(), arm.clone());
if self.gep_source_should_override(&key, &gep.source_ty) {
self.metadata_pointee_params.remove(&key);
self.ptr_pointees.insert(key, gep.source_ty.clone());
} else {
self.ptr_pointees
.entry(key)
.or_insert_with(|| gep.source_ty.clone());
}
}
}
}
}
continue;
}
if let Some(load) = &inst.load {
if let LlValue::Local(name) = &load.ptr.value {
if params.contains(name) {
self.ptr_pointees
.entry((f.name.clone(), name.clone()))
.or_insert_with(|| load.result_ty.clone());
}
}
}
}
let mut sources = self.infer_local_pointer_table_param_pointees(f, &pointer_params);
let mut roots = pointer_params
.iter()
.map(|param| (param.clone(), param.clone()))
.collect::<HashMap<_, _>>();
let mut changed = true;
while changed {
changed = false;
for inst in f.carrier_insts() {
if let Some((res, base)) = &inst.identity_ptr_bitcast {
if let Some(root) = roots.get(base).cloned() {
if roots.insert(res.clone(), root).is_none() {
changed = true;
}
}
continue;
}
let Some(res) = &inst.result else {
continue;
};
if let Some(gep) = &inst.gep {
let LlValue::Local(base) = &gep.base.value else {
continue;
};
let Some(root) = roots.get(base).cloned() else {
continue;
};
sources
.entry(root.clone())
.or_default()
.insert(self.resolve_known_type(&gep.source_ty));
if roots.insert(res.clone(), root).is_none() {
changed = true;
}
continue;
}
if let Some(incoming) = &inst.phi_incoming_values {
let mut root: Option<String> = None;
for value in incoming {
let LlValue::Local(name) = value else {
continue;
};
let Some(candidate) = roots.get(name).cloned() else {
continue;
};
match &root {
Some(existing) if existing != &candidate => {
root = None;
break;
}
None => root = Some(candidate),
_ => {}
}
}
if let Some(root) = root {
if roots.insert(res.clone(), root).is_none() {
changed = true;
}
}
continue;
}
if let Some((true_value, false_value)) = &inst.select_arms {
let (LlValue::Local(true_name), LlValue::Local(false_name)) =
(&true_value.value, &false_value.value)
else {
continue;
};
let (Some(true_root), Some(false_root)) = (
roots.get(true_name).cloned(),
roots.get(false_name).cloned(),
) else {
continue;
};
if true_root == false_root && roots.insert(res.clone(), true_root).is_none()
{
changed = true;
}
}
}
}
for (param, seen) in sources {
let mut seen = seen.into_iter().collect::<Vec<_>>();
seen.sort_by_key(|ty| format!("{ty:?}"));
seen.dedup();
let [pointee] = seen.as_slice() else {
continue;
};
self.ptr_pointees
.entry((f.name.clone(), param))
.or_insert_with(|| pointee.clone());
}
}
// Propagate pointees through calls: if caller param `%p` is passed to callee param `%q`, and
// `%q` is typed by a GEP in the callee, `%p` has the same pointee.
let call_edges = self.param_call_edges();
let mut changed = true;
while changed {
changed = false;
let mut candidates: HashMap<(String, String), HashSet<LlType>> = HashMap::new();
for edge in &call_edges {
let Some(pointee) = self
.ptr_pointees
.get(&(edge.callee_func.clone(), edge.callee_param.clone()))
.cloned()
else {
continue;
};
let key = (edge.caller_func.clone(), edge.caller_param.clone());
candidates.entry(key).or_default().insert(pointee);
}
for (key, pointees) in candidates {
let pointees = pointees.into_iter().collect::<Vec<_>>();
let [pointee] = pointees.as_slice() else {
continue;
};
match self.ptr_pointees.get(&key).cloned() {
Some(existing)
if self.metadata_pointee_params.contains(&key)
&& self.metadata_pointee_can_yield_to_call(&existing, pointee) =>
{
self.ptr_pointees.insert(key.clone(), pointee.clone());
self.metadata_pointee_params.remove(&key);
changed = true;
}
Some(_) => {}
None => {
self.ptr_pointees.insert(key, pointee.clone());
changed = true;
}
}
}
}
}
/// Record a buffer param's pointee from a GEP `source_ty`. If the param currently holds a
/// metadata-seeded pointee (from `air.struct_type_info`) whose member count/ordering can
/// diverge from the LLVM aggregate the GEP indices actually address — e.g. a union/bitfield
/// tail described as overlapping members — the concrete same-size LLVM aggregate must win:
/// GEP struct indices are emitted verbatim and walked by ordinal against this type, so it has
/// to be member-isomorphic to the SPIR-V element struct. Guarded by the same byte-size
/// equality as `metadata_pointee_can_yield_to_call`, so buffer stride/extent never changes.
/// Purely structural (IR shape + size), never keyed on a name.
pub(in crate::native) fn gep_source_should_override(
&self,
key: &(String, String),
source_ty: &LlType,
) -> bool {
if !self.metadata_pointee_params.contains(key) {
return false;
}
let resolved = self.resolve_known_type(source_ty);
if !matches!(resolved, LlType::Struct(_) | LlType::Array(_, _))
|| self.type_contains_pointer(&resolved)
{
return false;
}
// Byte extent must match the buffer's declared element size (offset-aware), NOT the
// re-derived metadata `LlType::Struct` size, which overcounts when the metadata described
// a union/bitfield tail as overlapping members. Same bytes => same stride/layout, only the
// member ordinals get corrected to what the verbatim GEP indices address.
let Some(&declared) = self.metadata_pointee_sizes.get(key) else {
return false;
};
self.native_memcpy_type_size_align(&resolved)
.is_some_and(|(size, _)| size == declared)
}
pub(in crate::native) fn metadata_pointee_can_yield_to_call(
&self,
metadata_pointee: &LlType,
candidate: &LlType,
) -> bool {
let metadata_pointee = self.resolve_known_type(metadata_pointee);
let candidate = self.resolve_known_type(candidate);
if self.type_contains_pointer(&metadata_pointee) || self.type_contains_pointer(&candidate) {
return false;
}
if !matches!(metadata_pointee, LlType::Array(_, _) | LlType::Struct(_))
|| !matches!(candidate, LlType::Array(_, _) | LlType::Struct(_))
{
return false;
}
let Some((metadata_size, _)) = self.native_memcpy_type_size_align(&metadata_pointee) else {
return false;
};
let Some((candidate_size, _)) = self.native_memcpy_type_size_align(&candidate) else {
return false;
};
metadata_size == candidate_size
}
pub(in crate::native) fn infer_local_pointer_table_param_pointees(
&self,
f: &LlFunction,
pointer_params: &HashSet<String>,
) -> HashMap<String, HashSet<LlType>> {
let mut table_roots: HashMap<String, String> = HashMap::new();
let mut table_params: HashMap<String, HashSet<String>> = HashMap::new();
let mut loaded_tables: HashMap<String, String> = HashMap::new();
let mut sources: HashMap<String, HashSet<LlType>> = HashMap::new();
for inst in f.carrier_insts() {
if let Some(res) = &inst.result {
if let Some(ty) = &inst.alloca_ty {
if self.type_contains_pointer(ty) {
table_roots.insert(res.clone(), res.clone());
}
continue;
}
if let Some((bres, base)) = &inst.identity_ptr_bitcast {
if let Some(root) = table_roots.get(base).cloned() {
table_roots.insert(bres.clone(), root);
}
continue;
}
if let Some(gep) = &inst.gep {
let LlValue::Local(base) = &gep.base.value else {
continue;
};
if let Some(root) = table_roots.get(base).cloned() {
table_roots.insert(res.clone(), root);
} else if let Some(root) = loaded_tables.get(base) {
if let Some(params) = table_params.get(root) {
for param in params {
sources
.entry(param.clone())
.or_default()
.insert(self.resolve_known_type(&gep.source_ty));
}
}
}
continue;
}
if let Some(load) = &inst.load {
if !matches!(load.result_ty, LlType::Ptr(_)) {
continue;
}
let LlValue::Local(ptr_name) = &load.ptr.value else {
continue;
};
if let Some(root) = table_roots.get(ptr_name).cloned() {
loaded_tables.insert(res.clone(), root);
}
}
continue;
}
let Some((object, ptr)) = &inst.store else {
continue;
};
let LlValue::Local(param) = &object.value else {
continue;
};
if !pointer_params.contains(param) {
continue;
}
let LlValue::Local(ptr_name) = &ptr.value else {
continue;
};
if let Some(root) = table_roots.get(ptr_name) {
table_params
.entry(root.clone())
.or_default()
.insert(param.clone());
}
}
sources
}
}