vyre-lower 0.4.1

Substrate-neutral lowering: vyre Program → KernelDescriptor consumed by vyre-emit-* crates.
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
//! Branch collapse — `StructuredIfThen`/`StructuredIfThenElse` whose
//! condition is a Literal(bool) collapses to the appropriate arm.
//!
//! This pairs naturally with `descriptor_const_fold`: descriptor_const_fold turns boolean
//! arithmetic chains into Literal(true)/Literal(false), then this
//! pass eliminates the dead branches.
//!
//! Conditions like `Eq(lit_5, lit_5)` get folded to Literal(true) by
//! descriptor_const_fold first; then this pass picks them up.

use crate::{KernelBody, KernelDescriptor, KernelOp, KernelOpKind, LiteralValue};
use rustc_hash::FxHashMap;

#[must_use]
pub fn branch_collapse(desc: &KernelDescriptor) -> KernelDescriptor {
    let mut out = desc.clone();
    out.body = collapse_body(out.body);
    out
}

fn collapse_body(mut body: KernelBody) -> KernelBody {
    // Map result-id → bool literal value (only when the producing op
    // is a Literal of a Bool value).
    let bool_lits: FxHashMap<u32, bool> = body
        .ops
        .iter()
        .filter_map(|op| match (&op.kind, op.result, op.operands.first()) {
            (KernelOpKind::Literal, Some(r), Some(pool_idx)) => {
                match body.literals.get(*pool_idx as usize) {
                    Some(LiteralValue::Bool(v)) => Some((r, *v)),
                    _ => None,
                }
            }
            _ => None,
        })
        .collect();

    // Pre-compute every result id referenced by any op in this body
    // (including nested children). If a candidate-for-drop body
    // produces an id in this set, we MUST NOT drop the body — its
    // result is consumed elsewhere and dropping creates dangling refs.
    let parent_referenced_ids = collect_all_referenced_ids(&body);
    let original_children = std::mem::take(&mut body.child_bodies);
    let mut new_ops: Vec<KernelOp> = Vec::with_capacity(body.ops.len());
    let mut new_children = original_children.clone();
    let old_ops = std::mem::take(&mut body.ops);

    for op in old_ops {
        match &op.kind {
            KernelOpKind::StructuredIfThen => {
                let cond_id = op.operands.first().copied();
                let body_id = op.operands.get(1).copied();
                if let (Some(cond_id), Some(body_id)) = (cond_id, body_id) {
                    if let Some(cond_lit) = bool_lits.get(&cond_id).copied() {
                        if cond_lit {
                            if let Some(child) = original_children.get(body_id as usize) {
                                if can_collapse_safely(child, &parent_referenced_ids) {
                                    inline_child_body(child, &mut new_ops, &mut new_children);
                                    continue;
                                }
                                // Fall through — leave the IfThen
                                // intact rather than yank refs out
                                // of scope.
                            }
                        } else {
                            // Drop the if op entirely IF the dropped
                            // body produces no id consumed elsewhere
                            // in the parent body.
                            if let Some(child) = original_children.get(body_id as usize) {
                                if dropping_body_is_safe(child, &parent_referenced_ids) {
                                    continue;
                                }
                            } else {
                                continue;
                            }
                        }
                    }
                }
            }
            KernelOpKind::StructuredIfThenElse => {
                let cond_id = op.operands.first().copied();
                let then_id = op.operands.get(1).copied();
                let else_id = op.operands.get(2).copied();
                if let (Some(cond_id), Some(then_id), Some(else_id)) = (cond_id, then_id, else_id) {
                    if let Some(cond_lit) = bool_lits.get(&cond_id).copied() {
                        let pick_id = if cond_lit { then_id } else { else_id };
                        let drop_id = if cond_lit { else_id } else { then_id };
                        let pick = original_children.get(pick_id as usize);
                        let drop = original_children.get(drop_id as usize);
                        if let Some(pick) = pick {
                            if can_collapse_safely(pick, &parent_referenced_ids)
                                && drop
                                    .map(|d| dropping_body_is_safe(d, &parent_referenced_ids))
                                    .unwrap_or(true)
                            {
                                inline_child_body(pick, &mut new_ops, &mut new_children);
                                continue;
                            }
                        }
                    }
                }
            }
            _ => {}
        }
        // Pass through: also recursively collapse any nested
        // structured-control-flow children even if the outer op isn't
        // collapsable.
        match &op.kind {
            KernelOpKind::StructuredIfThen
            | KernelOpKind::StructuredIfThenElse
            | KernelOpKind::StructuredForLoop { .. }
            | KernelOpKind::StructuredBlock
            | KernelOpKind::Region { .. } => {
                for child_id in op.operands.iter() {
                    if let Some(child) = original_children.get(*child_id as usize) {
                        let recursed = collapse_body(child.clone());
                        new_children[*child_id as usize] = recursed;
                    }
                }
            }
            _ => {}
        }
        new_ops.push(op);
    }

    body.ops = new_ops;
    body.child_bodies = new_children;
    body
}

