| Copyright | (C) 2012-2016 University of Twente 2016 Myrtle Software Ltd 2017 Google Inc. 2021-2026 QBayLogic B.V. |
|---|---|
| License | BSD2 (see the file LICENSE) |
| Maintainer | QBayLogic B.V. <devops@qbaylogic.com> |
| Safe Haskell | Safe-Inferred |
| Language | Haskell2010 |
Clash.Rewrite.Util
Description
Utilities for rewriting: e.g. inlining, specialisation, etc.
Synopsis
- apply :: String -> Rewrite extra -> Rewrite extra
- isUntranslatableType :: Bool -> Type -> RewriteMonad extra Bool
- runRewriteSession :: RewriteEnv -> RewriteState extra -> RewriteMonad extra a -> IO a
- changed :: a -> RewriteMonad extra a
- isUntranslatable :: Bool -> Term -> RewriteMonad extra Bool
- mkAltName :: TransformContext -> Pat -> TmName
- mkArgName :: TransformContext -> Term -> Int -> RewriteMonad extra TmName
- mkDerivedName :: TransformContext -> OccName -> TmName
- mkTmBinderFor :: MonadUnique m => InScopeSet -> TyConMap -> Name a -> Term -> m Id
- isFromInt :: Text -> Bool
- runWithHWTypeCache :: State HWMap a -> RewriteMonad extra a
- whnfRW :: Bool -> TransformContext -> Term -> Rewrite extra -> RewriteMonad extra Term
- inlineBinders :: (Term -> LetBinding -> RewriteMonad extra Bool) -> TransformContext -> Term -> Bind Term -> Term -> RewriteMonad extra Term
- inlineOrLiftBinders :: (LetBinding -> RewriteMonad extra Bool) -> (Term -> LetBinding -> Bool) -> TransformContext -> Term -> Bind Term -> Term -> RewriteMonad extra Term
- isJoinPointIn :: Id -> Term -> Bool
- isVoidWrapper :: Term -> Bool
- zoomExtra :: State extra a -> RewriteMonad extra a
- removeUnusedBinders :: Bind Term -> Term -> Maybe Term
- setChanged :: RewriteMonad extra ()
- mkBinderFor :: MonadUnique m => InScopeSet -> TyConMap -> Name a -> Either Term Type -> m (Either Id TyVar)
- mkFunction :: TmName -> SrcSpan -> InlineSpec -> Term -> RewriteMonad extra Id
- normalizeTermTypes :: TyConMap -> Term -> Term
- normalizeId :: TyConMap -> Id -> Id
- cloneNameWithBindingMap :: MonadUnique m => BindingMap -> Name a -> m (Name a)
- runRewrite :: String -> InScopeSet -> Rewrite extra -> Term -> RewriteMonad extra Term
- applyWith :: String -> TransformContext -> Term -> RewriteMonad extra Term -> RewriteMonad extra (Term, Bool)
- applyDebug :: TransformContext -> String -> Term -> Bool -> Term -> RewriteMonad extra Term
- findAccidentialShadows :: Term -> [[Id]]
- bumpCounter :: String -> Lens' (RewriteState extra) (HashMap Text Word) -> RewriteMonad extra ()
- closestLetBinder :: Context -> Maybe Id
- cloneNameWithInScopeSet :: MonadUnique m => InScopeSet -> Name a -> m (Name a)
- substituteBinders :: InScopeSet -> [LetBinding] -> [LetBinding] -> Term -> ([LetBinding], ([LetBinding], Term))
- tailCalls :: Id -> Term -> Maybe Int
- liftAndSubsituteBinders :: InScopeSet -> [LetBinding] -> [LetBinding] -> Term -> RewriteMonad extra ([LetBinding], Term)
- liftBinding :: LetBinding -> RewriteMonad extra LetBinding
- addGlobalBind :: TmName -> Type -> SrcSpan -> InlineSpec -> Term -> RewriteMonad extra ()
- bindPureHeap :: TyConMap -> PureHeap -> Rewrite extra -> Rewrite extra
- module Clash.Rewrite.WorkFree
Documentation
Arguments
| :: String | Name of the transformation |
| -> Rewrite extra | Transformation to be applied |
| -> Rewrite extra |
Record if a transformation is successfully applied
Arguments
| :: Bool | String representable |
| -> Type | |
| -> RewriteMonad extra Bool |
Determine if a type cannot be represented in hardware
runRewriteSession :: RewriteEnv -> RewriteState extra -> RewriteMonad extra a -> IO a Source #
Evaluate a RewriteSession to its inner monad.
changed :: a -> RewriteMonad extra a Source #
Identity function that additionally notifies that a transformation has changed the expression
Arguments
| :: Bool | String representable |
| -> Term | |
| -> RewriteMonad extra Bool |
Determine if a term cannot be represented in hardware
mkAltName :: TransformContext -> Pat -> TmName Source #
Name for a new binder for the right-hand side of a case alternative. Includes the constructor matched by the alternative. For example, in:
x = case s of Idle -> ... _ -> ...
the right-hand sides are named c$x_case_Idle and c$x_case_default. Uses
case_alt for literal patterns and constructors that are operators.
Arguments
| :: TransformContext | |
| -> Term | Function part of the application |
| -> Int | Index of the argument among the term arguments of the application |
| -> RewriteMonad extra TmName |
Name for a new binder for an argument of an application. Prefers names derived from what the argument is passed to:
- The record field name, for arguments of a data constructor, e.g.
c$baudCountforninTx { txState = st, baudCount = n } - The parameter name, for arguments of a global function, e.g.
c$txStep_sforstintxStep st i, giventxStep s input = ...
Falls back to mkDerivedName with suffix app_arg.
mkDerivedName :: TransformContext -> OccName -> TmName Source #
Arguments
| :: MonadUnique m | |
| => InScopeSet | |
| -> TyConMap | TyCon cache |
| -> Name a | Name of the new binder |
| -> Term | Term to bind |
| -> m Id |
Make a new binder and variable reference for a term
runWithHWTypeCache :: State HWMap a -> RewriteMonad extra a Source #
Run a Core-type to HWType translation action against the session-wide
translation cache (hwTypeCache).
Arguments
| :: Bool | Whether the expression we're reducing to WHNF is the subject of a case expression. |
| -> TransformContext | |
| -> Term | |
| -> Rewrite extra | |
| -> RewriteMonad extra Term |
Evaluate an expression to weak-head normal form (WHNF), and apply a transformation on the expression in WHNF.
Arguments
| :: (Term -> LetBinding -> RewriteMonad extra Bool) | Property test |
| -> TransformContext | |
| -> Term | The let-expression |
| -> Bind Term | |
| -> Term | |
| -> RewriteMonad extra Term |
Inline the binders in a let-binding that have a certain property. This is
a onLet handler: it receives the let-expression
itself, its bindings, and its body.
Arguments
| :: (LetBinding -> RewriteMonad extra Bool) | Property test |
| -> (Term -> LetBinding -> Bool) | Test whether to lift or inline
|
| -> TransformContext | |
| -> Term | The let-expression |
| -> Bind Term | |
| -> Term | |
| -> RewriteMonad extra Term |
Inline or lift the binders in a let-binding that have a certain property.
This is a onLet handler: it receives the
let-expression itself, its bindings, and its body.
Determine whether a binder is a join-point created for a complex case expression.
A join-point is when a local function only occurs in tail-call positions, and when it does, more than once.
isVoidWrapper :: Term -> Bool Source #
Determines whether a function has the following shape:
\(w :: Void) -> f a b c
i.e. is a wrapper around a (partially) applied function f, where the
introduced argument w is not used by f
zoomExtra :: State extra a -> RewriteMonad extra a Source #
Lift an action working in the _extra state to the RewriteMonad
removeUnusedBinders :: Bind Term -> Term -> Maybe Term Source #
Remove unused binders in given let-binding. Returns Nothing if no unused binders were found.
setChanged :: RewriteMonad extra () Source #
Notify that a transformation has changed the expression
Arguments
| :: MonadUnique m | |
| => InScopeSet | |
| -> TyConMap | TyCon cache |
| -> Name a | Name of the new binder |
| -> Either Term Type | Type or Term to bind |
| -> m (Either Id TyVar) |
Make a new binder and variable reference for either a term or a type
Arguments
| :: TmName | Name of the function |
| -> SrcSpan | |
| -> InlineSpec | |
| -> Term | Term bound to the function |
| -> RewriteMonad extra Id | Name with a proper unique and the type of the function |
Make a global function for a name-term tuple
cloneNameWithBindingMap :: MonadUnique m => BindingMap -> Name a -> m (Name a) Source #
Create a new name out of the given name, but with another unique. Resulting unique is guaranteed to not be in the given BindingMap.
Arguments
| :: String | Name of the transformation |
| -> InScopeSet | |
| -> Rewrite extra | Transformation to perform |
| -> Term | Term to transform |
| -> RewriteMonad extra Term |
Perform a transformation on a Term
Arguments
| :: String | Name of the transformation |
| -> TransformContext | |
| -> Term | The node the transformation was offered |
| -> RewriteMonad extra Term | The transformation, applied to that node |
| -> RewriteMonad extra (Term, Bool) |
The instrumentation core of apply (debug tracing, transformation
counter, rewrite history, invariant checks) around an already-applied
transformation action. Also returns whether the action signaled change.
Arguments
| :: TransformContext | |
| -> String | Name of the transformation |
| -> Term | Original expression |
| -> Bool | Whether the rewrite indicated change |
| -> Term | New expression |
| -> RewriteMonad extra Term |
findAccidentialShadows :: Term -> [[Id]] Source #
Some transformations might erroneously introduce shadowing. For example, a transformation might result in:
let a = ... b = ... a = ...
where the last a, shadows the first, while Clash assumes that this can't
happen. This function finds those constructs and a list of found duplicates.
bumpCounter :: String -> Lens' (RewriteState extra) (HashMap Text Word) -> RewriteMonad extra () Source #
Strictly bump a counter in RewriteState, typically transformTriedCounters
or transformAppliedCounters.
cloneNameWithInScopeSet :: MonadUnique m => InScopeSet -> Name a -> m (Name a) Source #
Create a new name out of the given name, but with another unique. Resulting unique is guaranteed to not be in the given InScopeSet.
Arguments
| :: InScopeSet | |
| -> [LetBinding] | Let-binders to substitute |
| -> [LetBinding] | Let-binders where substitution takes place |
| -> Term | Body where substitution takes place |
| -> ([LetBinding], ([LetBinding], Term)) |
|
Inline the first set of binder into the second set of binders and into the body of the original let expression.
Count the number of (only) tail calls of a function in an expression.
Nothing indicates that the function was used in a non-tail call position.
liftAndSubsituteBinders Source #
Arguments
| :: InScopeSet | |
| -> [LetBinding] | Let-binders to lift, and substitute the lifted result |
| -> [LetBinding] | Lef-binders where substitution takes place |
| -> Term | Body where substitution takes place |
| -> RewriteMonad extra ([LetBinding], Term) |
Lift the first set of binders to the level of global bindings, and substitute these lifted bindings into the second set of binders and the body of the original let expression.
liftBinding :: LetBinding -> RewriteMonad extra LetBinding Source #
Create a global function for a Let-binding and return a Let-binding where the RHS is a reference to the new global function applied to the free variables of the original RHS
addGlobalBind :: TmName -> Type -> SrcSpan -> InlineSpec -> Term -> RewriteMonad extra () Source #
Add a function to the set of global binders
bindPureHeap :: TyConMap -> PureHeap -> Rewrite extra -> Rewrite extra Source #
Binds variables on the PureHeap over the result of the rewrite
To prevent unnecessary rewrites only do this when rewrite changed something.
module Clash.Rewrite.WorkFree