4

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.

2
  • 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. Commented Aug 27, 2018 at 13:51
  • 2
    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. Commented Aug 27, 2018 at 13:53

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

5 Comments

I've edited my question after installing llvm-3.7.0 which now compiles without errors ... but I can't find the generated llvm IR file if one was indeed created ...
@OrenIshShalom See the fine manual.
great it works! one last question I promise: I edited the question again to try to run the generated *.bc file (with llvm-as from *.ll) but it misses the main function which was there in the original Haskell version ... any idea where it went?
@OrenIshShalom Dunno, I've never played with LLVM before, believe it or not. Might want to open a fresh question with the details. As an aside, what you've done here twice now (editing your question to change what you're asking) is generally frowned upon on SO: edits to clarify your question are fine, but if you come up with new questions as a result of what you learn, those should go in a fresh question instead.
Removed second EDIT altogether, and kept the first one indicating the exact command line that did work.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.