{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeFamilies #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}

module Futhark.Pass.ExplicitAllocations.SegOp
  ( allocInKernelBody,
    allocInBinOpLambda,
  )
where

import Futhark.IR.KernelsMem
import qualified Futhark.IR.Mem.IxFun as IxFun
import Futhark.Pass.ExplicitAllocations

instance SizeSubst (SegOp lvl lore) where
  opSizeSubst :: forall dec. PatternT dec -> SegOp lvl lore -> ChunkMap
opSizeSubst PatternT dec
_ SegOp lvl lore
_ = ChunkMap
forall a. Monoid a => a
mempty

allocInKernelBody ::
  Allocable fromlore tolore =>
  KernelBody fromlore ->
  AllocM fromlore tolore (KernelBody tolore)
allocInKernelBody :: forall fromlore tolore.
Allocable fromlore tolore =>
KernelBody fromlore -> AllocM fromlore tolore (KernelBody tolore)
allocInKernelBody (KernelBody () Stms fromlore
stms [KernelResult]
res) =
  ([KernelResult] -> Stms tolore -> KernelBody tolore)
-> ([KernelResult], Stms tolore) -> KernelBody tolore
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry ((Stms tolore -> [KernelResult] -> KernelBody tolore)
-> [KernelResult] -> Stms tolore -> KernelBody tolore
forall a b c. (a -> b -> c) -> b -> a -> c
flip (BodyDec tolore
-> Stms tolore -> [KernelResult] -> KernelBody tolore
forall lore.
BodyDec lore -> Stms lore -> [KernelResult] -> KernelBody lore
KernelBody ()))
    (([KernelResult], Stms tolore) -> KernelBody tolore)
-> AllocM fromlore tolore ([KernelResult], Stms tolore)
-> AllocM fromlore tolore (KernelBody tolore)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> AllocM fromlore tolore [KernelResult]
-> AllocM
     fromlore
     tolore
     ([KernelResult], Stms (Lore (AllocM fromlore tolore)))
forall (m :: * -> *) a.
MonadBinder m =>
m a -> m (a, Stms (Lore m))
collectStms (Stms fromlore
-> AllocM fromlore tolore [KernelResult]
-> AllocM fromlore tolore [KernelResult]
forall fromlore tolore a.
Allocable fromlore tolore =>
Stms fromlore
-> AllocM fromlore tolore a -> AllocM fromlore tolore a
allocInStms Stms fromlore
stms ([KernelResult] -> AllocM fromlore tolore [KernelResult]
forall (f :: * -> *) a. Applicative f => a -> f a
pure [KernelResult]
res))

allocInLambda ::
  Allocable fromlore tolore =>
  [LParam tolore] ->
  Body fromlore ->
  AllocM fromlore tolore (Lambda tolore)
allocInLambda :: forall fromlore tolore.
Allocable fromlore tolore =>
[LParam tolore]
-> Body fromlore -> AllocM fromlore tolore (Lambda tolore)
allocInLambda [LParam tolore]
params Body fromlore
body =
  [LParam (Lore (AllocM fromlore tolore))]
-> AllocM fromlore tolore Result
-> AllocM fromlore tolore (Lambda (Lore (AllocM fromlore tolore)))
forall (m :: * -> *).
MonadBinder m =>
[LParam (Lore m)] -> m Result -> m (Lambda (Lore m))
mkLambda [LParam tolore]
[LParam (Lore (AllocM fromlore tolore))]
params (AllocM fromlore tolore Result
 -> AllocM fromlore tolore (LambdaT tolore))
-> (AllocM fromlore tolore Result -> AllocM fromlore tolore Result)
-> AllocM fromlore tolore Result
-> AllocM fromlore tolore (LambdaT tolore)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Stms fromlore
-> AllocM fromlore tolore Result -> AllocM fromlore tolore Result
forall fromlore tolore a.
Allocable fromlore tolore =>
Stms fromlore
-> AllocM fromlore tolore a -> AllocM fromlore tolore a
allocInStms (Body fromlore -> Stms fromlore
forall lore. BodyT lore -> Stms lore
bodyStms Body fromlore
body) (AllocM fromlore tolore Result
 -> AllocM fromlore tolore (LambdaT tolore))
-> AllocM fromlore tolore Result
-> AllocM fromlore tolore (LambdaT tolore)
forall a b. (a -> b) -> a -> b
$
    Result -> AllocM fromlore tolore Result
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Result -> AllocM fromlore tolore Result)
-> Result -> AllocM fromlore tolore Result
forall a b. (a -> b) -> a -> b
$ Body fromlore -> Result
forall lore. BodyT lore -> Result
bodyResult Body fromlore
body

allocInBinOpParams ::
  Allocable fromlore tolore =>
  SubExp ->
  TPrimExp Int64 VName ->
  TPrimExp Int64 VName ->
  [LParam fromlore] ->
  [LParam fromlore] ->
  AllocM fromlore tolore ([LParam tolore], [LParam tolore])
allocInBinOpParams :: forall fromlore tolore.
Allocable fromlore tolore =>
SubExp
-> TPrimExp Int64 VName
-> TPrimExp Int64 VName
-> [LParam fromlore]
-> [LParam fromlore]
-> AllocM fromlore tolore ([LParam tolore], [LParam tolore])
allocInBinOpParams SubExp
num_threads TPrimExp Int64 VName
my_id TPrimExp Int64 VName
other_id [LParam fromlore]
xs [LParam fromlore]
ys = [(Param LParamMem, Param LParamMem)]
-> ([Param LParamMem], [Param LParamMem])
forall a b. [(a, b)] -> ([a], [b])
unzip ([(Param LParamMem, Param LParamMem)]
 -> ([Param LParamMem], [Param LParamMem]))
-> AllocM fromlore tolore [(Param LParamMem, Param LParamMem)]
-> AllocM fromlore tolore ([Param LParamMem], [Param LParamMem])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Param Type
 -> Param Type
 -> AllocM fromlore tolore (Param LParamMem, Param LParamMem))
-> [Param Type]
-> [Param Type]
-> AllocM fromlore tolore [(Param LParamMem, Param LParamMem)]
forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> m c) -> [a] -> [b] -> m [c]
zipWithM Param Type
-> Param Type
-> AllocM fromlore tolore (Param LParamMem, Param LParamMem)
alloc [Param Type]
[LParam fromlore]
xs [Param Type]
[LParam fromlore]
ys
  where
    alloc :: Param Type
-> Param Type
-> AllocM fromlore tolore (Param LParamMem, Param LParamMem)
alloc Param Type
x Param Type
y =
      case Param Type -> Type
forall dec. Typed dec => Param dec -> Type
paramType Param Type
x of
        Array PrimType
pt ShapeBase SubExp
shape NoUniqueness
u -> do
          SubExp
twice_num_threads <-
            String
-> Exp (Lore (AllocM fromlore tolore))
-> AllocM fromlore tolore SubExp
forall (m :: * -> *).
MonadBinder m =>
String -> Exp (Lore m) -> m SubExp
letSubExp String
"twice_num_threads" (Exp (Lore (AllocM fromlore tolore))
 -> AllocM fromlore tolore SubExp)
-> Exp (Lore (AllocM fromlore tolore))
-> AllocM fromlore tolore SubExp
forall a b. (a -> b) -> a -> b
$
              BasicOp -> ExpT tolore
forall lore. BasicOp -> ExpT lore
BasicOp (BasicOp -> ExpT tolore) -> BasicOp -> ExpT tolore
forall a b. (a -> b) -> a -> b
$ BinOp -> SubExp -> SubExp -> BasicOp
BinOp (IntType -> Overflow -> BinOp
Mul IntType
Int64 Overflow
OverflowUndef) SubExp
num_threads (SubExp -> BasicOp) -> SubExp -> BasicOp
forall a b. (a -> b) -> a -> b
$ IntType -> Integer -> SubExp
intConst IntType
Int64 Integer
2
          let t :: Type
t = Param Type -> Type
forall dec. Typed dec => Param dec -> Type
paramType Param Type
x Type -> SubExp -> Type
forall d.
ArrayShape (ShapeBase d) =>
TypeBase (ShapeBase d) NoUniqueness
-> d -> TypeBase (ShapeBase d) NoUniqueness
`arrayOfRow` SubExp
twice_num_threads
          VName
mem <- Type -> Space -> AllocM fromlore tolore VName
forall lore (m :: * -> *).
Allocator lore m =>
Type -> Space -> m VName
allocForArray Type
t Space
DefaultSpace
          -- XXX: this iota ixfun is a bit inefficient; leading to
          -- uncoalesced access.
          let base_dims :: [TPrimExp Int64 VName]
base_dims = (SubExp -> TPrimExp Int64 VName)
-> Result -> [TPrimExp Int64 VName]
forall a b. (a -> b) -> [a] -> [b]
map SubExp -> TPrimExp Int64 VName
pe64 (Result -> [TPrimExp Int64 VName])
-> Result -> [TPrimExp Int64 VName]
forall a b. (a -> b) -> a -> b
$ Type -> Result
forall u. TypeBase (ShapeBase SubExp) u -> Result
arrayDims Type
t
              ixfun_base :: IxFun (TPrimExp Int64 VName)
ixfun_base = [TPrimExp Int64 VName] -> IxFun (TPrimExp Int64 VName)
forall num. IntegralExp num => Shape num -> IxFun num
IxFun.iota [TPrimExp Int64 VName]
base_dims
              ixfun_x :: IxFun (TPrimExp Int64 VName)
ixfun_x =
                IxFun (TPrimExp Int64 VName)
-> Slice (TPrimExp Int64 VName) -> IxFun (TPrimExp Int64 VName)
forall num.
(Eq num, IntegralExp num) =>
IxFun num -> Slice num -> IxFun num
IxFun.slice IxFun (TPrimExp Int64 VName)
ixfun_base (Slice (TPrimExp Int64 VName) -> IxFun (TPrimExp Int64 VName))
-> Slice (TPrimExp Int64 VName) -> IxFun (TPrimExp Int64 VName)
forall a b. (a -> b) -> a -> b
$
                  [TPrimExp Int64 VName]
-> Slice (TPrimExp Int64 VName) -> Slice (TPrimExp Int64 VName)
forall d. Num d => [d] -> [DimIndex d] -> [DimIndex d]
fullSliceNum [TPrimExp Int64 VName]
base_dims [TPrimExp Int64 VName -> DimIndex (TPrimExp Int64 VName)
forall d. d -> DimIndex d
DimFix TPrimExp Int64 VName
my_id]
              ixfun_y :: IxFun (TPrimExp Int64 VName)
ixfun_y =
                IxFun (TPrimExp Int64 VName)
-> Slice (TPrimExp Int64 VName) -> IxFun (TPrimExp Int64 VName)
forall num.
(Eq num, IntegralExp num) =>
IxFun num -> Slice num -> IxFun num
IxFun.slice IxFun (TPrimExp Int64 VName)
ixfun_base (Slice (TPrimExp Int64 VName) -> IxFun (TPrimExp Int64 VName))
-> Slice (TPrimExp Int64 VName) -> IxFun (TPrimExp Int64 VName)
forall a b. (a -> b) -> a -> b
$
                  [TPrimExp Int64 VName]
-> Slice (TPrimExp Int64 VName) -> Slice (TPrimExp Int64 VName)
forall d. Num d => [d] -> [DimIndex d] -> [DimIndex d]
fullSliceNum [TPrimExp Int64 VName]
base_dims [TPrimExp Int64 VName -> DimIndex (TPrimExp Int64 VName)
forall d. d -> DimIndex d
DimFix TPrimExp Int64 VName
other_id]
          (Param LParamMem, Param LParamMem)
-> AllocM fromlore tolore (Param LParamMem, Param LParamMem)
forall (m :: * -> *) a. Monad m => a -> m a
return
            ( Param Type
x {paramDec :: LParamMem
paramDec = PrimType
-> ShapeBase SubExp -> NoUniqueness -> MemBind -> LParamMem
forall d u ret.
PrimType -> ShapeBase d -> u -> ret -> MemInfo d u ret
MemArray PrimType
pt ShapeBase SubExp
shape NoUniqueness
u (MemBind -> LParamMem) -> MemBind -> LParamMem
forall a b. (a -> b) -> a -> b
$ VName -> IxFun (TPrimExp Int64 VName) -> MemBind
ArrayIn VName
mem IxFun (TPrimExp Int64 VName)
ixfun_x},
              Param Type
y {paramDec :: LParamMem
paramDec = PrimType
-> ShapeBase SubExp -> NoUniqueness -> MemBind -> LParamMem
forall d u ret.
PrimType -> ShapeBase d -> u -> ret -> MemInfo d u ret
MemArray PrimType
pt ShapeBase SubExp
shape NoUniqueness
u (MemBind -> LParamMem) -> MemBind -> LParamMem
forall a b. (a -> b) -> a -> b
$ VName -> IxFun (TPrimExp Int64 VName) -> MemBind
ArrayIn VName
mem IxFun (TPrimExp Int64 VName)
ixfun_y}
            )
        Prim PrimType
bt ->
          (Param LParamMem, Param LParamMem)
-> AllocM fromlore tolore (Param LParamMem, Param LParamMem)
forall (m :: * -> *) a. Monad m => a -> m a
return
            ( Param Type
x {paramDec :: LParamMem
paramDec = PrimType -> LParamMem
forall d u ret. PrimType -> MemInfo d u ret
MemPrim PrimType
bt},
              Param Type
y {paramDec :: LParamMem
paramDec = PrimType -> LParamMem
forall d u ret. PrimType -> MemInfo d u ret
MemPrim PrimType
bt}
            )
        Mem Space
space ->
          (Param LParamMem, Param LParamMem)
-> AllocM fromlore tolore (Param LParamMem, Param LParamMem)
forall (m :: * -> *) a. Monad m => a -> m a
return
            ( Param Type
x {paramDec :: LParamMem
paramDec = Space -> LParamMem
forall d u ret. Space -> MemInfo d u ret
MemMem Space
space},
              Param Type
y {paramDec :: LParamMem
paramDec = Space -> LParamMem
forall d u ret. Space -> MemInfo d u ret
MemMem Space
space}
            )
        -- This next case will never happen.
        Acc VName
