{-# LANGUAGE TypeFamilies #-}

-- | A representation with flat parallelism via GPU-oriented kernels.
module Futhark.IR.GPU
  ( GPU,

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

import Futhark.Builder
import Futhark.Construct
import Futhark.IR.Aliases (Aliases)
import Futhark.IR.GPU.Op
import Futhark.IR.GPU.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 Futhark.IR.TypeCheck qualified as TC

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

instance RepTypes GPU where
  type OpC GPU = HostOp SOAC

instance ASTRep GPU where
  expTypesFromPat :: forall (m :: * -> *).
(HasScope GPU m, Monad m) =>
Pat (LetDec GPU) -> m [BranchType GPU]
expTypesFromPat = [ExtType] -> m [ExtType]
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([ExtType] -> m [ExtType])
-> (Pat Type -> [ExtType]) -> Pat Type -> m [ExtType]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Pat Type -> [ExtType]
forall dec. Typed dec => Pat dec -> [ExtType]
expExtTypesFromPat

instance TC.Checkable GPU where
  checkOp :: Op (Aliases GPU) -> TypeM GPU ()
checkOp = Maybe SegLevel -> HostOp SOAC (Aliases GPU) -> TypeM GPU ()
typeCheckGPUOp Maybe SegLevel
forall a. Maybe a
Nothing
    where
      -- GHC 9.2 goes into an infinite loop without the type annotation.
      typeCheckGPUOp ::
        Maybe SegLevel ->
        HostOp SOAC (Aliases GPU) ->
        TC.TypeM GPU ()
      typeCheckGPUOp :: Maybe SegLevel -> HostOp SOAC (Aliases GPU) -> TypeM GPU ()
typeCheckGPUOp Maybe SegLevel
lvl =
        (SegLevel -> Op (Aliases GPU) -> TypeM GPU ())
-> Maybe SegLevel
-> (SOAC (Aliases GPU) -> TypeM GPU ())
-> HostOp SOAC (Aliases GPU)
-> TypeM GPU ()
forall rep (op :: * -> *).
Checkable rep =>
(SegLevel -> Op (Aliases rep) -> TypeM rep ())
-> Maybe SegLevel
-> (op (Aliases rep) -> TypeM rep ())
-> HostOp op (Aliases rep)
-> TypeM rep ()
typeCheckHostOp (Maybe SegLevel -> HostOp SOAC (Aliases GPU) -> TypeM GPU ()
typeCheckGPUOp (Maybe SegLevel -> HostOp SOAC (Aliases GPU) -> TypeM GPU ())
-> (SegLevel -> Maybe SegLevel)
-> SegLevel
-> HostOp SOAC (Aliases GPU)
-> TypeM GPU ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SegLevel -> Maybe SegLevel
forall a. a -> Maybe a
Just) Maybe SegLevel
lvl SOAC (Aliases GPU) -> TypeM GPU ()
forall rep. Checkable rep => SOAC (Aliases rep) -> TypeM rep ()
typeCheckSOAC

instance Buildable GPU where
  mkBody :: Stms GPU -> Result -> Body GPU
mkBody = BodyDec GPU -> Stms GPU -> Result -> Body GPU
forall rep. BodyDec rep -> Stms rep -> Result -> Body rep
Body ()
  mkExpPat :: [Ident] -> Exp GPU -> Pat (LetDec GPU)
mkExpPat [Ident]
idents Exp GPU
_ = [Ident] -> Pat Type
basicPat [Ident]
idents
  mkExpDec :: Pat (LetDec GPU) -> Exp GPU -> ExpDec GPU
mkExpDec Pat (LetDec GPU)
_ Exp GPU
_ = ()
  mkLetNames :: forall (m :: * -> *).
(MonadFreshNames m, HasScope GPU m) =>
[VName] -> Exp GPU -> m (Stm GPU)
mkLetNames = [VName] -> Exp GPU -> m (Stm GPU)
forall rep (m :: * -> *).
(ExpDec rep ~ (), LetDec rep ~ Type, MonadFreshNames m,
 TypedOp (Op rep), HasScope rep m) =>
[VName] -> Exp rep -> m (Stm rep)
simpleMkLetNames

instance BuilderOps GPU

instance PrettyRep GPU

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