3
votes

According to this SO post compilation of Haskell programs to C is no longer (officially) supported. So I wanted to explore the option of compiling Haskell programs to LLVM IR. I chose the same program of the mentioned post:

quicksort [] = []
quicksort (p:xs) = (quicksort lesser) ++ [p] ++ (quicksort greater)
  where
    lesser  = filter (<  p) xs
    greater = filter (>= p) xs

main = print(quicksort([5,2,1,0,8,3]))

and then tried to compile it to LLVM IR with:

$ ghc -fllvm main.hs

Then I get this error regarding the LLVM version:

<no location info>: error:
    Warning: Couldn't figure out LLVM version!
             Make sure you have installed LLVM 3.7
ghc: could not execute: opt-3.7

When I check my opt version it's 3.8.0, which is bigger:

$ opt --version
LLVM (http://llvm.org/):
  LLVM version 3.8.0
  DEBUG build with assertions.
  Built Jun 20 2018 (14:59:34).
  Default target: x86_64-unknown-linux-gnu
  Host CPU: broadwell

So what's going on? could ghc expect exactly version 3.7.0 and only that?!

EDIT:

After installing llvm 3.7.0 and copying opt and llc to have 3.7 suffixes:

$ cp opt opt-3.7
$ cp llc llc-3.7

compilation to llvm goes without errors, using this line:

$ ghc -keep-llvm-files main.hs

and a file called main.ll is created.

1
Here GHC 8.4.3 requires LLVM 5.0. I guess each GHC version wants its own LLVM, but I'm unsure about it. - chi
Yes The LLVM IR is notoriously unstable with no formal spec (but a reasonable english reference). When patching a parser for the bitcode I've had to look at the source code to LLVM quite a bit to get clarity on issues. Similarly with the seemingly meaningless twiddles of human readable LLVM IR syntax between versions. - Thomas M. DuBuisson

1 Answers

4
votes

Yes, GHC expects an exact version of LLVM. The LLVM internals change very quickly, and so GHC (like many other tools which target or use LLVM) takes a very conservative approach to versioning of those tools.