Module Sarek_transpile

Pure OCaml-kernel-source to GPU-source transpiler.

Sarek_transpile converts an OCaml kernel source string through the full Sarek frontend (parse → type → convergence → mono → tailrec → lower) and emits GPU source code for the requested backend.

The library is FFI-free: it does not depend on spoc_core or ctypes and therefore links cleanly as bytecode and (with polyfills) under js_of_ocaml.

type backend =
  1. | CUDA
  2. | OpenCL
  3. | Metal
  4. | GLSL
  5. | WGSL

GPU backend selector.

type error =
  1. | Parse_error of string * Sarek_ast.loc
    (*

    OCaml parser or Sarek parse error with message and location.

    *)
  2. | Type_error of Sarek_error.error list
    (*

    Type-inference or constraint-solving failure.

    *)
  3. | Convergence_error of Sarek_error.error list
    (*

    Barrier-safety analysis failure.

    *)
  4. | Unsupported_native of Sarek_ast.loc
    (*

    Kernel contains [%native] which cannot be transpiled purely.

    *)
  5. | Internal_error of string
    (*

    Unexpected exception — indicates a bug, not a user error.

    *)

Structured error type. Every frontend failure is converted to one of these variants; no exception escapes of_source.

val string_of_error : error -> string

string_of_error e returns a human-readable representation of e. Intended for debugging and test output.

val of_source : backend -> string -> (string, error) Stdlib.result

of_source backend src parses src as an OCaml kernel expression and runs the full frontend pipeline:

  1. OCaml parse → ppxlib expression
  2. Sarek parse → Sarek_ast.kernel
  3. [%native] rejection
  4. Type inference (Sarek_typer.infer_kernel)
  5. Convergence check (Sarek_convergence.check_kernel)
  6. Monomorphisation, tail-recursion transform, IR lowering
  7. Code generation via sarek_codegen

Returns Ok gpu_source on success, or Error e with a structured description of the first failure encountered.

All frontend exceptions are caught and converted to error values.

val of_source_with_abi : backend -> string -> (string * string, error) Stdlib.result

of_source_with_abi backend src runs the same frontend pipeline as of_source and additionally returns the ABI descriptor as a JSON string.

  • For the WGSL backend: returns Ok (wgsl_code, abi_json) on success.
  • For all other backends: returns Error (Internal_error "ABI is only defined for the WGSL backend") without running the pipeline.

All frontend exceptions are caught and converted to error values.