Reading -ddump-simpl

What Core is

Between the type checker and the code generator GHC uses a small language called Core: a lambda calculus with data types, case expressions and coercions, and no type classes. Every class and instance is gone by then, replaced by dictionary arguments, and every do block has become a chain of binds. Reading Core is how you see what the compiler did with your program, which is the answer to most questions that start with "why is this slow".

The dump is not a bytecode listing and not an optimisation log: it is the program the rest of the compiler works on. Two flags are enough to read it: -ddump-simpl prints the Core after the simplifier, and -dsuppress-all removes the type annotations and the unique suffixes that make it unreadable at first.

A program with one function

module Main (main) where double :: Int -> Int double x = x + x main :: IO () main = print (double 21)

ghc -Wall -O1 -ddump-simpl -dsuppress-all -o core core.hs
./core

[1 of 2] Compiling Main ( core.hs, core.o )
[2 of 2] Linking core
42

The program is trivial on purpose: one function that is called once with a constant, so the whole question is whether the compiler notices.

The dump of -ddump-simpl

The dump goes to standard output while the build happens, between the compile line and the link line. It is 23 lines for this file:

==================== Tidy Core ==================== Result size of Tidy Core = {terms: 32, types: 30, coercions: 9, joins: 0/0} $trModule4 = "main"# $trModule3 = TrNameS $trModule4 $trModule2 = "Main"# $trModule1 = TrNameS $trModule2 $trModule = Module $trModule3 $trModule1 main2 = $fShowCallStack_itos' 42# [] main1 = \ eta_aTl -> hPutStr2 stdout main2 True eta_aTl main = main1 `cast` <Co:3> :: ... main3 = runMainIO1 (main1 `cast` <Co:3> :: ...) main = main3 `cast` <Co:3> :: ...

Reading the result

Four things are worth finding in that dump:

What is not there is as instructive: no double, no +, and no dictionary for Show Int beyond the name of the method that builds the string. That is the simplifier doing its job, and the reason a profile of this program would show the printing and nothing else.

The same file at -O0

Without optimisation the call survives as a local binding and an addition. The same command with -O0 gives this definition of main:

main   = print       $fShowInt (let { x_awr = I# 21# } in + $fNumInt x_awr x_awr)

The structure is the same and the folding is missing: x_awr is an Int holding 21 and the addition is a call to the Num Int method. The two dumps side by side are the clearest demonstration of what -O1 buys, and the reason the space-leak lesson measured the same source at both levels.

Two practical notes. First, the dump is per module, so a multi-module program needs the flag on the build of the module you are reading, and Cabal passes it with --ghc-options=-ddump-simpl or a ghc-options field. Second, -ddump-simpl prints after each simplifier pass unless you add -dno-suppress-all style flags; if the output is long, -ddump-simpl -dsuppress-all -dno-suppress-uniques is the combination that two names can be searched in. The next lesson goes the other way: profiling, and why it is not installed here.

Exercises

  1. Dump the Core of a function with a list literal and find out whether the list is built at run time.
  2. Compare the Core of foldl (+) 0 [1 .. 10] at -O0 and -O1.
  3. Find the dictionary argument of a function with a Show constraint.
  4. Explain what a cast in the dump means and why it costs nothing.
  5. Dump a program with a type class of your own and find its dictionary.
  6. Explain why the dump has no do and no where.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What is Core?

    Answer

    The small lambda calculus GHC uses after type checking

    Core is the intermediate language between the type checker and the code generator.

  2. What does -dsuppress-all do?

    Answer

    Removes type annotations and unique suffixes from the dump

    The flag makes the dump readable by hiding the annotations that the compiler keeps internally.

  3. What happened to double 21 in the -O1 dump?

    Answer

    It was folded into the literal 42#

    The simplifier evaluated the constant call, so the program prints a literal.

  4. What does this program print? module Main (main) where triple :: Int -> Int triple x = x * 3 main :: IO () main = print (triple 14)

    Answer

    42

    14 times 3 is 42, and print shows it without quotes.

  5. Which of these is NOT in the Core dump?

    Answer

    do notation

    do notation is desugared before Core, so only binds remain.

  6. What does a cast in the dump represent?

    Answer

    A proof that two types are equal, free at run time

    Coercions are erased by the code generator, so they cost nothing.

  7. What does this program print? module Main (main) where main :: IO () main = print (length [1 .. 4 :: Int])

    Answer

    4

    length of the four-element range is 4.

  8. What does the -O0 dump of the lesson keep that -O1 removed?

    Answer

    A local binding for 21 and a call to the Num method

    Without optimisation the addition and the local binding survive in main.

  9. Why does the dump show $fShowCallStack_itos?

    Answer

    The dictionary of Show Int is a record of methods with such names

    Class methods become dictionary fields, and the names encode the module and the class.

  10. Where does -ddump-simpl write its output?

    Answer

    Standard output of the compiler, between the compile and link lines

    The dump is printed by the compiler during the build, as the cout block of this page shows.

The code on this page was built with GHC 9.10.3 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev