Version 18 (modified by simonmar, 6 years ago) (diff)


Recompilation Avoidance

What is recompilation avoidance?

When GHC is compiling a module, it tries to determine early on whether

  • The object file (or byte-code in the case of GHCi) and interface file exist from a previous compilation
  • Recompilation is sure to produce exactly the same results, so it is not necessary.

If both of these hold, GHC stops compilation early, because the existing object code and interface are still valid. In GHCi and --make, we must generate the ModDetails from the ModIface, but this is easily done by calling MkIface.typecheckIface.


Let's use a running example to demonstrate the issues. We'll have four modules with dependencies like this:

     / \
    B   C
     \ /


module A where
import B
import C

a = print (f 2)


module B (f) where
import D


module C where
import D


module D (T, f, h) where

data T a b = C1 a | C2 b

f :: Int -> Int
f x = h x

h :: Int -> Int
h x = x + 3

Why do we need recompilation avoidance?

GHCi and --make

The simple fact is that when you make a small change to a large program, it is often not necessary to recompile every module that depends directly or indirectly on something that changed. In GHCi and --make, GHC considers every module in the program in dependency order, and decides whether it needs to be recompiled, or whether the existing object code and interface will do.


make works by checking the timestamps on dependencies and recompiling things when the dependencies are newer. Dependency lists for make look like this (generated by ghc -M):

# DO NOT DELETE: Beginning of Haskell dependencies
D.o : D.hs
B.o : B.hs
B.o : D.hi
C.o : C.hs
C.o : D.hi
A.o : A.hs
A.o : C.hi
A.o : B.hi
# DO NOT DELETE: End of Haskell dependencies

Only the .hi files of the direct imports of a module are listed. For example, A.o depends on C.hi and B.hi, but not D.hi. Nevertheless, if D is modified, we might need to recompile A. How does this happen?

  • first, make will recompile D because its source file has changed, generating a new D.o and D.hi.
  • If after recompiling D, we notice that its interface is the same as before, there is no need to modify the .hi file. If the .hi file is not modified by the compilation, then make will notice and not recompile B or C, or indeed A. This is an important optimisation.
  • Suppose the change to D did cause a change in the interface (e.g. the type of f changed). Now, make will recompile both B and C. Suppose that the interfaces to B and C remain the same: B's interface says only that it re-exports D.f, so the fact that f has a new type does not affect B's interface.
  • Now, A's dependencies are unchanged, so A will not be recompiled. But this is wrong: A might depend on something from D that was re-exported via B or C, and therefore need recompiling.

To ensure that A is recompiled, we therefore have two options:

  1. arrange that make knows about the dependency of A on D.
  1. arrange to touch B.hi and C.hi even if they haven't changed.

GHC currently does (2), more about that in a minute.

Why not do (1)? Well, then every time D.hi changed, GHC would be invoked on A again. But A doesn't depend directly on D: it imports B, and it might be therefore be insensitive to changes in D. By telling make only about direct dependencies, we gain the ability to avoid recompiling modules further up the dependency graph, by not touching interface files when they don't change.

Back to (2). In addition to correctness (recompile when necessary), we also want to avoid unnecessary recompilation as far as possible. Make only knows about very coarse-grained dependencies. For example, it doesn't know that changing the type of D.f can have no effect on C, so C does not in fact need to be recompiled, because to do so would generate exactly the same .o and .hi files as last time. GHC does have enough information to figure this out, so when GHC is asked to recompile a module it invokes the recompilation checker to determine whether recompilation can be avoided in this case.

How does it work?

We use fingerprints to uniquely identify the interface exposed by a module, and to detect when it changes. In particular, we currently use 128-bit hashes produced by the MD5 algorithm (see compiler/utils/Fingerprint.hsc).

An interface file contains:

  • Various fingerprints:
    • The interface hash, which depends on the entire contents of the interface file. This is used to detect whether we should update the interface on disk after recompiling the module. If the interface didn't change at all, then we don't want to touch the on-disk version because that would cause make to perform more compilations.
    • The ABI hash, which depends on everything that the module exposes about its implementation: think of this as a hash of exports and decls.
    • The export-list hash, which depends on the contents of the export list (a hash of exports).
    • The orphan hash, which depends on all the orphan instances/rules in the, and the orphan hashes of all orphan modules below this module in the dependency tree (see "Orphans" later).
  • exports: what the module exports
  • dependencies: modules and packages that this module depends on
  • usages: what specific entities the module depends on
  • decls: what the module defines
  • various other stuff, but the above are the important bits

To look at the contents of an interface, use ghc --show-iface. For example, here's the output of ghc --show-iface D.hi for the module D in our example:

interface main:D 6090
  interface hash: 413dacc4c360257e9fb06ad0c13d9fc9
  ABI hash: 0c5278c6f22844f996006259c9f551c8
  export-list hash: cb9dd0d414976d16451bdfe51a021d7d
  orphan hash: 693e9af84d3dfcc71e640e005bdc5e2e
export main:D T f h
module dependencies:
package dependencies: base integer ghc-prim
orphans: base:GHC.Base base:GHC.Num
family instance modules:
import base:GHC.Num 7a6f0c12ee2413f4c07165103924bd61
import base:Prelude ae91aa1798ed1ac514cde3dc4c921717
  data T a b
      RecFlag NonRecursive
      Generics: no
      {- abstract -}
      FamilyInstance: none
  f :: GHC.Base.Int -> GHC.Base.Int
  h :: GHC.Base.Int -> GHC.Base.Int

Lines beginning import are the usages, and after the usages are the decls.

Deciding whether to recompile

If we already have an object file and interface file for a module, we might not have to recompile it, if we can be sure the results will be the same as last time.

  • If the source file has changed since the object file was created, we better recompile.
  • If anything else has changed in a way that would affect the results of compiling this module, we must recompile.

In order to determine the second point, we look at the dependencies and usages fields of the old interface file. The dependencies contains:

  • dep_mods: Transitive closure of home-package modules that are imported by this module. That is, all modules below the current one in the dependency graph.
  • dep_pkgs: Transitive closure of packages depended on by this module, or by any module in dep_mods.
  • other less important stuff.

First, the direct imports of the current module are resolved to Modules using Finder.findModule (a Module contains a module name and a package identifier). If any of those Modules are not listed amongst the dependencies of the old interface file, then either:

  • an exposed package has been upgraded
  • we are compiling with different package flags
  • a home module that was shadowing a package module has been removed
  • a new home module has been added that shadows a package module

and we must recompile.

Second, the usages of the module are checked. The usages contains two types of information:

  • for a module that was imported, the export-list fingerprint of the imported module is recorded. If any of the modules we imported now has a different export list we must recompile, so we check the current export-list fingerprints against those recorded in the usages.
  • for every external name mentioned in the source code, the fingerprint of that name is recorded in the usages. This is so that if we mention for example an external function M.f, we'll recompile if M.f's type has changed, or anything referred to by M.f's type has changed, or M.f's unfolding has changed (when -O is on), and so on.

The interface files for everything in the usages are read (they'll already be in memory if we're doing --make), and the current versions for each of these entities checked against the usages from the old interface file. If any of these versions has changed, the module must be recompiled.


There are some tricky cases to consider.

Suppose we change the definition of D.f in the example, and make it

f x = h x + 1

Now, ultimately we need to recompile A, because it might be using an inlined copy of the old D.f, which it got via B.

It works like this:

  • D is recompiled; the fingerprint of D.f changes
  • B is considered; it recorded a usage on the old D.f, so gets recompiled, and now its interface records a usage on the new D.f
  • C is considered; it doesn't need to be recompiled.
  • A is considered (if we're using make, this is because B.hi changed); it recorded a usage on the old D.f, and so gets recompiled.

Now a slightly more tricky case: suppose we add an INLINE pragma to D.f (this is a trick to prevent GHC from inlining D.h, so that we can demonstrate dependencies between unfoldings). The code for D.hs is now

{-# INLINE f #-}
f :: Int -> Int
f x = h x + 1

h :: Int -> Int
h x = x + 3

Looking at the interface file we can see what happened (snipped slightly):

$ ghc --show-iface D.hi
interface main:D 6090
  interface hash: 0a7e886588b3799d909cca39be4b9232
  ABI hash: 8d5cfe1723f32a5b53ded43bf9a1e55b
  export-list hash: cb9dd0d414976d16451bdfe51a021d7d
  orphan hash: 693e9af84d3dfcc71e640e005bdc5e2e
export main:D T f h
  data T a b
      RecFlag NonRecursive
      Generics: no
      = C1 :: forall a b. a -> T a b Stricts: _ |
        C2 :: forall a b. b -> T a b Stricts: _
      FamilyInstance: none
  f :: GHC.Base.Int -> GHC.Base.Int
    {- Arity: 1 HasNoCafRefs Strictness: U(L)m
       Unfolding: (__inline_me (\ x :: GHC.Base.Int ->
                                GHC.Base.plusInt (D.h x) (GHC.Base.I# 1))) -}
  h :: GHC.Base.Int -> GHC.Base.Int
    {- Arity: 1 HasNoCafRefs Strictness: U(L)m
       Unfolding: (\ x :: GHC.Base.Int ->
                   case @ GHC.Base.Int x of wild { GHC.Base.I# x1 ->
                   GHC.Base.I# (GHC.Prim.+# x1 3) }) -}

Note that the unfolding of D.f mentions D.h.

Now, let's modify D.h, and look at the interface file again:

$ ghc -O --show-iface D.hi
interface main:D 6090
  interface hash: 55385e568aa80955acbd1b7370041890
  ABI hash: accc0413d94e27c90dff8427f4aafe6e
  export-list hash: cb9dd0d414976d16451bdfe51a021d7d
  orphan hash: 693e9af84d3dfcc71e640e005bdc5e2e
export main:D T f h
  data T a b
      RecFlag NonRecursive
      Generics: no
      = C1 :: forall a b. a -> T a b Stricts: _ |
        C2 :: forall a b. b -> T a b Stricts: _
      FamilyInstance: none
  f :: GHC.Base.Int -> GHC.Base.Int
    {- Arity: 1 HasNoCafRefs Strictness: U(L)m
       Unfolding: (__inline_me (\ x :: GHC.Base.Int ->
                                GHC.Base.plusInt (D.h x) (GHC.Base.I# 1))) -}
  h :: GHC.Base.Int -> GHC.Base.Int
    {- Arity: 1 HasNoCafRefs Strictness: U(L)m
       Unfolding: (\ x :: GHC.Base.Int ->
                   case @ GHC.Base.Int x of wild { GHC.Base.I# x1 ->
                   GHC.Base.I# (GHC.Prim.+# x1 4) }) -}

The fingerprint for D.h has changed, because we changed its definition. The fingerprint for D.f has also changed, because it depends on D.h. And consequently, the ABI hash has changed, and so has the interface hash (although the export hash and orphan hash are still the same).

Why did the fingerprint for D.f have to change? This is vital, because anything that referred to D.f must be recompiled, because it may now see the new unfolding for D.h.

So the fingerprint of an entity represents not just the definition of the entity itself, but also the definitions of all the entities reachable from it - its transitive closure. The consequence of this is that when recording usages we only have to record the fingerprints of entities that were referred to directly in the source code, because the transitive nature of the fingerprint means that we'll recompile if anything reachable from these entities changes.

How does fingerprinting work?

We calculate fingerprints by serialising the data to be fingerprinted using the Binary module, and then running the md5 algorithm over the serlialised data. When the data contains external Names, the serialiser emits the fingerprint of the Name; this is the way that the fingerprint of a declaration can be made to depend on the fingerprints of the things it mentions.

Mutually recursive groups of entities

When fingerprinting a recursive group of entities, we fingerprint the group as a whole. If any of the definitions changes, the fingerprint of every entity in the group changes.


We include the fixity of an entity when computing its fingerprint.


Instances are tricky in Haskell, because they aren't imported or exported explicitly. Haskell requires that any instance defined in a module directly or indirectly imported by the current module is visible. So how do we track instances for recompilation, such that if a relevant instance is changed, added, or removed anywhere beneath the current module we will trigger a recompilation?

Here's how it works. For each instance we pick a distinguished entity to attach the instance to - possibly the class itself, or a type constructor mentioned in the instance. The entity we pick must be defined in the current module; if there are none to pick, then the instance is an orphan (more about those in the section on Orphans, below).

Having picked the distinguished entity, when fingerprinting that entity we include the instances. For example, consider an instance for class C at type T. Any module that could use this instance must depend (directly or indirectly) on both C and T, so it doesn't matter whether we attach the instance to C or T - either way it will be included in the fingerprint of something that the module depends on. In this way we can be sure that if someone adds a new instance, or removes an existing instance, if the instance is relevant to a module then it will affect the fingerprint of something that the module depends on, and hence will trigger recompilation.

This is perfectly safe, but implementing it exactly as described above would lead to unnecessary recompilation. For example

module A (T) where
import B (C)
data T = ...
instance C t where ...

now the instance C T will be attached to T. But we really don't want to include C's fingerprint in T's fingerprint, because that would register an unnecessary dependency of T on C: every time C changed, we'd recompile anything that depended on T. The dependency is unnecessary, because we know anything that needs the instance will already depend on both C and T.

So instead of creating a fingerprint that depends on the instance declaration and everything it mentions, we fingerprint just the declaration itself. This works by fingerprinting each name in the declaration as its literal string, rather than the fingerprint of the name.

But what about the contents of the instance declaration? With optimisation on, the compiler may inline instances and their methods, so if an instance changes we need to trigger recompilation for any module that actually used the previous version of the instance. This works as follows. An instance declaration looks like this

instance GHC.Base.Eq [GHC.Bool.Bool] = GHC.Base.$f10

Here GHC.Base.$f10 is the "dictionary function", i.e. the value representing this dictionary. GHC.Base.$f10 has a declaration of its own, in the same interface, complete with a fingerprint.

  $f10 :: GHC.Base.Eq GHC.Bool.Bool
    {- HasNoCafRefs Strictness: m
       Unfolding: (GHC.Base.:DEq @ GHC.Bool.Bool GHC.Base.==2

So we ensure that any module that used this instance registers the fingerprint of GHC.Base.$f10 in its usages (this is the single exception to the rule that the usages only contains names that are explicitly mentioned in the source file).


What if we have no declaration to attach the instance to? Instances with no obvious parent are called orphans, and GHC must read the interface for any module that contains orphan instances below the current module, just in case those instances are relevant when compiling the current module.

Orphans require special treatment in the recompilation checker.

  • Every module has an orphan hash, which is a fingerprint of all the orphan instances (and rules) in the current module.
  • The export hash depends on the orphan hash of the current module, and all modules below the current module in the dependency tree. This models the fact that all instances defined in modules below the current module are available to importers of this module.

So if we add, delete, or modify an orphan instance, the orphan hash of the current module will change, and so will the export hash of the current module. This will trigger recompilation of modules that import the current module, which will cause their export hashes to change, and so on up the dependency tree.

This means a lot of recompilation, but it is at least safe. The trick is to avoid orphan instances as far as possible, which is why GHC has the warning flag -fwarn-orphans.


RULEs are treated very much like instances: they are attached to one particular parent declaration, and if a suitable parent cannot be found, they become orphans and are handled in the same way as orphan instances.

On ordering

When fingerprinting a collection of things, for example the export list, we must be careful to use a canonical ordering for the collection. Otherwise, if we recompile the module without making any changes, we might get a different fingerprint due to accidental reordering of the elements.

Why would we get accidental reordering? GHC relies heavily on "uniques" internally (see basicTypes/Unique.lhs): every entity has a unique, and uniques are assigned semi-randomly. Asking for the contents of a UniqSet or UniqFM will return the elements in order of their uniques, which may vary from run to run of the compiler.

The solution is to sort the elements using a stable ordering, such as lexicographic ordering.


We need to record usage information about package modules too, so that we can correctly trigger recompilation if we depend on a package that has changed. But packages change rarely, so it would be wasteful to record detailed usage information for every entity that we use from an external package (imagine recording the fingerprints for Bool, Int, etc.). Instead, we simply record the ABI fingerprint for every package module that was imported by the current module. That way, if anything about the ABI of that package module has changed, then we can trigger a recompilation.

(Correctly triggering recompilation when packages change was one of the things we fixed when implementing fingerprints, see #1372).

Interface stability

For recompilation avoidance to be really effective, we need to ensure that fingerprints do not change unnecessarily. That is, if a module is modified, it should be the case that the only fingerprints that change are related to the parts of the module that were modified. This may seem obvious, but it's surprisingly easy to get wrong. Here are some of the ways we got it wrong in the past, and some ways we still get it wrong.

  • Prior to GHC 6.12, dictionary functions were named something like M.$f23, where M is the module defining the instance, and the number 23 was generated by simply assigning numbers to the dictionary functions defined by M sequentially. This is a problem for recompilation avoidance, because now removing or adding an instance in M will change the numbering, and force recompilation of anything that depends on any instance in M. Worse, the numbers are assigned non-deterministically, so simply recompiling M without changing its code could change the fingerprints. In GHC 6.12 we changed it so that dictionary functions are named after the class and type(s) of the instance, e.g. M.$fOrdInteger.
  • compiler-generated bindings used to be numbered in the same way, non-deterministically. The non-determinism arises because Uniques are assigned by the compiler non-deterministically. Well, they are deterministic but not in a way that you can sensibly control, because it depends on the order in which interface bindings are read, etc. Internal mappings use Uniques as the key, so asking for the elements of a mapping gives a non-deterministic ordering. The list of bindings emitted by the simplifier, although in dependency order, can vary non-deterministically within the constraints of the dependencies. So if we number the compiler-generated bindings sequentially, the result will be a non-deterministic ABI.

    In GHC 6.12 we changed this so that compiler-generated bindings are given names of the form f_x, where f is the name of the exported Id that refers to the binding. If there are multiple f_xs, then they are disambiguated with an integer suffix, but the numbers are assigned deterministically, by traversing the definition of f in depth-first left-to-right order to find references. See TidyPgm.chooseExternalIds.
  • There are still some cases where an interface can change without changing the source code. Here are the ones I know about
    • The spec_ids (specialised Ids) attached to an Id have a non-deterministic ordering
    • CSE can give different results depending on the order in which the bindings are considered, and since the ordering is non-deterministic, the result of CSE is also non-deterministic. e.g. in x = z; y = z; z = 3, where y and x are exported, we can end up with either x = y; y = 3 or y = x; x = 3.