2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[CostCentre]{The @CostCentre@ data type}
8 CostCentre(..), CcName, IsDupdCC(..), IsCafCC(..),
9 -- All abstract except to friend: ParseIface.y
12 noCCS, subsumedCCS, currentCCS, overheadCCS, dontCareCCS,
13 noCostCentre, noCCAttached,
14 noCCSAttached, isCurrentCCS, isSubsumedCCS, currentOrSubsumedCCS,
16 mkUserCC, mkAutoCC, mkAllCafsCC,
17 mkSingletonCCS, cafifyCC, dupifyCC,
18 isCafCC, isDupdCC, isEmptyCC, isCafCCS,
23 pprCostCentreDecl, pprCostCentreStackDecl, pprCostCentreCore,
25 cmpCostCentre -- used for removing dups in a list
28 #include "HsVersions.h"
31 import Name ( UserFS, EncodedFS, encodeFS, decode,
34 import Module ( Module, ModuleName, moduleName,
35 pprModuleName, moduleNameUserString
38 import CStrings ( pprStringInCStyle )
39 import Util ( thenCmp )
42 A Cost Centre Stack is something that can be attached to a closure.
45 - the current cost centre stack (CCCS)
46 - a pre-defined cost centre stack (there are several
47 pre-defined CCSs, see below).
53 | CurrentCCS -- Pinned on a let(rec)-bound
54 -- thunk/function/constructor, this says that the
55 -- cost centre to be attached to the object, when it
56 -- is allocated, is whatever is in the
57 -- current-cost-centre-stack register.
59 | SubsumedCCS -- Cost centre stack for top-level subsumed functions
60 -- (CAFs get an AllCafsCC).
61 -- Its execution costs get subsumed into the caller.
62 -- This guy is *only* ever pinned on static closures,
63 -- and is *never* the cost centre for an SCC construct.
65 | OverheadCCS -- We charge costs due to the profiling-system
66 -- doing its work to "overhead".
68 -- Objects whose CCS is "Overhead"
69 -- have their *allocation* charged to "overhead",
70 -- but have the current CCS put into the object
73 -- For example, if we transform "f g" to "let
74 -- g' = g in f g'" (so that something about
75 -- profiling works better...), then we charge
76 -- the *allocation* of g' to OverheadCCS, but
77 -- we put the cost-centre of the call to f
78 -- (i.e., current CCS) into the g' object. When
79 -- g' is entered, the CCS of the call
82 | DontCareCCS -- We need a CCS to stick in static closures
83 -- (for data), but we *don't* expect them to
84 -- accumulate any costs. But we still need
85 -- the placeholder. This CCS is it.
87 | SingletonCCS CostCentre
88 -- This is primarily for CAF cost centres, which
89 -- are attached to top-level thunks right at the
90 -- end of STG processing, before code generation.
91 -- Hence, a CAF cost centre never appears as the
92 -- argument of an _scc_.
93 -- Also, we generate these singleton CCSs statically
94 -- as part of code generation.
96 deriving (Eq, Ord) -- needed for Ord on CLabel
99 A Cost Centre is the argument of an _scc_ expression.
103 = NoCostCentre -- Having this constructor avoids having
104 -- to use "Maybe CostCentre" all the time.
107 cc_name :: CcName, -- Name of the cost centre itself
108 cc_mod :: ModuleName, -- Name of module defining this CC.
109 cc_is_dupd :: IsDupdCC, -- see below
110 cc_is_caf :: IsCafCC -- see below
114 cc_mod :: ModuleName -- Name of module defining this CC.
117 type CcName = EncodedFS
120 = OriginalCC -- This says how the CC is *used*. Saying that
121 | DupdCC -- it is DupdCC doesn't make it a different
122 -- CC, just that it a sub-expression which has
123 -- been moved ("dupd") into a different scope.
125 -- The point about a dupd SCC is that we don't
126 -- count entries to it, because it's not the
129 -- In the papers, it's called "SCCsub",
130 -- i.e. SCCsub CC == SCC DupdCC,
131 -- but we are trying to avoid confusion between
132 -- "subd" and "subsumed". So we call the former
135 data IsCafCC = CafCC | NotCafCC
138 WILL: Would there be any merit to recording ``I am now using a
139 cost-centre from another module''? I don't know if this would help a
140 user; it might be interesting to us to know how much computation is
141 being moved across module boundaries.
143 SIMON: Maybe later...
148 subsumedCCS = SubsumedCCS
149 currentCCS = CurrentCCS
150 overheadCCS = OverheadCCS
151 dontCareCCS = DontCareCCS
153 noCostCentre = NoCostCentre
156 Predicates on Cost-Centre Stacks
159 noCCSAttached NoCCS = True
160 noCCSAttached _ = False
162 noCCAttached NoCostCentre = True
163 noCCAttached _ = False
165 isCurrentCCS CurrentCCS = True
166 isCurrentCCS _ = False
168 isSubsumedCCS SubsumedCCS = True
169 isSubsumedCCS _ = False
171 isCafCCS (SingletonCCS cc) = isCafCC cc
174 currentOrSubsumedCCS SubsumedCCS = True
175 currentOrSubsumedCCS CurrentCCS = True
176 currentOrSubsumedCCS _ = False
179 Building cost centres
182 mkUserCC :: UserFS -> Module -> CostCentre
185 = NormalCC { cc_name = encodeFS cc_name, cc_mod = moduleName mod,
186 cc_is_dupd = OriginalCC, cc_is_caf = NotCafCC {-might be changed-}
189 mkAutoCC :: Id -> Module -> IsCafCC -> CostCentre
191 mkAutoCC id mod is_caf
192 = NormalCC { cc_name = occNameFS (getOccName id), cc_mod = moduleName mod,
193 cc_is_dupd = OriginalCC, cc_is_caf = is_caf
196 mkAllCafsCC m = AllCafsCC { cc_mod = moduleName m }
198 mkSingletonCCS :: CostCentre -> CostCentreStack
199 mkSingletonCCS cc = SingletonCCS cc
201 cafifyCC, dupifyCC :: CostCentre -> CostCentre
203 cafifyCC cc@(NormalCC {cc_is_caf = is_caf})
204 = ASSERT(not_a_caf_already is_caf)
205 cc {cc_is_caf = CafCC}
207 not_a_caf_already CafCC = False
208 not_a_caf_already _ = True
209 cafifyCC cc = pprPanic "cafifyCC" (ppr cc)
211 dupifyCC cc = cc {cc_is_dupd = DupdCC}
213 isEmptyCC, isCafCC, isDupdCC :: CostCentre -> Bool
215 isEmptyCC (NoCostCentre) = True
218 isCafCC (AllCafsCC {}) = True
219 isCafCC (NormalCC {cc_is_caf = CafCC}) = True
222 isDupdCC (NormalCC {cc_is_dupd = DupdCC}) = True
225 isSccCountCostCentre :: CostCentre -> Bool
226 -- Is this a cost-centre which records scc counts
229 isSccCountCostCentre NoCostCentre = panic "isSccCount:NoCostCentre"
231 isSccCountCostCentre cc | isCafCC cc = False
232 | isDupdCC cc = False
235 sccAbleCostCentre :: CostCentre -> Bool
236 -- Is this a cost-centre which can be sccd ?
239 sccAbleCostCentre NoCostCentre = panic "sccAbleCC:NoCostCentre"
241 sccAbleCostCentre cc | isCafCC cc = False
244 ccFromThisModule :: CostCentre -> Module -> Bool
245 ccFromThisModule cc m = cc_mod cc == moduleName m
249 instance Eq CostCentre where
250 c1 == c2 = case c1 `cmpCostCentre` c2 of { EQ -> True; _ -> False }
252 instance Ord CostCentre where
253 compare = cmpCostCentre
255 cmpCostCentre :: CostCentre -> CostCentre -> Ordering
257 cmpCostCentre (AllCafsCC {cc_mod = m1}) (AllCafsCC {cc_mod = m2}) = m1 `compare` m2
259 cmpCostCentre (NormalCC {cc_name = n1, cc_mod = m1, cc_is_caf = c1})
260 (NormalCC {cc_name = n2, cc_mod = m2, cc_is_caf = c2})
261 -- first key is module name, then we use "kinds" (which include
262 -- names) and finally the caf flag
263 = (m1 `compare` m2) `thenCmp` (n1 `compare` n2) `thenCmp` (c1 `cmp_caf` c2)
265 cmpCostCentre other_1 other_2
267 tag1 = tag_CC other_1
268 tag2 = tag_CC other_2
270 if tag1 _LT_ tag2 then LT else GT
272 tag_CC (NormalCC {}) = (ILIT(1) :: FAST_INT)
273 tag_CC (AllCafsCC {}) = ILIT(2)
275 cmp_caf NotCafCC CafCC = LT
276 cmp_caf NotCafCC NotCafCC = EQ
277 cmp_caf CafCC CafCC = EQ
278 cmp_caf CafCC NotCafCC = GT
281 -----------------------------------------------------------------------------
282 Printing Cost Centre Stacks.
284 There are two ways to print a CCS:
286 - for debugging output (i.e. -ddump-whatever),
290 instance Outputable CostCentreStack where
291 ppr ccs = case ccs of
292 NoCCS -> ptext SLIT("NO_CCS")
293 CurrentCCS -> ptext SLIT("CCCS")
294 OverheadCCS -> ptext SLIT("CCS_OVERHEAD")
295 DontCareCCS -> ptext SLIT("CCS_DONT_CARE")
296 SubsumedCCS -> ptext SLIT("CCS_SUBSUMED")
297 SingletonCCS cc -> ppr cc <> ptext SLIT("_ccs")
299 pprCostCentreStackDecl :: CostCentreStack -> SDoc
300 pprCostCentreStackDecl ccs@(SingletonCCS cc)
301 = hcat [ ptext SLIT("CCS_DECLARE"), char '(',
302 ppr ccs, comma, -- better be codeStyle
303 ppCostCentreLbl cc, comma,
304 empty, -- Now always externally visible
308 pprCostCentreStackDecl ccs
309 = pprPanic "pprCostCentreStackDecl: " (ppr ccs)
312 -----------------------------------------------------------------------------
313 Printing Cost Centres.
315 There are several different ways in which we might want to print a
318 - the name of the cost centre, for profiling output (a C string)
319 - the label, i.e. C label for cost centre in .hc file.
320 - the debugging name, for output in -ddump things
321 - the interface name, for printing in _scc_ exprs in iface files.
323 The last 3 are derived from costCentreStr below. The first is given
327 instance Outputable CostCentre where
328 ppr cc = getPprStyle $ \ sty ->
330 then ppCostCentreLbl cc
331 else text (costCentreUserName cc)
333 -- Printing in an interface file or in Core generally
334 pprCostCentreCore (AllCafsCC {cc_mod = m})
335 = text "__sccC" <+> braces (pprModuleName m)
336 pprCostCentreCore (NormalCC {cc_name = n, cc_mod = m,
337 cc_is_caf = caf, cc_is_dupd = dup})
338 = text "__scc" <+> braces (hsep [
345 pp_dup DupdCC = char '!'
348 pp_caf CafCC = text "__C"
352 -- Printing as a C label
353 ppCostCentreLbl (NoCostCentre) = text "NONE_cc"
354 ppCostCentreLbl (AllCafsCC {cc_mod = m}) = pprModuleName m <> text "_CAFs_cc"
355 ppCostCentreLbl (NormalCC {cc_name = n, cc_mod = m, cc_is_caf = is_caf})
356 = pprModuleName m <> ptext n <>
357 text (case is_caf of { CafCC -> "_CAF"; _ -> "" }) <> text "_cc"
359 -- This is the name to go in the user-displayed string,
360 -- recorded in the cost centre declaration
361 costCentreUserName (NoCostCentre) = "NO_CC"
362 costCentreUserName (AllCafsCC {}) = "CAF"
363 costCentreUserName cc@(NormalCC {cc_name = name, cc_is_caf = is_caf})
364 = case is_caf of { CafCC -> "CAF:"; _ -> "" } ++ decode (_UNPK_ name)
367 Cost Centre Declarations
371 pprCostCentreDecl is_local (NoCostCentre)
372 = panic "pprCostCentreDecl: no cost centre!"
374 pprCostCentreDecl is_local cc
377 ptext SLIT("CC_DECLARE"),char '(',
379 pprStringInCStyle (costCentreUserName cc), comma,
380 pprStringInCStyle (moduleNameUserString mod_name), comma,
381 ptext is_subsumed, comma,
382 empty, -- Now always externally visible
385 hcat [ ptext SLIT("CC_EXTERN"),char '(', cc_ident, text ");" ]
387 cc_ident = ppCostCentreLbl cc
389 is_subsumed = ccSubsumed cc
391 ccSubsumed :: CostCentre -> FAST_STRING -- subsumed value
392 ccSubsumed cc | isCafCC cc = SLIT("CC_IS_CAF")
393 | otherwise = SLIT("CC_IS_BORING")