r/Compilers • u/pu1da • Oct 09 '26
Beyond learning: when does it actually make sense to write a custom native backend instead of using LLVM/Cranelift?
It feels like the default advice for any new language project nowadays is "just target LLVM IR" or maybe Cranelift. Obviously, getting world-class optimization passes for free is hard to beat.
But I'm curious about the specific industry use cases where building a custom backend straight to machine code is actually the right technical choice.
Context: I’ve been building my own language (Tapelang - tapelang.org) with a custom compiler that goes straight to native machine code. I come from a background of game development; and I initially chose this route just because I wanted to have embeddable scripting language (like lua) that could also compile to native code for performance. I didn't want to wrestle with LLVM's massive footprint. It started as a personal challenge.
But it got me thinking—beyond just "it's fun" or "compilation speed is way faster" (the Go/Jai argument), where does this approach actually win in the real world?
Are there specific domains, industries, or hardware constraints where dragging in LLVM is a dealbreaker, and a custom native backend is the strictly better choice? Would love to hear from anyone who has had to make this architectural call for a production system.
4
u/Competitive_Ideal866 Oct 09 '26
FWIW, I found it basically the same effort to target LLVM vs Aarch64 or RISC V asm. Even full JIT isn't that hard.
The main issue I had is that both approaches have bugs but I can fix the bugs in my own backend but fixing LLVM is a nightmare. I never want to touch C++ ever again. I'm amazed they wrote such a large project in C++. Makes no sense to me...
Unless I'm compiling a C-like language, I'd avoid LLVM. Custom backend to asm is easy to write and debug. JIT is harder to debug but still ok.
12
u/-dag- Oct 09 '26
Speed is a big one. Not many (if any) JITs use LLVM.
7
u/arjuna93 Oct 09 '26
Dragging in llvm is not a great choice, since it is a monstrous dependency (with relatively poor platform support and unstable APIs, if this is a concern).
7
u/[deleted] Oct 09 '26
I'll try to illustrate why using my own backend is no-brainer for me, however this is for a personal product now.
Usually it is statically built into my compiler, but it can be extracted, and a small front-end added, to form a standalone product called 'PC'.
This is roughly equivalent to a combination of LLI and LLC from LLVM (which take .ll input files and interpret or compile them).
Here are some comparisons (for a product that targets x64/Windows):
(1) (2) You'll have to fill in the relevant figures, as I simply don't know. You can guess however that they will be very different.
Basically my own backend is magnitudes smaller, simpler, and faster to build, AND IT IS ALL MY OWN WORK.
Code Quality This is always touted as being the big advantage of LLVM (otherwise who would bother with it), and yes it can generate great code.
But look at that 55ms build time above: that was achieve using my self-hosted compiler using the very same backend described above. How much faster could it possibly be?!
Optimised code might be 1-2 times as fast as mine, when using my front-end language, but it is just not worth the hassle of grappling with such a monstrously complex product, and ending up with a slow, cumbersome tool as a result.
IR Style
Let's try this bit of C:
This is the result of
clang -S -emit-llvm:This what I produce from my C compiler that also uses my IR:
It is so much easier on the eye! BTW this is the x64 code generated:
2
u/reini_urban Oct 10 '26
Dynamic vs static compilation. For dynamic langs they are way too big, and monstrous startup times. For statically compiled langs it's fine.
2
u/VincentPepper Oct 12 '26 edited Oct 12 '26
1
u/seg_lol Oct 09 '26
It is just bits, printf, or better yet, templates with holes you fill in.
1
u/rootkid1920 Oct 10 '26
When you are building a JIT
1
u/ElephantElectrical31 Oct 10 '26
just transpile to rust
1
u/mamcx Oct 10 '26
Others has touch to the ease of integration, ownership, simplicity or just I wanna, that are good things: you must answer if the cost of a deps vs the cost of maintaining your own smaller thing.
But not WHY somebody will do another OS, game engine, browser, RDBMs against a strong, solid and established thing?
This has happened: LLVM is a answer VS GCC.
Apart of commercial, politics, corporate reasons, the best reasons are:
For example, RDBMs start to see competitors once we are talking how have a store distributed across many machines, or the need for new columnar storage
Like how CPU, 64 bits, lots of RAM, DISK, need to deploy to many arcs, etc means that the old features become negative
There is a "mandate from the heaven" to cut ties to a perceived adversary depending, the internet, the emergence of PC, the emergence of IA, etc
-6
u/Grated-Expectations Oct 09 '26
I'm (well, Claude is helping) writing a language called 'xc' right now. The documentation is available and I went for a custom back-end because it originally started out as a modern language for the Atari X{L,E}.
LLVM isn't wonderful if you don't have lots of registers. The 6502 has 3, well, 1 really with two semi-registers that you can't do maths on. Instead the plan was to use zero-page as a large register array for a 100MHz 6502 I had running in an FPGA
This has ... evolved ... over time, along more than one axis. As far as the 6502 went..
Whereas the compiler is still evolving, it compiles a language ...
I recently found that the x86_64 machine was running under the power-saving governor when I was taking all the benchmark figures, so those are being re-done, and the focus of the next few releases will be optimisation to get back on par - the language moved from being generally faster to generally slower than gcc/clang when I moved the governor into performance mode. Definitely a double-face-palm moment - you know, for when one face-palm just isn't sufficient 🤬
I knew I liked the language, so I knew I was going to expand it and making the IR flexible to cover :banked memory annotations in the language while making it work on a 64-bit ARM was a challenge, but I really wanted to keep the 6502 in there. It's probably atrophied a little bit now (the focus recently has been on the bigger iron) but it still passes the unit tests every release, so in theory it ought to still work, I just haven't personally tried it recently.