srdatalog.ir.codegen.cuda.helpers

Shared codegen helpers for the batchfile emitter.

Ported from batchfile_gen_helpers.py on python-api-notemplate. The view-spec collection and pipeline walk now consume our mir_types nodes directly (ColumnSource/Scan/etc. use rel_name+index rather than a bundled FactIndex); CodeGenContext keeps the same state shape so follow-up batchfile emission can slot in unchanged.

Also fixes a latent bug in the original single-source branch of collect_unique_view_specs: isinstance(op, Scan, Negation, Aggregate) was missing the tuple parens and would have raised TypeError the moment the code actually ran. Rewritten as isinstance(op, (m.Scan, m.Negation, m.Aggregate)).

Module Contents

Classes

CodeGenContext

State threaded through batchfile emission.

ViewSpec

One distinct (rel_name, version, index) tuple read by a pipeline. handle_idx is the pipeline-local slot the node was assigned (carried over from the MIR node’s handle_start field; -1 means unassigned).

Functions

assign_handles

Placeholder: assign handle_start slots to each source/op in the pipeline. Stub matches mhk’s TODO-only version; a proper pass will live alongside batchfile codegen.

collect_unique_view_specs

Walk pipeline body ops and extract a de-duplicated list of the source views they consume. Order is first-occurrence (keeps the view-declaration layout stable).

emit_view_declarations

Top-of-kernel view declarations: emit using ViewType/HandleType aliases then one auto view_X = views[i]; per unique source spec.

find_source_idx

Locate a source node in a pipeline’s source_specs. version=None ignores version when matching. Returns -1 on miss.

spec_key

Canonical key for a (rel, ver, idx) triple used by the view-spec dedupe table and by downstream handle-to-view lookups.

API

class srdatalog.ir.codegen.cuda.helpers.CodeGenContext[source]

State threaded through batchfile emission.

Mirrors mhk’s CodeGenContext shape so handlers that get ported later can slot in without further adaptation. view_vars is intentionally typed as an unconstrained dict — mhk’s code stores both spec-key strings and integer cursor handles in it.

cartesian_bound_vars: list[str]

‘field(…)’

inside_cartesian_join: bool

False

is_counting: bool

None

is_jit_mode: bool

None

output_name: str

None

output_vars: dict[str, str]

‘field(…)’

set_output_vars(dests: collections.abc.Sequence[srdatalog.ir.mir.types.InsertInto]) None[source]

Each dest relation points at an OutputContext var; the first dest is the “active” one named output_ctx, subsequent dests share the buffer and record nothing during the count phase.

view_vars: dict

‘field(…)’

class srdatalog.ir.codegen.cuda.helpers.ViewSpec[source]

One distinct (rel_name, version, index) tuple read by a pipeline. handle_idx is the pipeline-local slot the node was assigned (carried over from the MIR node’s handle_start field; -1 means unassigned).

handle_idx: int

None

index: list[int]

None

rel_name: str

None

version: srdatalog.ir.hir.types.Version

None

srdatalog.ir.codegen.cuda.helpers.assign_handles(pipeline: srdatalog.ir.mir.types.ExecutePipeline) srdatalog.ir.mir.types.ExecutePipeline[source]

Placeholder: assign handle_start slots to each source/op in the pipeline. Stub matches mhk’s TODO-only version; a proper pass will live alongside batchfile codegen.

srdatalog.ir.codegen.cuda.helpers.collect_unique_view_specs(ops: list[srdatalog.ir.mir.types.MirNode]) list[srdatalog.ir.codegen.cuda.helpers.ViewSpec][source]

Walk pipeline body ops and extract a de-duplicated list of the source views they consume. Order is first-occurrence (keeps the view-declaration layout stable).

srdatalog.ir.codegen.cuda.helpers.emit_view_declarations(pipeline: srdatalog.ir.mir.types.ExecutePipeline, ctx: srdatalog.ir.codegen.cuda.helpers.CodeGenContext) str[source]

Top-of-kernel view declarations: emit using ViewType/HandleType aliases then one auto view_X = views[i]; per unique source spec.

Side effects on ctx.view_vars:

  • spec_key(rel, ver, idx) -> view_<rel>_<idx>_<VER> for every unique source, so downstream handlers can resolve a view by spec.

  • Each body-op’s handle_start int is mapped to its owning view_var, letting later handle-based codegen look up the C++ name directly.

srdatalog.ir.codegen.cuda.helpers.find_source_idx(specs: collections.abc.Sequence[srdatalog.ir.mir.types.MirNode], rel_name: str, index: list[int], version: srdatalog.ir.hir.types.Version | None) int[source]

Locate a source node in a pipeline’s source_specs. version=None ignores version when matching. Returns -1 on miss.

srdatalog.ir.codegen.cuda.helpers.spec_key(rel_name: str, version: srdatalog.ir.hir.types.Version, index: list[int]) str[source]

Canonical key for a (rel, ver, idx) triple used by the view-spec dedupe table and by downstream handle-to-view lookups.