-
Notifications
You must be signed in to change notification settings - Fork 13k
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
LLVM Memory Model needs more rigor to avoid undesired optimization results #34577
Comments
This problem does not just affect clang; even safe Rust has miscompilations due to this: rust-lang/rust#45839 |
TL;DR Given the current state of the world and LLVM memory model, it's unclear what is okay and not. Your claim that this result is not allowed is ... not clearly right :) IE i'd stick with language that says "we don't want this to be right". Nuno, et al are working on a memory model, and this isn't going to get fixed until then. |
Ralf is part of the memory model team btw. |
And it won't be fixed because ATM, you can show that propagating any equalities at all (pointer or not) is enough to break variants of this testcase. Equality propagation exists all over the compiler (SimplifyInstruction does it too, as does CVP, LVI, you name it). There are also non-obvious forms of equality propagation that would have the same effect, and be very hard to stop from happening. You'd have to be able to stop anything from determining equivalence in general, and that's really really hard. IE Besides the intractability of solving all of the above ATM, disabling all of it causes very significant performance loss. |
Oh, nice. |
AFAIK these other examples rely on the inttoptr-simplification, don't they?
I don't even know what half of that is, sorry -- but I get your point. :)
Well, fair enough. Can we agree that either the compilation is wrong, or the above program should somehow trigger UB? In the latter case, that would mean that safe Rust programs can be UB, i.e., we'd need to figure out a way to statically ensure that this kind of stuff does not happen. |
Yes, but mainly out of laziness (with on offense meant!) My only concern, for example, is that i have a straightforward and sane way to know what to disallow and that it doesn't affect performance hugely. IE All i have to do is disallow deriving equality from comparisons between two pointer values. |
Hi, I recently came across what seems to be at least a similar bug, while developing an application. I eventually managed to produce the following test case, which is miscompiling for me using llvm-8.0.0-x86_64-apple-darwin.
The expected output is 0x123 0x456. Omitting the |
Hi Kosta, If I understand correctly, it is more like a bug of PRE incorrectly assuming that r.pointer1 is equivalent to et.pointer1 at the first iteration of the loop. After -O1, the bitcode looks like this:
GVN-PRE replaces r.pointer1 with phi(et.pointer1, et2.pointer1), which is incorrect because The incorrect replacement may have been fired due to the existence of the null comparison, but I guess it is not related with our bug (which replaces a pointer with another one with different origin). |
mentioned in issue llvm/llvm-bugzilla-archive#39846 |
mentioned in issue #39717 |
For what it's worth, while the LLVM memory model is indeed very unclear, I have not even seen a rough proposal of a sketch of a model that would allow this optimization. LLVM has a definition of "based on" for pointers that is quite clearly relevant, and the optimization in the OP can replace a pointer "based on" X by a pointer "based on" Y -- which is not equivalent, so this is not something an optimization is allowed to do. Many other questions remain open, but I think this one question we can answer with certainty: replacing one pointer value by another just because their addresses are equal is incorrect in a language like LLVM IR where "based on" for pointers is a deeply relevant notion (and not just an analysis artifact). It'd be great if LLVM could at least offer the option for frontends to opt-out of this clearly incorrect optimization (similar to the |
We have |
Yeah if we could just make that always return |
Here is another example of the same bug due to Matthew House. This actually gets miscompiled by both clang and GCC. |
Turns out there has been some progress here about a year ago: #82458 disables the incorrect optimization for many cases. There are some cave-outs unfortunately; not sure if there is any chance of those being removed eventually? However, there is one comment in the PR which doesn't seem quite correct to me. @nunoplopes wrote
|
Extended Description
Clang/LLVM currently miscompiles the following program:
This result is not allowed. If
a
andb
are both1
, the branchq == p
must have been taken, sor
was set to&x
(viaq
), sox
cannot be7777
.I think this issue has already come up in #33896, but so far there was no example showing that the bug arises independent of the incorrect inttoptr-simplification.
What is happening here (if my analysis is correct) is that GVN sees the equality
q == p
and uses that to replaceq
byp
in the then-branch. Next, LLVM notices that becausep
is derived fromy
, writing tor
(which will either have value&g
orp
in the line where the assignment happens) cannot possibly affectx
, and hence the initial value ofx
can be propagated into theprintf
. GVN is wrong to perform this kind of replacement; just because the bit representations of two pointers are equal, that doesn't mean that their provenance information is equal.Test case by Gil Hur.
The text was updated successfully, but these errors were encountered: