The Flambda2 Snippets, Episode 0
At OCamlPro, the main ongoing task on the OCaml Compiler is to improve the high-level optimisation. This is something that we have been doing for quite some time now. Indeed, we are the authors behind the
Flambda
optimisation pass and today we would like to introduce the series of blog snippets showcasing the direct successor to it, the creatively namedFlambda2
.
Introducing our Flambda2 snippets
This series of blog posts will cover everything about Flambda2
, a
new optimising backend for the OCaml native compiler. This
introductory episode will provide you with some context and history
about Flambda2
but also about its predecessor Flambda
and, of course, the OCaml
compiler!
This work may be considered as a completement to an on-going documentation effort at OCamlPro as well as to the many different talks we have given last year on the subject, two of which you can watch online: OCaml Workshop ( slideshow ), ML Workshop ( slideshow ).
This work was developed in collaboration with, and funded by Jane Street. Warm thanks to Mark Shinwell for shepherding the Flambda project and to Ron Minsky for his support.
Compiling OCaml
The compiling of OCaml is done through a multitude of passes (see simplified
representation below), and the bulk of high-level optimisations happens between
the Lambda
IR (Intermediate Representation) and CMM
(which stands
for C--). This set of optimisations will be the main focus of this series of
snippets.
Indeed, that part of the compiler is quite crowded. Originally, after
the frontend has type-checked the sources, the Closure
pass was in
charge of transforming the Lambda
IR (see source
code)
into the Clambda
IR (see source
code).
This transformation handles Constant
Propagation, some
inlining, and some
Constant Lifting (moving constant structures to static
allocation). Then, a subsequent pass (called Cmmgen
) transforms the
Clambda
IR into the CMM
ID (see source
code)
and handles some peep-hole
optimisations and
unboxing. This final representation will be used by architecture-specific
backends to produce assembler code.
Before we get any further into the hairy details of Flambda2
in the
upcoming snippets, it is important that we address some context.
We introduced the Flambda
framework which was released with OCaml 4.03
. This was a success in improving
inlining and related optimisations, and has been stable ever since,
with very few bug reports.
We kept both Closure
and Flambda
alive together because some users cared a
lot about the compilation speed of OCaml - Flambda
is indeed a bit slower
than Closure
.
Now is time to introduce another choice to both Flambda
and Closure
:
Flambda2
, which is meant to eventually replace Flambda
and potentially
Closure
as well. In fact, Janestreet has been gradually moving from Closure
and Flambda
to Flambda2
during the past year and has to this day no more
systems relying on Closure
or Flambda
.
You can read more about the transition from staging to production-level workloads of
Flambda2
right here.
Flambda
is still maintained and will be for the forseeable future. However,
we have noticed some limitations that prevented us from doing some kinds of
optimisations and on which we will elaborate in the following episodes of The
Flambda2 Snippets series.
One obvious difference to notice is that Flambda2
translates directly to CMM
,
circumventing the Clambda
IR, allowing us to lift some limitations inherent
to Clambda
itself.
Furthermore, we experimented after releasing Flambda
with the aim to
incrementally improve and add new optimisations. We tried to improve its
internal representation and noticed that we could gain a lot by doing so, but
also that it required deeper changes and that is what led us to Flambda2
.
Snippets Roadmap
This is but the zeroth snippet of the series. It aims at providing you with
history and context for Flambda2
.
You can expect the rest of the snippets to alternate between deep dives into the
technical aspects of Flambda2
, and user-facing descriptions of the new
optimisations that we enable.
The F2S Series!
-
Episode 1: CPS Representation and Foundational Design Decisions in Flambda2
The first snippet covers the characteristics and benefits of a CPS-based internal representation for the optimisation of the OCaml language. It was already covered in part at the OCaml Workshop in 2023 we try to go deeper into the subject in these blog posts.
-
Episode 2: Loopifying Tail-Recursive Functions
Coming soon...
Stay tuned, and thank you for reading!
About OCamlPro:
OCamlPro is a R&D lab founded in 2011, with the mission to help industrial users benefit from experts with a state-of-the-art knowledge of programming languages theory and practice.
- We provide audit, support, custom developer tools and training for both the most modern languages, such as Rust, Wasm and OCaml, and for legacy languages, such as COBOL or even home-made domain-specific languages;
- We design, create and implement software with great added-value for our clients. High complexity is not a problem for our PhD-level experts. For example, we developed the prototype of the Tezos proof-of-stake blockchain.
- We have a long history of creating open-source projects, such as the Opam package manager, the LearnOCaml web platform, and contributing to other ones, such as the Flambda optimizing compiler, or the GnuCOBOL compiler.
- We are also experts of Formal Methods, developing tools such as our SMT Solver Alt-Ergo (check our Alt-Ergo Users' Club) and using them to prove safety or security properties of programs.
Please reach out, we'll be delighted to discuss your challenges: contact@ocamlpro.com or book a quick discussion.
Most Recent Articles
2024
2023
- Maturing Learn-OCaml to version 1.0: Gateway to the OCaml World
- The latest release of Alt-Ergo version 2.5.1 is out, with improved SMT-LIB and bitvector support!
- 2022 at OCamlPro
- Autofonce, GNU Autotests Revisited
- Sub-single-instruction Peano to machine integer conversion
- Statically guaranteeing security properties on Java bytecode: Paper presentation at VMCAI 23
- Release of ocplib-simplex, version 0.5
- The Growth of the OCaml Distribution
2022
2021