Safe Haskell | None |
---|---|
Language | Haskell2010 |
A language to express the evaluation context of an expression as a
Demand
and track how an expression evaluates free variables and arguments
in turn as a DmdType
.
Lays out the abstract domain for GHC.Core.Opt.DmdAnal.
Synopsis
- data Boxity
- data Card where
- type CardNonAbs = Card
- type CardNonOnce = Card
- data Demand where
- data SubDemand
- mkProd :: Boxity -> [Demand] -> SubDemand
- viewProd :: Arity -> SubDemand -> Maybe (Boxity, [Demand])
- absDmd :: Demand
- topDmd :: Demand
- botDmd :: Demand
- seqDmd :: Demand
- topSubDmd :: SubDemand
- lubCard :: Card -> Card -> Card
- lubDmd :: Demand -> Demand -> Demand
- lubSubDmd :: SubDemand -> SubDemand -> SubDemand
- plusCard :: Card -> Card -> Card
- plusDmd :: Demand -> Demand -> Demand
- plusSubDmd :: SubDemand -> SubDemand -> SubDemand
- multCard :: Card -> Card -> Card
- multDmd :: Card -> Demand -> Demand
- multSubDmd :: Card -> SubDemand -> SubDemand
- isAbs :: Card -> Bool
- isUsedOnce :: Card -> Bool
- isStrict :: Card -> Bool
- isAbsDmd :: Demand -> Bool
- isUsedOnceDmd :: Demand -> Bool
- isStrUsedDmd :: Demand -> Bool
- isStrictDmd :: Demand -> Bool
- isTopDmd :: Demand -> Bool
- isWeakDmd :: Demand -> Bool
- onlyBoxedArguments :: DmdSig -> Bool
- evalDmd :: Demand
- lazyApply1Dmd :: Demand
- lazyApply2Dmd :: Demand
- strictOnceApply1Dmd :: Demand
- strictManyApply1Dmd :: Demand
- oneifyCard :: Card -> Card
- oneifyDmd :: Demand -> Demand
- strictifyDmd :: Demand -> Demand
- strictifyDictDmd :: Type -> Demand -> Demand
- lazifyDmd :: Demand -> Demand
- peelCallDmd :: SubDemand -> (Card, SubDemand)
- peelManyCalls :: Int -> SubDemand -> Card
- mkCalledOnceDmd :: SubDemand -> SubDemand
- mkCalledOnceDmds :: Arity -> SubDemand -> SubDemand
- mkWorkerDemand :: Int -> Demand
- argOneShots :: Demand -> [OneShotInfo]
- argsOneShots :: DmdSig -> Arity -> [[OneShotInfo]]
- saturatedByOneShots :: Int -> Demand -> Bool
- unboxDeeplyDmd :: Demand -> Demand
- type DmdEnv = VarEnv Demand
- emptyDmdEnv :: DmdEnv
- keepAliveDmdEnv :: DmdEnv -> IdSet -> DmdEnv
- reuseEnv :: DmdEnv -> DmdEnv
- data Divergence
- topDiv :: Divergence
- botDiv :: Divergence
- exnDiv :: Divergence
- lubDivergence :: Divergence -> Divergence -> Divergence
- isDeadEndDiv :: Divergence -> Bool
- data DmdType = DmdType {}
- dmdTypeDepth :: DmdType -> Arity
- nopDmdType :: DmdType
- botDmdType :: DmdType
- lubDmdType :: DmdType -> DmdType -> DmdType
- plusDmdType :: DmdType -> PlusDmdArg -> DmdType
- multDmdType :: Card -> DmdType -> DmdType
- type PlusDmdArg = (DmdEnv, Divergence)
- mkPlusDmdArg :: DmdEnv -> PlusDmdArg
- toPlusDmdArg :: DmdType -> PlusDmdArg
- peelFV :: DmdType -> Var -> (DmdType, Demand)
- findIdDemand :: DmdType -> Var -> Demand
- addDemand :: Demand -> DmdType -> DmdType
- splitDmdTy :: DmdType -> (Demand, DmdType)
- deferAfterPreciseException :: DmdType -> DmdType
- keepAliveDmdType :: DmdType -> VarSet -> DmdType
- newtype DmdSig = DmdSig DmdType
- mkDmdSigForArity :: Arity -> DmdType -> DmdSig
- mkClosedDmdSig :: [Demand] -> Divergence -> DmdSig
- splitDmdSig :: DmdSig -> ([Demand], Divergence)
- dmdSigDmdEnv :: DmdSig -> DmdEnv
- hasDemandEnvSig :: DmdSig -> Bool
- nopSig :: DmdSig
- botSig :: DmdSig
- isTopSig :: DmdSig -> Bool
- isDeadEndSig :: DmdSig -> Bool
- appIsDeadEnd :: DmdSig -> Int -> Bool
- trimBoxityDmdSig :: DmdSig -> DmdSig
- prependArgsDmdSig :: Int -> DmdSig -> DmdSig
- etaConvertDmdSig :: Arity -> DmdSig -> DmdSig
- type DmdTransformer = SubDemand -> DmdType
- dmdTransformSig :: DmdSig -> DmdTransformer
- dmdTransformDataConSig :: Arity -> DmdTransformer
- dmdTransformDictSelSig :: DmdSig -> DmdTransformer
- data TypeShape
- trimToType :: Demand -> TypeShape -> Demand
- trimBoxity :: Demand -> Demand
- seqDemand :: Demand -> ()
- seqDemandList :: [Demand] -> ()
- seqDmdType :: DmdType -> ()
- seqDmdSig :: DmdSig -> ()
- zapUsageDemand :: Demand -> Demand
- zapDmdEnvSig :: DmdSig -> DmdSig
- zapUsedOnceDemand :: Demand -> Demand
- zapUsedOnceSig :: DmdSig -> DmdSig
Demands
Instances
Eq Boxity Source # | |
Data Boxity Source # | |
Defined in GHC.Types.Basic gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Boxity -> c Boxity # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c Boxity # toConstr :: Boxity -> Constr # dataTypeOf :: Boxity -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c Boxity) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Boxity) # gmapT :: (forall b. Data b => b -> b) -> Boxity -> Boxity # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Boxity -> r # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Boxity -> r # gmapQ :: (forall d. Data d => d -> u) -> Boxity -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> Boxity -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> Boxity -> m Boxity # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Boxity -> m Boxity # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Boxity -> m Boxity # | |
Outputable Boxity Source # | |
Binary Boxity Source # | |
Describes an interval of evaluation cardinalities. See Note [Evaluation cardinalities] See Note [Bit vector representation for Card]
pattern C_00 :: Card | Absent, {0}. Pretty-printed as A. |
pattern C_01 :: Card | Used at most once, {0,1}. Pretty-printed as M. |
pattern C_0N :: Card | Every possible cardinality; the top element, {0,1,n}. Pretty-printed as L. |
pattern C_10 :: Card | Bottom, {}. Pretty-printed as A. |
pattern C_11 :: Card | Strict and used once, {1}. Pretty-printed as 1. |
pattern C_1N :: Card | Strict and used (possibly) many times, {1,n}. Pretty-printed as S. |
type CardNonAbs = Card Source #
type CardNonOnce = Card Source #
A demand describes a scaled evaluation context, e.g. how many times and how deep the denoted thing is evaluated.
The "how many" component is represented by a Card
inality.
The "how deep" component is represented by a SubDemand
.
Examples (using Note [Demand notation]):
seq
puts demand1A
on its first argument: It evaluates the argument strictly (1
), but not any deeper (A
).fst
puts demand1P(1L,A)
on its argument: It evaluates the argument pair strictly and the first component strictly, but no nested info beyond that (L
). Its second argument is not used at all.$
puts demand1C1(L)
on its first argument: It calls (C
) the argument function with one argument, exactly once (1
). No info on how the result of that call is evaluated (L
).maybe
puts demandMCM(L)
on its second argument: It evaluates the argument function at most once ((M)aybe) and calls it once when it is evaluated.fst p + fst p
puts demandSP(SL,A)
onp
: It's1P(1L,A)
multiplied by two, so we getS
(used at least once, possibly multiple times).
This data type is quite similar to
, but it's scaled
by Scaled
SubDemand
Card
, which is an interval on Multiplicity
, the upper bound of
which could be used to infer uniqueness types. Also we treat AbsDmd
and
BotDmd
specially, as the concept of a SubDemand
doesn't apply when there
isn't any evaluation at all. If you don't care, simply use (:*)
.
BotDmd | A bottoming demand, produced by a diverging function ( |
AbsDmd | An absent demand: Evaluated exactly 0 times ( |
pattern (:*) :: HasDebugCallStack => Card -> SubDemand -> Demand |
Matching on this pattern synonym is a complete match.
If the matched demand was Call sites should consider whether they really want to look at the
|
A sub-demand describes an evaluation context, e.g. how deep the
denoted thing is evaluated. See Demand
for examples.
The nested SubDemand
d
of a Call
Cn(d)
is relative to a single such call.
E.g. The expression f 1 2 + f 3 4
puts call demand SCS(C1(L))
on f
:
f
is called exactly twice (S
), each time exactly once (1
) with an
additional argument.
The nested Demand
s dn
of a Prod
P(d1,d2,...)
apply absolutely:
If dn
is a used once demand (cf. isUsedOnce
), then that means that
the denoted sub-expression is used once in the entire evaluation context
described by the surrounding Demand
. E.g., LP(ML)
means that the
field of the denoted expression is used at most once, although the
entire expression might be used many times.
See Note [Call demands are relative] and Note [Demand notation]. See also Note [Why Boxity in SubDemand and not in Demand?].
Poly !Boxity !CardNonOnce | Polymorphic demand, the denoted thing is evaluated arbitrarily deep,
with the specified cardinality at every level. The
In Note [Demand notation]: We'll only see |
Prod !Boxity ![Demand] |
|
mkProd :: Boxity -> [Demand] -> SubDemand Source #
A smart constructor for Prod
, applying rewrite rules along the semantic
equality Prod b [n :* Poly Boxed n, ...] === Poly b n
, simplifying to
Poly
SubDemand
s when possible. Examples:
- Rewrites
P(L,L)
(e.g., argumentsBoxed
,[L,L]
) toL
- Rewrites
!P(L!L,L!L)
(e.g., argumentsUnboxed
,[L!L,L!L]
) to!L
- Does not rewrite
P(1L)
,P(L!L)
,!P(L)
orP(L,A)
Algebra
Least upper bound
Plus
Multiply
Predicates on Card
inalities and Demand
s
isUsedOnceDmd :: Demand -> Bool Source #
Is the value used at most once?
isStrUsedDmd :: Demand -> Bool Source #
Not absent and used strictly. See Note [Strict demands]
isStrictDmd :: Demand -> Bool Source #
Contrast with isStrictUsedDmd. See Note [Strict demands]
isWeakDmd :: Demand -> Bool Source #
We try to avoid tracking weak free variable demands in strictness signatures for analysis performance reasons. See Note [Lazy and unleashable free variables] in GHC.Core.Opt.DmdAnal.
onlyBoxedArguments :: DmdSig -> Bool Source #
True when the signature indicates all arguments are boxed
Special demands
Demands used in PrimOp signatures
lazyApply1Dmd :: Demand Source #
First argument of catch#: MCM(L)
.
Evaluates its arg lazily, but then applies it exactly once to one argument.
lazyApply2Dmd :: Demand Source #
Second argument of catch#: MCM(C1(L))
.
Calls its arg lazily, but then applies it exactly once to an additional argument.
strictOnceApply1Dmd :: Demand Source #
First argument of 'GHC.Exts.maskAsyncExceptions#': 1C1(L)
.
Called exactly once.
strictManyApply1Dmd :: Demand Source #
First argument of 'GHC.Exts.atomically#': SCS(L)
.
Called at least once, possibly many times.
Other Demand
operations
oneifyCard :: Card -> Card Source #
Intersect with [0,1].
strictifyDictDmd :: Type -> Demand -> Demand Source #
If the argument is a used non-newtype dictionary, give it strict demand. Also split the product type & demand and recur in order to similarly strictify the argument's contained used non-newtype superclass dictionaries. We use the demand as our recursive measure to guarantee termination.
lazifyDmd :: Demand -> Demand Source #
Make a Demand
lazy, setting all lower bounds (outside Call
s) to 0.
peelCallDmd :: SubDemand -> (Card, SubDemand) Source #
Peels one call level from the sub-demand, and also returns how many times we entered the lambda body.
mkCalledOnceDmd :: SubDemand -> SubDemand Source #
Wraps the SubDemand
with a one-shot call demand: d
-> C1(d)
.
mkCalledOnceDmds :: Arity -> SubDemand -> SubDemand Source #
mkCalledOnceDmds n d
returns C1(C1...(C1 d))
where there are n
C1
's.
mkWorkerDemand :: Int -> Demand Source #
Extracting one-shot information
:: Demand | depending on saturation |
-> [OneShotInfo] |
See Note [Computing one-shot info]
argsOneShots :: DmdSig -> Arity -> [[OneShotInfo]] Source #
See Note [Computing one-shot info]
saturatedByOneShots :: Int -> Demand -> Bool Source #
saturatedByOneShots n CM(CM(...)) = True
=
There are at least n nested CM(..) calls.
See Note [Demand on the worker] in GHC.Core.Opt.WorkWrap
Manipulating Boxity of a Demand
unboxDeeplyDmd :: Demand -> Demand Source #
Demand environments
emptyDmdEnv :: DmdEnv Source #
keepAliveDmdEnv :: DmdEnv -> IdSet -> DmdEnv Source #
keepAliveDmdType dt vs
makes sure that the Ids in vs
have
some usage in the returned demand types -- they are not Absent.
See Note [Absence analysis for stable unfoldings and RULES]
in GHC.Core.Opt.DmdAnal.
Divergence
data Divergence Source #
Divergence
characterises whether something surely diverges.
Models a subset lattice of the following exhaustive set of divergence
results:
- n
- nontermination (e.g. loops)
- i
- throws imprecise exception
- p
- throws precise exceTtion
- c
- converges (reduces to WHNF).
The different lattice elements correspond to different subsets, indicated by juxtaposition of indicators (e.g. nc definitely doesn't throw an exception, and may or may not reduce to WHNF).
Dunno (nipc) | ExnOrDiv (nip) | Diverges (ni)
As you can see, we don't distinguish n and i. See Note [Precise exceptions and strictness analysis] for why p is so special compared to i.
Diverges | Definitely throws an imprecise exception or diverges. |
ExnOrDiv | Definitely throws a *precise* exception, an imprecise
exception or diverges. Never converges, hence |
Dunno | Might diverge, throw any kind of exception or converge. |
Instances
Eq Divergence Source # | |
Defined in GHC.Types.Demand (==) :: Divergence -> Divergence -> Bool # (/=) :: Divergence -> Divergence -> Bool # | |
Outputable Divergence Source # | |
Defined in GHC.Types.Demand ppr :: Divergence -> SDoc Source # | |
Binary Divergence Source # | |
Defined in GHC.Types.Demand put_ :: BinHandle -> Divergence -> IO () Source # put :: BinHandle -> Divergence -> IO (Bin Divergence) Source # |
topDiv :: Divergence Source #
botDiv :: Divergence Source #
exnDiv :: Divergence Source #
lubDivergence :: Divergence -> Divergence -> Divergence Source #
isDeadEndDiv :: Divergence -> Bool Source #
True if the Divergence
indicates that evaluation will not return.
See Note [Dead ends].
Demand types
Characterises how an expression
- Evaluates its free variables (
dt_env
) - Evaluates its arguments (
dt_args
) - Diverges on every code path or not (
dt_div
)
Equality is defined modulo defaultFvDmd
s in dt_env
.
See Note [Demand type Equality].
dmdTypeDepth :: DmdType -> Arity Source #
Algebra
nopDmdType :: DmdType Source #
The demand type of doing nothing (lazy, absent, no Divergence
information). Note that it is 'not'
the top of the lattice (which would be
"may use everything"), so it is (no longer) called topDmdType.
botDmdType :: DmdType Source #
lubDmdType :: DmdType -> DmdType -> DmdType Source #
Compute the least upper bound of two DmdType
s elicited /by the same
incoming demand/!
plusDmdType :: DmdType -> PlusDmdArg -> DmdType Source #
PlusDmdArg
type PlusDmdArg = (DmdEnv, Divergence) Source #
mkPlusDmdArg :: DmdEnv -> PlusDmdArg Source #
toPlusDmdArg :: DmdType -> PlusDmdArg Source #
Other operations
deferAfterPreciseException :: DmdType -> DmdType Source #
When e is evaluated after executing an IO action that may throw a precise
exception, we act as if there is an additional control flow path that is
taken if e throws a precise exception. The demand type of this control flow
path
* is lazy and absent (topDmd
) and boxed in all free variables and arguments
* has exnDiv
Divergence
result
See Note [Precise exceptions and strictness analysis]
So we can simply take a variant of nopDmdType
, exnDmdType
.
Why not nopDmdType
? Because then the result of e
can never be exnDiv
!
That means failure to drop dead-ends, see #18086.
keepAliveDmdType :: DmdType -> VarSet -> DmdType Source #
See keepAliveDmdEnv
.
Demand signatures
The depth of the wrapped DmdType
encodes the arity at which it is safe
to unleash. Better construct this through mkDmdSigForArity
.
See Note [Understanding DmdType and DmdSig]
mkClosedDmdSig :: [Demand] -> Divergence -> DmdSig Source #
splitDmdSig :: DmdSig -> ([Demand], Divergence) Source #
dmdSigDmdEnv :: DmdSig -> DmdEnv Source #
hasDemandEnvSig :: DmdSig -> Bool Source #
isDeadEndSig :: DmdSig -> Bool Source #
True if the signature diverges or throws an exception in a saturated call. See Note [Dead ends].
appIsDeadEnd :: DmdSig -> Int -> Bool Source #
Returns true if an application to n args would diverge or throw an exception.
If a function having botDiv
is applied to a less number of arguments than
its syntactic arity, we cannot say for sure that it is going to diverge.
Hence this function conservatively returns False in that case.
See Note [Dead ends].
trimBoxityDmdSig :: DmdSig -> DmdSig Source #
Handling arity adjustments
prependArgsDmdSig :: Int -> DmdSig -> DmdSig Source #
Add extra (topDmd
) arguments to a strictness signature.
In contrast to etaConvertDmdSig
, this prepends additional argument
demands. This is used by FloatOut.
etaConvertDmdSig :: Arity -> DmdSig -> DmdSig Source #
We are expanding (x y. e) to (x y z. e z) or reducing from the latter to
the former (when the Simplifier identifies a new join points, for example).
In contrast to prependArgsDmdSig
, this appends extra arg demands if
necessary.
This works by looking at the DmdType
(which was produced under a call
demand for the old arity) and trying to transfer as many facts as we can to
the call demand of new arity.
An arity increase (resulting in a stronger incoming demand) can retain much
of the info, while an arity decrease (a weakening of the incoming demand)
must fall back to a conservative default.
Demand transformers from demand signatures
type DmdTransformer = SubDemand -> DmdType Source #
A demand transformer is a monotone function from an incoming evaluation
context (SubDemand
) to a DmdType
, describing how the denoted thing
(i.e. expression, function) uses its arguments and free variables, and
whether it diverges.
See Note [Understanding DmdType and DmdSig] and Note [What are demand signatures?].
dmdTransformSig :: DmdSig -> DmdTransformer Source #
Extrapolate a demand signature (DmdSig
) into a DmdTransformer
.
Given a function's DmdSig
and a SubDemand
for the evaluation context,
return how the function evaluates its free variables and arguments.
dmdTransformDataConSig :: Arity -> DmdTransformer Source #
A special DmdTransformer
for data constructors that feeds product
demands into the constructor arguments.
dmdTransformDictSelSig :: DmdSig -> DmdTransformer Source #
A special DmdTransformer
for dictionary selectors that feeds the demand
on the result into the indicated dictionary component (if saturated).
See Note [Demand transformer for a dictionary selector].
Trim to a type shape
Instances
trimBoxity :: Demand -> Demand Source #
Drop all boxity
seq
ing stuff
seqDemandList :: [Demand] -> () Source #
seqDmdType :: DmdType -> () Source #
Zapping usage information
zapUsageDemand :: Demand -> Demand Source #
zapDmdEnvSig :: DmdSig -> DmdSig Source #
Remove the demand environment from the signature.
zapUsedOnceDemand :: Demand -> Demand Source #
Remove all `C_01 :*` info (but not CM
sub-demands) from the demand
zapUsedOnceSig :: DmdSig -> DmdSig Source #
Remove all `C_01 :*` info (but not CM
sub-demands) from the strictness
signature