Safe Haskell | Safe-Inferred |
---|---|
Language | GHC2021 |
This module defines the concept of a simplification rule for bindings. The intent is that you pass some context (such as symbol table) and a binding, and is given back a sequence of bindings that compute the same result, but are "better" in some sense.
These rewrite rules are "local", in that they do not maintain any
state or look at the program as a whole. Compare this to the
fusion algorithm in Futhark.Optimise.Fusion.Fusion
, which must be implemented
as its own pass.
Synopsis
- data RuleM rep a
- cannotSimplify :: RuleM rep a
- liftMaybe :: Maybe a -> RuleM rep a
- data Rule rep
- data SimplificationRule rep a
- = RuleGeneric (RuleGeneric rep a)
- | RuleBasicOp (RuleBasicOp rep a)
- | RuleMatch (RuleMatch rep a)
- | RuleLoop (RuleLoop rep a)
- | RuleOp (RuleOp rep a)
- type RuleGeneric rep a = a -> Stm rep -> Rule rep
- type RuleBasicOp rep a = a -> Pat (LetDec rep) -> StmAux (ExpDec rep) -> BasicOp -> Rule rep
- type RuleMatch rep a = a -> Pat (LetDec rep) -> StmAux (ExpDec rep) -> ([SubExp], [Case (Body rep)], Body rep, MatchDec (BranchType rep)) -> Rule rep
- type RuleLoop rep a = a -> Pat (LetDec rep) -> StmAux (ExpDec rep) -> ([(FParam rep, SubExp)], LoopForm, Body rep) -> Rule rep
- type TopDown rep = SymbolTable rep
- type TopDownRule rep = SimplificationRule rep (TopDown rep)
- type TopDownRuleGeneric rep = RuleGeneric rep (TopDown rep)
- type TopDownRuleBasicOp rep = RuleBasicOp rep (TopDown rep)
- type TopDownRuleMatch rep = RuleMatch rep (TopDown rep)
- type TopDownRuleLoop rep = RuleLoop rep (TopDown rep)
- type TopDownRuleOp rep = RuleOp rep (TopDown rep)
- type BottomUp rep = (SymbolTable rep, UsageTable)
- type BottomUpRule rep = SimplificationRule rep (BottomUp rep)
- type BottomUpRuleGeneric rep = RuleGeneric rep (BottomUp rep)
- type BottomUpRuleBasicOp rep = RuleBasicOp rep (BottomUp rep)
- type BottomUpRuleMatch rep = RuleMatch rep (BottomUp rep)
- type BottomUpRuleLoop rep = RuleLoop rep (BottomUp rep)
- type BottomUpRuleOp rep = RuleOp rep (BottomUp rep)
- data RuleBook rep
- ruleBook :: [TopDownRule m] -> [BottomUpRule m] -> RuleBook m
- topDownSimplifyStm :: (MonadFreshNames m, HasScope rep m, PrettyRep rep) => RuleBook rep -> SymbolTable rep -> Stm rep -> m (Maybe (Stms rep))
- bottomUpSimplifyStm :: (MonadFreshNames m, HasScope rep m, PrettyRep rep) => RuleBook rep -> (SymbolTable rep, UsageTable) -> Stm rep -> m (Maybe (Stms rep))
The rule monad
The monad in which simplification rules are evaluated.
Instances
cannotSimplify :: RuleM rep a Source #
Rule definition
An efficient way of encoding whether a simplification rule should even be attempted.
data SimplificationRule rep a Source #
A simplification rule takes some argument and a statement, and tries to simplify the statement.
RuleGeneric (RuleGeneric rep a) | |
RuleBasicOp (RuleBasicOp rep a) | |
RuleMatch (RuleMatch rep a) | |
RuleLoop (RuleLoop rep a) | |
RuleOp (RuleOp rep a) |
type RuleGeneric rep a = a -> Stm rep -> Rule rep Source #
type RuleBasicOp rep a = a -> Pat (LetDec rep) -> StmAux (ExpDec rep) -> BasicOp -> Rule rep Source #
type RuleMatch rep a = a -> Pat (LetDec rep) -> StmAux (ExpDec rep) -> ([SubExp], [Case (Body rep)], Body rep, MatchDec (BranchType rep)) -> Rule rep Source #
type RuleLoop rep a = a -> Pat (LetDec rep) -> StmAux (ExpDec rep) -> ([(FParam rep, SubExp)], LoopForm, Body rep) -> Rule rep Source #
Top-down rules
type TopDown rep = SymbolTable rep Source #
Context for a rule applied during top-down traversal of the program. Takes a symbol table as argument.
type TopDownRule rep = SimplificationRule rep (TopDown rep) Source #
type TopDownRuleGeneric rep = RuleGeneric rep (TopDown rep) Source #
type TopDownRuleBasicOp rep = RuleBasicOp rep (TopDown rep) Source #
type TopDownRuleMatch rep = RuleMatch rep (TopDown rep) Source #
type TopDownRuleLoop rep = RuleLoop rep (TopDown rep) Source #
type TopDownRuleOp rep = RuleOp rep (TopDown rep) Source #
Bottom-up rules
type BottomUp rep = (SymbolTable rep, UsageTable) Source #
Context for a rule applied during bottom-up traversal of the program. Takes a symbol table and usage table as arguments.
type BottomUpRule rep = SimplificationRule rep (BottomUp rep) Source #
type BottomUpRuleGeneric rep = RuleGeneric rep (BottomUp rep) Source #
type BottomUpRuleBasicOp rep = RuleBasicOp rep (BottomUp rep) Source #
type BottomUpRuleMatch rep = RuleMatch rep (BottomUp rep) Source #
type BottomUpRuleLoop rep = RuleLoop rep (BottomUp rep) Source #
type BottomUpRuleOp rep = RuleOp rep (BottomUp rep) Source #
Assembling rules
A collection of both top-down and bottom-up rules.
ruleBook :: [TopDownRule m] -> [BottomUpRule m] -> RuleBook m Source #
Construct a rule book from a collection of rules.
Applying rules
topDownSimplifyStm :: (MonadFreshNames m, HasScope rep m, PrettyRep rep) => RuleBook rep -> SymbolTable rep -> Stm rep -> m (Maybe (Stms rep)) Source #
simplifyStm lookup stm
performs simplification of the
binding stm
. If simplification is possible, a replacement list
of bindings is returned, that bind at least the same names as the
original binding (and possibly more, for intermediate results).
bottomUpSimplifyStm :: (MonadFreshNames m, HasScope rep m, PrettyRep rep) => RuleBook rep -> (SymbolTable rep, UsageTable) -> Stm rep -> m (Maybe (Stms rep)) Source #
simplifyStm uses stm
performs simplification of the binding
stm
. If simplification is possible, a replacement list of
bindings is returned, that bind at least the same names as the
original binding (and possibly more, for intermediate results).
The first argument is the set of names used after this binding.