FlagChecker.hs 7.3 KB
Newer Older
1
{-# LANGUAGE RecordWildCards #-}
2 3 4 5 6

-- | This module manages storing the various GHC option flags in a modules
-- interface file as part of the recompilation checking infrastructure.
module FlagChecker (
        fingerprintDynFlags
7 8
      , fingerprintOptFlags
      , fingerprintHpcFlags
9 10
    ) where

11 12
import GhcPrelude

13 14 15
import Binary
import DynFlags
import HscTypes
16
import Module
17 18
import Name
import Fingerprint
19
import BinFingerprint
20
-- import Outputable
21

22
import qualified EnumSet
23
import System.FilePath (normalise)
24 25 26 27

-- | Produce a fingerprint of a @DynFlags@ value. We only base
-- the finger print on important fields in @DynFlags@ so that
-- the recompilation checker can use this fingerprint.
28 29 30 31
--
-- NB: The 'Module' parameter is the 'Module' recorded by the
-- *interface* file, not the actual 'Module' according to our
-- 'DynFlags'.
32 33
fingerprintDynFlags :: DynFlags -> Module
                    -> (BinHandle -> Name -> IO ())
34 35
                    -> IO Fingerprint

Ian Lynagh's avatar
Ian Lynagh committed
36
fingerprintDynFlags dflags@DynFlags{..} this_mod nameio =
37 38
    let mainis   = if mainModIs == this_mod then Just mainFunIs else Nothing
                      -- see #5878
39
        -- pkgopts  = (thisPackage dflags, sort $ packageFlags dflags)
40
        safeHs   = setSafeMode safeHaskell
41 42
        -- oflags   = sort $ filter filterOFlags $ flags dflags

43 44
        -- *all* the extension flags and the language
        lang = (fmap fromEnum language,
45
                map fromEnum $ EnumSet.toList extensionFlags)
46 47

        -- -I, -D and -U flags affect CPP
48
        cpp = ( map normalise $ flattenIncludes includePaths
49
            -- normalise: eliminate spurious differences due to "./foo" vs "foo"
niteria's avatar
niteria committed
50 51 52
              , picPOpts dflags
              , opt_P_signature dflags)
            -- See Note [Repeated -optP hashing]
53

54 55
        -- Note [path flags and recompilation]
        paths = [ hcSuf ]
56

Simon Marlow's avatar
Simon Marlow committed
57
        -- -fprof-auto etc.
ian@well-typed.com's avatar
ian@well-typed.com committed
58
        prof = if gopt Opt_SccProfilingOn dflags then fromEnum profAuto else 0
Simon Marlow's avatar
Simon Marlow committed
59

60 61 62 63
        -- Ticky
        ticky =
          map (`gopt` dflags) [Opt_Ticky, Opt_Ticky_Allocd, Opt_Ticky_LNE, Opt_Ticky_Dyn_Thunk]

64
        flags = ((mainis, safeHs, lang, cpp), (paths, prof, ticky, debugLevel))
65

66 67 68 69 70 71 72 73 74 75 76 77 78
    in -- pprTrace "flags" (ppr flags) $
       computeFingerprint nameio flags

-- Fingerprint the optimisation info. We keep this separate from the rest of
-- the flags because GHCi users (especially) may wish to ignore changes in
-- optimisation level or optimisation flags so as to use as many pre-existing
-- object files as they can.
-- See Note [Ignoring some flag changes]
fingerprintOptFlags :: DynFlags
                      -> (BinHandle -> Name -> IO ())
                      -> IO Fingerprint
fingerprintOptFlags DynFlags{..} nameio =
      let
79
        -- See https://gitlab.haskell.org/ghc/ghc/issues/10923
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
        -- We used to fingerprint the optimisation level, but as Joachim
        -- Breitner pointed out in comment 9 on that ticket, it's better
        -- to ignore that and just look at the individual optimisation flags.
        opt_flags = map fromEnum $ filter (`EnumSet.member` optimisationFlags)
                                          (EnumSet.toList generalFlags)

      in computeFingerprint nameio opt_flags

-- Fingerprint the HPC info. We keep this separate from the rest of
-- the flags because GHCi users (especially) may wish to use an object
-- file compiled for HPC when not actually using HPC.
-- See Note [Ignoring some flag changes]
fingerprintHpcFlags :: DynFlags
                      -> (BinHandle -> Name -> IO ())
                      -> IO Fingerprint
fingerprintHpcFlags dflags@DynFlags{..} nameio =
      let
97
        -- -fhpc, see https://gitlab.haskell.org/ghc/ghc/issues/11798
98 99
        -- hpcDir is output-only, so we should recompile if it changes
        hpc = if gopt Opt_Hpc dflags then Just hpcDir else Nothing
100

101
      in computeFingerprint nameio hpc
102

103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132

{- Note [path flags and recompilation]

There are several flags that we deliberately omit from the
recompilation check; here we explain why.

-osuf, -odir, -hisuf, -hidir
  If GHC decides that it does not need to recompile, then
  it must have found an up-to-date .hi file and .o file.
  There is no point recording these flags - the user must
  have passed the correct ones.  Indeed, the user may
  have compiled the source file in one-shot mode using
  -o to specify the .o file, and then loaded it in GHCi
  using -odir.

-stubdir
  We omit this one because it is automatically set by -outputdir, and
  we don't want changes in -outputdir to automatically trigger
  recompilation.  This could be wrong, but only in very rare cases.

-i (importPaths)
  For the same reason as -osuf etc. above: if GHC decides not to
  recompile, then it must have already checked all the .hi files on
  which the current module depends, so it must have found them
  successfully.  It is occasionally useful to be able to cd to a
  different directory and use -i flags to enable GHC to find the .hi
  files; we don't want this to force recompilation.

The only path-related flag left is -hcsuf.
-}
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151

{- Note [Ignoring some flag changes]
   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Normally, --make tries to reuse only compilation products that are
the same as those that would have been produced compiling from
scratch. Sometimes, however, users would like to be more aggressive
about recompilation avoidance. This is particularly likely when
developing using GHCi (see #13604). Currently, we allow users to
ignore optimisation changes using -fignore-optim-changes, and to
ignore HPC option changes using -fignore-hpc-changes. If there's a
demand for it, we could also allow changes to -fprof-auto-* flags
(although we can't allow -prof flags to differ). The key thing about
these options is that we can still successfully link a library or
executable when some of its components differ in these ways.

The way we accomplish this is to leave the optimization and HPC
options out of the flag hash, hashing them separately.
-}
niteria's avatar
niteria committed
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184

{- Note [Repeated -optP hashing]
   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

We invoke fingerprintDynFlags for each compiled module to include
the hash of relevant DynFlags in the resulting interface file.
-optP (preprocessor) flags are part of that hash.
-optP flags can come from multiple places:

  1. -optP flags directly passed on command line.
  2. -optP flags implied by other flags. Eg. -DPROFILING implied by -prof.
  3. -optP flags added with {-# OPTIONS -optP-D__F__ #-} in a file.

When compiling many modules at once with many -optP command line arguments
the work of hashing -optP flags would be repeated. This can get expensive
and as noted on #14697 it can take 7% of time and 14% of allocations on
a real codebase.

The obvious solution is to cache the hash of -optP flags per GHC invocation.
However, one has to be careful there, as the flags that were added in 3. way
have to be accounted for.

The current strategy is as follows:

  1. Lazily compute the hash of sOpt_p in sOpt_P_fingerprint whenever sOpt_p
     is modified. This serves dual purpose. It ensures correctness for when
     we add per file -optP flags and lets us save work for when we don't.
  2. When computing the fingerprint in fingerprintDynFlags use the cached
     value *and* fingerprint the additional implied (see 2. above) -optP flags.
     This is relatively cheap and saves the headache of fingerprinting all
     the -optP flags and tracking all the places that could invalidate the
     cache.
-}