Skip to content

Local allocations support for Flambda 2#490

Merged
mshinwell merged 6 commits intooxcaml:mainfrom
mshinwell:local-flambda2
Feb 2, 2022
Merged

Local allocations support for Flambda 2#490
mshinwell merged 6 commits intooxcaml:mainfrom
mshinwell:local-flambda2

Conversation

@mshinwell
Copy link
Copy Markdown
Collaborator

@mshinwell mshinwell commented Jan 26, 2022

This PR provides support for local allocations in Flambda 2.

First there is a new type Alloc_mode which either says whether an allocation is on the Heap or the Local allocation stack.

As for exception handling in Flambda 2, deconstruction of block-structured region constructs is performed on the way into Flambda 2, rather than during Selectgen.

The following new primitives are added and are generated on the way into Flambda 2:

  • Begin_region, which starts a new region in which local allocations may be performed. This primitive returns what is effectively a name for the region, which at runtime will actually be the value of the local allocation stack pointer when the region was opened. These region names have a new kind, Region, as they must be kept apart from everything else (especially since in the backend they have to be given register type typ_int). The Region kind was appropriated from the redundant Fabricated kind, so as a convenient side-effect that has now gone.
  • End_region, which takes a variable of kind Region and closes the region.

I did consider adding these as trap actions but in the end primitives seem better (@stedolan suggested maybe we could actually do the same for push and pop trap one day). I also considered whether locally-allocating primitives and sets of closures declarations should name the corresponding region variable, to make things more robust in the face of future code motion transformations. However there would be a tricky nuance here: only the most recent (innermost) region can be allocated in, and that constraint would have to be encoded. For the moment the region primitives are marked as effectful and coeffectful, so they cannot be removed or moved; and any locally-allocating primitive or local set of closures declaration is marked as coeffectful to act as a barrier.

There are two new Cmm operations corresponding to the above which directly translate to the pre-existing Mach equivalents. Note that it is critical that the result of the begin-region operation is typ_int not typ_val.

The interaction between allocation modes and currying is tricky, as will be seen in the partial application and over application wrapper expansion code (incidentally during this work I noted there doesn’t seem to be any partial application wrapper expansion in classic mode yet). The local allocations logic here follows that of Closure.

The following properties are now tracked on Code to implement all of this:

  • the number of trailing local parameters (closure construction as we move along parameter lists starts as Heap and then switches, possibly immediately, to Local but never the other way around);
  • whether a local allocation done inside the function (by that function itself or a callee) might escape out of the function.

The translation into Flambda 2 takes care of ensuring that End_region primitives are inserted immediately prior to an application in tail position (as judged by the type checker), after evaluation of any arguments, in the case where such application in the Lambda code is surrounded by an Lregion that is closing. (A simple translation would erroneously put the End_region after the application.)

This branch is currently based on one of @stedolan’s development branches, I will rebase it once the remainder of the local allocations support has been merged. See the most recent changeset for the Flambda 2 changes.

@mshinwell mshinwell added the flambda2 Prerequisite for, or part of, flambda2 label Jan 26, 2022
@mshinwell
Copy link
Copy Markdown
Collaborator Author

@chambart could you please advise what the code in Dissect_letrec needs to look like for Lregion?

@mshinwell mshinwell marked this pull request as draft January 26, 2022 16:28
@mshinwell mshinwell force-pushed the local-flambda2 branch 2 times, most recently from 69d947a to 9f9b5fb Compare January 27, 2022 09:52
@mshinwell mshinwell marked this pull request as ready for review January 27, 2022 11:24
@stedolan
Copy link
Copy Markdown
Contributor

stedolan commented Feb 2, 2022

This patch needs to remove tests/typing-local from testsuite/flambda2-test-list to ensure the local allocs tests actually run in CI.

@mshinwell
Copy link
Copy Markdown
Collaborator Author

Ah well spotted, I meant to do that but forgot. Done now.

@mshinwell mshinwell merged commit 45e5817 into oxcaml:main Feb 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

flambda2 beta flambda2 Prerequisite for, or part of, flambda2

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants