srdatalog.ir.codegen.cuda.main_file

Main C++ file emission — the top-level compile unit.

Port of src/srdatalog/codegen.nim:codegenFixpointRuleSets (the parts that build mir_cpp_str). The main file ties all the per-rule JIT batch files together into one compilable source containing:

  1. Relation typedefs (AST::RelationSchema per relation)

  2. Database blueprint (AST::Database<rels…>)

  3. Device DB aliases (SemiNaiveDatabase<Blueprint, DeviceRelationType>)

  4. GPU runtime #includes

  5. JitRunner_ forward declaration structs (from complete_runner’s decl output)

  6. Empty namespace _Plans {} (placeholder, was TMP-only)

  7. <Ruleset>_Runner struct with:

    • using DB = _DB

    • load_data() (generates CSV input loading for every relation with input_file)

    • Per-step step_N(db, max_iterations) methods — each is just the content of gen_step_body for that step

    • run(db, max_iterations) dispatcher that calls step_N with chrono timing + console log, plus print_size stats for every relation tagged print_size=True

Top-level entry: gen_main_file_content(...) returns the full string.

Scope caveats:

  • Skips the Nim-only cpp_str = ...AST::Fixpoint<...> AST dump (removed from Nim too — the comment at codegen.nim:148 confirms)

  • Canonical index tracking for print_size stats is a simplified port (uses relation’s declared canonical index when available; falls back to default column order otherwise).

  • User-level datalog_db(<BlueprintName>, <DBCode>) emission is caller responsibility — not part of this module.

Module Contents

Functions

gen_db_alias

using <Ruleset>_DB = AST::Database<...>; — matches the Nim genRelCpp emission (codegen.nim:224).

gen_db_type_alias_for_batch

DB blueprint + DeviceDB alias inlined into each batch file so JitRunner_<rule>::DB resolves. Mirrors the dbTypeAlias Nim passes to JitBatchManager.setDbTypeAlias.

gen_extern_c_shim

Emit an extern "C" shim the Python ctypes loader can call.

gen_kernel_decls_block

Device DB aliases + GPU runtime includes + JitRunner forward declaration structs. runner_decls[rule_name] is the decl string returned by gen_complete_runner — mirrors Nim’s collecting of ruleDecls[ruleName] in codegen.nim:357.

gen_load_data_method

Emit the load_data static template method — reads CSVs for every relation with input_file set.

gen_main_file_content

Emit the full main-file string — mirrors Nim’s mir_cpp_str.

gen_main_file_preamble

Top-of-file preamble needed for main.cpp to compile as its own TU. In Nim’s pipeline these using-directives come from the .nim driver; for the standalone Python path we emit them explicitly.

gen_relation_typedefs

Emit using <Name> = AST::RelationSchema<decltype("<Name>"_s), <Semiring>, std::tuple<<types>>>; for each relation decl. Used in the main-file scope where using namespace SRDatalog::AST makes the unqualified AST:: resolve.

gen_run_dispatcher_file

Emit the Runner::run() out-of-line definition in its own TU. Contains the step_0..step_N dispatch + print_size block.

gen_runner_struct

Emit <Ruleset>_Runner — the main orchestrator struct.

gen_runner_struct_declonly

Non-template Runner struct DECLARATION (for main.cpp).

gen_schema_definitions_for_batch

Schema definitions inlined into each jit_batch_N.cpp. Mirrors Nim’s collectSchemaDefinitions output: fully-qualified SRDatalog::AST::RelationSchema (no using namespace umbrella in the batch file scope) plus the Literals using-directive for the _s UDL.

gen_step_shard_file

Emit a standalone .cpp for one step_N out-of-line definition.

gen_unity_main_file_content

Emit ONE large .cpp that contains everything a project needs — preamble, schemas, DB alias, every JitRunner_X struct body, the host-side _Runner with step bodies, and the extern “C” shim.

API

srdatalog.ir.codegen.cuda.main_file.gen_db_alias(ruleset_name: str, decls: list[srdatalog.ir.hir.types.RelationDecl]) str[source]

using <Ruleset>_DB = AST::Database<...>; — matches the Nim genRelCpp emission (codegen.nim:224).

srdatalog.ir.codegen.cuda.main_file.gen_db_type_alias_for_batch(ruleset_name: str, decls: list[srdatalog.ir.hir.types.RelationDecl]) str[source]

DB blueprint + DeviceDB alias inlined into each batch file so JitRunner_<rule>::DB resolves. Mirrors the dbTypeAlias Nim passes to JitBatchManager.setDbTypeAlias.

ruleset_name here is the ext_db name (e.g. “TrianglePlan_DB”), not the bare ruleset; the resulting types are <ruleset>_Blueprint and <ruleset>_DeviceDB.

srdatalog.ir.codegen.cuda.main_file.gen_extern_c_shim(ruleset_name: str, decls: list[srdatalog.ir.hir.types.RelationDecl]) str[source]

Emit an extern "C" shim the Python ctypes loader can call.

Exposes (all C-ABI, return 0 on success / nonzero on error):

  • srdatalog_init() — init CUDA

  • srdatalog_load_csv(rel, path) — load_from_file for one relation

  • srdatalog_run(max_iters) — copy-to-device + _Runner::run

  • srdatalog_shutdown() — free host DB

  • srdatalog_size(rel_name) — FULL_VER canonical index size

The shim uses a file-scope HostDB* holding the live SemiNaiveDatabase so Python can stage data via multiple load_csv calls before run.

srdatalog.ir.codegen.cuda.main_file.gen_kernel_decls_block(ruleset_name: str, decls: list[srdatalog.ir.hir.types.RelationDecl], runner_decls: dict[str, str], cache_dir_hint: str, standalone_order: bool = False) str[source]

Device DB aliases + GPU runtime includes + JitRunner forward declaration structs. runner_decls[rule_name] is the decl string returned by gen_complete_runner — mirrors Nim’s collecting of ruleDecls[ruleName] in codegen.nim:357.

srdatalog.ir.codegen.cuda.main_file.gen_load_data_method(decls: list[srdatalog.ir.hir.types.RelationDecl]) str[source]

Emit the load_data static template method — reads CSVs for every relation with input_file set.

srdatalog.ir.codegen.cuda.main_file.gen_main_file_content(ruleset_name: str, decls: list[srdatalog.ir.hir.types.RelationDecl], mir_program: srdatalog.ir.mir.types.Program, step_bodies: list[str], runner_decls: dict[str, str], cache_dir_hint: str = '<jit-cache>', canonical_indices: dict[str, list[int]] | None = None, jit_batch_count: int = 0, emit_preamble: bool = False, extra_index_headers: list[str] | None = None, decl_only_runner: bool = False) str[source]

Emit the full main-file string — mirrors Nim’s mir_cpp_str.

Structure: 0. (opt) #include "srdatalog.h" + using namespace directives (emit_preamble=True, default — needed for the standalone Python path where main.cpp is a compilable TU. Pass False to match the pure Nim fragment byte-for-byte.)

  1. Relation typedefs + DB alias (genRelCpp)

  2. using namespace SRDatalog::mir::dsl;

  3. Kernel-decls block (device DB + GPU includes + JitRunner fwd decls)

  4. namespace <Ruleset>_Plans { } (empty for JIT target)

  5. <Ruleset>_Runner struct

  6. // ======== JIT File-Based Compilation ======== summary

srdatalog.ir.codegen.cuda.main_file.gen_main_file_preamble() str[source]

Top-of-file preamble needed for main.cpp to compile as its own TU. In Nim’s pipeline these using-directives come from the .nim driver; for the standalone Python path we emit them explicitly.

srdatalog.ir.codegen.cuda.main_file.gen_relation_typedefs(decls: list[srdatalog.ir.hir.types.RelationDecl]) str[source]

Emit using <Name> = AST::RelationSchema<decltype("<Name>"_s), <Semiring>, std::tuple<<types>>>; for each relation decl. Used in the main-file scope where using namespace SRDatalog::AST makes the unqualified AST:: resolve.

Appends a 4th template argument for relations with a non-default index_type (e.g. Device2LevelIndex for fat-fact points-to sets).

srdatalog.ir.codegen.cuda.main_file.gen_run_dispatcher_file(ruleset_name: str, decls: list[srdatalog.ir.hir.types.RelationDecl], runner_decls: dict[str, str], mir_program: srdatalog.ir.mir.types.Program, canonical_indices: dict[str, list[int]] | None = None, extra_index_headers: list[str] | None = None) str[source]

Emit the Runner::run() out-of-line definition in its own TU. Contains the step_0..step_N dispatch + print_size block.

srdatalog.ir.codegen.cuda.main_file.gen_runner_struct(ruleset_name: str, decls: list[srdatalog.ir.hir.types.RelationDecl], mir_program: srdatalog.ir.mir.types.Program, step_bodies: list[str], canonical_indices: dict[str, list[int]] | None = None) str[source]

Emit <Ruleset>_Runner — the main orchestrator struct.

Args: ruleset_name: base name (e.g. “TrianglePlan”) decls: relation declarations (for load_data + print_size) mir_program: output of compile_to_mir step_bodies: one per step, the full template <typename DB> static     void step_N(...) function body as emitted by gen_step_body. canonical_indices: optional relation→column-order map for print_size stats. If omitted, falls back to default order.

srdatalog.ir.codegen.cuda.main_file.gen_runner_struct_declonly(ruleset_name: str, decls: list[srdatalog.ir.hir.types.RelationDecl], mir_program: srdatalog.ir.mir.types.Program) str[source]

Non-template Runner struct DECLARATION (for main.cpp).

All step_N and run() become static void step_N(DB& db, size_t); with the concrete DB typedef resolved to <Ruleset>_DB_DeviceDB. Bodies live in separate shard files.

srdatalog.ir.codegen.cuda.main_file.gen_schema_definitions_for_batch(decls: list[srdatalog.ir.hir.types.RelationDecl]) str[source]

Schema definitions inlined into each jit_batch_N.cpp. Mirrors Nim’s collectSchemaDefinitions output: fully-qualified SRDatalog::AST::RelationSchema (no using namespace umbrella in the batch file scope) plus the Literals using-directive for the _s UDL.

Pass this to cache.JitBatchManager.set_schema_definitions(...) / write_jit_project(schema_definitions=...).

srdatalog.ir.codegen.cuda.main_file.gen_step_shard_file(ruleset_name: str, decls: list[srdatalog.ir.hir.types.RelationDecl], runner_decls: dict[str, str], mir_program: srdatalog.ir.mir.types.Program, step_bodies: list[str], step_idx: int, extra_index_headers: list[str] | None = None) str[source]

Emit a standalone .cpp for one step_N out-of-line definition.

Each shard repeats the schema/DB/GPU-runtime preamble so it is a valid TU — same cost as the batch files’ self-contained layout. The gain is parallelism: N shards compile alongside the batches instead of serializing through main.cpp.

srdatalog.ir.codegen.cuda.main_file.gen_unity_main_file_content(ruleset_name: str, decls: list[srdatalog.ir.hir.types.RelationDecl], mir_program: srdatalog.ir.mir.types.Program, step_bodies: list[str], runner_decls: dict[str, str], per_rule_runner_bodies: list[tuple[str, str]], *, canonical_indices: dict[str, list[int]] | None = None, extra_index_headers: list[str] | None = None) str[source]

Emit ONE large .cpp that contains everything a project needs — preamble, schemas, DB alias, every JitRunner_X struct body, the host-side _Runner with step bodies, and the extern “C” shim.

Compiling this single TU is much faster than compiling main.cpp + N batch files when PCH is unavailable, because srdatalog.h (the ~500 KB boost/hana/RMM header stack) parses ONCE instead of N times. For doop that’s ~100s → ~20s on cold compile.