weirflow 0.1.0

GPU-first dataflow analysis primitives for Vyre and Santh compiler pipelines.
Documentation
use super::*;

#[test]
fn reaching_defs_step_emits_program() {
    let shape = ProgramGraphShape::new(64, 64);
    let p = reaching_defs_step(shape, "fin", "fout");
    assert!(
        !p.entry().is_empty(),
        "weir::reaching: forward-step Program body must not be empty"
    );
}

#[test]
fn reaching_defs_step_is_deterministic() {
    let shape = ProgramGraphShape::new(64, 64);
    let p1 = reaching_defs_step(shape, "fin", "fout");
    let p2 = reaching_defs_step(shape, "fin", "fout");
    assert_eq!(
        p1.entry().len(),
        p2.entry().len(),
        "weir::reaching: reaching_defs_step must be deterministic"
    );
}

#[test]
fn reaching_defs_op_id_is_stable() {
    assert_eq!(
            OP_ID, "weir::reaching",
            "weir::reaching: op id is the consumption-contract key  -  must not change without a coordinated migration"
        );
}

#[test]
fn reaching_soundness_is_exact() {
    use super::super::soundness::SoundnessTagged;
    assert_eq!(
        ReachingDefs.soundness(),
        super::super::soundness::Soundness::Exact,
        "weir::reaching: classical may-analysis is Exact on a sound CFG"
    );
}

#[test]
fn reaching_closure_into_reuses_output_storage_across_iterations() {
    let calls = std::cell::Cell::new(0_u32);
    let saw_reused_slot = std::cell::Cell::new(false);
    let dispatch =
        |_: &Program, inputs: &[&[u8]], _: Option<[u32; 3]>, outputs: &mut Vec<Vec<u8>>| {
            if inputs.len() < 6 {
                return crate::fixed_point_closure::fallback_bitset_equal_dispatch(inputs, outputs);
            }
            let call = calls.get();
            if call > 0 && outputs.len() == 1 {
                saw_reused_slot.set(true);
            }
            calls.set(call + 1);
            let frontier = u32::from_le_bytes(inputs[5][..4].try_into().unwrap());
            let next = if frontier & 1 != 0 {
                frontier | 0b10
            } else {
                frontier
            };
            if outputs.is_empty() {
                outputs.push(Vec::new());
            }
            outputs[0].clear();
            outputs[0].extend_from_slice(&next.to_le_bytes());
            Ok(())
        };

    let result = reaching_closure_borrowed_into_via(
        &dispatch,
        2,
        &[0, 1, 1],
        &[1],
        &[vyre_primitives::predicate::edge_kind::CONTROL],
        &[0b01],
        4,
    )
    .expect("reaching closure into dispatch must converge");

    assert_eq!(result, vec![0b11]);
    assert_eq!(calls.get(), 2);
    assert!(saw_reused_slot.get());
}

#[test]
fn reaching_closure_with_scratch_reuses_edge_kind_filter_buffer() {
    let dispatch =
        |_: &Program, inputs: &[&[u8]], _: Option<[u32; 3]>, outputs: &mut Vec<Vec<u8>>| {
            if inputs.len() < 6 {
                return crate::fixed_point_closure::fallback_bitset_equal_dispatch(inputs, outputs);
            }
            if outputs.is_empty() {
                outputs.push(Vec::new());
            }
            outputs[0].clear();
            outputs[0].extend_from_slice(inputs[5]);
            Ok(())
        };
    let mut scratch = crate::fixed_point_scratch::FixedPointScratch::default();

    reaching_closure_borrowed_into_with_scratch_via(
        &dispatch,
        2,
        &[0, 1, 1],
        &[1],
        &[vyre_primitives::predicate::edge_kind::CONTROL],
        &[0b01],
        1,
        &mut scratch,
    )
    .expect("first reaching closure must converge");
    let ptr = scratch.edge_kind_masks.as_ptr() as usize;
    let capacity = scratch.edge_kind_mask_capacity();

    reaching_closure_borrowed_into_with_scratch_via(
        &dispatch,
        2,
        &[0, 1, 1],
        &[1],
        &[vyre_primitives::predicate::edge_kind::CONTROL],
        &[0b01],
        1,
        &mut scratch,
    )
    .expect("second reaching closure must converge");

    let density = scratch.frontier_density();
    assert_eq!(density.domain_bits, 2);
    assert_eq!(density.samples, 2);
    assert_eq!(density.iterations, 1);
    assert_eq!(density.last_active_bits, 1);
    assert_eq!(density.last_delta_bits, 0);
    assert_eq!(density.last_active_density_ppm(), 500_000);
    assert_eq!(scratch.edge_kind_masks.as_ptr() as usize, ptr);
    assert!(scratch.edge_kind_mask_capacity() >= capacity);
}