Module Sarek_metal.Sarek_ir_metal

val current_device : Spoc_core.Device.t option Stdlib.ref

Current device for SNative code generation (set during generate_for_device)

val current_variants : (string * (string * Sarek_ir_types.elttype list) list) list Stdlib.ref

Current kernel's variant definitions (set during generate)

Type Mapping

val mangle_name : string -> string

Mangle OCaml type name to valid C identifier (e.g., "Module.point" -> "Module_point")

val metal_type_of_elttype : Sarek_ir_types.elttype -> string

Map Sarek IR element type to Metal C type string

val metal_memspace : Sarek_ir_types.memspace -> string

Map memory space to Metal qualifier

val metal_param_type : Sarek_ir_types.elttype -> string

Map Sarek IR element type to Metal C type for kernel parameters

val metal_helper_param_type : Sarek_ir_types.elttype -> string

Map Sarek IR element type to Metal C type for helper function parameters

val metal_atomic_type_of_elttype : Sarek_ir_types.elttype -> string

Convert type to atomic type for Metal

Thread Intrinsics

val metal_thread_intrinsic : string -> string

Expression Generation

val gen_expr : Stdlib.Buffer.t -> Sarek_ir_types.expr -> unit
val gen_binop : Sarek_ir_types.binop -> string
val gen_unop : Sarek_ir_types.unop -> string
val gen_intrinsic : Stdlib.Buffer.t -> string list -> string -> Sarek_ir_types.expr list -> unit

L-value Generation

val gen_lvalue : Stdlib.Buffer.t -> Sarek_ir_types.lvalue -> unit

Statement Generation

val indent_nested : string -> string

Nested indentation level

val gen_match_pattern : Stdlib.Buffer.t -> string -> string -> string -> string list -> (string -> Sarek_ir_types.elttype list option) -> unit

Generate match case pattern with variable bindings

val gen_var_decl : Stdlib.Buffer.t -> string -> string -> Sarek_ir_types.elttype -> Sarek_ir_types.expr -> unit

Generate variable declaration with initialization

val gen_array_decl : Stdlib.Buffer.t -> string -> string -> Sarek_ir_types.elttype -> Sarek_ir_types.expr -> string -> unit

Generate array declaration

val gen_stmt : Stdlib.Buffer.t -> string -> Sarek_ir_types.stmt -> unit

Declaration Generation

val is_vec_type : Sarek_ir_types.elttype -> bool

Check if a type is a vector (requires length parameter)

val gen_param_metal : Stdlib.Buffer.t -> string list -> int -> Sarek_ir_types.decl -> int

Generate parameter with Metal buffer attributes, returns next buffer index

val gen_param : Stdlib.Buffer.t -> Sarek_ir_types.decl -> unit
val collect_atomic_vars_expr : Sarek_ir_types.expr -> string list

Collect variable names used in atomic operations

val collect_atomic_vars_lvalue : Sarek_ir_types.lvalue -> string list
val collect_atomic_vars_stmt : Sarek_ir_types.stmt -> string list
val gen_local : Stdlib.Buffer.t -> string -> string list -> Sarek_ir_types.decl -> unit

Helper Function Generation

val gen_helper_func : Stdlib.Buffer.t -> Sarek_ir_types.helper_func -> unit

Generate a helper function (Metal device function)

Kernel Generation

val generate : Sarek_ir_types.kernel -> string

Generate complete Metal source for a kernel

val generate_for_device : device:Spoc_core.Device.t -> Sarek_ir_types.kernel -> string

Generate complete Metal source with device context for SNative

val gen_variant_def : Stdlib.Buffer.t -> (string * (string * Sarek_ir_types.elttype list) list) -> unit

Generate variant type definition for Metal

val generate_with_types : types:(string * (string * Sarek_ir_types.elttype) list) list -> Sarek_ir_types.kernel -> string

Generate Metal source with custom type definitions

val generate_with_fp64 : Sarek_ir_types.kernel -> string

Generate Metal source with double precision (Metal supports FP64 natively)