[cfe-dev] [llvm-dev] RFC: Implementing -fno-delete-null-pointer-checks in clang

Manoj Gupta via cfe-dev cfe-dev at lists.llvm.org
Mon Apr 30 11:30:56 PDT 2018

Thanks everyone for the comments,

I actually have a working patch now for "-fno-delete-null-pointer-checks"
option based on the function attribute.
It is not a *huge* change and I only had to change a few places where IR
decision were based on null pointer references (Though I may have missed
some places).

Will be sending the patches for review after cleanup and testing.

On Mon, Apr 30, 2018 at 11:20 AM Reid Kleckner <rnk at google.com> wrote:

> 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

> 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.

> 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.

> 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.

> On Sat, Apr 28, 2018 at 1:12 PM Sanjoy Das via cfe-dev <
cfe-dev at lists.llvm.org> wrote:

>> On Thu, Apr 19, 2018 at 3:56 PM, Manoj Gupta via llvm-dev
>> <llvm-dev at lists.llvm.org> wrote:
>> > My understanding is we only want to disable  the optimizations
>> > undefined behavior
>> > related to null pointer deference optimizations. And address space
>> > will end up
>> > disabling more optimizations than needed.

>> [Repeating what others have already mentioned on this thread]

>> I this it is better to avoid framing this as "disable optimizations that
>> UB" and instead frame this as "define some behavior that was undefined

>> > I did look at some of the optimizations/transforms and there are some
>> > we definitely want to keep.
>> >
>> > Just a quick example from grepping:
>> > lib/Transforms/Scalar/LoopIdiomRecognize.cpp
>> > ...........
>> >              // Don't create memset_pattern16s with address spaces.
>> >              StorePtr->getType()->getPointerAddressSpace() == 0 &&
>> >              (PatternValue = getMemSetPatternValue(StoredVal, DL))) {
>> >     // It looks like we can use PatternValue!
>> >     return LegalStoreKind::MemsetPattern;
>> >   }
>> >
>> > Even worse, Sanitizers do NOT work with address spaces which is a big
>> > breaker IMO.

>> IMO fixing these seems less engineering overhead in the long term than
>> introducing
>> yet another knob to the IR.

>> More importantly, we should _not_ be doing these optimizations without
>> them individually.  For instance with -fno-delete-null-pointer checks,
it isn't
>> okay to change a memset loop to a call to llvm.memset unless we've
ensured llvm
>> DTRT for llvm.memset and null checks (i.e. the null check in
>> ...); if (!ptr) {}" does not get optimized away).

>> > Since address spaces and null pointers are really orthogonal issues, I
>> > prefer to
>> > not conflate them.

>> I'm not sure I agree with this.  Address spaces are a mechanism to
>> additional semantics to pointers.  In this case the additional property
>> want is "address 0 may be dereferenceable", and using address spaces
>> appropriate.

>> > In addition, It is already not easy to convince Linux Kernel
maintainers to
>> > accept clang specific patches.

>> Maybe I'm missing something, but I thought in this address space scheme
>> still provide a -fno-delete-null-ptr-checks flag -- it is clang that
would mark
>> pointers with address space n in this mode.

>> > From Linux kernel maintainers POV, Clang built kernels are already
>> > lucky". So I am not too
>> > worried about missing a few cases.
>> > I'll be glad to fix the missing cases whenever reported.

>> It seems to me that adding back missing (but correct) optimizations when
>> reported is better than removing existing (but incorrect) optimizations
>> reported.  If I were a kernel developer (which I am not) I'd rather have
>> kernel that boots slower than a security vulnerability.

>> > I also have some other concerns with address spaces e.g. how to pick a
>> > address space not used by
>> >  any target e.g. many targets (in and out-of-tree) actively use
>> > address spaces.
>> > User code can also specify any address space via
>> > __attribute__((address_space(N))) so mixing object
>> > files will be tricky in such cases.

>> I think for that we can start a thread on cfe-dev and llvm-dev about
>> address spaces.  While at it, we should reserve a range of address
spaces for
>> LLVM middle-end use so that we can do more things like
>> -fno-delete-null-pointer-checks without worrying about address space

>> -- Sanjoy
>> _______________________________________________
>> cfe-dev mailing list
>> cfe-dev at lists.llvm.org
>> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev

More information about the cfe-dev mailing list