Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 30 commits into from

Conversation

Ekdohibs
Copy link
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
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
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
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
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
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
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
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
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