Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions runtimec2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ add_library(kvlang_runtimec2 SHARED EXCLUDE_FROM_ALL
src/rwir_gen.cpp
src/rwir_lookup.cpp
src/codeseg.cpp
src/jit.cpp
src/value.cpp
src/vmkv.cpp
src/vmkv_native.cpp
Expand Down
31 changes: 31 additions & 0 deletions runtimec2/include/jit.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once
#include <cstdint>

#include "codeseg.hpp"
#include "vmbin.hpp"

// M1 原生发射(iops 形态):verify_jitable 静态判定 + 最小 x86_64 发射器。
// 发射产物 = 纯计算函数 int64_t(int64_t),帧 I/O(参数装载/结果回写/错误)
// 全在 jit_try_run 的 C++ 侧完成——JIT 代码本身零 kvspace/零 FReg 访问。
// 语义:值跨多条指令存活于机器寄存器,出口一次性回写;mem_frame 无观察点,
// 与 #499 checkpoint 语义一致(检查点=函数边界)。

namespace kv::rt {

// 发射函数 ABI(M1):arg → RAX 结果;err_out 非 0 = runaway guard 触发。
using JitFn = std::int64_t (*)(std::int64_t arg, std::int64_t* err_out);

// 判定 + 发射 + 缓存(进程级,键 = 函数目录,随段缓存失效)。
// 返回 nullptr = 不可 JIT / 发射失败(调用方回退 fast kernel)。
const void* jit_compile(const std::string& func_dir, const Segment* seg);
void jit_clear_cache();

// run_frame 钩子:命中则执行并把 RunResult 写入 *out,返回 true;
// 未命中/禁用/不满足运行时前提 → false(走解释路径)。
bool jit_try_run(bin::FrameCtx& f, const Segment* seg, int start_s0,
bin::RunResult* out, std::string* stop_pc, std::string* err);

// 环境开关:RC2_NOJIT=1 关 JIT;RC2_JITDUMP=1 发射后打印机器码。
bool jit_enabled();

} // namespace kv::rt
23 changes: 22 additions & 1 deletion runtimec2/src/codeseg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <unordered_map>
#include <vector>

#include "jit.hpp"

#include <kvspace/kvspace.h>

#include "value.hpp"
Expand Down Expand Up @@ -210,6 +212,22 @@ bool build(void* h, const std::string& func_dir, Segment* out) {
}
out->insns.push_back(ins);
}
if (std::getenv("RC2_TRACE")) {
for (std::size_t ri = 0; ri < out->regs.size(); ri++) {
const RegDesc& d = out->regs[ri];
std::fprintf(stderr, "[segdmp reg %zu kind=%d name='%s' key='%s' pk=%d]\n",
ri, (int)d.kind, d.name.c_str(), d.slot_key.c_str(),
d.param_k);
}
for (const CInsn& ci : out->insns) {
std::fprintf(stderr,
"[segdmp s0=%d op=%s opfn=%d fast=%d wrl=%d nrd=%u nwr=%u "
"rd=%d,%d,%d wr=%d,%d,%d]\n",
ci.s0, ci.name.c_str(), (int)ci.opfn, (int)ci.fast_i64,
(int)ci.wr_local, ci.nrd, ci.nwr, ci.rd[0], ci.rd[1],
ci.rd[2], ci.wr[0], ci.wr[1], ci.wr[2]);
}
}
// 字面量 int64 快值缓存:build 期读槽一次,运行期帧入口零 kvspace 预读
// (fib 递归每帧省 1 次 kvspaceXvalueBodyView 读字面量 `1`)。
out->lit.assign(out->regs.size(), LitFast{});
Expand Down Expand Up @@ -278,6 +296,9 @@ bool cached_insn(const std::string& func_dir, int s0, const CInsn** out,
return true;
}

void clear_segment_cache() { cache().clear(); }
void clear_segment_cache() {
cache().clear();
jit_clear_cache();
}

} // namespace kv::rt
Loading
Loading