r/C_Programming • • Aug 23 '26

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.

Link for anyone interested

Feedback welcome :)

38 Upvotes

13 comments sorted by

33

u/GhostVlvin Aug 23 '26

I was scared cause I misread this as "How LLM turns C into machine code"

8

u/awidesky Aug 24 '26

AI is gonna take away compiler's job!!

4

u/Zestyclose_Walk_6827 Aug 24 '26

Compilers have really fast and small LLMs under the hood. That’s how they optimize code

3

u/internet_safari_ Aug 24 '26

While that could be technically true it is misleading. Just because it has a parser, tokens, a lexer can allow you to claim this, but deviating anywhere farther from stricly what is actually is (a parser, tokens, and a lexer), brings attention away from how it actually works.

If someone said "I want to make a parser to learn making a compiler" it is in reality just practically speaking going to be unrecognizable compared to "I'm going make an LLM to learn making a compiler".

3

u/Zestyclose_Walk_6827 Aug 24 '26

It was a joke haha

2

u/internet_safari_ Aug 24 '26

This sub sometimes gives me anxiety about the typical post simply losing experience and knowledge of C and a lack of places to go for knowledgeable groups until the art is lost and replaced with "bold ideas" and C itself is either abandoned or changed in needless poor taste.

But usually it's just me being paranoid and I see equally many signs to keep the hope alive!

1

u/pierrejoy Aug 24 '26

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_ Aug 24 '26

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

1

u/pierrejoy Sep 11 '26

generating the IR and handling the IRs are different things. llvm is the compiler infrastructure. any frontend can add whatever passes they want,use mlir instead, which may have multiple pases too, etc. (like clang mlir recent addition as part of clang if I remember correctly).

for the rest i cannot parse the "rambling" ;-)

-11

u/tastygames_official Aug 23 '26

LLVM IR? Long-Lonst Venerial Machination IrreDescence?

6

u/smcameron Aug 24 '26

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_ Aug 24 '26

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 Aug 24 '26

I was just having some fun (or so I thought...)

I guess I forgot my <sarcasm class="ball-busting" /> tags... ;-)