Skip to main content

optimize_graph

Function optimize_graph 

Source
pub fn optimize_graph(
    graph: &mut Graph,
    roots: &[NodeId],
    bindings_table: &KernelBindingTable,
    opts: &PlanOptions<'_>,
) -> Result<OptimizedGraph>
Expand description

Optimize graph in place into the “plan IS the graph” form and return the transient OptimizedGraph lowering view.

roots are the realize targets (the bridge’s targets); bindings_table is the kernel registry the placement/cost machinery queries (production passes crate::dispatch::global_bindings; tests pass a local table); opts is the same PlanOptions the bridge builds for compile_plan.

§Behavior

optimize_graph:

  1. Derives the dispatch order via fuel_graph::opt::execution_plan — exactly the order the bridge feeds compile_plan today (data-flow topo refined by destructive-op ordering edges).
  2. Drives compile_plan over that order to reuse the existing placement DP / cost composer / filter chain and to fail-fast at build time on a missing binding or absent device context (validate-at-graph-build-time per the working agreement). Its crate::plan::ExecutionPlan is a transient by-product here — the source of truth is the graph.
  3. Runs the PR-A4 deliberate-fork pathfinder (PlacementForkPathfinder): records ONE Op::Branch per genuine ≥2-placement fork (arm-0 = winner, arm-1 = runner-up). A graph with no competing routes gets zero branches — it is already its own single-route plan.

The returned view’s OptimizedGraph::dispatch_order is the arm-0 single-route lowering; for any branchless graph it reproduces compile_plan(...).order exactly — the equivalence gate.

§The internal ExecutionPlan (Step D — no longer returned)

optimize_graph drives compile_plan internally for placement/cost/validation; the resulting ExecutionPlan is a transient, optimizer-internal accumulator the stamp / residency / layout-fixup passes + the lock-step rankers read below. It is NOT returned or threaded anywhere — the durable output is the graph’s target_backend stamps + Op::Branch arms. The executor reads the graph + re-derives any per-arm candidate from the binding registry at realize time, so the plan never becomes a dispatch-time authority.