Skip to content

Flambda fix: try to make unbox-closures behave more reasonably#479

Closed
mshinwell wants to merge 16 commits intoocaml:trunkfrom
mshinwell:flambda_unbox-closures
Closed

Flambda fix: try to make unbox-closures behave more reasonably#479
mshinwell wants to merge 16 commits intoocaml:trunkfrom
mshinwell:flambda_unbox-closures

Conversation

@mshinwell
Copy link
Copy Markdown
Contributor

(This is mainly for the attention of @chambart )

@damiendoligez : This is really a somewhat lengthy bug fix that I would like to get into 4.03, unless Pierre thinks the approach is unreasonable. Can this actually get into the release candidate? There may be a few more fixes to this today, but I hope to have the thing tested pretty thoroughly by the end of the day.

This patch aims to make Unbox_closures behave more reasonably. The pass now has two options:

  1. behave as previously, which penalises indirect calls;
  2. use the new notion of direct call surrogate, which does not penalise indirect calls, but potentially increases code size.

A direct call surrogate is a function g that will be used instead of another function f when a direct call is discovered to f. In the case of Unbox_closures, we end up with three functions:
(a) an alpha-renamed copy of the original function (this has the original fun_var), call this f;
(b) a stub, which is marked as a surrogate to f;
(c) a transformed version of the original function, without the free variable(s), which is called by the stub.
When an indirect call is discovered, nothing happens, and f is called. However upon a direct call, the set of closures instructs Inline_and_simplify to use the surrogate (the stub) instead. This of course guarantees that all such generated stubs will be inlined.

In the future, direct call surrogates could perhaps be implemented using functions with multiple entry points.

When doing the Unbox_closures transformation an estimate of the number of free variables being removed is used to calculate a benefit; this is assessed against the function's size. A scaling factor (-unbox-closures-factor) is applied, effectively multiplying the benefit, since it isn't yet clear where the sweet spot may be and this might potentially vary. If the benefit exceeds the size then the transformation will be willing to duplicate and generate a surrogate (case 2 above). Otherwise it proceeds with case 1 as usual.

Most of this patch is actually just moving code around to write the function that duplicates another function with the appropriate alpha-renaming. We will probably need this in the future in any case. It's also made simplify_set_of_closures rather more readable.

This patch also increases the default inline allocation cost, which we suspect is too low.

@mshinwell
Copy link
Copy Markdown
Contributor Author

Sorry, I created this against the wrong branch. I shall make another PR.

@mshinwell mshinwell closed this Feb 23, 2016
lthls pushed a commit to lthls/ocaml that referenced this pull request Jul 9, 2021
These functors are then instantiated in `Reg_width_things`. This will
avoid circularities when coercions carry rec info. This is a fairly
disruptive change, so it's worth doing separately.
chambart pushed a commit to chambart/ocaml-1 that referenced this pull request Feb 1, 2022
EmileTrotignon pushed a commit to EmileTrotignon/ocaml that referenced this pull request Jan 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant