    {-# LANGUAGE TypeFamilies #-}

    module Futhark.Pass.ExtractKernels.BlockedKernel
      ( DistRep,
        MkSegLevel,
        ThreadRecommendation (..),
        segRed,
        nonSegRed,
        segScan,
        segHist,
        segMap,
        mapKernel,
        KernelInput (..),
        readKernelInput,
        mkSegSpace,
        dummyDim,
      )
    where

    import Control.Monad
    import Futhark.Analysis.PrimExp
    import Futhark.IR
    import Futhark.IR.Aliases (AliasableRep)
    import Futhark.IR.GPU.Op (SegVirt (..))
    import Futhark.IR.SegOp
    import Futhark.MonadFreshNames
    import Futhark.Tools
    import Futhark.Transform.Rename
    import Prelude hiding (quot)

    -- | Constraints pertinent to performing distribution/flattening.
    type DistRep rep =
      ( Buildable rep,
        HasSegOp rep,
        BuilderOps rep,
        LetDec rep ~ Type,
        ExpDec rep ~ (),
        BodyDec rep ~ (),
        AliasableRep rep
      )

    data ThreadRecommendation = ManyThreads | NoRecommendation SegVirt

    type MkSegLevel rep m =
      [SubExp] -> String -> ThreadRecommendation -> BuilderT rep m (SegOpLevel rep)

    mkSegSpace :: (MonadFreshNames m) => [(VName, SubExp)] -> m SegSpace
    mkSegSpace :: forall (m :: * -> *).
    MonadFreshNames m =>
    [(VName, SubExp)] -> m SegSpace
    mkSegSpace [(VName, SubExp)]
    dims = VName -> [(VName, SubExp)] -> SegSpace
    SegSpace (VName -> [(VName, SubExp)] -> SegSpace)
    -> m VName -> m ([(VName, SubExp)] -> SegSpace)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> String -> m VName
    forall (m :: * -> *). MonadFreshNames m => String -> m VName
    newVName String
    "phys_tid" m ([(VName, SubExp)] -> SegSpace)
    -> m [(VName, SubExp)] -> m SegSpace
    forall a b. m (a -> b) -> m a -> m b
    forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
    <*> [(VName, SubExp)] -> m [(VName, SubExp)]
    forall a. a -> m a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure [(VName, SubExp)]
    dims

    prepareRedOrScan ::
      (MonadBuilder m, DistRep (Rep m)) =>
      Certs ->
      SubExp ->
      Lambda (Rep m) ->
      [VName] ->
      [(VName, SubExp)] ->
      [KernelInput] ->
      m (SegSpace, KernelBody (Rep m))
    prepareRedOrScan :: forall (m :: * -> *).
    (MonadBuilder m, DistRep (Rep m)) =>
    Certs
    -> SubExp
    -> Lambda (Rep m)
    -> [VName]
    -> [(VName, SubExp)]
    -> [KernelInput]
    -> m (SegSpace, KernelBody (Rep m))
    prepareRedOrScan Certs
    cs SubExp
    w Lambda (Rep m)
    map_lam [VName]
    arrs [(VName, SubExp)]
    ispace [KernelInput]
    inps = do
      gtid <- String -> m VName
    forall (m :: * -> *). MonadFreshNames m => String -> m VName
    newVName String
    "gtid"
      space <- mkSegSpace $ ispace ++ [(gtid, w)]
      kbody <- fmap (uncurry (flip (KernelBody ()))) $
        runBuilder $
          localScope (scopeOfSegSpace space) $ do
            mapM_ readKernelInput inps
            certifying cs . mapM_ readKernelInput $ do
              (p, arr) <- zip (lambdaParams map_lam) arrs
              pure $ KernelInput (paramName p) (paramType p) arr [Var gtid]
            res <- bodyBind (lambdaBody map_lam)
            forM res $ \(SubExpRes Certs
    res_cs SubExp
    se) -> KernelResult -> BuilderT (Rep m) (State VNameSource) KernelResult
    forall a. a -> BuilderT (Rep m) (State VNameSource) a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure (KernelResult -> BuilderT (Rep m) (State VNameSource) KernelResult)
    -> KernelResult
    -> BuilderT (Rep m) (State VNameSource) KernelResult
    forall a b. (a -> b) -> a -> b
    $ ResultManifest -> Certs -> SubExp -> KernelResult
    Returns ResultManifest
    ResultMaySimplify Certs
    res_cs SubExp
    se

      pure (space, kbody)

    segRed ::
      (MonadFreshNames m, DistRep rep, HasScope rep m) =>
      SegOpLevel rep ->
      Pat (LetDec rep) ->
      Certs ->
      SubExp -> -- segment size
      [SegBinOp rep] ->
      Lambda rep ->
      [VName] ->
      [(VName, SubExp)] -> -- ispace = pair of (gtid, size) for the maps on "top" of this reduction
      [KernelInput] -> -- inps = inputs that can be looked up by using the gtids from ispace
      m (Stms rep)
    segRed :: forall (m :: * -> *) rep.
    (MonadFreshNames m, DistRep rep, HasScope rep m) =>
    SegOpLevel rep
    -> Pat (LetDec rep)
    -> Certs
    -> SubExp
    -> [SegBinOp rep]
    -> Lambda rep
    -> [VName]
    -> [(VName, SubExp)]
    -> [KernelInput]
    -> m (Stms rep)
    segRed SegOpLevel rep
    lvl Pat (LetDec rep)
    pat Certs
    cs SubExp
    w [SegBinOp rep]
    ops Lambda rep
    map_lam [VName]
    arrs [(VName, SubExp)]
    ispace [KernelInput]
    inps = Builder rep () -> m (Stms rep)
    forall (m :: * -> *) somerep rep a.
    (MonadFreshNames m, HasScope somerep m, SameScope somerep rep) =>
    Builder rep a -> m (Stms rep)
    runBuilder_ (Builder rep () -> m (Stms rep)) -> Builder rep () -> m (Stms rep)
    forall a b. (a -> b) -> a -> b
    $ do
      (kspace, kbody) <- Certs
    -> SubExp
    -> Lambda (Rep (BuilderT rep (State VNameSource)))
    -> [VName]
    -> [(VName, SubExp)]
    -> [KernelInput]
    -> BuilderT
         rep
         (State VNameSource)
         (SegSpace, KernelBody (Rep (BuilderT rep (State VNameSource))))
    forall (m :: * -> *).
    (MonadBuilder m, DistRep (Rep m)) =>
    Certs
    -> SubExp
    -> Lambda (Rep m)
    -> [VName]
    -> [(VName, SubExp)]
    -> [KernelInput]
    -> m (SegSpace, KernelBody (Rep m))
    prepareRedOrScan Certs
    cs SubExp
    w Lambda rep
    Lambda (Rep (BuilderT rep (State VNameSource)))
    map_lam [VName]
    arrs [(VName, SubExp)]
    ispace [KernelInput]
    inps
      letBind pat $
        Op $
          segOp $
            SegRed lvl kspace (lambdaReturnType map_lam) kbody ops

    segScan ::
      (MonadFreshNames m, DistRep rep, HasScope rep m) =>
      SegOpLevel rep ->
      Pat (LetDec rep) ->
      Certs ->
      SubExp -> -- segment size
      [SegBinOp rep] ->
      Lambda rep ->
      [VName] ->
      [(VName, SubExp)] -> -- ispace = pair of (gtid, size) for the maps on "top" of this scan
      [KernelInput] -> -- inps = inputs that can be looked up by using the gtids from ispace
      m (Stms rep)
    segScan :: forall (m :: * -> *) rep.
    (MonadFreshNames m, DistRep rep, HasScope rep m) =>
    SegOpLevel rep
    -> Pat (LetDec rep)
    -> Certs
    -> SubExp
    -> [SegBinOp rep]
    -> Lambda rep
    -> [VName]
    -> [(VName, SubExp)]
    -> [KernelInput]
    -> m (Stms rep)
    segScan SegOpLevel rep
    lvl Pat (LetDec rep)
    pat Certs
    cs SubExp
    w [SegBinOp rep]
    ops Lambda rep
    map_lam [VName]
    arrs [(VName, SubExp)]
    ispace [KernelInput]
    inps = Builder rep () -> m (Stms rep)
    forall (m :: * -> *) somerep rep a.
    (MonadFreshNames m, HasScope somerep m, SameScope somerep rep) =>
    Builder rep a -> m (Stms rep)
    runBuilder_ (Builder rep () -> m (Stms rep)) -> Builder rep () -> m (Stms rep)
    forall a b. (a -> b) -> a -> b
    $ do
      (kspace, kbody) <- Certs
    -> SubExp
    -> Lambda (Rep (BuilderT rep (State VNameSource)))
    -> [VName]
    -> [(VName, SubExp)]
    -> [KernelInput]
    -> BuilderT
         rep
         (State VNameSource)
         (SegSpace, KernelBody (Rep (BuilderT rep (State VNameSource))))
    forall (m :: * -> *).
    (MonadBuilder m, DistRep (Rep m)) =>
    Certs
    -> SubExp
    -> Lambda (Rep m)
    -> [VName]
    -> [(VName, SubExp)]
    -> [KernelInput]
    -> m (SegSpace, KernelBody (Rep m))
    prepareRedOrScan Certs
    cs SubExp
    w Lambda rep
    Lambda (Rep (BuilderT rep (State VNameSource)))
    map_lam [VName]
    arrs [(VName, SubExp)]
    ispace [KernelInput]
    inps
      letBind pat $
        Op $
          segOp $
            SegScan lvl kspace (lambdaReturnType map_lam) kbody ops

    segMap ::
      (MonadFreshNames m, DistRep rep, HasScope rep m) =>
      SegOpLevel rep ->
      Pat (LetDec rep) ->
      SubExp -> -- segment size
      Lambda rep ->
      [VName] ->
      [(VName, SubExp)] -> -- ispace = pair of (gtid, size) for the maps on "top" of this map
      [KernelInput] -> -- inps = inputs that can be looked up by using the gtids from ispace
      m (Stms rep)
    segMap :: forall (m :: * -> *) rep.
    (MonadFreshNames m, DistRep rep, HasScope rep m) =>
    SegOpLevel rep
    -> Pat (LetDec rep)
    -> SubExp
    -> Lambda rep
    -> [VName]
    -> [(VName, SubExp)]
    -> [KernelInput]
    -> m (Stms rep)
    segMap SegOpLevel rep
    lvl Pat (LetDec rep)
    pat SubExp
    w Lambda rep
    map_lam [VName]
    arrs [(VName, SubExp)]
    ispace [KernelInput]
    inps = Builder rep () -> m (Stms rep)
    forall (m :: * -> *) somerep rep a.
    (MonadFreshNames m, HasScope somerep m, SameScope somerep rep) =>
    Builder rep a -> m (Stms rep)
    runBuilder_ (Builder rep () -> m (Stms rep)) -> Builder rep () -> m (Stms rep)
    forall a b. (a -> b) -> a -> b
    $ do
      (kspace, kbody) <- Certs
    -> SubExp
    -> Lambda (Rep (BuilderT rep (State VNameSource)))
    -> [VName]
    -> [(VName, SubExp)]
    -> [KernelInput]
    -> BuilderT
         rep
         (State VNameSource)
         (SegSpace, KernelBody (Rep (BuilderT rep (State VNameSource))))
    forall (m :: * -> *).
    (MonadBuilder m, DistRep (Rep m)) =>
    Certs
    -> SubExp
    -> Lambda (Rep m)
    -> [VName]
    -> [(VName, SubExp)]
    -> [KernelInput]
    -> m (SegSpace, KernelBody (Rep m))
    prepareRedOrScan Certs
    forall a. Monoid a => a
    mempty SubExp
    w Lambda rep
    Lambda (Rep (BuilderT rep (State VNameSource)))
    map_lam [VName]
    arrs [(VName, SubExp)]
    ispace [KernelInput]
    inps
      letBind pat $
        Op $
          segOp $
            SegMap lvl kspace (lambdaReturnType map_lam) kbody

    dummyDim ::
      (MonadBuilder m) =>
      Pat Type ->
      m (Pat Type, [(VName, SubExp)], m ())
    dummyDim :: forall (m :: * -> *).
    MonadBuilder m =>
    Pat Type -> m (Pat Type, [(VName, SubExp)], m ())
    dummyDim Pat Type
    pat = do
      -- We add a unit-size segment on top to ensure that the result
      -- of the SegRed is an array, which we then immediately index.
      -- This is useful in the case that the value is used on the
      -- device afterwards, as this may save an expensive
      -- host-device copy (scalars are kept on the host, but arrays
      -- may be on the device).
      let addDummyDim :: Type -> Type
    addDummyDim Type
    t = Type
    t Type -> SubExp -> Type
    forall d.
    ArrayShape (ShapeBase d) =>
    TypeBase (ShapeBase d) NoUniqueness
    -> d -> TypeBase (ShapeBase d) NoUniqueness
    `arrayOfRow` IntType -> Integer -> SubExp
    intConst IntType
    Int64 Integer
    1
      pat' <- (Type -> Type) -> Pat Type -> Pat Type
    forall a b. (a -> b) -> Pat a -> Pat b
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    fmap Type -> Type
    addDummyDim (Pat Type -> Pat Type) -> m (Pat Type) -> m (Pat Type)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Pat Type -> m (Pat Type)
    forall dec (m :: * -> *).
    (Rename dec, MonadFreshNames m) =>
    Pat dec -> m (Pat dec)
    renamePat Pat Type
    pat
      dummy <- newVName "dummy"
      let ispace = [(VName
    dummy, IntType -> Integer -> SubExp
    intConst IntType
    Int64 Integer
    1)]

      pure
        ( pat',
          ispace,
          forM_ (zip (patNames pat') (patNames pat)) $ \(VName
    from, VName
    to) -> do
            from_t <- VName -> m Type
    forall rep (m :: * -> *). HasScope rep m => VName -> m Type
    lookupType VName
    from
            letBindNames [to] . BasicOp $
              case from_t of
                Acc {} -> SubExp -> BasicOp
    SubExp (SubExp -> BasicOp) -> SubExp -> BasicOp
    forall a b. (a -> b) -> a -> b
    $ VName -> SubExp
    Var VName
    from
                Type
    _ -> VName -> Slice SubExp -> BasicOp
    Index VName
    from (Slice SubExp -> BasicOp) -> Slice SubExp -> BasicOp
    forall a b. (a -> b) -> a -> b
    $ Type -> [DimIndex SubExp] -> Slice SubExp
    fullSlice Type
    from_t [SubExp -> DimIndex SubExp
    forall d. d -> DimIndex d
    DimFix (SubExp -> DimIndex SubExp) -> SubExp -> DimIndex SubExp
    forall a b. (a -> b) -> a -> b
    $ IntType -> Integer -> SubExp
    intConst IntType
    Int64 Integer
    0]
        )

    nonSegRed ::
      (MonadFreshNames m, DistRep rep, HasScope rep m) =>
      SegOpLevel rep ->
      Pat Type ->
      SubExp ->
      [SegBinOp rep] ->
      Lambda rep ->
      [VName] ->
      m (Stms rep)
    nonSegRed :: forall (m :: * -> *) rep.
    (MonadFreshNames m, DistRep rep, HasScope rep m) =>
    SegOpLevel rep
    -> Pat Type
    -> SubExp
    -> [SegBinOp rep]
    -> Lambda rep
    -> [VName]
    -> m (Stms rep)
    nonSegRed SegOpLevel rep
    lvl Pat Type
    pat SubExp
    w [SegBinOp rep]
    ops Lambda rep
    map_lam [VName]
    arrs = Builder rep () -> m (Stms rep)
    forall (m :: * -> *) somerep rep a.
    (MonadFreshNames m, HasScope somerep m, SameScope somerep rep) =>
    Builder rep a -> m (Stms rep)
    runBuilder_ (Builder rep () -> m (Stms rep)) -> Builder rep () -> m (Stms rep)
    forall a b. (a -> b) -> a -> b
    $ do
      (pat', ispace, read_dummy) <- Pat Type
    -> BuilderT
         rep
         (State VNameSource)
         (Pat Type, [(VName, SubExp)], Builder rep ())
    forall (m :: * -> *).
    MonadBuilder m =>
    Pat Type -> m (Pat Type, [(VName, SubExp)], m ())
    dummyDim Pat Type
    pat
      addStms =<< segRed lvl pat' mempty w ops map_lam arrs ispace []
      read_dummy

    segHist ::
      (DistRep rep, MonadFreshNames m, HasScope rep m) =>
      SegOpLevel rep ->
      Pat Type ->
      SubExp ->
      -- | Segment indexes and sizes.
      [(VName, SubExp)] ->
      [KernelInput] ->
      [HistOp rep] ->
      Lambda rep ->
      [VName] ->
      m (Stms rep)
    segHist :: forall rep (m :: * -> *).
    (DistRep rep, MonadFreshNames m, HasScope rep m) =>
    SegOpLevel rep
    -> Pat Type
    -> SubExp
    -> [(VName, SubExp)]
    -> [KernelInput]
    -> [HistOp rep]
    -> Lambda rep
    -> [VName]
    -> m (Stms rep)
    segHist SegOpLevel rep
    lvl Pat Type
    pat SubExp
    arr_w [(VName, SubExp)]
    ispace [KernelInput]
    inps [HistOp rep]
    ops Lambda rep
    lam [VName]
    arrs = Builder rep () -> m (Stms rep)
    forall (m :: * -> *) somerep rep a.
    (MonadFreshNames m, HasScope somerep m, SameScope somerep rep) =>
    Builder rep a -> m (Stms rep)
    runBuilder_ (Builder rep () -> m (Stms rep)) -> Builder rep () -> m (Stms rep)
    forall a b. (a -> b) -> a -> b
    $ do
      gtid <- String -> BuilderT rep (State VNameSource) VName
    forall (m :: * -> *). MonadFreshNames m => String -> m VName
    newVName String
    "gtid"
      space <- mkSegSpace $ ispace ++ [(gtid, arr_w)]

      kbody <- fmap (uncurry (flip $ KernelBody ())) $
        runBuilder $
          localScope (scopeOfSegSpace space) $ do
            mapM_ readKernelInput inps
            forM_ (zip (lambdaParams lam) arrs) $ \(Param Type
    p, VName
    arr) -> do
              arr_t <- VName -> BuilderT rep (State VNameSource) Type
    forall rep (m :: * -> *). HasScope rep m => VName -> m Type
    lookupType VName
    arr
              letBindNames [paramName p] $
                BasicOp $
                  Index arr $
                    fullSlice arr_t [DimFix $ Var gtid]
            res <- bodyBind (lambdaBody lam)
            forM res $ \(SubExpRes Certs
    cs SubExp
    se) ->
              KernelResult -> BuilderT rep (State VNameSource) KernelResult
    forall a. a -> BuilderT rep (State VNameSource) a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure (KernelResult -> BuilderT rep (State VNameSource) KernelResult)
    -> KernelResult -> BuilderT rep (State VNameSource) KernelResult
    forall a b. (a -> b) -> a -> b
    $ ResultManifest -> Certs -> SubExp -> KernelResult
    Returns ResultManifest
    ResultMaySimplify Certs
    cs SubExp
    se

      letBind pat $ Op $ segOp $ SegHist lvl space (lambdaReturnType lam) kbody ops

    mapKernelSkeleton ::
      (DistRep rep, HasScope rep m, MonadFreshNames m) =>
      [(VName, SubExp)] ->
      [KernelInput] ->
      m (SegSpace, Stms rep)
    mapKernelSkeleton :: forall rep (m :: * -> *).
    (DistRep rep, HasScope rep m, MonadFreshNames m) =>
    [(VName, SubExp)] -> [KernelInput] -> m (SegSpace, Stms rep)
    mapKernelSkeleton [(VName, SubExp)]
    ispace [KernelInput]
    inputs = do
      read_input_stms <- Builder rep [()] -> m (Stms rep)
    forall (m :: * -> *) somerep rep a.
    (MonadFreshNames m, HasScope somerep m, SameScope somerep rep) =>
    Builder rep a -> m (Stms rep)
    runBuilder_ (Builder rep [()] -> m (Stms rep))
    -> Builder rep [()] -> m (Stms rep)
    forall a b. (a -> b) -> a -> b
    $ (KernelInput -> BuilderT rep (State VNameSource) ())
    -> [KernelInput] -> Builder rep [()]
    forall (t :: * -> *) (m :: * -> *) a b.
    (Traversable t, Monad m) =>
    (a -> m b) -> t a -> m (t b)
    forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
    mapM KernelInput -> BuilderT rep (State VNameSource) ()
    forall (m :: * -> *).
    (DistRep (Rep m), MonadBuilder m) =>
    KernelInput -> m ()
    readKernelInput [KernelInput]
    inputs

      space <- mkSegSpace ispace
      pure (space, read_input_stms)

    mapKernel ::
      (DistRep rep, HasScope rep m, MonadFreshNames m) =>
      MkSegLevel rep m ->
      [(VName, SubExp)] ->
      [KernelInput] ->
      [Type] ->
      KernelBody rep ->
      m (SegOp (SegOpLevel rep) rep, Stms rep)
    mapKernel :: forall rep (m :: * -> *).
    (DistRep rep, HasScope rep m, MonadFreshNames m) =>
    MkSegLevel rep m
    -> [(VName, SubExp)]
    -> [KernelInput]
    -> [Type]
    -> KernelBody rep
    -> m (SegOp (SegOpLevel rep) rep, Stms rep)
    mapKernel MkSegLevel rep m
    mk_lvl [(VName, SubExp)]
    ispace [KernelInput]
    inputs [Type]
    rts (KernelBody () Stms rep
    kstms [KernelResult]
    krets) = BuilderT rep m (SegOp (SegOpLevel rep) rep)
    -> m (SegOp (SegOpLevel rep) rep, Stms rep)
    forall (m :: * -> *) somerep rep a.
    (MonadFreshNames m, HasScope somerep m, SameScope somerep rep) =>
    BuilderT rep m a -> m (a, Stms rep)
    runBuilderT' (BuilderT rep m (SegOp (SegOpLevel rep) rep)
     -> m (SegOp (SegOpLevel rep) rep, Stms rep))
    -> BuilderT rep m (SegOp (SegOpLevel rep) rep)
    -> m (SegOp (SegOpLevel rep) rep, Stms rep)
    forall a b. (a -> b) -> a -> b
    $ do
      (space, read_input_stms) <- [(VName, SubExp)]
    -> [KernelInput] -> BuilderT rep m (SegSpace, Stms rep)
    forall rep (m :: * -> *).
    (DistRep rep, HasScope rep m, MonadFreshNames m) =>
    [(VName, SubExp)] -> [KernelInput] -> m (SegSpace, Stms rep)
    mapKernelSkeleton [(VName, SubExp)]
    ispace [KernelInput]
    inputs

      let kbody' = BodyDec rep -> Stms rep -> [KernelResult] -> KernelBody rep
    forall rep.
    BodyDec rep -> Stms rep -> [KernelResult] -> KernelBody rep
    KernelBody () (Stms rep
    read_input_stms Stms rep -> Stms rep -> Stms rep
    forall a. Semigroup a => a -> a -> a
    <> Stms rep
    kstms) [KernelResult]
    krets

      -- If the kernel creates arrays (meaning it will require memory
      -- expansion), we want to truncate the amount of threads.
      -- Otherwise, have at it!  This is a bit of a hack - in principle,
      -- we should make this decision later, when we have a clearer idea
      -- of what is happening inside the kernel.
      let r = if (Type -> Bool) -> [Type] -> Bool
    forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
    all Type -> Bool
    forall shape u. TypeBase shape u -> Bool
    primType [Type]
    rts then ThreadRecommendation
    ManyThreads else SegVirt -> ThreadRecommendation
    NoRecommendation SegVirt
    SegVirt

      lvl <- mk_lvl (map snd ispace) "segmap" r

      pure $ SegMap lvl space rts kbody'

    data KernelInput = KernelInput
      { KernelInput -> VName
    kernelInputName :: VName,
        KernelInput -> Type
    kernelInputType :: Type,
        KernelInput -> VName
    kernelInputArray :: VName,
        KernelInput -> [SubExp]
    kernelInputIndices :: [SubExp]
      }
      deriving (Int -> KernelInput -> ShowS
    [KernelInput] -> ShowS
    KernelInput -> String
    (Int -> KernelInput -> ShowS)
    -> (KernelInput -> String)
    -> ([KernelInput] -> ShowS)
    -> Show KernelInput
    forall a.
    (Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
    $cshowsPrec :: Int -> KernelInput -> ShowS
    showsPrec :: Int -> KernelInput -> ShowS
    $cshow :: KernelInput -> String
    show :: KernelInput -> String
    $cshowList :: [KernelInput] -> ShowS
    showList :: [KernelInput] -> ShowS
    Show)

    readKernelInput ::
      (DistRep (Rep m), MonadBuilder m) =>
      KernelInput ->
      m ()
    readKernelInput :: forall (m :: * -> *).
    (DistRep (Rep m), MonadBuilder m) =>
    KernelInput -> m ()
    readKernelInput KernelInput
    inp = do
      let pe :: PatElem Type
    pe = VName -> Type -> PatElem Type
    forall dec. VName -> dec -> PatElem dec
    PatElem (KernelInput -> VName
    kernelInputName KernelInput
    inp) (Type -> PatElem Type) -> Type -> PatElem Type
    forall a b. (a -> b) -> a -> b
    $ KernelInput -> Type
    kernelInputType KernelInput
    inp
      Pat (LetDec (Rep m)) -> Exp (Rep m) -> m ()
    forall (m :: * -> *).
    MonadBuilder m =>
    Pat (LetDec (Rep m)) -> Exp (Rep m) -> m ()
    letBind ([PatElem Type] -> Pat Type
    forall dec. [PatElem dec] -> Pat dec
    Pat [PatElem Type
    pe]) (Exp (Rep m) -> m ())
    -> (BasicOp -> Exp (Rep m)) -> BasicOp -> m ()
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . BasicOp -> Exp (Rep m)
    forall rep. BasicOp -> Exp rep
    BasicOp (BasicOp -> m ()) -> BasicOp -> m ()
    forall a b. (a -> b) -> a -> b
    $
        case KernelInput -> Type
    kernelInputType KernelInput
    inp of
          Acc {} ->
            SubExp -> BasicOp
    SubExp (SubExp -> BasicOp) -> SubExp -> BasicOp
    forall a b. (a -> b) -> a -> b
    $ VName -> SubExp
    Var (VName -> SubExp) -> VName -> SubExp
    forall a b. (a -> b) -> a -> b
    $ KernelInput -> VName
    kernelInputArray KernelInput
    inp
          Type
    _ ->
            VName -> Slice SubExp -> BasicOp
    Index (KernelInput -> VName
    kernelInputArray KernelInput
    inp) (Slice SubExp -> BasicOp)
    -> ([DimIndex SubExp] -> Slice SubExp)
    -> [DimIndex SubExp]
    -> BasicOp
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . [DimIndex SubExp] -> Slice SubExp
    forall d. [DimIndex d] -> Slice d
    Slice ([DimIndex SubExp] -> BasicOp) -> [DimIndex SubExp] -> BasicOp
    forall a b. (a -> b) -> a -> b
    $
              (SubExp -> DimIndex SubExp) -> [SubExp] -> [DimIndex SubExp]
    forall a b. (a -> b) -> [a] -> [b]
    map SubExp -> DimIndex SubExp
    forall d. d -> DimIndex d
    DimFix (KernelInput -> [SubExp]
    kernelInputIndices KernelInput
    inp)
                [DimIndex SubExp] -> [DimIndex SubExp] -> [DimIndex SubExp]
    forall a. [a] -> [a] -> [a]
    ++ (SubExp -> DimIndex SubExp) -> [SubExp] -> [DimIndex SubExp]
    forall a b. (a -> b) -> [a] -> [b]
    map SubExp -> DimIndex SubExp
    sliceDim (Type -> [SubExp]
    forall u. TypeBase (ShapeBase SubExp) u -> [SubExp]
    arrayDims (KernelInput -> Type
    kernelInputType KernelInput
    inp))