Update Trac ticket URLs to point to GitLab
[ghc.git] / compiler / iface / FlagChecker.hs
1 {-# LANGUAGE RecordWildCards #-}
2
3 -- | This module manages storing the various GHC option flags in a modules
4 -- interface file as part of the recompilation checking infrastructure.
5 module FlagChecker (
6 fingerprintDynFlags
7 , fingerprintOptFlags
8 , fingerprintHpcFlags
9 ) where
10
11 import GhcPrelude
12
13 import Binary
14 import BinIface ()
15 import DynFlags
16 import HscTypes
17 import Module
18 import Name
19 import Fingerprint
20 import BinFingerprint
21 -- import Outputable
22
23 import qualified EnumSet
24 import System.FilePath (normalise)
25
26 -- | Produce a fingerprint of a @DynFlags@ value. We only base
27 -- the finger print on important fields in @DynFlags@ so that
28 -- the recompilation checker can use this fingerprint.
29 --
30 -- NB: The 'Module' parameter is the 'Module' recorded by the
31 -- *interface* file, not the actual 'Module' according to our
32 -- 'DynFlags'.
33 fingerprintDynFlags :: DynFlags -> Module
34 -> (BinHandle -> Name -> IO ())
35 -> IO Fingerprint
36
37 fingerprintDynFlags dflags@DynFlags{..} this_mod nameio =
38 let mainis = if mainModIs == this_mod then Just mainFunIs else Nothing
39 -- see #5878
40 -- pkgopts = (thisPackage dflags, sort $ packageFlags dflags)
41 safeHs = setSafeMode safeHaskell
42 -- oflags = sort $ filter filterOFlags $ flags dflags
43
44 -- *all* the extension flags and the language
45 lang = (fmap fromEnum language,
46 map fromEnum $ EnumSet.toList extensionFlags)
47
48 -- -I, -D and -U flags affect CPP
49 cpp = ( map normalise $ flattenIncludes includePaths
50 -- normalise: eliminate spurious differences due to "./foo" vs "foo"
51 , picPOpts dflags
52 , opt_P_signature dflags)
53 -- See Note [Repeated -optP hashing]
54
55 -- Note [path flags and recompilation]
56 paths = [ hcSuf ]
57
58 -- -fprof-auto etc.
59 prof = if gopt Opt_SccProfilingOn dflags then fromEnum profAuto else 0
60
61 flags = (mainis, safeHs, lang, cpp, paths, prof)
62
63 in -- pprTrace "flags" (ppr flags) $
64 computeFingerprint nameio flags
65
66 -- Fingerprint the optimisation info. We keep this separate from the rest of
67 -- the flags because GHCi users (especially) may wish to ignore changes in
68 -- optimisation level or optimisation flags so as to use as many pre-existing
69 -- object files as they can.
70 -- See Note [Ignoring some flag changes]
71 fingerprintOptFlags :: DynFlags
72 -> (BinHandle -> Name -> IO ())
73 -> IO Fingerprint
74 fingerprintOptFlags DynFlags{..} nameio =
75 let
76 -- See https://gitlab.haskell.org/ghc/ghc/issues/10923
77 -- We used to fingerprint the optimisation level, but as Joachim
78 -- Breitner pointed out in comment 9 on that ticket, it's better
79 -- to ignore that and just look at the individual optimisation flags.
80 opt_flags = map fromEnum $ filter (`EnumSet.member` optimisationFlags)
81 (EnumSet.toList generalFlags)
82
83 in computeFingerprint nameio opt_flags
84
85 -- Fingerprint the HPC info. We keep this separate from the rest of
86 -- the flags because GHCi users (especially) may wish to use an object
87 -- file compiled for HPC when not actually using HPC.
88 -- See Note [Ignoring some flag changes]
89 fingerprintHpcFlags :: DynFlags
90 -> (BinHandle -> Name -> IO ())
91 -> IO Fingerprint
92 fingerprintHpcFlags dflags@DynFlags{..} nameio =
93 let
94 -- -fhpc, see https://gitlab.haskell.org/ghc/ghc/issues/11798
95 -- hpcDir is output-only, so we should recompile if it changes
96 hpc = if gopt Opt_Hpc dflags then Just hpcDir else Nothing
97
98 in computeFingerprint nameio hpc
99
100
101 {- Note [path flags and recompilation]
102
103 There are several flags that we deliberately omit from the
104 recompilation check; here we explain why.
105
106 -osuf, -odir, -hisuf, -hidir
107 If GHC decides that it does not need to recompile, then
108 it must have found an up-to-date .hi file and .o file.
109 There is no point recording these flags - the user must
110 have passed the correct ones. Indeed, the user may
111 have compiled the source file in one-shot mode using
112 -o to specify the .o file, and then loaded it in GHCi
113 using -odir.
114
115 -stubdir
116 We omit this one because it is automatically set by -outputdir, and
117 we don't want changes in -outputdir to automatically trigger
118 recompilation. This could be wrong, but only in very rare cases.
119
120 -i (importPaths)
121 For the same reason as -osuf etc. above: if GHC decides not to
122 recompile, then it must have already checked all the .hi files on
123 which the current module depends, so it must have found them
124 successfully. It is occasionally useful to be able to cd to a
125 different directory and use -i flags to enable GHC to find the .hi
126 files; we don't want this to force recompilation.
127
128 The only path-related flag left is -hcsuf.
129 -}
130
131 {- Note [Ignoring some flag changes]
132 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
133
134 Normally, --make tries to reuse only compilation products that are
135 the same as those that would have been produced compiling from
136 scratch. Sometimes, however, users would like to be more aggressive
137 about recompilation avoidance. This is particularly likely when
138 developing using GHCi (see #13604). Currently, we allow users to
139 ignore optimisation changes using -fignore-optim-changes, and to
140 ignore HPC option changes using -fignore-hpc-changes. If there's a
141 demand for it, we could also allow changes to -fprof-auto-* flags
142 (although we can't allow -prof flags to differ). The key thing about
143 these options is that we can still successfully link a library or
144 executable when some of its components differ in these ways.
145
146 The way we accomplish this is to leave the optimization and HPC
147 options out of the flag hash, hashing them separately.
148 -}
149
150 {- Note [Repeated -optP hashing]
151 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
152
153 We invoke fingerprintDynFlags for each compiled module to include
154 the hash of relevant DynFlags in the resulting interface file.
155 -optP (preprocessor) flags are part of that hash.
156 -optP flags can come from multiple places:
157
158 1. -optP flags directly passed on command line.
159 2. -optP flags implied by other flags. Eg. -DPROFILING implied by -prof.
160 3. -optP flags added with {-# OPTIONS -optP-D__F__ #-} in a file.
161
162 When compiling many modules at once with many -optP command line arguments
163 the work of hashing -optP flags would be repeated. This can get expensive
164 and as noted on #14697 it can take 7% of time and 14% of allocations on
165 a real codebase.
166
167 The obvious solution is to cache the hash of -optP flags per GHC invocation.
168 However, one has to be careful there, as the flags that were added in 3. way
169 have to be accounted for.
170
171 The current strategy is as follows:
172
173 1. Lazily compute the hash of sOpt_p in sOpt_P_fingerprint whenever sOpt_p
174 is modified. This serves dual purpose. It ensures correctness for when
175 we add per file -optP flags and lets us save work for when we don't.
176 2. When computing the fingerprint in fingerprintDynFlags use the cached
177 value *and* fingerprint the additional implied (see 2. above) -optP flags.
178 This is relatively cheap and saves the headache of fingerprinting all
179 the -optP flags and tracking all the places that could invalidate the
180 cache.
181 -}