Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

LLVM's support for this is bugged, so rust does not currently take (edit: full) advantage of it.


Rust does not take full advantage of it; that is, &T will still get noalias, it's &mut T that's currently disabled.

The tracking bug is https://github.com/rust-lang/rust/issues/54878


Here is an example which, while trivial, demonstrates some of the optimization issues you can run into in C and C++ but not in Rust, even with noalias currently only applying to &T.

C++: https://godbolt.org/z/dTPsbh Rust: https://rust.godbolt.org/z/Mdx87h

In C and C++, compilers are allowed to infer "noalias" based on pointer types; two pointers of different type are not allowed to alias. This is known as type-based alias analysis. But char * is given special treatment, because of its use as a generic pointer type; so if one of your arguments is char * or even signed char * , that disables any optimizations which were relying on type-based alias analysis.

This provides both for performance and undefined-behavior footguns. If you ever try to use some pointer type other than char * or signed char * to refer to data of another type, you may inadvertently cause type-based alias analysis to kick in, causing invalid optimizations and miscompilation. On the other hand, if you have a function which takes both a char * and another pointer, the compiler may not apply optimizations that it otherwise could because char * is allowed to alias anything.

In Rust, there is no such undefined behavior footgun. Because of the LLVM bug, noalias isn't applied to &mut pointers so there are still some cases which could be better optimized, though it sounds like there is some progress being made on the LLVM front so it should be fixed at some point, and there are already places where the compiler can do better optimizations with better safety due to the stronger semantics of &T.


Ah, my mistake. Thank you for the correction.


It's all good; most people don't draw the distinction. I myself didn't know that &T was also noalias for years.


I really hope this gets fixed at some point, just because I'd like to see how much faster rust can get. In particular I wonder how much it would impact the compiler itself since it is also written in rust.


Does not fully take advantage of it.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: