r/programming • u/Ok_Marionberry8922 • 9d ago
Why so many languages use LLVM
https://youtu.be/grjThkqV7uU?si=ei9cvzSS69v0MDS710
u/Slight-Bluebird-8921 8d ago
the common theme with all these things is that once someone solves a really boring problem that no one really wants to deal with, the instinct is to treat it as a black box and pray that it takes care of itself
for the same reason that no one really wants to do something as boring as write a web browser, no one wants to do this
2
u/AresFowl44 8d ago
TBF, writing a web browser is more of a black box nowadays, at least if you strife for high compatibility, as there are so many web standards to follow
5
u/Full-Spectral 7d ago
And purposefully so. Make it complex enough and it raises the barrier to competition till eventually you get down to basically 1 browser, owned by Google.
1
u/Blue_Moon_Lake 4d ago
You mean Firefox.
3
u/Full-Spectral 1d ago edited 1d ago
Well, Firefox really only exists because Google has traditionally financed the bulk of it, so they can avoid the kinds of monopoly issues that MS did back in the day. And it doesn't really take that much away from them, given that Firefox apparently only has about 3.5% of the market.
So, for what for them is a wee bit of money, they get to effectively own the (outside of closed ecosystems like Apple) browser market but still point to a 'competitor' to prove they aren't a monopoly. I use Firefox so I'm doing my part, but we are the few, the happy few, be we ne're so vile.
-15
9d ago
[deleted]
4
u/Ok_Chemistry_6387 8d ago
Source: Trust me bro.
Source: Some niche languages don't want to use it for issue with compile times.Meanwhile LLVM is every where. Used by every major tech company in the world.
4
u/double-you 8d ago
As a comment this is pretty worthless to any readers. Sure, perhaps it is. And? What are we to do with this information?
-3
9d ago edited 9d ago
[deleted]
10
u/AresFowl44 9d ago
LLVM and GCC are both very similar in optimizations, sometimes GCC is better, and sometimes Clang is. So your results are very surprising to say the least.
There's a reason why there's so many new low level languages on par with C in terms of speed and size and that reason is in big parts LLVM.
8
u/Ameisen 9d ago
I can say that I prefer Clang's frontend code to GCC's.
Especially that C and C++ use largely the same code in Clang, whereas in GCC they're separate. I never got ISO/IEC TR 18037:2008 working with C++, though Clang already supported named address spaces (though the semantics were... weird).
-2
9d ago
[deleted]
10
u/AresFowl44 9d ago
Like I mentioned I am a compiler author. Some random guy says to me
For the purposes of 99% of conversations on the internet we both are random guys. The best I was able to do to establish you as a compiler person was that you created a subreddit to a language with a dead website.
People say "sometimes GCC is better, and sometimes Clang is" despite never measuring.
I have measured as well and seen other people measure it, and in most cases the difference is negligible, while either wins in some cases.
As an example of some benchmarks, take the Benchmarks Game. Specifically the GCC vs Clang comparison or the Rust vs GCC comparison. For most of the given examples the performance is about the same.
I always measure and I never seen gcc worse.
Have you done the work to optimize the program equally for GCC and LLVM? Because what I otherwise assume to be happening is that your mental model is one that is more easily optimized by GCC than it is by Clang, so your GCC executable will always be better than your LLVM one.
Including the ones that transpile to C?
I was mostly referring to languages like Rust, Odin or previously Zig*, which all usually have similar performance to equivalent programs written in C, no matter if compiled with GCC or LLVM.
Languages transpiling to C to then be compiled by GCC can also achieve similar performance obviously, but that isn't relevant to my point that there are languages out there that do fully rely on LLVM to get C-like performance.
* Mind you, Zig was moving away from LLVM because LLVM is a very slow compiler, not because the code it produced was slow.
a few compiler authors
I would be curious which compiler authors you are talking about. Would you mind naming a few?
-1
9d ago edited 9d ago
[deleted]
8
u/AresFowl44 9d ago
I seen difference of 3 and 5% depending on how large the program is. Whats 'negligible'? Is 5% negligible? To some that is
For most applications a 5% is indeed neglible, because most applications aren't well optimized, aren't bound by CPU time or are running in noisy environments.
It'd make more sense to look at the same C source for gcc and clang, and some produce widely different speeds
Sure, those were just the numbers I found first. For some other numbers I've also found two Phoronix articles:
https://www.phoronix.com/review/clang20-gcc15-amd-znver5/2 https://www.phoronix.com/review/gcc-clang-eoy2023/2
Both articles still show my point, which is that sometimes GCC and sometimes LLVM is better, and that this has been true at least since GCC 13 / Clang 17.
The reason why I opened with llvm is a piece of junk was because it's consistently slower than gcc and the llvm project keeps breaking compilers due to breaking changes (api and llvm-ir).
I mean, it's not like GCC is any better in the regard of breaking changes, unless you target the frontend, at which point you can just as easily target LLVM without any breakage. I don't even think GCC has their IR documented, but that might have changed at some point.
And again, LLVM is mostly the same speed. The Benchmark Game GCC vs Clang examples have Clang 2% slower on average (excluding an outlier where only to programs total where submitted). Admittedly it's a small sample size, but as mentioned above,
Meanwhile for the Benchmark Game Rust examples I gave, it swang drastically between 80% slower and 30% faster, swinging drastically between much faster and much slower. So there isn't a lot to be gained from this example, other than the fact that LLVM can in fact optimize much better than GCC at times, but I doubt that was ever a point of contention.
The Phoronix articles I won't go too much into depth, but, as mentioned above, there's a fairly clear split between LLVM and GCC in terms of who produces an executable with more throughput.
Odin and Zig authors don't like llvm either, neither does the jai author.
Yes, but that's not because LLVM produces bad code, but because LLVM is slow and a big part of your languages infrastructure (which you also don't own). At least that's what I've heard from both Zig and Odin developers.
But do also note that both languages don't want to necessarily get rid of LLVM either, they just want to add alternate backends under their own banner.
Jai I haven't heard much of, but I also don't follow Blow at all.
1
7d ago
[deleted]
2
u/AresFowl44 7d ago
That question heavily depends on context and is not as easily answered as giving out a definitive percentage. For a HFT task 5% is the difference between you going rich or you going broke. If you're Cloudflare any 1% difference is going to save you millions.
Meanwhile somebody running an app on their computer isn't going to notice a difference between a task taking 5 seconds or 5.25 seconds. Especially if the user is for example running a web browser or a game or whatever in the background and that 5s suddenly becomes 7s. Or the application itself isn't optimized, and what could have been a 5s task now is a 10s task. And so on.
But it doesn't matter anyway, as I showed that the difference is more like 2%, or probably even less, but I'm not going to crunch all the ~200-300 numbers in the Phoronix articles.
24
u/wannaliveonmars 9d ago
TL;DR - it's because LLVM has a separate optimization step that converts unoptimized llvm code to optimized llvm code, and then a step that converts llvm code to different processor achitectures.
A new language needs to only create a "frontend" that converts from its source code to LLVM, then it gets the optimization and all the supported architectures for free.
Any new processor architecture can also implement an LLVM-backend and get all the supported languages for free, as well.