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

-- | This module exports facilities for transforming array accesses in
-- a list of 'Stm's (intended to be the bindings in a body).  The
-- idea is that you can state that some variable @x@ is in fact an
-- array indexing @v[i0,i1,...]@.
module Futhark.Optimise.InPlaceLowering.SubstituteIndices
  ( substituteIndices,
    IndexSubstitution,
    IndexSubstitutions,
  )
where

import Control.Monad
import qualified Data.Map.Strict as M
import Futhark.Construct
import Futhark.IR
import Futhark.IR.Prop.Aliases
import Futhark.Transform.Substitute
import Futhark.Util

type IndexSubstitution dec = (Certs, VName, dec, Slice SubExp)

type IndexSubstitutions dec = [(VName, IndexSubstitution dec)]

typeEnvFromSubstitutions ::
  LetDec rep ~ dec =>
  IndexSubstitutions dec ->
  Scope rep
typeEnvFromSubstitutions :: IndexSubstitutions dec -> Scope rep
typeEnvFromSubstitutions = [(VName, NameInfo rep)] -> Scope rep
forall k a. Ord k => [(k, a)] -> Map k a
M.fromList ([(VName, NameInfo rep)] -> Scope rep)
-> (IndexSubstitutions dec -> [(VName, NameInfo rep)])
-> IndexSubstitutions dec
-> Scope rep
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((VName, IndexSubstitution dec) -> (VName, NameInfo rep))
-> IndexSubstitutions dec -> [(VName, NameInfo rep)]
forall a b. (a -> b) -> [a] -> [b]
map (IndexSubstitution dec -> (VName, NameInfo rep)
forall a a rep d. (a, a, LetDec rep, d) -> (a, NameInfo rep)
fromSubstitution (IndexSubstitution dec -> (VName, NameInfo rep))
-> ((VName, IndexSubstitution dec) -> IndexSubstitution dec)
-> (VName, IndexSubstitution dec)
-> (VName, NameInfo rep)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (VName, IndexSubstitution dec) -> IndexSubstitution dec
forall a b. (a, b) -> b
snd)
  where
    fromSubstitution :: (a, a, LetDec rep, d) -> (a, NameInfo rep)
fromSubstitution (a
_, a
name, LetDec rep
t, d
_) =
      (a
name, LetDec rep -> NameInfo rep
forall rep. LetDec rep -> NameInfo rep
LetName LetDec rep
t)

-- | Perform the substitution.
substituteIndices ::
  ( MonadFreshNames m,
    BuilderOps rep,
    Buildable rep,
    Aliased rep,
    LetDec rep ~ dec
  ) =>
  IndexSubstitutions dec ->
  Stms rep ->
  m (IndexSubstitutions dec, Stms rep)
substituteIndices :: IndexSubstitutions dec
-> Stms rep -> m (IndexSubstitutions dec, Stms rep)
substituteIndices IndexSubstitutions dec
substs Stms rep
bnds =
  BuilderT rep m (IndexSubstitutions dec)
-> Scope rep -> m (IndexSubstitutions dec, Stms rep)
forall (m :: * -> *) rep a.
MonadFreshNames m =>
BuilderT rep m a -> Scope rep -> m (a, Stms rep)
runBuilderT (IndexSubstitutions (LetDec (Rep (BuilderT rep m)))
-> Stms (Rep (BuilderT rep m))
-> BuilderT
     rep m (IndexSubstitutions (LetDec (Rep (BuilderT rep m))))
forall (m :: * -> *).
(MonadBuilder m, Buildable (Rep m), Aliased (Rep m)) =>
IndexSubstitutions (LetDec (Rep m))
-> Stms (Rep m) -> m (IndexSubstitutions (LetDec (Rep m)))
substituteIndicesInStms IndexSubstitutions dec
IndexSubstitutions (LetDec (Rep (BuilderT rep m)))
substs Stms rep
Stms (Rep (BuilderT rep m))
bnds) Scope rep
types
  where
    types :: Scope rep
types = IndexSubstitutions dec -> Scope rep
forall rep dec.
(LetDec rep ~ dec) =>
IndexSubstitutions dec -> Scope rep
typeEnvFromSubstitutions IndexSubstitutions dec
substs

substituteIndicesInStms ::
  (MonadBuilder m, Buildable (Rep m), Aliased (Rep m)) =>
  IndexSubstitutions (LetDec (Rep m)) ->
  Stms (Rep m) ->
  m (IndexSubstitutions (LetDec (Rep m)))
substituteIndicesInStms :: IndexSubstitutions (LetDec (Rep m))
-> Stms (Rep m) -> m (IndexSubstitutions (LetDec (Rep m)))
substituteIndicesInStms = (IndexSubstitutions (LetDec (Rep m))
 -> Stm (Rep m) -> m (IndexSubstitutions (LetDec (Rep m))))
-> IndexSubstitutions (LetDec (Rep m))
-> Stms (Rep m)
-> m (IndexSubstitutions (LetDec (Rep m)))
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldM IndexSubstitutions (LetDec (Rep m))
-> Stm (Rep m) -> m (IndexSubstitutions (LetDec (Rep m)))
forall (m :: * -> *).
(MonadBuilder m, Buildable (Rep m), Aliased (Rep m)) =>
IndexSubstitutions (LetDec (Rep m))
-> Stm (Rep m) -> m (IndexSubstitutions (LetDec (Rep m)))
substituteIndicesInStm

substituteIndicesInStm ::
  (MonadBuilder m, Buildable (Rep m), Aliased (Rep m)) =>
  IndexSubstitutions (LetDec (Rep m)) ->
  Stm (Rep m) ->
  m (IndexSubstitutions (LetDec (Rep m)))
substituteIndicesInStm :: IndexSubstitutions (LetDec (Rep m))
-> Stm (Rep m) -> m (IndexSubstitutions (LetDec (Rep m)))
substituteIndicesInStm IndexSubstitutions (LetDec (Rep m))
substs (Let Pat (Rep m)
pat StmAux (ExpDec (Rep m))
rep Exp (Rep m)
e) = do
  Exp (Rep m)
e' <- IndexSubstitutions (LetDec (Rep m))
-> Exp (Rep m) -> m (Exp (Rep m))
forall (m :: * -> *) dec.
(MonadBuilder m, Buildable (Rep m), Aliased (Rep m),
 LetDec (Rep m) ~ dec) =>
