-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
noalias is not enough #53105
Comments
Ah, good point. Read-write data races "just" make the read yield
Correct. AFAIK,
Oh yes, it is very much stronger in various ways. |
Isn't this the sort of thing the |
@varkor what would be the scopes for the |
In this example, as you point out, the aliasing is important with regards to memory accesses outside the function. So if in theory you could mark all the others... I doubt that's sufficient for LLVM though. |
Is it a good idea to write a LLVM enhancement request? |
As @varkor says, we could mark all others, and we would have to mark all others for every &mut that the programs creates, and even then, this is not something that alias analysis would take into account because no sane language front-end will do this. Extending LLVM to support this won't be easy either. Currently LLVM hoists memory ops from functions when profitable, but: // T: Copy
fn foo(x: &mut T) {
// in this scope there is only one
// pointer to the value behind x
let y = *x;
...
}
{
let mut z = T;
let ptr = &mut z as *mut T;
// hoist the load from foo out here
foo(&mut z);
*ptr = T;
} so when hoisting the load (or store) from So all the optimizations that currently move memory across scope would need to update and be extremely careful with any attribute/metadata that we might want to use. Maybe a minimal extension to alias analysis that allow us to specify the "opposite" / "negative" aliasing groups would be enough, but one would need to teach many pieces of the pipeline about this for the new information to result in better code gen. |
Triage: no idea what the current status of this is, to be honest. I imagine that this was never suggested upstream. |
If However, a new round of noalias/restrict patches has been landing recently (https://lists.llvm.org/pipermail/llvm-dev/2019-March/131127.html, https://reviews.llvm.org/D69542#change-veawD9rpruA2), so maybe that new infrastructure is powerful enough to express the desired guarantee here. FWIW, Stacked Borrows does allow the optimization. |
It seems EDIT: |
This might be a useful link for those interested: LLVM Alias Analysis Technical Call - Notes. The document is long. For the older entries I have done some cleanup and formatting to help with reading. I hope that helps, since I'm not really capable to help with the @nikic: one sec, are you the Nikita mentioned in the Meeting Notes? I should perhaps add your name to the meetings where you participated.
What optimization do you mean? The LLVM alias analysis passes? :o I thought that might eventually happen (and it makes sense, I think), but I'm asking just in case that's not what you meant. |
No, specifically inserting spurious writes -- having a function that originally might not have written to a given location at all, perform a write to that location. Stacked Borrows allows that optimization, Tree Borrows does not. This is one of the main reasons why a lot of real-world code that has UB under Stacked Borrows is fine under Tree Borrows. |
Somebody on the internet (https://blog.dend.ro/rust-and-the-case-of-the-redundant-comparison/) complained that something like this:
generates a conditional store:
on
x86_64
instead of just an unconditional storemovl $0, (%rdi); retq
.Taking a look at the optimized LLVM-IR:
shows the issue.
The LLVM-IR generated by rustc is loosing critical information. It marks
i32*
asnoalias
, which means, that no other pointers invec_clear
's scope will alias it. However, outsidevec_clear
scope, other pointers are allowed to alias that memory. That is, if*x
is zero, other threads could be concurrently reading the memory and if LLVM would generate an unconditional store here, that would introduce a data-race, which means that this optimization is not safe on the LLVM-IR generated by rustc. OTOH, &mut i32` means that the pointer has unique access to the memory, that is, no other pointer can access the memory behind it as long as that pointer is alive. Therefore, transforming the code to an unconditional store does not introduce a data-race.Therefore, I think that
noalias
is not enough to perform this optimization and that we would need something stronger for LLVM to be able to perform it.This also shows that
&mut T
is stronger than C'srestrict
keyword.The text was updated successfully, but these errors were encountered: