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,
38 import CStrings ( pprStringInCStyle )
40 import Util ( thenCmp )
43 A Cost Centre Stack is something that can be attached to a closure.
46 - the current cost centre stack (CCCS)
47 - a pre-defined cost centre stack (there are several
48 pre-defined CCSs, see below).
54 | CurrentCCS -- Pinned on a let(rec)-bound
55 -- thunk/function/constructor, this says that the
56 -- cost centre to be attached to the object, when it
57 -- is allocated, is whatever is in the
58 -- current-cost-centre-stack register.
60 | SubsumedCCS -- Cost centre stack for top-level subsumed functions
61 -- (CAFs get an AllCafsCC).
62 -- Its execution costs get subsumed into the caller.
63 -- This guy is *only* ever pinned on static closures,
64 -- and is *never* the cost centre for an SCC construct.
66 | OverheadCCS -- We charge costs due to the profiling-system
67 -- doing its work to "overhead".
69 -- Objects whose CCS is "Overhead"
70 -- have their *allocation* charged to "overhead",
71 -- but have the current CCS put into the object
74 -- For example, if we transform "f g" to "let
75 -- g' = g in f g'" (so that something about
76 -- profiling works better...), then we charge
77 -- the *allocation* of g' to OverheadCCS, but
78 -- we put the cost-centre of the call to f
79 -- (i.e., current CCS) into the g' object. When
80 -- g' is entered, the CCS of the call
83 | DontCareCCS -- We need a CCS to stick in static closures
84 -- (for data), but we *don't* expect them to
85 -- accumulate any costs. But we still need
86 -- the placeholder. This CCS is it.
88 | SingletonCCS CostCentre
89 -- This is primarily for CAF cost centres, which
90 -- are attached to top-level thunks right at the
91 -- end of STG processing, before code generation.
92 -- Hence, a CAF cost centre never appears as the
93 -- argument of an _scc_.
94 -- Also, we generate these singleton CCSs statically
95 -- as part of code generation.
97 deriving (Eq, Ord) -- needed for Ord on CLabel
100 A Cost Centre is the argument of an _scc_ expression.
104 = NoCostCentre -- Having this constructor avoids having
105 -- to use "Maybe CostCentre" all the time.
108 cc_name :: CcName, -- Name of the cost centre itself
109 cc_mod :: ModuleName, -- Name of module defining this CC.
110 cc_is_dupd :: IsDupdCC, -- see below
111 cc_is_caf :: IsCafCC -- see below
115 cc_mod :: ModuleName -- Name of module defining this CC.
118 type CcName = EncodedFS
121 = OriginalCC -- This says how the CC is *used*. Saying that
122 | DupdCC -- it is DupdCC doesn't make it a different
123 -- CC, just that it a sub-expression which has
124 -- been moved ("dupd") into a different scope.
126 -- The point about a dupd SCC is that we don't
127 -- count entries to it, because it's not the
130 -- In the papers, it's called "SCCsub",
131 -- i.e. SCCsub CC == SCC DupdCC,
132 -- but we are trying to avoid confusion between
133 -- "subd" and "subsumed". So we call the former
136 data IsCafCC = CafCC | NotCafCC
139 WILL: Would there be any merit to recording ``I am now using a
140 cost-centre from another module''? I don't know if this would help a
141 user; it might be interesting to us to know how much computation is
142 being moved across module boundaries.
144 SIMON: Maybe later...
149 subsumedCCS = SubsumedCCS
150 currentCCS = CurrentCCS
151 overheadCCS = OverheadCCS
152 dontCareCCS = DontCareCCS
154 noCostCentre = NoCostCentre
157 Predicates on Cost-Centre Stacks
160 noCCSAttached NoCCS = True
161 noCCSAttached _ = False
163 noCCAttached NoCostCentre = True
164 noCCAttached _ = False
166 isCurrentCCS CurrentCCS = True
167 isCurrentCCS _ = False
169 isSubsumedCCS SubsumedCCS = True
170 isSubsumedCCS _ = False
172 isCafCCS (SingletonCCS cc) = isCafCC cc
175 currentOrSubsumedCCS SubsumedCCS = True
176 currentOrSubsumedCCS CurrentCCS = True
177 currentOrSubsumedCCS _ = False
180 Building cost centres
183 mkUserCC :: UserFS -> Module -> CostCentre
186 = NormalCC { cc_name = encodeFS cc_name, cc_mod = moduleName mod,
187 cc_is_dupd = OriginalCC, cc_is_caf = NotCafCC {-might be changed-}
190 mkAutoCC :: Id -> Module -> IsCafCC -> CostCentre
192 mkAutoCC id mod is_caf
193 = NormalCC { cc_name = occNameFS (getOccName id), cc_mod = moduleName mod,
194 cc_is_dupd = OriginalCC, cc_is_caf = is_caf
197 mkAllCafsCC m = AllCafsCC { cc_mod = moduleName m }
199 mkSingletonCCS :: CostCentre -> CostCentreStack
200 mkSingletonCCS cc = SingletonCCS cc
202 cafifyCC, dupifyCC :: CostCentre -> CostCentre
204 cafifyCC cc@(NormalCC {cc_is_caf = is_caf})
205 = ASSERT(not_a_caf_already is_caf)
206 cc {cc_is_caf = CafCC}
208 not_a_caf_already CafCC = False
209 not_a_caf_already _ = True
210 cafifyCC cc = pprPanic "cafifyCC" (ppr cc)
212 dupifyCC cc = cc {cc_is_dupd = DupdCC}
214 isEmptyCC, isCafCC, isDupdCC :: CostCentre -> Bool
216 isEmptyCC (NoCostCentre) = True
219 isCafCC (AllCafsCC {}) = True
220 isCafCC (NormalCC {cc_is_caf = CafCC}) = True
223 isDupdCC (NormalCC {cc_is_dupd = DupdCC}) = True
226 isSccCountCostCentre :: CostCentre -> Bool
227 -- Is this a cost-centre which records scc counts
230 isSccCountCostCentre NoCostCentre = panic "isSccCount:NoCostCentre"
232 isSccCountCostCentre cc | isCafCC cc = False
233 | isDupdCC cc = False
236 sccAbleCostCentre :: CostCentre -> Bool
237 -- Is this a cost-centre which can be sccd ?
240 sccAbleCostCentre NoCostCentre = panic "sccAbleCC:NoCostCentre"
242 sccAbleCostCentre cc | isCafCC cc = False
245 ccFromThisModule :: CostCentre -> Module -> Bool
246 ccFromThisModule cc m = cc_mod cc == moduleName m
250 instance Eq CostCentre where
251 c1 == c2 = case c1 `cmpCostCentre` c2 of { EQ -> True; _ -> False }
253 instance Ord CostCentre where
254 compare = cmpCostCentre
256 cmpCostCentre :: CostCentre -> CostCentre -> Ordering
258 cmpCostCentre (AllCafsCC {cc_mod = m1}) (AllCafsCC {cc_mod = m2}) = m1 `compare` m2
260 cmpCostCentre (NormalCC {cc_name = n1, cc_mod = m1, cc_is_caf = c1})
261 (NormalCC {cc_name = n2, cc_mod = m2, cc_is_caf = c2})
262 -- first key is module name, then we use "kinds" (which include
263 -- names) and finally the caf flag
264 = (m1 `compare` m2) `thenCmp` (n1 `compare` n2) `thenCmp` (c1 `cmp_caf` c2)
266 cmpCostCentre other_1 other_2
268 tag1 = tag_CC other_1
269 tag2 = tag_CC other_2
271 if tag1 <# tag2 then LT else GT
273 tag_CC (NormalCC {}) = (_ILIT 1 :: FastInt)
274 tag_CC (AllCafsCC {}) = _ILIT 2
276 cmp_caf NotCafCC CafCC = LT
277 cmp_caf NotCafCC NotCafCC = EQ
278 cmp_caf CafCC CafCC = EQ
279 cmp_caf CafCC NotCafCC = GT
282 -----------------------------------------------------------------------------
283 Printing Cost Centre Stacks.
285 There are two ways to print a CCS:
287 - for debugging output (i.e. -ddump-whatever),
291 instance Outputable CostCentreStack where
292 ppr ccs = case ccs of
293 NoCCS -> ptext SLIT("NO_CCS")
294 CurrentCCS -> ptext SLIT("CCCS")
295 OverheadCCS -> ptext SLIT("CCS_OVERHEAD")
296 DontCareCCS -> ptext SLIT("CCS_DONT_CARE")
297 SubsumedCCS -> ptext SLIT("CCS_SUBSUMED")
298 SingletonCCS cc -> ppr cc <> ptext SLIT("_ccs")
300 pprCostCentreStackDecl :: CostCentreStack -> SDoc
301 pprCostCentreStackDecl ccs@(SingletonCCS cc)
302 = hcat [ ptext SLIT("CCS_DECLARE"), char '(',
303 ppr ccs, comma, -- better be codeStyle
304 ppCostCentreLbl cc, comma,
305 empty, -- Now always externally visible
309 pprCostCentreStackDecl ccs
310 = pprPanic "pprCostCentreStackDecl: " (ppr ccs)
313 -----------------------------------------------------------------------------
314 Printing Cost Centres.
316 There are several different ways in which we might want to print a
319 - the name of the cost centre, for profiling output (a C string)
320 - the label, i.e. C label for cost centre in .hc file.
321 - the debugging name, for output in -ddump things
322 - the interface name, for printing in _scc_ exprs in iface files.
324 The last 3 are derived from costCentreStr below. The first is given
328 instance Outputable CostCentre where
329 ppr cc = getPprStyle $ \ sty ->
331 then ppCostCentreLbl cc
332 else text (costCentreUserName cc)
334 -- Printing in an interface file or in Core generally
335 pprCostCentreCore (AllCafsCC {cc_mod = m})
336 = text "__sccC" <+> braces (ppr m)
337 pprCostCentreCore (NormalCC {cc_name = n, cc_mod = m,
338 cc_is_caf = caf, cc_is_dupd = dup})
339 = text "__scc" <+> braces (hsep [
346 pp_dup DupdCC = char '!'
349 pp_caf CafCC = text "__C"
353 -- Printing as a C label
354 ppCostCentreLbl (NoCostCentre) = text "NONE_cc"
355 ppCostCentreLbl (AllCafsCC {cc_mod = m}) = ppr m <> text "_CAFs_cc"
356 ppCostCentreLbl (NormalCC {cc_name = n, cc_mod = m, cc_is_caf = is_caf})
357 = ppr m <> ptext n <>
358 text (case is_caf of { CafCC -> "_CAF"; _ -> "" }) <> text "_cc"
360 -- This is the name to go in the user-displayed string,
361 -- recorded in the cost centre declaration
362 costCentreUserName (NoCostCentre) = "NO_CC"
363 costCentreUserName (AllCafsCC {}) = "CAF"
364 costCentreUserName cc@(NormalCC {cc_name = name, cc_is_caf = is_caf})
365 = case is_caf of { CafCC -> "CAF:"; _ -> "" } ++ decode (_UNPK_ name)
368 Cost Centre Declarations
372 pprCostCentreDecl is_local (NoCostCentre)
373 = panic "pprCostCentreDecl: no cost centre!"
375 pprCostCentreDecl is_local cc
378 ptext SLIT("CC_DECLARE"),char '(',
380 pprStringInCStyle (costCentreUserName cc), comma,
381 pprStringInCStyle (moduleNameUserString mod_name), comma,
382 ptext is_subsumed, comma,
383 empty, -- Now always externally visible
386 hcat [ ptext SLIT("CC_EXTERN"),char '(', cc_ident, text ");" ]
388 cc_ident = ppCostCentreLbl cc
390 is_subsumed = ccSubsumed cc
392 ccSubsumed :: CostCentre -> FAST_STRING -- subsumed value
393 ccSubsumed cc | isCafCC cc = SLIT("CC_IS_CAF")
394 | otherwise = SLIT("CC_IS_BORING")