Skip to main content

weir/fixed_point_graph/
plan.rs

1use super::{FixedPointAnalysisKind, FixedPointForwardGraph};
2
3/// Prepared fixed-point analysis plan: invariant graph buffers plus emitted IR.
4#[derive(Clone, Debug, PartialEq)]
5pub struct FixedPointAnalysisPlan {
6    pub(crate) kind: FixedPointAnalysisKind,
7    pub(crate) graph: FixedPointForwardGraph,
8    pub(crate) program: vyre::ir::Program,
9}
10
11impl FixedPointAnalysisPlan {
12    /// Build a prepared fixed-point plan from packed graph buffers and Program.
13    #[must_use]
14    pub fn new(graph: FixedPointForwardGraph, program: vyre::ir::Program) -> Self {
15        Self::new_for_kind(FixedPointAnalysisKind::Generic, graph, program)
16    }
17
18    /// Build a prepared fixed-point plan with an explicit analysis family.
19    #[must_use]
20    pub fn new_for_kind(
21        kind: FixedPointAnalysisKind,
22        graph: FixedPointForwardGraph,
23        program: vyre::ir::Program,
24    ) -> Self {
25        Self {
26            kind,
27            graph,
28            program,
29        }
30    }
31
32    /// Analysis family this plan was prepared for.
33    #[must_use]
34    pub fn kind(&self) -> FixedPointAnalysisKind {
35        self.kind
36    }
37
38    /// Verify this plan is being used by the analysis family that prepared it.
39    pub fn require_kind(
40        &self,
41        expected: FixedPointAnalysisKind,
42        consumer: &str,
43    ) -> Result<(), String> {
44        if self.kind == expected {
45            return self.graph.require_kind(expected, consumer);
46        }
47        Err(format!(
48            "{consumer} received a {:?} fixed-point plan, expected {:?}. Fix: prepare the plan with the matching Weir analysis constructor.",
49            self.kind, expected
50        ))
51    }
52
53    /// Prepared graph buffers used by this plan.
54    #[must_use]
55    pub fn graph(&self) -> &FixedPointForwardGraph {
56        &self.graph
57    }
58
59    /// Mutable prepared graph buffers used by this plan.
60    #[must_use]
61    pub fn graph_mut(&mut self) -> &mut FixedPointForwardGraph {
62        &mut self.graph
63    }
64
65    /// Emitted Vyre Program used by this plan.
66    #[must_use]
67    pub fn program(&self) -> &vyre::ir::Program {
68        &self.program
69    }
70
71    /// Replace this plan's analysis family and emitted Program.
72    pub fn replace_program_for_kind(
73        &mut self,
74        kind: FixedPointAnalysisKind,
75        program: vyre::ir::Program,
76    ) {
77        self.kind = kind;
78        self.program = program;
79    }
80
81    /// Retained bytes across invariant graph buffers.
82    #[must_use]
83    pub fn retained_graph_bytes(&self) -> usize {
84        self.graph.retained_bytes()
85    }
86
87    /// Stable hash of the normalized graph layout used by this plan.
88    #[must_use]
89    pub fn stable_layout_hash(&self) -> u64 {
90        self.graph.stable_layout_hash()
91    }
92}