{-# LANGUAGE GADTs #-}
module GHC.Driver.GenerateCgIPEStub (generateCgIPEStub) where
import qualified Data.Map.Strict as Map
import Data.Maybe (mapMaybe, listToMaybe)
import GHC.Cmm
import GHC.Cmm.CLabel (CLabel)
import GHC.Cmm.Dataflow (Block, C, O)
import GHC.Cmm.Dataflow.Block (blockSplit, blockToList)
import GHC.Cmm.Dataflow.Collections (mapToList)
import GHC.Cmm.Dataflow.Label (Label)
import GHC.Cmm.Info.Build (emptySRT)
import GHC.Cmm.Pipeline (cmmPipeline)
import GHC.Data.Maybe (firstJusts)
import GHC.Data.Stream (Stream, liftIO)
import qualified GHC.Data.Stream as Stream
import GHC.Driver.Env (hsc_dflags, hsc_logger)
import GHC.Driver.Env.Types (HscEnv)
import GHC.Driver.Flags (GeneralFlag (Opt_InfoTableMap))
import GHC.Driver.Session (gopt, targetPlatform)
import GHC.Driver.Config.StgToCmm
import GHC.Driver.Config.Cmm
import GHC.Prelude
import GHC.Runtime.Heap.Layout (isStackRep)
import GHC.Settings (Platform, platformUnregisterised)
import GHC.StgToCmm.Monad (getCmm, initC, runC, initFCodeState)
import GHC.StgToCmm.Prof (initInfoTableProv)
import GHC.StgToCmm.Types (CmmCgInfos (..), ModuleLFInfos)
import GHC.Types.IPE (InfoTableProvMap (provInfoTables), IpeSourceLocation)
import GHC.Types.Name.Set (NonCaffySet)
import GHC.Types.Tickish (GenTickish (SourceNote))
import GHC.Unit.Types (Module)
import GHC.Utils.Misc
generateCgIPEStub :: HscEnv -> Module -> InfoTableProvMap -> Stream IO CmmGroupSRTs (NonCaffySet, ModuleLFInfos) -> Stream IO CmmGroupSRTs CmmCgInfos
generateCgIPEStub :: HscEnv
-> Module
-> InfoTableProvMap
-> Stream IO CmmGroupSRTs (NonCaffySet, ModuleLFInfos)
-> Stream IO CmmGroupSRTs CmmCgInfos
generateCgIPEStub HscEnv
hsc_env Module
this_mod InfoTableProvMap
denv Stream IO CmmGroupSRTs (NonCaffySet, ModuleLFInfos)
s = do
let dflags :: DynFlags
dflags = HscEnv -> DynFlags
hsc_dflags HscEnv
hsc_env
platform :: Platform
platform = DynFlags -> Platform
targetPlatform DynFlags
dflags
logger :: Logger
logger = HscEnv -> Logger
hsc_logger HscEnv
hsc_env
fstate :: FCodeState
fstate = Platform -> FCodeState
initFCodeState Platform
platform
cmm_cfg :: CmmConfig
cmm_cfg = DynFlags -> CmmConfig
initCmmConfig DynFlags
dflags
CgState
cgState <- IO CgState -> Stream IO CmmGroupSRTs CgState
forall a. IO a -> Stream IO CmmGroupSRTs a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO CgState
initC
let collectFun :: [(Label, CmmInfoTable, Maybe IpeSourceLocation)]
-> CmmGroupSRTs
-> IO
([(Label, CmmInfoTable, Maybe IpeSourceLocation)], CmmGroupSRTs)
collectFun = if GeneralFlag -> DynFlags -> Bool
gopt GeneralFlag
Opt_InfoTableMap DynFlags
dflags then Platform
-> [(Label, CmmInfoTable, Maybe IpeSourceLocation)]
-> CmmGroupSRTs
-> IO
([(Label, CmmInfoTable, Maybe IpeSourceLocation)], CmmGroupSRTs)
collect Platform
platform else [(Label, CmmInfoTable, Maybe IpeSourceLocation)]
-> CmmGroupSRTs
-> IO
([(Label, CmmInfoTable, Maybe IpeSourceLocation)], CmmGroupSRTs)
forall a. [a] -> CmmGroupSRTs -> IO ([a], CmmGroupSRTs)
collectNothing
([(Label, CmmInfoTable, Maybe IpeSourceLocation)]
labeledInfoTablesWithTickishes, (NonCaffySet
nonCaffySet, ModuleLFInfos
moduleLFInfos)) <- ([(Label, CmmInfoTable, Maybe IpeSourceLocation)]
-> CmmGroupSRTs
-> IO
([(Label, CmmInfoTable, Maybe IpeSourceLocation)], CmmGroupSRTs))
-> [(Label, CmmInfoTable, Maybe IpeSourceLocation)]
-> Stream IO CmmGroupSRTs (NonCaffySet, ModuleLFInfos)
-> Stream
IO
CmmGroupSRTs
([(Label, CmmInfoTable, Maybe IpeSourceLocation)],
(NonCaffySet, ModuleLFInfos))
forall (m :: * -> *) a b c r.
Monad m =>
(c -> a -> m (c, b)) -> c -> Stream m a r -> Stream m b (c, r)
Stream.mapAccumL_ [(Label, CmmInfoTable, Maybe IpeSourceLocation)]
-> CmmGroupSRTs
-> IO
([(Label, CmmInfoTable, Maybe IpeSourceLocation)], CmmGroupSRTs)
collectFun [] Stream IO CmmGroupSRTs (NonCaffySet, ModuleLFInfos)
s
let denv' :: InfoTableProvMap
denv' = InfoTableProvMap
denv {provInfoTables = Map.fromList (map (\(Label
_, CmmInfoTable
i, Maybe IpeSourceLocation
t) -> (CmmInfoTable -> CLabel
cit_lbl CmmInfoTable
i, Maybe IpeSourceLocation
t)) labeledInfoTablesWithTickishes)}
((CStub
ipeStub, CmmGroup
ipeCmmGroup), CgState
_) = StgToCmmConfig
-> FCodeState
-> CgState
-> FCode (CStub, CmmGroup)
-> ((CStub, CmmGroup), CgState)
forall a.
StgToCmmConfig -> FCodeState -> CgState -> FCode a -> (a, CgState)
runC (DynFlags -> Module -> StgToCmmConfig
initStgToCmmConfig DynFlags
dflags Module
this_mod) FCodeState
fstate CgState
cgState (FCode (CStub, CmmGroup) -> ((CStub, CmmGroup), CgState))
-> FCode (CStub, CmmGroup) -> ((CStub, CmmGroup), CgState)
forall a b. (a -> b) -> a -> b
$ FCode CStub -> FCode (CStub, CmmGroup)
forall a. FCode a -> FCode (a, CmmGroup)
getCmm ([CmmInfoTable] -> InfoTableProvMap -> FCode CStub
initInfoTableProv (((Label, CmmInfoTable, Maybe IpeSourceLocation) -> CmmInfoTable)
-> [(Label, CmmInfoTable, Maybe IpeSourceLocation)]
-> [CmmInfoTable]
forall a b. (a -> b) -> [a] -> [b]
map (Label, CmmInfoTable, Maybe IpeSourceLocation) -> CmmInfoTable
forall a b c. (a, b, c) -> b
sndOf3 [(Label, CmmInfoTable, Maybe IpeSourceLocation)]
labeledInfoTablesWithTickishes) InfoTableProvMap
denv')
(ModuleSRTInfo
_, CmmGroupSRTs
ipeCmmGroupSRTs) <- IO (ModuleSRTInfo, CmmGroupSRTs)
-> Stream IO CmmGroupSRTs (ModuleSRTInfo, CmmGroupSRTs)
forall a. IO a -> Stream IO CmmGroupSRTs a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (ModuleSRTInfo, CmmGroupSRTs)
-> Stream IO CmmGroupSRTs (ModuleSRTInfo, CmmGroupSRTs))
-> IO (ModuleSRTInfo, CmmGroupSRTs)
-> Stream IO CmmGroupSRTs (ModuleSRTInfo, CmmGroupSRTs)
forall a b. (a -> b) -> a -> b
$ Logger
-> CmmConfig
-> ModuleSRTInfo
-> CmmGroup
-> IO (ModuleSRTInfo, CmmGroupSRTs)
cmmPipeline Logger
logger CmmConfig
cmm_cfg (Module -> ModuleSRTInfo
emptySRT Module
this_mod) CmmGroup
ipeCmmGroup
CmmGroupSRTs -> Stream IO CmmGroupSRTs ()
forall (m :: * -> *) a. Monad m => a -> Stream m a ()
Stream.yield CmmGroupSRTs
ipeCmmGroupSRTs
CmmCgInfos -> Stream IO CmmGroupSRTs CmmCgInfos
forall a. a -> Stream IO CmmGroupSRTs a
forall (m :: * -> *) a. Monad m => a -> m a
return CmmCgInfos {cgNonCafs :: NonCaffySet
cgNonCafs = NonCaffySet
nonCaffySet, cgLFInfos :: ModuleLFInfos
cgLFInfos = ModuleLFInfos
moduleLFInfos, cgIPEStub :: CStub
cgIPEStub = CStub
ipeStub}
where
collect :: Platform -> [(Label, CmmInfoTable, Maybe IpeSourceLocation)] -> CmmGroupSRTs -> IO ([(Label, CmmInfoTable, Maybe IpeSourceLocation)], CmmGroupSRTs)
collect :: Platform
-> [(Label, CmmInfoTable, Maybe IpeSourceLocation)]
-> CmmGroupSRTs
-> IO
([(Label, CmmInfoTable, Maybe IpeSourceLocation)], CmmGroupSRTs)
collect Platform
platform [(Label, CmmInfoTable, Maybe IpeSourceLocation)]
acc CmmGroupSRTs
cmmGroupSRTs = do
let labelsToInfoTables :: [(Label, CmmInfoTable)]
labelsToInfoTables = CmmGroupSRTs -> [(Label, CmmInfoTable)]
collectInfoTables CmmGroupSRTs
cmmGroupSRTs
labelsToInfoTablesToTickishes :: [(Label, CmmInfoTable, Maybe IpeSourceLocation)]
labelsToInfoTablesToTickishes = ((Label, CmmInfoTable)
-> (Label, CmmInfoTable, Maybe IpeSourceLocation))
-> [(Label, CmmInfoTable)]
-> [(Label, CmmInfoTable, Maybe IpeSourceLocation)]
forall a b. (a -> b) -> [a] -> [b]
map (\(Label
l, CmmInfoTable
i) -> (Label
l, CmmInfoTable
i, Platform
-> CmmGroupSRTs -> Label -> CmmInfoTable -> Maybe IpeSourceLocation
lookupEstimatedTick Platform
platform CmmGroupSRTs
cmmGroupSRTs Label
l CmmInfoTable
i)) [(Label, CmmInfoTable)]
labelsToInfoTables
([(Label, CmmInfoTable, Maybe IpeSourceLocation)], CmmGroupSRTs)
-> IO
([(Label, CmmInfoTable, Maybe IpeSourceLocation)], CmmGroupSRTs)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ([(Label, CmmInfoTable, Maybe IpeSourceLocation)]
acc [(Label, CmmInfoTable, Maybe IpeSourceLocation)]
-> [(Label, CmmInfoTable, Maybe IpeSourceLocation)]
-> [(Label, CmmInfoTable, Maybe IpeSourceLocation)]
forall a. [a] -> [a] -> [a]
++ [(Label, CmmInfoTable, Maybe IpeSourceLocation)]
labelsToInfoTablesToTickishes, CmmGroupSRTs
cmmGroupSRTs)
collectNothing :: [a] -> CmmGroupSRTs -> IO ([a], CmmGroupSRTs)
collectNothing :: forall a. [a] -> CmmGroupSRTs -> IO ([a], CmmGroupSRTs)
collectNothing [a]
_ CmmGroupSRTs
cmmGroupSRTs = ([a], CmmGroupSRTs) -> IO ([a], CmmGroupSRTs)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([], CmmGroupSRTs
cmmGroupSRTs)
collectInfoTables :: CmmGroupSRTs -> [(Label, CmmInfoTable)]
collectInfoTables :: CmmGroupSRTs -> [(Label, CmmInfoTable)]
collectInfoTables CmmGroupSRTs
cmmGroup = [[(Label, CmmInfoTable)]] -> [(Label, CmmInfoTable)]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[(Label, CmmInfoTable)]] -> [(Label, CmmInfoTable)])
-> [[(Label, CmmInfoTable)]] -> [(Label, CmmInfoTable)]
forall a b. (a -> b) -> a -> b
$ (GenCmmDecl RawCmmStatics CmmTopInfo CmmGraph
-> Maybe [(Label, CmmInfoTable)])
-> CmmGroupSRTs -> [[(Label, CmmInfoTable)]]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe GenCmmDecl RawCmmStatics CmmTopInfo CmmGraph
-> Maybe [(Label, CmmInfoTable)]
extractInfoTables CmmGroupSRTs
cmmGroup
extractInfoTables :: GenCmmDecl RawCmmStatics CmmTopInfo CmmGraph -> Maybe [(Label, CmmInfoTable)]
extractInfoTables :: GenCmmDecl RawCmmStatics CmmTopInfo CmmGraph
-> Maybe [(Label, CmmInfoTable)]
extractInfoTables (CmmProc CmmTopInfo
h CLabel
_ [GlobalReg]
_ CmmGraph
_) = [(Label, CmmInfoTable)] -> Maybe [(Label, CmmInfoTable)]
forall a. a -> Maybe a
Just ([(Label, CmmInfoTable)] -> Maybe [(Label, CmmInfoTable)])
-> [(Label, CmmInfoTable)] -> Maybe [(Label, CmmInfoTable)]
forall a b. (a -> b) -> a -> b
$ LabelMap CmmInfoTable -> [(KeyOf LabelMap, CmmInfoTable)]
forall a. LabelMap a -> [(KeyOf LabelMap, a)]
forall (map :: * -> *) a. IsMap map => map a -> [(KeyOf map, a)]
mapToList (CmmTopInfo -> LabelMap CmmInfoTable
info_tbls CmmTopInfo
h)
extractInfoTables GenCmmDecl RawCmmStatics CmmTopInfo CmmGraph
_ = Maybe [(Label, CmmInfoTable)]
forall a. Maybe a
Nothing
lookupEstimatedTick :: Platform -> CmmGroupSRTs -> Label -> CmmInfoTable -> Maybe IpeSourceLocation
lookupEstimatedTick :: Platform
-> CmmGroupSRTs -> Label -> CmmInfoTable -> Maybe IpeSourceLocation
lookupEstimatedTick Platform
platform CmmGroupSRTs
cmmGroup Label
infoTableLabel CmmInfoTable
infoTable = do
if (SMRep -> Bool
isStackRep (SMRep -> Bool) -> (CmmInfoTable -> SMRep) -> CmmInfoTable -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CmmInfoTable -> SMRep
cit_rep) CmmInfoTable
infoTable
then do
let findFun :: Block CmmNode C C -> Maybe IpeSourceLocation
findFun =
if Platform -> Bool
platformUnregisterised Platform
platform
then CLabel -> Block CmmNode C C -> Maybe IpeSourceLocation
findCmmTickishForForUnregistered (CmmInfoTable -> CLabel
cit_lbl CmmInfoTable
infoTable)
else Label -> Block CmmNode C C -> Maybe IpeSourceLocation
findCmmTickishForRegistered Label
infoTableLabel
blocks :: [Block CmmNode C C]
blocks = (CmmGraph -> [Block CmmNode C C])
-> [CmmGraph] -> [Block CmmNode C C]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap CmmGraph -> [Block CmmNode C C]
toBlockList (CmmGroupSRTs -> [CmmGraph]
graphs CmmGroupSRTs
cmmGroup)
[Maybe IpeSourceLocation] -> Maybe IpeSourceLocation
forall (f :: * -> *) a. Foldable f => f (Maybe a) -> Maybe a
firstJusts ([Maybe IpeSourceLocation] -> Maybe IpeSourceLocation)
-> [Maybe IpeSourceLocation] -> Maybe IpeSourceLocation
forall a b. (a -> b) -> a -> b
$ (Block CmmNode C C -> Maybe IpeSourceLocation)
-> [Block CmmNode C C] -> [Maybe IpeSourceLocation]
forall a b. (a -> b) -> [a] -> [b]
map Block CmmNode C C -> Maybe IpeSourceLocation
findFun [Block CmmNode C C]
blocks
else Maybe IpeSourceLocation
forall a. Maybe a
Nothing
graphs :: CmmGroupSRTs -> [CmmGraph]
graphs :: CmmGroupSRTs -> [CmmGraph]
graphs = ([CmmGraph]
-> GenCmmDecl RawCmmStatics CmmTopInfo CmmGraph -> [CmmGraph])
-> [CmmGraph] -> CmmGroupSRTs -> [CmmGraph]
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' [CmmGraph]
-> GenCmmDecl RawCmmStatics CmmTopInfo CmmGraph -> [CmmGraph]
forall d h. [CmmGraph] -> GenCmmDecl d h CmmGraph -> [CmmGraph]
go []
where
go :: [CmmGraph] -> GenCmmDecl d h CmmGraph -> [CmmGraph]
go :: forall d h. [CmmGraph] -> GenCmmDecl d h CmmGraph -> [CmmGraph]
go [CmmGraph]
acc (CmmProc h
_ CLabel
_ [GlobalReg]
_ CmmGraph
g) = CmmGraph
g CmmGraph -> [CmmGraph] -> [CmmGraph]
forall a. a -> [a] -> [a]
: [CmmGraph]
acc
go [CmmGraph]
acc GenCmmDecl d h CmmGraph
_ = [CmmGraph]
acc
findCmmTickishForRegistered :: Label -> Block CmmNode C C -> Maybe IpeSourceLocation
findCmmTickishForRegistered :: Label -> Block CmmNode C C -> Maybe IpeSourceLocation
findCmmTickishForRegistered Label
label Block CmmNode C C
block = do
let (CmmNode C O
_, Block CmmNode O O
middleBlock, CmmNode O C
endBlock) = Block CmmNode C C -> (CmmNode C O, Block CmmNode O O, CmmNode O C)
forall (n :: Extensibility -> Extensibility -> *).
Block n C C -> (n C O, Block n O O, n O C)
blockSplit Block CmmNode C C
block
CmmNode O C -> Label -> Maybe ()
isCallWithReturnFrameLabel CmmNode O C
endBlock Label
label
Block CmmNode O O -> Maybe IpeSourceLocation
lastTickInBlock Block CmmNode O O
middleBlock
where
isCallWithReturnFrameLabel :: CmmNode O C -> Label -> Maybe ()
isCallWithReturnFrameLabel :: CmmNode O C -> Label -> Maybe ()
isCallWithReturnFrameLabel (CmmCall CmmExpr
_ (Just Label
l) [GlobalReg]
_ ByteOff
_ ByteOff
_ ByteOff
_) Label
clabel | Label
l Label -> Label -> Bool
forall a. Eq a => a -> a -> Bool
== Label
clabel = () -> Maybe ()
forall a. a -> Maybe a
Just ()
isCallWithReturnFrameLabel CmmNode O C
_ Label
_ = Maybe ()
forall a. Maybe a
Nothing
lastTickInBlock :: Block CmmNode O O -> Maybe IpeSourceLocation
lastTickInBlock Block CmmNode O O
block =
[IpeSourceLocation] -> Maybe IpeSourceLocation
forall a. [a] -> Maybe a
listToMaybe ([IpeSourceLocation] -> Maybe IpeSourceLocation)
-> [IpeSourceLocation] -> Maybe IpeSourceLocation
forall a b. (a -> b) -> a -> b
$
(CmmNode O O -> Maybe IpeSourceLocation)
-> [CmmNode O O] -> [IpeSourceLocation]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe CmmNode O O -> Maybe IpeSourceLocation
maybeTick ([CmmNode O O] -> [IpeSourceLocation])
-> [CmmNode O O] -> [IpeSourceLocation]
forall a b. (a -> b) -> a -> b
$ ([CmmNode O O] -> [CmmNode O O]
forall a. [a] -> [a]
reverse ([CmmNode O O] -> [CmmNode O O])
-> (Block CmmNode O O -> [CmmNode O O])
-> Block CmmNode O O
-> [CmmNode O O]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Block CmmNode O O -> [CmmNode O O]
forall (n :: Extensibility -> Extensibility -> *).
Block n O O -> [n O O]
blockToList) Block CmmNode O O
block
maybeTick :: CmmNode O O -> Maybe IpeSourceLocation
maybeTick :: CmmNode O O -> Maybe IpeSourceLocation
maybeTick (CmmTick (SourceNote RealSrcSpan
span String
name)) = IpeSourceLocation -> Maybe IpeSourceLocation
forall a. a -> Maybe a
Just (RealSrcSpan
span, String
name)
maybeTick CmmNode O O
_ = Maybe IpeSourceLocation
forall a. Maybe a
Nothing
findCmmTickishForForUnregistered :: CLabel -> Block CmmNode C C -> Maybe IpeSourceLocation
findCmmTickishForForUnregistered :: CLabel -> Block CmmNode C C -> Maybe IpeSourceLocation
findCmmTickishForForUnregistered CLabel
cLabel Block CmmNode C C
block = do
let (CmmNode C O
_, Block CmmNode O O
middleBlock, CmmNode O C
_) = Block CmmNode C C -> (CmmNode C O, Block CmmNode O O, CmmNode O C)
forall (n :: Extensibility -> Extensibility -> *).
Block n C C -> (n C O, Block n O O, n O C)
blockSplit Block CmmNode C C
block
CLabel
-> [CmmNode O O]
-> Maybe IpeSourceLocation
-> Maybe IpeSourceLocation
find CLabel
cLabel (Block CmmNode O O -> [CmmNode O O]
forall (n :: Extensibility -> Extensibility -> *).
Block n O O -> [n O O]
blockToList Block CmmNode O O
middleBlock) Maybe IpeSourceLocation
forall a. Maybe a
Nothing
where
find :: CLabel -> [CmmNode O O] -> Maybe IpeSourceLocation -> Maybe IpeSourceLocation
find :: CLabel
-> [CmmNode O O]
-> Maybe IpeSourceLocation
-> Maybe IpeSourceLocation
find CLabel
label (CmmNode O O
b : [CmmNode O O]
blocks) Maybe IpeSourceLocation
lastTick = case CmmNode O O
b of
(CmmStore CmmExpr
_ (CmmLit (CmmLabel CLabel
l)) AlignmentSpec
_) -> if CLabel
label CLabel -> CLabel -> Bool
forall a. Eq a => a -> a -> Bool
== CLabel
l then Maybe IpeSourceLocation
lastTick else CLabel
-> [CmmNode O O]
-> Maybe IpeSourceLocation
-> Maybe IpeSourceLocation
find CLabel
label [CmmNode O O]
blocks Maybe IpeSourceLocation
lastTick
(CmmTick (SourceNote RealSrcSpan
span String
name)) -> CLabel
-> [CmmNode O O]
-> Maybe IpeSourceLocation
-> Maybe IpeSourceLocation
find CLabel
label [CmmNode O O]
blocks (Maybe IpeSourceLocation -> Maybe IpeSourceLocation)
-> Maybe IpeSourceLocation -> Maybe IpeSourceLocation
forall a b. (a -> b) -> a -> b
$ IpeSourceLocation -> Maybe IpeSourceLocation
forall a. a -> Maybe a
Just (RealSrcSpan
span, String
name)
CmmNode O O
_ -> CLabel
-> [CmmNode O O]
-> Maybe IpeSourceLocation
-> Maybe IpeSourceLocation
find CLabel
label [CmmNode O O]
blocks Maybe IpeSourceLocation
lastTick
find CLabel
_ [] Maybe IpeSourceLocation
_ = Maybe IpeSourceLocation
forall a. Maybe a
Nothing