GHC flags

The stages of a build

One ghc command runs a pipeline, and the flags of this lesson stop it at different points. The program is small on purpose, so the numbers below are about the compiler and not about the program:

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

ghc -Wall -O1 -o tiny tiny.hs
./tiny

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

The parse produces an abstract syntax tree, the type checker turns it into Core, the simplifier rewrites the Core, the code generator produces Cmm and assembly, and the assembler and linker produce the binary. Every flag below either stops the pipeline or changes one of those steps.

-fno-code: type check only

ghc -fno-code -Wall tiny.hs

[1 of 1] Compiling Main ( tiny.hs, nothing )

The pipeline stops after the type checker and writes no object file, no interface and no binary; the nothing in the bracket is the output path placeholder. This is the fastest way to answer "does it compile" in an editor hook or a test, and it is what every lesson of this course runs on every program as its first check. The warnings of -Wall still appear, because they are produced by the type checker and the renamer.

-Wall and -Werror

-Wall turns on a large group of warnings but leaves them warnings. -Werror makes every one of them an error, which is what a project that wants a clean build uses in CI. The program below imports a name it never uses:

module Main (main) where import Data.List (sort) main :: IO () main = print (1 + 1 :: Int)

ghc -Wall -Werror -fno-code warn.hs

[1 of 1] Compiling Main ( warn.hs, nothing )
warn.hs:3:1: error: [GHC-66111] [-Wunused-imports, Werror=unused-imports]
    The import of ‘Data.List’ is redundant
      except perhaps to import instances from ‘Data.List’
    To import instances alone, use: import Data.List()
  |
3 | import Data.List (sort)
  | ^^^^^^^^^^^^^^^^^^^^^^^

The header of the message names both flags: -Wunused-imports is the warning and Werror=unused-imports says it was promoted. The GHC code [GHC-66111] is stable and can be matched by a tool; the flag names can be switched off one at a time with -Wno-unused-imports. The fix here is to delete the import, and the command exits with status 1.

-O0, -O1 and -O2

The three optimisation levels are not three different compilers: they enable more passes of the same pipeline, and each level takes longer for a smaller and faster program.

The lesson on space leaks showed what -O1 can do to a lazy fold: the same source at -O0 kept 49,563,296 bytes alive and at -O1 44,328. The lesson on measuring performance runs the same program five times at -O2 and reports the minimum and the median.

-v and -ddump-timings

-v prints what the driver does: the package database it loads, the modules it chases, the command line of every tool it calls. The first lines on this machine are:

ghc -v -fno-code tiny.hs

Glasgow Haskell Compiler, Version 9.10.3, stage 2 booted by GHC version 9.10.3
*** initializing unit database:
package flags []
loading package database /usr/lib/ghc-9.10.3/lib/package.conf.d
wired-in package ghc-prim mapped to ghc-prim-0.12.0-inplace
wired-in package ghc-bignum mapped to ghc-bignum-1.3-inplace
wired-in package ghc-internal mapped to ghc-internal-9.1003.0-inplace
wired-in package base mapped to base-4.20.2.0-inplace
wired-in package rts mapped to rts-1.0.2
wired-in package template-haskell mapped to template-haskell-2.22.0.0-inplace
wired-in package ghc-experimental mapped to ghc-experimental-9.1003.0-inplace

The rest of the output is the package list, the search path and the commands, which is what to read when a build cannot find a module. For the time each pass takes, -ddump-timings prints one block per pass with an allocation count and a time in milliseconds:

ghc -Wall -O1 -ddump-timings -o tiny tiny.hs

*** Parser [Main]:
Parser [Main]: alloc=174600 time=0.475
*** Renamer/typechecker [Main]:
Renamer/typechecker [Main]: alloc=17423984 time=70.271
*** Simplifier [Main]:
Simplifier [Main]: alloc=13630152 time=29.547
*** CodeGen [Main]:
CodeGen [Main]: alloc=1533336 time=5.504
*** systool:linker:
systool:linker: alloc=1018016 time=6.482

Six of the 76 lines of that run: the type checker and the simplifier take most of the time even for a file this small, and the code generator and the linker are a few milliseconds each. The numbers move with the load of the machine, so read them as a shape and not as a score. The next lesson opens the output of the simplifier itself.

Exercises

  1. Run ghc -fno-code -Wall on a file with a type error and compare the message with a full build.
  2. Add -Wno-unused-imports to the -Werror command and see it succeed.
  3. Time a build at -O0 and at -O2 with /usr/bin/time and compare.
  4. Find the line in ghc -v output that names the assembler.
  5. Dump the timings of your own program and find the most expensive pass.
  6. Explain why -fno-code is not a substitute for a full build in CI.

Questions

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

  1. What does -fno-code do?

    Answer

    Type checks and writes no object, interface or binary

    The pipeline stops after the type checker; the bracket says nothing was written.

  2. What does -Werror do?

    Answer

    Turns every warning into an error

    The warning is promoted, which the message shows as Werror=unused-imports.

  3. Which optimisation level does cabal build use by default?

    Answer

    -O1

    The build profile line of cabal says -O1, which is also the level of this course.

  4. What does this program print? module Main (main) where main :: IO () main = print (2 + 3 :: Int)

    Answer

    5

    print shows the Int without quotes, so the line is 5.

  5. What does the nothing in Compiling Main ( tiny.hs, nothing ) mean?

    Answer

    No output file was written, as with -fno-code

    The bracket names the output path, and nothing is the placeholder for no output.

  6. What does this program print? module Main (main) where main :: IO () main = putStrLn (show (length [1, 2, 3 :: Int]))

    Answer

    3

    length gives 3 and show turns it into the string that putStrLn prints.

  7. Where does the time go for the tiny file at -O1?

    Answer

    Mostly the renamer and type checker and the simplifier

    The timing dump shows about 70 ms in the renamer and 30 ms in the simplifier.

  8. What does -Wno-unused-imports do?

    Answer

    Turns off that one warning

    The -Wno- prefix disables a single warning group.

  9. What does this program print? module Main (main) where import Data.Char (toUpper) main :: IO () main = putStrLn (toUpper <$> "x")

    Answer

    X

    The list of characters is a Functor, so <$> applies toUpper to the one character and putStrLn prints X.

  10. What does ghc -v print?

    Answer

    The package database, the modules chased and the commands run

    The verbose driver output is the record of what the compiler did.

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