[PATCH] D152241: [CaptureTracking] Do not capture equality compares of same object

Noah Goldstein via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Jun 10 17:59:32 PDT 2023


goldstein.w.n added a comment.

In D152241#4411708 <https://reviews.llvm.org/D152241#4411708>, @caojoshua wrote:

>> If so then I think you can only do this for equality comparisons, otherwise something like:
>> Base + Offset1 < Base + Offset2 can probably end up leaking some bits (depending on what is known
>> about Offset1/Offset2) because of overflows.
>> If that is a concern then you need to also ensure the ICmp is equality (its unchecked at the moment).
>
> Thats a great point. I added a check to make sure its only equality comparisons.
>
> Does not have to be part of this patch, but could we extend this to cover non-equality comparisons if all the GEPs are inbounds? From https://llvm.org/docs/LangRef.html#id234, inbounds GEPs are poison if there is wrapping

If `inbounds` is present then comparison of two pointers that are products of GEP from the same base an entirely remove base and make the comparison entirely depend on the offsets
so it should be safe to include nocapture even for non-eq comparisons. That being said, something like:

  define i1 @src_gep(ptr %p, i64 %off) {
    %p_gep = getelementptr inbounds i64, ptr %p, i64 %off
    %cmp = icmp ugt ptr %p_gep, %p
    ret i1 %cmp
  }

Already folds out the GEP entirely (and as a result makes `%p` unused) so not sure if you actually need to change anything to get the desired behavior.

>   The multiplication of an index by the type size does not wrap the pointer index type in a signed sense (nsw).
>   The successive addition of offsets (without adding the base address) does not wrap the pointer index type in a signed sense (nsw).
>   The successive addition of the current address, interpreted as an unsigned number, and an offset, interpreted as a signed number, does not wrap the unsigned address space and remains in bounds of the allocated object. As a corollary, if the added offset is non-negative, the addition does not wrap in an unsigned sense (nuw).


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D152241/new/

https://reviews.llvm.org/D152241



More information about the llvm-commits mailing list