{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeFamilies #-}

-- | A representation with flat parallelism via GPU-oriented kernels.
module Futhark.IR.Kernels
  ( -- * The Lore definition
    Kernels,

    -- * Module re-exports
    module Futhark.IR.Prop,
    module Futhark.IR.Traversals,
    module Futhark.IR.Pretty,
    module Futhark.IR.Syntax,
    module Futhark.IR.Kernels.Kernel,
    module Futhark.IR.Kernels.Sizes,
    module Futhark.IR.SOACS.SOAC,
  )
where

import Futhark.Binder
import Futhark.Construct
import Futhark.IR.Kernels.Kernel
import Futhark.IR.Kernels.Sizes
import Futhark.IR.Pretty
import Futhark.IR.Prop
import Futhark.IR.SOACS.SOAC hiding (HistOp (..))
import Futhark.IR.Syntax
import Futhark.IR.Traversals
import qualified Futhark.TypeCheck as TypeCheck

-- | The phantom data type for the kernels representation.
data Kernels

instance Decorations Kernels where
  type Op Kernels = HostOp Kernels (SOAC Kernels)

instance ASTLore Kernels where
  expTypesFromPattern :: Pattern Kernels -> m [BranchType Kernels]
expTypesFromPattern = [ExtType] -> m [ExtType]
forall (m :: * -> *) a. Monad m => a -> m a
return ([ExtType] -> m [ExtType])
-> (PatternT Type -> [ExtType]) -> PatternT Type -> m [ExtType]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PatternT Type -> [ExtType]
forall dec. Typed dec => PatternT dec -> [ExtType]
expExtTypesFromPattern

instance TypeCheck.CheckableOp Kernels where
  checkOp :: OpWithAliases (Op Kernels) -> TypeM Kernels ()
checkOp = Maybe SegLevel
-> HostOp (Aliases Kernels) (SOAC (Aliases Kernels))
-> TypeM Kernels ()
forall lore.
(Checkable lore,
 OpWithAliases (Op lore)
 ~ HostOp (Aliases lore) (SOAC (Aliases lore))) =>
Maybe SegLevel
-> HostOp (Aliases lore) (SOAC (Aliases lore)) -> TypeM lore ()
typeCheckKernelsOp Maybe SegLevel
forall a. Maybe a
Nothing
    where
      typeCheckKernelsOp :: Maybe SegLevel
-> HostOp (Aliases lore) (SOAC (Aliases lore)) -> TypeM lore ()
typeCheckKernelsOp Maybe SegLevel
lvl =
        (SegLevel -> OpWithAliases (Op lore) -> TypeM lore ())
-> Maybe SegLevel
-> (SOAC (Aliases lore) -> TypeM lore ())
-> HostOp (Aliases lore) (SOAC (Aliases lore))
-> TypeM lore ()
forall lore op.
Checkable lore =>
(SegLevel -> OpWithAliases (Op lore) -> TypeM lore ())
-> Maybe SegLevel
-> (op -> TypeM lore ())
-> HostOp (Aliases lore) op
-> TypeM lore ()
typeCheckHostOp (Maybe SegLevel
-> HostOp (Aliases lore) (SOAC (Aliases lore)) -> TypeM lore ()
typeCheckKernelsOp (Maybe SegLevel
 -> HostOp (Aliases lore) (SOAC (Aliases lore)) -> TypeM lore ())
-> (SegLevel -> Maybe SegLevel)
-> SegLevel
-> HostOp (Aliases lore) (SOAC (Aliases lore))
-> TypeM lore ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SegLevel -> Maybe SegLevel
forall a. a -> Maybe a
Just) Maybe SegLevel
lvl SOAC (Aliases lore) -> TypeM lore ()
forall lore. Checkable lore => SOAC (Aliases lore) -> TypeM lore ()
typeCheckSOAC

instance TypeCheck.Checkable Kernels

instance Bindable Kernels where
  mkBody :: Stms Kernels -> [SubExp] -> Body Kernels
mkBody = BodyDec Kernels -> Stms Kernels -> [SubExp] -> Body Kernels
forall lore. BodyDec lore -> Stms lore -> [SubExp] -> BodyT lore
Body ()
  mkExpPat :: [Ident] -> [Ident] -> Exp Kernels -> Pattern Kernels
mkExpPat [Ident]
ctx [Ident]
val Exp Kernels
_ = [Ident] -> [Ident] -> PatternT Type
basicPattern [Ident]
ctx [Ident]
val
  mkExpDec :: Pattern Kernels -> Exp Kernels -> ExpDec Kernels
mkExpDec Pattern Kernels
_ Exp Kernels
_ = ()
  mkLetNames :: [VName] -> Exp Kernels -> m (Stm Kernels)
mkLetNames = [VName] -> Exp Kernels -> m (Stm Kernels)
forall lore (m :: * -> *).
(ExpDec lore ~ (), LetDec lore ~ Type, MonadFreshNames m,
 TypedOp (Op lore), HasScope lore m) =>
[VName] -> Exp lore -> m (Stm lore)
simpleMkLetNames

instance BinderOps Kernels

instance PrettyLore Kernels

instance HasSegOp Kernels where
  type SegOpLevel Kernels = SegLevel
  asSegOp :: Op Kernels -> Maybe (SegOp (SegOpLevel Kernels) Kernels)
asSegOp (SegOp op) = SegOp SegLevel Kernels -> Maybe (SegOp SegLevel Kernels)
forall a. a -> Maybe a
Just SegOp SegLevel Kernels
op
  asSegOp Op Kernels
_ = Maybe (SegOp (SegOpLevel Kernels) Kernels)
forall a. Maybe a
Nothing
  segOp :: SegOp (SegOpLevel Kernels) Kernels -> Op Kernels
segOp = SegOp (SegOpLevel Kernels) Kernels -> Op Kernels
forall lore op. SegOp SegLevel lore -> HostOp lore op
SegOp