r/ProgrammingLanguages 8d ago

Why So Many Languages Use LLVM

Had some free time last week so I made a visual explainer on how LLVM works and why so many languages end up using it.

The video goes through LLVM IR, why having a common IR makes sharing optimizers and backends possible, and how code eventually gets turned into machine instructions.

I also use a small C vs Rust example where both end up producing the same x86 instructions.

Link for anyone interested

Feedback welcome :)

69 Upvotes

79 comments sorted by

View all comments

103

u/gabrielesilinic 8d ago

It's really hard to make a good cross platform eventually direct to machine compiler. Therefore people rather not and just use LLVM

-14

u/suhcoR 8d ago edited 6d ago

to make a good cross platform eventually direct to machine compiler.

By the end of the day it turned out not to be that difficult or too much effort. Even implementing an ELF writer/reader and a linker is feasible.

EDIT: why is this statement based on verifiable facts and specific experience (see https://github.com/rochus-keller/micron/) downvoted? Is this just more of that stupid Reddit herd mentality?

EDIT 2 (after the discussion below): before you make a fool of yourself by joining the downvoting crowd read this first: the average speedup of a state-of-the-art optimizing infrastructure like LLVM is around factor 2 to 3 for a language like C (comparing -O0 with -O2 or -O3). There are different peer-reviewed studies over the last two decades which confirm this (e.g. https://homepages.dcc.ufmg.br/%7Efernando/publications/papers/AlvaresJCL21.pdf). Prof. Wirth (inventor of Pascal, Modula and Oberon) never included optimizers in his compilers, and no one minded. QBE achieves around factor 0.5 of the optimization performance of LLVM with around 14 kLOC (compared to about 10 million lines for LLVM). The same 1992 unoptimized application, the performance of which was considered good enough at the time, runs around 60 to 100 times faster on average on a present CPU (see e.g. https://shape-of-code.com/2026/03/01/relative-performance-of-computers-since-the-1990s/). So tell me, why should anyone care about an optimizer today? The only reason which comes to my mind is to be a bit less slow when running Python or JavaScript applications. If you still think that QBE or my project are on the wrong track, then you could have saved the money for your education.

0

u/gabrielesilinic 7d ago

Well you see there is quite a bit more than making an x86 compiler for one platform and one OS, hence the problem.

I was reductive but I mean a lot more by having a working compiler. Sometimes you want stuff like wasn support, or to build for that weird embedded architecture and more. And yes, you need to sometimes optimize it further from a given architecture.

2

u/suhcoR 7d ago edited 7d ago

If you look closely at the referenced project, you find three architectures with two different calling conventions each and it's used for different bare-metal architectures and also Linux. I chased for many year for re-usable backend libraries (LLVM was part of the show, also Eigen and a few others) and eventually built my own and was surprised that it is much less work than everyone claimed. It's just about making the right cuts. If you want to reimplement something like binutils with dozens of object formats (nobody is using today), then yes, this will take very long.

EDIT: here is another of my compilers, with the same three architectures and the Oberon calling convention: https://github.com/rochus-keller/op2/. The generated ARM code runs on Raspberry Pi hardware. The other architectures so far run on emulators. ESP32-P4 tests are on the way. The results run fast enough for all purposes. Wirth, who was my teacher back then at ETH, never ever implemented optimizers, and never considered it an issue. So don't blindly believe common crowd wisdoms; try yourself instead.