[PATCH] D93189: [WIP] Introduce the `!nocapture` metadata and "nocapture_use" operand bundle

Johannes Doerfert via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Dec 13 19:49:30 PST 2020


jdoerfert created this revision.
Herald added subscribers: dexonsmith, bollu, hiraditya.
jdoerfert requested review of this revision.
Herald added a subscriber: sstefan1.
Herald added a project: LLVM.

NOTE: This is a WIP patch. Tests are missing, RFC needs to be written.

Runtime functions, as well as regular functions, might require a pointer
to be passed in memory even though the memory is simply a means to pass
multiple arguments. That is, the indirection through memory is only used
on the call edge. However, such pointers are currently assumed to escape
as soon as they are stored in memory even though the callee will only
reload them. Similarly, certain stores do not cause a pointer to escape
if uses of the memory it is stored to share share the same "nocaputure"
property.

To allow optimizations in the presence of pointers stored to memory we
introduce two new IR extensions. `!nocapture` metadata on stores and
"nocapture_use" operand bundles for call instructions. The former
ensures that the store can be ignored for the purpose of escape
analysis. The latter indicates that a call is using a pointer value
but not capturing it.

As an example we can consider the following code:

  struct Payload {
    int *a;
    double *b;
  };
  
  int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
                     void *(*start_routine) (void *), void *arg);
  
  void use(int, double);
  
  void fn(void *v) {
    Payload *p = (Payload*)(v);
    int *a = p->a;
    double *b = p->b;
    use(*a, *b);
  }
  
  void foo(int *a, double *b) {
    Payload p = {a, b};
    pthread_create(..., &fn, &p);
  }

Given the usage of the payload struct in `fn` we can conclude neither
`a` nor `b` in are captured in `foo`, however we cannot express this
fact. Part of this patch also contains use cases in the OpenMP runtime
which will be split off. The deduction for "generic" uses cases is not
included.

Among other things, this fixes PR48475.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D93189

Files:
  llvm/docs/LangRef.rst
  llvm/include/llvm/IR/FixedMetadataKinds.def
  llvm/lib/Analysis/CaptureTracking.cpp
  llvm/lib/IR/Verifier.cpp
  llvm/lib/Transforms/IPO/OpenMPOpt.cpp
  llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D93189.311479.patch
Type: text/x-patch
Size: 10646 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20201214/80b00fad/attachment.bin>


More information about the llvm-commits mailing list