28

I've been excited about LLVM being low enough to model any system, and saw it as promising that Apple was adopting it; but then again Apple doesn't specifically support Haskell;

And, some think that Haskell would be better off with C--:

That LLVM'ers haven't solved the problem of zero-overhead garbage collection isn't too surprising. Solving this while staying agnostic of the data model is an open question in computer science.

-- LHC won't be using LLVM.

5
  • 10
    you need to get a blog May 3, 2009 at 0:35
  • 1
    You need to use the quote option for long quotes like that.
    – Unknown
    May 3, 2009 at 1:08
  • The edit seems to show that it was pasted from a blog. May 9, 2009 at 15:13
  • but before the edit it was using the paste was in quotes
    – dr.addn
    May 9, 2009 at 15:18
  • . I was worried about my source link disappearing, sorry .
    – dr.addn
    May 9, 2009 at 15:26

4 Answers 4

29

Having worked a bit with the new code generation backend which manipulates C--, I can say there are a number of reasons why C-- can be better than LLVM, and also why they’re not really at all the same thing.

  1. C-- operates at a higher level of abstraction than LLVM; for example, we can generate code in C-- where the stack pointer is entirely implicit, and only manifest it later during the compilation process. This makes applying certain types of optimizations much easier, because the higher level representation allows for more code motion with less invariants.

  2. While we’re actively looking to fix this, LLVM suffers from the same problem that the via-C backend suffered: it requires us to create proc points. What are proc points? Essentially, because Haskell does not use the classic call/ret calling convention, whenever we make the moral equivalent of a subprocedure call, we need to push a continuation onto the stack and then jump to the subprocedure. This continuation is usually a local label, but LLVM requires it to be an actual procedure, so we need to break functions into smaller pieces (each piece being called a proc point). This is bad news for optimizations, which work on a procedure-level.

  3. C-- and LLVM take a different approach to dataflow optimization. LLVM uses traditional SSA style with phi-nodes: C-- uses a cool framework called Hoopl which doesn’t require you to maintain the SSA invariant. I can confirm: programming optimizations in Hoopl is a lot of fun, though certain types of optimizations (inlining of one-time used variables comes to mind) are not exactly the most natural in this dataflow setting.

1
  • 1
    When you say C--, are you referring to the main C-- implementation or GHC's Cmm variant. Aug 21, 2015 at 23:15
22

Well, there is a project at UNSW to translate GHC Core to LLVM

Remember: it wasn't clear 10 years ago that LLVM would build up all the infrastructure C-- wasn't able to. Unfortunately, LLVM has the infrastructure for portable, optimized code, but not the infrastructure for nice high level language support, that C-- ha(s)d.

An interesting project would be to target LLVM from C-- ...


Update, as of GHC 7, GHC uses LLVM for code generation. Use the -fllvm flag. This has improved numerical performance for some low level programs. Otherwise, performance is similar to the old GCC backend.

3
  • . great answer; that was just the blindspot-undo I was looking for! . llvm'ers had a similar response to the lack of concurrency support: it's an add-on library thing . . c-- can be ported to llvm, meaning that llvm's gc simply won't be used .
    – dr.addn
    May 9, 2009 at 15:36
  • 1
    the link is no longer working :(, has the situation changed at all since the last update? Feb 24, 2014 at 5:44
  • GHC uses Cmm (their variant of C--) as the intermediate representation anyway, then it can be compiled to the native code directly (-fasm) or via LLVM (-fllvm), see the docs for details.
    – user
    Jun 4, 2019 at 10:28
14

GHC now officially has an LLVM backend, and it turns out that it's competitive with the GCC and native-codegen and actually faster in some cases. And the LLVM project has accepted the new calling convention David Terei created for Haskell on LLVM, so amazingly, the two projects are actually working together now.

1

One issue in practice is LLVM has been much more of a moving target.

GHC has had some trouble trying to support multiple versions of LLVM. There is an active discussion on the ghc-dev mailing list about this.

Btw, currently the llvm backend in ghc is after the Haskell is translated to the cmm language(which I believe is mostly just C-- extended with certain registers from the STG Language), and due to the above to-be-addressed difficulties, there are redundant optimizations being done which slows down the compilation.

Also, historically, and currently AFAIK, the LLVM project doesn't prioritize providing a portable platform, and some developers have made a point of articulating that it is a compiler IR and not a form of portable assembly language.

The LLVM IR you write for one intendend target may not at all be useful for a different intended target. For comparison, the C-- website actually refers to it as portable assembly. "You would be much happier with one portable assembly language that could be ..." is a quote from their website. That website also mentions a runtime interface to ease portable implementation of garbage collection and exception handling.

So you could think of C-- as a portable common ground for all of the front ends that has a bit more in common with CIL and Java byte code and LLVM IR as an expressive common-ground for all of your backends that facilitates unifying low-level optimizations common to multiple targets. LLVM IR also provides the added bonus that the LLVM project will implement a lot of those low level optimization. That being said, in some ways LLVM IR could actually be considered higher level than C--, for example LLVM IR has different types where as in C-- everything is just bit vectors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.