-- | Loop simplification rules.
module Futhark.Optimise.Simplify.Rules.Loop (loopRules) where

import Control.Monad
import Data.Bifunctor (second)
import Data.List (partition)
import Data.Maybe
import Futhark.Analysis.DataDependencies
import Futhark.Analysis.PrimExp.Convert
import Futhark.Analysis.SymbolTable qualified as ST
import Futhark.Analysis.UsageTable qualified as UT
import Futhark.Construct
import Futhark.IR
import Futhark.Optimise.Simplify.Rule
import Futhark.Optimise.Simplify.Rules.ClosedForm
import Futhark.Transform.Rename

-- This next one is tricky - it's easy enough to determine that some
-- loop result is not used after the loop, but here, we must also make
-- sure that it does not affect any other values.
--
-- I do not claim that the current implementation of this rule is
-- perfect, but it should suffice for many cases, and should never
-- generate wrong code.
removeRedundantMergeVariables :: (BuilderOps rep) => BottomUpRuleLoop rep
removeRedundantMergeVariables :: forall rep. BuilderOps rep => BottomUpRuleLoop rep
removeRedundantMergeVariables (SymbolTable rep
_, UsageTable
used) Pat (LetDec rep)
pat StmAux (ExpDec rep)
aux ([(Param (FParamInfo rep), SubExp)]
merge, LoopForm
form, Body rep
body)
  | Bool -> Bool
not forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (forall {dec}. Param dec -> Bool
usedAfterLoop forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> a
fst) [(Param (FParamInfo rep), SubExp)]
merge =
      let necessaryForReturned :: Names
necessaryForReturned =
            forall dec.
(Param dec -> Bool)
-> [(Param dec, SubExp)] -> Map VName Names -> Names
findNecessaryForReturned
              forall {dec}. Param dec -> Bool
usedAfterLoopOrInForm
              (forall a b. [a] -> [b] -> [(a, b)]
zip (forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst [(Param (FParamInfo rep), SubExp)]
merge) (forall a b. (a -> b) -> [a] -> [b]
map SubExpRes -> SubExp
resSubExp forall a b. (a -> b) -> a -> b
$ forall rep. Body rep -> Result
bodyResult Body rep
body))
              (forall rep. ASTRep rep => Body rep -> Map VName Names
dataDependencies Body rep
body)

          resIsNecessary :: ((Param dec, b), b) -> Bool
resIsNecessary ((Param dec
v, b
_), b
_) =
            forall {dec}. Param dec -> Bool
usedAfterLoop Param dec
v
              Bool -> Bool -> Bool
|| (forall dec. Param dec -> VName
paramName Param dec
v VName -> Names -> Bool
`nameIn` Names
necessaryForReturned)
              Bool -> Bool -> Bool
|| forall {dec}. Param dec -> Bool
referencedInPat Param dec
v
              Bool -> Bool -> Bool
|| forall {dec}. Param dec -> Bool
referencedInForm Param dec
v

          ([(PatElem (LetDec rep),
  ((Param (FParamInfo rep), SubExp), SubExpRes))]
keep_valpart, [(PatElem (LetDec rep),
  ((Param (FParamInfo rep), SubExp), SubExpRes))]
discard_valpart) =
            forall a. (a -> Bool) -> [a] -> ([a], [a])
partition (forall {dec} {b} {b}. ((Param dec, b), b) -> Bool
resIsNecessary forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> b
snd) forall a b. (a -> b) -> a -> b
$
              forall a b. [a] -> [b] -> [(a, b)]
zip (forall dec. Pat dec -> [PatElem dec]
patElems Pat (LetDec rep)
pat) forall a b. (a -> b) -> a -> b
$
                forall a b. [a] -> [b] -> [(a, b)]
zip [(Param (FParamInfo rep), SubExp)]
merge forall a b. (a -> b) -> a -> b
$
                  forall rep. Body rep -> Result
bodyResult Body rep
body

          ([PatElem (LetDec rep)]
keep_valpatelems, [((Param (FParamInfo rep), SubExp), SubExpRes)]
keep_val) = forall a b. [(a, b)] -> ([a], [b])
unzip [(PatElem (LetDec rep),
  ((Param (FParamInfo rep), SubExp), SubExpRes))]
keep_valpart
          ([PatElem (LetDec rep)]
_discard_valpatelems, [((Param (FParamInfo rep), SubExp), SubExpRes)]
discard_val) = forall a b. [(a, b)] -> ([a], [b])
unzip [(PatElem (LetDec rep),
  ((Param (FParamInfo rep), SubExp), SubExpRes))]
discard_valpart
          ([(Param (FParamInfo rep), SubExp)]
merge', Result
val_es') = forall a b. [(a, b)] -> ([a], [b])
unzip [((Param (FParamInfo rep), SubExp), SubExpRes)]
keep_val

          body' :: Body rep
body' = Body rep
body {bodyResult :: Result
bodyResult = Result
val_es'}

          pat' :: Pat (LetDec rep)
pat' = forall dec. [PatElem dec] -> Pat dec
Pat [PatElem (LetDec rep)]
keep_valpatelems
       in if [(Param (FParamInfo rep), SubExp)]
merge' forall a. Eq a => a -> a -> Bool
== [(Param (FParamInfo rep), SubExp)]
merge
            then forall rep. Rule rep
Skip
            else forall rep. RuleM rep () -> Rule rep
Simplify forall a b. (a -> b) -> a -> b
$ do
              -- We can't just remove the bindings in 'discard', since the loop
              -- body may still use their names in (now-dead) expressions.
              -- Hence, we add them inside the loop, fully aware that dead-code
              -- removal will eventually get rid of them.  Some care is
              -- necessary to handle unique bindings.
              Body rep
body'' <- forall (m :: * -> *).
MonadBuilder m =>
m (Body (Rep m)) -> m (Body (Rep m))
insertStmsM forall a b. (a -> b) -> a -> b
$ do
                forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry forall (m :: * -> *).
MonadBuilder m =>
[VName] -> Exp (Rep m) -> m ()
letBindNames) forall a b. (a -> b) -> a -> b
$ forall {b} {rep}.
[((Param (FParamInfo rep), SubExp), b)] -> [([VName], Exp rep)]
dummyStms [((Param (FParamInfo rep), SubExp), SubExpRes)]
discard_val
                forall (f :: * -> *) a. Applicative f => a -> f a
pure Body rep
body'
              forall (m :: * -> *) anyrep a.
MonadBuilder m =>
StmAux anyrep -> m a -> m a
auxing StmAux (ExpDec rep)
aux forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *).
MonadBuilder m =>
Pat (LetDec (Rep m)) -> Exp (Rep m) -> m ()
letBind Pat (LetDec rep)
pat' forall a b. (a -> b) -> a -> b
$ forall rep.
[(FParam rep, SubExp)] -> LoopForm -> Body rep -> Exp rep
Loop [(Param (FParamInfo rep), SubExp)]
merge' LoopForm
form Body rep
body''
  where
    pat_used :: [Bool]
pat_used = forall a b. (a -> b) -> [a] -> [b]
map (VName -> UsageTable -> Bool
`UT.isUsedDirectly` UsageTable
used) forall a b. (a -> b) -> a -> b
$ forall dec. Pat dec -> [VName]
patNames Pat (LetDec rep)
pat
    used_vals :: [VName]
used_vals = forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst forall a b. (a -> b) -> a -> b
$ forall a. (a -> Bool) -> [a] -> [a]
filter forall a b. (a, b) -> b
snd forall a b. (a -> b) -> a -> b
$ forall a b. [a] -> [b] -> [(a, b)]
zip (forall a b. (a -> b) -> [a] -> [b]
map (forall dec. Param dec -> VName
paramName forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> a
fst) [(Param (FParamInfo rep), SubExp)]
merge) [Bool]
pat_used
    usedAfterLoop :: Param dec -> Bool
usedAfterLoop = forall a b c. (a -> b -> c) -> b -> a -> c
flip forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
elem [VName]
used_vals forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall dec. Param dec -> VName
paramName
    usedAfterLoopOrInForm :: Param dec -> Bool
usedAfterLoopOrInForm Param dec
p =
      forall {dec}. Param dec -> Bool
usedAfterLoop Param dec
p Bool -> Bool -> Bool
|| forall dec. Param dec -> VName
paramName Param dec
p VName -> Names -> Bool
`nameIn` forall a. FreeIn a => a -> Names
freeIn LoopForm
form
    patAnnotNames :: Names
patAnnotNames = forall a. FreeIn a => a -> Names
freeIn forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst [(Param (FParamInfo rep), SubExp)]
merge
    referencedInPat :: Param dec -> Bool
referencedInPat = (VName -> Names -> Bool
`nameIn` Names
patAnnotNames) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall dec. Param dec -> VName
paramName
    referencedInForm :: Param dec -> Bool
referencedInForm = (VName -> Names -> Bool
`nameIn` forall a. FreeIn a => a -> Names
freeIn LoopForm
form) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall dec. Param dec -> VName
paramName

    dummyStms :: [((Param (FParamInfo rep), SubExp), b)] -> [([VName], Exp rep)]
dummyStms = forall a b. (a -> b) -> [a] -> [b]
map forall {dec} {b} {rep}.
DeclTyped dec =>
((Param dec, SubExp), b) -> ([VName], Exp rep)
dummyStm
    dummyStm :: ((Param dec, SubExp), b) -> ([VName], Exp rep)
dummyStm ((Param dec
p, SubExp
e), b
_)
      | forall shape. TypeBase shape Uniqueness -> Bool
unique (forall dec. DeclTyped dec => Param dec -> DeclType
paramDeclType Param dec
p),
        Var VName
v <- SubExp
e =
          ([forall dec. Param dec -> VName
paramName Param dec
p], forall rep. BasicOp -> Exp rep
BasicOp forall a b. (a -> b) -> a -> b
$ Shape -> SubExp -> BasicOp
Replicate forall a. Monoid a => a
mempty forall a b. (a -> b) -> a -> b
$ VName -> SubExp
Var VName
v)
      | Bool
otherwise = ([forall dec. Param dec -> VName
paramName Param dec
p], forall rep. BasicOp -> Exp rep
BasicOp forall a b. (a -> b) -> a -> b
$ SubExp -> BasicOp
SubExp SubExp
e)
removeRedundantMergeVariables (SymbolTable rep, UsageTable)
_ Pat (LetDec rep)
_ StmAux (ExpDec rep)
_ ([(Param (FParamInfo rep), SubExp)], LoopForm, Body rep)
_ =
  forall rep. Rule rep
Skip

-- We may change the type of the loop if we hoist out a shape
-- annotation, in which case we also need to tweak the bound pattern.
hoistLoopInvariantMergeVariables :: (BuilderOps rep) => TopDownRuleLoop rep
hoistLoopInvariantMergeVariables :: forall rep. BuilderOps rep => TopDownRuleLoop rep
hoistLoopInvariantMergeVariables TopDown rep
vtable Pat (LetDec rep)
pat StmAux (ExpDec rep)
aux ([(FParam rep, SubExp)]
merge, LoopForm
form, Body rep
loopbody) = do
  -- Figure out which of the elements of loopresult are
  -- loop-invariant, and hoist them out.
  let explpat :: [(PatElem (LetDec rep), VName)]
explpat = forall a b. [a] -> [b] -> [(a, b)]
zip (forall dec. Pat dec -> [PatElem dec]
patElems Pat (LetDec rep)
pat) forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (forall dec. Param dec -> VName
paramName forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> a
fst) [(FParam rep, SubExp)]
merge
  case forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr forall {dec} {dec}.
(FreeIn dec, Typed dec, Typed dec) =>
(VName, (Param dec, SubExp), SubExpRes)
-> ([(Ident, (SubExp, Certs))], [(PatElem dec, VName)],
    [(Param dec, SubExp)], Result)
-> ([(Ident, (SubExp, Certs))], [(PatElem dec, VName)],
    [(Param dec, SubExp)], Result)
checkInvariance ([], [(PatElem (LetDec rep), VName)]
explpat, [], []) forall a b. (a -> b) -> a -> b
$
    forall a b c. [a] -> [b] -> [c] -> [(a, b, c)]
zip3 (forall dec. Pat dec -> [VName]
patNames Pat (LetDec rep)
pat) [(FParam rep, SubExp)]
merge Result
res of
    ([], [(PatElem (LetDec rep), VName)]
_, [(FParam rep, SubExp)]
_, Result
_) ->
      -- Nothing is invariant.
      forall rep. Rule rep
Skip
    ([(Ident, (SubExp, Certs))]
invariant, [(PatElem (LetDec rep), VName)]
explpat', [(FParam rep, SubExp)]
merge', Result
res') -> forall rep. RuleM rep () -> Rule rep
Simplify forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) anyrep a.
MonadBuilder m =>
StmAux anyrep -> m a -> m a
auxing StmAux (ExpDec rep)
aux forall a b. (a -> b) -> a -> b
$ do
      -- We have moved something invariant out of the loop.
      let loopbody' :: Body rep
loopbody' = Body rep
loopbody {bodyResult :: Result
bodyResult = Result
res'}
          explpat'' :: [PatElem (LetDec rep)]
explpat'' = forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst [(PatElem (LetDec rep), VName)]
explpat'
      forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [(Ident, (SubExp, Certs))]
invariant forall a b. (a -> b) -> a -> b
$ \(Ident
v1, (SubExp
v2, Certs
cs)) ->
        forall (m :: * -> *) a. MonadBuilder m => Certs -> m a -> m a
certifying Certs
cs forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *).
MonadBuilder m =>
[VName] -> Exp (Rep m) -> m ()
letBindNames [Ident -> VName
identName Ident
v1] forall a b. (a -> b) -> a -> b
$ forall rep. BasicOp -> Exp rep
BasicOp forall a b. (a -> b) -> a -> b
$ SubExp -> BasicOp
SubExp SubExp
v2
      forall (m :: * -> *).
MonadBuilder m =>
Pat (LetDec (Rep m)) -> Exp (Rep m) -> m ()
letBind (forall dec. [PatElem dec] -> Pat dec
Pat [PatElem (LetDec rep)]
explpat'') forall a b. (a -> b) -> a -> b
$ forall rep.
[(FParam rep, SubExp)] -> LoopForm -> Body rep -> Exp rep
Loop [(FParam rep, SubExp)]
merge' LoopForm
form Body rep
loopbody'
  where
    res :: Result
res = forall rep. Body rep -> Result
bodyResult Body rep
loopbody

    namesOfMergeParams :: Names
namesOfMergeParams = [VName] -> Names
namesFromList forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (forall dec. Param dec -> VName
paramName forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> a
fst) [(FParam rep, SubExp)]
merge

    removeFromResult :: b
-> (Param dec, a)
-> [(PatElem dec, VName)]
-> (Maybe (Ident, (a, b)), [(PatElem dec, VName)])
removeFromResult b
cs (Param dec
mergeParam, a
mergeInit) [(PatElem dec, VName)]
explpat' =
      case forall a. (a -> Bool) -> [a] -> ([a], [a])
