Oh noes, noalias enter the stage… that’s another very poorly understood bit of LLVM, as far as I know :-/ . Is there a coherent documentation somewhere of what it is supposed to “mean”, semantically, and how LLVM uses it? Existing academic research on the LLVM IR skips over noalias, just like it skips over C’s restrict - for good reasons…
I agree with your analysis of what LLVM does. However, I think adding noalias to malloc is only valid because the C standard allows the value of pointers to change on free. The trouble is that return values of malloc actually can alias - looking just as malloc itself, the annotation is plain wrong. However, due to what the C standard says about free, it is impossible to observe aliasing in C programs, and hence noalias becomes valid.
[quote=“comex, post:21, topic:3019”]Glancing at the LLVM code, and checking by looking at assembly output, it seems like that is currently the case, but said optimization sounds more like something that could hypothetically be added in the future, as one could imagine it being useful to remove redundant pointer equality checks in inlined functions or whatever. I certainly don’t see any language in the definition of noalias suggesting it would be out of bounds.
However, were LLVM to add such an optimization, I believe Rust would have no choice but to disable it: the optimizer is pretty powerful, so if there is any situation where safe code can trick it into assuming something that can turn out to be false, it is likely possible to get it to mistakenly eliminate bounds checks or whatnot with enough contortions. (As mentioned, you can certainly do that with undef, though that isn’t the issue here.)
[/quote]
Right now, Rust would have to disable it because it allows comparing raw pointers safely. I think if that was prohibited, then this optimization would be fine. Indeterminate values arise from uninitialized variables as well as dangling pointers, Rust can rule out the use of both in safe code.
Thanks a lot, this is very helpful information! Is this your reconstruction based on what LLVM does, or is there actually some kind of official stanza on this? The closes thing to a language definition of the LLVM IR I could find is http://llvm.org/docs/LangRef.html, but that doesn’t touch all these complicated points. In particular I wonder whether…[quote=“sunfishcode, post:22, topic:3019”]
The aliasing rules only apply to loads and stores, and not comparisons.
[/quote]… is a “guarantee”, or whether it could be changed without much of an announcement. Rust is relying on this right now.