acc ShapeBase SubExp
ispace [Type]
ts NoUniqueness
u ->
          (Param LParamMem, Param LParamMem)
-> AllocM fromlore tolore (Param LParamMem, Param LParamMem)
forall (m :: * -> *) a. Monad m => a -> m a
return
            ( Param Type
x {paramDec :: LParamMem
paramDec = VName -> ShapeBase SubExp -> [Type] -> NoUniqueness -> LParamMem
forall d u ret.
VName -> ShapeBase SubExp -> [Type] -> u -> MemInfo d u ret
MemAcc VName
acc ShapeBase SubExp
ispace [Type]
ts NoUniqueness
u},
              Param Type
y {paramDec :: LParamMem
paramDec = VName -> ShapeBase SubExp -> [Type] -> NoUniqueness -> LParamMem
forall d u ret.
VName -> ShapeBase SubExp -> [Type] -> u -> MemInfo d u ret
MemAcc VName
acc ShapeBase SubExp
ispace [Type]
ts NoUniqueness
u}
            )

allocInBinOpLambda ::
  Allocable fromlore tolore =>
  SubExp ->
  SegSpace ->
  Lambda fromlore ->
  AllocM fromlore tolore (Lambda tolore)
allocInBinOpLambda :: forall fromlore tolore.
Allocable fromlore tolore =>
SubExp
-> SegSpace
-> Lambda fromlore
-> AllocM fromlore tolore (Lambda tolore)
allocInBinOpLambda SubExp
num_threads (SegSpace VName
flat [(VName, SubExp)]
_) Lambda fromlore
lam = do
  let ([Param Type]
acc_params, [Param Type]
arr_params) =
        Int -> [Param Type] -> ([Param Type], [Param Type])
forall a. Int -> [a] -> ([a], [a])
splitAt ([Param Type] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length (Lambda fromlore -> [LParam fromlore]
forall lore. LambdaT lore -> [LParam lore]
lambdaParams Lambda fromlore
lam) Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
2) ([Param Type] -> ([Param Type], [Param Type]))
-> [Param Type] -> ([Param Type], [Param Type])
forall a b. (a -> b) -> a -> b
$ Lambda fromlore -> [LParam fromlore]
forall lore. LambdaT lore -> [LParam lore]
lambdaParams Lambda fromlore
lam
      index_x :: TPrimExp Int64 VName
index_x = PrimExp VName -> TPrimExp Int64 VName
forall t v. PrimExp v -> TPrimExp t v
TPrimExp (PrimExp VName -> TPrimExp Int64 VName)
-> PrimExp VName -> TPrimExp Int64 VName
forall a b. (a -> b) -> a -> b
$ VName -> PrimType -> PrimExp VName
forall v. v -> PrimType -> PrimExp v
LeafExp VName
flat PrimType
int64
      index_y :: TPrimExp Int64 VName
index_y = TPrimExp Int64 VName
index_x TPrimExp Int64 VName
-> TPrimExp Int64 VName -> TPrimExp Int64 VName
forall a. Num a => a -> a -> a
+ SubExp -> TPrimExp Int64 VName
pe64 SubExp
num_threads
  ([Param LParamMem]
acc_params', [Param LParamMem]
arr_params') <-
    SubExp
-> TPrimExp Int64 VName
-> TPrimExp Int64 VName
-> [LParam fromlore]
-> [LParam fromlore]
-> AllocM fromlore tolore ([LParam tolore], [LParam tolore])
forall fromlore tolore.
Allocable fromlore tolore =>
SubExp
-> TPrimExp Int64 VName
-> TPrimExp Int64 VName
-> [LParam fromlore]
-> [LParam fromlore]
-> AllocM fromlore tolore ([LParam tolore], [LParam tolore])
allocInBinOpParams SubExp
num_threads TPrimExp Int64 VName
index_x TPrimExp Int64 VName
index_y [Param Type]
[LParam fromlore]
acc_params [Param Type]
[LParam fromlore]
arr_params

  [LParam tolore]
-> Body fromlore -> AllocM fromlore tolore (Lambda tolore)
forall fromlore tolore.
Allocable fromlore tolore =>
[LParam tolore]
-> Body fromlore -> AllocM fromlore tolore (Lambda tolore)
allocInLambda ([Param LParamMem]
acc_params' [Param LParamMem] -> [Param LParamMem] -> [Param LParamMem]
forall a. [a] -> [a] -> [a]
++ [Param LParamMem]
arr_params') (Lambda fromlore -> Body fromlore
forall lore. LambdaT lore -> BodyT lore
lambdaBody Lambda fromlore
lam)