Code Generation

Backends and lowering: Cranelift, native, LLVM, GPU, and debug info.

Code Generation

代码生成最能说明 Sounio 现在存在多个真实的编译器 profile。正确的 说法已经不是“默认构件只公开一条路径”,而是“仓库同时发布经过检查 的 JIT 构件和 GPU 构件,而且两者公开的是不同的 backend contract”。

当前源码地图

  • self-hosted/native/ 覆盖 native lowering、encoding、ABI、寄存器分配和 object formats。
  • self-hosted/wasm/ 覆盖 WASM 路径的 lowering、encoding 和 module construction。
  • self-hosted/gpu/ 覆盖 PTX、SPIR-V、Metal、tensor-oriented work 和 GPU lowering。
  • self-hosted/llvm/ 仍然作为 LLVM 相关子树存在。

已提交构件真正证明的内容

  • JIT profile: Cranelift JIT 已启用;LLVM 与 GPU codegen 已禁用。
  • GPU profile: GPU codegen 已启用;JIT 已禁用;可通过 build --backend gpu 输出 PTX。
  • 因此文档必须区分默认 profile 的行为、GPU profile 的行为,以及源码树中的实现广度。

native v2 预览通道

仓库现在还带有一个公开预览 native-v2 通道,用于 backend sovereignty program。旧的兼容别名 native-v2-shadow 已经退役。它不是一个新的稳定终端用户后端。

  • 这个通道的作用是把 RuntimeContext、目标寄存器策略和 Machine IR 预期固定为机器可读工件。
  • 当前的 x86-64 preview 通道现在在 self-hosted shell 中走真实的 Machine IR + legality 路径,发出严格的 scalar_core native ELF,通过 RuntimeContext 发布真实的 gc_state 与 managed-object descriptor table,并让 native-v2 堆对象通过固定容量的 handle table 完成 alloc/field/index 访问;同时还带有一个可执行的 descriptor-driven mark-compact GC model, 提供精确槽位扫描与 pin-aware relocation 规则;超出该子集的 opcode family 会直接 fail-closed。
  • AArch64 已经在 contract 中定义为目标策略,但还没有晋升为公开发布的 native emitter。

确认当前启用的 backend 集合

export SOUC_BIN="$(pwd)/artifacts/omega/souc-bin/souc-linux-x86_64-jit"
"$SOUC_BIN" info

export SOUC_GPU_BIN="$(pwd)/artifacts/omega/souc-bin/souc-linux-x86_64-gpu"
"$SOUC_GPU_BIN" info
"$SOUC_GPU_BIN" build examples/kernel_vec_add.sio --backend gpu -o /tmp/kernel_vec_add.ptx

export SOUNIO_NATIVE_V2_CONTRACT_PATH=/tmp/native_backend_v2_contract.v1.json
"$SOUC_BIN" run self-hosted/compiler/main.sio -- --self-test

规范的 gate-backed 工件是 artifacts/omega/native_backend_v2_contract.v1.json

如何写出不过时的 codegen 文档

  • 讨论实现架构时写 backend 目录。
  • 讨论默认公开体验时写 checked JIT 构件。
  • 讨论公开 GPU codegen 时写 checked GPU 构件。
  • 只有在你用精确的构件和命令验证过之后,才记录某条 backend 执行路径。