<div dir="ltr">Personally I don't think adding a new address space to express the idea that loads and stores may successfully dereference address zero is the best design.<br><div><br></div><div>The existing code to support the handling of address spaces does not inspire a lot of confidence. I am a simple non-GPU compiler developer. I have no idea how address spaces are supposed to work. When I read code that deals with them, I assume nothing about address spaces. I treat them conservatively. They are a black box, I ignore them.</div><div><br></div><div>This may sound bad, but this is normal. We all have different reasons to use and contribute to LLVM, and as much as we benefit by sharing designs and development resources as possible, sometimes we end up creating and playing in our own sandboxes and ignoring possible design space overlap with other users. A small amount of duplication of design and effort is OK if it allows people to make forward progress independently. I think this is an area where we want to do that.</div><div><br></div><div>I think we can bear the code complexity cost of having to check for both address spaces and this option, and I think both parties (address space users and -fno-delete-null-checks users) are willing to audit LLVM twice for these transformations.</div></div><br><div class="gmail_quote"><div dir="ltr">On Sat, Apr 28, 2018 at 1:12 PM Sanjoy Das via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On Thu, Apr 19, 2018 at 3:56 PM, Manoj Gupta via llvm-dev<br>
<<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<br>
> My understanding is we only want to disable  the optimizations regarding<br>
> undefined behavior<br>
> related to null pointer deference optimizations. And address space checks<br>
> will end up<br>
> disabling more optimizations than needed.<br>
<br>
[Repeating what others have already mentioned on this thread]<br>
<br>
I this it is better to avoid framing this as "disable optimizations that exploit<br>
UB" and instead frame this as "define some behavior that was undefined before".<br>
<br>
> I did look at some of the optimizations/transforms and there are some that<br>
> we definitely want to keep.<br>
><br>
> Just a quick example from grepping:<br>
> lib/Transforms/Scalar/LoopIdiomRecognize.cpp<br>
> ...........<br>
>              // Don't create memset_pattern16s with address spaces.<br>
>              StorePtr->getType()->getPointerAddressSpace() == 0 &&<br>
>              (PatternValue = getMemSetPatternValue(StoredVal, DL))) {<br>
>     // It looks like we can use PatternValue!<br>
>     return LegalStoreKind::MemsetPattern;<br>
>   }<br>
><br>
> Even worse, Sanitizers do NOT work with address spaces which is a big deal<br>
> breaker IMO.<br>
<br>
IMO fixing these seems less engineering overhead in the long term than<br>
introducing<br>
yet another knob to the IR.<br>
<br>
More importantly, we should _not_ be doing these optimizations without auditing<br>
them individually.  For instance with -fno-delete-null-pointer checks, it isn't<br>
okay to change a memset loop to a call to llvm.memset unless we've ensured llvm<br>
DTRT for llvm.memset and null checks (i.e. the null check in "llvm.memset(ptr,<br>
...); if (!ptr) {}" does not get optimized away).<br>
<br>
> Since address spaces and null pointers are really orthogonal issues, I would<br>
> prefer to<br>
> not conflate them.<br>
<br>
I'm not sure I agree with this.  Address spaces are a mechanism to provide<br>
additional semantics to pointers.  In this case the additional property we<br>
want is "address 0 may be dereferenceable", and using address spaces seems<br>
appropriate.<br>
<br>
> In addition, It is already not easy to convince Linux Kernel maintainers to<br>
> accept clang specific patches.<br>
<br>
Maybe I'm missing something, but I thought in this address space scheme we'd<br>
still provide a -fno-delete-null-ptr-checks flag -- it is clang that would mark<br>
pointers with address space n in this mode.<br>
<br>
> From Linux kernel maintainers POV, Clang built kernels are already "getting<br>
> lucky". So I am not too<br>
> worried about missing a few cases.<br>
> I'll be glad to fix the missing cases whenever reported.<br>
<br>
It seems to me that adding back missing (but correct) optimizations when<br>
reported is better than removing existing (but incorrect) optimizations when<br>
reported.  If I were a kernel developer (which I am not) I'd rather have a<br>
kernel that boots slower than a security vulnerability.<br>
<br>
> I also have some other concerns with address spaces e.g. how to pick a safe<br>
> address space not used by<br>
>  any target e.g. many targets (in and out-of-tree) actively use non-zero<br>
> address spaces.<br>
> User code can also specify any address space via<br>
> __attribute__((address_space(N))) so mixing object<br>
> files will be tricky in such cases.<br>
<br>
I think for that we can start a thread on cfe-dev and llvm-dev about reserving<br>
address spaces.  While at it, we should reserve a range of address spaces for<br>
LLVM middle-end use so that we can do more things like<br>
-fno-delete-null-pointer-checks without worrying about address space clashes.<br>
<br>
-- Sanjoy<br>
_______________________________________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a><br>
</blockquote></div>