Is LLVM well-optimised towards C++ compared to our beloved Rust?

While I was digging a bit deeper I learnt that LLVM toolchain is much more well optimised towards C++ compared to Rust. I don't know if it true or just a myth. I hope someone who understands the LLVM back-end would answer question.

I asked a question that's vaguely related previously.

I lurk a fair amount in the rust-lang zulip, and there hasn't really been any huge criticisms of llvm from what I've seen -- my general sense is that people are mostly happy with it.

I've been assuming that Rust internals were originally designed to work optimally with LLVM, meaning that changes to LLVM are not usually needed. Although I did hear about the LLVM bugs that needed fixing due to Rust making more extensive use of the restrict/noalias attribute.

2 Likes

My understanding, after reading rumours here and there for a few years, is that LLVM will generate as good code for Rust as it does for C++. The fact that Rust performance on various problems has been shown to be neck and neck with C/C++ bears that out.

However I have also heard that thanks to Rust's type system and the borrow checker there are optimisations that could be done for Rust that cannot be done for C/C++. These extra optimisations are either not supported din LLVM or are buggy because nobody has used them before. They will be available in the future. Sorry I can't link you to any such rumours just now.

1 Like

There used to be some performance related feature issues, but they are getting less.
As one example, (no)alias information was notoriously broken, because in C/C++ it requires an extra keyword (restrict), so people barely used it and a lot of cornercases remained. Then Rust added it everywhere, revealing bugs in llvm's handling of noalias.
Another fun bug went around when LLVM assumed that you can't have an infinite loop without side-effects, because that's also UB in C++. Well, bad, in Rust it wasn't UB, so LLVM broke valid Rust code. That also got fixed. If someone asks Ralf Jung, he'd probably be able to come up with a good list of things that are insufficiently defined in LLVM/C++, and which Rust want's to clarify, but I don't know too much about these.

So yeah, if you ask about performance there are some optimizations that are not as developed as we'd want for Rust, but I feel like that's mostly just a matter of time because other languages don't generate that IR and Rust/LLVM people work on adding these optimizations for Rust. For example, the new LLVM lead developer Nikita is also a Rust compiler team member, so I'm not really worried about this.

4 Likes

I thought no need to pay much attention on it.
For real would program, some times it's too complicated for compiler to optimize.
And you can try to optimizing some benchmarks like SPEC2017. I thought code size in some items of SPEC2017 is not too huge, but it still difficult for me to opt them.

Just write code, when you need to optimize them, then opt them.