freenode
Databases & Infrastructure

QEMU patch series adds LLVM tool to auto-generate TCG from C helpers

helper-to-tcg turns annotated helper functions into TCG at build time, with Hexagon as the first large-scale frontend.

Anton Johansson has posted a 50-patch series that adds helper-to-tcg, an LLVM-based build-time translator, as a QEMU subproject. The tool takes C helper functions (or other code that lowers to LLVM IR), runs a custom pass pipeline, and emits TCG so target maintainers need not hand-write intermediate code for every instruction.

The practical payoff is largest on dense ISAs. On Hexagon the series already translates 1240 instructions, including 162 HVX vector ops, while holding back some heavier vectorized forms that would inflate translation blocks and temporary pressure. The older idef-parser path still covers 281 instructions, mostly complicated loads; Johansson expects that set to shrink until idef-parser can be retired. Presentations at KVM Forum 2023 and 2025 sketched the same approach for Hexagon and RISC-V.

Under the hood, helpers are annotated so the pipeline knows which functions to convert, which arguments must stay immediates, and which pointers are offsets into CPU state for gvec work. Constant folding and a simple register allocator cut temporary use before emission, which matters for wide vector code. Generated gen_helper_* stubs call the emitted TCG when translation succeeds and fall back to a normal helper call otherwise. Hexagon also gains declarative TCG global mappings, more vector temporaries, and small glue so memory helpers can recover the MMU index without signature changes.

As a standalone utility the same translator can sketch a first TCG implementation for awkward instructions. Docker coverage builds the tool across LLVM 15 through 21 and runs end-to-end checks including check-tcg for targets such as Hexagon. If the approach lands and spreads, frontends that today maintain large helper libraries could keep semantics in C while still executing most of that work as inlined TCG.