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
13 noCCS, subsumedCCS, currentCCS, overheadCCS, dontCareCCS,
15 noCCSAttached, isCurrentCCS, isSubsumedCCS, currentOrSubsumedCCS,
16 isDerivedFromCurrentCCS,
18 mkUserCC, mkAutoCC, mkAllCafsCC,
19 mkSingletonCCS, dupifyCC, pushCCOnCCS,
25 pprCostCentreDecl, pprCostCentreStackDecl, pprCostCentreCore,
27 cmpCostCentre -- used for removing dups in a list
30 #include "HsVersions.h"
33 import Name ( UserFS, EncodedFS, encodeFS, decode,
36 import Module ( Module, ModuleName, moduleName,
40 import CStrings ( pprStringInCStyle )
43 import Util ( thenCmp )
46 A Cost Centre Stack is something that can be attached to a closure.
49 - the current cost centre stack (CCCS)
50 - a pre-defined cost centre stack (there are several
51 pre-defined CCSs, see below).
57 | CurrentCCS -- Pinned on a let(rec)-bound
58 -- thunk/function/constructor, this says that the
59 -- cost centre to be attached to the object, when it
60 -- is allocated, is whatever is in the
61 -- current-cost-centre-stack register.
63 | SubsumedCCS -- Cost centre stack for top-level subsumed functions
64 -- (CAFs get an AllCafsCC).
65 -- Its execution costs get subsumed into the caller.
66 -- This guy is *only* ever pinned on static closures,
67 -- and is *never* the cost centre for an SCC construct.
69 | OverheadCCS -- We charge costs due to the profiling-system
70 -- doing its work to "overhead".
72 -- Objects whose CCS is "Overhead"
73 -- have their *allocation* charged to "overhead",
74 -- but have the current CCS put into the object
77 -- For example, if we transform "f g" to "let
78 -- g' = g in f g'" (so that something about
79 -- profiling works better...), then we charge
80 -- the *allocation* of g' to OverheadCCS, but
81 -- we put the cost-centre of the call to f
82 -- (i.e., current CCS) into the g' object. When
83 -- g' is entered, the CCS of the call
86 | DontCareCCS -- We need a CCS to stick in static closures
87 -- (for data), but we *don't* expect them to
88 -- accumulate any costs. But we still need
89 -- the placeholder. This CCS is it.
91 | PushCC CostCentre CostCentreStack
92 -- These are used during code generation as the CCSs
93 -- attached to closures. A PushCC never appears as
94 -- the argument to an _scc_.
96 -- The tail (2nd argument) is either NoCCS, indicating
97 -- a staticly allocated CCS, or CurrentCCS indicating
98 -- a dynamically created CCS. We only support
99 -- statically allocated *singleton* CCSs at the
100 -- moment, for the purposes of initialising the CCS
103 deriving (Eq, Ord) -- needed for Ord on CLabel
106 A Cost Centre is the argument of an _scc_ expression.
110 = NoCostCentre -- Having this constructor avoids having
111 -- to use "Maybe CostCentre" all the time.
114 cc_name :: CcName, -- Name of the cost centre itself
115 cc_mod :: ModuleName, -- Name of module defining this CC.
116 cc_is_dupd :: IsDupdCC, -- see below
117 cc_is_caf :: IsCafCC -- see below
121 cc_mod :: ModuleName -- Name of module defining this CC.
124 type CcName = EncodedFS
127 = OriginalCC -- This says how the CC is *used*. Saying that
128 | DupdCC -- it is DupdCC doesn't make it a different
129 -- CC, just that it a sub-expression which has
130 -- been moved ("dupd") into a different scope.
132 -- The point about a dupd SCC is that we don't
133 -- count entries to it, because it's not the
136 -- In the papers, it's called "SCCsub",
137 -- i.e. SCCsub CC == SCC DupdCC,
138 -- but we are trying to avoid confusion between
139 -- "subd" and "subsumed". So we call the former
142 data IsCafCC = CafCC | NotCafCC
144 -- synonym for triple which describes the cost centre info in the generated
145 -- code for a module.
147 = ( [CostCentre] -- local cost-centres that need to be decl'd
148 , [CostCentre] -- "extern" cost-centres
149 , [CostCentreStack] -- pre-defined "singleton" cost centre stacks
153 WILL: Would there be any merit to recording ``I am now using a
154 cost-centre from another module''? I don't know if this would help a
155 user; it might be interesting to us to know how much computation is
156 being moved across module boundaries.
158 SIMON: Maybe later...
163 subsumedCCS = SubsumedCCS
164 currentCCS = CurrentCCS
165 overheadCCS = OverheadCCS
166 dontCareCCS = DontCareCCS
168 noCostCentre = NoCostCentre
171 Predicates on Cost-Centre Stacks
174 noCCSAttached NoCCS = True
175 noCCSAttached _ = False
177 noCCAttached NoCostCentre = True
178 noCCAttached _ = False
180 isCurrentCCS CurrentCCS = True
181 isCurrentCCS _ = False
183 isSubsumedCCS SubsumedCCS = True
184 isSubsumedCCS _ = False
186 isCafCCS (PushCC cc NoCCS) = isCafCC cc
189 isDerivedFromCurrentCCS CurrentCCS = True
190 isDerivedFromCurrentCCS (PushCC _ ccs) = isDerivedFromCurrentCCS ccs
191 isDerivedFromCurrentCCS _ = False
193 currentOrSubsumedCCS SubsumedCCS = True
194 currentOrSubsumedCCS CurrentCCS = True
195 currentOrSubsumedCCS _ = False
198 Building cost centres
201 mkUserCC :: UserFS -> Module -> CostCentre
203 = NormalCC { cc_name = encodeFS cc_name, cc_mod = moduleName mod,
204 cc_is_dupd = OriginalCC, cc_is_caf = NotCafCC {-might be changed-}
207 mkAutoCC :: Id -> Module -> IsCafCC -> CostCentre
208 mkAutoCC id mod is_caf
209 = NormalCC { cc_name = occNameFS (getOccName id), cc_mod = moduleName mod,
210 cc_is_dupd = OriginalCC, cc_is_caf = is_caf
213 mkAllCafsCC m = AllCafsCC { cc_mod = moduleName m }
217 mkSingletonCCS :: CostCentre -> CostCentreStack
218 mkSingletonCCS cc = pushCCOnCCS cc NoCCS
220 pushCCOnCCS :: CostCentre -> CostCentreStack -> CostCentreStack
223 dupifyCC cc = cc {cc_is_dupd = DupdCC}
225 isEmptyCC, isCafCC, isDupdCC :: CostCentre -> Bool
227 isEmptyCC (NoCostCentre) = True
230 isCafCC (AllCafsCC {}) = True
231 isCafCC (NormalCC {cc_is_caf = CafCC}) = True
234 isDupdCC (NormalCC {cc_is_dupd = DupdCC}) = True
237 isSccCountCostCentre :: CostCentre -> Bool
238 -- Is this a cost-centre which records scc counts
241 isSccCountCostCentre NoCostCentre = panic "isSccCount:NoCostCentre"
243 isSccCountCostCentre cc | isCafCC cc = False
244 | isDupdCC cc = False
247 sccAbleCostCentre :: CostCentre -> Bool
248 -- Is this a cost-centre which can be sccd ?
251 sccAbleCostCentre NoCostCentre = panic "sccAbleCC:NoCostCentre"
253 sccAbleCostCentre cc | isCafCC cc = False
256 ccFromThisModule :: CostCentre -> Module -> Bool
257 ccFromThisModule cc m = cc_mod cc == moduleName m
261 instance Eq CostCentre where
262 c1 == c2 = case c1 `cmpCostCentre` c2 of { EQ -> True; _ -> False }
264 instance Ord CostCentre where
265 compare = cmpCostCentre
267 cmpCostCentre :: CostCentre -> CostCentre -> Ordering
269 cmpCostCentre (AllCafsCC {cc_mod = m1}) (AllCafsCC {cc_mod = m2}) = m1 `compare` m2
271 cmpCostCentre (NormalCC {cc_name = n1, cc_mod = m1, cc_is_caf = c1})
272 (NormalCC {cc_name = n2, cc_mod = m2, cc_is_caf = c2})
273 -- first key is module name, then we use "kinds" (which include
274 -- names) and finally the caf flag
275 = (m1 `compare` m2) `thenCmp` (n1 `compare` n2) `thenCmp` (c1 `cmp_caf` c2)
277 cmpCostCentre other_1 other_2
279 tag1 = tag_CC other_1
280 tag2 = tag_CC other_2
282 if tag1 <# tag2 then LT else GT
284 tag_CC (NormalCC {}) = (_ILIT 1 :: FastInt)
285 tag_CC (AllCafsCC {}) = _ILIT 2
287 cmp_caf NotCafCC CafCC = LT
288 cmp_caf NotCafCC NotCafCC = EQ
289 cmp_caf CafCC CafCC = EQ
290 cmp_caf CafCC NotCafCC = GT
293 -----------------------------------------------------------------------------
294 Printing Cost Centre Stacks.
296 The outputable instance for CostCentreStack prints the CCS as a C
299 NOTE: Not all cost centres are suitable for using in a static
300 initializer. In particular, the PushCC forms where the tail is CCCS
301 may only be used in inline C code because they expand to a
302 non-constant C expression.
305 instance Outputable CostCentreStack where
306 ppr NoCCS = ptext SLIT("NO_CCS")
307 ppr CurrentCCS = ptext SLIT("CCCS")
308 ppr OverheadCCS = ptext SLIT("CCS_OVERHEAD")
309 ppr DontCareCCS = ptext SLIT("CCS_DONT_CARE")
310 ppr SubsumedCCS = ptext SLIT("CCS_SUBSUMED")
311 ppr (PushCC cc NoCCS) = ppr cc <> ptext SLIT("_ccs")
312 ppr (PushCC cc ccs) = ptext SLIT("PushCostCentre") <>
313 parens (ppr ccs <> comma <> ppr cc)
315 -- print the static declaration for a singleton CCS.
316 pprCostCentreStackDecl :: CostCentreStack -> SDoc
317 pprCostCentreStackDecl ccs@(PushCC cc NoCCS)
318 = hcat [ ptext SLIT("CCS_DECLARE"), char '(',
319 ppr ccs, comma, -- better be codeStyle
320 ppCostCentreLbl cc, comma,
321 empty, -- Now always externally visible
325 pprCostCentreStackDecl ccs
326 = pprPanic "pprCostCentreStackDecl: " (ppr ccs)
329 -----------------------------------------------------------------------------
330 Printing Cost Centres.
332 There are several different ways in which we might want to print a
335 - the name of the cost centre, for profiling output (a C string)
336 - the label, i.e. C label for cost centre in .hc file.
337 - the debugging name, for output in -ddump things
338 - the interface name, for printing in _scc_ exprs in iface files.
340 The last 3 are derived from costCentreStr below. The first is given
344 instance Outputable CostCentre where
345 ppr cc = getPprStyle $ \ sty ->
347 then ppCostCentreLbl cc
348 else text (costCentreUserName cc)
350 -- Printing in an interface file or in Core generally
351 pprCostCentreCore (AllCafsCC {cc_mod = m})
352 = text "__sccC" <+> braces (ppr m)
353 pprCostCentreCore (NormalCC {cc_name = n, cc_mod = m,
354 cc_is_caf = caf, cc_is_dupd = dup})
355 = text "__scc" <+> braces (hsep [
362 pp_dup DupdCC = char '!'
365 pp_caf CafCC = text "__C"
369 -- Printing as a C label
370 ppCostCentreLbl (NoCostCentre) = text "NONE_cc"
371 ppCostCentreLbl (AllCafsCC {cc_mod = m}) = ppr m <> text "_CAFs_cc"
372 ppCostCentreLbl (NormalCC {cc_name = n, cc_mod = m, cc_is_caf = is_caf})
373 = ppr m <> ftext n <>
374 text (case is_caf of { CafCC -> "_CAF"; _ -> "" }) <> text "_cc"
376 -- This is the name to go in the user-displayed string,
377 -- recorded in the cost centre declaration
378 costCentreUserName (NoCostCentre) = "NO_CC"
379 costCentreUserName (AllCafsCC {}) = "CAF"
380 costCentreUserName cc@(NormalCC {cc_name = name, cc_is_caf = is_caf})
381 = case is_caf of { CafCC -> "CAF:"; _ -> "" } ++ decode (unpackFS name)
384 Cost Centre Declarations
388 pprCostCentreDecl is_local (NoCostCentre)
389 = panic "pprCostCentreDecl: no cost centre!"
391 pprCostCentreDecl is_local cc
394 ptext SLIT("CC_DECLARE"),char '(',
396 pprStringInCStyle (costCentreUserName cc), comma,
397 pprStringInCStyle (moduleNameUserString mod_name), comma,
399 empty, -- Now always externally visible
402 hcat [ ptext SLIT("CC_EXTERN"),char '(', cc_ident, text ");" ]
404 cc_ident = ppCostCentreLbl cc
406 is_subsumed = ccSubsumed cc
408 ccSubsumed :: CostCentre -> SDoc -- subsumed value
409 ccSubsumed cc | isCafCC cc = ptext SLIT("CC_IS_CAF")
410 | otherwise = ptext SLIT("CC_IS_BORING")