Skip to content

Make tail-rec optimization improvements#541

Merged
LPTK merged 17 commits into
hkust-taco:hkmc2from
LPTK:tailrec-opt-improvement
Jul 5, 2026
Merged

Make tail-rec optimization improvements#541
LPTK merged 17 commits into
hkust-taco:hkmc2from
LPTK:tailrec-opt-improvement

Conversation

@LPTK

@LPTK LPTK commented Jul 4, 2026

Copy link
Copy Markdown
Contributor
  • Run TailRecOpt both before and after simplification so annotations are checked early while later optimization opportunities exposed by simplifications can still be rewritten.
  • Add a checkAnnotations mode to suppress duplicate/spurious annotation diagnostics on the second TailRecOpt run.
  • Fix TailRecOpt’s call graph so under-applied curried calls are not treated as executable recursive call edges.
  • Add/update regression coverage for multi-argument-list tail recursion, including the partial-application case that previously caused the second pass to reprocess already optimized code.
  • Refresh affected golden outputs, including Runtime.mjs.

Codex was used to help diagnose issues and change code, but the design was mine (the fixes proposed by Codex were all stupid workarounds eschewing the real issue).

@LPTK LPTK requested review from CAG2Mark and Copilot July 4, 2026 12:41
@LPTK LPTK added the optimizer label Jul 4, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refines the HKMC2 tail-recursion optimization pipeline by running TailRecOpt twice (pre/post simplification), improving annotation checking behavior, and fixing call-graph construction so under-applied curried calls are not treated as executable recursion edges. It also updates/extends regression coverage around multi-argument-list tail recursion and refreshes the associated golden outputs (including runtime snapshots).

Changes:

  • Run TailRecOpt both before and after simplification, with a new checkAnnotations mode to avoid duplicate/spurious diagnostics on the second run.
  • Correct TailRecOpt call-graph edge construction to ignore under-applied curried calls (closure construction) as recursion edges.
  • Update/add regression tests and golden outputs for tailrec + currying + partial-application scenarios, plus refreshed IR/JS snapshots.

Reviewed changes

Copilot reviewed 14 out of 14 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
hkmc2/shared/src/main/scala/hkmc2/codegen/TailRecOpt.scala Adds checkAnnotations mode and refines call-graph edge detection to require exact saturation for executable edges/jumps.
hkmc2/shared/src/main/scala/hkmc2/codegen/CompilationPipeline.scala Runs TailRecOpt twice (checked early, then optimization-only later) to capture post-simplification opportunities.
hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala Makes Call equality/hash include metadata so annotations like @tailcall aren’t ignored.
hkmc2/shared/src/test/mlscript/tailrec/TailRecOpt.mls Updates snapshots to :soir and refreshes expected optimized IR/errors.
hkmc2/shared/src/test/mlscript/tailrec/Simple.mls Updates warning text and refreshes JS snapshot for a non-tail-recursive nested self-call case.
hkmc2/shared/src/test/mlscript/tailrec/MultiArgLists.mls Adds/updates regression coverage for multi-arg-list tail recursion, including the partial-application edge case.
hkmc2/shared/src/test/mlscript/tailrec/Errors.mls Updates warning text expectations and adds new warning/error cases for @tailrec.
hkmc2/shared/src/test/mlscript/tailrec/Annots.mls Updates warning text expectation for @tailrec with no tail calls.
hkmc2/shared/src/test/mlscript/deforest/eta-expansion/recursive.mls Refreshes optimized IR snapshot (loopification visible post pipeline changes).
hkmc2/shared/src/test/mlscript/deforest/cyclic.mls Refreshes optimized IR snapshot reflecting loopified worker behavior.
hkmc2/shared/src/test/mlscript/codegen/TailRecFormerFailure.mls Refreshes optimized IR snapshots for former tailrec failure cases.
hkmc2/shared/src/test/mlscript/codegen/ConfigDirective.mls Refreshes optimized IR snapshots and updates directives to :soir.
hkmc2/shared/src/test/mlscript-compile/Runtime.mls Updates runtime source to use unreachable in a way consistent with generated JS.
hkmc2/shared/src/test/mlscript-compile/Runtime.mjs Refreshes generated runtime snapshot (notably inlining loop structure into separate methods).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread hkmc2/shared/src/test/mlscript/tailrec/TailRecOpt.mls

@CAG2Mark CAG2Mark left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one small comment about the error message.

Comment thread hkmc2/shared/src/main/scala/hkmc2/codegen/TailRecOpt.scala
Comment thread hkmc2/shared/src/main/scala/hkmc2/codegen/TailRecOpt.scala Outdated
Comment thread hkmc2/shared/src/test/mlscript/tailrec/Simple.mls Outdated
@LPTK LPTK merged commit 3af9244 into hkust-taco:hkmc2 Jul 5, 2026
1 check passed
@LPTK LPTK deleted the tailrec-opt-improvement branch July 5, 2026 15:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants