Make tail-rec optimization improvements#541
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
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
TailRecOptboth before and after simplification, with a newcheckAnnotationsmode 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.
CAG2Mark
approved these changes
Jul 4, 2026
CAG2Mark
left a comment
Contributor
There was a problem hiding this comment.
Just one small comment about the error message.
CAG2Mark
reviewed
Jul 5, 2026
LPTK
commented
Jul 5, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
TailRecOptboth before and after simplification so annotations are checked early while later optimization opportunities exposed by simplifications can still be rewritten.checkAnnotationsmode to suppress duplicate/spurious annotation diagnostics on the second TailRecOpt run.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).