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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
//! Apply interface bindings and collapse their resource wrappers into final resource values.
use super::rewrites::*;
use super::*;
use crate::passes::stage_input::{const_ivec, BufWrap, ParamBinding};
/// Apply the per-param bindings to the entry function body:
/// - remove all OpFunctionParameter,
/// - for LoadVar/Sampler params: insert an OpLoad at function entry, splice its id for the param id,
/// - for Image params: insert an OpLoad of the image at entry, splice for the param id,
/// - for Buffer params: splice the variable id for the param id, then rewrite buffer-derived
/// pointer storage classes (UniformConstant -> StorageBuffer),
/// - for WorkgroupMemory params: splice the variable id and rewrite derived pointers to Workgroup,
/// - for ZeroValue params: splice the undef id.
pub(in crate::passes) fn apply_bindings(
ctx: &mut Ctx,
entry_idx: usize,
bindings: Vec<(Word, ParamBinding)>,
buffer_structs: &[(Word, Word)],
defs: &HashMap<Word, Instruction>,
) -> Result<(), String> {
// Prepare loads to inject at the top of the entry block, and id splices to perform.
let mut loads: Vec<Instruction> = vec![];
let mut splices: Vec<(Word, Word)> = vec![];
let mut resource_values: HashSet<Word> = HashSet::new();
let mut buffer_param_ids: Vec<Word> = vec![];
// Collapsed buffers (RuntimeArray-wrapped arrays + reconstructed structs): (param id, var,
// block type, prepend_member0). Rewritten after the generic splices, since their uses need
// per-use handling (re-root chains vs route direct loads through the offset-0 leaf).
let mut collapsed_buffers: Vec<(Word, Word, Word, bool)> = vec![];
// Struct buffers with implicit record indexing need per-chain handling because some chains are
// record-0 member paths while others carry a real record index as their first operand.
let mut record_array_buffers: Vec<(Word, Word, Word, Word)> = vec![];
// Direct record-member GEPs can bridge a padded AIR struct pointer to its compact metadata
// layout. Descendant helper GEPs still carry AIR member ordinals and need one offset remap.
let mut nested_air_ordinal_roots = HashSet::new();
// Unmodeled pointer params bound to Private zero vars: their derived access chains keep the
// backend's (UniformConstant) element-pointer storage class and must be re-classed to Private.
let mut zero_pointer_vars: Vec<Word> = vec![];
// Threadgroup memory params are true Workgroup variables, not resource descriptors.
let mut workgroup_vars: Vec<Word> = vec![];
for (pid, b) in bindings {
match b {
ParamBinding::LoadVar { var, ty } => {
let lid = ctx.module.fresh_id();
loads.push(Instruction::new(
Op::Load,
Some(ty),
Some(lid),
vec![Operand::IdRef(var)],
));
splices.push((pid, lid));
}
ParamBinding::LoadVarConverted {
var,
load_ty,
param_ty,
} => {
// load the 32-bit builtin, then UConvert down to the narrower param int type.
let lid = ctx.module.fresh_id();
loads.push(Instruction::new(
Op::Load,
Some(load_ty),
Some(lid),
vec![Operand::IdRef(var)],
));
let cid = ctx.module.fresh_id();
loads.push(Instruction::new(
Op::UConvert,
Some(param_ty),
Some(cid),
vec![Operand::IdRef(lid)],
));
splices.push((pid, cid));
}
ParamBinding::LoadVarBitAnd {
var,
load_ty,
param_ty,
mask,
} => {
let lid = ctx.module.fresh_id();
loads.push(Instruction::new(
Op::Load,
Some(load_ty),
Some(lid),
vec![Operand::IdRef(var)],
));
let masked = ctx.module.fresh_id();
let mask = ctx.const_uint(mask);
loads.push(Instruction::new(
Op::BitwiseAnd,
Some(load_ty),
Some(masked),
vec![Operand::IdRef(lid), Operand::IdRef(mask)],
));
if param_ty == load_ty {
splices.push((pid, masked));
} else {
let cid = ctx.module.fresh_id();
loads.push(Instruction::new(
Op::UConvert,
Some(param_ty),
Some(cid),
vec![Operand::IdRef(masked)],
));
splices.push((pid, cid));
}
}
ParamBinding::LoadVarShiftRight {
var,
load_ty,
param_ty,
shift,
} => {
let lid = ctx.module.fresh_id();
loads.push(Instruction::new(
Op::Load,
Some(load_ty),
Some(lid),
vec![Operand::IdRef(var)],
));
let shifted = ctx.module.fresh_id();
let shift = ctx.const_uint(shift);
loads.push(Instruction::new(
Op::ShiftRightLogical,
Some(load_ty),
Some(shifted),
vec![Operand::IdRef(lid), Operand::IdRef(shift)],
));
if param_ty == load_ty {
splices.push((pid, shifted));
} else {
let cid = ctx.module.fresh_id();
loads.push(Instruction::new(
Op::UConvert,
Some(param_ty),
Some(cid),
vec![Operand::IdRef(shifted)],
));
splices.push((pid, cid));
}
}
ParamBinding::LoadVarComponent {
var,
vec_ty,
scalar_ty,
out_ty,
comp,
} => {
// load the builtin vector, then extract the wanted scalar component.
let vid = ctx.module.fresh_id();
loads.push(Instruction::new(
Op::Load,
Some(vec_ty),
Some(vid),
vec![Operand::IdRef(var)],
));
let cid = ctx.module.fresh_id();
loads.push(Instruction::new(
Op::CompositeExtract,
Some(scalar_ty),
Some(cid),
vec![Operand::IdRef(vid), Operand::LiteralBit32(comp)],
));
if scalar_ty == out_ty {
splices.push((pid, cid));
} else {
let converted = ctx.module.fresh_id();
loads.push(Instruction::new(
Op::UConvert,
Some(out_ty),
Some(converted),
vec![Operand::IdRef(cid)],
));
splices.push((pid, converted));
}
}
ParamBinding::LoadVarVectorPrefix {
var,
vec_ty,
out_ty,
lanes,
} => {
let vid = ctx.module.fresh_id();
loads.push(Instruction::new(
Op::Load,
Some(vec_ty),
Some(vid),
vec![Operand::IdRef(var)],
));
let scalar_ty = ctx.ty_uint();
let prefix_ty = ctx.ty_vec_uint(lanes);
let mut components = Vec::with_capacity(lanes as usize);
for lane in 0..lanes {
let cid = ctx.module.fresh_id();
loads.push(Instruction::new(
Op::CompositeExtract,
Some(scalar_ty),
Some(cid),
vec![Operand::IdRef(vid), Operand::LiteralBit32(lane)],
));
components.push(Operand::IdRef(cid));
}
let vector = ctx.module.fresh_id();
loads.push(Instruction::new(
Op::CompositeConstruct,
Some(prefix_ty),
Some(vector),
components,
));
if prefix_ty == out_ty {
splices.push((pid, vector));
} else {
let converted = ctx.module.fresh_id();
loads.push(Instruction::new(
Op::UConvert,
Some(out_ty),
Some(converted),
vec![Operand::IdRef(vector)],
));
splices.push((pid, converted));
}
}
ParamBinding::LoadThreadsPerGrid {
var,
vec_ty,
out_ty,
lanes,
} => {
let vid = ctx.module.fresh_id();
loads.push(Instruction::new(
Op::Load,
Some(vec_ty),
Some(vid),
vec![Operand::IdRef(var)],
));
let uint_ty = ctx.ty_uint();
let local_size = ctx.kernel_local_size;
let mut components = Vec::with_capacity(lanes as usize);
for lane in 0..lanes {
let workgroups = ctx.module.fresh_id();
loads.push(Instruction::new(
Op::CompositeExtract,
Some(uint_ty),
Some(workgroups),
vec![Operand::IdRef(vid), Operand::LiteralBit32(lane)],
));
let product = ctx.module.fresh_id();
let local = ctx.const_uint(local_size[lane as usize]);
loads.push(Instruction::new(
Op::IMul,
Some(uint_ty),
Some(product),
vec![Operand::IdRef(workgroups), Operand::IdRef(local)],
));
components.push(Operand::IdRef(product));
}
if lanes == 1 {
let product = match &components[0] {
Operand::IdRef(product) => *product,
_ => {
return Err(
"apply_bindings: dispatch-index component is not an id".to_string()
)
}
};
if out_ty == uint_ty {
splices.push((pid, product));
} else {
let converted = ctx.module.fresh_id();
loads.push(Instruction::new(
Op::UConvert,
Some(out_ty),
Some(converted),
vec![Operand::IdRef(product)],
));
splices.push((pid, converted));
}
} else {
let prefix_ty = ctx.ty_vec_uint(lanes);
let vector = ctx.module.fresh_id();
loads.push(Instruction::new(
Op::CompositeConstruct,
Some(prefix_ty),
Some(vector),
components,
));
if prefix_ty == out_ty {
splices.push((pid, vector));
} else {
let converted = ctx.module.fresh_id();
loads.push(Instruction::new(
Op::UConvert,
Some(out_ty),
Some(converted),
vec![Operand::IdRef(vector)],
));
splices.push((pid, converted));
}
}
}
ParamBinding::Sampler { var } => {
let sty = ctx.ty_sampler();
let lid = ctx.module.fresh_id();
loads.push(Instruction::new(
Op::Load,
Some(sty),
Some(lid),
vec![Operand::IdRef(var)],
));
resource_values.insert(lid);
splices.push((pid, lid));
}
ParamBinding::Image {
var,
image_ty,
dim,
comp,
} => {
let lid = ctx.module.fresh_id();
loads.push(Instruction::new(
Op::Load,
Some(image_ty),
Some(lid),
vec![Operand::IdRef(var)],
));
ctx.image_dims.insert(lid, dim);
ctx.image_comp.insert(lid, comp);
resource_values.insert(lid);
splices.push((pid, lid));
}
ParamBinding::ImageArray {
var,
elem_image_ty,
dim,
comp,
} => {
// Splice the param to the array VARIABLE (a pointer to `array<image>`); the per-element
// image is materialized at each use by `materialize_texture_array_loads` reading
// `image_array_vars`. No function-top load: the array as a whole is never an operand.
ctx.image_array_vars.insert(var, (elem_image_ty, dim, comp));
resource_values.insert(var);
splices.push((pid, var));
}
ParamBinding::StorageImage {
var,
image_ty,
dim,
comp,
} => {
let lid = ctx.module.fresh_id();
loads.push(Instruction::new(
Op::Load,
Some(image_ty),
Some(lid),
vec![Operand::IdRef(var)],
));
ctx.image_dims.insert(lid, dim);
ctx.image_comp.insert(lid, comp);
ctx.image_storage.insert(lid);
resource_values.insert(lid);
splices.push((pid, lid));
}
ParamBinding::InputAttachment {
var,
image_ty,
read_ty,
param_ty,
} => {
let image = ctx.module.fresh_id();
loads.push(Instruction::new(
Op::Load,
Some(image_ty),
Some(image),
vec![Operand::IdRef(var)],
));
let coord_ty = ctx.ty_vec_sint(2);
let coord = const_ivec(ctx, coord_ty, &[0, 0]);
let read = ctx.module.fresh_id();
loads.push(Instruction::new(
Op::ImageRead,
Some(read_ty),
Some(read),
vec![Operand::IdRef(image), Operand::IdRef(coord)],
));
if read_ty == param_ty {
splices.push((pid, read));
} else {
let converted = ctx.module.fresh_id();
loads.push(Instruction::new(
Op::FConvert,
Some(param_ty),
Some(converted),
vec![Operand::IdRef(read)],
));
splices.push((pid, converted));
}
}
ParamBinding::Buffer { var, wrap } => match wrap {
BufWrap::Direct => {
// struct buffer: the body's access chains index into the struct off the var.
splices.push((pid, var));
buffer_param_ids.push(var);
}
BufWrap::Collapsed {
block_ty,
prepend_member0,
} => {
// re-rooted / leaf-routed after splices (see below).
collapsed_buffers.push((pid, var, block_ty, prepend_member0));
}
BufWrap::RecordArray { block_ty, elem_ty } => {
record_array_buffers.push((pid, var, block_ty, elem_ty));
}
},
ParamBinding::WorkgroupMemory { var } => {
splices.push((pid, var));
workgroup_vars.push(var);
}
ParamBinding::Value { val } => {
splices.push((pid, val));
}
ParamBinding::ZeroValue { val } => {
splices.push((pid, val));
}
ParamBinding::ZeroPointer { var } => {
splices.push((pid, var));
zero_pointer_vars.push(var);
}
}
}
// Remove parameters.
ctx.module.functions[entry_idx].parameters.clear();
// Inject loads at the top of the first block, but AFTER any leading function-local OpVariables:
// SPIR-V requires every OpVariable in a function to be among the first instructions of the entry
// block, so the load (a non-Variable instruction) must follow them. Order among the loads is
// preserved.
if !loads.is_empty() {
let func = &mut ctx.module.functions[entry_idx];
if let Some(first) = func.blocks.first_mut() {
let at = first
.instructions
.iter()
.position(|i| i.class.opcode != Op::Variable)
.unwrap_or(first.instructions.len());
for (k, ld) in loads.into_iter().enumerate() {
first.instructions.insert(at + k, ld);
}
}
}
// Keep typed pointer-field store provenance in step with the interface splice so
// cross-function helper-field recovery can replay texture handles after entry parameters become
// loaded image ids.
ctx.emit_sidecar
.remap_local_pointer_field_store_sources(&splices.iter().copied().collect());
// Splice param ids -> their replacement ids.
{
let func = &mut ctx.module.functions[entry_idx];
for (from, to) in splices {
replace_id_in_function(func, from, to);
}
}
collapse_resource_wrappers(ctx, entry_idx, &resource_values);
// Rewrite collapsed-buffer uses. Done after the generic splices (these param ids were deliberately
// NOT spliced) so each use can be handled by kind.
for (pid, var, block_ty, prepend_member0) in collapsed_buffers {
rewrite_collapsed_buffer(ctx, entry_idx, pid, var, block_ty, prepend_member0, defs);
}
for (pid, var, block_ty, elem_ty) in record_array_buffers {
nested_air_ordinal_roots.extend(rewrite_record_array_buffer(
ctx, entry_idx, pid, var, block_ty, elem_ty, defs,
));
}
if !nested_air_ordinal_roots.is_empty() {
remap_nested_air_struct_accesses(ctx, entry_idx, &nested_air_ordinal_roots, defs);
}
// Rewrite buffer-derived pointer storage classes to StorageBuffer. The buffer var is already
// StorageBuffer-typed; the OpAccessChain results that walk into it keep the backend's
// UniformConstant element-pointer types, which Vulkan rejects. Clone those pointer types into
// StorageBuffer and remap. Root from EVERY buffer variable (struct + synthesized-wrapper) so
// both direct uses and the spliced member-0 chains are covered.
let _ = &buffer_param_ids;
if !buffer_structs.is_empty() {
let roots: Vec<Word> = buffer_structs.iter().map(|(v, _)| *v).collect();
rewrite_pointer_storage(ctx, entry_idx, &roots, StorageClass::StorageBuffer, defs)?;
rewrite_raw_word_alias_chains(ctx, entry_idx, buffer_structs, defs)?;
}
// Re-class the access chains derived from unmodeled-pointer Private zero vars (same problem as the
// buffer chains, but the var/leaf type is Private not StorageBuffer).
if !zero_pointer_vars.is_empty() {
rewrite_pointer_storage(
ctx,
entry_idx,
&zero_pointer_vars,
StorageClass::Private,
defs,
)?;
// A function-constant-gated atomic buffer (e.g. an MPS reduce `groupid_counter`) becomes one
// of these absent Private zero vars; SPIR-V forbids OpAtomic* on Private storage. Private
// memory is per-invocation, so the atomic is semantically a plain load/op/store — rewrite it.
rewrite_private_pointer_atomics(ctx, entry_idx);
}
if !workgroup_vars.is_empty() {
for var in &workgroup_vars {
rewrite_workgroup_root_access(ctx, entry_idx, *var, defs);
}
rewrite_pointer_storage(
ctx,
entry_idx,
&workgroup_vars,
StorageClass::Workgroup,
defs,
)?;
rewrite_flattened_workgroup_leaf_accesses(ctx, entry_idx, &workgroup_vars, defs);
}
rewrite_structural_load_result_types(ctx, entry_idx, defs);
rewrite_ulong_uint2_memory_reinterprets(ctx, entry_idx, defs);
Ok(())
}
pub(in crate::passes) fn rewrite_ulong_uint2_memory_reinterprets(
ctx: &mut Ctx,
entry_idx: usize,
defs: &HashMap<Word, Instruction>,
) {
let types = combined_type_defs(ctx, defs);
let mut value_types = combined_value_types(ctx, entry_idx);
let uint = ctx.ty_uint();
let ulong = ctx.ty_ulong();
let v2uint = ctx.ty_vec_uint(2);
let shift = ctx.const_uint(32);
let block_count = ctx.module.functions[entry_idx].blocks.len();
for block_idx in 0..block_count {
let instructions =
std::mem::take(&mut ctx.module.functions[entry_idx].blocks[block_idx].instructions);
let mut rewritten = Vec::with_capacity(instructions.len());
for mut inst in instructions {
match inst.class.opcode {
Op::Load if inst.result_type == Some(v2uint) => {
let Some(result) = inst.result_id else {
rewritten.push(inst);
continue;
};
let Some(Operand::IdRef(ptr)) = inst.operands.first() else {
rewritten.push(inst);
continue;
};
if pointer_value_pointee(&types, &value_types, *ptr) != Some(ulong) {
rewritten.push(inst);
continue;
}
let loaded = ctx.module.fresh_id();
inst.result_type = Some(ulong);
inst.result_id = Some(loaded);
rewritten.push(inst);
let low = ctx.module.fresh_id();
rewritten.push(Instruction::new(
Op::UConvert,
Some(uint),
Some(low),
vec![Operand::IdRef(loaded)],
));
let shifted = ctx.module.fresh_id();
rewritten.push(Instruction::new(
Op::ShiftRightLogical,
Some(ulong),
Some(shifted),
vec![Operand::IdRef(loaded), Operand::IdRef(shift)],
));
let high = ctx.module.fresh_id();
rewritten.push(Instruction::new(
Op::UConvert,
Some(uint),
Some(high),
vec![Operand::IdRef(shifted)],
));
rewritten.push(Instruction::new(
Op::CompositeConstruct,
Some(v2uint),
Some(result),
vec![Operand::IdRef(low), Operand::IdRef(high)],
));
value_types.insert(loaded, ulong);
value_types.insert(low, uint);
value_types.insert(shifted, ulong);
value_types.insert(high, uint);
value_types.insert(result, v2uint);
}
Op::Store => {
let (Some(Operand::IdRef(ptr)), Some(Operand::IdRef(object))) =
(inst.operands.first(), inst.operands.get(1))
else {
rewritten.push(inst);
continue;
};
if pointer_value_pointee(&types, &value_types, *ptr) != Some(ulong)
|| value_types.get(object).copied() != Some(v2uint)
{
rewritten.push(inst);
continue;
}
let low = ctx.module.fresh_id();
rewritten.push(Instruction::new(
Op::CompositeExtract,
Some(uint),
Some(low),
vec![Operand::IdRef(*object), Operand::LiteralBit32(0)],
));
let high = ctx.module.fresh_id();
rewritten.push(Instruction::new(
Op::CompositeExtract,
Some(uint),
Some(high),
vec![Operand::IdRef(*object), Operand::LiteralBit32(1)],
));
let low64 = ctx.module.fresh_id();
rewritten.push(Instruction::new(
Op::UConvert,
Some(ulong),
Some(low64),
vec![Operand::IdRef(low)],
));
let high64 = ctx.module.fresh_id();
rewritten.push(Instruction::new(
Op::UConvert,
Some(ulong),
Some(high64),
vec![Operand::IdRef(high)],
));
let shifted = ctx.module.fresh_id();
rewritten.push(Instruction::new(
Op::ShiftLeftLogical,
Some(ulong),
Some(shifted),
vec![Operand::IdRef(high64), Operand::IdRef(shift)],
));
let joined = ctx.module.fresh_id();
rewritten.push(Instruction::new(
Op::BitwiseOr,
Some(ulong),
Some(joined),
vec![Operand::IdRef(low64), Operand::IdRef(shifted)],
));
inst.operands[1] = Operand::IdRef(joined);
rewritten.push(inst);
value_types.insert(low, uint);
value_types.insert(high, uint);
value_types.insert(low64, ulong);
value_types.insert(high64, ulong);
value_types.insert(shifted, ulong);
value_types.insert(joined, ulong);
}
_ => rewritten.push(inst),
}
}
ctx.module.functions[entry_idx].blocks[block_idx].instructions = rewritten;
}
}
pub(in crate::passes) fn pointer_value_pointee(
types: &HashMap<Word, Instruction>,
value_types: &HashMap<Word, Word>,
pointer: Word,
) -> Option<Word> {
value_types
.get(&pointer)
.and_then(|ptr_ty| ptr_pointee(types, *ptr_ty))
}
pub(in crate::passes) fn collapse_resource_wrappers(
ctx: &mut Ctx,
entry_idx: usize,
resource_values: &HashSet<Word>,
) {
if resource_values.is_empty() {
return;
}
let func = &ctx.module.functions[entry_idx];
let mut paths: HashMap<Word, HashMap<Vec<u32>, Word>> = HashMap::new();
let mut replacements: Vec<(Word, Word)> = vec![];
for blk in &func.blocks {
for inst in &blk.instructions {
let Some(result) = inst.result_id else {
continue;
};
match inst.class.opcode {
Op::CompositeInsert => {
let Some(Operand::IdRef(object)) = inst.operands.first() else {
continue;
};
let Some(Operand::IdRef(base)) = inst.operands.get(1) else {
continue;
};
let path = literal_path(&inst.operands[2..]);
let mut result_paths = paths.get(base).cloned().unwrap_or_default();
insert_resource_path(
&mut result_paths,
&path,
*object,
&paths,
resource_values,
);
if !result_paths.is_empty() {
paths.insert(result, result_paths);
}
}
Op::CompositeConstruct => {
let mut result_paths = HashMap::new();
for (idx, operand) in inst.operands.iter().enumerate() {
let Operand::IdRef(object) = operand else {
continue;
};
insert_resource_path(
&mut result_paths,
&[idx as u32],
*object,
&paths,
resource_values,
);
}
if !result_paths.is_empty() {
paths.insert(result, result_paths);
}
}
Op::CopyObject => {
let Some(Operand::IdRef(source)) = inst.operands.first() else {
continue;
};
if let Some(source_paths) = paths.get(source).cloned() {
paths.insert(result, source_paths);
} else if resource_values.contains(source) {
let mut result_paths = HashMap::new();
result_paths.insert(vec![], *source);
paths.insert(result, result_paths);
}
}
Op::CompositeExtract => {
let Some(Operand::IdRef(composite)) = inst.operands.first() else {
continue;
};
let path = literal_path(&inst.operands[1..]);
let Some(composite_paths) = paths.get(composite) else {
continue;
};
if let Some(resource) = composite_paths.get(&path).copied() {
replacements.push((result, resource));
continue;
}
let mut result_paths = HashMap::new();
for (resource_path, resource) in composite_paths {
if let Some(suffix) = path_suffix(resource_path, &path) {
result_paths.insert(suffix.to_vec(), *resource);
}
}
if !result_paths.is_empty() {
paths.insert(result, result_paths);
}
}
_ => {}
}
}
}
if replacements.is_empty() {
return;
}
{
let func = &mut ctx.module.functions[entry_idx];
for (from, to) in replacements {
replace_id_in_function(func, from, to);
}
}
remove_dead_resource_wrapper_ops(ctx, entry_idx);
}
pub(in crate::passes) fn insert_resource_path(
out: &mut HashMap<Vec<u32>, Word>,
prefix: &[u32],
value: Word,
paths: &HashMap<Word, HashMap<Vec<u32>, Word>>,
resource_values: &HashSet<Word>,
) {
if resource_values.contains(&value) {
out.insert(prefix.to_vec(), value);
}
if let Some(value_paths) = paths.get(&value) {
for (suffix, resource) in value_paths {
let mut full = prefix.to_vec();
full.extend(suffix);
out.insert(full, *resource);
}
}
}
pub(in crate::passes) fn literal_path(operands: &[Operand]) -> Vec<u32> {
operands
.iter()
.filter_map(|operand| match operand {
Operand::LiteralBit32(value) => Some(*value),
_ => None,
})
.collect()
}
pub(in crate::passes) fn path_suffix<'a>(path: &'a [u32], prefix: &[u32]) -> Option<&'a [u32]> {
if path.len() < prefix.len() || &path[..prefix.len()] != prefix {
return None;
}
Some(&path[prefix.len()..])
}
pub(in crate::passes) fn remove_dead_resource_wrapper_ops(ctx: &mut Ctx, entry_idx: usize) {
loop {
let used = function_used_ids(&ctx.module.functions[entry_idx]);
let mut changed = false;
for blk in &mut ctx.module.functions[entry_idx].blocks {
let before = blk.instructions.len();
blk.instructions.retain(|inst| {
let dead = inst
.result_id
.map(|id| !used.contains(&id))
.unwrap_or(false);
!(dead
&& matches!(
inst.class.opcode,
Op::CompositeInsert
| Op::CompositeExtract
| Op::CompositeConstruct
| Op::CopyObject
| Op::Undef
))
});
changed |= blk.instructions.len() != before;
}
if !changed {
break;
}
}
}
pub(in crate::passes) fn function_used_ids(func: &Function) -> HashSet<Word> {
let mut used = HashSet::new();
for param in &func.parameters {
for operand in ¶m.operands {
collect_operand_id_refs(operand, &mut used);
}
}
for blk in &func.blocks {
if let Some(label) = &blk.label {
for operand in &label.operands {
collect_operand_id_refs(operand, &mut used);
}
}
for inst in &blk.instructions {
for operand in &inst.operands {
collect_operand_id_refs(operand, &mut used);
}
}
}
used
}
pub(in crate::passes) fn collect_operand_id_refs(operand: &Operand, used: &mut HashSet<Word>) {
if let Operand::IdRef(id) = operand {
used.insert(*id);
}
}