Optimize/boost imutate speed - #14
Merged
Merged
Conversation
为评估 JIT 必要性建 Google Benchmark micro-bench(B1-B7),锁定端到端 成本结构:coordinator 40% + 中断检查 16% 是翻译块缓存碰不到的 56%。 针对性优化: - coordinator cpu_ WeakPtr→裸 CPU*(5 处 set_cpu 调用方跟着改) - NVIC has_pending_irq 复用 highest_priority_pending cache 端到端 ips 翻倍(gpio 21.5M→50.2M,uart/tim 25.5M→46M),逼近 STM32 72M 原生。ctest 392 绿(含 QEMU oracle)。baseline 更新。 详见 document/notes/045。
WFI(0xBF30)设 sleeping_ 挂起 fetch,exception 唤醒;coordinator 检测 sleeping 时快进到下个 timer event(advance_cycles + tickable.tick), 跳过逐 cycle 空转。ARMv7-M §B5.2.2 语义(确定睡,非 hint)。 基础设施(为 fast-forward 备): - CPU::advance_cycles(n) / is_sleeping()(virtual,Cortex-M3 override) - Device::cycles_until_next_event()(SysTick override → next reload) - SysTick::cycles_until_next_tick()(P1 事件化查询) - coordinator set_fast_forward_enabled + is_cpu_sleeping_ - Stm32f103Soc::set_fast_forward_enabled(转发) 测试:WfiSetsSleepingAndSuspendsFetch(cpu)+ FastForwardSkipsWfiSleepToTimerEvent(coordinator)。ctest 374 绿。 业界标准 fast-forward(QEMU sleep=no / Simics hypersimulation): 只快进 WFI 真睡,不识别 busy-wait(emu_busy_wait_research P2.a)。
PC13 LED + __WFI() 等 SysTick(500 tick)。runner 验证: 关 fast-forward 8M step → 3 toggle(WFI 逐 cycle); 开 → 939 toggle(WFI 快进,~313×)。 实证 P2.a:用 WFI 的固件(标准低功耗等)fast-forward 实时; HAL_Delay busy-wait(tamcpp_led)不触发(逐指令),符合业界标准。
Session rebuild 读 env,开 SoC fast-forward(WFI 快进)。GUI 加载 WFI 固件 (如 wfi_blink)+ env 启动 → LED 实时闪。默认关,不影响 busy-wait。
P2.a WFI fast-forward(ARMv7-M §B5.2.2)实现 + wfi_blink 端到端(939 vs 3 toggle,GUI LED 实时闪)。业界标准路实证:WFI 固件实时,busy-wait(HAL_Delay) 逐指令(QEMU/Simics 同)。beyond 业界(c/d 识别)实测放弃(误判 + 死循环)。
toolbar 加 QCheckBox,toggled → Session::set_fast_forward_enabled。 Session fast_forward_ 跨 rebuild 持久,rebuild 重新 apply 到新 SoC。 env MICRO_FORGE_FAST_FORWARD 保留(默认 + 兼容)。
Step 1: 抽 6 个 inline case(CPS/CBZ/ADR/ADD_SP/B<cond>/B)为独立
handler,execute_16bit 的 dispatch 变纯 handler 绑定。
Step 2: 加 translate_16bit(insn → Handler16 指针,复制同一 dispatch),
execute_16bit 重构为 translate + (this->*handler)(insn)。
位等价(ctest 374 全绿)。为 JIT 翻译块缓存铺路:translate 可被 cache
复用,绑 {PC → handler} 后跳过 fetch+decode。
PC → {handler, insn} 缓存。hit 时跳过 fetch16 + translate_16bit,直接
dispatch cached handler(IT/pc_written/fault 全保,与 miss 路径位等价)。
miss 时 execute_16bit 后 translate_16bit + insert(下次 hit)。
set_jit_enabled 开关(默认关,tcache_enabled_=false → 行为完全不变,
ctest 374 全绿)。开 jit 后命中路径省 fetch(4.2ns)+ translate(3ns),
预期 bare core dispatch 显著加速。
开 jit(cache 命中跳过 fetch+translate)B1 `B.` 自循环 86M→147M ips(+70%)。 B5 ALU 循环无变化(execute 主导,fetch+translate 占比小,符合预期)。
SoC::set_jit_enabled(转发 cpu)+ bench_sim MF_JIT env。端到端对比: - uart_printf/tim_timebase(全 16-bit)+33% - gpio_iotoggle(38% 32-bit 不 cache)+21% Step 3 的 translation cache 在真实固件上生效,位等价经 ctest 374 保证。
所有 t16 handler 从 (uint16_t insn) 改为 (uint16_t insn, uint16_t)。 16-bit 的第二参数 hw2 忽略(无名),t32 handler 将用它。 Handler16 类型同步改双参数。execute_16bit + cache hit 调用加 , 0。 为统一 IR 铺路:一种 handler 类型覆盖 16/32-bit, translate + cache + execute 无分支。ctest 374 绿。
抽 14 个 inline case(BL/BLX、B.W T3/T4、MOVW、MOVT、DMB/DSB、NOP.W、 MRS、MSR、BFI/BFC、SBFX/UBFX、UDIV/SDIV、MLA/MLS、MULL/MLAL)为独立 handler(统一 (hw1,hw2) 签名)。read_special/write_special lambda 嵌入 t32_mrs/t32_msr handler 体。 translate_32bit(hw1,hw2) → Handler16(复制 mask 链 + 表驱动 dispatch, return handler 指针,nullptr=illegal)。execute_32bit 瘦成 translate+execute。 现在 16/32-bit 统一:Handler16 类型 + translate + execute 无分支。 ctest 381 全绿。
CachedInsn 扩展 {handler, hw1, hw2, is_32bit}。step_execute_one cache hit
路径统一处理 16/32-bit(PC+2/+4、handler(hw1,hw2)、fault 日志全分支)。
miss 路径 32-bit execute_32bit 后 translate_32bit + cache insert。
端到端收益(gpio_iotoggle 38% t32):+21% → +35%(16+32 bit cache)。
uart/tim(全 16-bit)维持 +33%。统一 Handler16 类型覆盖 16/32-bit,
translate + cache + execute 零分支。ctest 374 全绿。
JIT 统一 IR 架构完整落地。
Session::set_jit_enabled(转发 SoC)+ QCheckBox("JIT cache")。
跨 rebuild 持久(同 Fast-forward WFI 模式)。
- 新增 Turbo 档:每 tick 按时间预算(~35ms)跑批量步骤,自动适配机器性能 - 默认选 Turbo(不再需要手动选 100×) - JIT cache 移除 UI checkbox:始终在 Session 层开(纯优化,不改语义) - 速度档保留 1×/5×/25×/100×(调试用慢速观察)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
JIT + 快进 + 自动选档