IndexSubstitutions (LetDec (Rep m))
-> Exp (Rep m) -> m (Exp (Rep m))
substituteIndicesInExp IndexSubstitutions (LetDec (Rep m))
substs Exp (Rep m)
e
  (IndexSubstitutions (LetDec (Rep m))
substs', Pat (Rep m)
pat') <- IndexSubstitutions (LetDec (Rep m))
-> Pat (Rep m)
-> m (IndexSubstitutions (LetDec (Rep m)), Pat (Rep m))
forall (m :: * -> *) dec.
(MonadBuilder m, LetDec (Rep m) ~ dec) =>
IndexSubstitutions (LetDec (Rep m))
-> PatT dec -> m (IndexSubstitutions (LetDec (Rep m)), PatT dec)
substituteIndicesInPat IndexSubstitutions (LetDec (Rep m))
substs Pat (Rep m)
pat
  Stm (Rep m) -> m ()
forall (m :: * -> *). MonadBuilder m => Stm (Rep m) -> m ()
addStm (Stm (Rep m) -> m ()) -> Stm (Rep m) -> m ()
forall a b. (a -> b) -> a -> b
$ Pat (Rep m)
-> StmAux (ExpDec (Rep m)) -> Exp (Rep m) -> Stm (Rep m)
forall rep. Pat rep -> StmAux (ExpDec rep) -> Exp rep -> Stm rep
Let Pat (Rep m)
pat' StmAux (ExpDec (Rep m))
rep Exp (Rep m)
e'
  IndexSubstitutions (LetDec (Rep m))
-> m (IndexSubstitutions (LetDec (Rep m)))
forall (m :: * -> *) a. Monad m => a -> m a
return IndexSubstitutions (LetDec (Rep m))
substs'

substituteIndicesInPat ::
  (MonadBuilder m, LetDec (Rep m) ~ dec) =>
  IndexSubstitutions (LetDec (Rep m)) ->
  PatT dec ->
  m (IndexSubstitutions (LetDec (Rep m)), PatT dec)
substituteIndicesInPat :: IndexSubstitutions (LetDec (Rep m))
-> PatT dec -> m (IndexSubstitutions (LetDec (Rep m)), PatT dec)
substituteIndicesInPat IndexSubstitutions (LetDec (Rep m))
substs PatT dec
pat = do
  (IndexSubstitutions dec
substs', [PatElemT dec]
pes) <- (IndexSubstitutions dec
 -> PatElemT dec -> m (IndexSubstitutions dec, PatElemT dec))
-> IndexSubstitutions dec
-> [PatElemT dec]
-> m (IndexSubstitutions dec, [PatElemT dec])
forall (m :: * -> *) acc x y.
Monad m =>
(acc -> x -> m (acc, y)) -> acc -> [x] -> m (acc, [y])
mapAccumLM IndexSubstitutions dec
-> PatElemT dec -> m (IndexSubstitutions dec, PatElemT dec)
forall (m :: * -> *) a b. Monad m => a -> b -> m (a, b)
sub IndexSubstitutions dec
IndexSubstitutions (LetDec (Rep m))
substs ([PatElemT dec] -> m (IndexSubstitutions dec, [PatElemT dec]))
-> [PatElemT dec] -> m (IndexSubstitutions dec, [PatElemT dec])
forall a b. (a -> b) -> a -> b
$ PatT dec -> [PatElemT dec]
forall dec. PatT dec -> [PatElemT dec]
patElems PatT dec
pat
  (IndexSubstitutions dec, PatT dec)
-> m (IndexSubstitutions dec, PatT dec)
forall (m :: * -> *) a. Monad m => a -> m a
return (IndexSubstitutions dec
substs', [PatElemT dec] -> PatT dec
forall dec. [PatElemT dec] -> PatT dec
Pat [PatElemT dec]
pes)
  where
    sub :: a -> b -> m (a, b)
sub a
substs' b
patElem = (a, b) -> m (a, b)
forall (m :: * -> *) a. Monad m => a -> m a
return (a
substs', b
patElem)

substituteIndicesInExp ::
  ( MonadBuilder m,
    Buildable (Rep m),
    Aliased (Rep m),
    LetDec (Rep m) ~ dec
  ) =>
  IndexSubstitutions (LetDec (Rep m)) ->
  Exp (Rep m) ->
  m (Exp (Rep m))
substituteIndicesInExp :: IndexSubstitutions (LetDec (Rep m))
-> Exp (Rep m) -> m (Exp (Rep m))
substituteIndicesInExp IndexSubstitutions (LetDec (Rep m))
substs (Op Op (Rep m)
op) = do
  let used_in_op :: [(VName, (Certs, VName, dec, Slice SubExp))]
used_in_op = ((VName, (Certs, VName, dec, Slice SubExp)) -> Bool)
-> [(VName, (Certs, VName, dec, Slice SubExp))]
-> [(VName, (Certs, VName, dec, Slice SubExp))]
forall a. (a -> Bool) -> [a] -> [a]
filter ((VName -> Names -> Bool
`nameIn` Op (Rep m) -> Names
forall a. FreeIn a => a -> Names
freeIn Op (Rep m)
op) (VName -> Bool)
-> ((VName, (Certs, VName, dec, Slice SubExp)) -> VName)
-> (VName, (Certs, VName, dec, Slice SubExp))
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (VName, (Certs, VName, dec, Slice SubExp)) -> VName
forall a b. (a, b) -> a
fst) [(VName, (Certs, VName, dec, Slice SubExp))]
IndexSubstitutions (LetDec (Rep m))
substs
  Map VName VName
var_substs <- ([Map VName VName] -> Map VName VName)
-> m [Map VName VName] -> m (Map VName VName)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [Map VName VName] -> Map VName VName
forall a. Monoid a => [a] -> a
mconcat (m [Map VName VName] -> m (Map VName VName))
-> m [Map VName VName] -> m (Map VName VName)
forall a b. (a -> b) -> a -> b
$
    [(VName, (Certs, VName, dec, Slice SubExp))]
-> ((VName, (Certs, VName, dec, Slice SubExp))
    -> m (Map VName VName))
-> m [Map VName VName]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [(VName, (Certs, VName, dec, Slice SubExp))]
used_in_op (((VName, (Certs, VName, dec, Slice SubExp))
  -> m (Map VName VName))
 -> m [Map VName VName])
-> ((VName, (Certs, VName, dec, Slice SubExp))
    -> m (Map VName VName))
-> m [Map VName VName]
forall a b. (a -> b) -> a -> b
$ \(VName
v, (Certs
cs, VName
src, dec
src_dec, Slice [DimIndex SubExp]
is)) -> do
      VName
v' <-
        Certs -> m VName -> m VName
forall (m :: * -> *) a. MonadBuilder m => Certs -> m a -> m a
certifying Certs
cs (m VName -> m VName) -> m VName -> m VName
forall a b. (a -> b) -> a -> b
$
          String -> Exp (Rep m) -> m VName
forall (m :: * -> *).
MonadBuilder m =>
String -> Exp (Rep m) -> m VName
letExp String
"idx" (Exp (Rep m) -> m VName) -> Exp (Rep m) -> m VName
forall a b. (a -> b) -> a -> b
$ BasicOp -> Exp (Rep m)
forall rep. BasicOp -> ExpT rep
BasicOp (BasicOp -> Exp (Rep m)) -> BasicOp -> Exp (Rep m)
forall a b. (a -> b) -> a -> b
$ VName -> Slice SubExp -> BasicOp
Index VName
src (Slice SubExp -> BasicOp) -> Slice SubExp -> BasicOp
forall a b. (a -> b) -> a -> b
$ Type -> [DimIndex SubExp] -> Slice SubExp
fullSlice (dec -> Type
forall t. Typed t => t -> Type
typeOf dec
src_dec) [DimIndex SubExp]
is
      Map VName VName -> m (Map VName VName)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Map VName VName -> m (Map VName VName))
-> Map VName VName -> m (Map VName VName)
forall a b. (a -> b) -> a -> b
$ VName -> VName -> Map VName VName
forall k a. k -> a -> Map k a
M.singleton VName
v VName
v'
  Exp (Rep m) -> m (Exp (Rep m))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp (Rep m) -> m (Exp (Rep m))) -> Exp (Rep m) -> m (Exp (Rep m))
forall a b. (a -> b) -> a -> b
$ Op (Rep m) -> Exp (Rep m)
forall rep. Op rep -> ExpT rep
Op (Op (Rep m) -> Exp (Rep m)) -> Op (Rep m) -> Exp (Rep m)
forall a b. (a -> b) -> a -> b
$ Map VName VName -> Op (Rep m) -> Op (Rep m)
forall a. Substitute a => Map VName VName -> a -> a
substituteNames Map VName VName
var_substs Op (Rep m)
op
substituteIndicesInExp IndexSubstitutions (LetDec (Rep m))
substs Exp (Rep m)
e = do
  [(VName, (Certs, VName, dec, Slice SubExp))]
substs' <- Exp (Rep m) -> m [(VName, (Certs, VName, dec, Slice SubExp))]
copyAnyConsumed Exp (Rep m)
e
  let substitute :: Mapper (Rep m) (Rep m) m
substitute =
        Mapper (Rep m) (Rep m) m
forall (m :: * -> *) rep. Monad m => Mapper rep rep m
identityMapper
          { mapOnSubExp :: SubExp -> m SubExp
mapOnSubExp = IndexSubstitutions (LetDec (Rep m)) -> SubExp -> m SubExp
forall (m :: * -> *).
MonadBuilder m =>
IndexSubstitutions (LetDec (Rep m)) -> SubExp -> m SubExp
substituteIndicesInSubExp [(VName, (Certs, VName, dec, Slice SubExp))]
IndexSubstitutions (LetDec (Rep m))
substs',
            mapOnVName :: VName -> m VName
mapOnVName = IndexSubstitutions (LetDec (Rep m)) -> VName -> m VName
forall (m :: * -> *).
MonadBuilder m =>
IndexSubstitutions (LetDec (Rep m)) -> VName -> m VName
substituteIndicesInVar [(VName, (Certs, VName, dec, Slice SubExp))]
IndexSubstitutions (LetDec (Rep m))
substs',
            mapOnBody :: Scope (Rep m) -> Body (Rep m) -> m (Body (Rep m))
mapOnBody = (Body (Rep m) -> m (Body (Rep m)))
-> Scope (Rep m) -> Body (Rep m) -> m (Body (Rep m))
forall a b. a -> b -> a
const ((Body (Rep m) -> m (Body (Rep m)))
 -> Scope (Rep m) -> Body (Rep m) -> m (Body (Rep m)))
-> (Body (Rep m) -> m (Body (Rep m)))
-> Scope (Rep m)
-> Body (Rep m)
-> m (Body (Rep m))
forall a b. (a -> b) -> a -> b
$ IndexSubstitutions (LetDec (Rep m))
-> Body (Rep m) -> m (Body (Rep m))
forall (m :: * -> *).
(MonadBuilder m, Buildable (Rep m), Aliased (Rep m)) =>
IndexSubstitutions (LetDec (Rep m))
-> Body (Rep m) -> m (Body (Rep m))
substituteIndicesInBody [(VName, (Certs, VName, dec, Slice SubExp))]
IndexSubstitutions (LetDec (Rep m))
substs'
          }

  Mapper (Rep m) (Rep m) m -> Exp (Rep m) -> m (Exp (Rep m))
forall (m :: * -> *) frep trep.
(Applicative m, Monad m) =>
Mapper frep trep m -> Exp frep -> m (Exp trep)
mapExpM Mapper (Rep m) (Rep m) m
substitute Exp (Rep m)
e
  where
    copyAnyConsumed :: Exp (Rep m) -> m [(VName, (Certs, VName, dec, Slice SubExp))]
copyAnyConsumed =
      let consumingSubst :: [(VName, (Certs, VName, dec, Slice SubExp))]
-> VName -> m [(VName, (Certs, VName, dec, Slice SubExp))]
consumingSubst [(VName, (Certs, VName, dec, Slice SubExp))]
substs' VName
v
            | Just (Certs
cs2, VName
src2, dec
src2dec, Slice SubExp
is2) <- VName
-> [(VName, (Certs, VName, dec, Slice SubExp))]
-> Maybe (Certs, VName, dec, Slice SubExp)
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup VName
v [(VName, (Certs, VName, dec, Slice SubExp))]
IndexSubstitutions (LetDec (Rep m))
substs = do
              VName
row <-
                Certs -> m VName -> m VName
forall (m :: * -> *) a. MonadBuilder m => Certs -> m a -> m a
certifying Certs
cs2 (m VName -> m VName) -> m VName -> m VName
forall a b. (a -> b) -> a -> b
$
                  String -> Exp (Rep m) -> m VName
forall (m :: * -> *).
MonadBuilder m =>
String -> Exp (Rep m) -> m VName
letExp (VName -> String
baseString VName
v String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"_row") (Exp (Rep m) -> m VName) -> Exp (Rep m) -> m VName
forall a b. (a -> b) -> a -> b
$
                    BasicOp -> Exp (Rep m)
forall rep. BasicOp -> ExpT rep
BasicOp (BasicOp -> Exp (Rep m)) -> BasicOp -> Exp (Rep m)
forall a b. (a -> b) -> a -> b
$ VName -> Slice SubExp -> BasicOp
Index VName
src2 (Slice SubExp -> BasicOp) -> Slice SubExp -> BasicOp
forall a b. (a -> b) -> a -> b
$ Type -> [DimIndex SubExp] -> Slice SubExp
fullSlice (dec -> Type
forall t. Typed t => t -> Type
typeOf dec
src2dec) (Slice SubExp -> [DimIndex SubExp]
forall d. Slice d -> [DimIndex d]
unSlice Slice SubExp
is2)
              VName
row_copy <-
                String -> Exp (Rep m) -> m VName
forall (m :: * -> *).
MonadBuilder m =>
String -> Exp (Rep m) -> m VName
letExp (VName -> String
baseString VName
v String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"_row_copy") (Exp (Rep m) -> m VName) -> Exp (Rep m) -> m VName
forall a b. (a -> b) -> a -> b
$ BasicOp -> Exp (Rep m)
forall rep. BasicOp -> ExpT rep
BasicOp (BasicOp -> Exp (Rep m)) -> BasicOp -> Exp (Rep m)
forall a b. (a -> b) -> a -> b
$ VName -> BasicOp
Copy VName
row
              [(VName, (Certs, VName, dec, Slice SubExp))]
-> m [(VName, (Certs, VName, dec, Slice SubExp))]
forall (m :: * -> *) a. Monad m => a -> m a
return ([(VName, (Certs, VName, dec, Slice SubExp))]
 -> m [(VName, (Certs, VName, dec, Slice SubExp))])
-> [(VName, (Certs, VName, dec, Slice SubExp))]
-> m [(VName, (Certs, VName, dec, Slice SubExp))]
forall a b. (a -> b) -> a -> b
$
                VName
-> VName
-> (Certs, VName, dec, Slice SubExp)
-> [(VName, (Certs, VName, dec, Slice SubExp))]
-> [(VName, (Certs, VName, dec, Slice SubExp))]
forall dec.
VName
-> VName
-> IndexSubstitution dec
-> IndexSubstitutions dec
-> IndexSubstitutions dec
update
                  VName
v
                  VName
v
                  ( Certs
forall a. Monoid a => a
mempty,
                    VName
row_copy,
                    dec
src2dec
                      dec -> Type -> dec
forall a. SetType a => a -> Type -> a
`setType` ( dec -> Type
forall t. Typed t => t -> Type
typeOf dec
src2dec
                                    Type -> [SubExp] -> Type
forall oldshape u.
TypeBase oldshape u -> [SubExp] -> TypeBase Shape u
`setArrayDims` Slice SubExp -> [SubExp]
forall d. Slice d -> [d]
sliceDims Slice SubExp
is2
                                ),
                    [DimIndex SubExp] -> Slice SubExp
forall d. [DimIndex d] -> Slice d
Slice []
                  )
                  [(VName, (Certs, VName, dec, Slice SubExp))]
substs'
          consumingSubst [(VName, (Certs, VName, dec, Slice SubExp))]
substs' VName
_ =
            [(VName, (Certs, VName, dec, Slice SubExp))]
-> m [(VName, (Certs, VName, dec, Slice SubExp))]
forall (m :: * -> *) a. Monad m => a -> m a
return [(VName, (Certs, VName, dec, Slice SubExp))]
substs'
       in ([(VName, (Certs, VName, dec, Slice SubExp))]
 -> VName -> m [(VName, (Certs, VName, dec, Slice SubExp))])
-> [(VName, (Certs, VName, dec, Slice SubExp))]
-> [VName]
-> m [(VName, (Certs, VName, dec, Slice SubExp))]
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldM [(VName, (Certs, VName, dec, Slice SubExp))]
-> VName -> m [(VName, (Certs, VName, dec, Slice SubExp))]
consumingSubst [(VName, (Certs, VName, dec, Slice SubExp))]
IndexSubstitutions (LetDec (Rep m))
substs ([VName] -> m [(VName, (Certs, VName, dec, Slice SubExp))])
-> (Exp (Rep m) -> [VName])
-> Exp (Rep m)
-> m [(VName, (Certs, VName, dec, Slice SubExp))]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Names -> [VName]
namesToList (Names -> [VName])
-> (Exp (Rep m) -> Names) -> Exp (Rep m) -> [VName]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Exp (Rep m) -> Names
forall rep. Aliased rep => Exp rep -> Names
consumedInExp

substituteIndicesInSubExp ::
  MonadBuilder m =>
  IndexSubstitutions (LetDec (Rep m)) ->
  SubExp ->
  m SubExp
substituteIndicesInSubExp :: IndexSubstitutions (LetDec (Rep m)) -> SubExp -> m SubExp
substituteIndicesInSubExp IndexSubstitutions (LetDec (Rep m))
substs (Var VName
v) =
  VName -> SubExp
Var (VName -> SubExp) -> m VName -> m SubExp
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IndexSubstitutions (LetDec (Rep m)) -> VName -> m VName
forall (m :: * -> *).
MonadBuilder m =>
IndexSubstitutions (LetDec (Rep m)) -> VName -> m VName
substituteIndicesInVar IndexSubstitutions (LetDec (Rep m))
substs VName
v
substituteIndicesInSubExp IndexSubstitutions (LetDec (Rep m))
_ SubExp
se =
  SubExp -> m SubExp
forall (m :: * -> *) a. Monad m => a -> m a
return SubExp
se

substituteIndicesInVar ::
  MonadBuilder m =>
  IndexSubstitutions (LetDec (Rep m)) ->
  VName ->
  m VName
substituteIndicesInVar :: IndexSubstitutions (LetDec (Rep m)) -> VName -> m VName
substituteIndicesInVar IndexSubstitutions (LetDec (Rep m))
substs VName
v
  | Just (Certs
cs2, VName
src2, LetDec (Rep m)
_, Slice []) <- VName
-> IndexSubstitutions (LetDec (Rep m))
-> Maybe (Certs, VName, LetDec (Rep m), Slice SubExp)
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup VName
v IndexSubstitutions (LetDec (Rep m))
substs =
    Certs -> m VName -> m VName
forall (m :: * -> *) a. MonadBuilder m => Certs -> m a -> m a
certifying Certs
cs2 (m VName -> m VName) -> m VName -> m VName
forall a b. (a -> b) -> a -> b
$
      String -> Exp (Rep m) -> m VName
forall (m :: * -> *).
MonadBuilder m =>
String -> Exp (Rep m) -> m VName
letExp (VName -> String
baseString VName
src2) (Exp (Rep m) -> m VName) -> Exp (Rep m) -> m VName
forall a b. (a -> b) -> a -> b
$ BasicOp -> Exp (Rep m)
forall rep. BasicOp -> ExpT rep
BasicOp (BasicOp -> Exp (Rep m)) -> BasicOp -> Exp (Rep m)
forall a b. (a -> b) -> a -> b
$ SubExp -> BasicOp
SubExp (SubExp -> BasicOp) -> SubExp -> BasicOp
forall a b. (a -> b) -> a -> b
$ VName -> SubExp
Var VName
src2
  | Just (Certs
cs2, VName
src2, LetDec (Rep m)
src2_dec, Slice [DimIndex SubExp]
is2) <- VName
-> IndexSubstitutions (LetDec (Rep m))
-> Maybe (Certs, VName, LetDec (Rep m), Slice SubExp)
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup VName
v IndexSubstitutions (LetDec (Rep m))
substs =
    Certs -> m VName -> m VName
forall (m :: * -> *) a. MonadBuilder m => Certs -> m a -> m a
certifying Certs
cs2 (m VName -> m VName) -> m VName -> m VName
forall a b. (a -> b) -> a -> b
$
      String -> Exp (Rep m) -> m VName
forall (m :: * -> *).
MonadBuilder m =>
String -> Exp (Rep m) -> m VName
letExp String
"idx" (Exp (Rep m) -> m VName) -> Exp (Rep m) -> m VName
forall a b. (a -> b) -> a -> b
$ BasicOp -> Exp (Rep m)
forall rep. BasicOp -> ExpT rep
BasicOp (BasicOp -> Exp (Rep m)) -> BasicOp -> Exp (Rep m)
forall a b. (a -> b) -> a -> b
$ VName -> Slice SubExp -> BasicOp
Index VName
src2 (Slice SubExp -> BasicOp) -> Slice SubExp -> BasicOp
forall a b. (a -> b) -> a -> b
$ Type -> [DimIndex SubExp] -> Slice SubExp
fullSlice (LetDec (Rep m) -> Type
forall t. Typed t => t -> Type
typeOf LetDec (Rep m)
src2_dec) [DimIndex SubExp]
is2
  | Bool
otherwise =
    VName -> m VName
forall (m :: * -> *) a. Monad m => a -> m a
return VName
v

substituteIndicesInBody ::
  (MonadBuilder m, Buildable (Rep m), Aliased (Rep m)) =>
  IndexSubstitutions (LetDec (Rep m)) ->
  Body (Rep m) ->
  m (Body (Rep m))
substituteIndicesInBody :: IndexSubstitutions (LetDec (Rep m))
-> Body (Rep m) -> m (Body (Rep m))
substituteIndicesInBody IndexSubstitutions (LetDec (Rep m))
substs (Body BodyDec (Rep m)
_ Stms (Rep m)
stms Result
res) = do
  (IndexSubstitutions (LetDec (Rep m))
substs', Stms (Rep m)
stms') <-
    Stms (Rep m)
-> m (IndexSubstitutions (LetDec (Rep m)), Stms (Rep m))
-> m (IndexSubstitutions (LetDec (Rep m)), Stms (Rep m))
forall rep a (m :: * -> *) b.
(Scoped rep a, LocalScope rep m) =>
a -> m b -> m b
inScopeOf Stms (Rep m)
stms (m (IndexSubstitutions (LetDec (Rep m)), Stms (Rep m))
 -> m (IndexSubstitutions (LetDec (Rep m)), Stms (Rep m)))
-> m (IndexSubstitutions (LetDec (Rep m)), Stms (Rep m))
-> m (IndexSubstitutions (LetDec (Rep m)), Stms (Rep m))
forall a b. (a -> b) -> a -> b
$
      m (IndexSubstitutions (LetDec (Rep m)))
-> m (IndexSubstitutions (LetDec (Rep m)), Stms (Rep m))
forall (m :: * -> *) a.
MonadBuilder m =>
m a -> m (a, Stms (Rep m))
collectStms (m (IndexSubstitutions (LetDec (Rep m)))
 -> m (IndexSubstitutions (LetDec (Rep m)), Stms (Rep m)))
-> m (IndexSubstitutions (LetDec (Rep m)))
-> m (IndexSubstitutions (LetDec (Rep m)), Stms (Rep m))
forall a b. (a -> b) -> a -> b
$ IndexSubstitutions (LetDec (Rep m))
-> Stms (Rep m) -> m (IndexSubstitutions (LetDec (Rep m)))
forall (m :: * -> *).
(MonadBuilder m, Buildable (Rep m), Aliased (Rep m)) =>
IndexSubstitutions (LetDec (Rep m))
-> Stms (Rep m) -> m (IndexSubstitutions (LetDec (Rep m)))
substituteIndicesInStms IndexSubstitutions (LetDec (Rep m))
substs Stms (Rep m)
stms
  (Result
res', Stms (Rep m)
res_stms) <-
    Stms (Rep m)
-> m (Result, Stms (Rep m)) -> m (Result, Stms (Rep m))
forall rep a (m :: * -> *) b.
(Scoped rep a, LocalScope rep m) =>
a -> m b -> m b
inScopeOf Stms (Rep m)
stms' (m (Result, Stms (Rep m)) -> m (Result, Stms (Rep m)))
-> m (Result, Stms (Rep m)) -> m (Result, Stms (Rep m))
forall a b. (a -> b) -> a -> b
$
      m Result -> m (Result, Stms (Rep m))
forall (m :: * -> *) a.
MonadBuilder m =>
m a -> m (a, Stms (Rep m))
collectStms (m Result -> m (Result, Stms (Rep m)))
-> m Result -> m (Result, Stms (Rep m))
forall a b. (a -> b) -> a -> b
$ (SubExpRes -> m SubExpRes) -> Result -> m Result
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (IndexSubstitutions (LetDec (Rep m)) -> SubExpRes -> m SubExpRes
forall (f :: * -> *).
MonadBuilder f =>
[(VName, IndexSubstitution (LetDec (Rep f)))]
-> SubExpRes -> f SubExpRes
onSubExpRes IndexSubstitutions (LetDec (Rep m))
substs') Result
res
  Stms (Rep m) -> Result -> m (Body (Rep m))
forall (m :: * -> *).
MonadBuilder m =>
Stms (Rep m) -> Result -> m (Body (Rep m))
mkBodyM (Stms (Rep m)
stms' Stms (Rep m) -> Stms (Rep m) -> Stms (Rep m)
forall a. Semigroup a => a -> a -> a
<> Stms (Rep m)
res_stms) Result
res'
  where
    onSubExpRes :: [(VName, IndexSubstitution (LetDec (Rep f)))]
-> SubExpRes -> f SubExpRes
onSubExpRes [(VName, IndexSubstitution (LetDec (Rep f)))]
substs' (SubExpRes Certs
cs SubExp
se) =
      Certs -> SubExp -> SubExpRes
SubExpRes Certs
cs (SubExp -> SubExpRes) -> f SubExp -> f SubExpRes
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [(VName, IndexSubstitution (LetDec (Rep f)))] -> SubExp -> f SubExp
forall (m :: * -> *).
MonadBuilder m =>
IndexSubstitutions (LetDec (Rep m)) -> SubExp -> m SubExp
substituteIndicesInSubExp [(VName, IndexSubstitution (LetDec (Rep f)))]
substs' SubExp
se

update ::
  VName ->
  VName ->
  IndexSubstitution dec ->
  IndexSubstitutions dec ->
  IndexSubstitutions dec
update :: VName
-> VName
-> IndexSubstitution dec
-> IndexSubstitutions dec
-> IndexSubstitutions dec
update VName
needle VName
name IndexSubstitution dec
subst ((VName
othername, IndexSubstitution dec
othersubst) : IndexSubstitutions dec
substs)
  | VName
needle VName -> VName -> Bool
forall a. Eq a => a -> a -> Bool
== VName
othername = (VName
name, IndexSubstitution dec
subst) (VName, IndexSubstitution dec)
-> IndexSubstitutions dec -> IndexSubstitutions dec
forall a. a -> [a] -> [a]
: IndexSubstitutions dec
substs
  | Bool
otherwise = (VName
othername, IndexSubstitution dec
othersubst) (VName, IndexSubstitution dec)
-> IndexSubstitutions dec -> IndexSubstitutions dec
forall a. a -> [a] -> [a]
: VName
-> VName
-> IndexSubstitution dec
-> IndexSubstitutions dec
-> IndexSubstitutions dec
forall dec.
VName
-> VName
-> IndexSubstitution dec
-> IndexSubstitutions dec
-> IndexSubstitutions dec
update VName
needle VName
name IndexSubstitution dec
subst IndexSubstitutions dec
substs
update VName
needle VName
_ IndexSubstitution dec
_ [] = String -> IndexSubstitutions dec
forall a. HasCallStack => String -> a
error (String -> IndexSubstitutions dec)
-> String -> IndexSubstitutions dec
forall a b. (a -> b) -> a -> b
$ String
"Cannot find substitution for " String -> String -> String
forall a. [a] -> [a] -> [a]
++ VName -> String
forall a. Pretty a => a -> String
pretty VName
needle