Deforest rewriting improvements#499
Conversation
| // marks if a function or lambda is affine, i.e. called at most once. | ||
| // for functions with multiple parameter lists, `whichParamList` is the zero-based parameter-list index. | ||
| // marks if a function or lambda is one-shot, i.e. called at most once. | ||
| // `whichParamList` is the zero-based index of the parameter list for functions with multiple parameter lists. |
There was a problem hiding this comment.
I still don't get it, from this comment. Why is there a parameter list index?!
There was a problem hiding this comment.
I've tried to further clarify this comment in b4e9fb6
Wow... If I'd known the so-called "implicit returns" would cause such problems, I would have pushed for removing them earlier. They're mostly a legacy piece of design that should probably go. They should certainly not be the cause of such extra complexity. |
# Conflicts: # hkmc2/shared/src/main/scala/hkmc2/codegen/deforest/Rewrite.scala
Ok, done. And I've merged this branch with the help of Codex, so please do review your own diff, making sure all the important relevant changes are still in. |
There was a problem hiding this comment.
Pull request overview
This PR refines the deforestation (fusion) rewriting pass to avoid incorrectly converting non-returning contexts into value-returning return ... statements (notably in class/module constructor-like blocks), which could make subsequent constructor code unreachable (e.g., return super()).
Changes:
- Make the rewriter conditional: skip program rewriting when the solver reports no fusions.
- Introduce context-sensitive tail rewriting (
returnsFromCurrentBlock) so “tail” fused calls are either emitted asReturn(...)(function/lambda bodies) or as discarded statements (constructor/module bodies). - Add/clarify high-level comments and rename a comment in
Annot.Affineto describe “one-shot” usage.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| hkmc2/shared/src/main/scala/hkmc2/semantics/Term.scala | Updates Annot.Affine doc comment to describe one-shot semantics more clearly. |
| hkmc2/shared/src/main/scala/hkmc2/codegen/deforest/Rewrite.scala | Adds context-sensitive tail rewriting to avoid value-returning return ... in constructor-like blocks; skips rewriting when there is no fusion. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…mprovements Conflicts: hkmc2/shared/src/main/scala/hkmc2/codegen/deforest/Rewrite.scala hkmc2/shared/src/main/scala/hkmc2/codegen/flowAnalysis/FlowAnalysis.scala
LPTK
left a comment
There was a problem hiding this comment.
Is the PR otherwise ready to merge?
| :deforest | ||
| :expect 5 | ||
| :noInline | ||
| :fixme |
There was a problem hiding this comment.
You should add the logic to treat self-references as free variables, right?
Will you fix this as part of this PR, or a follow-up? I'm fine with both options.
There was a problem hiding this comment.
You should add the logic to treat self-references as free variables, right?
Yes.
Will you fix this as part of this PR, or a follow-up? I'm fine with both options.
I currently plan to fix this in a follow-up, maybe it's also better to fix this after #505 is merged?
Co-authored-by: Lionel Parreaux <lionel.parreaux@gmail.com>
df3f951 to
3b039bb
Compare
|
@ychenfo Pls fix the conflicts again. |
…mprovements Conflicts: hkmc2/shared/src/main/scala/hkmc2/codegen/deforest/Rewrite.scala hkmc2/shared/src/test/mlscript/deforest/fusibility.mls
…l a class and a module
- remove error raising in extractor - uses `MemberRefTo` instead - update `Annot.Affine` documentation
LPTK
left a comment
There was a problem hiding this comment.
Net-negative PR despite added tests & documentation – nice!
This PR fixes a deforestation bug where the rewriter wrongly turns implicit returns to explicit returns. Turning implicit returns to explicit ones is needed for branch-body and the continuation "rest" functions whose bodies are originated from top level blocks, but wrong for rewriting blocks like class constructors: it could produce explicit
return super()and made code aftersuper()unreachable, which causes bugs. This is now fixed.This PR also avoids traversing the program when there is no fusion, and adds some high level comments for the rewriting process.
TODO here and in following PRs
irDefnto re-expose the bugclassCtorSymbolto useClassCtorSymbolRewrite's handling of FVs