{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeFamilies #-}

-- | It is well known that fully parallel loops can always be
-- interchanged inwards with a sequential loop.  This module
-- implements that transformation.
--
-- This is also where we implement loop-switching (for branches),
-- which is semantically similar to interchange.
module Futhark.Pass.ExtractKernels.Interchange
  ( SeqLoop (..),
    interchangeLoops,
    Branch (..),
    interchangeBranch,
  )
where

import Control.Monad.RWS.Strict
import Data.List (find)
import Data.Maybe
import Futhark.IR.SOACS
import Futhark.MonadFreshNames
import Futhark.Pass.ExtractKernels.Distribution
  ( KernelNest,
    LoopNesting (..),
    kernelNestLoops,
  )
import Futhark.Tools
import Futhark.Transform.Rename

-- | An encoding of a sequential do-loop with no existential context,
-- alongside its result pattern.
data SeqLoop = SeqLoop [Int] Pattern [(FParam, SubExp)] (LoopForm SOACS) Body

seqLoopStm :: SeqLoop -> Stm
seqLoopStm :: SeqLoop -> Stm
seqLoopStm (SeqLoop [Int]
_ Pattern
pat [(FParam, SubExp)]
merge LoopForm SOACS
form Body
body) =
  Pattern -> StmAux (ExpDec SOACS) -> Exp SOACS -> Stm
forall lore.
Pattern lore -> StmAux (ExpDec lore) -> Exp lore -> Stm lore
Let Pattern
pat (() -> StmAux ()
forall dec. dec -> StmAux dec
defAux ()) (Exp SOACS -> Stm) -> Exp SOACS -> Stm
forall a b. (a -> b) -> a -> b
$ [(FParam, SubExp)]
-> [(FParam, SubExp)] -> LoopForm SOACS -> Body -> Exp SOACS
forall lore.
[(FParam lore, SubExp)]
-> [(FParam lore, SubExp)]
-> LoopForm lore
-> BodyT lore
-> ExpT lore
DoLoop [] [(FParam, SubExp)]
merge LoopForm SOACS
form Body
body

interchangeLoop ::
  (MonadBinder m, LocalScope SOACS m) =>
  (VName -> Maybe VName) ->
  SeqLoop ->
  LoopNesting ->
  m SeqLoop
interchangeLoop :: (VName -> Maybe VName) -> SeqLoop -> LoopNesting -> m SeqLoop
interchangeLoop
  VName -> Maybe VName
isMapParameter
  (SeqLoop [Int]
perm Pattern
loop_pat [(FParam, SubExp)]
merge LoopForm SOACS
form Body
body)
  (MapNesting PatternT Type
pat StmAux ()
aux SubExp
w [(Param Type, VName)]
params_and_arrs) = do
    [(Param (TypeBase Shape Uniqueness), SubExp)]
merge_expanded <-
      Scope SOACS
-> m [(Param (TypeBase Shape Uniqueness), SubExp)]
-> m [(Param (TypeBase Shape Uniqueness), SubExp)]
forall lore (m :: * -> *) a.
LocalScope lore m =>
Scope lore -> m a -> m a
localScope ([Param Type] -> Scope SOACS
forall lore dec.
(LParamInfo lore ~ dec) =>
[Param dec] -> Scope lore
scopeOfLParams ([Param Type] -> Scope SOACS) -> [Param Type] -> Scope SOACS
forall a b. (a -> b) -> a -> b
$ ((Param Type, VName) -> Param Type)
-> [(Param Type, VName)] -> [Param Type]
forall a b. (a -> b) -> [a] -> [b]
map (Param Type, VName) -> Param Type
forall a b. (a, b) -> a
fst [(Param Type, VName)]
params_and_arrs) (m [(Param (TypeBase Shape Uniqueness), SubExp)]
 -> m [(Param (TypeBase Shape Uniqueness), SubExp)])
-> m [(Param (TypeBase Shape Uniqueness), SubExp)]
-> m [(Param (TypeBase Shape Uniqueness), SubExp)]
forall a b. (a -> b) -> a -> b
$
        ((Param (TypeBase Shape Uniqueness), SubExp)
 -> m (Param (TypeBase Shape Uniqueness), SubExp))
-> [(Param (TypeBase Shape Uniqueness), SubExp)]
-> m [(Param (TypeBase Shape Uniqueness), SubExp)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (Param (TypeBase Shape Uniqueness), SubExp)
-> m (Param (TypeBase Shape Uniqueness), SubExp)
expand [(Param (TypeBase Shape Uniqueness), SubExp)]
[(FParam, SubExp)]
merge

    let loop_pat_expanded :: PatternT Type
loop_pat_expanded =
          [PatElemT Type] -> [PatElemT Type] -> PatternT Type
forall dec. [PatElemT dec] -> [PatElemT dec] -> PatternT dec
Pattern [] ([PatElemT Type] -> PatternT Type)
-> [PatElemT Type] -> PatternT Type
forall a b. (a -> b) -> a -> b
$ (PatElemT Type -> PatElemT Type)
-> [PatElemT Type] -> [PatElemT Type]
forall a b. (a -> b) -> [a] -> [b]
map PatElemT Type -> PatElemT Type
expandPatElem ([PatElemT Type] -> [PatElemT Type])
-> [PatElemT Type] -> [PatElemT Type]
forall a b. (a -> b) -> a -> b
$ PatternT Type -> [PatElemT Type]
forall dec. PatternT dec -> [PatElemT dec]
patternElements PatternT Type
Pattern
loop_pat
        new_params :: [Param Type]
new_params =
          [ VName -> Type -> Param Type
forall dec. VName -> dec -> Param dec
Param VName
pname (Type -> Param Type) -> Type -> Param Type
forall a b. (a -> b) -> a -> b
$ TypeBase Shape Uniqueness -> Type
forall shape.
TypeBase shape Uniqueness -> TypeBase shape NoUniqueness
fromDecl TypeBase Shape Uniqueness
ptype
            | (Param VName
pname TypeBase Shape Uniqueness
ptype, SubExp
_) <- [(Param (TypeBase Shape Uniqueness), SubExp)]
[(FParam, SubExp)]
merge
          ]
        new_arrs :: [VName]
new_arrs = ((Param (TypeBase Shape Uniqueness), SubExp) -> VName)
-> [(Param (TypeBase Shape Uniqueness), SubExp)] -> [VName]
forall a b. (a -> b) -> [a] -> [b]
map (Param (TypeBase Shape Uniqueness) -> VName
forall dec. Param dec -> VName
paramName (Param (TypeBase Shape Uniqueness) -> VName)
-> ((Param (TypeBase Shape Uniqueness), SubExp)
    -> Param (TypeBase Shape Uniqueness))
-> (Param (TypeBase Shape Uniqueness), SubExp)
-> VName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Param (TypeBase Shape Uniqueness), SubExp)
-> Param (TypeBase Shape Uniqueness)
forall a b. (a, b) -> a
fst) [(Param (TypeBase Shape Uniqueness), SubExp)]
merge_expanded
        rettype :: [Type]
rettype = (Type -> Type) -> [Type] -> [Type]
forall a b. (a -> b) -> [a] -> [b]
map Type -> Type
forall shape u.
ArrayShape shape =>
TypeBase shape u -> TypeBase shape u
rowType ([Type] -> [Type]) -> [Type] -> [Type]
forall a b. (a -> b) -> a -> b
$ PatternT Type -> [Type]
forall dec. Typed dec => PatternT dec -> [Type]
patternTypes PatternT Type
loop_pat_expanded

    -- If the map consumes something that is bound outside the loop
    -- (i.e. is not a merge parameter), we have to copy() it.  As a
    -- small simplification, we just remove the parameter outright if
    -- it is not used anymore.  This might happen if the parameter was
    -- used just as the inital value of a merge parameter.
    (([Param Type]
params', [VName]
arrs'), Stms SOACS
pre_copy_bnds) <-
      Binder SOACS ([Param Type], [VName])
-> m (([Param Type], [VName]), Stms SOACS)
forall (m :: * -> *) somelore lore a.
(MonadFreshNames m, HasScope somelore m,
 SameScope somelore lore) =>
Binder lore a -> m (a, Stms lore)
runBinder (Binder SOACS ([Param Type], [VName])
 -> m (([Param Type], [VName]), Stms SOACS))
-> Binder SOACS ([Param Type], [VName])
-> m (([Param Type], [VName]), Stms SOACS)
forall a b. (a -> b) -> a -> b
$
        Scope SOACS
-> Binder SOACS ([Param Type], [VName])
-> Binder SOACS ([Param Type], [VName])
forall lore (m :: * -> *) a.
LocalScope lore m =>
Scope lore -> m a -> m a
localScope ([Param Type] -> Scope SOACS
forall lore dec.
(LParamInfo lore ~ dec) =>
[Param dec] -> Scope lore
scopeOfLParams [Param Type]
new_params) (Binder SOACS ([Param Type], [VName])
 -> Binder SOACS ([Param Type], [VName]))
-> Binder SOACS ([Param Type], [VName])
-> Binder SOACS ([Param Type], [VName])
forall a b. (a -> b) -> a -> b
$
          [(Param Type, VName)] -> ([Param Type], [VName])
forall a b. [(a, b)] -> ([a], [b])
unzip ([(Param Type, VName)] -> ([Param Type], [VName]))
-> ([Maybe (Param Type, VName)] -> [(Param Type, VName)])
-> [Maybe (Param Type, VName)]
-> ([Param Type], [VName])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Maybe (Param Type, VName)] -> [(Param Type, VName)]
forall a. [Maybe a] -> [a]
catMaybes ([Maybe (Param Type, VName)] -> ([Param Type], [VName]))
-> BinderT SOACS (State VNameSource) [Maybe (Param Type, VName)]
-> Binder SOACS ([Param Type], [VName])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ((Param Type, VName)
 -> BinderT SOACS (State VNameSource) (Maybe (Param Type, VName)))
-> [(Param Type, VName)]
-> BinderT SOACS (State VNameSource) [Maybe (Param Type, VName)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (Param Type, VName)
-> BinderT SOACS (State VNameSource) (Maybe (Param Type, VName))
copyOrRemoveParam [(Param Type, VName)]
params_and_arrs

    let lam :: LambdaT SOACS
lam = [LParam SOACS] -> Body -> [Type] -> LambdaT SOACS
forall lore. [LParam lore] -> BodyT lore -> [Type] -> LambdaT lore
Lambda ([Param Type]
params' [Param Type] -> [Param Type] -> [Param Type]
forall a. Semigroup a => a -> a -> a
<> [Param Type]
new_params) Body
body [Type]
rettype
        map_bnd :: Stm
map_bnd =
          Pattern -> StmAux (ExpDec SOACS) -> Exp SOACS -> Stm
forall lore.
Pattern lore -> StmAux (ExpDec lore) -> Exp lore -> Stm lore
Let PatternT Type
Pattern
loop_pat_expanded StmAux ()
StmAux (ExpDec SOACS)
aux (Exp SOACS -> Stm) -> Exp SOACS -> Stm
forall a b. (a -> b) -> a -> b
$
            Op SOACS -> Exp SOACS
forall lore. Op lore -> ExpT lore
Op (Op SOACS -> Exp SOACS) -> Op SOACS -> Exp SOACS
forall a b. (a -> b) -> a -> b
$ SubExp -> [VName] -> ScremaForm SOACS -> SOAC SOACS
forall lore. SubExp -> [VName] -> ScremaForm lore -> SOAC lore
Screma SubExp
w ([VName]
arrs' [VName] -> [VName] -> [VName]
forall a. Semigroup a => a -> a -> a
<> [VName]
new_arrs) (LambdaT SOACS -> ScremaForm SOACS
forall lore. Lambda lore -> ScremaForm lore
mapSOAC LambdaT SOACS
lam)
        res :: [SubExp]
res = (VName -> SubExp) -> [VName] -> [SubExp]
forall a b. (a -> b) -> [a] -> [b]
map VName -> SubExp
Var ([VName] -> [SubExp]) -> [VName] -> [SubExp]
forall a b. (a -> b) -> a -> b
$ PatternT Type -> [VName]
forall dec. PatternT dec -> [VName]
patternNames PatternT Type
loop_pat_expanded
        pat' :: PatternT Type
pat' = [PatElemT Type] -> [PatElemT Type] -> PatternT Type
forall dec. [PatElemT dec] -> [PatElemT dec] -> PatternT dec
Pattern [] ([PatElemT Type] -> PatternT Type)
-> [PatElemT Type] -> PatternT Type
forall a b. (a -> b) -> a -> b
$ [Int] -> [PatElemT Type] -> [PatElemT Type]
forall a. [Int] -> [a] -> [a]
rearrangeShape [Int]
perm ([PatElemT Type] -> [PatElemT Type])
-> [PatElemT Type] -> [PatElemT Type]
forall a b. (a -> b) -> a -> b
$ PatternT Type -> [PatElemT Type]
forall dec. PatternT dec -> [PatElemT dec]
patternValueElements PatternT Type
pat

    SeqLoop -> m SeqLoop
forall (m :: * -> *) a. Monad m => a -> m a
return (SeqLoop -> m SeqLoop) -> SeqLoop -> m SeqLoop
forall a b. (a -> b) -> a -> b
$
      [Int]
-> Pattern
-> [(FParam, SubExp)]
-> LoopForm SOACS
-> Body
-> SeqLoop
SeqLoop [Int]
perm PatternT Type
Pattern
pat' [(Param (TypeBase Shape Uniqueness), SubExp)]
[(FParam, SubExp)]
merge_expanded LoopForm SOACS
form (Body -> SeqLoop) -> Body -> SeqLoop
forall a b. (a -> b) -> a -> b
$
        Stms SOACS -> [SubExp] -> Body
forall lore. Bindable lore => Stms lore -> [SubExp] -> Body lore
mkBody (Stms SOACS
pre_copy_bnds Stms SOACS -> Stms SOACS -> Stms SOACS
forall a. Semigroup a => a -> a -> a
<> Stm -> Stms SOACS
forall lore. Stm lore -> Stms lore
oneStm Stm
map_bnd) [SubExp]
res
    where
      free_in_body :: Names
free_in_body = Body -> Names
forall a. FreeIn a => a -> Names
freeIn Body
body

      copyOrRemoveParam :: (Param Type, VName)
-> BinderT SOACS (State VNameSource) (Maybe (Param Type, VName))
copyOrRemoveParam (Param Type
param, VName
arr)
        | Bool -> Bool
not (Param Type -> VName
forall dec. Param dec -> VName
paramName Param Type
param VName -> Names -> Bool
`nameIn` Names
free_in_body) =
          Maybe (Param Type, VName)
-> BinderT SOACS (State VNameSource) (Maybe (Param Type, VName))
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (Param Type, VName)
forall a. Maybe a
Nothing
        | Bool
otherwise =
          Maybe (Param Type, VName)
-> BinderT SOACS (State VNameSource) (Maybe (Param Type, VName))
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (Param Type, VName)
 -> BinderT SOACS (State VNameSource) (Maybe (Param Type, VName)))
-> Maybe (Param Type, VName)
-> BinderT SOACS (State VNameSource) (Maybe (Param Type, VName))
forall a b. (a -> b) -> a -> b
$ (Param Type, VName) -> Maybe (Param Type, VName)
forall a. a -> Maybe a
Just (Param Type
param, VName
arr)

      expandedInit :: [Char] -> SubExp -> m SubExp
expandedInit [Char]
_ (Var VName
v)
        | Just VName
arr <- VName -> Maybe VName
isMapParameter VName
v =
          SubExp -> m SubExp
forall (m :: * -> *) a. Monad m => a -> m a
return (SubExp -> m SubExp) -> SubExp -> m SubExp
forall a b. (a -> b) -> a -> b
$ VName -> SubExp
Var VName
arr
      expandedInit [Char]
param_name SubExp
se =
        [Char] -> Exp (Lore m) -> m SubExp
forall (m :: * -> *).
MonadBinder m =>
[Char] -> Exp (Lore m) -> m SubExp
letSubExp ([Char]
param_name [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> [Char]
"_expanded_init") (Exp (Lore m) -> m SubExp) -> Exp (Lore m) -> m SubExp
forall a b. (a -> b) -> a -> b
$
          BasicOp -> Exp (Lore m)
forall lore. BasicOp -> ExpT lore
BasicOp (BasicOp -> Exp (Lore m)) -> BasicOp -> Exp (Lore m)
forall a b. (a -> b) -> a -> b
$ Shape -> SubExp -> BasicOp
Replicate ([SubExp] -> Shape
forall d. [d] -> ShapeBase d
Shape [SubExp
w]) SubExp
se

      expand :: (Param (TypeBase Shape Uniqueness), SubExp)
-> m (Param (TypeBase Shape Uniqueness), SubExp)
expand (Param (TypeBase Shape Uniqueness)
merge_param, SubExp
merge_init) = do
        Param (TypeBase Shape Uniqueness)
expanded_param <-
          [Char]
-> TypeBase Shape Uniqueness
-> m (Param (TypeBase Shape Uniqueness))
forall (m :: * -> *) dec.
MonadFreshNames m =>
[Char] -> dec -> m (Param dec)
newParam ([Char]
param_name [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> [Char]
"_expanded") (TypeBase Shape Uniqueness
 -> m (Param (TypeBase Shape Uniqueness)))
-> TypeBase Shape Uniqueness
-> m (Param (TypeBase Shape Uniqueness))
forall a b. (a -> b) -> a -> b
$
            TypeBase Shape Uniqueness
-> Shape -> Uniqueness -> TypeBase Shape Uniqueness
forall shape u_unused u.
ArrayShape shape =>
TypeBase shape u_unused -> shape -> u -> TypeBase shape u
arrayOf (Param (TypeBase Shape Uniqueness) -> TypeBase Shape Uniqueness
forall dec. DeclTyped dec => Param dec -> TypeBase Shape Uniqueness
paramDeclType Param (TypeBase Shape Uniqueness)
merge_param) ([SubExp] -> Shape
forall d. [d] -> ShapeBase d
Shape [SubExp
w]) (Uniqueness -> TypeBase Shape Uniqueness)
-> Uniqueness -> TypeBase Shape Uniqueness
forall a b. (a -> b) -> a -> b
$
              TypeBase Shape Uniqueness -> Uniqueness
forall shape. TypeBase shape Uniqueness -> Uniqueness
uniqueness (TypeBase Shape Uniqueness -> Uniqueness)
-> TypeBase Shape Uniqueness -> Uniqueness
forall a b. (a -> b) -> a -> b
$ Param (TypeBase Shape Uniqueness) -> TypeBase Shape Uniqueness
forall t. DeclTyped t => t -> TypeBase Shape Uniqueness
declTypeOf Param (TypeBase Shape Uniqueness)
merge_param
        SubExp
expanded_init <- [Char] -> SubExp -> m SubExp
expandedInit [Char]
param_name SubExp
merge_init
        (Param (TypeBase Shape Uniqueness), SubExp)
-> m (Param (TypeBase Shape Uniqueness), SubExp)
forall (m :: * -> *) a. Monad m => a -> m a
return (Param (TypeBase Shape Uniqueness)
expanded_param, SubExp
expanded_init)
        where
          param_name :: [Char]
param_name = VName -> [Char]
baseString (VName -> [Char]) -> VName -> [Char]
forall a b. (a -> b) -> a -> b
$ Param (TypeBase Shape Uniqueness) -> VName
forall dec. Param dec -> VName
paramName Param (TypeBase Shape Uniqueness)
merge_param

      expandPatElem :: PatElemT Type -> PatElemT Type
expandPatElem (PatElem VName
name Type
t) =
        VName -> Type -> PatElemT Type
forall dec. VName -> dec -> PatElemT dec
PatElem VName
name (Type -> PatElemT Type) -> Type -> PatElemT Type
forall a b. (a -> b) -> a -> b
$ Type -> SubExp -> Type
forall d.
ArrayShape (ShapeBase d) =>
TypeBase (ShapeBase d) NoUniqueness
-> d -> TypeBase (ShapeBase d) NoUniqueness
arrayOfRow Type
t SubExp
w

-- | Given a (parallel) map nesting and an inner sequential loop, move
-- the maps inside the sequential loop.  The result is several
-- statements - one of these will be the loop, which will then contain
-- statements with @map@ expressions.
interchangeLoops ::
  (MonadFreshNames m, HasScope SOACS m) =>
  KernelNest ->
  SeqLoop ->
  m (Stms SOACS)
interchangeLoops :: KernelNest -> SeqLoop -> m (Stms SOACS)
interchangeLoops KernelNest
nest SeqLoop
loop = do
  (SeqLoop
loop', Stms SOACS
bnds) <-
    Binder SOACS SeqLoop -> m (SeqLoop, Stms SOACS)
forall (m :: * -> *) somelore lore a.
(MonadFreshNames m, HasScope somelore m,
 SameScope somelore lore) =>
Binder lore a -> m (a, Stms lore)
runBinder (Binder SOACS SeqLoop -> m (SeqLoop, Stms SOACS))
-> Binder SOACS SeqLoop -> m (SeqLoop, Stms SOACS)
forall a b. (a -> b) -> a -> b
$
      (SeqLoop -> LoopNesting -> Binder SOACS SeqLoop)
-> SeqLoop -> [LoopNesting] -> Binder SOACS SeqLoop
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldM ((VName -> Maybe VName)
-> SeqLoop -> LoopNesting -> Binder SOACS SeqLoop
forall (m :: * -> *).
(MonadBinder m, LocalScope SOACS m) =>
(VName -> Maybe VName) -> SeqLoop -> LoopNesting -> m SeqLoop
interchangeLoop VName -> Maybe VName
isMapParameter) SeqLoop
loop ([LoopNesting] -> Binder SOACS SeqLoop)
-> [LoopNesting] -> Binder SOACS SeqLoop
forall a b. (a -> b) -> a -> b
$
        [LoopNesting] -> [LoopNesting]
forall a. [a] -> [a]
reverse ([LoopNesting] -> [LoopNesting]) -> [LoopNesting] -> [LoopNesting]
forall a b. (a -> b) -> a -> b
$ KernelNest -> [LoopNesting]
kernelNestLoops KernelNest
nest
  Stms SOACS -> m (Stms SOACS)
forall (m :: * -> *) a. Monad m => a -> m a
return (Stms SOACS -> m (Stms SOACS)) -> Stms SOACS -> m (Stms SOACS)
forall a b. (a -> b) -> a -> b
$ Stms SOACS
bnds Stms SOACS -> Stms SOACS -> Stms SOACS
forall a. Semigroup a => a -> a -> a
<> Stm -> Stms SOACS
forall lore. Stm lore -> Stms lore
oneStm (SeqLoop -> Stm
seqLoopStm SeqLoop
loop')
  where
    isMapParameter :: VName -> Maybe VName
isMapParameter VName
v =
      ((Param Type, VName) -> VName)
-> Maybe (Param Type, VName) -> Maybe VName
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Param Type, VName) -> VName
forall a b. (a, b) -> b
snd (Maybe (Param Type, VName) -> Maybe VName)
-> Maybe (Param Type, VName) -> Maybe VName
forall a b. (a -> b) -> a -> b
$
        ((Param Type, VName) -> Bool)
-> [(Param Type, VName)] -> Maybe (Param Type, VName)
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find ((VName -> VName -> Bool
forall a. Eq a => a -> a -> Bool
== VName
v) (VName -> Bool)
-> ((Param Type, VName) -> VName) -> (Param Type, VName) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Param Type -> VName
forall dec. Param dec -> VName
paramName (Param Type -> VName)
-> ((Param Type, VName) -> Param Type)
-> (Param Type, VName)
-> VName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Param Type, VName) -> Param Type
forall a b. (a, b) -> a
fst) ([(Param Type, VName)] -> Maybe (Param Type, VName))
-> [(Param Type, VName)] -> Maybe (Param Type, VName)
forall a b. (a -> b) -> a -> b
$
          (LoopNesting -> [(Param Type, VName)])
-> [LoopNesting] -> [(Param Type, VName)]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap LoopNesting -> [(Param Type, VName)]
loopNestingParamsAndArrs ([LoopNesting] -> [(Param Type, VName)])
-> [LoopNesting] -> [(Param Type, VName)]
forall a b. (a -> b) -> a -> b
$ KernelNest -> [LoopNesting]
kernelNestLoops KernelNest
nest

data Branch = Branch [Int] Pattern SubExp Body Body (IfDec (BranchType SOACS))

branchStm :: Branch -> Stm
branchStm :: Branch -> Stm
branchStm (Branch [Int]
_ Pattern
pat SubExp
cond Body
tbranch Body
fbranch IfDec (BranchType SOACS)
ret) =
  Pattern -> StmAux (ExpDec SOACS) -> Exp SOACS -> Stm
forall lore.
Pattern lore -> StmAux (ExpDec lore) -> Exp lore -> Stm lore
Let Pattern
pat (() -> StmAux ()
forall dec. dec -> StmAux dec
defAux ()) (Exp SOACS -> Stm) -> Exp SOACS -> Stm
forall a b. (a -> b) -> a -> b
$ SubExp -> Body -> Body -> IfDec (BranchType SOACS) -> Exp SOACS
forall lore.
SubExp
-> BodyT lore -> BodyT lore -> IfDec (BranchType lore) -> ExpT lore
If SubExp
cond Body
tbranch Body
fbranch IfDec (BranchType SOACS)
ret

interchangeBranch1 ::
  (MonadBinder m) =>
  Branch ->
  LoopNesting ->
  m Branch
interchangeBranch1 :: Branch -> LoopNesting -> m Branch
interchangeBranch1
  (Branch [Int]
perm Pattern
branch_pat SubExp
cond Body
tbranch Body
fbranch (IfDec [BranchType SOACS]
ret IfSort
if_sort))
  (MapNesting PatternT Type
pat StmAux ()
aux SubExp
w [(Param Type, VName)]
params_and_arrs) = do
    let ret' :: [TypeBase (ShapeBase (Ext SubExp)) NoUniqueness]
ret' = (TypeBase (ShapeBase (Ext SubExp)) NoUniqueness
 -> TypeBase (ShapeBase (Ext SubExp)) NoUniqueness)
-> [TypeBase (ShapeBase (Ext SubExp)) NoUniqueness]
-> [TypeBase (ShapeBase (Ext SubExp)) NoUniqueness]
forall a b. (a -> b) -> [a] -> [b]
map (TypeBase (ShapeBase (Ext SubExp)) NoUniqueness
-> Ext SubExp -> TypeBase (ShapeBase (Ext SubExp)) NoUniqueness
forall d.
ArrayShape (ShapeBase d) =>
TypeBase (ShapeBase d) NoUniqueness
-> d -> TypeBase (ShapeBase d) NoUniqueness
`arrayOfRow` SubExp -> Ext SubExp
forall a. a -> Ext a
Free SubExp
w) [TypeBase (ShapeBase (Ext SubExp)) NoUniqueness]
[BranchType SOACS]
ret
        pat' :: PatternT Type
pat' = [PatElemT Type] -> [PatElemT Type] -> PatternT Type
forall dec. [PatElemT dec] -> [PatElemT dec] -> PatternT dec
Pattern [] ([PatElemT Type] -> PatternT Type)
-> [PatElemT Type] -> PatternT Type
forall a b. (a -> b) -> a -> b
$ [Int] -> [PatElemT Type] -> [PatElemT Type]
forall a. [Int] -> [a] -> [a]
rearrangeShape [Int]
perm ([PatElemT Type] -> [PatElemT Type])
-> [PatElemT Type] -> [PatElemT Type]
forall a b. (a -> b) -> a -> b
$ PatternT Type -> [PatElemT Type]
forall dec. PatternT dec -> [PatElemT dec]
patternValueElements PatternT Type
pat

        ([Param Type]
params, [VName]
arrs) = [(Param Type, VName)] -> ([Param Type], [VName])
forall a b. [(a, b)] -> ([a], [b])
unzip [(Param Type, VName)]
params_and_arrs
        lam_ret :: [Type]
lam_ret = [Int] -> [Type] -> [Type]
forall a. [Int] -> [a] -> [a]
rearrangeShape [Int]
perm ([Type] -> [Type]) -> [Type] -> [Type]
forall a b. (a -> b) -> a -> b
$ (Type -> Type) -> [Type] -> [Type]
forall a b. (a -> b) -> [a] -> [b]
map Type -> Type
forall shape u.
ArrayShape shape =>
TypeBase shape u -> TypeBase shape u
rowType ([Type] -> [Type]) -> [Type] -> [Type]
forall a b. (a -> b) -> a -> b
$ PatternT Type -> [Type]
forall dec. Typed dec => PatternT dec -> [Type]
patternTypes PatternT Type
pat

        branch_pat' :: PatternT Type
branch_pat' =
          [PatElemT Type] -> [PatElemT Type] -> PatternT Type
forall dec. [PatElemT dec] -> [PatElemT dec] -> PatternT dec
Pattern [] ([PatElemT Type] -> PatternT Type)
-> [PatElemT Type] -> PatternT Type
forall a b. (a -> b) -> a -> b
$ (PatElemT Type -> PatElemT Type)
-> [PatElemT Type] -> [PatElemT Type]
forall a b. (a -> b) -> [a] -> [b]
map ((Type -> Type) -> PatElemT Type -> PatElemT Type
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Type -> SubExp -> Type
forall d.
ArrayShape (ShapeBase d) =>
TypeBase (ShapeBase d) NoUniqueness
-> d -> TypeBase (ShapeBase d) NoUniqueness
`arrayOfRow` SubExp
w)) ([PatElemT Type] -> [PatElemT Type])
-> [PatElemT Type] -> [PatElemT Type]
forall a b. (a -> b) -> a -> b
$ PatternT Type -> [PatElemT Type]
forall dec. PatternT dec -> [PatElemT dec]
patternElements PatternT Type
Pattern
branch_pat

        mkBranch :: Body -> m Body
mkBranch Body
branch = (Body -> m Body
forall lore (m :: * -> *).
(Renameable lore, MonadFreshNames m) =>
Body lore -> m (Body lore)
renameBody (Body -> m Body) -> m Body -> m Body
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<<) (m Body -> m Body) -> m Body -> m Body
forall a b. (a -> b) -> a -> b
$ do
          let lam :: LambdaT SOACS
lam = [LParam SOACS] -> Body -> [Type] -> LambdaT SOACS
forall lore. [LParam lore] -> BodyT lore -> [Type] -> LambdaT lore
Lambda [Param Type]
[LParam SOACS]
params Body
branch [Type]
lam_ret
              res :: [SubExp]
res = (VName -> SubExp) -> [VName] -> [SubExp]
forall a b. (a -> b) -> [a] -> [b]
map VName -> SubExp
Var ([VName] -> [SubExp]) -> [VName] -> [SubExp]
forall a b. (a -> b) -> a -> b
$ PatternT Type -> [VName]
forall dec. PatternT dec -> [VName]
patternNames PatternT Type
branch_pat'
              map_bnd :: Stm
map_bnd = Pattern -> StmAux (ExpDec SOACS) -> Exp SOACS -> Stm
forall lore.
Pattern lore -> StmAux (ExpDec lore) -> Exp lore -> Stm lore
Let PatternT Type
Pattern
branch_pat' StmAux ()
StmAux (ExpDec SOACS)
aux (Exp SOACS -> Stm) -> Exp SOACS -> Stm
forall a b. (a -> b) -> a -> b
$ Op SOACS -> Exp SOACS
forall lore. Op lore -> ExpT lore
Op (Op SOACS -> Exp SOACS) -> Op SOACS -> Exp SOACS
forall a b. (a -> b) -> a -> b
$ SubExp -> [VName] -> ScremaForm SOACS -> SOAC SOACS
forall lore. SubExp -> [VName] -> ScremaForm lore -> SOAC lore
Screma SubExp
w [VName]
arrs (ScremaForm SOACS -> SOAC SOACS) -> ScremaForm SOACS -> SOAC SOACS
forall a b. (a -> b) -> a -> b
$ LambdaT SOACS -> ScremaForm SOACS
forall lore. Lambda lore -> ScremaForm lore
mapSOAC LambdaT SOACS
lam
          Body -> m Body
forall (m :: * -> *) a. Monad m => a -> m a
return (Body -> m Body) -> Body -> m Body
forall a b. (a -> b) -> a -> b
$ Stms SOACS -> [SubExp] -> Body
forall lore. Bindable lore => Stms lore -> [SubExp] -> Body lore
mkBody (Stm -> Stms SOACS
forall lore. Stm lore -> Stms lore
oneStm Stm
map_bnd) [SubExp]
res

    Body
tbranch' <- Body -> m Body
mkBranch Body
tbranch
    Body
fbranch' <- Body -> m Body
mkBranch Body
fbranch
    Branch -> m Branch
forall (m :: * -> *) a. Monad m => a -> m a
return (Branch -> m Branch) -> Branch -> m Branch
forall a b. (a -> b) -> a -> b
$
      [Int]
-> Pattern
-> SubExp
-> Body
-> Body
-> IfDec (BranchType SOACS)
-> Branch
Branch [Int
0 .. PatternT Type -> Int
forall dec. PatternT dec -> Int
patternSize PatternT Type
pat Int -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1] PatternT Type
Pattern
pat' SubExp
cond Body
tbranch' Body
fbranch' (IfDec (BranchType SOACS) -> Branch)
-> IfDec (BranchType SOACS) -> Branch
forall a b. (a -> b) -> a -> b
$
        [TypeBase (ShapeBase (Ext SubExp)) NoUniqueness]
-> IfSort -> IfDec (TypeBase (ShapeBase (Ext SubExp)) NoUniqueness)
forall rt. [rt] -> IfSort -> IfDec rt
IfDec [TypeBase (ShapeBase (Ext SubExp)) NoUniqueness]
ret' IfSort
if_sort

interchangeBranch ::
  (MonadFreshNames m, HasScope SOACS m) =>
  KernelNest ->
  Branch ->
  m (Stms SOACS)
interchangeBranch :: KernelNest -> Branch -> m (Stms SOACS)
interchangeBranch KernelNest
nest Branch
loop = do
  (Branch
loop', Stms SOACS
bnds) <-
    Binder SOACS Branch -> m (Branch, Stms SOACS)
forall (m :: * -> *) somelore lore a.
(MonadFreshNames m, HasScope somelore m,
 SameScope somelore lore) =>
Binder lore a -> m (a, Stms lore)
runBinder (Binder SOACS Branch -> m (Branch, Stms SOACS))
-> Binder SOACS Branch -> m (Branch, Stms SOACS)
forall a b. (a -> b) -> a -> b
$ (Branch -> LoopNesting -> Binder SOACS Branch)
-> Branch -> [LoopNesting] -> Binder SOACS Branch
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldM Branch -> LoopNesting -> Binder SOACS Branch
forall (m :: * -> *).
MonadBinder m =>
Branch -> LoopNesting -> m Branch
interchangeBranch1 Branch
loop ([LoopNesting] -> Binder SOACS Branch)
-> [LoopNesting] -> Binder SOACS Branch
forall a b. (a -> b) -> a -> b
$ [LoopNesting] -> [LoopNesting]
forall a. [a] -> [a]
reverse ([LoopNesting] -> [LoopNesting]) -> [LoopNesting] -> [LoopNesting]
forall a b. (a -> b) -> a -> b
$ KernelNest -> [LoopNesting]
kernelNestLoops KernelNest
nest
  Stms SOACS -> m (Stms SOACS)
forall (m :: * -> *) a. Monad m => a -> m a
return (Stms SOACS -> m (Stms SOACS)) -> Stms SOACS -> m (Stms SOACS)
forall a b. (a -> b) -> a -> b
$ Stms SOACS
bnds Stms SOACS -> Stms SOACS -> Stms SOACS
forall a. Semigroup a => a -> a -> a
<> Stm -> Stms SOACS
forall lore. Stm lore -> Stms lore
oneStm (Branch -> Stm
branchStm Branch
loop')