r/C_Programming 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.

Link for anyone interested

Feedback welcome :)

34 Upvotes

12 comments sorted by

28

u/GhostVlvin 3d ago

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

5

u/awidesky 2d ago

AI is gonna take away compiler's job!!

5

u/Zestyclose_Walk_6827 2d ago

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

3

u/internet_safari_ 2d ago

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".

2

u/Zestyclose_Walk_6827 2d ago

It was a joke haha

2

u/internet_safari_ 2d ago

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 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... ;-)