<html><head><meta http-equiv="Content-Type" content="text/html; charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div><blockquote type="cite" class=""><div class="">On Apr 30, 2018, at 4:24 PM, Richard Smith <<a href="mailto:richard@metafoo.co.uk" class="">richard@metafoo.co.uk</a>> wrote:</div><div class=""><div dir="ltr" class=""><div class="gmail_extra"><div class="gmail_quote">On 30 April 2018 at 11:14, John McCall via llvm-dev <span dir="ltr" class=""><<a href="mailto:llvm-dev@lists.llvm.org" target="_blank" class="">llvm-dev@lists.llvm.org</a>></span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5">> On Apr 28, 2018, at 4:12 PM, Sanjoy Das via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org" class="">cfe-dev@lists.llvm.org</a>> wrote:<br class="">
> On Thu, Apr 19, 2018 at 3:56 PM, Manoj Gupta via llvm-dev<br class="">
> <<a href="mailto:llvm-dev@lists.llvm.org" class="">llvm-dev@lists.llvm.org</a>> wrote:<br class="">
>> My understanding is we only want to disable  the optimizations regarding<br class="">
>> undefined behavior<br class="">
>> related to null pointer deference optimizations. And address space checks<br class="">
>> will end up<br class="">
>> disabling more optimizations than needed.<br class="">
> <br class="">
> [Repeating what others have already mentioned on this thread]<br class="">
> <br class="">
> I this it is better to avoid framing this as "disable optimizations that exploit<br class="">
> UB" and instead frame this as "define some behavior that was undefined before".<br class="">
> <br class="">
>> I did look at some of the optimizations/transforms and there are some that<br class="">
>> we definitely want to keep.<br class="">
>> <br class="">
>> Just a quick example from grepping:<br class="">
>> lib/Transforms/Scalar/<wbr class="">LoopIdiomRecognize.cpp<br class="">
>> ...........<br class="">
>>             // Don't create memset_pattern16s with address spaces.<br class="">
>>             StorePtr->getType()-><wbr class="">getPointerAddressSpace() == 0 &&<br class="">
>>             (PatternValue = getMemSetPatternValue(<wbr class="">StoredVal, DL))) {<br class="">
>>    // It looks like we can use PatternValue!<br class="">
>>    return LegalStoreKind::MemsetPattern;<br class="">
>>  }<br class="">
>> <br class="">
>> Even worse, Sanitizers do NOT work with address spaces which is a big deal<br class="">
>> breaker IMO.<br class="">
> <br class="">
> IMO fixing these seems less engineering overhead in the long term than<br class="">
> introducing<br class="">
> yet another knob to the IR.<br class="">
> <br class="">
> More importantly, we should _not_ be doing these optimizations without auditing<br class="">
> them individually.  For instance with -fno-delete-null-pointer checks, it isn't<br class="">
> okay to change a memset loop to a call to llvm.memset unless we've ensured llvm<br class="">
> DTRT for llvm.memset and null checks (i.e. the null check in "llvm.memset(ptr,<br class="">
> ...); if (!ptr) {}" does not get optimized away).<br class="">
> <br class="">
>> Since address spaces and null pointers are really orthogonal issues, I would<br class="">
>> prefer to<br class="">
>> not conflate them.<br class="">
> <br class="">
> I'm not sure I agree with this.  Address spaces are a mechanism to provide<br class="">
> additional semantics to pointers.  In this case the additional property we<br class="">
> want is "address 0 may be dereferenceable", and using address spaces seems<br class="">
> appropriate.<br class="">
> <br class="">
>> In addition, It is already not easy to convince Linux Kernel maintainers to<br class="">
>> accept clang specific patches.<br class="">
> <br class="">
> Maybe I'm missing something, but I thought in this address space scheme we'd<br class="">
> still provide a -fno-delete-null-ptr-checks flag -- it is clang that would mark<br class="">
> pointers with address space n in this mode.<br class="">
> <br class="">
>> From Linux kernel maintainers POV, Clang built kernels are already "getting<br class="">
>> lucky". So I am not too<br class="">
>> worried about missing a few cases.<br class="">
>> I'll be glad to fix the missing cases whenever reported.<br class="">
> <br class="">
> It seems to me that adding back missing (but correct) optimizations when<br class="">
> reported is better than removing existing (but incorrect) optimizations when<br class="">
> reported.  If I were a kernel developer (which I am not) I'd rather have a<br class="">
> kernel that boots slower than a security vulnerability.<br class="">
> <br class="">
>> I also have some other concerns with address spaces e.g. how to pick a safe<br class="">
>> address space not used by<br class="">
>> any target e.g. many targets (in and out-of-tree) actively use non-zero<br class="">
>> address spaces.<br class="">
>> User code can also specify any address space via<br class="">
>> __attribute__((address_space(<wbr class="">N))) so mixing object<br class="">
>> files will be tricky in such cases.<br class="">
> <br class="">
> I think for that we can start a thread on cfe-dev and llvm-dev about reserving<br class="">
> address spaces.  While at it, we should reserve a range of address spaces for<br class="">
> LLVM middle-end use so that we can do more things like<br class="">
> -fno-delete-null-pointer-<wbr class="">checks without worrying about address space clashes.<br class="">
<br class="">
</div></div>The LLVM address space design has pushed well beyond the sensible boundaries<br class="">
of less-is-more and really needs some concerted effort to actually define the expected<br class="">
properties of different address spaces instead of a dozen different engineers applying<br class="">
a "don't do this optimization if the pointer is in a non-zero address space" rule to the<br class="">
optimizer with a shotgun.<br class="">
<br class="">
In fact, if we'd already done that, we wouldn't need any sort of address-space hack<br class="">
to support this request.  We'd just need a very simple audit of the places that check<br class="">
the "are dereferences of the zero address undefined behavior" bit to make sure that<br class="">
they honor it even in address space 0.  But instead that audit will be confused by a<br class="">
thousand places that just bail out for non-zero address spaces without further<br class="">
explanation.<br class="">
<br class="">
Such a design would hopefully include reserving ranges of address spaces for different<br class="">
purposes.  Clang is already perfectly capable of remapping address space numbers<br class="">
from the user-facing address_space attribute when generating IR.</blockquote><div class=""><br class=""></div><div class="">From the peanut gallery: has any thought been given to moving away from hardcoded address space numbers entirely? Abstractly it seems like an extra layer of indirection here could help (eg, we could allow IR to define address spaces that can be part of pointer types, and those could nominate which target-specific pointer representation and interpretation they use -- perhaps by name instead of by number -- as well as other properties such as whether zero is a valid address).</div></div></div></div>
</div></blockquote></div><br class=""><div class="">I think that would be great design, yeah.  Even if we stuck with identifying them by numbers, I would hope that we would allow them to be described at the top-level this way, and that backward compatibility would be handled by just filling in maximally conservative information.</div><div class=""><br class=""></div><div class="">John.</div></body></html>