vyre-lower 0.6.5

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
//! Common-subexpression elimination rewrite.
//!
//! Uses `analyses::common_subexpr` to find equivalence groups, picks
//! the canonical (lowest op-index) of each group, rewrites references
//! inside the same body to point at the canonical, then strips duplicate
//! producers whose result is not visible across a structured-body
//! boundary.

use crate::analyses::common_subexpr;
use crate::operand_semantics::operand_is_result_reference;
use crate::{KernelBody, KernelDescriptor, KernelOp};
use rustc_hash::{FxHashMap, FxHashSet};

#[must_use]
pub fn descriptor_cse(desc: &KernelDescriptor) -> KernelDescriptor {
    let mut out = desc.clone();
    out.body = cse_body(out.body, &FxHashSet::default());
    out
}

fn cse_body(mut body: KernelBody, externally_protected: &FxHashSet<u32>) -> KernelBody {
    let report = common_subexpr::analyze_body_shallow(String::new(), &body);
    let protected = protected_result_ids(&body, externally_protected);

    // For each equivalence group, build a "duplicate result-id →
    // canonical result-id" map.
    let mut id_remap = FxHashMap::<u32, u32>::default();
    let mut duplicates_to_strip = FxHashSet::<usize>::default();
    for group in &report.groups {
        let canonical_idx = group.op_indices[0];
        // Only collapse groups whose ops are within this body's
        // top-level ops (the analysis returns op-indices spanning
        // child bodies via offset). Filter to top-level.
        if canonical_idx >= body.ops.len() {
            continue;
        }
        let canonical_result = match body.ops[canonical_idx].result {
            Some(r) => r,
            None => continue,
        };
        for dup_idx in group.op_indices.iter().skip(1) {
            if *dup_idx >= body.ops.len() {
                continue;
            }
            if let Some(dup_result) = body.ops[*dup_idx].result {
                if protected.contains(&dup_result) {
                    continue;
                }
                id_remap.insert(dup_result, canonical_result);
                duplicates_to_strip.insert(*dup_idx);
            }
        }
    }

    // Strip duplicates and rewrite operand refs.
    let child_external_refs: Vec<FxHashSet<u32>> = body
        .child_bodies
        .iter()
        .map(|child| external_refs_for_child(&body, child))
        .collect();
    let mut surviving: Vec<KernelOp> = Vec::with_capacity(body.ops.len());
    let old_ops = std::mem::take(&mut body.ops);
    for (idx, mut op) in old_ops.into_iter().enumerate() {
        if duplicates_to_strip.contains(&idx) {
            continue;
        }
        for pos in 0..op.operands.len() {
            if operand_is_result_reference(&op.kind, pos) {
                if let Some(canonical) = id_remap.get(&op.operands[pos]) {
                    op.operands[pos] = *canonical;
                }
            }
        }
        surviving.push(op);
    }

    body.child_bodies = body
        .child_bodies
        .into_iter()
        .zip(child_external_refs.iter())
        .map(|(child, refs)| cse_body(child, refs))
        .collect();

    body.ops = surviving;
    body
}

fn protected_result_ids(
    body: &KernelBody,
    externally_protected: &FxHashSet<u32>,
) -> FxHashSet<u32> {
    let produced: FxHashSet<u32> = body.ops.iter().flat_map(KernelOp::result_ids).collect();
    let mut protected: FxHashSet<u32> = externally_protected
        .intersection(&produced)
        .copied()
        .collect();
    for child in &body.child_bodies {
        for result_ref in collect_result_refs(child) {
            if produced.contains(&result_ref) {
                protected.insert(result_ref);
            }
        }
    }
    protected
}

fn external_refs_for_child(parent: &KernelBody, child: &KernelBody) -> FxHashSet<u32> {
    let child_results = collect_results(child);
    let mut refs = FxHashSet::default();
    for op in &parent.ops {
        for (pos, operand) in op.operands.iter().enumerate() {
            if operand_is_result_reference(&op.kind, pos) && child_results.contains(operand) {
                refs.insert(*operand);
            }
        }
    }
    refs
}

