-
Notifications
You must be signed in to change notification settings - Fork 57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unique reference pointer equality tests are not optimized #390
Comments
Maybe related: rust-lang/rust#56604 (nevermind, the optimization doesn't happen with |
It is currently not clear whether we are allowed to optimize this, as we do not have a finalized aliasing model yet. Under Stacked Borrows this would be a legal optimization, but under LLVM |
@Nilstrieb even without Stacked Borrows, the current documentation seems very clear that unique references can never alias, even when Tagging @RalfJung as the resident Stacked Borrows expert. |
Depending on what you mean with alias, this is trivially untrue in safe code. This code works with NLL: playground fn main() {
let mut a = ();
let a1 = &mut a;
let a2 = &mut *a1;
let p2 = a2 as *const ();
let p1 = a1 as *const ();
assert_eq!(p1, p2);
} I think this code here is similar enough to not warrant these opts until we have decided on an exact aliasing model that may or may not allow this opt (although I guess that it will). |
|
From the
To me that seems to leave very little room for interpretation. |
It does not copy |
It doesn't define what "alias" means, so there's plenty of room for Interpretation :). But yes, according to the spirit of the docs this optimization is legal, but I don't think it's precise enough to act on it. Although I do agree that it's quite likely that this optimization will be legal under the aliasing model we will get. |
This is an interesting question. Surprising as it may sound, aliasing is actually only loosely related to pointer equality. (For that reason, LLVM cannot use noalias to optimize ptr equality tests.) So until we have a very concrete model I think it is advisable to avoid optimizing prematurely.
However it would be valuable to identify real-world cases where an optimization like this could be measurably useful.
|
Fascinating, I'd love to hear more about it. Can you point me to a source. |
Also what do you suggest we do with this bug, close it? |
The definition of
Basically, when compilers talk about 'noalias', the property they really care about is "can I reorder these two memory accesses". A Now, Rust's definition does not exactly match LLVM or C. So as others stated above, it may well be that some pointer equality optimizations will be legal on Rust. But that would be "collateral benefits" and not treated with high priority -- unless we have known examples where such pointer equality optimizations could be very valuable. Also we'd have to write our own optimization passes for this; LLVM is not allowed to do that since noalias does not make any such promises. |
I have moved it to the UCG repo. I assume this is a reoccurring question and thus worth tracking as an FAQ item, and as a place for people to put examples where such an optimization could be very useful. |
I tried this code:
I expected to see this happen:
The optimised function should always return false. However it generates a comparison. As seen here https://godbolt.org/z/5YMK1Wc38.
rustc --version --verbose
:The text was updated successfully, but these errors were encountered: