use super::graph::{live_reverse_control_csr, live_reverse_control_csr_into};
use super::live_step;
use vyre_primitives::graph::program_graph::ProgramGraphShape;
pub fn prepare_live_graph(
node_count: u32,
edge_offsets: &[u32],
edge_targets: &[u32],
edge_kind_mask: &[u32],
) -> Result<crate::fixed_point_graph::FixedPointForwardGraph, String> {
let nodes = usize::try_from(node_count).map_err(|_| {
format!("live_closure node_count={node_count} cannot fit usize. Fix: shard the graph before dispatch.")
})?;
let (rev_offsets, rev_targets, rev_masks) =
live_reverse_control_csr(nodes, edge_offsets, edge_targets, edge_kind_mask)?;
crate::fixed_point_graph::FixedPointForwardGraph::new_for_kind(
crate::fixed_point_graph::FixedPointAnalysisKind::Live,
"live_closure",
node_count,
&rev_offsets,
&rev_targets,
&rev_masks,
)
}
pub fn prepare_live_graph_with_scratch(
node_count: u32,
edge_offsets: &[u32],
edge_targets: &[u32],
edge_kind_mask: &[u32],
scratch: &mut crate::fixed_point_scratch::FixedPointScratch,
) -> Result<crate::fixed_point_graph::FixedPointForwardGraph, String> {
let nodes = usize::try_from(node_count).map_err(|_| {
format!("live_closure node_count={node_count} cannot fit usize. Fix: shard the graph before dispatch.")
})?;
live_reverse_control_csr_into(nodes, edge_offsets, edge_targets, edge_kind_mask, scratch)?;
crate::fixed_point_graph::FixedPointForwardGraph::new_for_kind(
crate::fixed_point_graph::FixedPointAnalysisKind::Live,
"live_closure",
node_count,
&scratch.reverse_offsets,
&scratch.reverse_targets,
&scratch.reverse_masks,
)
}
pub fn prepare_live_graph_into(
graph: &mut crate::fixed_point_graph::FixedPointForwardGraph,
node_count: u32,
edge_offsets: &[u32],
edge_targets: &[u32],
edge_kind_mask: &[u32],
) -> Result<(), String> {
let nodes = usize::try_from(node_count).map_err(|_| {
format!("live_closure node_count={node_count} cannot fit usize. Fix: shard the graph before dispatch.")
})?;
let (rev_offsets, rev_targets, rev_masks) =
live_reverse_control_csr(nodes, edge_offsets, edge_targets, edge_kind_mask)?;
graph.replace_for_kind(
crate::fixed_point_graph::FixedPointAnalysisKind::Live,
"live_closure",
node_count,
&rev_offsets,
&rev_targets,
&rev_masks,
)
}
pub fn prepare_live_graph_into_with_scratch(
graph: &mut crate::fixed_point_graph::FixedPointForwardGraph,
node_count: u32,
edge_offsets: &[u32],
edge_targets: &[u32],
edge_kind_mask: &[u32],
scratch: &mut crate::fixed_point_scratch::FixedPointScratch,
) -> Result<(), String> {
let nodes = usize::try_from(node_count).map_err(|_| {
format!("live_closure node_count={node_count} cannot fit usize. Fix: shard the graph before dispatch.")
})?;
live_reverse_control_csr_into(nodes, edge_offsets, edge_targets, edge_kind_mask, scratch)?;
graph.replace_for_kind(
crate::fixed_point_graph::FixedPointAnalysisKind::Live,
"live_closure",
node_count,
&scratch.reverse_offsets,
&scratch.reverse_targets,
&scratch.reverse_masks,
)
}
pub fn prepare_live_plan(
node_count: u32,
edge_offsets: &[u32],
edge_targets: &[u32],
edge_kind_mask: &[u32],
) -> Result<crate::fixed_point_graph::FixedPointAnalysisPlan, String> {
let graph = prepare_live_graph(node_count, edge_offsets, edge_targets, edge_kind_mask)?;
let program = live_step(
ProgramGraphShape::new(graph.node_count, graph.edge_count),
"fin",
"fout",
);
Ok(
crate::fixed_point_graph::FixedPointAnalysisPlan::new_for_kind(
crate::fixed_point_graph::FixedPointAnalysisKind::Live,
graph,
program,
),
)
}
pub fn prepare_live_plan_with_scratch(
node_count: u32,
edge_offsets: &[u32],
edge_targets: &[u32],
edge_kind_mask: &[u32],
scratch: &mut crate::fixed_point_scratch::FixedPointScratch,
) -> Result<crate::fixed_point_graph::FixedPointAnalysisPlan, String> {
let graph = prepare_live_graph_with_scratch(
node_count,
edge_offsets,
edge_targets,
edge_kind_mask,
scratch,
)?;
let program = live_step(
ProgramGraphShape::new(graph.node_count, graph.edge_count),
"fin",
"fout",
);
Ok(
crate::fixed_point_graph::FixedPointAnalysisPlan::new_for_kind(
crate::fixed_point_graph::FixedPointAnalysisKind::Live,
graph,
program,
),
)
}
pub fn prepare_live_plan_into(
plan: &mut crate::fixed_point_graph::FixedPointAnalysisPlan,
node_count: u32,
edge_offsets: &[u32],
edge_targets: &[u32],
edge_kind_mask: &[u32],
) -> Result<(), String> {
plan.require_kind(
crate::fixed_point_graph::FixedPointAnalysisKind::Live,
"prepare_live_plan_into",
)?;
prepare_live_graph_into(
plan.graph_mut(),
node_count,
edge_offsets,
edge_targets,
edge_kind_mask,
)?;
let program = live_step(
ProgramGraphShape::new(plan.graph().node_count, plan.graph().edge_count),
"fin",
"fout",
);
plan.replace_program_for_kind(
crate::fixed_point_graph::FixedPointAnalysisKind::Live,
program,
);
Ok(())
}
pub fn prepare_live_plan_into_with_scratch(
plan: &mut crate::fixed_point_graph::FixedPointAnalysisPlan,
node_count: u32,
edge_offsets: &[u32],
edge_targets: &[u32],
edge_kind_mask: &[u32],
scratch: &mut crate::fixed_point_scratch::FixedPointScratch,
) -> Result<(), String> {
plan.require_kind(
crate::fixed_point_graph::FixedPointAnalysisKind::Live,
"prepare_live_plan_into_with_scratch",
)?;
prepare_live_graph_into_with_scratch(
plan.graph_mut(),
node_count,
edge_offsets,
edge_targets,
edge_kind_mask,
scratch,
)?;
let program = live_step(
ProgramGraphShape::new(plan.graph().node_count, plan.graph().edge_count),
"fin",
"fout",
);
plan.replace_program_for_kind(
crate::fixed_point_graph::FixedPointAnalysisKind::Live,
program,
);
Ok(())
}