Repository navigation
Bytecode: keep some optimization hints from lambda - #13438
Conversation
b3abfea to
355f4fb
Compare
|
An obvious question: why do we want to store the information on the side, instead of including the information in the bytecode instructions? I suppose that you don't want to break bytecode compatibility by adding more opcodes, or more opcode arguments? |
|
Indeed, an alternative would be to add some opcodes and/or primitives. But I'm not sure we want to make the bytecode interpreter and the runtime larger just for this. |
|
Note that having optimisation hints on the side is also how the native compiler operates ( |
4ee1f46 to
ea88542
Compare
| | Event_parent of int ref | ||
| | Event_child of int ref | ||
|
|
||
| type optimization_hint = |
There was a problem hiding this comment.
Each constructor needs to be documented
| | Pbigstring_set_64 true -> | ||
| Khint (Hint_unsafe) :: cont | ||
| | Parraylength kind -> | ||
| Khint (Hint_array kind) :: cont |
There was a problem hiding this comment.
Why a hint for Parray_length and not for Parray_(ref/set)(s,u) ?
There was a problem hiding this comment.
The bytecode compiler generates different code for Parray_(ref/set)(s,u) depending on the array kind.
|
@vouillon, we currently can't distinguish makeblock from makearray at the bytecode level. Should we have a hint for that ? |
|
Another thing is that This is enjoying in the context of jsoo because we end up using javascript We could use an hint to distinguish "int comparison" and "physical equality". Or we could use a different bytecode instruction. |
ea88542 to
dccc3e3
Compare
I'm not sure what we could do with that in Js_of_ocaml. There was this idea of using objects rather than arrays for blocks, but that would be a lot of work. If we can distinguish blocks from arrays, we could save one word per array in Wasm. |
That could be useful, indeed. But it's a bit more intrusive than the other changes since the information is already lost in the lambda code. |
|
During the developer meeting yesterday, it was asked whether storing Lambda code as side information would work for your purposes @vouillon. There was also agreement that it would be good to have numbers regarding the size increase of bytecode executables due to this change (or to storing Lambda). |
|
Here are the size of the largest sections in the ocaml bytecode executable.
And for stdlib.cma:
I suppose one could store the lambda code into bytecode archives and executable files. But I guess it would close to double their size. And it would take some work to use it in Js_of_ocaml. |
I've a commit for that. I'll link it here later this week |
Some of this work has been started by @TyOverby in ocsigen/js_of_ocaml#784 |
|
|
In light of the small size that the hints take, this seems reasonable to me. But I’m slightly conflicted here as I also work on {js,wasm}_of_ocaml. Does someone still object to this HINT section? |
|
Yeah given the small size of the hints, I think this also ok. |
|
Even at a small size, I find it a bit odd that we unconditionally add this to all bytecode executables, given that it’s not of any use to them (and given that we routinely install bytecode programs). Could the emission of the section be gated (it could even be another -d* flag)? |
|
Compiling with |
|
I see. In all cases, I think that we can agree that it makes sense that |
|
(Or, if we guard hints by a flag, that this flag be set if |
|
Yes indeed, to both |
c8ce357 to
531bd3e
Compare
There was a problem hiding this comment.
Conflict of interest disclosure: I have been working with Jérôme on js_of_ocaml and wasm_of_ocaml and I hope that the consumer side of bytecode hints (ocsigen/js_of_ocaml#1721) will see the light.
This looks good to me except for the minor point below.
|
Sorry for the delay on this one. As for me, once my minor naming point is addressed, I am happy to approve it. |
|
@gasche had a question that I find relevant now that he’s said it: why adding a |
Adding a Khint constructor has the advantage that the changes are very localized. But we could add an argument to the following constructors instead, indeed: |
|
I think that the version with the data in the instructions would be nicer, but this does not change the external interface (the format of the actual bytecode object), so it's only an "internal" question. At first my impression was that the design of the PR was a bit ad-hoc, but after thinking about it more I am fine with the proposed object format representation. So I'm happy to approve the design, if someone can vouch for the implementation. |
That was my initial reasoning as well. But in fact I think that these constructors are used almost exclusively when emitting bytecode, so changing them wouldn’t be a big change, unless I’m missing something. I’ve already expressed that I’m fine to approve the PR except for my minor naming qualm, but I’m conflicted. Would someone else be ready to look at the implementation? @lthls maybe? |
I don't have the time for that right now, but I should be able to look at it in February. |
455917f to
7e0bbe7
Compare
|
@gasche I have added a commit that switch to adding additional arguments instead of adding an instruction. |
lthls
left a comment
There was a problem hiding this comment.
I've left some comments on things that I think could be improved without too much work. Overall I think the implementation is correct and reasonably easy to maintain.
| else comp_args stack_info env args sz | ||
| (Kmakeblock(List.length args, 0) :: | ||
| Kccall("caml_array_of_uniform_array", 1) :: cont) | ||
| (Kmakeblock(List.length args, 0, Immutable) :: |
There was a problem hiding this comment.
| (Kmakeblock(List.length args, 0, Immutable) :: | |
| (Kmakeblock(List.length args, 0, mut) :: |
caml_array_of_uniform_array returns its argument unchanged unless it contains floats
| Bytesections.record toc_writer DBUG; | ||
| (* Hint info *) | ||
| output_hint_info outchan; | ||
| Bytesections.record toc_writer HINT; |
There was a problem hiding this comment.
I'm not sure it makes sense to output the hints in a .cds file. Looking at #4703, it seems that such files are used for debugging bytecode linked as a C object, so optimisation hints are likely irrelevant.
There was a problem hiding this comment.
Indeed! I have removed this code.
| specialise : Lambda.specialise_attribute; | ||
| is_a_functor : bool } | ||
|
|
||
| type optimization_hint = |
There was a problem hiding this comment.
I think this would benefit from being split into two levels, for instance:
type closure_hint = ...
type block_hint = Hint_immutable
type array_hint = Lambda.array_kind
type ccall_hint =
| Hint_unsafe
| Hint_int of Primitive.boxed_integer
| Hint_bigarray of
{ unsafe : bool;
elt_kind : Lambda.bigarray_kind;
layout : Lambda.bigarray_layout }
| Hint_primitive of Primitive.description
type optimization_hint =
| Hint_makeblock of block_hint
| Hint_arraylength of array_hint
| Hint_closure of closure_hint
| Hint_ccall of ccall_hintThen you can easily check that each kind of hint is attached to the expected instruction.
If this doesn't simplify the consumers (on the js_of_ocaml/wasm_of_ocaml side) it's probably not worth doing though.
There was a problem hiding this comment.
I have added a type ccall_hint. I'm not sure adding types block_hint and array_hint are worth it.
| | Kclosure of label * int | ||
| | Kclosurerec of label list * int | ||
| | Kclosure of label * int * closure_hint | ||
| | Kclosurerec of label list * int * closure_hint list |
There was a problem hiding this comment.
I'm surprised that you chose to attach the hints to the closures. At the very least I would expect something like Kclosurerec of (label * closure_hint) list * int, but for Flambda 2 at least this kind of metadata is associated to the code and not the closure, so I would have expected the hints to be emitted at the start of the code of each function instead of at the closure allocation point.
There was a problem hiding this comment.
I have switched to Kclosurerec of (label * closure_hint) list * int.
7e0bbe7 to
5dbda39
Compare
Some lambda information, such as whether an allocated block is immutable, or the kind and layout of a bigarray we are accessing, are not taken into account when generating bytecode. This information could be useful for Js_of_ocaml and Wasm_of_ocaml to generate better code. We insert it as an additional section in bytecode modules and executables, similarly to what is done for debug events.
5dbda39 to
b88ad78
Compare
Starting with OCaml 5.6, pass -bytecode-hints when linking the bytecode executable compiled by js_of_ocaml and wasm_of_ocaml in whole program mode, so that the [optimization hints](ocaml/ocaml#13438) are available to them.
Some lambda information, such as whether an allocated block is immutable, or the kind and layout of a bigarray we are accessing, are not taken into account when generating bytecode. This information could be useful for Js_of_ocaml and Wasm_of_ocaml to generate better code. We insert it as an additional section in bytecode modules and executables, similarly to what is done for debug events.