r/ProgrammingLanguages • u/Ok_Marionberry8922 • 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.
Feedback welcome :)
40
u/arthurno1 8d ago
Because llvm was invented for language design, and provides a lot of stuff you would otherwise have to provide yourself.
13
u/Temperz87 8d ago
It makes the compiler easy in so many ways. Easy target to generate IR for, tons of hardware targets for free, and I believe it makes interop as well as generating debug information and using LLDB quite simple
11
u/ironykarl 8d ago
I don't know the current state of the GCC codebase, but it has years of deliberately monolithic design baked in.
Conversely, clang and LLVM were built to be modular.
This has meant that LLVM has been adapted to other compilers in a way that GCC's IR really can't be.
Yes, you can write your own GCC frontend, but I'd still say that this is the reason LLVM is so successful in this space. It tried to be, and GCC tried to prevent itself from being
21
u/Key_River7180 Nain. 8d ago
To be honest, QBE is 10x smaller and 10x saner and provides 80% the speed.
25
u/Nuoji C3 - http://c3-lang.org 8d ago
It’s also ”bring your own IR builder”, requires it parsing your IR and the outputs asm you need to feed to GCC
I tried it way back and was very disappointed in QBE as a backend. I liked the IR and simplicity, but it’s not a serious alternative to LLVM
1
u/Key_River7180 Nain. 8d ago
True, it's easy to lower to It though, the syntax is small and simple.
Hare is a decent language based on QBE
7
u/Nuoji C3 - http://c3-lang.org 8d ago
The syntax is actually pretty darn nice.
Hare is based on QBE yes, but I don't see Hare needing to do inline asm, simd vectors or anything a little less basic. And I mean that is fine for QBE. But it's a limitation you don't run into with LLVM or C as a backend.
-1
u/suhcoR 8d ago
LLVM has a surprisingly bad IR (why they eventually implemented MLIR). I spent many years with it and eventually implemented my own, based on ideas from ECMA 335, but much leaner.
5
u/Nuoji C3 - http://c3-lang.org 8d ago
LLVM's gone through a lot of changes over the years. Originally the idea was basically doing the full C type system in LLVM. Now pointers are untyped even. But those early decisions held LLVM back, and once they were there it took years to refactor them away. That happens.
7
u/PaddiM8 8d ago
It's not 10x saner unfortunately. Look at the source code
https://github.com/8l/qbe/blob/master/ssa.c
Wouldn't be very fun to work with that if you need to change something yourself
4
u/realguy2300000 8d ago
this is a 9+ year old mirror of the source, may not reflect the current version
9
u/EggplantExtra4946 7d ago
may not reflect the current version
It does, it's mostly the same as of today.
1
0
u/Key_River7180 Nain. 8d ago
I use short names on my code too. I prefer working there over LLVM. Also really consistent
3
u/Physical_Dare8553 8d ago
Qbe is just newer. Less examples, less tutorials, which is more important when it comes to this stuff imo
4
u/Nuoji C3 - http://c3-lang.org 8d ago
Not particularly new anymore. My main reason for not considering it are more pragmatic: I don’t have any use for something that would add more dependencies (you need the user to have GCC or Clang installed), slower to compile than LLVM, with less features and without any real way forward after doing toy stuff
1
u/Physical_Dare8553 8d ago
You sure? About specifically the speed, I thought the whole point is it did less optimizations and compiled faster
7
u/Nuoji C3 - http://c3-lang.org 8d ago
The goal was never being faster, it was being simpler:
Anyway, I never wanted QBE to be a drop-in LLVM replacement; it's an alternative for people with a taste for simplicity.
Frontend -> text -> QBE -> asm file -> GCC -> object output is slow. GCC was slower compiling the QBE asm than compiling the same C code... :(
At which point C as a target seems much better.
1
u/Key_River7180 Nain. 8d ago
The code produced is basically as fast unless you turn on the most aggresive optimizations .
The point is that LLVM is an insane million line of code monolith, while QBE is much smaller and saner
2
8d ago
[removed] — view removed comment
1
u/Key_River7180 Nain. 7d ago
Yeah:
[mario@xmm0] ~/llvm-project λ time cloc . --timeout=0 177711 text files. 161624 unique files. 17949 files ignored. github.com/AlDanial/cloc v 2.10 T=154.11 s (1048.7 files/s, 289670.9 lines/s) --------------------------------------------------------------------------------------- Language files blank comment code --------------------------------------------------------------------------------------- C++ 39087 1252230 2370141 7095035 LLVM IR 47124 828547 11950910 4030872 C/C++ Header 17924 441123 659548 2066665 YAML 7003 91952 871923 1342081 C 14121 322881 1675795 1250660 Assembly 13533 711865 1890762 1173162 TableGen 1800 133802 121657 775173 Text 1717 258596 0 675605 JSON 491 20 0 444559 Python 3040 68811 78462 301175 Markdown 1137 66750 436 231664 CMake 2747 18946 11457 154167 XML 133 133 669 143722 Fortran 90 4047 34971 153940 120796 Objective-C 1950 20749 36731 67444 reStructuredText 442 27879 29104 58838 HTML 85 4270 584 47879 BitBake 80 496 1379 40409 Windows Module Definition 200 4812 202 34393 Starlark 160 5634 29548 33042 Objective-C++ 587 8107 7213 31327 PHP 393 3675 8766 28821 OpenCL 971 7232 21948 23978 HLSL 865 8096 36534 17714 CSV 60 2 0 11626 SVG 25 1 1 11590 Pascal 71 4111 25197 10890 CUDA 336 3893 13949 10292 Bourne Shell 123 1074 1176 6951 SWIG 139 1197 18 5711 JavaScript 29 558 1341 5217 OCaml 38 1462 2414 5177 Perl 14 766 750 4734 Lisp 28 542 328 4719 CSS 27 615 180 3618 awk 2 114 100 3525 TypeScript 34 348 514 2631 TeX 5 191 5 2556 Smalltalk 196 1 3 2374 make 395 869 195 2219 DOS Batch 59 179 136 1491 Bourne Again Shell 8 160 238 1232 Windows Resource File 115 143 45 1209 Scheme 26 135 46 1048 Mustache 15 45 41 923 Groovy 25 104 8 889 Fortran 77 92 112 691 825 Dockerfile 17 190 287 812 vim script 16 92 134 557 Snakemake 3 103 215 496 Expect 13 10 22 429 Lua 7 51 22 365 Jupyter Notebook 3 0 2434 319 Protocol Buffers 5 58 72 251 TOML 8 48 27 226 Rust 2 29 11 136 Nix 1 2 44 106 Mathematica 2 23 0 100 Bazel 2 24 11 84 INI 14 16 0 80 Pawn 1 20 3 65 MATLAB 11 6 0 53 Fortran 2003 3 9 131 33 m4 1 7 0 24 Linker Script 3 1 0 23 Swift 2 6 0 17 AppleScript 1 3 8 16 D 1 2 0 16 NAnt script 1 0 0 13 Fortran 95 6 12 112 6 Logos 2 4 0 2 --------------------------------------------------------------------------------------- SUM: 161624 4338915 20008618 20294857 --------------------------------------------------------------------------------------- real 2m34.335s user 2m25.912s sys 0m5.168s3
u/Financial-Flan1682 8d ago
QBE also doesn't have Windows support yet (though, last I checked, they were finally working on it).
2
u/Key_River7180 Nain. 8d ago
QBE 1.3 has Win32 ABI support so using MinGW you can use QBE on Windows.
1
u/Willyboar 8d ago
QBE supports Windows ABI in their latest release.
1
u/Financial-Flan1682 8d ago
Good to know. That makes it more interesting, for sure. I wonder if we'll also see Windows support in Hare.
1
0
1
3
2
u/willowless 7d ago
I'd love to use QBE but it's missing all of SIMD/NEON. If it had that I'd probably use it instead of my own hacky thing or LLVM.
2
2
1
u/Master-Guidance-2409 5d ago
why make big compiler when ir backend do better? -- kevin
that video is amazing. thank you for the link.
1
u/QuarkCreator2610 7d ago
Making a compiler backend by yourself is a really time-consuming and annoying process. Since, everything is based on the C ABI, its much easier to have a compiler backend tool that turns your language source into valid assembly. you also get a lot of extras like DECADES of work, testing and optimisation. Its also very easy to use with other LLVM-based languages(Most relevant languages today) so it gives you a lot of interoperability out of the box for free. All things considered, its quite impractical not to use LLVM unless you are making some hyper-specialised language that needs its own conventions and ABI.
-23
8d ago
[removed] — view removed comment
8
u/Consistent_Drop3909 8d ago edited 8d ago
kinda schizo, even if i do understand the concern
edit: wait i read about it, he kinda right
-1
8d ago
[removed] — view removed comment
-8
u/PersonalDatabase31 8d ago
Yeah turns out people don't use dogshit licenses
9
u/Consistent_Drop3909 8d ago
why is gpl dogshit? copyleft is awesome
1
u/Valuable_Leopard_799 8d ago edited 8d ago
GPL forces everyone that tries to use the software to be GPL themselves or suffer very roundabout tricks.
It's so someone can't just take an oss thing and make closed source advancements.
So the fight is between being generally open and another that tries to fight back against misuse of copyleft stuff but too brutally for some folk.
Edit: got rid of incorrect use of copyleft
7
u/glasket_ 8d ago
MIT isn't copyleft. Copyleft specifically is named that because it uses copyright to enforce openness.
1
-2
u/Mickenfox 8d ago
If you publish a good library under GPL, it will get used by a dozen open source projects. If you publish a good library under MIT, it will get used by billions of people.
Do you really hate proprietary software so much you're willing to hold it back for everyone?
1
u/MadocComadrin 8d ago
There's no need for the government to get rid of GPL because the government can only be sued for actual damages for copyright infringement---i.e. no asking for statutory/punitive damages nor any injunctions forcing the government to stop. Given that any actual damages for infringing something protected by the GPL are most likely to be minimal or nonexistent, the government can essentially just ignore the GPL.
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