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¶
Threaded through every emitter. Field order + names mirror Nim’s CodeGenContext 1:1 so port diffs stay local. |
|
Feature-specific codegen hooks, resolved once per kernel. Default implementations are identity / no-op; BG / WS / dedup modules will supply their own. |
|
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. |
|
Functions¶
Baseline hook implementations — no-ops / identity. Feature modules (BG, WS, dedup) override individual hooks via their own factories. |
|
Chained .prefix(…) calls. Prefix vars go through sanitize_var_name first (keyword escape). |
|
All-sequential variant — every key applied via prefix_seq. |
|
Chained .prefix(…) with last key using .prefix_lower_bound(). |
|
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. |
|
Unique handle variable name |
|
Key for handle/view lookup: |
|
Root handle: |
|
Shorthand — inline the |
|
Bump the per-context counter and return |
|
|
|
|
|
Look up the index type for a relation. Empty string = DSAI default. |
|
Base view slot for a source. Falls back to |
|
Current indentation string (2-space levels). |
|
Fresh context with Nim-matching defaults. |
|
Append |
|
Return a shallow copy of |
Data¶
API¶
- 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.
- hooks: srdatalog.ir.codegen.cuda.context.CodeGenHooks¶
‘field(…)’
- neg_pre_narrow: dict[int, srdatalog.ir.codegen.cuda.context.NegPreNarrowInfo]¶
‘field(…)’
- 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.
- 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_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_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_idxwhen 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
_valto 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
ctxwithvar_nameadded tobound_vars.