metal2vulkan 0.1.0

Translate Metal AIR / LLVM IR to Vulkan SPIR-V with a native Rust emitter
Documentation
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
//! In-memory Function sub-word packed-scalar reinterpret remodel.
//!
//! Metal's `as_type<uint>(half2(a, b))` pack idiom (and its `uchar4`/`ushort2` siblings) compiles to a
//! scalar `i32` alloca whose bytes are written through SMALLER-element geps — e.g.
//! `getelementptr inbounds half, ptr %slot, i64 0/1` + `store half` — and then read back whole as
//! `load i32`. The native emitter declares the alloca's `OpVariable` from its `i32` pointee but takes the
//! access-chain result pointer type straight from the gep element type, emitting
//! `OpInBoundsAccessChain %_ptr_Function_half %slot %uint_0` — indexing a SCALAR variable, which
//! spirv-val rejects: *"reached non-composite type while indexes still remain to be traversed"*. The
//! existing same-size alloca reinterpret (`ir::infer_local_alloca_pointees`) does not cover a sub-word
//! element access (the element is narrower than the alloca scalar), so the module ships invalid.
//!
//! The honest fix is to RETYPE the variable's pointee from the scalar integer `T` (width `W`) to a vector
//! `<N x E>` of the sub-word element `E` (width `w`, `N = W / w`): the sub-element access chains then
//! index a vector component (legal in Function storage) unchanged, and the whole-word `OpLoad %T` /
//! `OpStore` reinterpret via a VALUE `OpBitcast` between `<N x E>` and `T` (a legal numeric reinterpret,
//! both `W` bits wide).
//!
//! This is byte-SAFE by construction: a Function alloca is per-invocation scratch (never device-visible,
//! never the golden's output), and `<N x E>` shares `T`'s little-endian byte layout exactly — component 0
//! occupies the lowest-address `w` bits, identical to gep element index 0, so every store/load lands on
//! the same bytes the packed scalar did. The retry clones the canonical pre-primary `Module`, mutates
//! it in place, and adopts it only if it validates, so it is floor-safe by construction. It decides
//! purely from IR structure (a Function scalar-integer variable whose only access chains use one
//! smaller scalar element type) — never a shader name.

use crate::spirv_module::Instruction;
use crate::spirv_module::Module;
use crate::spirv_module::Operand;
use spirv::{Op, StorageClass, Word};
use std::collections::HashMap;

/// Retype every Function scalar-integer variable that is accessed ONLY as the sub-word packed-scalar
/// idiom so its pointee becomes a `<N x E>` vector of the sub-word element, dropping the illegal
/// scalar-indexing access chains' invalidity and value-bitcasting its whole-word loads/stores. Returns
/// true if any variable was remodeled.
pub(super) fn rewrite_subword_packed_scalars(module: &mut Module) -> bool {
    // id -> (storage class, pointee) for every pointer type; id -> type def for every type.
    let mut ptr_info: HashMap<Word, (StorageClass, Word)> = HashMap::new();
    let mut type_defs: HashMap<Word, Instruction> = HashMap::new();
    for inst in &module.types_global_values {
        if let Some(result) = inst.result_id {
            type_defs.insert(result, inst.clone());
        }
        if inst.class.opcode == Op::TypePointer {
            if let (Some(id), Some(Operand::StorageClass(s)), Some(Operand::IdRef(p))) =
                (inst.result_id, inst.operands.first(), inst.operands.get(1))
            {
                ptr_info.insert(id, (*s, *p));
            }
        }
    }

    // Candidate Function variables whose pointee is a scalar integer type (the packed whole word).
    // Function-storage `OpVariable`s live in the function's entry block, NOT the module-scope
    // types_global_values (only Private/Workgroup/UniformConstant vars are module scope).
    let mut cands: Vec<(Word, Word, u32)> = Vec::new(); // (var, scalar_int_ty, width)
    for func in &module.functions {
        for block in &func.blocks {
            for inst in &block.instructions {
                if inst.class.opcode != Op::Variable {
                    continue;
                }
                // An initializer (operand beyond the storage class) would not survive the retype — skip.
                if inst.operands.len() != 1
                    || inst.operands.first() != Some(&Operand::StorageClass(StorageClass::Function))
                {
                    continue;
                }
                let (Some(var), Some(ptr_ty)) = (inst.result_id, inst.result_type) else {
                    continue;
                };
                let Some(&(StorageClass::Function, pointee)) = ptr_info.get(&ptr_ty) else {
                    continue;
                };
                if let Some(width) = scalar_int_width(&type_defs, pointee) {
                    cands.push((var, pointee, width));
                }
            }
        }
    }
    if cands.is_empty() {
        return false;
    }

    let mut next_id = module.header.as_ref().map(|h| h.bound).unwrap_or(0);
    let mut changed = false;
    for (var, scalar_ty, width) in cands {
        if remodel_one(module, var, scalar_ty, width, &mut next_id) {
            changed = true;
        }
    }
    if changed {
        if let Some(header) = module.header.as_mut() {
            header.bound = next_id;
        }
    }
    changed
}

