srdatalog.ir.codegen.cuda.context

Code-generation context + C++ expression helpers.

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

The CodeGenContext is the big state object threaded through every emitter in the JIT backend. It tracks bound variables, handle/view name tables, indentation, thread group size, and the menagerie of feature-flag state (work-stealing, block-group, tiled Cartesian, dedup-hash, etc.). Every field mirrors the Nim source so field names line up 1:1 with emitter ports.

CodeGenHooks lets feature-specific modules override emit/materialize and runner-level hooks without checking flags inline (same role as Halide’s schedule/algorithm split). Defaults are no-ops / identity; BG / WS / dedup modules will override individual hooks in later commits.

The gen_* helpers at the bottom dispatch through the index plugin registry, so custom index types (like Device2LevelIndex) can override C++ expression shapes without touching emitter code.

Module Contents

Classes

CodeGenContext

Threaded through every emitter. Field order + names mirror Nim’s CodeGenContext 1:1 so port diffs stay local.

CodeGenHooks

Feature-specific codegen hooks, resolved once per kernel. Default implementations are identity / no-op; BG / WS / dedup modules will supply their own.

NegPreNarrowInfo

Pre-narrowing info for a negation handle applied before the Cartesian loop. Pre-cartesian prefix vars are applied cooperatively once; in-cartesian vars are applied per-thread via prefix_seq inside the loop.

RunnerGenState

Functions

dec_indent

default_hooks

Baseline hook implementations — no-ops / identity. Feature modules (BG, WS, dedup) override individual hooks via their own factories.

gen_chained_prefix_calls

Chained .prefix(…) calls. Prefix vars go through sanitize_var_name first (keyword escape).

gen_chained_prefix_calls_seq

All-sequential variant — every key applied via prefix_seq.

gen_chained_prefix_with_last_lower_bound

Chained .prefix(…) with last key using .prefix_lower_bound().

gen_child

gen_child_range

gen_degree

gen_get_value

gen_get_value_at

gen_handle_state_key

Semantic key tying together (rel, idx, bound prefixes, version). Lets handle reuse work across different MIR handleIdx values that point at the same narrowed trie path.

gen_handle_var_name

Unique handle variable name h_<rel>_<handle>_<n>.

gen_index_spec_key

Key for handle/view lookup: Rel_<cols joined by _> optionally suffixed with _<VER>. Differentiates DELTA from FULL sources that share a relation + index.

gen_iterators

gen_root_handle

Root handle: HandleType(0, view.num_rows_, 0) (DSAI default).

gen_root_handle_from_view_idx

Shorthand — inline the views[i] form.

gen_unique_name

Bump the per-context counter and return <prefix>_<n>.

gen_valid

gen_view_access

views[i] — positional view access.

gen_view_var_name

view_<rel>_<handle> — readable view variable name.

get_rel_index_type

Look up the index type for a relation. Empty string = DSAI default.

get_view_slot_base

Base view slot for a source. Falls back to handle_idx when no override is set (single-view / legacy case).

inc_indent

ind

Current indentation string (2-space levels).

is_var_bound

new_code_gen_context

Fresh context with Nim-matching defaults.

sanitize_var_name

Append _val to any C++ keyword so it’s safe as a C++ identifier.

with_bound_var

Return a shallow copy of ctx with var_name added to bound_vars.

Data

API

srdatalog.ir.codegen.cuda.context.CPP_KEYWORDS: frozenset[str]

‘frozenset(…)’

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

Threaded through every emitter. Field order + names mirror Nim’s CodeGenContext 1:1 so port diffs stay local.

balanced_idx1: str = <Multiline-String>
balanced_idx2: str = <Multiline-String>
bg_cumulative_var: str = <Multiline-String>
bg_done_var: str = <Multiline-String>
bg_enabled: bool

False

bg_histogram_mode: bool

False

bg_warp_begin_var: str = <Multiline-String>
bg_warp_end_var: str = <Multiline-String>
bound_vars: list[str]

‘field(…)’

cartesian_as_product: bool

False

cartesian_bound_vars: list[str]

‘field(…)’

debug: bool

True

dedup_hash_enabled: bool

False

dedup_hash_vars: list[str]

‘field(…)’

group_size: int

32

handle_vars: dict[str, str]

‘field(…)’

hooks: srdatalog.ir.codegen.cuda.context.CodeGenHooks

‘field(…)’

indent: int

2

inside_cartesian: bool

False

is_counting: bool

False

is_fan_out_explore: bool

False

is_jit_mode: bool

False

is_leaf_level: bool

False

name_counter: int

0

neg_pre_narrow: dict[int, srdatalog.ir.codegen.cuda.context.NegPreNarrowInfo]

‘field(…)’

output_var_name: str

‘output’

output_vars: dict[str, str]

‘field(…)’

parent_tile_var: str

‘tile’

rel_index_types: dict[str, str]

‘field(…)’

scalar_mode: bool

False

tile_var: str

‘tile’

tiled_cartesian_ballot_done: bool

False

tiled_cartesian_enabled: bool

False

tiled_cartesian_valid_var: str = <Multiline-String>
view_slot_offsets: dict[int, int]

‘field(…)’

view_vars: dict[str, str]

‘field(…)’

ws_cartesian_bound_vars: list[str]

‘field(…)’

ws_cartesian_valid_var: str = <Multiline-String>
ws_enabled: bool

False

ws_has_cartesian: bool

False

ws_level: int

0

ws_live_handles: list[tuple]

‘field(…)’

ws_queue_var: str = <Multiline-String>
ws_range_board_var: str = <Multiline-String>
class srdatalog.ir.codegen.cuda.context.CodeGenHooks[source]

Feature-specific codegen hooks, resolved once per kernel. Default implementations are identity / no-op; BG / WS / dedup modules will supply their own.

emit_count: collections.abc.Callable[[str, str, bool, CodeGenContext], str] | None

None

emit_execute_body: collections.abc.Callable[[srdatalog.ir.codegen.cuda.context.RunnerGenState], str] | None

None

emit_extra_kernels: collections.abc.Callable[[srdatalog.ir.codegen.cuda.context.RunnerGenState], str] | None

None

emit_extra_types: collections.abc.Callable[[srdatalog.ir.codegen.cuda.context.RunnerGenState], str] | None

None

emit_materialize: collections.abc.Callable[[str, str, list[str], bool, CodeGenContext], str] | None

None

emit_phase_methods: collections.abc.Callable[[srdatalog.ir.codegen.cuda.context.RunnerGenState], str] | None

None

post_column_join: collections.abc.Callable[[Any, CodeGenContext], None] | None

None

pre_cartesian_join: collections.abc.Callable[[Any, list[Any], CodeGenContext], None] | None

None

pre_column_join: collections.abc.Callable[[Any, CodeGenContext], None] | None

None

root_column_join: collections.abc.Callable[[Any, CodeGenContext, str], str] | None

None

wrap_emit: collections.abc.Callable[[str, CodeGenContext], str] | None

None

class srdatalog.ir.codegen.cuda.context.NegPreNarrowInfo[source]

Pre-narrowing info for a negation handle applied before the Cartesian loop. Pre-cartesian prefix vars are applied cooperatively once; in-cartesian vars are applied per-thread via prefix_seq inside the loop.

in_cartesian_vars: list[str]

‘field(…)’

index_type: str = <Multiline-String>
pre_consts: list[tuple[int, int]]

‘field(…)’

pre_vars: list[str]

‘field(…)’

rel_name: str = <Multiline-String>
var_name: str = <Multiline-String>
view_var: str = <Multiline-String>
class srdatalog.ir.codegen.cuda.context.RunnerGenState[source]
db_type_name: str = <Multiline-String>
dest_arities: list[int]

‘field(…)’

first_index: list[int]

‘field(…)’

first_schema: str = <Multiline-String>
first_version: str = <Multiline-String>
is_balanced: bool

False

is_block_group: bool

False

is_count: bool

False

is_dedup_hash: bool

False

is_work_stealing: bool

False

mutable_pipe: list[Any]

‘field(…)’

node: Any

None

rel_index_types: dict[str, str]

‘field(…)’

rule_name: str = <Multiline-String>
runner_prefix: str = <Multiline-String>
total_view_count: int

0

srdatalog.ir.codegen.cuda.context.dec_indent(ctx: srdatalog.ir.codegen.cuda.context.CodeGenContext) None[source]
srdatalog.ir.codegen.cuda.context.default_hooks() srdatalog.ir.codegen.cuda.context.CodeGenHooks[source]

Baseline hook implementations — no-ops / identity. Feature modules (BG, WS, dedup) override individual hooks via their own factories.

srdatalog.ir.codegen.cuda.context.gen_chained_prefix_calls(parent_handle: str, prefix_vars: list[str], view_var: str, cartesian_bound_vars: list[str] | None = None, scalar_mode: bool = False, index_type: str = '') str[source]

Chained .prefix(…) calls. Prefix vars go through sanitize_var_name first (keyword escape).

srdatalog.ir.codegen.cuda.context.gen_chained_prefix_calls_seq(parent_handle: str, prefix_vars: list[str], view_var: str, index_type: str = '') str[source]

All-sequential variant — every key applied via prefix_seq.

srdatalog.ir.codegen.cuda.context.gen_chained_prefix_with_last_lower_bound(parent_handle: str, prefix_vars: list[str], view_var: str, cartesian_bound_vars: list[str] | None = None, scalar_mode: bool = False, index_type: str = '') str[source]

Chained .prefix(…) with last key using .prefix_lower_bound().

srdatalog.ir.codegen.cuda.context.gen_child(handle: str, idx: str, index_type: str = '') str[source]
srdatalog.ir.codegen.cuda.context.gen_child_range(handle: str, pos: str, key: str, tile: str, view_var: str, index_type: str = '') str[source]
srdatalog.ir.codegen.cuda.context.gen_degree(handle: str, index_type: str = '') str[source]
srdatalog.ir.codegen.cuda.context.gen_get_value(view_var: str, col: int, pos: str, index_type: str = '') str[source]
srdatalog.ir.codegen.cuda.context.gen_get_value_at(handle: str, view_var: str, idx: str, index_type: str = '') str[source]
srdatalog.ir.codegen.cuda.context.gen_handle_state_key(rel_name: str, index: list[int], bound_prefixes: list[str], version: str = '') str[source]

Semantic key tying together (rel, idx, bound prefixes, version). Lets handle reuse work across different MIR handleIdx values that point at the same narrowed trie path.

srdatalog.ir.codegen.cuda.context.gen_handle_var_name(rel_name: str, handle_idx: int, ctx: srdatalog.ir.codegen.cuda.context.CodeGenContext) str[source]

Unique handle variable name h_<rel>_<handle>_<n>.

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

Key for handle/view lookup: Rel_<cols joined by _> optionally suffixed with _<VER>. Differentiates DELTA from FULL sources that share a relation + index.

srdatalog.ir.codegen.cuda.context.gen_iterators(handle: str, view_var: str, index_type: str = '') str[source]
srdatalog.ir.codegen.cuda.context.gen_root_handle(view_var: str, index_type: str = '') str[source]

Root handle: HandleType(0, view.num_rows_, 0) (DSAI default).

srdatalog.ir.codegen.cuda.context.gen_root_handle_from_view_idx(view_idx: int, index_type: str = '') str[source]

Shorthand — inline the views[i] form.

srdatalog.ir.codegen.cuda.context.gen_unique_name(ctx: srdatalog.ir.codegen.cuda.context.CodeGenContext, prefix: str) str[source]

Bump the per-context counter and return <prefix>_<n>.

srdatalog.ir.codegen.cuda.context.gen_valid(handle: str, index_type: str = '') str[source]
srdatalog.ir.codegen.cuda.context.gen_view_access(handle_idx: int) str[source]

views[i] — positional view access.

srdatalog.ir.codegen.cuda.context.gen_view_var_name(rel_name: str, handle_idx: int) str[source]

view_<rel>_<handle> — readable view variable name.

srdatalog.ir.codegen.cuda.context.get_rel_index_type(ctx: srdatalog.ir.codegen.cuda.context.CodeGenContext, rel_name: str) str[source]

Look up the index type for a relation. Empty string = DSAI default.

srdatalog.ir.codegen.cuda.context.get_view_slot_base(ctx: srdatalog.ir.codegen.cuda.context.CodeGenContext, handle_idx: int) int[source]

Base view slot for a source. Falls back to handle_idx when no override is set (single-view / legacy case).

srdatalog.ir.codegen.cuda.context.inc_indent(ctx: srdatalog.ir.codegen.cuda.context.CodeGenContext) None[source]
srdatalog.ir.codegen.cuda.context.ind(ctx: srdatalog.ir.codegen.cuda.context.CodeGenContext) str[source]

Current indentation string (2-space levels).

srdatalog.ir.codegen.cuda.context.is_var_bound(ctx: srdatalog.ir.codegen.cuda.context.CodeGenContext, var_name: str) bool[source]
srdatalog.ir.codegen.cuda.context.new_code_gen_context() srdatalog.ir.codegen.cuda.context.CodeGenContext[source]

Fresh context with Nim-matching defaults.

srdatalog.ir.codegen.cuda.context.sanitize_var_name(name: str) str[source]

Append _val to any C++ keyword so it’s safe as a C++ identifier.

srdatalog.ir.codegen.cuda.context.with_bound_var(ctx: srdatalog.ir.codegen.cuda.context.CodeGenContext, var_name: str) srdatalog.ir.codegen.cuda.context.CodeGenContext[source]

Return a shallow copy of ctx with var_name added to bound_vars.