[llvm-dev] GEP with a null pointer base

Chris Lattner via llvm-dev llvm-dev at lists.llvm.org
Thu Jul 6 15:07:07 PDT 2017


> On Jul 6, 2017, at 2:05 PM, Peter Lawrence via llvm-dev <llvm-dev at lists.llvm.org> wrote:
> 
> 
>> On Jul 6, 2017, at 1:00 PM, via llvm-dev <llvm-dev at lists.llvm.org <mailto:llvm-dev at lists.llvm.org>> wrote:
>> 
>>>    So far, so good.  The problem is that while LLVM seems to consider
>>>    the above IR to be valid, we officially do not allow dereferencing
>>>    a pointer constructed in this way (if I’m reading the rules
>>>    correctly).  Consequently, if this GEP ever gets close enough to a
>>>    load using the pointer, InstCombine will eliminate the GEP and the
>>>    load.
> 
> This is the part that confuses me, why would such code be eliminated.
> If it is illegal then this should be a compilation failure,

This is illegal code, and if we only cared about the C spec, we could at least warn about it if not reject it outright.

That said, the purpose of clang is to build real code, and real code contains some amount of invalid constructs in important code bases.  We care about building a pragmatic compiler that gets the job done, so we sometimes “make things work” even though we don’t have to.  There are numerous patterns in old-style “offsetof” macros that do similar things.  Instead of fighting to make all the world’s code be theoretically ideal, it is better to just eat it and “do what they meant”.

-Chris


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20170706/e3e0cc8b/attachment.html>


More information about the llvm-dev mailing list