← Back to C-Kernel-Engine Docs Doxygen Source Documentation
ckernel_codegen_v2.h File Reference
#include "ckernel_codegen.h"
#include "ckernel_ir_v2.h"

Go to the source code of this file.

Functions

int ck_codegen_v2_emit_runtime (const CKIRV2Graph *graph, const char *path, CKEmitMode mode)
 

Function Documentation

◆ ck_codegen_v2_emit_runtime()

int ck_codegen_v2_emit_runtime ( const CKIRV2Graph graph,
const char *  path,
CKEmitMode  mode 
)

Emit a C runtime file from a CKIRV2Graph.

This v2 emitter is IR-driven and will evolve to generate fully wired kernels once buffer bindings and fusion metadata are added to the IR.

Definition at line 10 of file ckernel_codegen_v2.c.

13 {
14  if (!graph || !path) {
15  return -1;
16  }
17 
18  FILE *out = fopen(path, "wb");
19  if (!out) {
20  fprintf(stderr, "ck_codegen_v2_emit_runtime: failed to open %s: %s\n",
21  path, strerror(errno));
22  return -1;
23  }
24 
25  CKMemPlan prefill_plan = {0};
26  CKMemPlan decode_plan = {0};
27  CKMemPlan backward_plan = {0};
28 
29  if (ck_mem_plan_build_inference_with_tokens(graph, &prefill_plan,
30  CK_MEM_PLAN_DEFAULT_ALIGN, -1) != 0 ||
31  ck_mem_plan_build_inference_with_tokens(graph, &decode_plan,
32  CK_MEM_PLAN_DEFAULT_ALIGN, 1) != 0 ||
33  ck_mem_plan_build_training_with_tokens(graph, &backward_plan,
34  CK_MEM_PLAN_DEFAULT_ALIGN, -1) != 0) {
35  fclose(out);
36  ck_mem_plan_free(&prefill_plan);
37  ck_mem_plan_free(&decode_plan);
38  ck_mem_plan_free(&backward_plan);
39  return -1;
40  }
41 
43  ck_codegen_v2_emit_struct(out, graph, &prefill_plan, "prefill");
44  ck_codegen_v2_emit_struct(out, graph, &decode_plan, "decode");
45  ck_codegen_v2_emit_struct(out, graph, &backward_plan, "backward");
46  ck_codegen_v2_emit_sections(out, graph,
47  &prefill_plan,
48  &decode_plan,
49  &backward_plan);
50  ck_codegen_v2_emit_dispatch(out, graph);
51  ck_codegen_v2_emit_schedule(out, graph,
52  "ck_v2_prefill_runtime",
53  "ck_v2_decode_runtime",
54  "ck_v2_backward_runtime");
55 
56  if (mode == CK_EMIT_STANDALONE) {
57  fprintf(out,
58  "int main(void) {\n"
59  " ck_v2_run_forward();\n"
60  " return 0;\n"
61  "}\n");
62  }
63 
64  ck_mem_plan_free(&prefill_plan);
65  ck_mem_plan_free(&decode_plan);
66  ck_mem_plan_free(&backward_plan);
67  fclose(out);
68  return 0;
69 }
@ CK_EMIT_STANDALONE
void ck_codegen_v2_emit_dispatch(FILE *out, const CKIRV2Graph *graph)
void ck_codegen_v2_emit_struct(FILE *out, const CKIRV2Graph *graph, const CKMemPlan *plan, const char *tag)
void ck_codegen_v2_emit_sections(FILE *out, const CKIRV2Graph *graph, const CKMemPlan *prefill_plan, const CKMemPlan *decode_plan, const CKMemPlan *backward_plan)
void ck_codegen_v2_emit_schedule(FILE *out, const CKIRV2Graph *graph, const char *prefill_runtime, const char *decode_runtime, const char *backward_runtime)
int ck_codegen_v2_emit_preamble(FILE *out)
int ck_mem_plan_build_training_with_tokens(const CKIRV2Graph *graph, CKMemPlan *plan, size_t alignment_bytes, int tokens_override)
void ck_mem_plan_free(CKMemPlan *plan)
#define CK_MEM_PLAN_DEFAULT_ALIGN
int ck_mem_plan_build_inference_with_tokens(const CKIRV2Graph *graph, CKMemPlan *plan, size_t alignment_bytes, int tokens_override)

References ck_codegen_v2_emit_dispatch(), ck_codegen_v2_emit_preamble(), ck_codegen_v2_emit_schedule(), ck_codegen_v2_emit_sections(), ck_codegen_v2_emit_struct(), CK_EMIT_STANDALONE, ck_mem_plan_build_inference_with_tokens(), ck_mem_plan_build_training_with_tokens(), CK_MEM_PLAN_DEFAULT_ALIGN, and ck_mem_plan_free().

Referenced by main().