Skip to main content

Module optimize

Module optimize 

Source
Expand description

optimize_graph — the new “plan IS the graph” entry point.

Phase A PR-A3a/A4 of the “plan IS the graph” rebuild (../../docs/session-prompts/plan-is-graph-rebuild.md, the PR-A3/A4 lines + the “Representation — DECIDED 2026-06-15” blockquote, incl. the architect-approved arm-0 temporary lowering).

§What this is

optimize_graph is the new optimization entry point: it transforms a Graph in place into the bounded multi-path form and returns a transient OptimizedGraph view whose dispatch order is the arm-0 single-route lowering of fuel-graph’s extract_runs (PR-A2/A4). The optimized form lives in the graph — a graph with zero Op::Branch nodes is exactly today’s single-route graph.

§PR-A4 — the first pathfinder (deliberate-fork seed)

PlacementForkPathfinder is the first real pathfinder: where the placement DP admitted a kernel-bearing node with ≥2 distinct (backend, device) placements that has a producer (the diverge) and exactly one consumer (the reconverge), it records ONE Op::Branch — arm-0 = the DP winner (the route realize uses today), arm-1 = the runner-up clone (orphaned, read only by the Branch). It emits a branch only at a genuine placement choice, never at ordinary DAG fan-out, so the fewness gate holds. A CPU-only build (one placement per node) emits zero branches ⇒ today’s single-route graph.

OptimizedGraph::dispatch_order is the architect-approved arm-0 single-route lowering: it follows arm 0 through every branch and skips the other arms’ runs, so a branched graph realizes to the same result as before (arm-0 = the winner). The Phase-C runtime picker is what later selects non-arm-0 arms.

§A3a origin scope (ADDITIVE — proved equivalence, deleted nothing)

This entry point landed in the split A3a: introduced alongside the old path and proved equivalent before anything old was removed. PR-A3b-1 then made it the default realize path, and PR-A3b-2 deleted PlanStore and the legacy compile_plan route-picking dispatch entirely (optimize_graph is now the only path). As originally introduced in A3a:

  • There were no pathfinders yet — the first (PlacementForkPathfinder) landed in PR-A4. A graph with no competing routes is already its own single-route plan, so optimize_graph introduces zero Op::Branch nodes for it.
  • It reuses the existing placement / cost / target_backend annotation machinery wholesale by driving crate::plan::compile_plan over the same execution_plan order the bridge uses — the placement DP, cost composer, filter chain, and fail-fast missing-binding diagnostics are not reinvented. The point of A3a is to establish the new entry point
    • in-place form, not new optimization.
  • crate::plan::compile_plan + crate::plan::ExecutionPlan are reused (the latter as a transient by-product). PR-A3b-1 wired optimize_graph in as the default realize path; PR-A3b-2 made it the ONLY path — the legacy compile_plan/PlanStore route-picking dispatch and the identity-keyed plan store are deleted, and optimize_graph now surfaces its internal ExecutionPlan so the bridge’s stamp/residency/layout passes reuse it (one compile_plan per realize).

§The equivalence gate

For a graph with no competing routes, optimize_graph leaves zero Op::Branch nodes and the extract_runs / lower_run dispatch order equals today’s crate::plan::compile_plan(...).order — the exact sequence the production executor walks. The order today is simply the execution_plan topo order compile_plan is handed (it copies it into ExecutionPlan::order verbatim); for a branchless, single-residency graph the run extractor produces exactly one run whose members are that same topo order. The gate asserts that equality exactly (same NodeIds, same order) — see tests::equivalence_gate_branchless_order_matches_compile_plan.

Structs§

OptimizedGraph
The transient view optimize_graph returns — the realize-roots it optimized for plus the topology generation it ran under. It is not stored on the graph; the optimized form is the graph itself (with its Op::Branch decision points, of which A3a emits none).

Functions§

optimize_graph
Optimize graph in place into the “plan IS the graph” form and return the transient OptimizedGraph lowering view.
optimize_graph_with_runtime_fusion
optimize_graph with the runtime-fusion pathfinder registered (PassRegistry::default_passes_with_runtime_fusion): adopted (Tier-2 / JIT-synthesized) fused ops get their gated Op::Branch arms emitted before placement runs. This is the production realize path’s entry once a process can adopt runtime kernels; optimize_graph stays runtime-op-free so bare-graph callers and tests never scan the process-global sidecar.