Skip to content

Composable typed transformations for optimization passes (Kamodo pattern) #73

@avrabe

Description

@avrabe

Context

NASA Kamodo represents datasets as mathematical functions that can be composed, with automatic unit conversion. The core insight: transformations are first-class, composable objects with declared input/output types.

Problem

Loom's 12-phase pipeline is a fixed sequence. Each pass has implicit requirements about what IR shape it expects and what it guarantees. These requirements aren't declared in the type system — they're implicit in the code and documentation.

Proposal

Make each optimization pass a typed, composable transformation:

trait OptimizationPass {
    /// What IR properties this pass requires (e.g., "SSA form", "no dead code")
    type Input: IrConstraint;
    /// What IR properties this pass guarantees after running
    type Output: IrConstraint;
    /// Run the pass
    fn apply(&self, ir: Ir<Self::Input>) -> Ir<Self::Output>;
}

Benefits

  1. Compile-time safety: The type system prevents invalid pass orderings (can't run LICM before SSA construction)
  2. Self-documenting: Each pass declares its requirements and guarantees
  3. Automated search: The island-model optimization (see related issue) can automatically explore valid pass orderings by matching Output types to Input types
  4. Verification integration: Each pass's Output type carries verification evidence

Implementation

Use Rust's type system with marker traits for IR properties:

trait SsaForm {}
trait NoDeadCode {}
trait ConstantsFolded {}

// DCE requires SSA, guarantees NoDeadCode
impl OptimizationPass for DeadCodeElimination {
    type Input = dyn SsaForm;
    type Output = dyn SsaForm + NoDeadCode;
}

Effort

Low-Medium — can be introduced incrementally as a wrapper around existing passes.

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions