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
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
//! Combined performance audit for vyre kernels.
//!
//! Source-of-truth: `PERF_ROADMAP_2026-05-01.md` section B.3 +
//! `SEPARATION_AUDIT_2026-05-01.md` section S3.
//!
//! One entry point runs every substrate-neutral analysis on a
//! `KernelDescriptor` and returns a unified `PerfAuditReport` with
//! sub-reports + a single `waste_score` aggregate + a list of
//! prioritized recommendations.
//!
//! This is the user-visible "tell me what's slow" call. Substrate-
//! specific patterns produce their own per-emit reports; emitter crates
//! expose their own audit entry points and the host can compose them.

use crate::analyses::{
    bank_conflict, coalesce, shared_mem_promote, BankConflictReport, CoalescenceReport,
    PromotionPlan,
};
use crate::KernelDescriptor;
use serde::{Deserialize, Serialize};

/// Unified report combining every substrate-neutral analysis.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PerfAuditReport {
    pub kernel_id: String,
    pub coalesce: CoalescenceReport,
    pub shared_mem: PromotionPlan,
    pub bank_conflict: BankConflictReport,
    /// Single aggregate score: sum of `1 - throughput_factor` across
    /// every coalesce site + critical bank-conflict count weight +
    /// number of unrealized promotion candidates. Higher = worse.
    /// Useful for ranking kernels in a corpus by which to optimize first.
    pub waste_score: f32,
    /// Human-readable, prioritized recommendations. Ordered by impact.
    pub recommendations: Vec<Recommendation>,
}

impl PerfAuditReport {
    /// One-line human-readable summary suitable for log lines.
    /// Format: `"<id>: waste=X.X, N recommendations (top: <top-msg>)"`.
    /// When there are no recommendations: `"<id>: waste=X.X, clean"`.
    #[must_use]
    pub fn format_short(&self) -> String {
        let id = if self.kernel_id.is_empty() {
            "<unnamed>"
        } else {
            self.kernel_id.as_str()
        };
        match self.recommendations.first() {
            Some(top) => format!(
                "{id}: waste={:.2}, {} recommendations (top: {})",
                self.waste_score,
                self.recommendations.len(),
                top.message
            ),
            None => format!("{id}: waste={:.2}, clean", self.waste_score),
        }
    }

    /// True iff the report has zero recommendations AND zero waste.
    /// Stronger than `recommendations.is_empty()` because a kernel
    /// can have non-zero waste even without recommendations (e.g.,
    /// uncoalesced accesses we don't currently flag at recommendation
    /// granularity).
    #[must_use]
    pub fn is_clean(&self) -> bool {
        self.recommendations.is_empty() && self.waste_score == 0.0
    }

    /// Recommendations filtered by category. Useful for tooling that
    /// wants only one perf-issue family (e.g., only coalesce-related
    /// advice for a memory-perf dashboard).
    #[must_use]
    pub fn recommendations_by_category(
        &self,
        category: RecommendationCategory,
    ) -> Vec<&Recommendation> {
        self.recommendations
            .iter()
            .filter(|r| r.category == category)
            .collect()
    }

    /// The highest-priority recommendation (lowest `priority` value).
    /// `None` if the report has no recommendations.
    #[must_use]
    pub fn top_recommendation(&self) -> Option<&Recommendation> {
        self.recommendations.iter().min_by_key(|r| r.priority)
    }
}

impl std::fmt::Display for PerfAuditReport {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.format_short())
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Recommendation {
    pub category: RecommendationCategory,
    /// Priority `0` = highest. Ties broken by category order in the enum.
    pub priority: u32,
    /// Human-readable explanation, stable enough to grep for.
    pub message: String,
    /// Estimated speedup multiplier if this is fixed alone (best-case
    /// upper bound, not a promise).
    pub estimated_speedup_upper_bound: f32,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum RecommendationCategory {
    /// PERF B14: memory access not coalesced; emit-side fix or layout
    /// change needed.
    Coalesce,
    /// PERF B12: shared-memory promotion candidate remains in global memory.
    SharedMemPromote,
    /// PERF B13: shared-memory bank conflict serializes accesses.
    BankConflict,
}

/// Like [`audit`] but also returns an [`crate::analyses::OpHistogram`]
/// so the caller gets perf recommendations AND kernel-shape telemetry
/// in a single walk. Useful for routing decisions: a memory-bound
/// kernel with a Coalesce recommendation should be addressed via the
/// substrate-specific emission layer; an arithmetic-bound kernel might
/// benefit more from hardware math fusion at the same layer.
#[must_use]
pub fn audit_with_histogram(
    desc: &KernelDescriptor,
) -> (PerfAuditReport, crate::analyses::OpHistogram) {
    (audit(desc), crate::analyses::op_histogram::analyze(desc))
}

/// Like [`audit`] but runs the standard rewrite pipeline first.
///
/// Tells callers: "what substrate-neutral perf issues REMAIN after
/// the optimization stack already ran?" Mirrors the
/// emitter-level `audit_optimized` functions; every layer offers the
/// same diagnostic question at its level.
#[must_use]
pub fn audit_optimized(desc: &KernelDescriptor) -> PerfAuditReport {
    let optimized = crate::rewrites::run_all(desc);
    audit(&optimized)
}

/// Run every substrate-neutral analysis and return the unified report.
#[must_use]
pub fn audit(desc: &KernelDescriptor) -> PerfAuditReport {
    let coalesce_report = coalesce::analyze(desc);
    let shared_mem_report = shared_mem_promote::analyze(desc);
    let bank_conflict_report = bank_conflict::analyze(desc);

    let mut waste_score = coalesce_report.waste_score();
    waste_score += shared_mem_report.candidates.len() as f32 * 5.0;
    waste_score += bank_conflict_report.critical_count() as f32 * 8.0;
    waste_score += (bank_conflict_report.problematic_count()
        - bank_conflict_report.critical_count()) as f32
        * 2.0;

    let recommendations = recommend(&coalesce_report, &shared_mem_report, &bank_conflict_report);

    PerfAuditReport {
        kernel_id: desc.id.clone(),
        coalesce: coalesce_report,
        shared_mem: shared_mem_report,
        bank_conflict: bank_conflict_report,
        waste_score,
        recommendations,
    }
}

fn recommend(
    coalesce: &CoalescenceReport,
    shared_mem: &PromotionPlan,
    bank_conflict: &BankConflictReport,
) -> Vec<Recommendation> {
    // Upper bound: every coalesce site, every shared-mem candidate,
    // and every bank-conflict site can produce at most one recommendation.
    // Pre-size to the sum so the three sequential pushes never resize.
    let mut out = Vec::with_capacity(
        coalesce.sites.len() + shared_mem.candidates.len() + bank_conflict.sites.len(),
    );

    for site in &coalesce.sites {
        if site.pattern.is_problematic() {
            let speedup = 1.0 / site.pattern.throughput_factor();
            out.push(Recommendation {
                category: RecommendationCategory::Coalesce,
                priority: priority_for_speedup(speedup),
                message: format!(
                    "non-coalesced access at op {}, slot {}: {:?}",
                    site.op_index, site.binding_slot, site.pattern
                ),
                estimated_speedup_upper_bound: speedup,
            });
        }
    }

    for cand in &shared_mem.candidates {
        out.push(Recommendation {
            category: RecommendationCategory::SharedMemPromote,
            priority: priority_for_speedup(cand.estimated_speedup_factor),
            message: format!(
                "shared-memory promotion candidate slot {}: {} accesses, tile {} bytes",
                cand.binding_slot, cand.access_count, cand.tile_bytes
            ),
            estimated_speedup_upper_bound: cand.estimated_speedup_factor,
        });
    }

    for site in &bank_conflict.sites {
        use crate::analyses::bank_conflict::ConflictSeverity;
        if let crate::analyses::bank_conflict::BankConflictKind::Conflict { way_count } =
            site.conflict
        {
            let speedup = way_count as f32;
            let severity = site.conflict.severity();
            let priority = match severity {
                ConflictSeverity::Critical => 0,
                ConflictSeverity::Severe => 1,
                ConflictSeverity::Mild => 2,
                _ => 3,
            };
            out.push(Recommendation {
                category: RecommendationCategory::BankConflict,
                priority,
                message: format!(
                    "{}-way bank conflict at op {}, slot {}",
                    way_count, site.op_index, site.binding_slot
                ),
                estimated_speedup_upper_bound: speedup,
            });
        }
    }

    out.sort_by(|a, b| {
        a.priority.cmp(&b.priority).then(
            b.estimated_speedup_upper_bound
                .partial_cmp(&a.estimated_speedup_upper_bound)
                .unwrap_or(std::cmp::Ordering::Equal),
        )
    });

    out
}

fn priority_for_speedup(speedup: f32) -> u32 {
    if speedup >= 16.0 {
        0
    } else if speedup >= 4.0 {
        1
    } else if speedup >= 2.0 {
        2
    } else {
        3
    }
}

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

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

    #[test]
    fn empty_kernel_has_zero_waste_and_no_recommendations() {
        let r = audit(&empty_kernel());
        assert_eq!(r.kernel_id, "empty");
        assert!((r.waste_score - 0.0).abs() < 1e-6);
        assert!(r.recommendations.is_empty());
    }

    #[test]
    fn coalesced_kernel_has_zero_waste() {
        // Single coalesced load  -  perfect.
        let kk = KernelDescriptor {
            id: "perfect".into(),
            bindings: BindingLayout {
                slots: vec![BindingSlot {
                    slot: 0,
                    element_type: DataType::F32,
                    element_count: None,
                    memory_class: MemoryClass::Global,
                    visibility: BindingVisibility::ReadOnly,
                    name: "buf".into(),
                }],
            },
            dispatch: Dispatch::new(64, 1, 1),
            body: KernelBody {
                ops: vec![
                    KernelOp {
                        kind: KernelOpKind::LocalInvocationId,
                        operands: vec![],
                        result: Some(0),
                    },
                    KernelOp {
                        kind: KernelOpKind::LoadGlobal,
                        operands: vec![0, 0],
                        result: Some(1),
                    },
                ],
                child_bodies: vec![],
                literals: vec![],
            },
        };
        let r = audit(&kk);
        assert!((r.waste_score - 0.0).abs() < 1e-6);
        assert!(r.recommendations.is_empty());
    }

    #[test]
    fn strided_kernel_produces_coalesce_recommendation() {
        // load(buf, 4 * tid)
        let kk = KernelDescriptor {
            id: "strided".into(),
            bindings: BindingLayout {
                slots: vec![BindingSlot {
                    slot: 0,
                    element_type: DataType::F32,
                    element_count: None,
                    memory_class: MemoryClass::Global,
                    visibility: BindingVisibility::ReadOnly,
                    name: "buf".into(),
                }],
            },
            dispatch: Dispatch::new(64, 1, 1),
            body: KernelBody {
                ops: vec![
                    KernelOp {
                        kind: KernelOpKind::LocalInvocationId,
                        operands: vec![],
                        result: Some(0),
                    },
                    KernelOp {
                        kind: KernelOpKind::Literal,
                        operands: vec![0],
                        result: Some(1),
                    },
                    KernelOp {
                        kind: KernelOpKind::BinOpKind(BinOp::Mul),
                        operands: vec![1, 0],
                        result: Some(2),
                    },
                    KernelOp {
                        kind: KernelOpKind::LoadGlobal,
                        operands: vec![0, 2],
                        result: Some(3),
                    },
                ],
                child_bodies: vec![],
                literals: vec![LiteralValue::U32(4)],
            },
        };
        let r = audit(&kk);
        assert!(r.waste_score > 0.0);
        assert_eq!(r.recommendations.len(), 1);
        assert_eq!(
            r.recommendations[0].category,
            RecommendationCategory::Coalesce
        );
        assert!(r.recommendations[0].message.contains("non-coalesced"));
    }

    #[test]
    fn shared_mem_promotion_candidate_appears_in_recommendations() {
        // Two LoadGlobal of same slot  -  promotion candidate.
        let kk = KernelDescriptor {
            id: "promote".into(),
            bindings: BindingLayout {
                slots: vec![BindingSlot {
                    slot: 0,
                    element_type: DataType::F32,
                    element_count: None,
                    memory_class: MemoryClass::Global,
                    visibility: BindingVisibility::ReadOnly,
                    name: "buf".into(),
                }],
            },
            dispatch: Dispatch::new(32, 1, 1),
            body: KernelBody {
                ops: vec![
                    KernelOp {
                        kind: KernelOpKind::Literal,
                        operands: vec![0],
                        result: Some(0),
                    },
                    KernelOp {
                        kind: KernelOpKind::LoadGlobal,
                        operands: vec![0, 0],
                        result: Some(1),
                    },
                    KernelOp {
                        kind: KernelOpKind::LoadGlobal,
                        operands: vec![0, 0],
                        result: Some(2),
                    },
                ],
                child_bodies: vec![],
                literals: vec![LiteralValue::U32(0)],
            },
        };
        let r = audit(&kk);
        assert!(
            r.recommendations
                .iter()
                .any(|rec| rec.category == RecommendationCategory::SharedMemPromote),
            "got: {:?}",
            r.recommendations
        );
    }

    #[test]
    fn recommendations_sorted_by_priority_then_speedup() {
        // Build a kernel with mixed problems to verify ordering.
        let kk = KernelDescriptor {
            id: "mixed".into(),
            bindings: BindingLayout {
                slots: vec![
                    BindingSlot {
                        slot: 0,
                        element_type: DataType::F32,
                        element_count: None,
                        memory_class: MemoryClass::Global,
                        visibility: BindingVisibility::ReadOnly,
                        name: "g".into(),
                    },
                    BindingSlot {
                        slot: 1,
                        element_type: DataType::F32,
                        element_count: Some(64),
                        memory_class: MemoryClass::Shared,
                        visibility: BindingVisibility::ReadWrite,
                        name: "s".into(),
                    },
                ],
            },
            dispatch: Dispatch::new(32, 1, 1),
            body: KernelBody {
                ops: vec![
                    // Strided global load (4× speedup)
                    KernelOp {
                        kind: KernelOpKind::LocalInvocationId,
                        operands: vec![],
                        result: Some(0),
                    },
                    KernelOp {
                        kind: KernelOpKind::Literal,
                        operands: vec![0],
                        result: Some(1),
                    },
                    KernelOp {
                        kind: KernelOpKind::BinOpKind(BinOp::Mul),
                        operands: vec![1, 0],
                        result: Some(2),
                    },
                    KernelOp {
                        kind: KernelOpKind::LoadGlobal,
                        operands: vec![0, 2],
                        result: Some(3),
                    },
                    // 32-way bank conflict on shared (32× speedup, critical)
                    KernelOp {
                        kind: KernelOpKind::Literal,
                        operands: vec![1],
                        result: Some(4),
                    },
                    KernelOp {
                        kind: KernelOpKind::BinOpKind(BinOp::Mul),
                        operands: vec![0, 4],
                        result: Some(5),
                    },
                    KernelOp {
                        kind: KernelOpKind::LoadShared,
                        operands: vec![1, 5],
                        result: Some(6),
                    },
                ],
                child_bodies: vec![],
                literals: vec![LiteralValue::U32(4), LiteralValue::U32(32)],
            },
        };
        let r = audit(&kk);
        assert!(r.recommendations.len() >= 2);
        // Critical bank conflict (priority 0) must come before strided
        // global load (priority 1).
        let categories: Vec<_> = r.recommendations.iter().map(|r| r.category).collect();
        let bank_pos = categories
            .iter()
            .position(|c| *c == RecommendationCategory::BankConflict);
        let coalesce_pos = categories
            .iter()
            .position(|c| *c == RecommendationCategory::Coalesce);
        assert!(bank_pos.unwrap() < coalesce_pos.unwrap());
    }

    #[test]
    fn report_kernel_id_echoes_descriptor_id() {
        let r = audit(&empty_kernel());
        assert_eq!(r.kernel_id, "empty");
    }

    #[test]
    fn priority_for_speedup_brackets() {
        assert_eq!(priority_for_speedup(32.0), 0);
        assert_eq!(priority_for_speedup(16.0), 0);
        assert_eq!(priority_for_speedup(8.0), 1);
        assert_eq!(priority_for_speedup(4.0), 1);
        assert_eq!(priority_for_speedup(3.0), 2);
        assert_eq!(priority_for_speedup(2.0), 2);
        assert_eq!(priority_for_speedup(1.5), 3);
    }

    #[test]
    fn format_short_clean_kernel_says_clean() {
        use crate::{BindingLayout, Dispatch, KernelBody, KernelDescriptor};
        let desc = KernelDescriptor {
            id: "named".into(),
            bindings: BindingLayout { slots: vec![] },
            dispatch: Dispatch::new(64, 1, 1),
            body: KernelBody {
                ops: vec![],
                child_bodies: vec![],
                literals: vec![],
            },
        };
        let r = audit(&desc);
        let s = r.format_short();
        assert!(s.contains("named:"));
        assert!(s.contains("clean") || s.contains("recommendations"));
    }

    #[test]
    fn format_short_unnamed_uses_unnamed_label() {
        use crate::{BindingLayout, Dispatch, KernelBody, KernelDescriptor};
        let mut desc = KernelDescriptor {
            id: String::new(),
            bindings: BindingLayout { slots: vec![] },
            dispatch: Dispatch::new(64, 1, 1),
            body: KernelBody {
                ops: vec![],
                child_bodies: vec![],
                literals: vec![],
            },
        };
        desc.id = String::new();
        let r = audit(&desc);
        assert!(r.format_short().contains("<unnamed>"));
    }

    #[test]
    fn is_clean_on_empty_kernel() {
        use crate::{BindingLayout, Dispatch, KernelBody, KernelDescriptor};
        let desc = KernelDescriptor {
            id: "k".into(),
            bindings: BindingLayout { slots: vec![] },
            dispatch: Dispatch::new(64, 1, 1),
            body: KernelBody {
                ops: vec![],
                child_bodies: vec![],
                literals: vec![],
            },
        };
        assert!(audit(&desc).is_clean());
    }

    fn empty_report_with_recs(recs: Vec<Recommendation>) -> PerfAuditReport {
        use crate::{BindingLayout, Dispatch, KernelBody, KernelDescriptor};
        let empty = KernelDescriptor {
            id: "k".into(),
            bindings: BindingLayout { slots: vec![] },
            dispatch: Dispatch::new(64, 1, 1),
            body: KernelBody {
                ops: vec![],
                child_bodies: vec![],
                literals: vec![],
            },
        };
        let mut r = audit(&empty);
        r.recommendations = recs;
        r
    }

    #[test]
    fn recommendations_by_category_filters() {
        let report = empty_report_with_recs(vec![
            Recommendation {
                category: RecommendationCategory::Coalesce,
                priority: 0,
                message: "uncoalesced access at op 3".into(),
                estimated_speedup_upper_bound: 4.0,
            },
            Recommendation {
                category: RecommendationCategory::SharedMemPromote,
                priority: 1,
                message: "promote slot 0".into(),
                estimated_speedup_upper_bound: 2.0,
            },
            Recommendation {
                category: RecommendationCategory::Coalesce,
                priority: 2,
                message: "another coalesce".into(),
                estimated_speedup_upper_bound: 1.5,
            },
        ]);
        let coalesce_only = report.recommendations_by_category(RecommendationCategory::Coalesce);
        assert_eq!(coalesce_only.len(), 2);
        assert!(coalesce_only
            .iter()
            .all(|r| r.category == RecommendationCategory::Coalesce));
    }

    #[test]
    fn top_recommendation_picks_lowest_priority_value() {
        let report = empty_report_with_recs(vec![
            Recommendation {
                category: RecommendationCategory::Coalesce,
                priority: 5,
                message: "low".into(),
                estimated_speedup_upper_bound: 1.0,
            },
            Recommendation {
                category: RecommendationCategory::BankConflict,
                priority: 0,
                message: "top".into(),
                estimated_speedup_upper_bound: 8.0,
            },
        ]);
        let top = report.top_recommendation().unwrap();
        assert_eq!(top.message, "top");
    }

    #[test]
    fn top_recommendation_none_when_empty() {
        let report = empty_report_with_recs(vec![]);
        assert!(report.top_recommendation().is_none());
    }

    #[test]
    fn audit_with_histogram_returns_both() {
        use crate::{
            BindingLayout, Dispatch, KernelBody, KernelDescriptor, KernelOp, KernelOpKind,
        };
        let desc = KernelDescriptor {
            id: "k".into(),
            bindings: BindingLayout { slots: vec![] },
            dispatch: Dispatch::new(64, 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),
                    },
                ],
                child_bodies: vec![],
                literals: vec![crate::LiteralValue::U32(7)],
            },
        };
        let (report, hist) = audit_with_histogram(&desc);
        assert_eq!(report.kernel_id, "k");
        assert_eq!(hist.literal, 2);
        assert_eq!(hist.total(), 2);
    }

    #[test]
    fn audit_optimized_runs_pipeline_first_and_returns_report() {
        use crate::{
            BindingLayout, Dispatch, KernelBody, KernelDescriptor, KernelOp, KernelOpKind,
        };
        let desc = KernelDescriptor {
            id: "audit_opt".into(),
            bindings: BindingLayout { slots: vec![] },
            dispatch: Dispatch::new(64, 1, 1),
            body: KernelBody {
                ops: vec![KernelOp {
                    kind: KernelOpKind::Literal,
                    operands: vec![0],
                    result: Some(0),
                }],
                child_bodies: vec![],
                literals: vec![crate::LiteralValue::U32(7)],
            },
        };
        let r = audit_optimized(&desc);
        // Just confirm it doesn't panic and returns a populated report.
        assert_eq!(r.kernel_id, "audit_opt");
    }
}