[llvm-dev] Why is remalloc not marked as noalias?
Hal Finkel via llvm-dev
llvm-dev at lists.llvm.org
Mon Jan 8 19:39:48 PST 2018
On 01/08/2018 04:40 AM, David Chisnall via llvm-dev wrote:
> On 26 Dec 2017, at 10:13, Bhatu via llvm-dev <llvm-dev at lists.llvm.org> wrote:
>> According to my understanding, it seems that the result of realloc will not technically alias the original pointer. When the realloc is done in-place the reference says:
>> "The original pointer ptr is invalidated and any access to it is undefined behavior (even if reallocation was in-place)."
>>
>> Additionally from the C11 standard we have:
>> "The value of a pointer becomes indeterminate when the object it points to (or just past) reaches the end of its lifetime"
>>
>> Is this enough to infer that we can safely mark realloc as noalias?
> Realloc is tricky in practice and has caused us to rethink pointer equality on CHERI. We’ve observed that this is a fairly common idiom:
>
> char *new = realloc(old, someLength);
> if (new != old)
> {
> // Update some copies of old to use new
> }
>
> This was problematic for us, because old and new had the same address but different bounds information and we were treating comparisons of pointers as equal if they pointed to the same location.
>
> This is problematic with noalias, because if the result of realloc is guaranteed never to alias with any existing pointer then this comparison is trivially true. This won’t break any of the code that I’ve seen, but it will mean that we’re optimising away the fast path and leaving only the slow path (which doesn’t quite accord with how most programmers understand the notion of optimisation).
Because in general we can't (and don't) use noalias to decide on pointer
equality, we won't optimize away the comparison. I think we're okay in
this regard.
>
> Note that this code does contain undefined behaviour, but as our prior work has shown, so does pretty much any nontrivial C program.
Indeed ;)
-Hal
>
> David
>
> _______________________________________________
> LLVM Developers mailing list
> llvm-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
--
Hal Finkel
Lead, Compiler Technology and Programming Languages
Leadership Computing Facility
Argonne National Laboratory
More information about the llvm-dev
mailing list