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

Safe Haskell | Safe-Inferred
Language | GHC2021

Futhark.CodeGen.Backends.GenericC.Monad

Contents

- Pluggable compiler
- Monadic compiler interface
- Building Blocks

Description

C code generator framework.

Synopsis

- data Operations op s = Operations {
  - opsWriteScalar :: WriteScalar op s
  - opsReadScalar :: ReadScalar op s
  - opsAllocate :: Allocate op s
  - opsDeallocate :: Deallocate op s
  - opsCopy :: Copy op s
  - opsMemoryType :: MemoryType op s
  - opsCompiler :: OpCompiler op s
  - opsError :: ErrorCompiler op s
  - opsCall :: CallCompiler op s
  - opsCopies :: Map (Space, Space) (DoCopy op s)
  - opsFatMemory :: Bool
  - opsCritical :: ([BlockItem], [BlockItem])
  }
- data Publicness
  - = Private
  - | Public
- type OpCompiler op s = op -> CompilerM op s ()
- type ErrorCompiler op s = ErrorMsg Exp -> String -> CompilerM op s ()
- type CallCompiler op s = [VName] -> Name -> [Exp] -> CompilerM op s ()
- type PointerQuals = String -> [TypeQual]
- type MemoryType op s = SpaceId -> CompilerM op s Type
- type WriteScalar op s = Exp -> Exp -> Type -> SpaceId -> Volatility -> Exp -> CompilerM op s ()
- writeScalarPointerWithQuals :: PointerQuals -> WriteScalar op s
- type ReadScalar op s = Exp -> Exp -> Type -> SpaceId -> Volatility -> CompilerM op s Exp
- readScalarPointerWithQuals :: PointerQuals -> ReadScalar op s
- type Allocate op s = Exp -> Exp -> Exp -> SpaceId -> CompilerM op s ()
- type Deallocate op s = Exp -> Exp -> Exp -> SpaceId -> CompilerM op s ()
- data CopyBarrier
  - = CopyBarrier
  - | CopyNoBarrier
- type Copy op s = CopyBarrier -> Exp -> Exp -> Space -> Exp -> Exp -> Space -> Exp -> CompilerM op s ()
- type DoCopy op s = CopyBarrier -> PrimType -> [Count Elements Exp] -> Exp -> (Count Elements Exp, [Count Elements Exp]) -> Exp -> (Count Elements Exp, [Count Elements Exp]) -> CompilerM op s ()
- data CompilerM op s a
- data CompilerState s = CompilerState {
  - compArrayTypes :: Map ArrayType Publicness
  - compEarlyDecls :: DList Definition
  - compNameSrc :: VNameSource
  - compUserState :: s
  - compHeaderDecls :: Map HeaderSection (DList Definition)
  - compLibDecls :: DList Definition
  - compCtxFields :: DList (Id, Type, Maybe Exp, Maybe (Stm, Stm))
  - compClearItems :: DList BlockItem
  - compDeclaredMem :: [(VName, Space)]
  - compItems :: DList BlockItem
  }
- data CompilerEnv op s = CompilerEnv {
  - envOperations :: Operations op s
  - envCachedMem :: Map Exp VName
  - envProvenance :: Provenance
  }
- getUserState :: CompilerM op s s
- modifyUserState :: (s -> s) -> CompilerM op s ()
- generateProgramStruct :: CompilerM op s ()
- runCompilerM :: Operations op s -> VNameSource -> s -> CompilerM op s a -> (a, CompilerState s)
- inNewFunction :: CompilerM op s a -> CompilerM op s a
- cachingMemory :: Map VName Space -> ([BlockItem] -> [Stm] -> CompilerM op s a) -> CompilerM op s a
- volQuals :: Volatility -> [TypeQual]
- rawMem :: VName -> CompilerM op s Exp
- item :: BlockItem -> CompilerM op s ()
- items :: [BlockItem] -> CompilerM op s ()
- stm :: Stm -> CompilerM op s ()
- stms :: [Stm] -> CompilerM op s ()
- comment :: Text -> CompilerM op s ()
- decl :: InitGroup -> CompilerM op s ()
- headerDecl :: HeaderSection -> Definition -> CompilerM op s ()
- publicDef :: Text -> HeaderSection -> (Text -> (Definition, Definition)) -> CompilerM op s Text
- publicDef_ :: Text -> HeaderSection -> (Text -> (Definition, Definition)) -> CompilerM op s ()
- onClear :: BlockItem -> CompilerM op s ()
- data HeaderSection
  - = ArrayDecl Name
  - | OpaqueTypeDecl Name
  - | OpaqueDecl Name
  - | EntryDecl
  - | MiscDecl
  - | InitDecl
- libDecl :: Definition -> CompilerM op s ()
- earlyDecl :: Definition -> CompilerM op s ()
- publicName :: Text -> CompilerM op s Text
- contextField :: Id -> Type -> Maybe Exp -> CompilerM op s ()
- contextFieldDyn :: Id -> Type -> Stm -> Stm -> CompilerM op s ()
- memToCType :: VName -> Space -> CompilerM op s Type
- cacheMem :: ToExp a => a -> CompilerM op s (Maybe VName)
- fatMemory :: Space -> CompilerM op s Bool
- rawMemCType :: Space -> CompilerM op s Type
- freeRawMem :: (ToExp a, ToExp b, ToExp c) => a -> b -> Space -> c -> CompilerM op s ()
- allocRawMem :: (ToExp a, ToExp b, ToExp c) => a -> b -> Space -> c -> CompilerM op s ()
- fatMemType :: Space -> Type
- declAllocatedMem :: CompilerM op s [BlockItem]
- freeAllocatedMem :: CompilerM op s [BlockItem]
- collect :: CompilerM op s () -> CompilerM op s [BlockItem]
- collect' :: CompilerM op s a -> CompilerM op s (a, [BlockItem])
- contextType :: CompilerM op s Type
- configType :: CompilerM op s Type
- localProvenance :: Provenance -> CompilerM op s a -> CompilerM op s a
- askProvenance :: CompilerM op s Provenance
- provenanceExp :: CompilerM op s Exp
- copyMemoryDefaultSpace :: Exp -> Exp -> Exp -> Exp -> Exp -> CompilerM op s ()
- derefPointer :: Exp -> Exp -> Type -> Exp
- setMem :: (ToExp a, ToExp b) => a -> b -> Space -> CompilerM op s ()
- allocMem :: (ToExp a, ToExp b) => a -> b -> Space -> Stm -> CompilerM op s ()
- unRefMem :: ToExp a => a -> Space -> CompilerM op s ()
- declMem :: VName -> Space -> CompilerM op s ()
- resetMem :: ToExp a => a -> Space -> CompilerM op s ()
- fatMemAlloc :: Space -> String
- fatMemSet :: Space -> String
- fatMemUnRef :: Space -> String
- criticalSection :: Operations op s -> [BlockItem] -> [BlockItem]
- module Futhark.CodeGen.Backends.SimpleRep

# Pluggable compiler

data Operations op s Source #

Constructors

Operations |

Fields

- opsWriteScalar :: WriteScalar op s
- opsReadScalar :: ReadScalar op s
- opsAllocate :: Allocate op s
- opsDeallocate :: Deallocate op s
- opsCopy :: Copy op s
- opsMemoryType :: MemoryType op s
- opsCompiler :: OpCompiler op s
- opsError :: ErrorCompiler op s
- opsCall :: CallCompiler op s
- opsCopies :: Map (Space, Space) (DoCopy op s)

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

- opsFatMemory :: Bool

  If true, use reference counting. Otherwise, bare pointers.

- opsCritical :: ([BlockItem], [BlockItem])

  Code to bracket critical sections.

data Publicness Source #

Constructors

Private |
Public |

#### Instances

Instances details

Show Publicness Source # |
Instance details Defined in Futhark.CodeGen.Backends.GenericC.Monad Methods showsPrec :: Int -> Publicness -> ShowS Source # show :: Publicness -> String Source # showList :: [Publicness] -> ShowS Source #
Eq Publicness Source # |
Instance details Defined in Futhark.CodeGen.Backends.GenericC.Monad Methods (==) :: Publicness -> Publicness -> Bool Source # (/=) :: Publicness -> Publicness -> Bool Source #
Ord Publicness Source # |

Instance details

Defined in Futhark.CodeGen.Backends.GenericC.Monad

Methods

compare :: Publicness -> Publicness -> Ordering Source #

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

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

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

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

max :: Publicness -> Publicness -> Publicness Source #

min :: Publicness -> Publicness -> Publicness Source #

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

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

type ErrorCompiler op s = ErrorMsg Exp -> String -> CompilerM op s () Source #

type CallCompiler op s = [VName] -> Name -> [Exp] -> CompilerM op s () Source #

Call a function.

type PointerQuals = String -> [TypeQual] Source #

The address space qualifiers for a pointer of the given type with the given annotation.

type MemoryType op s = SpaceId -> CompilerM op s Type Source #

The type of a memory block in the given memory space.

type WriteScalar op s = Exp -> Exp -> Type -> SpaceId -> Volatility -> Exp -> CompilerM op s () Source #

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

writeScalarPointerWithQuals :: PointerQuals -> WriteScalar op s Source #

type ReadScalar op s = Exp -> Exp -> Type -> SpaceId -> Volatility -> CompilerM op s Exp Source #

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

readScalarPointerWithQuals :: PointerQuals -> ReadScalar op s Source #

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

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

type Deallocate op s = Exp -> Exp -> Exp -> SpaceId -> CompilerM op s () Source #

De-allocate the given memory block, with the given tag, with the given size,, which is in the given memory space.

data CopyBarrier Source #

Whether a copying operation should implicitly function as a barrier regarding further operations on the source. This is a rather subtle detail and is mostly useful for letting some device/GPU copies be asynchronous (#1664).

Constructors

CopyBarrier |
CopyNoBarrier | Explicit context synchronisation should be done before the source or target is used.

#### Instances

Instances details

Show CopyBarrier Source # |
Instance details Defined in Futhark.CodeGen.Backends.GenericC.Monad Methods showsPrec :: Int -> CopyBarrier -> ShowS Source # show :: CopyBarrier -> String Source # showList :: [CopyBarrier] -> ShowS Source #
Eq CopyBarrier Source # |
Instance details Defined in Futhark.CodeGen.Backends.GenericC.Monad Methods (==) :: CopyBarrier -> CopyBarrier -> Bool Source # (/=) :: CopyBarrier -> CopyBarrier -> Bool Source #

type Copy op s = CopyBarrier -> Exp -> Exp -> Space -> Exp -> Exp -> Space -> Exp -> CompilerM op s () Source #

Copy from one memory block to another.

type DoCopy op s = CopyBarrier -> PrimType -> [Count Elements Exp] -> Exp -> (Count Elements Exp, [Count Elements Exp]) -> Exp -> (Count Elements Exp, [Count Elements Exp]) -> 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.

# Monadic compiler interface

data CompilerM op s a Source #

#### Instances

Instances details

MonadState (CompilerState s) (CompilerM op s) Source # |
Instance details Defined in Futhark.CodeGen.Backends.GenericC.Monad 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 #
Applicative (CompilerM op s) Source # |

Instance details

Defined in Futhark.CodeGen.Backends.GenericC.Monad

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.GenericC.Monad 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.GenericC.Monad 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 #
MonadFreshNames (CompilerM op s) Source # |
Instance details Defined in Futhark.CodeGen.Backends.GenericC.Monad Methods getNameSource :: CompilerM op s VNameSource Source # putNameSource :: VNameSource -> CompilerM op s () Source #
MonadReader (CompilerEnv op s) (CompilerM op s) Source # |
Instance details Defined in Futhark.CodeGen.Backends.GenericC.Monad 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

- compArrayTypes :: Map ArrayType Publicness
- compEarlyDecls :: DList Definition
- compNameSrc :: VNameSource
- compUserState :: s
- compHeaderDecls :: Map HeaderSection (DList Definition)
- compLibDecls :: DList Definition
- compCtxFields :: DList (Id, Type, Maybe Exp, Maybe (Stm, Stm))
- compClearItems :: DList BlockItem
- compDeclaredMem :: [(VName, Space)]
- compItems :: DList BlockItem

#### Instances

Instances details

MonadState (CompilerState s) (CompilerM op s) Source # |
Instance details Defined in Futhark.CodeGen.Backends.GenericC.Monad 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 #

data CompilerEnv op s Source #

Constructors

CompilerEnv |

Fields

- envOperations :: Operations op s
- envCachedMem :: Map Exp VName

  Mapping memory blocks to sizes. These memory blocks are CPU memory that we know are used in particularly simple ways (no reference counting necessary). To cut down on allocator pressure, we keep these allocations around for a long time, and record their sizes so we can reuse them if possible (and realloc() when needed).

- envProvenance :: Provenance

  The provenance of an enclosing `MetaProvenance`, if any.

#### Instances

Instances details

MonadReader (CompilerEnv op s) (CompilerM op s) Source # |
Instance details Defined in Futhark.CodeGen.Backends.GenericC.Monad 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 #

getUserState :: CompilerM op s s Source #

modifyUserState :: (s -> s) -> CompilerM op s () Source #

generateProgramStruct :: CompilerM op s () Source #

runCompilerM :: Operations op s -> VNameSource -> s -> CompilerM op s a -> (a, CompilerState s) Source #

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

Used when we, inside an existing `CompilerM` action, want to generate code for a new function. Use this so that the compiler understands that previously declared memory doesn't need to be freed inside this action.

cachingMemory :: Map VName Space -> ([BlockItem] -> [Stm] -> CompilerM op s a) -> CompilerM op s a Source #

volQuals :: Volatility -> [TypeQual] Source #

rawMem :: VName -> CompilerM op s Exp Source #

item :: BlockItem -> CompilerM op s () Source #

Insert a block item in the generated code at this point.

items :: [BlockItem] -> CompilerM op s () Source #

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

Insert a statement in the generated code at this point.

stms :: [Stm] -> CompilerM op s () Source #

comment :: Text -> CompilerM op s () Source #

Insert a comment in the generated code at this point. The comment may contain linebreaks, and must not contain any comment markers.

decl :: InitGroup -> CompilerM op s () Source #

headerDecl :: HeaderSection -> Definition -> CompilerM op s () Source #

publicDef :: Text -> HeaderSection -> (Text -> (Definition, Definition)) -> CompilerM op s Text Source #

Construct a publicly visible definition using the specified name as the template. The first returned definition is put in the header file, and the second is the implementation. Returns the public name.

publicDef_ :: Text -> HeaderSection -> (Text -> (Definition, Definition)) -> CompilerM op s () Source #

As `publicDef`, but ignores the public name.

onClear :: BlockItem -> CompilerM op s () Source #

data HeaderSection Source #

In which part of the header file we put the declaration. This is to ensure that the header file remains structured and readable.

Constructors

ArrayDecl Name |
OpaqueTypeDecl Name |
OpaqueDecl Name |
EntryDecl |
MiscDecl |
InitDecl |

#### Instances

Instances details

Eq HeaderSection Source # |
Instance details Defined in Futhark.CodeGen.Backends.GenericC.Monad Methods (==) :: HeaderSection -> HeaderSection -> Bool Source # (/=) :: HeaderSection -> HeaderSection -> Bool Source #
Ord HeaderSection Source # |

Instance details

Defined in Futhark.CodeGen.Backends.GenericC.Monad

Methods

compare :: HeaderSection -> HeaderSection -> Ordering Source #

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

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

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

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

max :: HeaderSection -> HeaderSection -> HeaderSection Source #

min :: HeaderSection -> HeaderSection -> HeaderSection Source #

libDecl :: Definition -> CompilerM op s () Source #

earlyDecl :: Definition -> CompilerM op s () Source #

publicName :: Text -> CompilerM op s Text Source #

Public names must have a consitent prefix.

contextField :: Id -> Type -> Maybe Exp -> CompilerM op s () Source #

contextFieldDyn :: Id -> Type -> Stm -> Stm -> CompilerM op s () Source #

memToCType :: VName -> Space -> CompilerM op s Type Source #

cacheMem :: ToExp a => a -> CompilerM op s (Maybe VName) Source #

fatMemory :: Space -> CompilerM op s Bool Source #

rawMemCType :: Space -> CompilerM op s Type Source #

freeRawMem :: (ToExp a, ToExp b, ToExp c) => a -> b -> Space -> c -> CompilerM op s () Source #

allocRawMem :: (ToExp a, ToExp b, ToExp c) => a -> b -> Space -> c -> CompilerM op s () Source #

fatMemType :: Space -> Type Source #

declAllocatedMem :: CompilerM op s [BlockItem] Source #

freeAllocatedMem :: CompilerM op s [BlockItem] Source #

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

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

contextType :: CompilerM op s Type Source #

The generated code must define a context struct with this name.

configType :: CompilerM op s Type Source #

The generated code must define a configuration struct with this name.

localProvenance :: Provenance -> CompilerM op s a -> CompilerM op s a Source #

Locally replace (not extend!) the provenance.

askProvenance :: CompilerM op s Provenance Source #

The provenance of the closest enclosing `MetaProvenance`.

provenanceExp :: CompilerM op s Exp Source #

A C expression corresponding to the current provenance.

# Building Blocks

copyMemoryDefaultSpace :: Exp -> Exp -> Exp -> Exp -> Exp -> CompilerM op s () Source #

derefPointer :: Exp -> Exp -> Type -> Exp Source #

setMem :: (ToExp a, ToExp b) => a -> b -> Space -> CompilerM op s () Source #

allocMem :: (ToExp a, ToExp b) => a -> b -> Space -> Stm -> CompilerM op s () Source #

unRefMem :: ToExp a => a -> Space -> CompilerM op s () Source #

declMem :: VName -> Space -> CompilerM op s () Source #

resetMem :: ToExp a => a -> Space -> CompilerM op s () Source #

fatMemAlloc :: Space -> String Source #

fatMemSet :: Space -> String Source #

fatMemUnRef :: Space -> String Source #

criticalSection :: Operations op s -> [BlockItem] -> [BlockItem] Source #

module Futhark.CodeGen.Backends.SimpleRep