Skip to content

Bytecode: keep some optimization hints from lambda - #13438

Merged
OlivierNicole merged 2 commits into
ocaml:trunkfrom
vouillon:optimization-hints
Mar 12, 2026
Merged

OlivierNicole merged 2 commits into
ocaml:trunkfrom
vouillon:optimization-hints

Conversation

@vouillon

Copy link
Copy Markdown
Member

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.

Comment thread tools/ocamlsize Resolved
@vouillon
vouillon force-pushed the optimization-hints branch 2 times, most recently from b3abfea to 355f4fb Compare September 13, 2024 14:04
@gasche

gasche commented Sep 13, 2024

Copy link
Copy Markdown
Member

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?

@vouillon

Copy link
Copy Markdown
Member Author

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.

@lthls

lthls commented Sep 13, 2024

Copy link
Copy Markdown
Contributor

Note that having optimisation hints on the side is also how the native compiler operates (.o file contains the code, .cmx contains the optimisation hints).
The advantage is that you don't need to decompile code to find out the information you need.

Comment thread bytecomp/instruct.mli Outdated
| Event_parent of int ref
| Event_child of int ref

type optimization_hint =

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Each constructor needs to be documented

Comment thread bytecomp/bytegen.ml Outdated
| Pbigstring_set_64 true ->
Khint (Hint_unsafe) :: cont
| Parraylength kind ->
Khint (Hint_array kind) :: cont

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why a hint for Parray_length and not for Parray_(ref/set)(s,u) ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The bytecode compiler generates different code for Parray_(ref/set)(s,u) depending on the array kind.

@hhugo

hhugo commented Oct 26, 2024

Copy link
Copy Markdown
Contributor

@vouillon, we currently can't distinguish makeblock from makearray at the bytecode level. Should we have a hint for that ?

@hhugo

hhugo commented Oct 27, 2024

Copy link
Copy Markdown
Contributor

Another thing is that let f (a : int) b = a = b and let g a b = a == b are compiled the same in bytecode.
Translprim.ml compiles == and != to

"%eq", Primitive ((Pintcomp Ceq), 2); 
"%noteq", Primitive ((Pintcomp Cne), 2);

This is enjoying in the context of jsoo because we end up using javascript === for ocaml == and the javascript === doesn't distinguish +0. from -0. and consider nan !== nan

We could use an hint to distinguish "int comparison" and "physical equality". Or we could use a different bytecode instruction.

Comment thread boot/camlheader Resolved
@vouillon

Copy link
Copy Markdown
Member Author

@vouillon, we currently can't distinguish makeblock from makearray at the bytecode level. Should we have a hint for that ?

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.
get/setvectitem would need a hint as well.

@vouillon

Copy link
Copy Markdown
Member Author

We could use an hint to distinguish "int comparison" and "physical equality". Or we could use a different bytecode instruction.

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.

@OlivierNicole

Copy link
Copy Markdown
Contributor

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).

@vouillon

Copy link
Copy Markdown
Member Author

Here are the size of the largest sections in the ocaml bytecode executable.

Total 24466262 100%
DBUG 20802289 85.0%
CODE 3031260 12.4%
DATA 483482 2.0%
HINT 135609 0.6%

And for stdlib.cma:

Total 1694726 100%
Debug size 1158000 68%
Code size 438548 25.9%
Hint size 7449 0.4%

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.

@hhugo

hhugo commented Oct 29, 2024

Copy link
Copy Markdown
Contributor

We could use an hint to distinguish "int comparison" and "physical equality". Or we could use a different bytecode instruction.

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.

I've a commit for that. I'll link it here later this week

@hhugo

hhugo commented Oct 29, 2024

Copy link
Copy Markdown
Contributor

@vouillon, we currently can't distinguish makeblock from makearray at the bytecode level. Should we have a hint for that ?

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. get/setvectitem would need a hint as well.

Some of this work has been started by @TyOverby in ocsigen/js_of_ocaml#784

@hhugo

hhugo commented Nov 2, 2024

Copy link
Copy Markdown
Contributor

We could use an hint to distinguish "int comparison" and "physical equality". Or we could use a different bytecode instruction.

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.

I've a commit for that. I'll link it here later this week

#13588

@OlivierNicole

OlivierNicole commented Dec 16, 2024 •

Copy link
Copy Markdown
Contributor

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?

@mshinwell

Copy link
Copy Markdown
Contributor

Yeah given the small size of the hints, I think this also ok.

@dra27

dra27 commented Jan 15, 2025

Copy link
Copy Markdown
Member

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)?

@dra27

dra27 commented Mar 28, 2025

Copy link
Copy Markdown
Member

Compiling with -g, the executable contains information which is of use to the executable (and consequently the user) - the HINT information is still not of any use, even then!

@OlivierNicole

Copy link
Copy Markdown
Contributor

I see. In all cases, I think that we can agree that it makes sense that -g enables the hints for libraries.

@OlivierNicole

Copy link
Copy Markdown
Contributor

(Or, if we guard hints by a flag, that this flag be set if -g is set for libraries, and maybe not for executables.)

@dra27

dra27 commented Mar 28, 2025

Copy link
Copy Markdown
Member

Yes indeed, to both

@vouillon
vouillon force-pushed the optimization-hints branch 2 times, most recently from c8ce357 to 531bd3e Compare October 24, 2025 10:51
@OlivierNicole
OlivierNicole self-requested a review December 4, 2025 13:29

@OlivierNicole OlivierNicole left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread utils/clflags.ml Resolved
Comment thread driver/main_args.ml Resolved
@OlivierNicole

Copy link
Copy Markdown
Contributor

Sorry for the delay on this one. As for me, once my minor naming point is addressed, I am happy to approve it.

@OlivierNicole

Copy link
Copy Markdown
Contributor

@gasche had a question that I find relevant now that he’s said it: why adding a Khint constructor instead of adding arguments to the other constructors? (This question only concerns the instruct type, not the chosen representation on the object files, which is fine as-is.)

@vouillon

Copy link
Copy Markdown
Member Author

@gasche had a question that I find relevant now that he’s said it: why adding a Khint constructor instead of adding arguments to the other constructors? (This question only concerns the instruct type, not the chosen representation on the object files, which is fine as-is.)

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:

  | Kmakeblock of int * int
  | Kmakefloatblock of int
  | Kccall of string * int
  | Kvectlength
  | Kclosure of label * int
  | Kclosurerec of label list * int
```

@gasche

gasche commented Jan 16, 2026

Copy link
Copy Markdown
Member

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.

@OlivierNicole

Copy link
Copy Markdown
Contributor

Adding a Khint constructor has the advantage that the changes are very localized.

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?

@lthls

lthls commented Jan 19, 2026

Copy link
Copy Markdown
Contributor

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.

@vouillon
vouillon force-pushed the optimization-hints branch 3 times, most recently from 455917f to 7e0bbe7 Compare January 20, 2026 16:50
@vouillon

Copy link
Copy Markdown
Member Author

@gasche I have added a commit that switch to adding additional arguments instead of adding an instruction.

@vouillon
vouillon marked this pull request as ready for review January 30, 2026 21:38

@lthls lthls left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread bytecomp/bytegen.ml Outdated
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) ::

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
(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

Comment thread bytecomp/bytelink.ml Outdated
Bytesections.record toc_writer DBUG;
(* Hint info *)
output_hint_info outchan;
Bytesections.record toc_writer HINT;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed! I have removed this code.

Comment thread bytecomp/instruct.ml Outdated
specialise : Lambda.specialise_attribute;
is_a_functor : bool }

type optimization_hint =

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_hint

Then 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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added a type ccall_hint. I'm not sure adding types block_hint and array_hint are worth it.

Comment thread bytecomp/instruct.ml Outdated
| Kclosure of label * int
| Kclosurerec of label list * int
| Kclosure of label * int * closure_hint
| Kclosurerec of label list * int * closure_hint list

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have switched to Kclosurerec of (label * closure_hint) list * int.

@vouillon
vouillon force-pushed the optimization-hints branch from 7e0bbe7 to 5dbda39 Compare March 10, 2026 23:18

@OlivierNicole OlivierNicole left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My comments have been addressed and it seems to me that, taking into account the latest commits, @lthls and @dra27's reviews are addressed too (please shout if you disagree), so I would be in favor of merging this PR.

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.
@vouillon
vouillon force-pushed the optimization-hints branch from 5dbda39 to b88ad78 Compare March 11, 2026 17:32
@OlivierNicole
OlivierNicole merged commit e11e8c6 into ocaml:trunk Mar 12, 2026
25 checks passed
Alizter added a commit to ocaml/dune that referenced this pull request Oct 7, 2026
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.
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.

7 participants