weirflow 0.1.0

GPU-first dataflow analysis primitives for Vyre and Santh compiler pipelines.
Documentation
use std::{fs, path::Path};

const ANALYSIS_FILES: &[&str] = &[
    "src/reaching.rs",
    "src/reaching/closure.rs",
    "src/reaching/resident.rs",
    "src/live.rs",
    "src/live/closure.rs",
    "src/live/resident.rs",
    "src/slice.rs",
    "src/slice/closure.rs",
    "src/slice/resident.rs",
    "src/ifds.rs",
    "src/ifds/closure.rs",
    "src/points_to.rs",
    "src/points_to/closure.rs",
    "src/points_to/resident.rs",
];

const SCRATCH_REQUIRED_APIS: &[(&str, &str)] = &[
    (
        "reaching_closure_borrowed_into_via",
        "reaching_closure_borrowed_into_with_scratch_via",
    ),
    (
        "live_closure_borrowed_into_via",
        "live_closure_borrowed_into_with_scratch_via",
    ),
    (
        "slice_closure_borrowed_into_via",
        "slice_closure_borrowed_into_with_scratch_via",
    ),
    (
        "ifds_reach_closure_borrowed_into_via",
        "ifds_reach_closure_borrowed_into_with_scratch_via",
    ),
    (
        "subset_closure_borrowed_into_via",
        "subset_closure_borrowed_into_with_scratch_via",
    ),
];

const REUSABLE_RESIDENT_FRONTIER_APIS: &[(&str, &str)] = &[
    (
        "src/reaching/resident.rs",
        "reaching_closure_resident_plan_with_reusable_frontier_scratch",
    ),
    (
        "src/live/resident.rs",
        "live_closure_resident_plan_with_reusable_frontier_scratch",
    ),
    (
        "src/points_to/resident.rs",
        "subset_closure_resident_plan_with_reusable_frontier_scratch",
    ),
    (
        "src/slice/resident.rs",
        "slice_closure_resident_plan_with_reusable_frontier_scratch",
    ),
];

const RESIDENT_FRONTIER_DRIVER_DELEGATES: &[(&str, &str, &str)] = &[
    // Every analysis family delegates through its own *frontier_resources_into
    // helper which contains the shared upload/swap hot-loop pattern.
    (
        "src/reaching/resident.rs",
        "reaching_closure_resident_plan_with_backend_scratch_into",
        "reaching_closure_resident_plan_with_frontier_resources_into",
    ),
    (
        "src/reaching/resident.rs",
        "reaching_closure_resident_plan_with_reusable_frontier_scratch_into",
        "reaching_closure_resident_plan_with_frontier_resources_into",
    ),
    (
        "src/live/resident.rs",
        "live_closure_resident_plan_with_backend_scratch_into",
        "live_closure_resident_plan_with_frontier_resources_into",
    ),
    (
        "src/live/resident.rs",
        "live_closure_resident_plan_with_reusable_frontier_scratch_into",
        "live_closure_resident_plan_with_frontier_resources_into",
    ),
    (
        "src/points_to/resident.rs",
        "subset_closure_resident_plan_with_backend_scratch_into",
        "subset_closure_resident_plan_with_frontier_resources_into",
    ),
    (
        "src/points_to/resident.rs",
        "subset_closure_resident_plan_with_reusable_frontier_scratch_into",
        "subset_closure_resident_plan_with_frontier_resources_into",
    ),
    (
        "src/slice/resident.rs",
        "slice_closure_resident_plan_with_backend_scratch_into",
        "slice_closure_resident_plan_with_frontier_resources_into",
    ),
    (
        "src/slice/resident.rs",
        "slice_closure_resident_plan_with_reusable_frontier_scratch_into",
        "slice_closure_resident_plan_with_frontier_resources_into",
    ),
];

const CACHED_GRID_CONFIG_RESIDENT_FILES: &[&str] = &[
    "src/reaching/resident.rs",
    "src/live/resident.rs",
    "src/points_to/resident.rs",
    "src/slice/resident.rs",
];

const RESIDENT_SEQUENCE_WINDOW_APIS: &[(&str, &str)] = &[
    (
        "src/reaching/resident.rs",
        "reaching_closure_resident_plan_with_reusable_frontier_scratch_sequence_window_into",
    ),
    (
        "src/live/resident.rs",
        "live_closure_resident_plan_with_reusable_frontier_scratch_sequence_window_into",
    ),
    (
        "src/points_to/resident.rs",
        "subset_closure_resident_plan_with_reusable_frontier_scratch_sequence_window_into",
    ),
    (
        "src/slice/resident.rs",
        "slice_closure_resident_plan_with_reusable_frontier_scratch_sequence_window_into",
    ),
];

