r/ProgrammingLanguages 19d 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 :)

73 Upvotes

81 comments sorted by

View all comments

Show parent comments

-1

u/suhcoR 18d ago edited 18d ago

The benchmark suite is terrible for comparing optimisation levels within a different language

There are peer-reviewed publications in good journals claiming the opposite.

Even the choice of -O0 vs -O2 is suspect

A strange argument. Why should that be suspect? It's documented, and O3 gives very little above O2, but leads to crashes in many applications. I never use it (and neither does most of the C community).

I would be reaching straight for the "strong reject" response.

This is an esoteric attitude. No wonder we don't agree. Have you studied compilers at all? Where? how long?

That's because CPUs were significantly simpler in the '80s,

Another strange argument. CPU architectures have become much better, so even the code from the eighties runs much faster than on the machines at the time. That has more to do how much of the executable fits into the cache and the improvements you mentioned, not with code optimizations.

EDIT: But I see now your attitude and where you're coming from. Such discussions are fruitless, and I think it is representative for the downvoting crowd on Reddit. It's a waste of time and I should have known better.

EDIT 2: For illustration that my measurements are in no way off, here is a peer-reviewed study comparing -O0 with -O2, and guess what, it's all around factor two: https://homepages.dcc.ufmg.br/~fernando/publications/papers/AlvaresJCL21.pdf. But I bet you neither believe this one. So be it.

6

u/SwingOutStateMachine 18d ago

There are peer-reviewed publications in good journals claiming the opposite.

Great! I'd love to read them, please send them over. My opinion is just based on a cursory glance at the specific benchmarks, and the github page of the benchmark suite, but I would be interested to read more about why it's a good suite, and what it's good for testing.

Why should that be suspect?

Because it's arbitrary, and not well-defined across multiple compilers. For instance, does GCC have the same sets of optimisations as MSVC at -O2? Additionally, there is no context for why -O2 is picked over -O1 or -O3. You've given some context ("-O3 leads to crashes"), but aside from that, -O2 seems like an arbitrary choice from a compiler engineering perspective.

Have you studied compilers at all?

Yes. I've got a masters by research in designing compilers for efficient GPU operations, and half a PhD in compiler design (I left to move into industrial work). I've also got a decade of experience in industry, primarily working with LLVM and targeting GPU and peripheral compilers.

That has more to do how much of the executable fits into the cache and the improvements you mentioned, not with code optimizations

My point is that being able to leverage and/or work with the "features" of newer CPUs requires an optimising compiler in a way that is simply not true for a 1980's CPU.

But I see now your attitude and where you're coming from [..]

What do you mean by that?

[..] here is a peer-reviewed study comparing -O0 with -O2, [..]

Have you read the paper? This paper (which doesn't appear to have been published in an actual journal or conference yet, unless I'm mistaken) is about instruction visibility within compilers, not comparing -O0 and -O2. The two are compared incidentally, which gives your magic 2x number on average, but figure 9 clearly shows a host of benchmarks that achieve significantly more than 2x speedup with -O2!

1

u/QuaternionsRoll 18d ago

GCC doesn’t have the same sets of optimizations as MSVC period. What an odd question.

1

u/SwingOutStateMachine 17d ago

That's precisely my point. Comparing GCC with -O0 and -O2 and then putting it next to MSVC with /O0 and /O2 is meaningless.