Getting Started with LLVM and the Ocaml LLVM Tutorial

I recently started playing with LLVM, and I ran into a few issues with getting started that didn’t have google-able solutions, so I’m posting this on the off chance it might help someone else.

The directions on the LLVM website are very detailed, but intimidatingly long. Getting a build working is actually really simple. Aaron Gray put together an amazingly easy four step guide on bulding LLVM for cygwin here (and the directions worked fine for me on Unbuntu, too). It looks like he was working with an older version of LLVM, and you can actually skip his “build the tools” step. If you want to do that step anyway, and follow his instructions with the current version of cygwin, the tools will drop into a “Debug+Asserts” directory, instead of a “debug” directory.

Also, on cygwin, you can build with gcc3 or gcc4 but, if you have gcc3 and ocaml installed, you have to install gcc4 (or fiddle with your path). Ocaml installs enough components of gcc4 that configure detects that you have gcc4, but during the compile process, cc1plus from gcc3 will complain about arguments that are only legal with gcc4:

cc1plus: error: unrecognized command line option “-Wno-variadic-macros”
make[3]: *** [llvm-main.o] Error 1

If you’re on 64-bit Ubuntu, you’ll need to do sudo apt-get install libc6-dev-i386, or you’ll get an error related to stubs-32.h

That’s it! It’s really that simple.

If you want to build a back-end, there’s a nice guide here. As with the official instructions, the prerequisite reading list, which states “These essential documents must be read before reading this document”, is intimidating long, but it’s actually simple enough that you can just dive in and refer to the detailed manual when an obscure reference pops up, JIT :-).

There’s an even simpler guide to writing a front-end by Chris Lattner here, with an Ocaml translation by Erick Tryzelaar here.

The Ocaml tutorial just requires LLVM + Ocaml + camlp4. If you don’t have camlp4, you’ll know it because you’ll get

/usr/bin/ocamldep -pp camlp4of -modules >
sh: camlp4of: not found

On Ubuntu, you just need to do sudo apt-get install camlp4-extra to fix that.

Also, the tutorial has a few minor bugs.

In chapter 3

| '+' -> build_add lhs_val rhs_val "addtmp" builder
| '-' -> build_sub lhs_val rhs_val "subtmp" builder
| '*' -> build_mul lhs_val rhs_val "multmp" builder

Should be

| '+' -> build_fadd lhs_val rhs_val "addtmp" builder
| '-' -> build_fsub lhs_val rhs_val "subtmp" builder
| '*' -> build_fmul lhs_val rhs_val "multmp" builder

Since everything is converted to a double, but the base build_* functions expect an int, you’ll get

lvm::BinaryOperator::init(llvm::Instruction::BinaryOps): Assertion `getType()->isIntOrIntVectorTy() && “Tried to create an integer operation on a non-integer type!”‘ failed.

unless you use operations that expect floats

Similarly, in Chapter 5,

let next_var = build_add variable step_val "nextvar" builder in

should be

let next_var = build_fadd variable step_val "nextvar" builder in

Also, in chapter 4, to get extern functions working, see this.

And if you compiled using the instructions above, you’ll need to build with ocamlbuild -cflags -I,+llvm-2.8 -lflags -I,+llvm-2.8 toy.byte instead of just ocamlbuild toy.byte (or you can modify your build file).

Getting Started with LLVM and the Ocaml LLVM Tutorial

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s