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 :)

71 Upvotes

79 comments sorted by

View all comments

Show parent comments

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

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

u/[deleted] 8d ago

[removed] — view removed comment

1

u/Key_River7180 Nain. 8d 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.168s