Module Sarek_ppx_lib.Sarek_ast

type loc = {
  1. loc_file : string;
  2. loc_line : int;
  3. loc_col : int;
  4. loc_end_line : int;
  5. loc_end_col : int;
}

Source locations

val dummy_loc : loc
val loc_of_ppxlib : Ppxlib.Location.t -> loc
val loc_to_ppxlib : loc -> Ppxlib.Location.t
type memspace =
  1. | Local
    (*

    Thread-private memory

    *)
  2. | Shared
    (*

    Block-shared memory

    *)
  3. | Global
    (*

    Global device memory

    *)

Memory spaces for arrays

type type_expr =
  1. | TEVar of string
    (*

    'a, 'b - type variables

    *)
  2. | TEConstr of string * type_expr list
    (*

    int32, float32 vector, etc.

    *)
  3. | TEArrow of type_expr * type_expr
    (*

    a -> b

    *)
  4. | TETuple of type_expr list
    (*

    a * b

    *)

Type syntax (what user writes, before inference)

type binop =
  1. | Add
  2. | Sub
  3. | Mul
  4. | Div
  5. | Mod
  6. | And
  7. | Or
  8. | Eq
  9. | Ne
  10. | Lt
  11. | Le
  12. | Gt
  13. | Ge
  14. | Land
  15. | Lor
  16. | Lxor
  17. | Lsl
  18. | Lsr
  19. | Asr
    (*

    Bitwise ops

    *)

Binary operators - single constructor each, type resolved during typing

type unop =
  1. | Neg
    (*

    Arithmetic negation

    *)
  2. | Not
    (*

    Logical not

    *)
  3. | Lnot
    (*

    Bitwise not

    *)

Unary operators

type pattern = {
  1. pat : pattern_desc;
  2. pat_loc : loc;
}

Patterns for match expressions

and pattern_desc =
  1. | PAny
    (*

    _

    *)
  2. | PVar of string
    (*

    x

    *)
  3. | PConstr of string * pattern option
    (*

    Some x, None

    *)
  4. | PTuple of pattern list
    (*

    (a, b)

    *)
type param = {
  1. param_name : string;
  2. param_type : type_expr;
  3. param_loc : loc;
}

Kernel parameters - defined before expr so ELetRec can reference it

type expr = {
  1. e : expr_desc;
  2. expr_loc : loc;
}

Expressions

and expr_desc =
  1. | EUnit
  2. | EBool of bool
  3. | EInt of int
    (*

    int literal (will be int32 in kernel)

    *)
  4. | EInt32 of int32
  5. | EInt64 of int64
  6. | EFloat of float
    (*

    float32

    *)
  7. | EDouble of float
    (*

    float64

    *)
  8. | EVar of string
  9. | EVecGet of expr * expr
    (*

    v.i - vector element access

    *)
  10. | EVecSet of expr * expr * expr
    (*

    v.i <- x

    *)
  11. | EArrGet of expr * expr
    (*

    a.(i) - array element access

    *)
  12. | EArrSet of expr * expr * expr
    (*

    a.(i) <- x

    *)
  13. | EFieldGet of expr * string
    (*

    r.field

    *)
  14. | EFieldSet of expr * string * expr
    (*

    r.field <- x

    *)
  15. | EBinop of binop * expr * expr
  16. | EUnop of unop * expr
  17. | EApp of expr * expr list
    (*

    f x y

    *)
  18. | EAssign of string * expr
    (*

    x := e

    *)
  19. | ELet of string * type_expr option * expr * expr
    (*

    let x : t = e1 in e2

    *)
  20. | ELetRec of string * param list * type_expr option * expr * expr
    (*

    let rec f params : ret_ty = body in cont

    *)
  21. | ELetMut of string * type_expr option * expr * expr
    (*

    let mutable x = ...

    *)
  22. | EIf of expr * expr * expr option
    (*

    if c then a else b

    *)
  23. | EFor of string * expr * expr * for_dir * expr
    (*

    for i = a to/downto b do ... done

    *)
  24. | EWhile of expr * expr
  25. | ESeq of expr * expr
  26. | EMatch of expr * (pattern * expr) list
  27. | ERecord of string option * (string * expr) list
    (*

    Record literal with optional type name

    *)
  28. | EConstr of string * expr option
    (*

    Constructor application

    *)
  29. | ETuple of expr list
    (*

    (a, b, c)

    *)
  30. | EReturn of expr
  31. | ECreateArray of expr * type_expr * memspace
  32. | EGlobalRef of string
    (*

    Reference to OCaml value by name

    *)
  33. | ENative of {
    1. gpu : Ppxlib.expression;
      (*

      fun dev -> "cuda/opencl code"

      *)
    2. ocaml : Ppxlib.expression;
      (*

      fun arg1 arg2 ... -> OCaml fallback

      *)
    }
    (*

    Native code with GPU string generator and OCaml fallback function. Usage: %native (fun dev -> "code"), (fun x y -> ...) x y The result is a function that takes the same args as the ocaml fallback.

    *)
  34. | EPragma of string list * expr
    (*

    pragma "unroll" body

    *)
  35. | ETyped of expr * type_expr
  36. | EOpen of string list * expr
    (*

    let open M.N in e

    *)
  37. | ELetShared of string * type_expr * expr option * expr
    (*

    let%shared name : elem_type = size in body

    *)
  38. | ESuperstep of string * bool * expr * expr
    (*

    let%superstep ~divergent name = body in cont

    *)
and for_dir =
  1. | Upto
  2. | Downto
type type_decl =
  1. | Type_record of {
    1. tdecl_name : string;
    2. tdecl_module : string option;
    3. tdecl_fields : (string * bool * type_expr) list;
      (*

      name, mutable, type

      *)
    4. tdecl_loc : loc;
    }
  2. | Type_variant of {
    1. tdecl_name : string;
    2. tdecl_module : string option;
    3. tdecl_constructors : (string * type_expr option) list;
    4. tdecl_loc : loc;
    }

Kernel-local type declarations

type module_item =
  1. | MConst of string * type_expr * expr
    (*

    let name : ty = expr

    *)
  2. | MFun of string * bool * param list * expr
    (*

    MFun(name, is_recursive, params, body)

    *)

Module-level items (constants or functions) within a kernel payload

type kernel = {
  1. kern_name : string option;
    (*

    None for anonymous kernels

    *)
  2. kern_types : type_decl list;
    (*

    Type declarations visible in body

    *)
  3. kern_module_items : module_item list;
    (*

    Module-level items visible in body

    *)
  4. kern_external_item_count : int;
    (*

    Number of items in kern_module_items that are external (from @sarek.module). First N items are external, rest are inline.

    *)
  5. kern_params : param list;
  6. kern_body : expr;
  7. kern_loc : loc;
}

A complete kernel definition

val pp_type_expr : Stdlib.Format.formatter -> type_expr -> unit

Pretty printing for debugging

val pp_binop : Stdlib.Format.formatter -> binop -> unit
val pp_unop : Stdlib.Format.formatter -> unop -> unit
val pp_memspace : Stdlib.Format.formatter -> memspace -> unit
val pp_pattern : Stdlib.Format.formatter -> pattern -> unit
val pp_expr : Stdlib.Format.formatter -> expr -> unit
val pp_param : Stdlib.Format.formatter -> param -> unit
val pp_kernel : Stdlib.Format.formatter -> kernel -> unit