Module Sarek_codegen.Sarek_ir_warp_text

Source-text lowering of the warp operations, shared by the source backends (CUDA C / HIP, OpenCL C, GLSL, MSL, WGSL).

The semantics are those of Sarek_warp: every lane of the warp participates, and the result for lane i is the table of docs/plans/warp-shuffle-intake-2026-10-17.md section 4.3. The vendors differ in what they leave undefined (an out-of-range delta, the high bits of a lane operand), so no backend passes the Sarek operand to a relative shuffle unguarded:

A shuffle is emitted as a call of a small helper function, defined once per (operation, value type) the kernel uses, so an operand expression is evaluated exactly once. The value type is part of the IR name (Sarek_warp.ir_name), so no backend infers expression types.

type dialect =
  1. | Cuda
  2. | Opencl
  3. | Glsl
  4. | Metal
  5. | Wgsl
val value_ctype : dialect -> Sarek_warp.value_type -> string
val helper_name : Sarek_warp.op -> Sarek_warp.value_type -> string

The name of the helper a shuffle call is emitted as.

val shuffles_of_calls : Sarek_warp.call list -> (Sarek_warp.op * Sarek_warp.value_type) list
val uses_shuffle : Sarek_warp.call list -> bool

The source-lane expression

Written once over a tiny syntax record, instantiated for C-like targets, GLSL and WGSL. x is the signed lane operand, lane and w the lane id and the warp width, both unsigned.

type syntax = {
  1. to_u32 : string -> string;
  2. one : string;
    (*

    the literal 1 as an unsigned

    *)
  3. select : cond:string -> then_:string -> else_:string -> string;
}
val c_syntax : syntax
val cuda_syntax : syntax
val glsl_syntax : syntax
val wgsl_syntax : syntax
val source_lane_expr : syntax -> Sarek_warp.op -> x:string -> lane:string -> w:string -> string

The unsigned source lane for operation op, operand expression x.

Calls

The spelling of a warp operation at its call site.

val metal_lane : string
val metal_width : string
val wgsl_lane : string
val wgsl_width : string
val constant_expr : dialect -> Sarek_warp.call -> string

warp_size and lane_id, as int32 expressions.

val emit_call : dialect -> bad_arity:(string -> int -> int -> unit) -> Stdlib.Buffer.t -> Sarek_warp.call -> string list -> unit

Emit warp_size, lane_id or a shuffle call. args are the already rendered operand strings; bad_arity raises the backend's located arity error.

Helper definitions

val cuda_helper : Sarek_warp.op -> Sarek_warp.value_type -> string
val index_shuffle : dialect -> Sarek_warp.value_type -> src:string -> string
val c_helper : dialect -> Sarek_warp.op -> Sarek_warp.value_type -> string
val glsl_helper : Sarek_warp.op -> Sarek_warp.value_type -> string
val wgsl_helper : Sarek_warp.op -> Sarek_warp.value_type -> string
val helper_source : dialect -> Sarek_warp.op -> Sarek_warp.value_type -> string

Preambles

val helpers : dialect -> Sarek_warp.call list -> string

The helper definitions for the shuffles in calls.

val opencl_pragmas : Sarek_warp.call list -> string

OpenCL C extension pragmas for calls: sub-group built-ins, and the shuffle extension when a shuffle is used. Empty when the kernel uses no warp operation.

val glsl_extensions : Sarek_warp.call list -> string

GLSL #extension lines for calls (placed in the header, before any declaration).

val wgsl_enable : 'a list -> string

The WGSL directive and module-scope state: enable subgroups; must come first in the module; the lane and width are module-scope private variables that main assigns from its subgroup built-ins, so helper functions can read them.

val wgsl_state : 'a list -> string
val wgsl_main_params : 'a list -> string

Extra main parameters and prologue for WGSL.

val wgsl_main_prologue : 'a list -> string
val metal_params : 'a list -> string

Extra Metal kernel parameters (the lane and the width are attributes of the kernel entry, not readable from a helper, so they are passed on).