fn inline_child_body(child: &KernelBody, ops: &mut Vec<KernelOp>, children: &mut Vec<KernelBody>) {
    let inlined = collapse_body(child.clone());
    let child_base = children.len() as u32;
    children.extend(inlined.child_bodies);
    ops.extend(
        inlined
            .ops
            .into_iter()
            .map(|op| rebase_child_body_refs(op, child_base)),
    );
}

/// Conservative pre-collapse safety check used by the IfThen and
/// IfThenElse handlers.
///
/// Inlining a child body into its grandparent is only safe when every
/// SSA result-reference inside the child resolves to an id produced
/// inside the child itself. If the child body references ids defined
/// in the body that contained the if-then (i.e. ids the inlining
/// would yank out of scope), refuse to collapse — the IfThen stays
/// intact and the verifier stays clean. This is the fix for
/// `DanglingResultRef { ref_id: 13 }` on shunting_yard descriptors.
fn can_collapse_safely(child: &KernelBody, _parent_refs: &rustc_hash::FxHashSet<u32>) -> bool {
    let mut produced = rustc_hash::FxHashSet::default();
    collect_produced_ids_inclusive(child, &mut produced);
    body_refs_only(child, &produced)
}

/// Conservative drop safety: dropping the child body is safe only
/// when none of its produced result ids are referenced anywhere in
/// the parent body (which would dangle if the producing ops vanished).
fn dropping_body_is_safe(child: &KernelBody, parent_refs: &rustc_hash::FxHashSet<u32>) -> bool {
    let mut produced = rustc_hash::FxHashSet::default();
    collect_produced_ids_inclusive(child, &mut produced);
    produced.is_disjoint(parent_refs)
}

fn collect_all_referenced_ids(body: &KernelBody) -> rustc_hash::FxHashSet<u32> {
    let mut out = rustc_hash::FxHashSet::default();
    collect_refs(body, &mut out);
    out
}

fn collect_refs(body: &KernelBody, out: &mut rustc_hash::FxHashSet<u32>) {
    for op in &body.ops {
        for (pos, &operand) in op.operands.iter().enumerate() {
            if operand_is_result_reference(&op.kind, pos) {
                out.insert(operand);
            }
        }
    }
    for child in &body.child_bodies {
        collect_refs(child, out);
    }
}

fn collect_produced_ids_inclusive(body: &KernelBody, out: &mut rustc_hash::FxHashSet<u32>) {
    for op in &body.ops {
        if let Some(r) = op.result {
            out.insert(r);
        }
    }
    for child in &body.child_bodies {
        collect_produced_ids_inclusive(child, out);
    }
}

fn body_refs_only(body: &KernelBody, produced: &rustc_hash::FxHashSet<u32>) -> bool {
    for op in &body.ops {
        for (pos, &operand) in op.operands.iter().enumerate() {
            if !operand_is_result_reference(&op.kind, pos) {
                continue;
            }
            if !produced.contains(&operand) {
                return false;
            }
        }
    }
    for child in &body.child_bodies {
        if !body_refs_only(child, produced) {
            return false;
        }
    }
    true
}

