Local allocations support for Flambda 2#490
Merged
mshinwell merged 6 commits intooxcaml:mainfrom Feb 2, 2022
Merged
Conversation
Collaborator
Author
|
@chambart could you please advise what the code in |
69d947a to
9f9b5fb
Compare
1976c80 to
fe217d0
Compare
cea4b2c to
5306e17
Compare
lthls
approved these changes
Feb 2, 2022
Contributor
|
This patch needs to remove tests/typing-local from |
Collaborator
Author
|
Ah well spotted, I meant to do that but forgot. Done now. |
mshinwell
added a commit
that referenced
this pull request
Mar 3, 2022
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR provides support for local allocations in Flambda 2.
First there is a new type
Alloc_modewhich either says whether an allocation is on theHeapor theLocalallocation 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 typetyp_int). TheRegionkind was appropriated from the redundantFabricatedkind, so as a convenient side-effect that has now gone.End_region, which takes a variable of kindRegionand 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_intnottyp_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
Codeto implement all of this:Heapand then switches, possibly immediately, toLocalbut never the other way around);The translation into Flambda 2 takes care of ensuring that
End_regionprimitives 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 theLambdacode is surrounded by anLregionthat is closing. (A simple translation would erroneously put theEnd_regionafter 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.