Skip to content
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

Add tail-recursion modulo cons (TMC) optimisation #1089

Open
myreen opened this issue Nov 12, 2024 · 0 comments
Open

Add tail-recursion modulo cons (TMC) optimisation #1089

myreen opened this issue Nov 12, 2024 · 0 comments
Labels
enhancement medium effort medium reward Easy to measure but may not be noticed by itself performance Runtime of a plausible real cakeml-generated binary student project can be done as a student project (at various levels)

Comments

@myreen
Copy link
Contributor

myreen commented Nov 12, 2024

This issue is about adding tail-recursion modulo cons (TMC) as a new optimisation to the CakeML compiler. The idea is to automatically transform non-tail-recursive functions such as

fun append [] ys = ys
  | append (Cons x xs) ys = Cons x (append xs ys);

into tail-recursive functions:

fun append' ctxt [] ys = (ys → ctxt)
  | append' ctxt (Cons x xs) ys = append' (Cons x HOLE → ctxt) xs ys;

fun append [] ys = ys
  | append (Cons x xs) ys = append' (Cons x HOLE) xs ys

or, if the original function is large and we don't want to duplicate it, into append' as above and the following:

fun append xs ys = Option.valOf (append' (SOME HOLE) xs ys)

Concretely, the the context ctxt is a reference to a MutableBlock and an offset where the primitive for plugging () the hole in MutableBlock is to add the new element.

This optimisation is to happen around bvl-to-bvi in the compiler. It uses mutable state as a temporary, but returns on completion exactly the same immutable result as the original version of the code.

This is suitable as a student project. Contact me (myreen) on the CakeML Discord to discuss details.

@myreen myreen added enhancement performance Runtime of a plausible real cakeml-generated binary medium effort medium reward Easy to measure but may not be noticed by itself student project can be done as a student project (at various levels) labels Nov 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement medium effort medium reward Easy to measure but may not be noticed by itself performance Runtime of a plausible real cakeml-generated binary student project can be done as a student project (at various levels)
Projects
None yet
Development

No branches or pull requests

1 participant