partition ((forall a. Eq a => a -> a -> Bool
== forall dec. Param dec -> VName
paramName Param dec
mergeParam) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> b
snd) [(PatElem dec, VName)]
explpat' of
        ([(PatElem dec
patelem, VName
_)], [(PatElem dec, VName)]
rest) ->
          (forall a. a -> Maybe a
Just (forall dec. Typed dec => PatElem dec -> Ident
patElemIdent PatElem dec
patelem, (a
mergeInit, b
cs)), [(PatElem dec, VName)]
rest)
        ([(PatElem dec, VName)]
_, [(PatElem dec, VName)]
_) ->
          (forall a. Maybe a
Nothing, [(PatElem dec, VName)]
explpat')

    checkInvariance :: (VName, (Param dec, SubExp), SubExpRes)
-> ([(Ident, (SubExp, Certs))], [(PatElem dec, VName)],
    [(Param dec, SubExp)], Result)
-> ([(Ident, (SubExp, Certs))], [(PatElem dec, VName)],
    [(Param dec, SubExp)], Result)
checkInvariance
      (VName
pat_name, (Param dec
mergeParam, SubExp
mergeInit), SubExpRes
resExp)
      ([(Ident, (SubExp, Certs))]
invariant, [(PatElem dec, VName)]
explpat', [(Param dec, SubExp)]
merge', Result
resExps)
        | Bool
isInvariant,
          -- Certificates must be available.
          forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (forall rep. VName -> SymbolTable rep -> Bool
`ST.elem` TopDown rep
vtable) forall a b. (a -> b) -> a -> b
$ Certs -> [VName]
unCerts forall a b. (a -> b) -> a -> b
$ SubExpRes -> Certs
resCerts SubExpRes
resExp =
            let (Maybe (Ident, (SubExp, Certs))
stm, [(PatElem dec, VName)]
explpat'') =
                  forall {dec} {b} {dec} {a}.
Typed dec =>
b
-> (Param dec, a)
-> [(PatElem dec, VName)]
-> (Maybe (Ident, (a, b)), [(PatElem dec, VName)])
removeFromResult
                    (SubExpRes -> Certs
resCerts SubExpRes
resExp)
                    (Param dec
mergeParam, SubExp
mergeInit)
                    [(PatElem dec, VName)]
explpat'
             in ( forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. a -> a
id (:) Maybe (Ident, (SubExp, Certs))
stm forall a b. (a -> b) -> a -> b
$
                    (forall dec. Typed dec => Param dec -> Ident
paramIdent Param dec
mergeParam, (SubExp
mergeInit, SubExpRes -> Certs
resCerts SubExpRes
resExp)) forall a. a -> [a] -> [a]
: [(Ident, (SubExp, Certs))]
invariant,
                  [(PatElem dec, VName)]
explpat'',
                  [(Param dec, SubExp)]
merge',
                  Result
resExps
                )
        where
          -- A non-unique merge variable is invariant if one of the
          -- following is true:
          isInvariant :: Bool
isInvariant
            -- (0) The result is a variable of the same name as the
            -- parameter, where all existential parameters are already
            -- known to be invariant
            | Var VName
v2 <- SubExpRes -> SubExp
resSubExp SubExpRes
resExp,
              forall dec. Param dec -> VName
paramName Param dec
mergeParam forall a. Eq a => a -> a -> Bool
== VName
v2 =
                forall {dec}. FreeIn dec => Names -> Param dec -> Bool
allExistentialInvariant
                  ([VName] -> Names
namesFromList forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (Ident -> VName
identName forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> a
fst) [(Ident, (SubExp, Certs))]
invariant)
                  Param dec
mergeParam
            -- (1) The result is identical to the initial parameter value.
            | SubExp
mergeInit forall a. Eq a => a -> a -> Bool
== SubExpRes -> SubExp
resSubExp SubExpRes
resExp = Bool
True
            -- (2) The initial parameter value is equal to an outer
            -- loop parameter 'P', where the initial value of 'P' is
            -- equal to 'resExp', AND 'resExp' ultimately becomes the
            -- new value of 'P'.  XXX: it's a bit clumsy that this
            -- only works for one level of nesting, and I think it
            -- would not be too hard to generalise.
            | Var VName
init_v <- SubExp
mergeInit,
              Just (SubExp
p_init, SubExp
p_res) <- forall rep. VName -> SymbolTable rep -> Maybe (SubExp, SubExp)
ST.lookupLoopParam VName
init_v TopDown rep
vtable,
              SubExp
p_init forall a. Eq a => a -> a -> Bool
== SubExpRes -> SubExp
resSubExp SubExpRes
resExp,
              SubExp
p_res forall a. Eq a => a -> a -> Bool
== VName -> SubExp
Var VName
pat_name =
                Bool
True
            -- (3) It is a statically empty array.
            | forall a. Maybe a -> Bool
isJust forall a b. (a -> b) -> a -> b
$ Type -> Maybe (PrimType, Shape)
isEmptyArray (forall dec. Typed dec => Param dec -> Type
paramType Param dec
mergeParam) = Bool
True
            | Bool
otherwise = Bool
False
    checkInvariance
      (VName
_pat_name, (Param dec
mergeParam, SubExp
mergeInit), SubExpRes
resExp)
      ([(Ident, (SubExp, Certs))]
invariant, [(PatElem dec, VName)]
explpat', [(Param dec, SubExp)]
merge', Result
resExps) =
        ([(Ident, (SubExp, Certs))]
invariant, [(PatElem dec, VName)]
explpat', (Param dec
mergeParam, SubExp
mergeInit) forall a. a -> [a] -> [a]
: [(Param dec, SubExp)]
merge', SubExpRes
resExp forall a. a -> [a] -> [a]
: Result
resExps)

    allExistentialInvariant :: Names -> Param dec -> Bool
allExistentialInvariant Names
namesOfInvariant Param dec
mergeParam =
      forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (Names -> VName -> Bool
invariantOrNotMergeParam Names
namesOfInvariant) forall a b. (a -> b) -> a -> b
$
        Names -> [VName]
namesToList forall a b. (a -> b) -> a -> b
$
          forall a. FreeIn a => a -> Names
freeIn Param dec
mergeParam Names -> Names -> Names
`namesSubtract` VName -> Names
oneName (forall dec. Param dec -> VName
paramName Param dec
mergeParam)
    invariantOrNotMergeParam :: Names -> VName -> Bool
invariantOrNotMergeParam Names
namesOfInvariant VName
name =
      (VName
name VName -> Names -> Bool
`notNameIn` Names
namesOfMergeParams)
        Bool -> Bool -> Bool
|| (VName
name VName -> Names -> Bool
`nameIn` Names
namesOfInvariant)

simplifyClosedFormLoop :: (BuilderOps rep) => TopDownRuleLoop rep
simplifyClosedFormLoop :: forall rep. BuilderOps rep => TopDownRuleLoop rep
simplifyClosedFormLoop TopDown rep
_ Pat (LetDec rep)
pat StmAux (ExpDec rep)
_ ([(FParam rep, SubExp)]
val, ForLoop VName
i IntType
it SubExp
bound, Body rep
body) =
  forall rep. RuleM rep () -> Rule rep
Simplify forall a b. (a -> b) -> a -> b
$ forall rep.
BuilderOps rep =>
Pat (LetDec rep)
-> [(FParam rep, SubExp)]
-> Names
-> IntType
-> SubExp
-> Body rep
-> RuleM rep ()
loopClosedForm Pat (LetDec rep)
pat [(FParam rep, SubExp)]
val (VName -> Names
oneName VName
i) IntType
it SubExp
bound Body rep
body
simplifyClosedFormLoop TopDown rep
_ Pat (LetDec rep)
_ StmAux (ExpDec rep)
_ ([(FParam rep, SubExp)], LoopForm, Body rep)
_ = forall rep. Rule rep
Skip

