[llvm-dev] getelementptr inbounds with offset 0

Ralf Jung via llvm-dev llvm-dev at lists.llvm.org
Mon Apr 15 02:06:39 PDT 2019


Hi,

>> For the (a) case, the question about "0-sized objects" remains, but it doesn't
>> seem like the answer could affect what LLVM does.
> 
> I think I now see (maybe part of) your point.
> Something like:
> 
>   x = malloc(0);
>   // ... anything except free(x) or equivalent
>   y = gep inbounds x, 0
>   // ... anything except free(x) or equivalent
>   use_but_not_dereference(y);
> 
> should be OK (= no undef/poison appears). Does that at least go in the
> right direction? I think this should be OK from the IR definition or
> something is broken. Obviously, there is always the possibility, or
> better the certainty, that the implementation is somewhere broken ;)

I guess that is a way to look at it -- though malloc can return NULL, and that
may (or may not) change the rules here.
But yes, this is the closest that you can get to in C when trying to mirror what
we do in Rust.  C does not have 0-sized types, Rust does, so there is no more
direct equivalent.

Kind regards,
Ralf


More information about the llvm-dev mailing list