# \[BLOG\] The Flambda2 Snippets, by OCamlPro

**URL:** <https://discuss.ocaml.org/t/blog-the-flambda2-snippets-by-ocamlpro/14331>\
**Category:** Learning\
**Tags:** flambda, compiler, flambda2, blog\
**Created:** [March 19, 2024, 4:46pm UTC](https://discuss.ocaml.org/t/blog-the-flambda2-snippets-by-ocamlpro/14331 "2024-03-19T16:46:49Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![OCamlPro](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/ocamlpro/32/2011_2.png) [@OCamlPro](https://discuss.ocaml.org/u/OCamlPro)\
**Post date:** [March 19, 2024, 4:46pm UTC](https://discuss.ocaml.org/t/blog-the-flambda2-snippets-by-ocamlpro/14331/1 "2024-03-19T16:46:49Z")

</div>

Greetings Cameleers,

Today, we are excited to share with you a first glance at some redactional work  
that has been brewing behind at the scenes at OCamlPro for quite a some time  
now!

We are starting a series of blogposts on the Flambda2 project. The goals are  
plenty, one of them being to give all readers an idea of the inner workings of  
this great piece of software, 10 years of research & development in the making.

The first two episodes are rather special to the series:

- [Episode 0](https://ocamlpro.com/blog/2024_03_18_the_flambda2_snippets_0/) gives context and broader information on both the Flambda2 Optimising Compiler project, and the series of blogposts itself.

- [Episode 1](https://ocamlpro.com/blog/2024_03_19_the_flambda2_snippets_1/), on the other hand, steps right into the subject at hand and covers some of the foundational design decisions of this compiler.

We await your feedback below, and hope that you will enjoy reading these posts, and all ensuing ones!

Kind regards,  
The OCamlPro Team

---

<div class="post-metadata">

**Author:** ![rbjorklin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/rbjorklin/32/1126_2.png) [@rbjorklin](https://discuss.ocaml.org/u/rbjorklin)\
**Post date:** [March 19, 2024, 10:51pm UTC](https://discuss.ocaml.org/t/blog-the-flambda2-snippets-by-ocamlpro/14331/2 "2024-03-19T22:51:57Z")

</div>

This all looks very interesting. Is there an easy way for me to try out flambda2?

---

<div class="post-metadata">

**Author:** ![lambda\_foo](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/lambda_foo/32/275_2.png) [@lambda\_foo](https://discuss.ocaml.org/u/lambda_foo)\
**Post date:** [March 20, 2024, 4:20am UTC](https://discuss.ocaml.org/t/blog-the-flambda2-snippets-by-ocamlpro/14331/3 "2024-03-20T04:20:36Z")

</div>

The instructions for compiling from source on [GitHub - ocaml-flambda/flambda-backend: The Flambda backend project for OCaml](https://github.com/ocaml-flambda/flambda-backend) should work. I’ve been using those on MacOS ARM64 and x86\_64.

There is also a section for [building an opam switch](https://github.com/ocaml-flambda/flambda-backend/blob/main/HACKING.md#testing-the-compiler-built-locally-with-opam), I haven’t used it so can’t confirm it works.

---

<div class="post-metadata">

**Author:** ![Ban](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/ban/32/5017_2.png) [@Ban](https://discuss.ocaml.org/u/Ban)\
**Post date:** [March 20, 2024, 8:04am UTC](https://discuss.ocaml.org/t/blog-the-flambda2-snippets-by-ocamlpro/14331/4 "2024-03-20T08:04:29Z")

</div>

I wasn’t familiar with First-Class CPS (thanks for introducing me to this!), but shouldn’t the last line of the Tail-recursive CPS implementation of map be

`| x :: r -> let fx = f x in map_cps f r (fun r -> k(fx :: r))`

i.e., include `k`?

---

<div class="post-metadata">

**Author:** ![vlaviron](https://avatars.discourse-cdn.com/v4/letter/v/ec9cab/32.png) [@vlaviron](https://discuss.ocaml.org/u/vlaviron)\
**Post date:** [March 20, 2024, 1:27pm UTC](https://discuss.ocaml.org/t/blog-the-flambda2-snippets-by-ocamlpro/14331/5 "2024-03-20T13:27:06Z")

</div>

You’re right, that’s a mistake. We should have the correct version up now. Thanks for reporting !

---

<div class="post-metadata">

**Author:** ![Kakadu](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/kakadu/32/1204_2.png) [@Kakadu](https://discuss.ocaml.org/u/Kakadu)\
**Post date:** [March 21, 2024, 3:13pm UTC](https://discuss.ocaml.org/t/blog-the-flambda2-snippets-by-ocamlpro/14331/7 "2024-03-21T15:13:11Z")

</div>

Trying flambda2 is really an issue. I tried to report it but with no luck. Could you help?

> <https://github.com/ocaml-flambda/flambda-backend/issues/1794>
>
> There are some examples in tests/simd, I tried them, but only think I got is a c…all of C primitive. So I decided to call compiler out of the source tree of flambda2. As you can see, it tried to call C stubs, which is not what I expect. 
> 
> How to invoke compiler properly? I'm on amd64
> 
> \`\`\`ocaml
> ➜ flambda\_backend\_test make                                             
> /media/work/asp/flambda-backend/\_install/bin/ocamlopt.opt -I /media/work/asp/flambda-backend/\_install/lib/ocaml -extension simd -S test1.ml -o test1.exe
> /usr/bin/ld: test1.o: in function \`camlTest1\_\_entry':
> :(.text+0x1d): undefined reference to \`caml\_nativeint\_popcnt\_unboxed\_to\_untagged'
> /usr/bin/ld: test1.o: in function \`camlTest1':
> :(.data+0x50): undefined reference to \`caml\_nativeint\_popcnt\_unboxed\_to\_untagged'
> collect2: error: ld returned 1 exit status
> File "caml\_startup", line 1:
> Error: Error during linking (exit code 1)
> make: \*\*\* \[Makefile:7: test1.exe\] Error 2
> ➜ flambda\_backend\_test cat test1.ml flambda-backend
> open Stdlib
> 
> external popcnt : nativeint -\> nativeint = "" "caml\_nativeint\_popcnt\_unboxed\_to\_untagged" \[@@noalloc\] \[@@unboxed\]
> 
> let() = Printf.printf "%d\\n" (popcnt 15n |\> Nativeint.to\_int)
> \`\`\`

---

<div class="post-metadata">

**Author:** ![vlaviron](https://avatars.discourse-cdn.com/v4/letter/v/ec9cab/32.png) [@vlaviron](https://discuss.ocaml.org/u/vlaviron)\
**Post date:** [March 21, 2024, 3:30pm UTC](https://discuss.ocaml.org/t/blog-the-flambda2-snippets-by-ocamlpro/14331/8 "2024-03-21T15:30:23Z")

</div>

Opam support is still in progress. We have something that seems to work, but we need a bit more testing and haven’t updated yet the instructions in the readme. We hope to have instructions that boil down to “add the janestreet-bleeding repo, then install this opam package”, but finding how to generate our opam package correctly took a while and it’s possible that some subtle bugs are still hidden somewhere.

---

<div class="post-metadata">

**Author:** ![Kakadu](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/kakadu/32/1204_2.png) [@Kakadu](https://discuss.ocaml.org/u/Kakadu)\
**Post date:** [March 28, 2024, 8:35am UTC](https://discuss.ocaml.org/t/blog-the-flambda2-snippets-by-ocamlpro/14331/9 "2024-03-28T08:35:14Z")

</div>

A couple of questions:

- I’m curious about your opinion about “Compiling without continuations” [https://dl.acm.org/doi/pdf/10.1145/3062341.3062380](https://dl.acm.org/doi/pdf/10.1145/3062341.3062380) . Some body may think that continuations are unneeded complication comparately to direct approaches. What are the cases when CPS representation works better than conventional ones (=Haskell)?

- Should CPS based representation help to optimize something like [Manual CPS is faster than CPS monad. Is it expected?](https://discuss.ocaml.org/t/manual-cps-is-faster-than-cps-monad-is-it-expected/13560) ? Or do we need to add purity inference CPS IR too?

---

<div class="post-metadata">

**Author:** ![vlaviron](https://avatars.discourse-cdn.com/v4/letter/v/ec9cab/32.png) [@vlaviron](https://discuss.ocaml.org/u/vlaviron)\
**Post date:** [March 28, 2024, 11:53am UTC](https://discuss.ocaml.org/t/blog-the-flambda2-snippets-by-ocamlpro/14331/10 "2024-03-28T11:53:00Z")

</div>

> [@Kakadu](#):
>
> I’m curious about your opinion about “Compiling without continuations” [https://dl.acm.org/doi/pdf/10.1145/3062341.3062380](https://dl.acm.org/doi/pdf/10.1145/3062341.3062380) . Some body may think that continuations are unneeded complication comparately to direct approaches. What are the cases when CPS representation works better than conventional ones (=Haskell)?

As far as I can tell, we’re doing exactly what they propose, but they worry about continuations being interpreted as first-class functions so they call them join-points, while we don’t care and call them continuations.

> [@Kakadu](#):
>
> Should CPS based representation help to optimize something like [Manual CPS is faster than CPS monad. Is it expected?](https://discuss.ocaml.org/t/manual-cps-is-faster-than-cps-monad-is-it-expected/13560) ? Or do we need to add purity inference CPS IR too?

This is about first-class CPS, so our second-class CPS compilation scheme will not have any impact on those issues.

---

<div class="post-metadata">

**Author:** ![gadmm](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/gadmm/32/877_2.png) [@gadmm](https://discuss.ocaml.org/u/gadmm)\
**Post date:** [March 30, 2024, 9:45pm UTC](https://discuss.ocaml.org/t/blog-the-flambda2-snippets-by-ocamlpro/14331/11 "2024-03-30T21:45:01Z")

</div>

@kakadu You might be interested in the following paper which was born out of an attempt to reproduce the “Compiling without continuations” paper (with apologies to people who already saw it the previous times I mentioned it): [Compiling with continuations, or without? whatever. | Proceedings of the ACM on Programming Languages](https://dl.acm.org/doi/10.1145/3341643).

And of course it is very interesting to hear about everything the Flambda2 team has to tell us about their experience.
