Skip to content

Flambda fix: try to make Unbox_closures behave more reasonably#480

Merged
mshinwell merged 1 commit intoocaml:4.03from
mshinwell:flambda_unbox-closures
Feb 26, 2016
Merged

Flambda fix: try to make Unbox_closures behave more reasonably#480
mshinwell merged 1 commit intoocaml:4.03from
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

Oh, I forgot one other thing: this patch also marks partial application wrappers as stubs, which should have been the case before.

@mshinwell mshinwell force-pushed the flambda_unbox-closures branch from 449cee7 to edee702 Compare February 24, 2016 09:01
@mshinwell mshinwell force-pushed the flambda_unbox-closures branch from edee702 to 0453c74 Compare February 24, 2016 09:03
mshinwell added a commit that referenced this pull request Feb 26, 2016
GPR#480: Flambda fix: try to make Unbox_closures behave more reasonably
@mshinwell mshinwell merged commit b1d1eed into ocaml:4.03 Feb 26, 2016
mshinwell added a commit that referenced this pull request Feb 26, 2016
GPR#480: Flambda fix: try to make Unbox_closures behave more reasonably
Octachron pushed a commit to Octachron/ocaml that referenced this pull request Feb 29, 2016
GPR#480: Flambda fix: try to make Unbox_closures behave more reasonably
mshinwell pushed a commit to mshinwell/ocaml that referenced this pull request Jul 9, 2021
Allow Rec_info_expr to express that an occurrence may be arbitrarily
deep, so that we don't want to inline it automatically but may still
observe an `@unrolled` attribute, or that we don't want to inline or
unroll at all.
stedolan pushed a commit to stedolan/ocaml that referenced this pull request May 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants