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
//! Byte-neutral responsibility split of the former monolith impl; see the parent module.
use super::*;
impl Emitter {
/// When a select arm is already a pointer to scalar `pointee`, fold `indices` through
/// `source_ty` into one element offset and emit `OpPtrAccessChain %ptr_S %base %flat`.
/// Returns `None` if linearization is unsupported (caller keeps the structured path).
pub(in crate::native::emitter) fn emit_flattened_scalar_arm_access_chain(
&mut self,
ptr_type: Word,
base: Word,
source_ty: &LlType,
pointee: &LlType,
indices: &[TypedValue],
instructions: &mut Vec<Instruction>,
) -> Result<Option<Word>, String> {
let Some(flat) =
self.linearize_gep_indices_in_scalar_units(source_ty, indices, pointee, instructions)?
else {
return Ok(None);
};
let flat_id = self.value_id(&flat.value, &flat.ty)?;
let result = self.fresh();
instructions.push(Self::inst(
Op::PtrAccessChain,
Some(ptr_type),
Some(result),
vec![Operand::IdRef(base), Operand::IdRef(flat_id)],
));
Ok(Some(result))
}
/// Look up the SPIR-V result type of a previously emitted SSA id (pending block instructions
/// first, then the module). Used to detect a select arm already typed as the destination
/// scalar pointer — `pointer_pointees` can disagree after network unify.
pub(in crate::native::emitter) fn word_result_type(
&self,
id: Word,
pending: &[Instruction],
) -> Option<Word> {
for inst in pending.iter().rev() {
if inst.result_id == Some(id) {
return inst.result_type;
}
}
for function in &self.module.functions {
for inst in &function.parameters {
if inst.result_id == Some(id) {
return inst.result_type;
}
}
for block in &function.blocks {
for inst in &block.instructions {
if inst.result_id == Some(id) {
return inst.result_type;
}
}
}
}
for inst in &self.module.types_global_values {
if inst.result_id == Some(id) {
return inst.result_type;
}
}
// Globals / params may live only in the emitter side-tables until the module is finalized.
for (gid, _) in self.global_values.values() {
if *gid == id {
// Global's SPIR-V pointer type is not stored separately; fall through.
break;
}
}
None
}
/// Whether a pointer SSA id already points at a scalar (so a multi-index structured GEP would
/// over-index). Ground truth is the Word's SPIR-V `OpTypePointer` pointee when findable; falls
/// back to modeled `pointer_pointees` / `base_pointee` when the defining instruction is not
/// visible yet.
pub(in crate::native::emitter) fn pointer_id_already_at_scalar(
&self,
base_id: Word,
base_value: &LlValue,
want_scalar: &LlType,
want_ptr_type: Word,
pending: &[Instruction],
base_pointee: Option<&LlType>,
) -> bool {
if !is_scalar_pointee(want_scalar) {
return false;
}
if let Some(base_ptr_ty) = self.word_result_type(base_id, pending) {
if base_ptr_ty == want_ptr_type {
return true;
}
// SPIR-V ground truth: if the base pointer's pointee is already a scalar type, any
// multi-index structured chain over-indexes — flatten regardless of TypePointer id
// equality (interning can leave distinct Words for the same shape in edge cases).
if let Some(pointee_ty) = self.type_pointer_pointee_id(base_ptr_ty) {
if self.spirv_type_is_scalar(pointee_ty) {
return true;
}
}
}
if let Some(bp) = base_pointee {
if let Ok(bp) = self.resolve_type(bp) {
if types_compatible(&bp, want_scalar) {
return true;
}
}
}
if let LlValue::Local(name) = base_value {
if let Some(pp) = self.pointer_pointees.get(name) {
if let Ok(pp) = self.resolve_type(pp) {
if types_compatible(&pp, want_scalar) {
return true;
}
}
}
}
false
}
/// Pointee type id of an `OpTypePointer` Word, or `None` if `ptr_ty` is not a pointer type.
pub(in crate::native::emitter) fn type_pointer_pointee_id(&self, ptr_ty: Word) -> Option<Word> {
for inst in &self.module.types_global_values {
if inst.class.opcode == Op::TypePointer && inst.result_id == Some(ptr_ty) {
if let Some(Operand::IdRef(p)) = inst.operands.get(1) {
return Some(*p);
}
}
}
None
}
/// Whether a SPIR-V type id is a scalar (float/int/bool), not a composite.
pub(in crate::native::emitter) fn spirv_type_is_scalar(&self, ty: Word) -> bool {
for inst in &self.module.types_global_values {
if inst.result_id == Some(ty) {
return matches!(
inst.class.opcode,
Op::TypeFloat | Op::TypeInt | Op::TypeBool
);
}
}
false
}
/// Fold structured GEP `indices` through `source_ty` into a single offset in units of `scalar`.
/// Returns `None` when the layout cannot be linearized (dynamic struct index, size mismatch,
/// non-divisible stride) so the caller can leave the chain alone rather than miscompile.
///
/// Index list is the AIR/LLVM GEP list (leading const-0 dropped via [`gep_spirv_indices`], same
/// as the structured emit path). First remaining index strides by `sizeof(source_ty)`; further
/// indices walk members/elements.
pub(in crate::native::emitter) fn linearize_gep_indices_in_scalar_units(
&mut self,
source_ty: &LlType,
indices: &[TypedValue],
scalar: &LlType,
instructions: &mut Vec<Instruction>,
) -> Result<Option<TypedValue>, String> {
let indices = match gep_spirv_indices(indices) {
Ok(v) => v,
Err(_) => return Ok(None),
};
if indices.is_empty() {
return Ok(None);
}
let source = self.resolve_type(source_ty)?;
let scalar = self.resolve_type(scalar)?;
if !is_scalar_pointee(&scalar) {
return Ok(None);
}
let (scalar_size, _) = match self.raw_type_size_align(&scalar) {
Ok(sa) if sa.0 > 0 => sa,
_ => return Ok(None),
};
let mut total: Option<TypedValue> = None;
let mut cur = source;
for (i, idx) in indices.iter().enumerate() {
let contrib = if i == 0 {
// Leading index: element stride over the whole source type.
let (stride_bytes, _) = match self.raw_type_size_align(&cur) {
Ok(sa) => sa,
Err(_) => return Ok(None),
};
if stride_bytes % scalar_size != 0 {
return Ok(None);
}
let stride_units = (stride_bytes / scalar_size) as u32;
self.scale_gep_index(idx, stride_units, "sel.flat0", instructions)?
} else {
match &cur {
LlType::Struct(fields) => {
let Some(member_i) = const_index(Some(idx)) else {
return Ok(None);
};
let mut offset = 0u64;
let mut member_ty = None;
for (fi, field) in fields.iter().enumerate() {
let field = match self.resolve_type(field) {
Ok(t) => t,
Err(_) => return Ok(None),
};
let (fsz, falign) = match self.raw_type_size_align(&field) {
Ok(sa) => sa,
Err(_) => return Ok(None),
};
let align = falign.max(1);
offset = offset.div_ceil(align) * align;
if fi as u32 == member_i {
member_ty = Some(field);
break;
}
offset += fsz;
}
let Some(member_ty) = member_ty else {
return Ok(None);
};
if !offset.is_multiple_of(scalar_size) {
return Ok(None);
}
let units = offset / scalar_size;
cur = member_ty;
TypedValue {
ty: LlType::Int(32),
value: LlValue::Int(units),
}
}
LlType::Array(elem, _) | LlType::Vector(elem, _) => {
let elem = match self.resolve_type(elem) {
Ok(t) => t,
Err(_) => return Ok(None),
};
let (stride_bytes, _) = match self.raw_type_size_align(&elem) {
Ok(sa) => sa,
Err(_) => return Ok(None),
};
if stride_bytes % scalar_size != 0 {
return Ok(None);
}
let stride_units = (stride_bytes / scalar_size) as u32;
let scaled =
self.scale_gep_index(idx, stride_units, "sel.flati", instructions)?;
cur = elem;
scaled
}
_ => return Ok(None),
}
};
total = Some(match total {
None => contrib,
Some(acc) => self.combine_gep_indices(&acc, &contrib, instructions)?,
});
}
// After member walks (`indices.len() > 1`), the walked type must be the result scalar.
// A leading-only index list leaves `cur == source` (element stride only) — still a valid
// flat offset when the arm is already at scalar depth for a same-scalar source (rare).
if indices.len() > 1 {
let landed = match self.resolve_type(&cur) {
Ok(t) => t,
Err(_) => return Ok(None),
};
if !types_compatible(&landed, &scalar) {
return Ok(None);
}
}
Ok(total)
}
}