unroll ::
  (BuilderOps rep) =>
  Integer ->
  [(FParam rep, SubExpRes)] ->
  (VName, IntType, Integer) ->
  Body rep ->
  RuleM rep [SubExpRes]
unroll :: forall rep.
BuilderOps rep =>
Integer
-> [(FParam rep, SubExpRes)]
-> (VName, IntType, Integer)
-> Body rep
-> RuleM rep Result
unroll Integer
n [(FParam rep, SubExpRes)]
merge (VName
iv, IntType
it, Integer
i) Body rep
body
  | Integer
i forall a. Ord a => a -> a -> Bool
>= Integer
n =
      forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> b
snd [(FParam rep, SubExpRes)]
merge
  | Bool
otherwise = do
      Body rep
iter_body <- forall (m :: * -> *).
MonadBuilder m =>
m (Body (Rep m)) -> m (Body (Rep m))
insertStmsM forall a b. (a -> b) -> a -> b
$ do
        forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [(FParam rep, SubExpRes)]
merge forall a b. (a -> b) -> a -> b
$ \(FParam rep
mergevar, SubExpRes Certs
cs SubExp
mergeinit) ->
          forall (m :: * -> *) a. MonadBuilder m => Certs -> m a -> m a
certifying Certs
cs forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *).
MonadBuilder m =>
[VName] -> Exp (Rep m) -> m ()
letBindNames [forall dec. Param dec -> VName
paramName FParam rep
mergevar] forall a b. (a -> b) -> a -> b
$ forall rep. BasicOp -> Exp rep
BasicOp forall a b. (a -> b) -> a -> b
$ SubExp -> BasicOp
SubExp SubExp
mergeinit

        forall (m :: * -> *).
MonadBuilder m =>
[VName] -> Exp (Rep m) -> m ()
letBindNames [VName
iv] forall a b. (a -> b) -> a -> b
$ forall rep. BasicOp -> Exp rep
BasicOp forall a b. (a -> b) -> a -> b
$ SubExp -> BasicOp
SubExp forall a b. (a -> b) -> a -> b
$ IntType -> Integer -> SubExp
intConst IntType
it Integer
i

        -- Some of the sizes in the types here might be temporarily wrong
        -- until copy propagation fixes it up.
        forall (f :: * -> *) a. Applicative f => a -> f a
pure Body rep
body

      Body rep
iter_body' <- forall rep (m :: * -> *).
(Renameable rep, MonadFreshNames m) =>
Body rep -> m (Body rep)
renameBody Body rep
iter_body
      forall (m :: * -> *). MonadBuilder m => Stms (Rep m) -> m ()
addStms forall a b. (a -> b) -> a -> b
$ forall rep. Body rep -> Stms rep
bodyStms Body rep
iter_body'

      let merge' :: [(FParam rep, SubExpRes)]
merge' = forall a b. [a] -> [b] -> [(a, b)]
zip (forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst [(FParam rep, SubExpRes)]
merge) forall a b. (a -> b) -> a -> b
$ forall rep. Body rep -> Result
bodyResult Body rep
iter_body'
      forall rep.
BuilderOps rep =>
Integer
-> [(FParam rep, SubExpRes)]
-> (VName, IntType, Integer)
-> Body rep
-> RuleM rep Result
unroll Integer
n [(FParam rep, SubExpRes)]
merge' (VName
iv, IntType
it, Integer
i forall a. Num a => a -> a -> a
+ Integer
1) Body rep
body

simplifyKnownIterationLoop :: (BuilderOps rep) => TopDownRuleLoop rep
simplifyKnownIterationLoop :: forall rep. BuilderOps rep => TopDownRuleLoop rep
simplifyKnownIterationLoop TopDown rep
_ Pat (LetDec rep)
pat StmAux (ExpDec rep)
aux ([(FParam rep, SubExp)]
merge, ForLoop VName
i IntType
it (Constant PrimValue
iters), Body rep
body)
  | IntValue IntValue