const RESIDENT_SEQUENCE_WINDOW_FACADE_APIS: &[&str] = &[
    "reaching_reusing_frontier_sequence_window",
    "reaching_reusing_frontier_sequence_window_into",
    "live_reusing_frontier_sequence_window",
    "live_reusing_frontier_sequence_window_into",
    "points_to_subset_reusing_frontier_sequence_window",
    "points_to_subset_reusing_frontier_sequence_window_into",
    "slice_reusing_frontier_sequence_window",
    "slice_reusing_frontier_sequence_window_into",
];

const RESIDENT_SEQUENCE_WINDOW_BATCH_APIS: &[&str] = &[
    "reaching_sequence_window",
    "reaching_sequence_window_into",
    "live_sequence_window",
    "live_sequence_window_into",
    "points_to_subset_sequence_window",
    "points_to_subset_sequence_window_into",
    "slice_sequence_window",
    "slice_sequence_window_into",
];

fn crate_root() -> &'static Path {
    Path::new(env!("CARGO_MANIFEST_DIR"))
}

fn read_source(relative: &str) -> String {
    let path = crate_root().join(relative);
    fs::read_to_string(&path).unwrap_or_else(|err| {
        panic!(
            "failed to read {}; Fix: keep Weir scratch contract paths current: {err}",
            path.display()
        )
    })
}

fn function_bodies_with_visibility(source: &str) -> Vec<(&str, &str)> {
    let mut bodies = Vec::new();
    let mut search_from = 0usize;
    while let Some((relative_start, prefix_len)) = ["pub fn ", "pub(crate) fn "]
        .iter()
        .filter_map(|prefix| {
            source[search_from..]
                .find(prefix)
                .map(|offset| (offset, prefix.len()))
        })
        .min_by_key(|(offset, _)| *offset)
    {
        let fn_start = search_from + relative_start;
        let name_start = fn_start + prefix_len;
        let name_end = source[name_start..]
            .find(|ch: char| !(ch == '_' || ch.is_ascii_alphanumeric()))
            .map(|offset| name_start + offset)
            .expect("public function name must terminate");
        let Some(open_relative) = source[name_end..].find('{') else {
            break;
        };
        let body_start = name_end + open_relative;
        let mut depth = 0usize;
        let mut body_end = body_start;
        for (offset, ch) in source[body_start..].char_indices() {
            match ch {
                '{' => depth += 1,
                '}' => {
                    depth -= 1;
                    if depth == 0 {
                        body_end = body_start + offset + 1;
                        break;
                    }
                }
                _ => {}
            }
        }
        assert!(
            body_end > body_start,
            "unterminated public function body near {}; Fix: keep this source contract parser aligned with Rust syntax",
            &source[fn_start..name_end]
        );
        bodies.push((&source[name_start..name_end], &source[body_start..body_end]));
        search_from = body_end;
    }
    bodies
}

fn function_body<'a>(source: &'a str, name: &str) -> &'a str {
    let signature = format!("fn {name}");
    let fn_start = source.find(&signature).unwrap_or_else(|| {
        panic!("missing function {name}; Fix: keep resident frontier upload contracts current")
    });
    let open_relative = source[fn_start..].find('{').unwrap_or_else(|| {
        panic!("missing function body for {name}; Fix: keep this source contract parser aligned")
    });
    let body_start = fn_start + open_relative;
    let mut depth = 0usize;
    for (offset, ch) in source[body_start..].char_indices() {
        match ch {
            '{' => depth += 1,
            '}' => {
                depth -= 1;
                if depth == 0 {
                    return &source[body_start..body_start + offset + 1];
                }
            }
            _ => {}
        }
    }
    panic!("unterminated function body for {name}; Fix: keep this source contract parser aligned")
}

#[path = "fixed_point_scratch_contract/public_scratch_api_contracts.rs"]
mod public_scratch_api_contracts;
#[path = "fixed_point_scratch_contract/resident_frontier_contracts.rs"]
mod resident_frontier_contracts;
#[path = "fixed_point_scratch_contract/sequence_window_contracts.rs"]
mod sequence_window_contracts;