fn collect_results(body: &KernelBody) -> FxHashSet<u32> {
    let mut results = FxHashSet::default();
    for op in &body.ops {
        for result in op.result_ids() {
            results.insert(result);
        }
    }
    for child in &body.child_bodies {
        results.extend(collect_results(child));
    }
    results
}

fn collect_result_refs(body: &KernelBody) -> FxHashSet<u32> {
    let mut refs = FxHashSet::default();
    for op in &body.ops {
        for (pos, operand) in op.operands.iter().enumerate() {
            if operand_is_result_reference(&op.kind, pos) {
                refs.insert(*operand);
            }
        }
    }
    for child in &body.child_bodies {
        refs.extend(collect_result_refs(child));
    }
    refs
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        BindingLayout, BindingSlot, BindingVisibility, Dispatch, KernelBody, KernelDescriptor,
        KernelOp, KernelOpKind, LiteralValue, MemoryClass,
    };
    use vyre_foundation::ir::{BinOp, DataType};

    #[test]
    fn cse_collapses_duplicate_literals() {
        let desc = KernelDescriptor {
            id: "k".into(),
            bindings: BindingLayout {
                slots: vec![BindingSlot {
                    slot: 0,
                    element_type: DataType::U32,
                    element_count: None,
                    memory_class: MemoryClass::Global,
                    visibility: BindingVisibility::WriteOnly,
                    name: "out".into(),
                }],
            },
            dispatch: Dispatch::new(1, 1, 1),
            body: KernelBody {
                ops: vec![
                    KernelOp {
                        kind: KernelOpKind::Literal,
                        operands: vec![0],
                        result: Some(0),
                    },
                    KernelOp {
                        kind: KernelOpKind::Literal,
                        operands: vec![0],
                        result: Some(1),
                    }, // dup of r0
                    KernelOp {
                        kind: KernelOpKind::StoreGlobal,
                        operands: vec![0, 0, 1], // index uses r0; value uses r1 (dup)
                        result: None,
                    },
                ],
                child_bodies: vec![],
                literals: vec![LiteralValue::U32(7)],
            },
        };
        let out = descriptor_cse(&desc);
        // Duplicate r1 should be stripped. 3 ops → 2 ops.
        assert_eq!(out.body.ops.len(), 2);
        // Store should now reference r0 in BOTH index and value positions
        // (both point at the canonical literal).
        assert_eq!(out.body.ops[1].kind, KernelOpKind::StoreGlobal);
        assert_eq!(out.body.ops[1].operands, vec![0, 0, 0]);
    }

    #[test]
    fn cse_collapses_duplicate_arithmetic() {
        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::Literal,
                        operands: vec![1],
                        result: Some(1),
                    },
                    KernelOp {
                        kind: KernelOpKind::BinOpKind(BinOp::Add),
                        operands: vec![0, 1],
                        result: Some(2),
                    },
                    KernelOp {
                        kind: KernelOpKind::BinOpKind(BinOp::Add),
                        operands: vec![0, 1],
                        result: Some(3),
                    }, // dup of op 2
                ],
                child_bodies: vec![],
                literals: vec![LiteralValue::U32(3), LiteralValue::U32(4)],
            },
        };
        let out = descriptor_cse(&desc);
        // 4 ops → 3 ops (one Add stripped).
        assert_eq!(out.body.ops.len(), 3);
    }

    #[test]
    fn cse_on_empty_kernel_is_noop() {
        let desc = KernelDescriptor {
            id: "k".into(),
            bindings: BindingLayout { slots: vec![] },
            dispatch: Dispatch::new(1, 1, 1),
            body: KernelBody {
                ops: vec![],
                child_bodies: vec![],
                literals: vec![],
            },
        };
        let out = descriptor_cse(&desc);
        assert!(out.body.ops.is_empty());
    }

    #[test]
    fn cse_preserves_unique_ops() {
        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::Literal,
                        operands: vec![1],
                        result: Some(1),
                    },
                    KernelOp {
                        kind: KernelOpKind::Literal,
                        operands: vec![2],
                        result: Some(2),
                    },
                ],
                child_bodies: vec![],
                literals: vec![
                    LiteralValue::U32(1),
                    LiteralValue::U32(2),
                    LiteralValue::U32(3),
                ],
            },
        };
        let out = descriptor_cse(&desc);
        assert_eq!(out.body.ops.len(), 3);
    }

    #[test]
    fn cse_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::Literal,
                        operands: vec![0],
                        result: Some(1),
                    },
                    KernelOp {
                        kind: KernelOpKind::Literal,
                        operands: vec![0],
                        result: Some(2),
                    },
                ],
                child_bodies: vec![],
                literals: vec![LiteralValue::U32(7)],
            },
        };
        let once = descriptor_cse(&desc);
        let twice = descriptor_cse(&once);
        assert_eq!(once.body.ops.len(), twice.body.ops.len());
    }

    #[test]
    fn cse_preserves_parent_result_used_by_child_body() {
        let desc = KernelDescriptor {
            id: "cross_body_parent".into(),
            bindings: BindingLayout {
                slots: vec![BindingSlot {
                    slot: 0,
                    element_type: DataType::U32,
                    element_count: None,
                    memory_class: MemoryClass::Global,
                    visibility: BindingVisibility::WriteOnly,
                    name: "out".into(),
                }],
            },
            dispatch: Dispatch::new(1, 1, 1),
            body: KernelBody {
                ops: vec![
                    KernelOp {
                        kind: KernelOpKind::Literal,
                        operands: vec![0],
                        result: Some(0),
                    },
                    KernelOp {
                        kind: KernelOpKind::Literal,
                        operands: vec![0],
                        result: Some(9),
                    },
                    KernelOp {
                        kind: KernelOpKind::StructuredBlock,
                        operands: vec![0],
                        result: None,
                    },
                ],
                child_bodies: vec![KernelBody {
                    ops: vec![KernelOp {
                        kind: KernelOpKind::StoreGlobal,
                        operands: vec![0, 9, 0],
                        result: None,
                    }],
                    child_bodies: vec![],
                    literals: vec![],
                }],
                literals: vec![LiteralValue::U32(7)],
            },
        };

        assert_eq!(crate::verify::verify(&desc), Ok(()));
        let out = descriptor_cse(&desc);
        assert_eq!(crate::verify::verify(&out), Ok(()));
        assert!(
            out.body.ops.iter().any(|op| op.result == Some(9)),
            "result 9 is read by the child body and cannot be stripped"
        );
    }

    #[test]
    fn cse_preserves_child_result_used_by_parent_body() {
        let desc = KernelDescriptor {
            id: "cross_body_child".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::StructuredBlock,
                        operands: vec![0],
                        result: None,
                    },
                    KernelOp {
                        kind: KernelOpKind::BinOpKind(BinOp::Mul),
                        operands: vec![9, 0],
                        result: Some(10),
                    },
                ],
                child_bodies: vec![KernelBody {
                    ops: vec![
                        KernelOp {
                            kind: KernelOpKind::BinOpKind(BinOp::Add),
                            operands: vec![0, 0],
                            result: Some(1),
                        },
                        KernelOp {
                            kind: KernelOpKind::BinOpKind(BinOp::Add),
                            operands: vec![0, 0],
                            result: Some(9),
                        },
                    ],
                    child_bodies: vec![],
                    literals: vec![],
                }],
                literals: vec![LiteralValue::U32(7)],
            },
        };

        assert_eq!(crate::verify::verify(&desc), Ok(()));
        let out = descriptor_cse(&desc);
        assert_eq!(crate::verify::verify(&out), Ok(()));
        assert!(
            out.body.child_bodies[0]
                .ops
                .iter()
                .any(|op| op.result == Some(9)),
            "result 9 is read by the parent body and cannot be stripped"
        );
    }
}