vyre-lower 0.6.3

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
//! Bounded descriptor rewrite saturation surface.
//!
//! The release architecture requires e-graph/egglog-family rewrites to
//! have a real integration point. This module provides the deterministic
//! bounded saturation contract used by release evidence and future
//! equality-saturation engines.

use vyre_foundation::ir::BinOp;

use super::body_index::BodyIndex;
use super::literal::ResultAllocator;
use crate::{rewrites, KernelBody, KernelDescriptor, KernelOp, KernelOpKind, LiteralValue};

/// Saturation execution limits.
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct SaturationLimits {
    pub max_iterations: u32,
    pub max_nodes: u32,
}

impl Default for SaturationLimits {
    fn default() -> Self {
        Self {
            max_iterations: 8,
            max_nodes: 65_536,
        }
    }
}

/// Result of bounded saturation.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct SaturationReport {
    pub iterations: u32,
    pub input_ops: usize,
    pub output_ops: usize,
    pub equality_classes: usize,
    pub applied_rewrites: usize,
    pub hit_iteration_limit: bool,
    pub hit_node_limit: bool,
}

/// Run the current deterministic rewrite pipeline under a bounded
/// saturation contract.
///
/// This is intentionally conservative: until the equality-saturation
/// engine owns extraction, the release path gets a bounded fixed-point
/// driver over the descriptor rewrite pipeline. It never returns an
/// unchecked descriptor; callers still run verifier gates around it.
#[must_use]
pub fn saturate_descriptor(
    desc: &KernelDescriptor,
    limits: SaturationLimits,
) -> (KernelDescriptor, SaturationReport) {
    let mut current = desc.clone();
    let input_ops = current.body.ops.len();
    let mut equality_classes = 0usize;
    let mut applied_rewrites = 0usize;
    let mut iterations = 0;
    let mut hit_node_limit = false;
    for _ in 0..limits.max_iterations {
        let algebraic = saturate_local_algebra(&current);
        equality_classes = equality_classes.saturating_add(algebraic.equality_classes);
        applied_rewrites = applied_rewrites.saturating_add(algebraic.applied_rewrites);
        let (next, _) = rewrites::run_all_with_stats(&algebraic.descriptor);
        iterations += 1;
        if next.body.ops.len() as u32 > limits.max_nodes {
            hit_node_limit = true;
            break;
        }
        if next == current {
            current = next;
            break;
        }
        current = next;
    }
    let hit_iteration_limit = iterations == limits.max_iterations;
    let output_ops = current.body.ops.len();
    (
        current,
        SaturationReport {
            iterations,
            input_ops,
            output_ops,
            equality_classes,
            applied_rewrites,
            hit_iteration_limit,
            hit_node_limit,
        },
    )
}

/// Run the non-recursive local algebraic saturation step.
///
/// This is the canonical-pipeline integration point: it performs the
/// e-graph-family reassociation rewrites without calling back into
/// `run_all_with_stats`, so it is safe to compose inside the standard
/// pass sequence.
#[must_use]
pub fn saturate_algebraic_descriptor(
    desc: &KernelDescriptor,
) -> (KernelDescriptor, SaturationReport) {
    let input_ops = desc.body.ops.len();
    let algebraic = saturate_local_algebra(desc);
    let output_ops = algebraic.descriptor.body.ops.len();
    (
        algebraic.descriptor,
        SaturationReport {
            iterations: 1,
            input_ops,
            output_ops,
            equality_classes: algebraic.equality_classes,
            applied_rewrites: algebraic.applied_rewrites,
            hit_iteration_limit: false,
            hit_node_limit: false,
        },
    )
}

struct AlgebraicSaturation {
    descriptor: KernelDescriptor,
    equality_classes: usize,
    applied_rewrites: usize,
}

fn saturate_local_algebra(desc: &KernelDescriptor) -> AlgebraicSaturation {
    let mut descriptor = desc.clone();
    let mut stats = AlgebraicStats::default();
    descriptor.body = saturate_body(descriptor.body, &mut stats);
    AlgebraicSaturation {
        descriptor,
        equality_classes: stats.equality_classes,
        applied_rewrites: stats.applied_rewrites,
    }
}

#[derive(Default)]
struct AlgebraicStats {
    equality_classes: usize,
    applied_rewrites: usize,
}

fn saturate_body(mut body: KernelBody, stats: &mut AlgebraicStats) -> KernelBody {
    body.child_bodies = body
        .child_bodies
        .into_iter()
        .map(|child| saturate_body(child, stats))
        .collect();

    let index = BodyIndex::new(&body);
    let mut allocator = ResultAllocator::for_body_tree(&body);
    let mut new_ops = Vec::with_capacity(body.ops.len());
    for current_op in &body.ops {
        let mut op = current_op.clone();
        if let Some(rewrite) = reassociate_constant_chain(&op, &body, &index) {
            let literal_result =
                allocator.push_literal(&mut new_ops, &mut body.literals, rewrite.literal);
            op.operands = vec![rewrite.base, literal_result];
            stats.equality_classes = stats.equality_classes.saturating_add(1);
            stats.applied_rewrites = stats.applied_rewrites.saturating_add(1);
        }
        new_ops.push(op);
    }
    body.ops = new_ops;
    body
}

struct Reassociated {
    base: u32,
    literal: LiteralValue,
}

fn reassociate_constant_chain(
    op: &KernelOp,
    body: &KernelBody,
    index: &BodyIndex,
) -> Option<Reassociated> {
    let KernelOpKind::BinOpKind(
        kind @ (BinOp::Add
        | BinOp::Mul
        | BinOp::BitAnd
        | BinOp::BitOr
        | BinOp::BitXor
        | BinOp::And
        | BinOp::Or),
    ) = &op.kind
    else {
        return None;
    };
    let kind = *kind;
    let left = *op.operands.first()?;
    let right = *op.operands.get(1)?;
    if matches!(kind, BinOp::And | BinOp::Or) {
        return reassociate_bool_constant_chain(kind, left, right, body, index);
    }
    let (inner_result, outer_const) = split_value_const(left, right, body, index)
        .or_else(|| split_value_const(right, left, body, index))?;
    let inner = index.producer(body, inner_result)?;
    let KernelOpKind::BinOpKind(inner_kind) = &inner.kind else {
        return None;
    };
    if *inner_kind != kind {
        return None;
    }
    let inner_left = *inner.operands.first()?;
    let inner_right = *inner.operands.get(1)?;
    let (base, inner_const) = split_value_const(inner_left, inner_right, body, index)
        .or_else(|| split_value_const(inner_right, inner_left, body, index))?;
    let constant = match kind {
        BinOp::Add => inner_const.checked_add(outer_const)?,
        BinOp::Mul => inner_const.checked_mul(outer_const)?,
        BinOp::BitAnd => inner_const & outer_const,
        BinOp::BitOr => inner_const | outer_const,
        BinOp::BitXor => inner_const ^ outer_const,
        _ => return None,
    };
    Some(Reassociated {
        base,
        literal: LiteralValue::U32(constant),
    })
}

fn split_value_const(
    value: u32,
    maybe_const: u32,
    body: &KernelBody,
    index: &BodyIndex,
) -> Option<(u32, u32)> {
    index
        .u32_lit(body, maybe_const)
        .map(|constant| (value, constant))
}

fn reassociate_bool_constant_chain(
    kind: BinOp,
    left: u32,
    right: u32,
    body: &KernelBody,
    index: &BodyIndex,
) -> Option<Reassociated> {
    let (inner_result, outer_const) = split_value_bool(left, right, body, index)
        .or_else(|| split_value_bool(right, left, body, index))?;
    let inner = index.producer(body, inner_result)?;
    let KernelOpKind::BinOpKind(inner_kind) = &inner.kind else {
        return None;
    };
    if *inner_kind != kind {
        return None;
    }
    let inner_left = *inner.operands.first()?;
    let inner_right = *inner.operands.get(1)?;
    let (base, inner_const) = split_value_bool(inner_left, inner_right, body, index)
        .or_else(|| split_value_bool(inner_right, inner_left, body, index))?;
    let literal = match kind {
        BinOp::And => LiteralValue::Bool(inner_const && outer_const),
        BinOp::Or => LiteralValue::Bool(inner_const || outer_const),
        _ => return None,
    };
    Some(Reassociated { base, literal })
}

fn split_value_bool(
    value: u32,
    maybe_const: u32,
    body: &KernelBody,
    index: &BodyIndex,
) -> Option<(u32, bool)> {
    index
        .bool_lit(body, maybe_const)
        .map(|constant| (value, constant))
}

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

    use super::*;

    #[test]
    fn bounded_saturation_reports_progress_without_unbounded_loop() {
        let desc = KernelDescriptor {
            id: "sat".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![LiteralValue::U32(1)],
            },
        };
        let (_, report) = saturate_descriptor(
            &desc,
            SaturationLimits {
                max_iterations: 2,
                max_nodes: 16,
            },
        );
        assert!(report.iterations >= 1);
        assert_eq!(report.input_ops, 1);
    }

    #[test]
    fn saturation_reassociates_constant_add_chain() {
        let desc = KernelDescriptor {
            id: "sat_add_chain".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![1],
                        result: Some(1),
                    },
                    KernelOp {
                        kind: KernelOpKind::Literal,
                        operands: vec![2],
                        result: Some(2),
                    },
                    KernelOp {
                        kind: KernelOpKind::BinOpKind(BinOp::Add),
                        operands: vec![0, 1],
                        result: Some(3),
                    },
                    KernelOp {
                        kind: KernelOpKind::BinOpKind(BinOp::Add),
                        operands: vec![3, 2],
                        result: Some(4),
                    },
                ],
                child_bodies: vec![],
                literals: vec![
                    LiteralValue::U32(10),
                    LiteralValue::U32(2),
                    LiteralValue::U32(3),
                ],
            },
        };
        let (_, report) = saturate_descriptor(
            &desc,
            SaturationLimits {
                max_iterations: 2,
                max_nodes: 64,
            },
        );
        assert!(report.equality_classes >= 1);
        assert!(report.applied_rewrites >= 1);
    }

    #[test]
    fn saturation_reassociates_constant_bitwise_chain() {
        let desc = KernelDescriptor {
            id: "sat_bitwise_chain".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![1],
                        result: Some(1),
                    },
                    KernelOp {
                        kind: KernelOpKind::Literal,
                        operands: vec![2],
                        result: Some(2),
                    },
                    KernelOp {
                        kind: KernelOpKind::BinOpKind(BinOp::BitOr),
                        operands: vec![0, 1],
                        result: Some(3),
                    },
                    KernelOp {
                        kind: KernelOpKind::BinOpKind(BinOp::BitOr),
                        operands: vec![3, 2],
                        result: Some(4),
                    },
                ],
                child_bodies: vec![],
                literals: vec![
                    LiteralValue::U32(0b0001),
                    LiteralValue::U32(0b0010),
                    LiteralValue::U32(0b0100),
                ],
            },
        };
        let (_, report) = saturate_descriptor(
            &desc,
            SaturationLimits {
                max_iterations: 2,
                max_nodes: 64,
            },
        );
        assert!(report.equality_classes >= 1);
        assert!(report.applied_rewrites >= 1);
    }

    #[test]
    fn saturation_reassociates_constant_boolean_chain() {
        let desc = KernelDescriptor {
            id: "sat_bool_chain".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![1],
                        result: Some(1),
                    },
                    KernelOp {
                        kind: KernelOpKind::BinOpKind(BinOp::And),
                        operands: vec![0, 1],
                        result: Some(2),
                    },
                    KernelOp {
                        kind: KernelOpKind::Literal,
                        operands: vec![2],
                        result: Some(3),
                    },
                    KernelOp {
                        kind: KernelOpKind::BinOpKind(BinOp::And),
                        operands: vec![2, 3],
                        result: Some(4),
                    },
                ],
                child_bodies: vec![],
                literals: vec![
                    LiteralValue::Bool(true),
                    LiteralValue::Bool(true),
                    LiteralValue::Bool(false),
                ],
            },
        };
        let (_, report) = saturate_descriptor(
            &desc,
            SaturationLimits {
                max_iterations: 2,
                max_nodes: 64,
            },
        );
        assert!(report.equality_classes >= 1);
        assert!(report.applied_rewrites >= 1);
    }
}