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.

Example

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

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

make works by checking the timestamps on dependencies and
recompiling things when the dependencies are newer. Dependency lists
for make look like this:

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. 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. Now, 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:

arrange that make knows about the dependency of A on D.

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.

The module version starts at 1, and is increased each time the module
is compiled and either the exports or decls changes.

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 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 in the
interface file contains a list of every name that was used during
typechecking: that is, everything external that is referred to by the
source code.

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.

Example

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 version of D.f increases

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

Note from the first line that this is version 3. The first change was
to the definition of D.f above, and the second change we made just
now was to add the INLINE pragma to D.f. The version of D.f is
also 3 (it has changed twice), and the version of D.h is still 1
(the version number is omitted if it is 1).