Skip to content

codegen: type-directed specialization — emit native ops where the type is statically known (#5334 Tier 3 / Lever E) #5497

Description

@proggeramlug

Follow-on to #5334. That issue tracked making perry's IR efficient for large/untyped modules; its Tier 1 (outline the dynamic machinery) and Tier 2 (emit less scaffolding) levers all shipped:

This issue carries the one remaining lever, Tier 3 / Lever E: type-directed specialization — the strategic, long-term piece that #5334 explicitly deferred.

Goal

Where a value is statically a number / string / known-shape (real .ts, tsgo, or local inference), emit native ops instead of the dynamic form:

  • fadd / fmul / fcmp instead of js_add / js_rel_lt / IC guards
  • direct slot load/store instead of the inline-cache diamond
  • skip the nan-box round-trip entirely when the value never leaves a typed register

This attacks the two largest line-counts from #5334's measurement at the source — the 4.4M call @js_* and 365K js_typed_feedback_*_guard sites — rather than outlining them after the fact.

Why separate from #5334

Sketch

  1. Thread a static-type signal to codegen op-emit sites (extend the existing static_type_of / class_field_declared_type machinery).
  2. Number/boolean arithmetic + comparisons → native f64 ops, guarded only where the static type is number | undefined-ish.
  3. Known-shape field access → direct slot, bypassing the IC diamond.
  4. String concat / comparison on statically-string operands → direct runtime calls without the dynamic dispatch.
  5. Measure on a typed hot-loop benchmark (IR-line reduction + wall-clock) and on a real .ts app; hard constraint carried over from codegen: IR is ~96x bloated for large/untyped modules (1.25GB / ~15GB clang RSS for a 13MB bundle) — outline dynamic machinery + specialize #5334: no runtime perf regression on real code.

Context: surfaced taking the 13 MB @anthropic-ai/claude-code cli.js through perry. Tiers 1–2 made untyped bundles compilable; this is the typed-code performance frontier.

Metadata

Metadata

Assignees

No one assigned

    Labels

    performanceRuntime or compile-time performance

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions