{-# LANGUAGE DerivingVia #-}
{-# OPTIONS_GHC -Wno-incomplete-record-updates #-}
module GHC.Core.Opt.Stats (
SimplCount, doSimplTick, doFreeSimplTick, simplCountN,
pprSimplCount, plusSimplCount, zeroSimplCount,
isZeroSimplCount, hasDetailedCounts, Tick(..)
) where
import GHC.Prelude
import GHC.Types.Var
import GHC.Types.Error
import GHC.Utils.Outputable as Outputable
import GHC.Data.FastString
import Data.List (sortOn)
import Data.List.NonEmpty (NonEmpty(..))
import qualified Data.List.NonEmpty as NE
import Data.Ord
import Data.Map (Map)
import qualified Data.Map as Map
import qualified Data.Map.Strict as MapStrict
import GHC.Utils.Panic (throwGhcException, GhcException(..))
getVerboseSimplStats :: (Bool -> SDoc) -> SDoc
getVerboseSimplStats :: (Bool -> SDoc) -> SDoc
getVerboseSimplStats = (Bool -> SDoc) -> SDoc
forall doc. IsOutput doc => (Bool -> doc) -> doc
getPprDebug
zeroSimplCount :: Bool
-> SimplCount
isZeroSimplCount :: SimplCount -> Bool
hasDetailedCounts :: SimplCount -> Bool
pprSimplCount :: SimplCount -> SDoc
doSimplTick :: Int
-> Tick -> SimplCount -> SimplCount
doFreeSimplTick :: Tick -> SimplCount -> SimplCount
plusSimplCount :: SimplCount -> SimplCount -> SimplCount
data SimplCount
= VerySimplCount !Int
| SimplCount {
SimplCount -> Int
ticks :: !Int,
SimplCount -> TickCounts
details :: !TickCounts,
SimplCount -> Int
n_log :: !Int,
SimplCount -> [Tick]
log1 :: [Tick],
SimplCount -> [Tick]
log2 :: [Tick]
}
type TickCounts = Map Tick Int
simplCountN :: SimplCount -> Int
simplCountN :: SimplCount -> Int
simplCountN (VerySimplCount Int
n) = Int
n
simplCountN (SimplCount { ticks :: SimplCount -> Int
ticks = Int
n }) = Int
n
zeroSimplCount :: Bool -> SimplCount
zeroSimplCount Bool
dump_simpl_stats
| Bool
dump_simpl_stats
= SimplCount {ticks :: Int
ticks = Int
0, details :: TickCounts
details = TickCounts
forall k a. Map k a
Map.empty,
n_log :: Int
n_log = Int
0, log1 :: [Tick]
log1 = [], log2 :: [Tick]
log2 = []}
| Bool
otherwise
= Int -> SimplCount
VerySimplCount Int
0
isZeroSimplCount :: SimplCount -> Bool
isZeroSimplCount (VerySimplCount Int
n) = Int
nInt -> Int -> Bool
forall a. Eq a => a -> a -> Bool
==Int
0
isZeroSimplCount (SimplCount { ticks :: SimplCount -> Int
ticks = Int
n }) = Int
nInt -> Int -> Bool
forall a. Eq a => a -> a -> Bool
==Int
0
hasDetailedCounts :: SimplCount -> Bool
hasDetailedCounts (VerySimplCount {}) = Bool
False
hasDetailedCounts (SimplCount {}) = Bool
True
doFreeSimplTick :: Tick -> SimplCount -> SimplCount
doFreeSimplTick Tick
tick sc :: SimplCount
sc@SimplCount { details :: SimplCount -> TickCounts
details = TickCounts
dts }
= SimplCount
sc { details = dts `addTick` tick }
doFreeSimplTick Tick
_ SimplCount
sc = SimplCount
sc
doSimplTick :: Int -> Tick -> SimplCount -> SimplCount
doSimplTick Int
history_size Tick
tick
sc :: SimplCount
sc@(SimplCount { ticks :: SimplCount -> Int
ticks = Int
tks, details :: SimplCount -> TickCounts
details = TickCounts
dts, n_log :: SimplCount -> Int
n_log = Int
nl, log1 :: SimplCount -> [Tick]
log1 = [Tick]
l1 })
| Int
nl Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
history_size = SimplCount
sc1 { n_log = 1, log1 = [tick], log2 = l1 }
| Bool
otherwise = SimplCount
sc1 { n_log = nl+1, log1 = tick : l1 }
where
sc1 :: SimplCount
sc1 = SimplCount
sc { ticks = tks+1, details = dts `addTick` tick }
doSimplTick Int
_ Tick
_ (VerySimplCount Int
n) = Int -> SimplCount
VerySimplCount (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1)
addTick :: TickCounts -> Tick -> TickCounts
addTick :: TickCounts -> Tick -> TickCounts
addTick TickCounts
fm Tick
tick = (Int -> Int -> Int) -> Tick -> Int -> TickCounts -> TickCounts
forall k a. Ord k => (a -> a -> a) -> k -> a -> Map k a -> Map k a
MapStrict.insertWith Int -> Int -> Int
forall a. Num a => a -> a -> a
(+) Tick
tick Int
1 TickCounts
fm
plusSimplCount :: SimplCount -> SimplCount -> SimplCount
plusSimplCount sc1 :: SimplCount
sc1@(SimplCount { ticks :: SimplCount -> Int
ticks = Int
tks1, details :: SimplCount -> TickCounts
details = TickCounts
dts1 })
sc2 :: SimplCount
sc2@(SimplCount { ticks :: SimplCount -> Int
ticks = Int
tks2, details :: SimplCount -> TickCounts
details = TickCounts
dts2 })
= SimplCount
log_base { ticks = tks1 + tks2
, details = MapStrict.unionWith (+) dts1 dts2 }
where
log_base :: SimplCount
log_base | [Tick] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null (SimplCount -> [Tick]
log1 SimplCount
sc2) = SimplCount
sc1
| [Tick] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null (SimplCount -> [Tick]
log2 SimplCount
sc2) = SimplCount
sc2 { log2 = log1 sc1 }
| Bool
otherwise = SimplCount
sc2
plusSimplCount (VerySimplCount Int
n) (VerySimplCount Int
m) = Int -> SimplCount
VerySimplCount (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
m)
plusSimplCount SimplCount
lhs SimplCount
rhs =
GhcException -> SimplCount
forall a. GhcException -> a
throwGhcException (GhcException -> SimplCount)
-> (SDoc -> GhcException) -> SDoc -> SimplCount
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> SDoc -> GhcException
PprProgramError String
"plusSimplCount" (SDoc -> SimplCount) -> SDoc -> SimplCount
forall a b. (a -> b) -> a -> b
$ [SDoc] -> SDoc
forall doc. IsDoc doc => [doc] -> doc
vcat
[ String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"lhs"
, SimplCount -> SDoc
pprSimplCount SimplCount
lhs
, String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"rhs"
, SimplCount -> SDoc
pprSimplCount SimplCount
rhs
]
pprSimplCount :: SimplCount -> SDoc
pprSimplCount (VerySimplCount Int
n) = String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"Total ticks:" SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<+> Int -> SDoc
forall doc. IsLine doc => Int -> doc
int Int
n
pprSimplCount (SimplCount { ticks :: SimplCount -> Int
ticks = Int
tks, details :: SimplCount -> TickCounts
details = TickCounts
dts, log1 :: SimplCount -> [Tick]
log1 = [Tick]
l1, log2 :: SimplCount -> [Tick]
log2 = [Tick]
l2 })
= [SDoc] -> SDoc
forall doc. IsDoc doc => [doc] -> doc
vcat [String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"Total ticks: " SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<+> Int -> SDoc
forall doc. IsLine doc => Int -> doc
int Int
tks,
SDoc
blankLine,
TickCounts -> SDoc
pprTickCounts TickCounts
dts,
(Bool -> SDoc) -> SDoc
getVerboseSimplStats ((Bool -> SDoc) -> SDoc) -> (Bool -> SDoc) -> SDoc
forall a b. (a -> b) -> a -> b
$ \Bool
dbg -> if Bool
dbg
then
[SDoc] -> SDoc
forall doc. IsDoc doc => [doc] -> doc
vcat [SDoc
blankLine,
String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"Log (most recent first)",
Int -> SDoc -> SDoc
nest Int
4 ([SDoc] -> SDoc
forall doc. IsDoc doc => [doc] -> doc
vcat ((Tick -> SDoc) -> [Tick] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map Tick -> SDoc
forall a. Outputable a => a -> SDoc
ppr [Tick]
l1) SDoc -> SDoc -> SDoc
forall doc. IsDoc doc => doc -> doc -> doc
$$ [SDoc] -> SDoc
forall doc. IsDoc doc => [doc] -> doc
vcat ((Tick -> SDoc) -> [Tick] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map Tick -> SDoc
forall a. Outputable a => a -> SDoc
ppr [Tick]
l2))]
else SDoc
forall doc. IsOutput doc => doc
Outputable.empty
]
pprTickCounts :: Map Tick Int -> SDoc
pprTickCounts :: TickCounts -> SDoc
pprTickCounts TickCounts
counts
= [SDoc] -> SDoc
forall doc. IsDoc doc => [doc] -> doc
vcat ((NonEmpty (Tick, Int) -> SDoc) -> [NonEmpty (Tick, Int)] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map NonEmpty (Tick, Int) -> SDoc
pprTickGroup [NonEmpty (Tick, Int)]
groups)
where
groups :: [NonEmpty (Tick, Int)]
groups :: [NonEmpty (Tick, Int)]
groups = ((Tick, Int) -> Int) -> [(Tick, Int)] -> [NonEmpty (Tick, Int)]
forall (f :: * -> *) b a.
(Foldable f, Eq b) =>
(a -> b) -> f a -> [NonEmpty a]
NE.groupWith (Tick -> Int
tickToTag (Tick -> Int) -> ((Tick, Int) -> Tick) -> (Tick, Int) -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Tick, Int) -> Tick
forall a b. (a, b) -> a
fst) (TickCounts -> [(Tick, Int)]
forall k a. Map k a -> [(k, a)]
Map.toList TickCounts
counts)
pprTickGroup :: NonEmpty (Tick, Int) -> SDoc
pprTickGroup :: NonEmpty (Tick, Int) -> SDoc
pprTickGroup group :: NonEmpty (Tick, Int)
group@((Tick
tick1,Int
_) :| [(Tick, Int)]
_)
= SDoc -> Int -> SDoc -> SDoc
hang (Int -> SDoc
forall doc. IsLine doc => Int -> doc
int (NonEmpty Int -> Int
forall a. Num a => NonEmpty a -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum (((Tick, Int) -> Int) -> NonEmpty (Tick, Int) -> NonEmpty Int
forall a b. (a -> b) -> NonEmpty a -> NonEmpty b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Tick, Int) -> Int
forall a b. (a, b) -> b
snd NonEmpty (Tick, Int)
group)) SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<+> Tick -> SDoc
pprTickType Tick
tick1)
Int
2 ([SDoc] -> SDoc
forall doc. IsDoc doc => [doc] -> doc
vcat [ Int -> SDoc
forall doc. IsLine doc => Int -> doc
int Int
n SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<+> Tick -> SDoc
pprTickCts Tick
tick
| (Tick
tick,Int
n) <- ((Tick, Int) -> Down Int) -> [(Tick, Int)] -> [(Tick, Int)]
forall b a. Ord b => (a -> b) -> [a] -> [a]
sortOn (Int -> Down Int
forall a. a -> Down a
Down (Int -> Down Int)
-> ((Tick, Int) -> Int) -> (Tick, Int) -> Down Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Tick, Int) -> Int
forall a b. (a, b) -> b
snd) (NonEmpty (Tick, Int) -> [(Tick, Int)]
forall a. NonEmpty a -> [a]
NE.toList NonEmpty (Tick, Int)
group)])
data Tick
= PreInlineUnconditionally Id
| PostInlineUnconditionally Id
| UnfoldingDone Id
| RuleFired FastString
| LetFloatFromLet
| EtaExpansion Id
| EtaReduction Id
| BetaReduction Id
| CaseOfCase Id
| KnownBranch Id
| CaseMerge Id
| AltMerge Id
| CaseElim Id
| CaseIdentity Id
| FillInCaseDefault Id
| SimplifierDone
instance Outputable Tick where
ppr :: Tick -> SDoc
ppr Tick
tick = Tick -> SDoc
pprTickType Tick
tick SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<+> Tick -> SDoc
pprTickCts Tick
tick
instance Eq Tick where
Tick
a == :: Tick -> Tick -> Bool
== Tick
b = case Tick
a Tick -> Tick -> Ordering
`cmpTick` Tick
b of
Ordering
EQ -> Bool
True
Ordering
_ -> Bool
False
instance Ord Tick where
compare :: Tick -> Tick -> Ordering
compare = Tick -> Tick -> Ordering
cmpTick
tickToTag :: Tick -> Int
tickToTag :: Tick -> Int
tickToTag (PreInlineUnconditionally Id
_) = Int
0
tickToTag (PostInlineUnconditionally Id
_) = Int
1
tickToTag (UnfoldingDone Id
_) = Int
2
tickToTag (RuleFired FastString
_) = Int
3
tickToTag Tick
LetFloatFromLet = Int
4
tickToTag (EtaExpansion Id
_) = Int
5
tickToTag (EtaReduction Id
_) = Int
6
tickToTag (BetaReduction Id
_) = Int
7
tickToTag (CaseOfCase Id
_) = Int
8
tickToTag (KnownBranch Id
_) = Int
9
tickToTag (CaseMerge Id
_) = Int
10
tickToTag (CaseElim Id
_) = Int
11
tickToTag (CaseIdentity Id
_) = Int
12
tickToTag (FillInCaseDefault Id
_) = Int
13
tickToTag Tick
SimplifierDone = Int
16
tickToTag (AltMerge Id
_) = Int
17
pprTickType :: Tick -> SDoc
pprTickType :: Tick -> SDoc
pprTickType (PreInlineUnconditionally Id
_) = String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"PreInlineUnconditionally"
pprTickType (PostInlineUnconditionally Id
_)= String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"PostInlineUnconditionally"
pprTickType (UnfoldingDone Id
_) = String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"UnfoldingDone"
pprTickType (RuleFired FastString
_) = String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"RuleFired"
pprTickType Tick
LetFloatFromLet = String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"LetFloatFromLet"
pprTickType (EtaExpansion Id
_) = String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"EtaExpansion"
pprTickType (EtaReduction Id
_) = String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"EtaReduction"
pprTickType (BetaReduction Id
_) = String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"BetaReduction"
pprTickType (CaseOfCase Id
_) = String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"CaseOfCase"
pprTickType (KnownBranch Id
_) = String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"KnownBranch"
pprTickType (CaseMerge Id
_) = String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"CaseMerge"
pprTickType (AltMerge Id
_) = String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"AltMerge"
pprTickType (CaseElim Id
_) = String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"CaseElim"
pprTickType (CaseIdentity Id
_) = String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"CaseIdentity"
pprTickType (FillInCaseDefault Id
_) = String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"FillInCaseDefault"
pprTickType Tick
SimplifierDone = String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"SimplifierDone"
pprTickCts :: Tick -> SDoc
pprTickCts :: Tick -> SDoc
pprTickCts (PreInlineUnconditionally Id
v) = Id -> SDoc
forall a. Outputable a => a -> SDoc
ppr Id
v
pprTickCts (PostInlineUnconditionally Id
v)= Id -> SDoc
forall a. Outputable a => a -> SDoc
ppr Id
v
pprTickCts (UnfoldingDone Id
v) = Id -> SDoc
forall a. Outputable a => a -> SDoc
ppr Id
v
pprTickCts (RuleFired FastString
v) = FastString -> SDoc
forall a. Outputable a => a -> SDoc
ppr FastString
v
pprTickCts Tick
LetFloatFromLet = SDoc
forall doc. IsOutput doc => doc
Outputable.empty
pprTickCts (EtaExpansion Id
v) = Id -> SDoc
forall a. Outputable a => a -> SDoc
ppr Id
v
pprTickCts (EtaReduction Id
v) = Id -> SDoc
forall a. Outputable a => a -> SDoc
ppr Id
v
pprTickCts (BetaReduction Id
v) = Id -> SDoc
forall a. Outputable a => a -> SDoc
ppr Id
v
pprTickCts (CaseOfCase Id
v) = Id -> SDoc
forall a. Outputable a => a -> SDoc
ppr Id
v
pprTickCts (KnownBranch Id
v) = Id -> SDoc
forall a. Outputable a => a -> SDoc
ppr Id
v
pprTickCts (CaseMerge Id
v) = Id -> SDoc
forall a. Outputable a => a -> SDoc
ppr Id
v
pprTickCts (AltMerge Id
v) = Id -> SDoc
forall a. Outputable a => a -> SDoc
ppr Id
v
pprTickCts (CaseElim Id
v) = Id -> SDoc
forall a. Outputable a => a -> SDoc
ppr Id
v
pprTickCts (CaseIdentity Id
v) = Id -> SDoc
forall a. Outputable a => a -> SDoc
ppr Id
v
pprTickCts (FillInCaseDefault Id
v) = Id -> SDoc
forall a. Outputable a => a -> SDoc
ppr Id
v
pprTickCts Tick
_ = SDoc
forall doc. IsOutput doc => doc
Outputable.empty
cmpTick :: Tick -> Tick -> Ordering
cmpTick :: Tick -> Tick -> Ordering
cmpTick Tick
a Tick
b = case (Tick -> Int
tickToTag Tick
a Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
`compare` Tick -> Int
tickToTag Tick
b) of
Ordering
GT -> Ordering
GT
Ordering
EQ -> Tick -> Tick -> Ordering
cmpEqTick Tick
a Tick
b
Ordering
LT -> Ordering
LT
cmpEqTick :: Tick -> Tick -> Ordering
cmpEqTick :: Tick -> Tick -> Ordering
cmpEqTick (PreInlineUnconditionally Id
a) (PreInlineUnconditionally Id
b) = Id
a Id -> Id -> Ordering
forall a. Ord a => a -> a -> Ordering
`compare` Id
b
cmpEqTick (PostInlineUnconditionally Id
a) (PostInlineUnconditionally Id
b) = Id
a Id -> Id -> Ordering
forall a. Ord a => a -> a -> Ordering
`compare` Id
b
cmpEqTick (UnfoldingDone Id
a) (UnfoldingDone Id
b) = Id
a Id -> Id -> Ordering
forall a. Ord a => a -> a -> Ordering
`compare` Id
b
cmpEqTick (RuleFired FastString
a) (RuleFired FastString
b) = FastString
a FastString -> FastString -> Ordering
`uniqCompareFS` FastString
b
cmpEqTick (EtaExpansion Id
a) (EtaExpansion Id
b) = Id
a Id -> Id -> Ordering
forall a. Ord a => a -> a -> Ordering
`compare` Id
b
cmpEqTick (EtaReduction Id
a) (EtaReduction Id
b) = Id
a Id -> Id -> Ordering
forall a. Ord a => a -> a -> Ordering
`compare` Id
b
cmpEqTick (BetaReduction Id
a) (BetaReduction Id
b) = Id
a Id -> Id -> Ordering
forall a. Ord a => a -> a -> Ordering
`compare` Id
b
cmpEqTick (CaseOfCase Id
a) (CaseOfCase Id
b) = Id
a Id -> Id -> Ordering
forall a. Ord a => a -> a -> Ordering
`compare` Id
b
cmpEqTick (KnownBranch Id
a) (KnownBranch Id
b) = Id
a Id -> Id -> Ordering
forall a. Ord a => a -> a -> Ordering
`compare` Id
b
cmpEqTick (CaseMerge Id
a) (CaseMerge Id
b) = Id
a Id -> Id -> Ordering
forall a. Ord a => a -> a -> Ordering
`compare` Id
b
cmpEqTick (AltMerge Id
a) (AltMerge Id
b) = Id
a Id -> Id -> Ordering
forall a. Ord a => a -> a -> Ordering
`compare` Id
b
cmpEqTick (CaseElim Id
a) (CaseElim Id
b) = Id
a Id -> Id -> Ordering
forall a. Ord a => a -> a -> Ordering
`compare` Id
b
cmpEqTick (CaseIdentity Id
a) (CaseIdentity Id
b) = Id
a Id -> Id -> Ordering
forall a. Ord a => a -> a -> Ordering
`compare` Id
b
cmpEqTick (FillInCaseDefault Id
a) (FillInCaseDefault Id
b) = Id
a Id -> Id -> Ordering
forall a. Ord a => a -> a -> Ordering
`compare` Id
b
cmpEqTick Tick
_ Tick
_ = Ordering
EQ