4 ms·
I'm trying to see if rustc does the same thing: https://godbolt.org/z/6WcYMf https://godbolt.org/z/6WcYMf but I am not good at reading this sort of thing. It l
by totalperspectiv 7y ago
I'm trying to see if rustc does the same thing: https://godbolt.org/z/6WcYMf https://godbolt.org/z/6WcYMf
but I am not good at reading this sort of thing. It looks like it has the same issue, but I didn't think rust had the type ambiguity that C++ does around u8 / char?
- totalperspectiv 7y agorustc also doesn't vectorize it?
- masklinn 7y agoIt does if you use iterators as totalperspectiv did in their alternate version https://godbolt.org/z/JpwcRN https://godbolt.org/z/JpwcRN Because Rust bounds-checks indexes, which likely breaks LLVM's vectorisation (you'd have to reimplement vectorisation & vectorised bounds-checking in MIR for it to work I guess e.g. bounds-check the entire chunk before applying the vectorised operation).
- totalperspectiv 7y agoTo answer both my own questions. Rust does not seem to have this same problem: https://godbolt.org/z/JpwcRN https://godbolt.org/z/JpwcRN But iterating over the loop with .iter_mut is different than iterating over 0..v.len(), which is interesting.
- masklinn 7y agoI'm guessing its bounds-checking, though replacing v[i] += 1 by `*v.get_unchecked_mut(i) += 1` generates yet another bit of code (it seems to somewhat unroll the loop but stops there). Also you can use `-C target_cpu=native` and get AVX512 instructions on godbolt's.
- comex 7y agoRust has no type-based aliasing restrictions at all. However, it does require references to be either unique or read-only, which means that it can automatically treat them as noalias (LLVM equivalent of __restrict). Unfortunately, this feature is disabled due to an LLVM bug: https://github.com/rust-lang/rust/issues/54878 https://github.com/rust-lang/rust/issues/54878 On the other hand, if you use the more idiomatic `&mut [u8]`, that passes the pointer and size by value, so it should avoid the issue. Edit: Using an iterator also avoids the problem, unlike in C++, because Rust iterators store their bounds within the iterator itself rather than calling iterable.end() on each iteration.
- lytigas 7y agoRegardless of what exactly rustc does right now, in the future Rust's strict aliasing rules will basically ensure Rust does this optimization in every possible case, because Rust reasons at compile time to ensure no mutable references ever alias. Things have been held up for a while on this LLVM bug, though[0]. [0] https://github.com/rust-lang/rust/issues/54878 https://github.com/rust-lang/rust/issues/54878
- yoshuaw 7y agoLast I heard there's a fair chance that this might be implemented at the MIR level instead, allowing it to be unblocked from the LLVM bug.