开发者

Reading GHC Core

开发者 https://www.devze.com 2023-03-08 09:55 出处:网络
Core is GHC\'s intermediate language. Reading Core can help you better understand the performance of your program. Someone asked me for documentation or tutorials on reading Core, but I couldn\'t find

Core is GHC's intermediate language. Reading Core can help you better understand the performance of your program. Someone asked me for documentation or tutorials on reading Core, but I couldn't find much.

Wh开发者_开发技巧at documentation is available for reading GHC Core?

Here's what I've found so far:

  • Write Haskell as fast as C: exploiting strictness, laziness and recursion
  • Haskell as fast as C: working at a high altitude for low level performance
  • RWH: Chapter 25. Profiling and optimization
  • High-Performance Haskell talk at CUFP (slide 65-80)


GHC Core is the System FC language into which all Haskell is translated. The (approximate) grammar for Core is given by:

Reading GHC Core

Core is closely related to the simpler and better known System F. All transformations GHC does on the Core level are type-preserving refactorings of this Core representation, to improve performance. And, not so well known, you can write directly in Core to program GHC.

GHC Core fits in the compiler pipeline (as it was in 2002, sans-LLVM and CMM):

Reading GHC Core

The primary documents to learn about GHC Core are:

  • An External Representation for the GHC Core Language, Tolmach, 2001
  • GHC.Core.Expr, the GHC definition itself
  • Secrets of the Glasgow Haskell Compiler inliner, Peyton Jones and Marlow, 1999. Core is described in Section 2.3, including details on the occurrence analysis annotations.
  • A transformation-based optimiser for Haskell, Peyton Jones and Santos, 1998. Core is described in S3, including a discussion of polymorphism and operational readings of Core.

Related material that can aid understanding:

  • The GHC -fext-core output
  • I spent a lot of time learning Core by reading GHC source. Some is described in my undergraduate thesis from 2002, from page 16.
  • From using the ghc-core tool, to generate Core in a format I find pleasing.

Core in turn is translated into STG code, which looks something like:

Reading GHC Core

The funny names in Core are encoded in the "Z-encoding":

Reading GHC Core

GHC Core's types and kinds (from Tolmach's paper):

Reading GHC Core

Finally, GHC's primops appear regularly in GHC Core output, when you have optimized your Haskell down to the basic instructions GHC knows about. The primop set is given as a set of Core functions in a pre-processed file.


A tip: If you don't care about type annotations and coercions use -ddump-simpl together with the -dsuppress-all option. The Core output should be much more readable.


Although not exactly the GHC Core language, as Don mentions the STG language is quite similar. I recently went through the exercise of proving type safety of the STG language + machine, and afterwards I found I could understand Core easily.

The text I used to learn STG is quite accessible: Implementing Lazy Functional Languages on Stock Hardware: The Spineless Tagless G-machine by Simon Peyton-Jones. Much of the paper is concerned with implementation details, but I recommend section 4 in particular as a top-to-bottom explanation of the STG language that gives motivations for some of the counter-intuitive design decisions and provides translations of familiar examples like map.


"An External Representation for the GHC Core Language" is a document which can be found in the installation of ghc (share/doc/ghc/core.pdf) or on the internet.

0

精彩评论

暂无评论...
验证码 换一张
取 消