/// Conservative classification of operand positions that name a
/// result id (vs. a child-body index, literal-pool index, or binding
/// slot). Mirrors the shape the verifier uses.
fn operand_is_result_reference(kind: &KernelOpKind, pos: usize) -> bool {
    use KernelOpKind::*;
    match kind {
        Literal => false,
        LocalInvocationId
        | GlobalInvocationId
        | WorkgroupId
        | SubgroupLocalId
        | SubgroupSize
        | LoopIndex { .. } => false,
        LoopCarrier { .. } | LoopCarrierEnd { .. } => pos == 0,
        LoopCarrierFinal { .. } => false,
        LoadGlobal
        | LoadShared
        | LoadConstant
        | StoreGlobal
        | StoreShared
        | BufferLength
        | Atomic { .. }
        | AsyncLoad { .. }
        | AsyncStore { .. } => pos != 0,
        StructuredIfThen => pos == 0,
        StructuredIfThenElse => pos == 0,
        StructuredForLoop { .. } => pos == 0 || pos == 1,
        StructuredBlock | Region { .. } => false,
        IndirectDispatch { .. } => false,
        _ => true,
    }
}

fn rebase_child_body_refs(mut op: KernelOp, child_base: u32) -> KernelOp {
    for (pos, operand) in op.operands.iter_mut().enumerate() {
        if operand_is_child_body_ref(&op.kind, pos) {
            *operand = operand.saturating_add(child_base);
        }
    }
    op
}

