Skip to content

Transform tail-recursive function calls into calls to recursive continuations#836

Closed
Ekdohibs wants to merge 30 commits intooxcaml:mainfrom
Ekdohibs:rec-to-cont
Closed

Transform tail-recursive function calls into calls to recursive continuations#836
Ekdohibs wants to merge 30 commits intooxcaml:mainfrom
Ekdohibs:rec-to-cont

Conversation

@Ekdohibs
Copy link
Copy Markdown
Contributor

Self tail-calls are identified at the very beginning of simplify_apply_expr and are transformed into a call to the recursive continuation corresponding to the function, which is always added. An optimisation in simplify_let_cont_expr ensures that this recursive continuation is simplified if it is unused.

@mshinwell mshinwell added the flambda2 Prerequisite for, or part of, flambda2 label Sep 15, 2022
@chambart
Copy link
Copy Markdown
Contributor

The module simplify_rec_to_cont.ml does not modify the expression, it should probably be named differently and be moved to the env directory or be included in a submodule in dacc

@chambart
Copy link
Copy Markdown
Contributor

I'm not sure, but it doesn't seem that switch arms arguments are not accounted.

In general, it seems a bit fragile to track it like that everywhere. Maybe doing it in Simplify_simple would be more robust

Copy link
Copy Markdown
Contributor

@chambart chambart left a comment

Choose a reason for hiding this comment

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

Ok, some changes needed

@Ekdohibs
Copy link
Copy Markdown
Contributor Author

The detection of whether a function is completely tail-recursive is now done in lambda-to-flambda, which allows us to remove everything concerning the use of my_closure. The code still needs a few additional cleanups, however, as well as possibly an attribute to force the transformation when it does not apply automatically.

@Ekdohibs
Copy link
Copy Markdown
Contributor Author

Rebased for compatibility with the new Code_metadata refactor.

@Ekdohibs Ekdohibs force-pushed the rec-to-cont branch 3 times, most recently from 07dce85 to d0ec360 Compare September 22, 2022 13:59
Copy link
Copy Markdown
Contributor

@chambart chambart left a comment

Choose a reason for hiding this comment

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

In general ok.

let[@inline always] canon simple =
Simple.without_coercion (TE.get_canonical_simple_exn tenv simple)
in
if Simple.equal (canon (Simple.var my_closure)) (canon (Apply.callee apply))
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.

There should be a comment for why this matter rather than doing that after simplify simple. Note that this would work after simplify_simple

@chambart
Copy link
Copy Markdown
Contributor

In general I would prefer to try another method for is_purely_tail_call for a more robust method

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

flambda2 Prerequisite for, or part of, flambda2

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants