Getting GHC HEAD and LLVM working together
Since dons’ two posts, I have seen many people on the haskell-cafe mailing-list and the haskell-related IRC channels asking how they could get GHC HEAD and the LLVM-related stuff, build them and all. There are a lot of this information on the ghc trac but I have been told it would be nice to have a comprehensive guide with all the necessary steps and information.
Getting, patching and building LLVM
It is as simple as executing the following commands:
$ svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm
$ cd llvm
$ patch -p0 -i ~/llvm-ghc.patch
$ ./configure –enable-optimized # probably also want to set –prefix
$ make
$ make install
where the given llvm-ghc.patch file can be downloaded here : http://www.cse.unsw.edu.au/~davidt/downloads/llvm-ghc.patch.
Getting GHC HEAD and Patching it to add the LLVM backend
First, get the latest archive matching that pattern : ghc-HEAD-AAAA-MM-JJ-ghc-corelibs-testsuite.tar.{gz or bz2} at http://darcs.haskell.org/, the latest currently being ghc-HEAD-2009-10-23-ghc-corelibs-testsuite.tar.gz. It ships GHC with all its dependencies, also including a testsuite.
Put the archive file in (on my computer) /home/alp/haskell/. Uncompress it with for example:
$ tar xzf ghc-HEAD-2009-10-23-ghc-corelibs-testsuite.tar.gz
You should get a /home/alp/haskell/ghc/ directory. Now we have to fetch the latest patches applied to GHC since the archive you downloaded got published on darcs.haskell.org. To do this, simply do:
$ chmod +x ./darcs-all
$ ./darcs-all pull -a
(the former may not be necessary though)
It can take some time, there may be a lot of patches to fetch and apply, don’t worry.
If you are not interested in having the LLVM backend, you can stop reading that section and go straight to the next one.
Once done with that, you have to download the patch to add the support to the LLVM backend to GHC. Just download the following file : http://www.cse.unséw.edu.au/~davidt/downloads/ghc-llvmbackend-full.gz (it actually just is a darcs patch, not an archive or anything else — you may want to rename it with a .patch extension), put it in ghc’s directory (/home/alp/haskell/ghc/ here) and apply the patch :
$ darcs apply ghc-llvmbackend-full.patch
(or .gz)
You just have one more patch to apply (isn’t life about applying patches?), written by Don Stewart, to be able to pass LLVM options to GHC without conflicting with other options. You can get it at http://www.galois.com/~dons/add-new-llvm-code-generator-to-ghc_.dpatch. Just move it to your ghc directory and darcs apply it, like you just did for the previous patch. (note: this step might not be necessary if the previous patch or the ghc repository contain it)
Ok, we are almost done! Now create a file named “build.mk” in (on my computer) /home/alp/haskell/ghc/mk/, and put the following content in that file:
GhcWithLlvmCodeGen = YES
GhcEnableTablesNextToCode = NO
The first, obviously, enables LLVM code generation in GHC. The second disables a feature currently being in conflict between the LLVM and the C code generators, if I remember correctly. Now, let’s build GHC, hooray!
Building and using the patched GHC non-intrusively
Once all the patches are fetched and applied, you just have to do:
$ sh boot
$ ./configure
$ make
Now, have a cup of coffee, read newspapers, try to solve P vs NP, whatever. It takes some time.
Once done with ‘make’, there should be many binaries in (on my computer) /home/alp/haskell/ghc/inplace/bin/. The ghc binary is called ghc-stage2
To check that the LLVM backend is enabled, as explained on Don’s post, just do the following (in the your ghc/inplace/bin/ directory):
$ ghc-stage2 –info
and verify that it gives you (“Have llvm code generator”,”YES”).
To make use of them properly without installing your brand new GHC HEAD system-wide, here is one possibility; add the following to your ~/.bashrc (or whatever you use, it’s just about defining variables):
GHC613 = /home/alp/haskell/ghc/inplace/bin
GHC613BIN=$GHC613/ghc-stage2
GHC613PKG=$GHC613/ghc-pkg
And now, when you will want to install libraries via cabal for your new GHC, just execute commands close to the following (to install say the vector package):
$ cabal install vector –with-compiler=$GHC613BIN –with-hc-pkg=$GHC613PKG
And it will build and install the vector package using GHC HEAD and its library directories (in ~/.ghc/).
Moreover, if you want to build packages with the llvm backend, just ask cabal gently:
$ cabal install vector –with-compiler=$GHC613BIN –with-hc-pkg=$GHC613PKG –ghc-options=-fllvm
You can even chain options this way, passing for example -optlo-03. For more informations on that, check Don’s posts:
- Smoking fast Haskell code using GHC’s new LLVM codegen
- Evolving Faster Haskell Programs (now with LLVM!)
But be careful, -fvia-C and -fllvm aren’t much friends, you may sometimes have to either edit the .cabal file of the package you want to install and removing -fvia-C from there, or via the –flags option of cabal install. For more informations, cabal help install.
Here are few links that you may find to be of interest:
- Home of the LLVM backend section of GHC’s trac wiki
- The Building and Porting section of GHC’s trac wiki
- David Terei’s thesis about the LLVM backend for GHC [PDF] — thank you David !
I hope this post will be of help and that it will lead some of you to contribute to GHC !
18 comments