[RFC] Support for indirect branches / computed gotos in libgccjit
Basile Starynkevitch
basile@starynkevitch.net
Sat Sep 12 20:30:34 GMT 2026
On Sat, 2026-09-12 at 15:10 -0400, Marcus Valette wrote:
> TLDR: Request for indirect jump exposure in api, with required target
> lists to make control flow easy.
>
> Hi all,
>
> I am currently working on a language compiler backend targeting
> libgccjit
> and ran into a limitation regarding low-level control flow
> primitives:
> the lack of support for indirect branches (computed gotos).
>
> While dense switch statements can often be lowered to jump tables,
> doing
> so routes execution through a single centralized dispatch point. In
> interpreter dispatch loops and state machines, this introduces branch
> target buffer aliasing and prediction penalties compared to direct
> threading, where each instruction handler site ends with its own
> indirect
> jump.
>
> Beyond interpreter loops, having first-class indirect jumps avoids
> impedance mismatches when lowering language-level computed gotos or
> arbitrary CFGs, matching what GNU C (&&label / goto *expr) and LLVM
> IR
> (indirectbr) already provides.
>
> To support this cleanly without overcomplicating the existing API, I
> would
> like to propose exposing two core primitives:
>
> 1. Extracting the code address of a basic block as a void* rvalue:
>
> gcc_jit_rvalue *
> gcc_jit_block_get_address (gcc_jit_block *block,
> gcc_jit_location *loc);
>
> 2. Terminating a block with an indirect jump to a dynamic pointer,
> explicitly passing possible targets to preserve CFG edges and
> prevent
> dead-code elimination:
>
> void
> gcc_jit_block_end_with_indirect_jump (gcc_jit_block *block,
> gcc_jit_location *loc,
> gcc_jit_rvalue *target_ptr,
> int num_possible_targets,
> gcc_jit_block
> **possible_targets);
I hope your code and patch will soon be accepted. It definitely makes
sense and is missing in libgccjit.
In my limited understanding, GNU lightning (see
https://www.gnu.org/software/lightning/ ...) and TinyCC
(see https://github.com/TinyCC/tinycc ...) also have these features.
And so does of course https://asmjit.com/
Long time ago I did contribute to GCC: https://arxiv.org/abs/1109.0779
Obviously computed gotos are needed in any code generation libraries
Thanks, regards from near Paris in France
--
Basile STARYNKEVITCH
<basile@starynkevitch.net>
8 rue de la Faïencerie
http://starynkevitch.net/Basile/
92340 Bourg-la-Reine
https://github.com/bstarynk
France
https://github.com/RefPerSys/RefPerSys
https://orcid.org/0000-0003-0908-5250
More information about the Jit
mailing list