vyre-foundation 0.6.3

Foundation layer: IR, type system, memory model, wire format. Zero application semantics. Part of the vyre GPU compiler.
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
//! Benchmark and hot-path driven optimizer pass selection.
//!
//! This is an execution input, not a docs surface: callers can build a
//! `PassScheduler` from [`registered_passes_for_profile_and_program`] so
//! expensive optimization families run only when program shape or runtime
//! telemetry justifies them. Correctness-critical normalizers remain selected.

use super::{
    registered_pass_registrations, CostModelFamily, OptimizerError, OptimizerProfile,
    OptimizerRunReport, PassMetadata, PassRunMetric, ProgramPassKind,
};
use crate::ir_inner::model::program::Program;
use crate::optimizer::hot_path_hints::HotPathHints;
use rustc_hash::{FxHashMap, FxHashSet};

const MIN_LOOP_NODES: usize = 12;
const MIN_MEMORY_BYTES: u64 = 16 * 1024;
const MIN_FUSION_REGIONS: usize = 2;
const MIN_DATAFLOW_NODES: usize = 64;
const MIN_MEGAKERNEL_NODES: usize = 512;

/// Why one pass was selected or skipped for a concrete Program.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum PassSelectionReason {
    /// Pass is cheap or correctness-preserving enough to always keep.
    AlwaysOn,
    /// Program shape crosses the pass family's benchmark threshold.
    ProgramShape,
    /// Runtime hot-path telemetry says this region is expensive enough.
    HotPathTelemetry,
    /// Previous pass-cost metrics prove this pass reduced a cost proxy on the
    /// same pass family.
    PassCostReport,
    /// Pass was included because another selected pass requires it.
    RequiredDependency,
    /// Pass does not belong to the requested profile.
    ProfileRejected,
    /// Program and telemetry do not justify this pass family.
    BelowThreshold,
}

/// Selection result for one pass metadata row.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct PassSelectionDecision {
    /// Pass metadata.
    pub metadata: PassMetadata,
    /// Whether the pass should be instantiated for this Program.
    pub selected: bool,
    /// Stable reason for the decision.
    pub reason: PassSelectionReason,
    /// Stable selection priority. Higher values represent stronger evidence;
    /// callers that do not reorder can still surface this to explain why a pass
    /// was kept.
    pub priority: u16,
}

/// Instantiate registered passes accepted by `profile` and selected for
/// `program`.
///
/// # Errors
/// Returns [`OptimizerError`] if the live pass registry is invalid.
pub fn registered_passes_for_profile_and_program(
    profile: OptimizerProfile,
    program: &Program,
    hints: &HotPathHints,
) -> Result<Vec<ProgramPassKind>, OptimizerError> {
    let registrations = registered_pass_registrations()?;
    let metadata = registrations
        .iter()
        .map(|registration| registration.metadata)
        .collect::<Vec<_>>();
    let selected = selected_name_set(&metadata, profile, program, hints, None);
    let mut passes = Vec::with_capacity(selected.len());
    for registration in registrations.iter() {
        if selected.contains(registration.metadata.name) {
            passes.push(ProgramPassKind::from_boxed((registration.factory)()));
        }
    }
    Ok(passes)
}

/// Return pass-selection decisions for a metadata slice.
#[must_use]
pub fn select_pass_metadata_for_program(
    metadata: &[PassMetadata],
    profile: OptimizerProfile,
    program: &Program,
    hints: &HotPathHints,
) -> Vec<PassSelectionDecision> {
    select_pass_metadata_for_program_with_report(metadata, profile, program, hints, None)
}

/// Return pass-selection decisions using optional previous pass-cost metrics.
///
/// The report path is additive: no caller is required to provide profile data,
/// and the empty report behaves exactly like [`select_pass_metadata_for_program`].
#[must_use]
pub fn select_pass_metadata_for_program_with_report(
    metadata: &[PassMetadata],
    profile: OptimizerProfile,
    program: &Program,
    hints: &HotPathHints,
    report: Option<&OptimizerRunReport>,
) -> Vec<PassSelectionDecision> {
    let cost_report = report.map(PassCostReport::from_optimizer_report);
    let selected = selected_name_set(metadata, profile, program, hints, cost_report.as_ref());
    metadata
        .iter()
        .copied()
        .map(|metadata| {
            let profile_accepted = profile.accepts(metadata);
            let initially =
                initial_selection_reason(metadata, profile, program, hints, cost_report.as_ref());
            let selected_by_closure = selected.contains(metadata.name);
            let reason = if !profile_accepted {
                PassSelectionReason::ProfileRejected
            } else if matches!(initially, PassSelectionReason::BelowThreshold)
                && selected_by_closure
            {
                PassSelectionReason::RequiredDependency
            } else {
                initially
            };
            PassSelectionDecision {
                metadata,
                selected: selected_by_closure,
                priority: selection_priority(reason),
                reason,
            }
        })
        .collect()
}

fn selected_name_set(
    metadata: &[PassMetadata],
    profile: OptimizerProfile,
    program: &Program,
    hints: &HotPathHints,
    cost_report: Option<&PassCostReport>,
) -> FxHashSet<&'static str> {
    let mut selected = FxHashSet::default();
    for pass in metadata {
        if matches!(
            initial_selection_reason(*pass, profile, program, hints, cost_report),
            PassSelectionReason::AlwaysOn
                | PassSelectionReason::ProgramShape
                | PassSelectionReason::HotPathTelemetry
                | PassSelectionReason::PassCostReport
        ) {
            selected.insert(pass.name);
        }
    }
    close_over_requirements(metadata, &mut selected);
    selected
}

fn close_over_requirements(metadata: &[PassMetadata], selected: &mut FxHashSet<&'static str>) {
    loop {
        let before = selected.len();
        for pass in metadata {
            if selected.contains(pass.name) {
                for &requirement in pass.requires {
                    if metadata
                        .iter()
                        .any(|candidate| candidate.name == requirement)
                    {
                        selected.insert(requirement);
                    }
                }
            }
        }
        if selected.len() == before {
            break;
        }
    }
}

fn initial_selection_reason(
    metadata: PassMetadata,
    profile: OptimizerProfile,
    program: &Program,
    hints: &HotPathHints,
    cost_report: Option<&PassCostReport>,
) -> PassSelectionReason {
    if !profile.accepts(metadata) {
        return PassSelectionReason::ProfileRejected;
    }
    if entry_region_is_hot(program, hints) {
        return PassSelectionReason::HotPathTelemetry;
    }
    if cost_report.is_some_and(|report| report.selects(metadata.name)) {
        return PassSelectionReason::PassCostReport;
    }
    let stats = program.stats();
    let reason_for = |above_threshold: bool| {
        if above_threshold {
            PassSelectionReason::ProgramShape
        } else {
            PassSelectionReason::BelowThreshold
        }
    };
    match metadata.cost_model_family {
        CostModelFamily::Loop => reason_for(stats.node_count >= MIN_LOOP_NODES),
        CostModelFamily::Memory => {
            reason_for(program.estimate_peak_vram_bytes() >= MIN_MEMORY_BYTES)
        }
        CostModelFamily::Fusion => {
            reason_for(stats.top_level_regions as usize >= MIN_FUSION_REGIONS)
        }
        CostModelFamily::Dataflow => reason_for(stats.node_count >= MIN_DATAFLOW_NODES),
        CostModelFamily::Megakernel => reason_for(stats.node_count >= MIN_MEGAKERNEL_NODES),
        CostModelFamily::Scalar | CostModelFamily::Sync | CostModelFamily::Unknown => {
            PassSelectionReason::AlwaysOn
        }
    }
}

#[derive(Clone, Debug, Default, Eq, PartialEq)]
struct PassCostReport {
    cost_reducing_passes: FxHashSet<&'static str>,
}

impl PassCostReport {
    fn from_optimizer_report(report: &OptimizerRunReport) -> Self {
        let mut cost_reducing_passes = FxHashSet::default();
        cost_reducing_passes.reserve(report.passes.len());
        let mut best_delta_by_pass: FxHashMap<&'static str, i128> = FxHashMap::default();
        for metric in &report.passes {
            let reduction = metric_cost_reduction(metric);
            if reduction > 0 {
                best_delta_by_pass
                    .entry(metric.pass)
                    .and_modify(|best| *best = (*best).max(reduction))
                    .or_insert(reduction);
            }
        }
        cost_reducing_passes.extend(best_delta_by_pass.into_keys());
        Self {
            cost_reducing_passes,
        }
    }

    fn selects(&self, pass: &'static str) -> bool {
        self.cost_reducing_passes.contains(pass)
    }
}

fn metric_cost_reduction(metric: &PassRunMetric) -> i128 {
    if !metric.changed {
        return 0;
    }
    [
        reduction(metric.nodes_before, metric.nodes_after),
        reduction(
            metric.static_storage_bytes_before,
            metric.static_storage_bytes_after,
        ),
        reduction(
            metric.instruction_count_before,
            metric.instruction_count_after,
        ),
        reduction(metric.memory_op_count_before, metric.memory_op_count_after),
        reduction(metric.atomic_op_count_before, metric.atomic_op_count_after),
        reduction(
            metric.control_flow_count_before,
            metric.control_flow_count_after,
        ),
        reduction(
            metric.register_pressure_before,
            metric.register_pressure_after,
        ),
        reduction(
            metric.ir_heap_allocations_before,
            metric.ir_heap_allocations_after,
        ),
        reduction(metric.ir_heap_bytes_before, metric.ir_heap_bytes_after),
    ]
    .into_iter()
    .max()
    .unwrap_or(0)
}

fn reduction<T>(before: T, after: T) -> i128
where
    T: TryInto<i128>,
{
    before.try_into().unwrap_or(i128::MAX) - after.try_into().unwrap_or(i128::MAX)
}

fn selection_priority(reason: PassSelectionReason) -> u16 {
    match reason {
        PassSelectionReason::HotPathTelemetry => 500,
        PassSelectionReason::PassCostReport => 400,
        PassSelectionReason::ProgramShape => 300,
        PassSelectionReason::AlwaysOn => 200,
        PassSelectionReason::RequiredDependency => 100,
        PassSelectionReason::BelowThreshold | PassSelectionReason::ProfileRejected => 0,
    }
}

fn entry_region_is_hot(program: &Program, hints: &HotPathHints) -> bool {
    program
        .entry_op_id()
        .is_some_and(|op_id| hints.is_hot(op_id))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ir::{BufferDecl, DataType, Expr, Node, Program};
    use crate::optimizer::{PassBoundaryClass, PassPhase, PassRunDecision};

    fn meta(
        name: &'static str,
        family: CostModelFamily,
        phase: PassPhase,
        requires: &'static [&'static str],
    ) -> PassMetadata {
        PassMetadata {
            name,
            requires,
            invalidates: &[],
            phase,
            boundary_class: PassBoundaryClass::AbiPreserving,
            requires_caps: &[],
            preserves_abi: true,
            cost_model_family: family,
        }
    }

    fn tiny_program() -> Program {
        Program::wrapped(
            vec![BufferDecl::output("out", 0, DataType::U32).with_count(1)],
            [1, 1, 1],
            vec![Node::store("out", Expr::u32(0), Expr::u32(1))],
        )
    }

    fn pass_metric(
        pass: &'static str,
        changed: bool,
        nodes_before: usize,
        nodes_after: usize,
    ) -> PassRunMetric {
        PassRunMetric {
            iteration: 0,
            pass,
            ran: true,
            changed,
            decision: if changed {
                PassRunDecision::Changed
            } else {
                PassRunDecision::RanUnchanged
            },
            refusal_kind: None,
            required_analyses: &[],
            declared_invalidations: &[],
            fact_substrate_reused: !changed,
            fact_substrate_recomputed: changed,
            fact_substrate_invalidated: changed,
            effect_bits_before: 0,
            effect_bits_after: 0,
            linear_type_violations_before: 0,
            linear_type_violations_after: 0,
            shape_predicate_violations_before: 0,
            shape_predicate_violations_after: 0,
            runtime_ns: 100,
            nodes_before,
            nodes_after,
            static_storage_bytes_before: 4096,
            static_storage_bytes_after: 4096,
            instruction_count_before: 12,
            instruction_count_after: 12,
            memory_op_count_before: 1,
            memory_op_count_after: 1,
            atomic_op_count_before: 0,
            atomic_op_count_after: 0,
            control_flow_count_before: 0,
            control_flow_count_after: 0,
            register_pressure_before: 4,
            register_pressure_after: 4,
            ir_heap_allocations_before: 0,
            ir_heap_allocations_after: 0,
            ir_heap_bytes_before: 0,
            ir_heap_bytes_after: 0,
            research_trace: None,
        }
    }

    fn report_with(metric: PassRunMetric) -> OptimizerRunReport {
        OptimizerRunReport {
            program: tiny_program(),
            passes: vec![metric],
        }
    }

    #[test]
    fn small_cold_program_skips_expensive_memory_pass() {
        let decisions = select_pass_metadata_for_program(
            &[meta(
                "decode_scan_fuse",
                CostModelFamily::Memory,
                PassPhase::Memory,
                &[],
            )],
            OptimizerProfile::Release,
            &tiny_program(),
            &HotPathHints::default(),
        );
        assert_eq!(decisions[0].selected, false);
        assert_eq!(decisions[0].reason, PassSelectionReason::BelowThreshold);
    }

    #[test]
    fn hot_region_selects_expensive_pass() {
        let hints = HotPathHints::with_capacity(4);
        hints.record("hot_entry", 1_000_000, 4);
        let program = Program::wrapped(
            vec![BufferDecl::output("out", 0, DataType::U32).with_count(1)],
            [1, 1, 1],
            vec![Node::store("out", Expr::u32(0), Expr::u32(1))],
        )
        .with_entry_op_id("hot_entry");
        let decisions = select_pass_metadata_for_program(
            &[meta(
                "decode_scan_fuse",
                CostModelFamily::Memory,
                PassPhase::Memory,
                &[],
            )],
            OptimizerProfile::Release,
            &program,
            &hints,
        );
        assert!(decisions[0].selected);
        assert_eq!(decisions[0].reason, PassSelectionReason::HotPathTelemetry);
        assert_eq!(
            decisions[0].priority,
            selection_priority(PassSelectionReason::HotPathTelemetry)
        );
        assert!(decisions[0].priority > selection_priority(PassSelectionReason::PassCostReport));
    }

    #[test]
    fn pass_cost_report_selects_cold_expensive_pass() {
        let report = report_with(pass_metric("decode_scan_fuse", true, 10, 3));
        let decisions = select_pass_metadata_for_program_with_report(
            &[meta(
                "decode_scan_fuse",
                CostModelFamily::Memory,
                PassPhase::Memory,
                &[],
            )],
            OptimizerProfile::Release,
            &tiny_program(),
            &HotPathHints::default(),
            Some(&report),
        );
        assert!(decisions[0].selected);
        assert_eq!(decisions[0].reason, PassSelectionReason::PassCostReport);
        assert_eq!(
            decisions[0].priority,
            selection_priority(PassSelectionReason::PassCostReport)
        );
    }

    #[test]
    fn non_reducing_cost_report_preserves_cold_fallback() {
        let report = report_with(pass_metric("decode_scan_fuse", false, 10, 3));
        let decisions = select_pass_metadata_for_program_with_report(
            &[meta(
                "decode_scan_fuse",
                CostModelFamily::Memory,
                PassPhase::Memory,
                &[],
            )],
            OptimizerProfile::Release,
            &tiny_program(),
            &HotPathHints::default(),
            Some(&report),
        );
        assert!(!decisions[0].selected);
        assert_eq!(decisions[0].reason, PassSelectionReason::BelowThreshold);
        assert_eq!(
            decisions[0].priority,
            selection_priority(PassSelectionReason::BelowThreshold)
        );
    }

    #[test]
    fn selected_pass_closes_over_required_dependencies() {
        let metadata = [
            meta(
                "shape_facts",
                CostModelFamily::Memory,
                PassPhase::Memory,
                &[],
            ),
            meta(
                "memory_optimizer",
                CostModelFamily::Scalar,
                PassPhase::ScalarAlgebra,
                &["shape_facts"],
            ),
        ];
        let decisions = select_pass_metadata_for_program(
            &metadata,
            OptimizerProfile::Release,
            &tiny_program(),
            &HotPathHints::default(),
        );
        assert!(decisions.iter().all(|decision| decision.selected));
        assert_eq!(decisions[0].reason, PassSelectionReason::RequiredDependency);
    }

    #[test]
    fn selected_registered_passes_run_through_scheduler() {
        let program = tiny_program();
        let passes = registered_passes_for_profile_and_program(
            OptimizerProfile::Release,
            &program,
            &HotPathHints::default(),
        )
        .expect("Fix: live registry selection must succeed");
        let optimized = crate::optimizer::PassScheduler::with_passes(passes)
            .run(program)
            .expect("Fix: selected release pass scheduler must converge");
        assert!(optimized.stats().node_count > 0);
    }
}