fn operand_is_child_body_ref(kind: &KernelOpKind, pos: usize) -> bool {
    match kind {
        KernelOpKind::StructuredIfThen => pos == 1,
        KernelOpKind::StructuredIfThenElse => pos == 1 || pos == 2,
        KernelOpKind::StructuredForLoop { .. } => pos == 2,
        KernelOpKind::StructuredBlock | KernelOpKind::Region { .. } => pos == 0,
        _ => false,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        BindingLayout, Dispatch, KernelBody, KernelDescriptor, KernelOp, KernelOpKind, LiteralValue,
    };

    fn empty_kernel() -> KernelDescriptor {
        KernelDescriptor {
            id: "k".into(),
            bindings: BindingLayout { slots: vec![] },
            dispatch: Dispatch::new(1, 1, 1),
            body: KernelBody {
                ops: vec![],
                child_bodies: vec![],
                literals: vec![],
            },
        }
    }

    #[test]
    fn empty_kernel_no_change() {
        let out = branch_collapse(&empty_kernel());
        assert!(out.body.ops.is_empty());
    }

    #[test]
    fn if_then_with_true_cond_inlines_body() {
        // Lit(true); if(cond=true) { Lit(7); }
        let desc = KernelDescriptor {
            id: "true_then".into(),
            bindings: BindingLayout { slots: vec![] },
            dispatch: Dispatch::new(1, 1, 1),
            body: KernelBody {
                ops: vec![
                    KernelOp {
                        kind: KernelOpKind::Literal,
                        operands: vec![0],
                        result: Some(0),
                    },
                    KernelOp {
                        kind: KernelOpKind::StructuredIfThen,
                        operands: vec![0, 0],
                        result: None,
                    },
                ],
                child_bodies: vec![KernelBody {
                    ops: vec![KernelOp {
                        kind: KernelOpKind::Literal,
                        operands: vec![1],
                        result: Some(1),
                    }],
                    child_bodies: vec![],
                    literals: vec![LiteralValue::U32(7)],
                }],
                literals: vec![LiteralValue::Bool(true)],
            },
        };
        let out = branch_collapse(&desc);
        // Expected: outer ops = [Lit(true), Lit(7)] — IfThen replaced by inlined body.
        assert_eq!(out.body.ops.len(), 2);
        assert!(matches!(out.body.ops[0].kind, KernelOpKind::Literal));
        assert!(matches!(out.body.ops[1].kind, KernelOpKind::Literal));
        assert!(out
            .body
            .ops
            .iter()
            .all(|o| !matches!(o.kind, KernelOpKind::StructuredIfThen)));
    }

    #[test]
    fn if_then_with_false_cond_drops_branch() {
        // Lit(false); if(cond=false) { Lit(7); }
        let desc = KernelDescriptor {
            id: "false_then".into(),
            bindings: BindingLayout { slots: vec![] },
            dispatch: Dispatch::new(1, 1, 1),
            body: KernelBody {
                ops: vec![
                    KernelOp {
                        kind: KernelOpKind::Literal,
                        operands: vec![0],
                        result: Some(0),
                    },
                    KernelOp {
                        kind: KernelOpKind::StructuredIfThen,
                        operands: vec![0, 0],
                        result: None,
                    },
                ],
                child_bodies: vec![KernelBody {
                    ops: vec![KernelOp {
                        kind: KernelOpKind::Literal,
                        operands: vec![1],
                        result: Some(1),
                    }],
                    child_bodies: vec![],
                    literals: vec![LiteralValue::U32(7)],
                }],
                literals: vec![LiteralValue::Bool(false)],
            },
        };
        let out = branch_collapse(&desc);
        // Expected: outer ops = [Lit(false)] only — IfThen dropped, body discarded.
        assert_eq!(out.body.ops.len(), 1);
        assert!(matches!(out.body.ops[0].kind, KernelOpKind::Literal));
    }

    #[test]
    fn if_then_with_non_literal_cond_unchanged() {
        // tid; if(cond=tid) { Lit(7); }
        let desc = KernelDescriptor {
            id: "runtime_cond".into(),
            bindings: BindingLayout { slots: vec![] },
            dispatch: Dispatch::new(64, 1, 1),
            body: KernelBody {
                ops: vec![
                    KernelOp {
                        kind: KernelOpKind::LocalInvocationId,
                        operands: vec![0],
                        result: Some(0),
                    },
                    KernelOp {
                        kind: KernelOpKind::StructuredIfThen,
                        operands: vec![0, 0],
                        result: None,
                    },
                ],
                child_bodies: vec![KernelBody {
                    ops: vec![KernelOp {
                        kind: KernelOpKind::Literal,
                        operands: vec![0],
                        result: Some(1),
                    }],
                    child_bodies: vec![],
                    literals: vec![LiteralValue::U32(7)],
                }],
                literals: vec![],
            },
        };
        let out = branch_collapse(&desc);
        // No change.
        assert_eq!(out.body.ops.len(), 2);
        assert!(out
            .body
            .ops
            .iter()
            .any(|o| matches!(o.kind, KernelOpKind::StructuredIfThen)));
    }

    #[test]
    fn if_then_else_picks_then_arm_for_true() {
        let desc = KernelDescriptor {
            id: "true_pick".into(),
            bindings: BindingLayout { slots: vec![] },
            dispatch: Dispatch::new(1, 1, 1),
            body: KernelBody {
                ops: vec![
                    KernelOp {
                        kind: KernelOpKind::Literal,
                        operands: vec![0],
                        result: Some(0),
                    },
                    KernelOp {
                        kind: KernelOpKind::StructuredIfThenElse,
                        operands: vec![0, 0, 1],
                        result: None,
                    },
                ],
                child_bodies: vec![
                    KernelBody {
                        ops: vec![KernelOp {
                            kind: KernelOpKind::Literal,
                            operands: vec![1],
                            result: Some(1),
                        }],
                        child_bodies: vec![],
                        literals: vec![LiteralValue::U32(99)],
                    },
                    KernelBody {
                        ops: vec![KernelOp {
                            kind: KernelOpKind::Literal,
                            operands: vec![2],
                            result: Some(2),
                        }],
                        child_bodies: vec![],
                        literals: vec![LiteralValue::U32(88)],
                    },
                ],
                literals: vec![LiteralValue::Bool(true)],
            },
        };
        let out = branch_collapse(&desc);
        // Then-arm is at child_bodies[0]; we should see its 1 op survive.
        assert_eq!(out.body.ops.len(), 2); // [Lit(true), inlined_then_op]
        assert!(out
            .body
            .ops
            .iter()
            .all(|o| !matches!(o.kind, KernelOpKind::StructuredIfThenElse)));
    }

    #[test]
    fn if_then_else_picks_else_arm_for_false() {
        let desc = KernelDescriptor {
            id: "false_pick".into(),
            bindings: BindingLayout { slots: vec![] },
            dispatch: Dispatch::new(1, 1, 1),
            body: KernelBody {
                ops: vec![
                    KernelOp {
                        kind: KernelOpKind::Literal,
                        operands: vec![0],
                        result: Some(0),
                    },
                    KernelOp {
                        kind: KernelOpKind::StructuredIfThenElse,
                        operands: vec![0, 0, 1],
                        result: None,
                    },
                ],
                child_bodies: vec![
                    KernelBody {
                        ops: vec![KernelOp {
                            kind: KernelOpKind::Literal,
                            operands: vec![1],
                            result: Some(1),
                        }],
                        child_bodies: vec![],
                        literals: vec![LiteralValue::U32(99)],
                    },
                    KernelBody {
                        ops: vec![KernelOp {
                            kind: KernelOpKind::Literal,
                            operands: vec![2],
                            result: Some(2),
                        }],
                        child_bodies: vec![],
                        literals: vec![LiteralValue::U32(88)],
                    },
                ],
                literals: vec![LiteralValue::Bool(false)],
            },
        };
        let out = branch_collapse(&desc);
        // Else-arm at child_bodies[1] — its 1 op survives.
        assert_eq!(out.body.ops.len(), 2);
        assert!(out
            .body
            .ops
            .iter()
            .all(|o| !matches!(o.kind, KernelOpKind::StructuredIfThenElse)));
    }

    #[test]
    fn inlined_nested_control_flow_rebases_child_body_refs() {
        let desc = KernelDescriptor {
            id: "nested".into(),
            bindings: BindingLayout { slots: vec![] },
            dispatch: Dispatch::new(1, 1, 1),
            body: KernelBody {
                ops: vec![
                    KernelOp {
                        kind: KernelOpKind::Literal,
                        operands: vec![0],
                        result: Some(0),
                    },
                    KernelOp {
                        kind: KernelOpKind::StructuredIfThen,
                        operands: vec![0, 0],
                        result: None,
                    },
                ],
                child_bodies: vec![KernelBody {
                    ops: vec![KernelOp {
                        kind: KernelOpKind::StructuredBlock,
                        operands: vec![0],
                        result: None,
                    }],
                    child_bodies: vec![KernelBody {
                        ops: vec![KernelOp {
                            kind: KernelOpKind::Literal,
                            operands: vec![1],
                            result: Some(1),
                        }],
                        child_bodies: vec![],
                        literals: vec![LiteralValue::U32(7)],
                    }],
                    literals: vec![],
                }],
                literals: vec![LiteralValue::Bool(true)],
            },
        };

        let out = branch_collapse(&desc);
        let block = out
            .body
            .ops
            .iter()
            .find(|op| matches!(op.kind, KernelOpKind::StructuredBlock))
            .expect("nested block must survive inlined branch");
        let child_id = block.operands[0] as usize;
        assert!(
            out.body.child_bodies.get(child_id).is_some(),
            "inlined nested control-flow child id must point at a reparented child body"
        );
    }

    #[test]
    fn branch_collapse_is_idempotent() {
        let desc = KernelDescriptor {
            id: "k".into(),
            bindings: BindingLayout { slots: vec![] },
            dispatch: Dispatch::new(1, 1, 1),
            body: KernelBody {
                ops: vec![
                    KernelOp {
                        kind: KernelOpKind::Literal,
                        operands: vec![0],
                        result: Some(0),
                    },
                    KernelOp {
                        kind: KernelOpKind::StructuredIfThen,
                        operands: vec![0, 0],
                        result: None,
                    },
                ],
                child_bodies: vec![KernelBody {
                    ops: vec![KernelOp {
                        kind: KernelOpKind::Literal,
                        operands: vec![1],
                        result: Some(1),
                    }],
                    child_bodies: vec![],
                    literals: vec![LiteralValue::U32(42)],
                }],
                literals: vec![LiteralValue::Bool(true)],
            },
        };
        let once = branch_collapse(&desc);
        let twice = branch_collapse(&once);
        assert_eq!(once.body.ops.len(), twice.body.ops.len());
    }
}