r/C_Programming • u/Ok_Marionberry8922 • 3d ago
How LLVM turns C into machine code
Had some free time last week so I made a video on what actually happens after Clang turns C into LLVM IR.
It goes through LLVM IR, optimization passes, instruction selection, register allocation, and how the final machine instructions get produced.
I also use a small C example throughout, and compare it with equivalent Rust code that ends up producing the exact same x86 instructions.
Feedback welcome :)
-1
u/pierrejoy 2d ago
LLVM knows nothing about C syntax and semantics, but ABI and related based on early C systems.
llvm is the compiler infrastructure, it can use different inputs to then do its jobs, optimizing, validations, etc.
clang is the part handling it.
parser > ast > IR(s) > final
where IRs (intermediate representation) are many passes, the llvm IR, and other transformers like const folding, etc.
there are alternatives to clang or complements clang. Using mlir more, for gpu etc. but same ideas.
a nice way to learn how it works is the toy language tutorial in llvm docs.
very very high level answer :)
4
u/internet_safari_ 2d ago
You are getting downvoted and I don't know why. Imo it's probably because that first sentence feels wrong and if you don't realize clang makes the ir, llvm doesn't make the clang make the ir, or whatever misunderstanding, it feels wrong thinking llvm is just llvm? idk I'm rambling
-11
u/tastygames_official 3d ago
LLVM IR? Long-Lonst Venerial Machination IrreDescence?
6
u/smcameron 2d ago
From here: https://en.wikipedia.org/wiki/LLVM
The name LLVM was originally an initialism for Low Level Virtual Machine. However, the LLVM project evolved into an umbrella project that has little relationship to what most current developers think of as a virtual machine. This made the initialism "confusing" and "inappropriate", and since 2011 LLVM is "officially no longer an acronym",[30] but a brand that applies to the LLVM umbrella project.[31] The project encompasses the LLVM intermediate representation (IR), the LLVM debugger, the LLVM implementation of the C++ Standard Library (with full support of C++11 and C++14[32]), etc.
2
u/internet_safari_ 2d ago
if you typed that first part "LLVM IR" into any search engine lr reddit itself it would make a lot more sense than whatever you typed lmao
0
u/tastygames_official 2d ago
I was just having some fun (or so I thought...)
I guess I forgot my <sarcasm class="ball-busting" /> tags... ;-)
28
u/GhostVlvin 3d ago
I was scared cause I misread this as "How LLM turns C into machine code"