← Back to C-Kernel-Engine Docs Doxygen Source Documentation
ckernel_codegen_v2_struct.c
Go to the documentation of this file.
2 
3 #include <stdio.h>
4 
6 {
7  fprintf(out,
8  "/* Auto-generated runtime from CKIRV2Graph.\n"
9  " * This v2 emitter currently writes the schedule and buffer layout\n"
10  " * stubs. Kernel wiring will be layered on as IR v2 grows.\n"
11  " */\n\n");
12  fprintf(out,
13  "#include <stddef.h>\n"
14  "#include <stdint.h>\n"
15  "#include <stdio.h>\n"
16  "#include <string.h>\n\n");
17  return 0;
18 }
19 
21  const CKIRV2Graph *graph,
22  const CKMemPlan *plan,
23  const char *tag)
24 {
25  const char *suffix = tag ? tag : "prefill";
26  fprintf(out, "typedef struct {\n"
27  " size_t offset_bytes;\n"
28  " size_t size_bytes;\n"
29  " int arena;\n"
30  "} CKV2BufferLayout;\n\n");
31 
32  fprintf(out, "static const CKV2BufferLayout ck_v2_%s_buffers[] = {\n", suffix);
33  for (int i = 0; i < graph->num_buffers; ++i) {
34  const CKMemSpan *span = &plan->spans[i];
35  fprintf(out,
36  " { %zu, %zu, %d }, /* %s */\n",
37  span->offset_bytes,
38  span->size_bytes,
39  (int)span->arena,
40  graph->buffers[i].name ? graph->buffers[i].name : "unnamed");
41  }
42  fprintf(out, "};\n\n");
43 
44  fprintf(out, "static const size_t ck_v2_%s_total_bytes[] = {\n", suffix);
45  for (int i = 0; i < CK_MEM_ARENA_COUNT; ++i) {
46  fprintf(out, " %zu%s\n",
47  plan->total_bytes[i],
48  (i + 1 == CK_MEM_ARENA_COUNT) ? "" : ",");
49  }
50  fprintf(out, "};\n\n");
51 
52  fprintf(out, "typedef struct {\n"
53  " const CKV2BufferLayout *buffers;\n"
54  " int num_buffers;\n"
55  " const size_t *total_bytes;\n"
56  " size_t alignment_bytes;\n"
57  "} CKV2Runtime;\n\n");
58 
59  fprintf(out, "static const CKV2Runtime ck_v2_%s_runtime = {\n"
60  " ck_v2_%s_buffers,\n"
61  " %d,\n"
62  " ck_v2_%s_total_bytes,\n"
63  " %d\n"
64  "};\n\n",
65  suffix,
66  suffix,
67  graph->num_buffers,
68  suffix,
70 }
void ck_codegen_v2_emit_struct(FILE *out, const CKIRV2Graph *graph, const CKMemPlan *plan, const char *tag)
int ck_codegen_v2_emit_preamble(FILE *out)
@ CK_MEM_ARENA_COUNT
#define CK_MEM_PLAN_DEFAULT_ALIGN
CKIRV2Buffer * buffers
Definition: ckernel_ir_v2.h:62
CKMemSpan * spans
size_t total_bytes[CK_MEM_ARENA_COUNT]
size_t size_bytes
CKMemArenaKind arena
size_t offset_bytes