r/cpp
•
u/steveklabnik1
•
Oct 24 '24
•
1
Why Safety Profiles Failed
https://www.circle-lang.org/draft-profiles.html
180
Upvotes
View all comments
Show parent comment
1
u/MEaster Nov 01 '24
Ooh, that's a nice solution. I like that.
It looks like if you do the same on the unsafe Rust version, it does the same thing. I actually didn't think to check that, good catch! I wonder if the machinery to turn this off noalias tagging is still in the Rust compiler. It'd be kinda interesting to disable it and see what the performance difference is over a whole program.
No, that's not the case at all. Whether references alias other pointers or references has no bearing on whether it's marked noalias. I don't actually know how the borrow checker actually functions, I only know it as an end user, but from what I understand the current borrow checker doesn't work with origins, it instead works with spans of valid access. If I've understood things correctly, that's actually the primary difference between the current checker and Polonius, and what allows Polonius to better reason about borrowing.
But beyond that, borrow checking is purely a checking phase. It's not required to actually compile, in that it doesn't inform codegen in any way. In fact there's a Rust compiler, mrustc, which implements just enough Rust to build rustc (to shorted the bootstrap chain), and doesn't even have a borrow checker.
I could be wrong, but in order to do this and retain local reasoning, wouldn't you need to have runtime metadata on the references? When analysing a function you could do this for local references, but you can't just look at its callers to check for aliasing info on incoming references, because this could be the public API of a library, or in a different compilation unit, or the references are stored inside a vector where they all have different sources.
This is getting into differences in Rust's and C++'s object models. In C++'s abstract machine, I believe that an allocation (as in, a place where a value is stored, not a heap allocation) is inherently typed, and viewing it as the wrong type is UB.
In Rust's object model an allocation is a bundle of bytes with no inherent type. Whether a read is well defined is entirely dependent on those bytes being properly initialized and being valid values for the type you read it as. For example, I believe this program is sound because at no point do I read uninitialized data, or bytes that are invalid for the type being read.
This is how C++'s aliasing information is transferred over function boundaries: through the type. It would be UB to read as the wrong type, therefore it can infer that pointers to different types (with the exception of char, I think) never alias. You can see that in this version of our contrived example. By making the write pointer a different type the compiler now has the information it needs to know the pointers don't alias, and vectorises like the safe Rust version.
The unsafe Rust version wouldn't vectorise from changing the type of the write pointer because it would still be valid for it to alias one of the read pointers, because the object model allows it.
I have a toy compiler project written in Rust that uses LLVM, so I can relate a bit, too. Though my situation isn't greatly helped by using binding crate to handle FFI. It is definitely nice having LLVM's optimizer to make my crappy little horror show of a language fast.
Sure, and it's worth making sure that you don't jump the gun and go for what could ultimately be the wrong model and end up stuck with it. One argument in favour of going with a very strict model to start with is that it's not a backwards incompatible change to relax those restrictions later as long as the newer model doesn't reject anything the stricter model allowed. So it could be worth going with a strict model now, and then continuing research on relaxing it.
Unsafe only really allows you to do five things that safe doesn't, so it's not a big superset. That said, there is definitely a desire to improve unsafe, make it easier to use and harder to get wrong.
The concept of unsafe traits already exists in Rust, specifically so that unsafe code is allowed to make assumptions (Send and Sync are the common ones). So that part isn't a big leap. Nor is having a trait alter codegen (Copy). I think it would need to be two functions, something like this:
The second could have a default implementation that just drops
dstthen calls the first. Part of the invariants that must be upheld by the implementer would be no panicking, though that could be non-trivial. So, from a technical point of view, I don't think there's as much of an issue. I think it's more cultural; there's not a lot of desire for it. That could, of course, change over time, but that's not guaranteed.This conversation has had me wondering if it would be worth changing things so that you have to explicitly call a function to make a reference. It could probably be done on an edition change; the function would be available on all editions, but the new one would reject the deref operator on pointers.