Evan Martin (evan) wrote in evan_tech,
Evan Martin
evan
evan_tech

ghc llvm

I read this thesis on an LLVM backend for GHC, primarily because I was curious to learn more about GHC internals. The thesis serves well as an overview of the pieces. As for the actual change, it seems to fall somewhere in between compiling via C and generating machine code directly on the sorts of tradeoffs you'd expect (generating machine code directly = more control, more performance, more work).

GHC is designed around the spineless tagless G-machine, a paper I've never gotten around to reading, but conceptually is like a VM that GHC targets and then codegens from. It has a number of values ("virtual registers") it wants to keep track of while running, like the current stack pointer and current thread object (see table 2.1 in the paper). Because they're used so frequently, GHC pins these through to machine registers. I was surprised to read that on x86 this leaves only one register free for doing computations!

That was mentioned in a section describing one place where the LLVM implementation outperforms the GHC implementation: because LLVM doesn't support this register pinning directly, the code generator instead sets things up so that the relevant values are frequently (at the entrance to each function, in fact) in the appropriate registers, with the assumption that the LLVM optimizer will leave the values alone since each function call would otherwise need to restore those values in the appropriate registers. But in fact, sometimes it does make sense to spill those to the stack, and those are exactly some of the cases where the LLVM implementation is superior.

(Update: found an old thread where SPJ briefly discusses LLVM.)
Tags: go read, haskell
Subscribe

  • blog moved

    As described elsewhere, I've quit LiveJournal. If you're interested in my continuing posts, you should look at one of these (each contains feed…

  • dremel

    They published a paper on Dremel, my favorite previously-unpublished tool from the Google toolchest. Greg Linden discusses it: "[...] it is capable…

  • treemaps

    I finally wrote up my recent adventures in treemapping, complete with nifty clickable visualizations.

  • Post a new comment

    Error

    default userpic
    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.
  • 0 comments