n <- PrimValue
iters,
    IntValue -> Bool
zeroIshInt IntValue
n Bool -> Bool -> Bool
|| IntValue -> Bool
oneIshInt IntValue
n Bool -> Bool -> Bool
|| Attr
"unroll" Attr -> Attrs -> Bool
`inAttrs` forall dec. StmAux dec -> Attrs
stmAuxAttrs StmAux (ExpDec rep)
aux = forall rep. RuleM rep () -> Rule rep
Simplify forall a b. (a -> b) -> a -> b
$ do
      Result
res <- forall rep.
BuilderOps rep =>
Integer
-> [(FParam rep, SubExpRes)]
-> (VName, IntType, Integer)
-> Body rep
-> RuleM rep Result
unroll (forall int. Integral int => IntValue -> int
valueIntegral IntValue
n) (forall a b. (a -> b) -> [a] -> [b]
map (forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second SubExp -> SubExpRes
subExpRes) [(FParam rep, SubExp)]
merge) (VName
i, IntType
it, Integer
0) Body rep
body
      forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ (forall a b. [a] -> [b] -> [(a, b)]
zip (forall dec. Pat dec -> [VName]
patNames Pat (LetDec rep)
pat) Result
res) forall a b. (a -> b) -> a -> b
$ \(VName
v, SubExpRes Certs
cs SubExp
se) ->
        forall (m :: * -> *) a. MonadBuilder m => Certs -> m a -> m a
certifying Certs
cs forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *).
MonadBuilder m =>
[VName] -> Exp (Rep m) -> m ()
letBindNames [VName
v] forall a b. (a -> b) -> a -> b
$ forall rep. BasicOp -> Exp rep
BasicOp forall a b. (a -> b) -> a -> b
$ SubExp -> BasicOp
SubExp SubExp
se
simplifyKnownIterationLoop TopDown rep
_ Pat (LetDec rep)
_ StmAux (ExpDec rep)
_ ([(FParam rep, SubExp)], LoopForm, Body rep)
_ =
  forall rep. Rule rep
Skip

topDownRules :: (BuilderOps rep) => [TopDownRule rep]
topDownRules :: forall rep. BuilderOps rep => [TopDownRule rep]
topDownRules =
  [ forall rep a. RuleLoop rep a -> SimplificationRule rep a
RuleLoop forall rep. BuilderOps rep => TopDownRuleLoop rep
hoistLoopInvariantMergeVariables,
    forall rep a. RuleLoop rep a -> SimplificationRule rep a
RuleLoop forall rep. BuilderOps rep => TopDownRuleLoop rep
simplifyClosedFormLoop,
    forall rep a. RuleLoop rep a -> SimplificationRule rep a
RuleLoop forall rep. BuilderOps rep => TopDownRuleLoop rep
simplifyKnownIterationLoop
  ]

bottomUpRules :: (BuilderOps rep) => [BottomUpRule rep]
bottomUpRules :: forall rep. BuilderOps rep => [BottomUpRule rep]
bottomUpRules =
  [ forall rep a. RuleLoop rep a -> SimplificationRule rep a
RuleLoop forall rep. BuilderOps rep => BottomUpRuleLoop rep
removeRedundantMergeVariables
  ]

-- | Standard loop simplification rules.
loopRules :: (BuilderOps rep) => RuleBook rep
loopRules :: forall rep. BuilderOps rep => RuleBook rep
loopRules = forall m. [TopDownRule m] -> [BottomUpRule m] -> RuleBook m
ruleBook forall rep. BuilderOps rep => [TopDownRule rep]
topDownRules forall rep. BuilderOps rep => [BottomUpRule rep]
bottomUpRules