module GHC.StgToCmm.Prof (
initCostCentres, ccType, ccsType,
mkCCostCentre, mkCCostCentreStack,
initInfoTableProv, emitInfoTableProv,
dynProfHdr, profDynAlloc, profAlloc, staticProfHdr, initUpdFrameProf,
enterCostCentreThunk, enterCostCentreFun,
costCentreFrom,
storeCurCCS,
emitSetCCC,
saveCurrentCostCentre, restoreCurrentCostCentre,
ldvEnter, ldvEnterClosure, ldvRecordCreate
) where
import GHC.Prelude
import GHC.Driver.Session
import GHC.Driver.Ppr
import GHC.Platform
import GHC.Platform.Profile
import GHC.StgToCmm.Closure
import GHC.StgToCmm.Utils
import GHC.StgToCmm.Monad
import GHC.Runtime.Heap.Layout
import GHC.Cmm.Graph
import GHC.Cmm
import GHC.Cmm.Utils
import GHC.Cmm.CLabel
import GHC.Types.CostCentre
import GHC.Types.IPE
import GHC.Types.ForeignStubs
import GHC.Data.FastString
import GHC.Unit.Module as Module
import GHC.Utils.Outputable
import GHC.Utils.Panic
import GHC.Driver.CodeOutput ( ipInitCode )
import GHC.Utils.Encoding
import Control.Monad
import Data.Char (ord)
ccsType :: Platform -> CmmType
ccsType = bWord
ccType :: Platform -> CmmType
ccType = bWord
storeCurCCS :: CmmExpr -> CmmAGraph
storeCurCCS e = mkAssign cccsReg e
mkCCostCentre :: CostCentre -> CmmLit
mkCCostCentre cc = CmmLabel (mkCCLabel cc)
mkCCostCentreStack :: CostCentreStack -> CmmLit
mkCCostCentreStack ccs = CmmLabel (mkCCSLabel ccs)
costCentreFrom :: Platform
-> CmmExpr
-> CmmExpr
costCentreFrom platform cl = CmmLoad (cmmOffsetB platform cl (pc_OFFSET_StgHeader_ccs (platformConstants platform))) (ccsType platform)
staticProfHdr :: Profile -> CostCentreStack -> [CmmLit]
staticProfHdr profile ccs
| profileIsProfiling profile = [mkCCostCentreStack ccs, staticLdvInit platform]
| otherwise = []
where platform = profilePlatform profile
dynProfHdr :: Profile -> CmmExpr -> [CmmExpr]
dynProfHdr profile ccs
| profileIsProfiling profile = [ccs, dynLdvInit (profilePlatform profile)]
| otherwise = []
initUpdFrameProf :: CmmExpr -> FCode ()
initUpdFrameProf frame
= ifProfiling $
do platform <- getPlatform
emitStore (cmmOffset platform frame (pc_OFFSET_StgHeader_ccs (platformConstants platform))) cccsExpr
saveCurrentCostCentre :: FCode (Maybe LocalReg)
saveCurrentCostCentre
= do dflags <- getDynFlags
platform <- getPlatform
if not (sccProfilingEnabled dflags)
then return Nothing
else do local_cc <- newTemp (ccType platform)
emitAssign (CmmLocal local_cc) cccsExpr
return (Just local_cc)
restoreCurrentCostCentre :: Maybe LocalReg -> FCode ()
restoreCurrentCostCentre Nothing
= return ()
restoreCurrentCostCentre (Just local_cc)
= emit (storeCurCCS (CmmReg (CmmLocal local_cc)))
profDynAlloc :: SMRep -> CmmExpr -> FCode ()
profDynAlloc rep ccs
= ifProfiling $
do profile <- targetProfile <$> getDynFlags
let platform = profilePlatform profile
profAlloc (mkIntExpr platform (heapClosureSizeW profile rep)) ccs
profAlloc :: CmmExpr -> CmmExpr -> FCode ()
profAlloc words ccs
= ifProfiling $
do profile <- targetProfile <$> getDynFlags
let platform = profilePlatform profile
let alloc_rep = rEP_CostCentreStack_mem_alloc platform
emit $ addToMemE alloc_rep
(cmmOffsetB platform ccs (pc_OFFSET_CostCentreStack_mem_alloc (platformConstants platform)))
(CmmMachOp (MO_UU_Conv (wordWidth platform) (typeWidth alloc_rep)) $
[CmmMachOp (mo_wordSub platform) [ words, mkIntExpr platform (profHdrSize profile)]]
)
enterCostCentreThunk :: CmmExpr -> FCode ()
enterCostCentreThunk closure =
ifProfiling $ do
platform <- getPlatform
emit $ storeCurCCS (costCentreFrom platform closure)
enterCostCentreFun :: CostCentreStack -> CmmExpr -> FCode ()
enterCostCentreFun ccs closure =
ifProfiling $
if isCurrentCCS ccs
then do platform <- getPlatform
emitRtsCall rtsUnitId (fsLit "enterFunCCS")
[(baseExpr, AddrHint),
(costCentreFrom platform closure, AddrHint)] False
else return ()
ifProfiling :: FCode () -> FCode ()
ifProfiling code
= do profile <- targetProfile <$> getDynFlags
if profileIsProfiling profile
then code
else return ()
initCostCentres :: CollectedCCs -> FCode ()
initCostCentres (local_CCs, singleton_CCSs)
= ifProfiling $ do
mapM_ emitCostCentreDecl local_CCs
mapM_ emitCostCentreStackDecl singleton_CCSs
emitCostCentreDecl :: CostCentre -> FCode ()
emitCostCentreDecl cc = do
{ dflags <- getDynFlags
; platform <- getPlatform
; let is_caf | isCafCC cc = mkIntCLit platform (ord 'c')
| otherwise = zero platform
; label <- newByteStringCLit (bytesFS $ costCentreUserNameFS cc)
; modl <- newByteStringCLit (bytesFS $ moduleNameFS
$ moduleName
$ cc_mod cc)
; loc <- newByteStringCLit $ utf8EncodeString $
showPpr dflags (costCentreSrcSpan cc)
; let
lits = [ zero platform,
label,
modl,
loc,
zero64,
zero platform,
is_caf,
zero platform
]
; emitDataLits (mkCCLabel cc) lits
}
emitCostCentreStackDecl :: CostCentreStack -> FCode ()
emitCostCentreStackDecl ccs
= case maybeSingletonCCS ccs of
Just cc ->
do platform <- getPlatform
let mk_lits cc = zero platform :
mkCCostCentre cc :
replicate (sizeof_ccs_words platform 2) (zero platform)
emitDataLits (mkCCSLabel ccs) (mk_lits cc)
Nothing -> pprPanic "emitCostCentreStackDecl" (ppr ccs)
zero :: Platform -> CmmLit
zero platform = mkIntCLit platform 0
zero64 :: CmmLit
zero64 = CmmInt 0 W64
sizeof_ccs_words :: Platform -> Int
sizeof_ccs_words platform
| ms == 0 = ws
| otherwise = ws + 1
where
(ws,ms) = pc_SIZEOF_CostCentreStack (platformConstants platform) `divMod` platformWordSizeInBytes platform
initInfoTableProv :: [CmmInfoTable] -> InfoTableProvMap -> Module -> FCode CStub
initInfoTableProv infos itmap this_mod
= do
dflags <- getDynFlags
let ents = convertInfoProvMap dflags infos this_mod itmap
mapM_ emitInfoTableProv ents
return (ipInitCode dflags this_mod ents)
emitInfoTableProv :: InfoProvEnt -> FCode ()
emitInfoTableProv ip = do
{ dflags <- getDynFlags
; let mod = infoProvModule ip
; let (src, label) = maybe ("", "") (\(s, l) -> (showPpr dflags s, l)) (infoTableProv ip)
; platform <- getPlatform
; let mk_string = newByteStringCLit . utf8EncodeString
; label <- mk_string label
; modl <- newByteStringCLit (bytesFS $ moduleNameFS
$ moduleName
$ mod)
; ty_string <- mk_string (infoTableType ip)
; loc <- mk_string src
; table_name <- mk_string (showPpr dflags (pprCLabel platform CStyle (infoTablePtr ip)))
; closure_type <- mk_string
(showPpr dflags (text $ show $ infoProvEntClosureType ip))
; let
lits = [ CmmLabel (infoTablePtr ip),
table_name,
closure_type,
ty_string,
label,
modl,
loc,
zero platform
]
; emitDataLits (mkIPELabel ip) lits
}
emitSetCCC :: CostCentre -> Bool -> Bool -> FCode ()
emitSetCCC cc tick push
= do profile <- targetProfile <$> getDynFlags
let platform = profilePlatform profile
if not (profileIsProfiling profile)
then return ()
else do tmp <- newTemp (ccsType platform)
pushCostCentre tmp cccsExpr cc
when tick $ emit (bumpSccCount platform (CmmReg (CmmLocal tmp)))
when push $ emit (storeCurCCS (CmmReg (CmmLocal tmp)))
pushCostCentre :: LocalReg -> CmmExpr -> CostCentre -> FCode ()
pushCostCentre result ccs cc
= emitRtsCallWithResult result AddrHint
rtsUnitId
(fsLit "pushCostCentre") [(ccs,AddrHint),
(CmmLit (mkCCostCentre cc), AddrHint)]
False
bumpSccCount :: Platform -> CmmExpr -> CmmAGraph
bumpSccCount platform ccs
= addToMem (rEP_CostCentreStack_scc_count platform)
(cmmOffsetB platform ccs (pc_OFFSET_CostCentreStack_scc_count (platformConstants platform))) 1
staticLdvInit :: Platform -> CmmLit
staticLdvInit = zeroCLit
dynLdvInit :: Platform -> CmmExpr
dynLdvInit platform =
CmmMachOp (mo_wordOr platform) [
CmmMachOp (mo_wordShl platform) [loadEra platform, mkIntExpr platform (pc_LDV_SHIFT (platformConstants platform))],
CmmLit (mkWordCLit platform (pc_ILDV_STATE_CREATE (platformConstants platform)))
]
ldvRecordCreate :: CmmExpr -> FCode ()
ldvRecordCreate closure = do
platform <- getPlatform
emit $ mkStore (ldvWord platform closure) (dynLdvInit platform)
ldvEnterClosure :: ClosureInfo -> CmmReg -> FCode ()
ldvEnterClosure closure_info node_reg = do
platform <- getPlatform
let tag = funTag platform closure_info
ldvEnter (cmmOffsetB platform (CmmReg node_reg) (tag))
ldvEnter :: CmmExpr -> FCode ()
ldvEnter cl_ptr = do
platform <- getPlatform
let constants = platformConstants platform
ldv_wd = ldvWord platform cl_ptr
new_ldv_wd = cmmOrWord platform
(cmmAndWord platform (CmmLoad ldv_wd (bWord platform))
(CmmLit (mkWordCLit platform (pc_ILDV_CREATE_MASK constants))))
(cmmOrWord platform (loadEra platform) (CmmLit (mkWordCLit platform (pc_ILDV_STATE_USE constants))))
ifProfiling $
emit =<< mkCmmIfThenElse (CmmMachOp (mo_wordUGt platform) [loadEra platform, CmmLit (zeroCLit platform)])
(mkStore ldv_wd new_ldv_wd)
mkNop
loadEra :: Platform -> CmmExpr
loadEra platform = CmmMachOp (MO_UU_Conv (cIntWidth platform) (wordWidth platform))
[CmmLoad (mkLblExpr (mkRtsCmmDataLabel (fsLit "era")))
(cInt platform)]
ldvWord :: Platform -> CmmExpr -> CmmExpr
ldvWord platform closure_ptr
= cmmOffsetB platform closure_ptr (pc_OFFSET_StgHeader_ldvw (platformConstants platform))