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:
- main2 = $fShowCallStack_itos' 42# [] is the important line: double 21 became the literal 42#, a machine integer. The function and the call are gone, so the binary cannot be slower than printing a constant.
- The $trModule definitions are the module name of the program, built once as a small structure. They are in every dump and say nothing about your code.
- main = main1 `cast` <Co:3> :: ... is a coercion: the type checker needed a proof that two types are the same, and the cast is free at run time. A dump full of casts is normal in code that uses newtypes.
- main1 and main3 are the printing function and the runtime entry point. The 42# flows through them as a value of type Int.
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
- Dump the Core of a function with a list literal and find out whether the list is built at run time.
- Compare the Core of foldl (+) 0 [1 .. 10] at -O0 and -O1.
- Find the dictionary argument of a function with a Show constraint.
- Explain what a cast in the dump means and why it costs nothing.
- Dump a program with a type class of your own and find its dictionary.
- 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.
-
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.
-
What does
-dsuppress-alldo?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.
-
What happened to
double 21in the -O1 dump?Answer
It was folded into the literal 42#
The simplifier evaluated the constant call, so the program prints a literal.
-
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.
-
Which of these is NOT in the Core dump?
Answer
donotationdo notation is desugared before Core, so only binds remain.
-
What does a
castin 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.
-
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.
-
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.
-
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.
-
Where does
-ddump-simplwrite 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