Skip to content

feat(dsp): compile-time pipeline fusion for map/scale/offset stages - #399

Merged
godofecht merged 2 commits into
mainfrom
feat/pipeline-fusion
Aug 10, 2026
Merged

godofecht merged 2 commits into
mainfrom
feat/pipeline-fusion

Conversation

@godofecht

Copy link
Copy Markdown
Contributor

Summary

  • src/flow/pipeline_fusion.py: AST optimization pass that fuses adjacent |> stages of the same function into a single call, eliminating intermediate buffer allocations
  • map_T(map_T(buf, n, f), n, g) fuses to map_T(buf, n, |x| g(f(x))) by composing lambdas
  • scale_T(scale_T(buf, n, a), n, b) fuses to scale_T(buf, n, a*b)
  • offset_T(offset_T(buf, n, a), n, b) fuses to offset_T(buf, n, a+b)
  • Triple fusion (three adjacent maps) collapses to one call in a single pass
  • Pass runs after type checking, before monomorphization
  • 7 tests including correctness verification (compile + run fused code)

Test plan

  • 1271 unit tests pass locally
  • Fused map pipeline produces correct result (sum = 24)
  • Fused scale pipeline produces correct result (sum = 60)
  • Triple map fusion collapses to single call
  • CI passes

Generated with Devin

Add src/flow/pipeline_fusion.py, an AST optimization pass that fuses
adjacent |> stages of the same function into a single call:

  map_T(map_T(buf, n, f), n, g)  ->  map_T(buf, n, |x| g(f(x)))
  scale_T(scale_T(buf, n, a), n, b)  ->  scale_T(buf, n, a*b)
  offset_T(offset_T(buf, n, a), n, b)  ->  offset_T(buf, n, a+b)

This eliminates intermediate buffer allocations. For map fusion, the
two lambdas are composed by substituting the inner lambda's body into
the outer lambda's parameter, producing a new lambda |x| g(f(x)).

The pass runs after type checking and before monomorphization. It walks
all function bodies and rewrites expressions in place.

Triple fusion (three adjacent maps) collapses to one call in a single
pass due to bottom-up recursion.

Add tests/unit/test_pipeline_fusion.py (7 tests): map fusion, scale
fusion, offset fusion, no-fusion for different functions, correctness
preservation (compile + run), triple map fusion.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@cursor

cursor Bot commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

Bugbot is not enabled for your account, so this pull request was not reviewed.

Enable Bugbot in the Cursor dashboard to get automatic reviews on future PRs.

@godofecht
godofecht merged commit 494f9e3 into main Aug 10, 2026
14 checks passed
@godofecht
godofecht deleted the feat/pipeline-fusion branch August 10, 2026 14:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant