Module Sarek_codegen.Sarek_ir_ptx_expr

PTX expression emitter.

Translates Sarek IR expressions to PTX instruction sequences. All emitters return the PTX register holding the result and mutate buf and alloc as side effects.

val emit_expr : Stdlib.Buffer.t -> Sarek_ir_ptx_types.reg_alloc -> Sarek_ir_ptx_types.env -> Sarek_ir_types.expr -> string

emit_expr buf alloc env expr emits PTX instructions for expr into buf and returns the register name holding the result.

Raises Sarek_ir_ptx_types.Ptx_codegen_error for IR constructs not yet covered by the PTX backend (variants, records, recursive device functions, etc.). Non-recursive helper-function calls (EApp) are inlined at the call site.

emit_value buf alloc env expr is the aggregate-aware emitter: scalar expressions delegate to emit_expr (wrapped in Scalar); record construction (ERecord) evaluates each field into an SROA register set, and field projection (ERecordField) on a local record is pure register selection (Agg values, no memory traffic — FR-020).

val emit_match_arms : Stdlib.Buffer.t -> Sarek_ir_ptx_types.reg_alloc -> Sarek_ir_ptx_types.env -> Sarek_ir_ptx_types.binding -> (Sarek_ir_types.pattern * 'a) list -> emit_arm:('a -> unit) -> unit

emit_match_arms buf alloc env scrut arms ~emit_arm emits a full match on the scrutinee binding scrut: variant scrutinees get a tag-compare branch chain (per-arm setp.eq + bra, last/catch-all arm unconditional — never selp, FR-022); tuple/record/scalar scrutinees support exactly one destructuring arm. emit_arm emits one arm body (expression or statement) with the arm's pattern variables bound arm-scoped. Raises Sarek_ir_ptx_types.Ptx_codegen_error on a non-exhaustive variant match (no catch-all arm and at least one constructor uncovered).

emit_binop buf alloc env op e1 e2 emits a binary operation. Type is inferred from the register-name prefix of the first operand.

val emit_cast : Stdlib.Buffer.t -> Sarek_ir_ptx_types.reg_alloc -> string -> Sarek_ir_types.elttype -> string

emit_cast buf alloc r_src dst_ty emits a PTX cvt.* instruction if needed and returns the destination register. Returns r_src unchanged when no conversion is needed.

val emit_intrinsic : Stdlib.Buffer.t -> Sarek_ir_ptx_types.reg_alloc -> Sarek_ir_ptx_types.env -> string list -> string -> Sarek_ir_types.expr list -> string

emit_intrinsic buf alloc env path name args emits the PTX sequence for the named Sarek intrinsic. path disambiguates module-qualified names ("of_int" resolves to f32 or f64 by its Float32/Float64 path). Raises Sarek_ir_ptx_types.Ptx_codegen_error for unknown intrinsic names.

Intrinsic dispatch registry

Each intrinsic is one entry in a per-category handler registry: the names it answers to paired with the closure that lowers them. emit_intrinsic dispatches by looking a name up there, so intrinsic_registry is what the backend actually lowers; the tables below are pinned to it entry for entry by a value-level test. A name absent from the registry raises Sarek_ir_ptx_types.Ptx_codegen_error; a name claimed by two handlers is an internal error. Exposed so the ptxas sweep gate can assemble one kernel per intrinsic name and so a new intrinsic is covered automatically.

type intrinsic_category =
  1. | Index
  2. | Transcendental
  3. | Float_ops
  4. | Bitcast
  5. | Convert
  6. | Atomic

The per-category handler registries, in dispatch order.

val intrinsic_registry : (string * intrinsic_category) list

Every intrinsic name the PTX backend lowers, with the registry that owns it, in dispatch order. Derived from the handler values themselves: an entry here is a handler that exists, and every handler has its entries here. The name tables below are checked against it — over these values, not over source text — by sarek/tests/unit/test_ptx_intrinsic_sweep.ml.

val index_intrinsic_names : string list
val transcendental_intrinsic_names : string list
val float_ops_intrinsic_names : string list
val bitcast_intrinsic_names : string list
val convert_intrinsic_names : string list
val atomic_intrinsic_names : string list
val intrinsic_names : string list

All of the above concatenated, in dispatch order — i.e. the names of intrinsic_registry.