srdatalog.ir.codegen.cuda.view_slots

View/handle slot mapping + view-declaration emission.

Port of src/srdatalog/codegen/target_jit/jit_view_management.nim.

Deduplicates pipeline source views by (rel_name, index, version), computes view-slot offsets for multi-view sources (e.g. Device2LevelIndex contributes >1 slot per source via the plugin), and emits the auto view_X = views[i]; block at the top of each generated kernel.

A “view spec” is the triple (rel_name, index cols, version) plus the handle_idx of the first op that referenced it. Two uses of the same relation with different index orderings are distinct views; two uses with the same (rel, idx, ver) share one view slot even across nested CJ / Cartesian handles.

Module Contents

Classes

ViewSpec

(rel_name, index, version, handle_idx) — handle_idx is the index of the FIRST op that referenced this view.

Functions

build_root_slot_map

Map <relName>_<VER>_<cols> → view-slot base for each root source. First occurrence wins; subsequent duplicates share the first’s slot.

collect_unique_view_specs

Walk the pipeline body and collect a de-duplicated list of ViewSpecs, first-occurrence order. Covers every op kind that references a view: ColumnJoin, CartesianJoin, Scan, Negation, Aggregate, BalancedScan, PositionedExtract.

compute_total_view_count

Total view slots needed for all unique sources. Nested CJ/Cart handles for the same (rel, version, index) share slots.

compute_view_slot_offsets

Map handle_idx → base slot in views[].

get_source_index

Extract column ordering from a source-bearing node.

jit_emit_view_declarations

Emit the top-of-kernel auto view_X = views[i]; block.

register_pipeline_handles

Walk the pipeline body and register every ColumnSource handle_start against the root_slots table. Mutates offsets in place — matches Nim’s registerPipelineHandles var-param signature.

source_spec_key

Unique key for a source spec: <relName>_<VERSION>_<cols_joined>. Two uses of the same relation with different index orderings → distinct keys. Includes version to keep DELTA / FULL views distinct.

spec_key

Rel_<cols>_<VER> — version-suffixed so DELTA and FULL share-key doesn’t collapse them.

API

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

(rel_name, index, version, handle_idx) — handle_idx is the index of the FIRST op that referenced this view.

handle_idx: int

None

index: list[int]

None

rel_name: str

None

version: str

None

srdatalog.ir.codegen.cuda.view_slots.build_root_slot_map(source_specs: collections.abc.Sequence[srdatalog.ir.mir.types.MirNode], rel_index_types: dict[str, str]) dict[str, int][source]

Map <relName>_<VER>_<cols> → view-slot base for each root source. First occurrence wins; subsequent duplicates share the first’s slot.

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

Walk the pipeline body and collect a de-duplicated list of ViewSpecs, first-occurrence order. Covers every op kind that references a view: ColumnJoin, CartesianJoin, Scan, Negation, Aggregate, BalancedScan, PositionedExtract.

srdatalog.ir.codegen.cuda.view_slots.compute_total_view_count(source_specs: collections.abc.Sequence[srdatalog.ir.mir.types.MirNode], rel_index_types: dict[str, str]) int[source]

Total view slots needed for all unique sources. Nested CJ/Cart handles for the same (rel, version, index) share slots.

srdatalog.ir.codegen.cuda.view_slots.compute_view_slot_offsets(source_specs: collections.abc.Sequence[srdatalog.ir.mir.types.MirNode], rel_index_types: dict[str, str]) dict[int, int][source]

Map handle_idx → base slot in views[].

Multiple handles for the same relation+version+index share one slot — nested CJ/Cart handles reference the same physical view as the root handle.

srdatalog.ir.codegen.cuda.view_slots.get_source_index(src_spec: srdatalog.ir.mir.types.MirNode) list[int][source]

Extract column ordering from a source-bearing node.

srdatalog.ir.codegen.cuda.view_slots.jit_emit_view_declarations(specs: list[srdatalog.ir.codegen.cuda.view_slots.ViewSpec], ops: list[srdatalog.ir.mir.types.MirNode], ep_source_specs: collections.abc.Sequence[srdatalog.ir.mir.types.MirNode], ctx: srdatalog.ir.codegen.cuda.context.CodeGenContext) str[source]

Emit the top-of-kernel auto view_X = views[i]; block.

Populates ctx.view_vars with both:

  • spec key (rel_idx_VER) → view_var

  • str(handle_idx) → view_var (so nested op emitters can resolve “which view is this handle referring to?” directly)

srdatalog.ir.codegen.cuda.view_slots.register_pipeline_handles(offsets: dict[int, int], pipeline: list[srdatalog.ir.mir.types.MirNode], rel_index_types: dict[str, str], root_slots: dict[str, int]) None[source]

Walk the pipeline body and register every ColumnSource handle_start against the root_slots table. Mutates offsets in place — matches Nim’s registerPipelineHandles var-param signature.

srdatalog.ir.codegen.cuda.view_slots.source_spec_key(src_spec: srdatalog.ir.mir.types.MirNode) str[source]

Unique key for a source spec: <relName>_<VERSION>_<cols_joined>. Two uses of the same relation with different index orderings → distinct keys. Includes version to keep DELTA / FULL views distinct.

srdatalog.ir.codegen.cuda.view_slots.spec_key(rel_name: str, index: list[int], version: str = '') str[source]

Rel_<cols>_<VER> — version-suffixed so DELTA and FULL share-key doesn’t collapse them.