/// Width in bits of a scalar integer type id, or None if `ty` is not an `OpTypeInt`.
fn scalar_int_width(defs: &HashMap<Word, Instruction>, ty: Word) -> Option<u32> {
    let def = defs.get(&ty)?;
    if def.class.opcode != Op::TypeInt {
        return None;
    }
    match def.operands.first()? {
        Operand::LiteralBit32(bits) => Some(*bits),
        _ => None,
    }
}

/// Width in bits of a scalar numeric (int or float) type id, or None otherwise.
fn scalar_numeric_width(defs: &HashMap<Word, Instruction>, ty: Word) -> Option<u32> {
    let def = defs.get(&ty)?;
    match def.class.opcode {
        Op::TypeInt | Op::TypeFloat => match def.operands.first()? {
            Operand::LiteralBit32(bits) => Some(*bits),
            _ => None,
        },
        _ => None,
    }
}

struct Plan {
    /// The sub-word element type `E` every access chain agrees on.
    elem_ty: Word,
    /// Number of `E` lanes packed in the scalar word (`W / w`).
    lanes: u32,
    /// Whole-word `OpLoad %T %var` result ids (rewritten: load the vector, value-bitcast to `T`).
    word_loads: Vec<Word>,
    /// Whole-word `OpStore %var %val` — pointer is operand 0 (rewritten: bitcast `val` to vec, store).
    /// Identified at rewrite time by `operand 0 == var`; nothing to collect here beyond the var.
    has_word_store: bool,
}

/// Validate `var` (scalar word type `scalar_ty`, width `width`) is reached only as the sub-word pack
/// idiom and, if so, retype it to a `<lanes x elem>` vector in place. Returns true if remodeled. On any
/// unmodeled use the variable is left untouched.
fn remodel_one(
    module: &mut Module,
    var: Word,
    scalar_ty: Word,
    width: u32,
    next_id: &mut Word,
) -> bool {
    let type_defs: HashMap<Word, Instruction> = module
        .types_global_values
        .iter()
        .filter_map(|i| i.result_id.map(|id| (id, i.clone())))
        .collect();
    let ptr_info: HashMap<Word, (StorageClass, Word)> = module
        .types_global_values
        .iter()
        .filter(|i| i.class.opcode == Op::TypePointer)
        .filter_map(|i| {
            let id = i.result_id?;
            match (i.operands.first()?, i.operands.get(1)?) {
                (Operand::StorageClass(s), Operand::IdRef(p)) => Some((id, (*s, *p))),
                _ => None,
            }
        })
        .collect();

    let Some(plan) = validate(module, var, scalar_ty, width, &type_defs, &ptr_info) else {
        return false;
    };

    let mut fresh = || {
        let id = *next_id;
        *next_id += 1;
        id
    };

    // The `<lanes x elem>` vector type, and a Function pointer to it. Reuse existing defs when present,
    // else synthesize and append to the module-scope type section. Appending at the end is define-
    // before-use safe: the vector references the pre-existing element scalar, the pointer references the
    // vector just before it, and the only user (the function-body variable) follows all types.
    let vec_ty = find_vector(module, plan.elem_ty, plan.lanes).unwrap_or_else(|| {
        let id = fresh();
        module.types_global_values.push(Instruction::new(
            Op::TypeVector,
            None,
            Some(id),
            vec![
                Operand::IdRef(plan.elem_ty),
                Operand::LiteralBit32(plan.lanes),
            ],
        ));
        id
    });
    let vec_ptr = find_ptr(module, StorageClass::Function, vec_ty).unwrap_or_else(|| {
        let id = fresh();
        module.types_global_values.push(Instruction::new(
            Op::TypePointer,
            None,
            Some(id),
            vec![
                Operand::StorageClass(StorageClass::Function),
                Operand::IdRef(vec_ty),
            ],
        ));
        id
    });

    // Repoint the function-body variable instruction to the vector pointer.
    let mut found = false;
    for func in module.functions.iter_mut() {
        for block in func.blocks.iter_mut() {
            for inst in block.instructions.iter_mut() {
                if inst.class.opcode == Op::Variable && inst.result_id == Some(var) {
                    inst.result_type = Some(vec_ptr);
                    found = true;
                }
            }
        }
    }
    if !found {
        return false;
    }

    // Rewrite the function bodies: value-bitcast every whole-word load/store of the variable between the
    // vector view and the scalar word. The sub-element access chains stay as-is (now valid, since they
    // index a vector component instead of a scalar).
    rewrite_bodies(module, var, scalar_ty, vec_ty, &plan, next_id);
    true
}

fn validate(
    module: &Module,
    var: Word,
    scalar_ty: Word,
    width: u32,
    type_defs: &HashMap<Word, Instruction>,
    ptr_info: &HashMap<Word, (StorageClass, Word)>,
) -> Option<Plan> {
    let mut elem_ty: Option<Word> = None;
    let mut word_loads: Vec<Word> = Vec::new();
    let mut has_word_store = false;
    let mut saw_subword_chain = false;

    for func in &module.functions {
        for block in &func.blocks {
            for inst in &block.instructions {
                // Examine every use of the variable as an id operand.
                for (oi, op) in inst.operands.iter().enumerate() {
                    if !matches!(op, Operand::IdRef(id) if *id == var) {
                        continue;
                    }
                    match inst.class.opcode {
                        // A sub-word access chain: base (operand 0) is the variable, exactly one index,
                        // result a Function pointer to a smaller scalar element. All such chains must
                        // agree on the same element type.
                        Op::InBoundsAccessChain | Op::AccessChain if oi == 0 => {
                            // Exactly one index operand (a scalar word has no nested structure).
                            if inst.operands.len() != 2 {
                                return None;
                            }
                            let result_ty = inst.result_type?;
                            let &(sc, pointee) = ptr_info.get(&result_ty)?;
                            if sc != StorageClass::Function {
                                return None;
                            }
                            let ew = scalar_numeric_width(type_defs, pointee)?;
                            if ew >= width || !width.is_multiple_of(ew) {
                                return None;
                            }
                            match elem_ty {
                                Some(t) if t != pointee => return None,
                                _ => elem_ty = Some(pointee),
                            }
                            saw_subword_chain = true;
                        }
                        // Whole-word load: the variable is the pointer (operand 0), result is the word.
                        Op::Load if oi == 0 => {
                            if inst.result_type != Some(scalar_ty) {
                                return None;
                            }
                            word_loads.push(inst.result_id?);
                        }
                        // Whole-word store: the variable is the pointer (operand 0).
                        Op::Store if oi == 0 => {
                            has_word_store = true;
                        }
                        // Any other mention of the variable disqualifies it (do not miscompile).
                        _ => return None,
                    }
                }
            }
        }
    }

    let elem_ty = elem_ty?;
    if !saw_subword_chain {
        return None;
    }
    let ew = scalar_numeric_width(type_defs, elem_ty)?;
    let lanes = width / ew;
    // Vulkan vectors are 2..=4 components; a wider pack would need Vector16 — leave it to the
    // adopt-if-validates gate by simply declining here.
    if !(2..=4).contains(&lanes) {
        return None;
    }
    Some(Plan {
        elem_ty,
        lanes,
        word_loads,
        has_word_store,
    })
}

fn rewrite_bodies(
    module: &mut Module,
    var: Word,
    scalar_ty: Word,
    vec_ty: Word,
    plan: &Plan,
    next_id: &mut Word,
) {
    for func in module.functions.iter_mut() {
        for block in func.blocks.iter_mut() {
            let insts = std::mem::take(&mut block.instructions);
            let mut out = Vec::with_capacity(insts.len());
            for inst in insts {
                // Whole-word load: load the vector, then bitcast the value to the scalar word (keeping the
                // original load's result id for downstream uses).
                if inst.class.opcode == Op::Load
                    && inst
                        .result_id
                        .map(|r| plan.word_loads.contains(&r))
                        .unwrap_or(false)
                    && operand_id(&inst, 0) == Some(var)
                {
                    let rid = inst.result_id.unwrap();
                    let tmp = *next_id;
                    *next_id += 1;
                    out.push(Instruction::new(
                        Op::Load,
                        Some(vec_ty),
                        Some(tmp),
                        vec![Operand::IdRef(var)],
                    ));
                    out.push(Instruction::new(
                        Op::Bitcast,
                        Some(scalar_ty),
                        Some(rid),
                        vec![Operand::IdRef(tmp)],
                    ));
                    continue;
                }
                // Whole-word store: bitcast the scalar object to the vector view, then store.
                if plan.has_word_store
                    && inst.class.opcode == Op::Store
                    && operand_id(&inst, 0) == Some(var)
                {
                    let val = operand_id(&inst, 1).unwrap();
                    let tmp = *next_id;
                    *next_id += 1;
                    out.push(Instruction::new(
                        Op::Bitcast,
                        Some(vec_ty),
                        Some(tmp),
                        vec![Operand::IdRef(val)],
                    ));
                    out.push(Instruction::new(
                        Op::Store,
                        None,
                        None,
                        vec![Operand::IdRef(var), Operand::IdRef(tmp)],
                    ));
                    continue;
                }
                out.push(inst);
            }
            block.instructions = out;
        }
    }
}

fn find_vector(module: &Module, component: Word, count: u32) -> Option<Word> {
    module.types_global_values.iter().find_map(|i| {
        (i.class.opcode == Op::TypeVector
            && i.operands.first() == Some(&Operand::IdRef(component))
            && i.operands.get(1) == Some(&Operand::LiteralBit32(count)))
        .then_some(i.result_id)
        .flatten()
    })
}

fn find_ptr(module: &Module, sc: StorageClass, pointee: Word) -> Option<Word> {
    module.types_global_values.iter().find_map(|i| {
        (i.class.opcode == Op::TypePointer
            && i.operands.first() == Some(&Operand::StorageClass(sc))
            && i.operands.get(1) == Some(&Operand::IdRef(pointee)))
        .then_some(i.result_id)
        .flatten()
    })
}

fn operand_id(inst: &Instruction, idx: usize) -> Option<Word> {
    match inst.operands.get(idx) {
        Some(Operand::IdRef(id)) => Some(*id),
        _ => None,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::spirv_module::{Block, Function, ModuleHeader};

    fn i(op: Op, ty: Option<Word>, res: Option<Word>, ops: Vec<Operand>) -> Instruction {
        Instruction::new(op, ty, res, ops)
    }

    // A `uint` Function alloca written as two packed `half` lanes (index 0/1) then read whole as `uint`
    // (the `as_type<uint>(half2)` idiom). The remodel must retype the variable to `<2 x half>`, leave the
    // half access chains untouched, and rewrite the whole-word `OpLoad %uint` into `OpLoad %v2half` +
    // `OpBitcast %uint`. Byte-safe (Function scratch, 32-bit reinterpret, same byte offsets).
    #[test]
    fn half2_packed_into_uint_remodels_to_vector() {
        // ids: uint=1 half=2 v2half=3(synth) | uint_0=10 uint_1=11 | ptrF_uint=4 ptrF_half=5
        //      var=20 | entry=30 g0=31 g1=32 hv0=33 hv1=34 load=35
        let mut m = Module::new();
        m.header = Some(ModuleHeader::new(40));
        m.types_global_values = vec![
            i(
                Op::TypeInt,
                None,
                Some(1),
                vec![Operand::LiteralBit32(32), Operand::LiteralBit32(0)],
            ),
            i(
                Op::TypeFloat,
                None,
                Some(2),
                vec![Operand::LiteralBit32(16)],
            ),
            i(
                Op::TypePointer,
                None,
                Some(4),
                vec![
                    Operand::StorageClass(StorageClass::Function),
                    Operand::IdRef(1),
                ],
            ),
            i(
                Op::TypePointer,
                None,
                Some(5),
                vec![
                    Operand::StorageClass(StorageClass::Function),
                    Operand::IdRef(2),
                ],
            ),
            i(
                Op::Constant,
                Some(1),
                Some(10),
                vec![Operand::LiteralBit32(0)],
            ),
            i(
                Op::Constant,
                Some(1),
                Some(11),
                vec![Operand::LiteralBit32(1)],
            ),
        ];
        let mut block = Block::new();
        block.label = Some(i(Op::Label, None, Some(30), vec![]));
        block.instructions = vec![
            // The Function-storage variable lives in the function's entry block.
            i(
                Op::Variable,
                Some(4),
                Some(20),
                vec![Operand::StorageClass(StorageClass::Function)],
            ),
            i(
                Op::InBoundsAccessChain,
                Some(5),
                Some(31),
                vec![Operand::IdRef(20), Operand::IdRef(10)],
            ),
            i(
                Op::Store,
                None,
                None,
                vec![Operand::IdRef(31), Operand::IdRef(33)],
            ),
            i(
                Op::InBoundsAccessChain,
                Some(5),
                Some(32),
                vec![Operand::IdRef(20), Operand::IdRef(11)],
            ),
            i(
                Op::Store,
                None,
                None,
                vec![Operand::IdRef(32), Operand::IdRef(34)],
            ),
            i(Op::Load, Some(1), Some(35), vec![Operand::IdRef(20)]),
            i(Op::Return, None, None, vec![]),
        ];
        // %33/%34 are half values produced upstream (modeled as undefs here for the test body).
        let mut func = Function::new();
        func.blocks = vec![block];
        m.functions = vec![func];

        assert!(rewrite_subword_packed_scalars(&mut m));

        // The variable now points at a `<2 x half>` Function pointer (not the original uint pointer %4).
        let var = m.functions[0].blocks[0]
            .instructions
            .iter()
            .find(|x| x.result_id == Some(20) && x.class.opcode == Op::Variable)
            .unwrap();
        let var_ptr = var.result_type.unwrap();
        assert_ne!(var_ptr, 4);
        let pointee = m
            .types_global_values
            .iter()
            .find(|x| x.result_id == Some(var_ptr))
            .and_then(|x| x.operands.get(1))
            .cloned();
        let vec_id = match pointee {
            Some(Operand::IdRef(v)) => v,
            _ => panic!("var ptr has no pointee"),
        };
        let vecdef = m
            .types_global_values
            .iter()
            .find(|x| x.result_id == Some(vec_id))
            .unwrap();
        assert_eq!(vecdef.class.opcode, Op::TypeVector);
        assert_eq!(vecdef.operands.first(), Some(&Operand::IdRef(2))); // component = half
        assert_eq!(vecdef.operands.get(1), Some(&Operand::LiteralBit32(2))); // 2 lanes

        // The whole-word load %35 became OpBitcast %uint (its result id is preserved), fed by a fresh
        // OpLoad of the vector type.
        let body = &m.functions[0].blocks[0].instructions;
        let bc = body.iter().find(|x| x.result_id == Some(35)).unwrap();
        assert_eq!(bc.class.opcode, Op::Bitcast);
        assert_eq!(bc.result_type, Some(1)); // -> uint
        let src = match bc.operands.first() {
            Some(Operand::IdRef(s)) => *s,
            _ => panic!("bitcast has no source"),
        };
        let vload = body.iter().find(|x| x.result_id == Some(src)).unwrap();
        assert_eq!(vload.class.opcode, Op::Load);
        assert_eq!(vload.result_type, Some(vec_id));
        assert_eq!(vload.operands.first(), Some(&Operand::IdRef(20)));

        // The half access chains are untouched (still index the variable producing %_ptr_Function_half).
        let g0 = body.iter().find(|x| x.result_id == Some(31)).unwrap();
        assert_eq!(g0.class.opcode, Op::InBoundsAccessChain);
        assert_eq!(g0.result_type, Some(5));
    }

    // A `uint` Function alloca whose bytes are taken by a foreign op (here passed as a function-call
    // argument, not a load/store/sub-word-chain) must be left untouched — the strict all-uses gate bails.
    #[test]
    fn uint_alloca_with_foreign_use_is_left_untouched() {
        let mut m = Module::new();
        m.header = Some(ModuleHeader::new(40));
        m.types_global_values = vec![
            i(
                Op::TypeInt,
                None,
                Some(1),
                vec![Operand::LiteralBit32(32), Operand::LiteralBit32(0)],
            ),
            i(
                Op::TypeFloat,
                None,
                Some(2),
                vec![Operand::LiteralBit32(16)],
            ),
            i(
                Op::TypePointer,
                None,
                Some(4),
                vec![
                    Operand::StorageClass(StorageClass::Function),
                    Operand::IdRef(1),
                ],
            ),
            i(
                Op::TypePointer,
                None,
                Some(5),
                vec![
                    Operand::StorageClass(StorageClass::Function),
                    Operand::IdRef(2),
                ],
            ),
            i(
                Op::Constant,
                Some(1),
                Some(10),
                vec![Operand::LiteralBit32(0)],
            ),
        ];
        let mut block = Block::new();
        block.label = Some(i(Op::Label, None, Some(30), vec![]));
        block.instructions = vec![
            // The Function-storage variable lives in the function's entry block.
            i(
                Op::Variable,
                Some(4),
                Some(20),
                vec![Operand::StorageClass(StorageClass::Function)],
            ),
            // A legitimate sub-word chain...
            i(
                Op::InBoundsAccessChain,
                Some(5),
                Some(31),
                vec![Operand::IdRef(20), Operand::IdRef(10)],
            ),
            // ...but the variable is ALSO handed whole to a foreign op (the address escapes) -> bail.
            i(
                Op::FunctionCall,
                Some(1),
                Some(32),
                vec![Operand::IdRef(99), Operand::IdRef(20)],
            ),
            i(Op::Return, None, None, vec![]),
        ];
        let mut func = Function::new();
        func.blocks = vec![block];
        m.functions = vec![func];

        assert!(!rewrite_subword_packed_scalars(&mut m));
    }
}