weirflow 0.1.0

GPU-first dataflow analysis primitives for Vyre and Santh compiler pipelines.
Documentation
use super::{FixedPointAnalysisKind, FixedPointForwardGraph};

/// Prepared fixed-point analysis plan: invariant graph buffers plus emitted IR.
#[derive(Clone, Debug, PartialEq)]
pub struct FixedPointAnalysisPlan {
    pub(crate) kind: FixedPointAnalysisKind,
    pub(crate) graph: FixedPointForwardGraph,
    pub(crate) program: vyre::ir::Program,
}

impl FixedPointAnalysisPlan {
    /// Build a prepared fixed-point plan from packed graph buffers and Program.
    #[must_use]
    pub fn new(graph: FixedPointForwardGraph, program: vyre::ir::Program) -> Self {
        Self::new_for_kind(FixedPointAnalysisKind::Generic, graph, program)
    }

    /// Build a prepared fixed-point plan with an explicit analysis family.
    #[must_use]
    pub fn new_for_kind(
        kind: FixedPointAnalysisKind,
        graph: FixedPointForwardGraph,
        program: vyre::ir::Program,
    ) -> Self {
        Self {
            kind,
            graph,
            program,
        }
    }

    /// Analysis family this plan was prepared for.
    #[must_use]
    pub fn kind(&self) -> FixedPointAnalysisKind {
        self.kind
    }

    /// Verify this plan is being used by the analysis family that prepared it.
    pub fn require_kind(
        &self,
        expected: FixedPointAnalysisKind,
        consumer: &str,
    ) -> Result<(), String> {
        if self.kind == expected {
            return self.graph.require_kind(expected, consumer);
        }
        Err(format!(
            "{consumer} received a {:?} fixed-point plan, expected {:?}. Fix: prepare the plan with the matching Weir analysis constructor.",
            self.kind, expected
        ))
    }

    /// Prepared graph buffers used by this plan.
    #[must_use]
    pub fn graph(&self) -> &FixedPointForwardGraph {
        &self.graph
    }

    /// Mutable prepared graph buffers used by this plan.
    #[must_use]
    pub fn graph_mut(&mut self) -> &mut FixedPointForwardGraph {
        &mut self.graph
    }

    /// Emitted Vyre Program used by this plan.
    #[must_use]
    pub fn program(&self) -> &vyre::ir::Program {
        &self.program
    }

    /// Replace this plan's analysis family and emitted Program.
    pub fn replace_program_for_kind(
        &mut self,
        kind: FixedPointAnalysisKind,
        program: vyre::ir::Program,
    ) {
        self.kind = kind;
        self.program = program;
    }

    /// Retained bytes across invariant graph buffers.
    #[must_use]
    pub fn retained_graph_bytes(&self) -> usize {
        self.graph.retained_bytes()
    }

    /// Stable hash of the normalized graph layout used by this plan.
    #[must_use]
    pub fn stable_layout_hash(&self) -> u64 {
        self.graph.stable_layout_hash()
    }
}