Move symbol initialization up in to_cmm
#4238
Open
+93
−35
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 adds to
to_cmm
code to lift/move some symbol initialization upwards. The intent of that move is to (as much as possible and reasonable) have symbol initializations right after the definitions for the variable/value that is written in the symbol field. This should reduce the lifetime of some of these values, which should reduce the pressure on the register allocator (and allow for less spilling).This is particularly useful for symbols that have a lot of fields that are not static (i.e. whose value is computed during module initialization), which is often the case in classic mode for most modules (where the lifting and static allocation is limited), but can also occur even with
Simplify
depending on the shape of the code.Let's consider the following code:
Before this PR (i.e. on current
main
), this is the cmm code generated for the module initialization in classic mode:After this PR, we now generate the following:
Notice how the calls to
caml_initialize
are now spread out and occur immediately after the loads or function calls that create the value to be used, instead of all bunched up. This makes it so that the lifetime of the field values are now much shorter.Finally, this effect of moving symbol initializations up is limited by the current implement, which only allows to move them up to the closest environment flush (or in flambda2 terms, roughly a the top of the current continuation's handler), so code with complex control flow might now get as optimized (one solution for this would be to disallow inlining of functions at top-level, thus ensuring a simple workflow).