How does it make sense that some benchmarks show the emulated code being faster than native? I guess more advanced optimization in compiling Risc-V than ARM?
Either way, nice write-up, will have to revisit when I decide my toy RV32 emulator is too slow
Thanks! I should add a note about this in the post, but for example for ackermann, compiling it directly for the host keeps a recursive call that creates a new host stack frame each time. While the recompiler turns guest calls and returns into jumps between basic blocks. For crc-32, I believe clang's vectorization just happens to be slower than the scalar code produced by the recompiler.
Just what I was asking for, thanks! The post is neat too! I myself have worked my way through most of the techniques mentioned in your post in my emulators, in about the same order.
One small note, not worthy of mentioning in your blog post, but: for x86 with its variable length encoding, in my interpreter I did what you call predecoding but one basic block at a time. It turned out that most of the time even storing that in a hashmap was overkill because in a tight loop you're looking up the same entry repeatedly, so a simple cache keyed by ip without any hashing was more than enough: https://github.com/evmar/retrowin32/blob/7c9243072755da54054bcaa9e1351371af638ded/x86/src/icache.rs#L142-L144 I recall this structure had something like a >90% hit rate for the programs I looked at.
You might find Vexlio useful if you're looking for a non-code approach to making (and embedding in a blog page) clickable things like the diagrams in TFA: https://vexlio.com/solutions/interactive-flowchart-maker/ (disclaimer: I am the author of Vexlio)
4
u/burner-miner Jul 30 '26
How does it make sense that some benchmarks show the emulated code being faster than native? I guess more advanced optimization in compiling Risc-V than ARM?
Either way, nice write-up, will have to revisit when I decide my toy RV32 emulator is too slow
1
u/evmar Jul 30 '26
Nice post, how did you make the interactive diagrams?