futhark-0.25.32: An optimising compiler for a functional, array-oriented language.

Safe Haskell | None
Language | GHC2021

Futhark.CodeGen.Backends.GenericPython

Description

A generic Python code generator which is polymorphic in the type of the operations. Concretely, we use this to handle both sequential and PyOpenCL Python code.

Synopsis

- compileProg :: MonadFreshNames m => CompilerMode -> String -> Constructor -> [PyStmt] -> [PyStmt] -> Operations op s -> s -> [PyStmt] -> [Option] -> Definitions op -> m Text
- data CompilerMode
- data Constructor = Constructor [String] [PyStmt]
- emptyConstructor :: Constructor
- compileName :: VName -> String
- compileVar :: VName -> CompilerM op s PyExp
- compileDim :: DimSize -> CompilerM op s PyExp
- compileExp :: Exp -> CompilerM op s PyExp
- compilePrimExp :: Monad m => (v -> m PyExp) -> PrimExp v -> m PyExp
- compileCode :: Code op -> CompilerM op s ()
- compilePrimValue :: PrimValue -> PyExp
- compilePrimType :: PrimType -> String
- compilePrimToNp :: PrimType -> String
- compilePrimToExtNp :: PrimType -> Signedness -> String
- fromStorage :: PrimType -> PyExp -> PyExp
- toStorage :: PrimType -> PyExp -> PyExp
- data Operations op s = Operations {
  - opsWriteScalar :: WriteScalar op s
  - opsReadScalar :: ReadScalar op s
  - opsAllocate :: Allocate op s
  - opsCopies :: Map (Space, Space) (DoCopy op s)
  - opsCompiler :: OpCompiler op s
  - opsEntryOutput :: EntryOutput op s
  - opsEntryInput :: EntryInput op s
  }
- type DoCopy op s = PrimType -> [Count Elements PyExp] -> PyExp -> (Count Elements PyExp, [Count Elements PyExp]) -> PyExp -> (Count Elements PyExp, [Count Elements PyExp]) -> CompilerM op s ()
- defaultOperations :: Operations op s
- unpackDim :: PyExp -> DimSize -> Int32 -> CompilerM op s ()
- newtype CompilerM op s a = CompilerM (RWS (CompilerEnv op s) [PyStmt] (CompilerState s) a)
- type OpCompiler op s = op -> CompilerM op s ()
- type WriteScalar op s = PyExp -> PyExp -> PrimType -> SpaceId -> PyExp -> CompilerM op s ()
- type ReadScalar op s = PyExp -> PyExp -> PrimType -> SpaceId -> CompilerM op s PyExp
- type Allocate op s = PyExp -> PyExp -> SpaceId -> CompilerM op s ()
- type Copy op s = PyExp -> PyExp -> Space -> PyExp -> PyExp -> Space -> PyExp -> PrimType -> CompilerM op s ()
- type EntryOutput op s = VName -> SpaceId -> PrimType -> Signedness -> [DimSize] -> CompilerM op s PyExp
- type EntryInput op s = PyExp -> SpaceId -> PrimType -> Signedness -> [DimSize] -> PyExp -> CompilerM op s ()
- data CompilerEnv op s = CompilerEnv {
  - envOperations :: Operations op s
  - envVarExp :: Map String PyExp
  }
- data CompilerState s = CompilerState {
  - compNameSrc :: VNameSource
  - compInit :: [PyStmt]
  - compUserState :: s
  }
- stm :: PyStmt -> CompilerM op s ()
- atInit :: PyStmt -> CompilerM op s ()
- collect' :: CompilerM op s a -> CompilerM op s (a, [PyStmt])
- collect :: CompilerM op s () -> CompilerM op s [PyStmt]
- simpleCall :: String -> [PyExp] -> PyExp

# Documentation

compileProg :: MonadFreshNames m => CompilerMode -> String -> Constructor -> [PyStmt] -> [PyStmt] -> Operations op s -> s -> [PyStmt] -> [Option] -> Definitions op -> m Text Source #

data CompilerMode Source #

Are we compiling a library or an executable?

#### Instances

Instances details

Show CompilerMode Source # |
Instance details Defined in Futhark.Compiler.Config Methods showsPrec :: Int -> CompilerMode -> ShowS Source # show :: CompilerMode -> String Source # showList :: [CompilerMode] -> ShowS Source #
Eq CompilerMode Source # |
Instance details Defined in Futhark.Compiler.Config Methods (==) :: CompilerMode -> CompilerMode -> Bool Source # (/=) :: CompilerMode -> CompilerMode -> Bool Source #
Ord CompilerMode Source # |

Instance details

Defined in Futhark.Compiler.Config

Methods

compare :: CompilerMode -> CompilerMode -> Ordering Source #

(<) :: CompilerMode -> CompilerMode -> Bool Source #

(<=) :: CompilerMode -> CompilerMode -> Bool Source #

(>) :: CompilerMode -> CompilerMode -> Bool Source #

(>=) :: CompilerMode -> CompilerMode -> Bool Source #

max :: CompilerMode -> CompilerMode -> CompilerMode Source #

min :: CompilerMode -> CompilerMode -> CompilerMode Source #

data Constructor Source #

The class generated by the code generator must have a constructor, although it can be vacuous.

Constructors

Constructor [String] [PyStmt] |

emptyConstructor :: Constructor Source #

A constructor that takes no arguments and does nothing.

compileName :: VName -> String Source #

compileVar :: VName -> CompilerM op s PyExp Source #

compileDim :: DimSize -> CompilerM op s PyExp Source #

compileExp :: Exp -> CompilerM op s PyExp Source #

compilePrimExp :: Monad m => (v -> m PyExp) -> PrimExp v -> m PyExp Source #

Tell me how to compile a `v`, and I'll Compile any `PrimExp v` for you.

compileCode :: Code op -> CompilerM op s () Source #

compilePrimValue :: PrimValue -> PyExp Source #

compilePrimType :: PrimType -> String Source #

The ctypes type corresponding to a `PrimType`.

compilePrimToNp :: PrimType -> String Source #

The Numpy type corresponding to a `PrimType`.

compilePrimToExtNp :: PrimType -> Signedness -> String Source #

The Numpy type corresponding to a `PrimType`, taking sign into account.

fromStorage :: PrimType -> PyExp -> PyExp Source #

Convert from storage to scalar representation for the given type.

toStorage :: PrimType -> PyExp -> PyExp Source #

Convert from scalar to storage representation for the given type.

data Operations op s Source #

Constructors

Operations |

Fields

- opsWriteScalar :: WriteScalar op s
- opsReadScalar :: ReadScalar op s
- opsAllocate :: Allocate op s
- opsCopies :: Map (Space, Space) (DoCopy op s)

  `(dst,src)`-space mapping to copy functions.

- opsCompiler :: OpCompiler op s
- opsEntryOutput :: EntryOutput op s
- opsEntryInput :: EntryInput op s

type DoCopy op s = PrimType -> [Count Elements PyExp] -> PyExp -> (Count Elements PyExp, [Count Elements PyExp]) -> PyExp -> (Count Elements PyExp, [Count Elements PyExp]) -> CompilerM op s () Source #

Perform an `Copy`. It is expected that these functions are each specialised on which spaces they operate on, so that is not part of their arguments.

defaultOperations :: Operations op s Source #

A set of operations that fail for every operation involving non-default memory spaces. Uses plain pointers and `malloc` for memory management.

unpackDim :: PyExp -> DimSize -> Int32 -> CompilerM op s () Source #

newtype CompilerM op s a Source #

Constructors

CompilerM (RWS (CompilerEnv op s) [PyStmt] (CompilerState s) a) |

#### Instances

Instances details

MonadState (CompilerState s) (CompilerM op s) Source # |
Instance details Defined in Futhark.CodeGen.Backends.GenericPython Methods get :: CompilerM op s (CompilerState s) Source # put :: CompilerState s -> CompilerM op s () Source # state :: (CompilerState s -> (a, CompilerState s)) -> CompilerM op s a Source #
MonadWriter [PyStmt] (CompilerM op s) Source # |
Instance details Defined in Futhark.CodeGen.Backends.GenericPython Methods writer :: (a, [PyStmt]) -> CompilerM op s a Source # tell :: [PyStmt] -> CompilerM op s () Source # listen :: CompilerM op s a -> CompilerM op s (a, [PyStmt]) Source # pass :: CompilerM op s (a, [PyStmt] -> [PyStmt]) -> CompilerM op s a Source #
MonadFreshNames (CompilerM op s) Source # |
Instance details Defined in Futhark.CodeGen.Backends.GenericPython Methods getNameSource :: CompilerM op s VNameSource Source # putNameSource :: VNameSource -> CompilerM op s () Source #
Applicative (CompilerM op s) Source # |

Instance details

Defined in Futhark.CodeGen.Backends.GenericPython

Methods

pure :: a -> CompilerM op s a Source #

(<*>) :: CompilerM op s (a -> b) -> CompilerM op s a -> CompilerM op s b Source #

liftA2 :: (a -> b -> c) -> CompilerM op s a -> CompilerM op s b -> CompilerM op s c Source #

(*>) :: CompilerM op s a -> CompilerM op s b -> CompilerM op s b Source #

(<*) :: CompilerM op s a -> CompilerM op s b -> CompilerM op s a Source #

Functor (CompilerM op s) Source # |
Instance details Defined in Futhark.CodeGen.Backends.GenericPython Methods fmap :: (a -> b) -> CompilerM op s a -> CompilerM op s b Source # (<$) :: a -> CompilerM op s b -> CompilerM op s a Source #
Monad (CompilerM op s) Source # |
Instance details Defined in Futhark.CodeGen.Backends.GenericPython Methods (>>=) :: CompilerM op s a -> (a -> CompilerM op s b) -> CompilerM op s b Source # (>>) :: CompilerM op s a -> CompilerM op s b -> CompilerM op s b Source # return :: a -> CompilerM op s a Source #
MonadReader (CompilerEnv op s) (CompilerM op s) Source # |
Instance details Defined in Futhark.CodeGen.Backends.GenericPython Methods ask :: CompilerM op s (CompilerEnv op s) Source # local :: (CompilerEnv op s -> CompilerEnv op s) -> CompilerM op s a -> CompilerM op s a Source # reader :: (CompilerEnv op s -> a) -> CompilerM op s a Source #

type OpCompiler op s = op -> CompilerM op s () Source #

A substitute expression compiler, tried before the main compilation function.

type WriteScalar op s = PyExp -> PyExp -> PrimType -> SpaceId -> PyExp -> CompilerM op s () Source #

Write a scalar to the given memory block with the given index and in the given memory space.

type ReadScalar op s = PyExp -> PyExp -> PrimType -> SpaceId -> CompilerM op s PyExp Source #

Read a scalar from the given memory block with the given index and in the given memory space.

type Allocate op s = PyExp -> PyExp -> SpaceId -> CompilerM op s () Source #

Allocate a memory block of the given size in the given memory space, saving a reference in the given variable name.

type Copy op s = PyExp -> PyExp -> Space -> PyExp -> PyExp -> Space -> PyExp -> PrimType -> CompilerM op s () Source #

Copy from one memory block to another.

type EntryOutput op s = VName -> SpaceId -> PrimType -> Signedness -> [DimSize] -> CompilerM op s PyExp Source #

Construct the Python array being returned from an entry point.

type EntryInput op s = PyExp -> SpaceId -> PrimType -> Signedness -> [DimSize] -> PyExp -> CompilerM op s () Source #

Unpack the array being passed to an entry point.

data CompilerEnv op s Source #

Constructors

CompilerEnv |

Fields

- envOperations :: Operations op s
- envVarExp :: Map String PyExp

#### Instances

Instances details

MonadReader (CompilerEnv op s) (CompilerM op s) Source # |
Instance details Defined in Futhark.CodeGen.Backends.GenericPython Methods ask :: CompilerM op s (CompilerEnv op s) Source # local :: (CompilerEnv op s -> CompilerEnv op s) -> CompilerM op s a -> CompilerM op s a Source # reader :: (CompilerEnv op s -> a) -> CompilerM op s a Source #

data CompilerState s Source #

Constructors

CompilerState |

Fields

- compNameSrc :: VNameSource
- compInit :: [PyStmt]
- compUserState :: s

#### Instances

Instances details

MonadState (CompilerState s) (CompilerM op s) Source # |
Instance details Defined in Futhark.CodeGen.Backends.GenericPython Methods get :: CompilerM op s (CompilerState s) Source # put :: CompilerState s -> CompilerM op s () Source # state :: (CompilerState s -> (a, CompilerState s)) -> CompilerM op s a Source #

stm :: PyStmt -> CompilerM op s () Source #

atInit :: PyStmt -> CompilerM op s () Source #

collect' :: CompilerM op s a -> CompilerM op s (a, [PyStmt]) Source #

collect :: CompilerM op s () -> CompilerM op s [PyStmt] Source #

simpleCall :: String -> [PyExp] -> PyExp Source #

A `Call` where the function is a variable and every argument is a simple `Arg`.