Reading GHC Core

PerformanceCompiler ConstructionHaskellGhc

Performance Problem Overview


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.

What documentation is available for reading GHC Core?

Here's what I've found so far:

Performance Solutions


Solution 1 - Performance

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

enter image description here

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):

enter image description here

The primary documents to learn about GHC Core are:

Related material that can aid understanding:

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

enter image description here

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

enter image description here

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

enter image description here

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.

Solution 2 - Performance

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.

Solution 3 - Performance

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.

Solution 4 - Performance

"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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontibbeView Question on Stackoverflow
Solution 1 - PerformanceDon StewartView Answer on Stackoverflow
Solution 2 - PerformancenominoloView Answer on Stackoverflow
Solution 3 - PerformanceacfoltzerView Answer on Stackoverflow
Solution 4 - PerformanceRoman CheplyakaView Answer on Stackoverflow