<div dir="ltr">This seems like a useful feature. I read the comments on the review, and I think the reviewers have a lot more context than I do, but with the limited information I have, tagging stores as in proposal A seems more general.<div><br></div><div>I have previously discussed the problem of how to make SROA work for C++ objects, in particular, std::vector, and I think that's a use case worth considering in your design.</div><div><br></div><div>The problem with C++ objects is that there is often some out-of-line method (think grow) that blocks SROA by taking the address of the object. Other optimizations such as GVN may do some work to reduce loads and stores to the vector object, but we could probably do more optimizations if a vector looked like three pointers (begin, end, capacity) to the optimizer, rather than a struct in memory. Trying to solve this problem would tend to lead in the direction of option B, where we have a "blessed" alloca that can be used to spill SSA values back to memory in the ABI-expected struct layout when there is register pressure.</div><div><br></div><div>This is definitely beyond the scope of what you have described here, since `grow` updates vector fields, and they then have to be reloaded as new SSA values. The vector storage can even be captured by special members of the stored object, so "nocapture" is not a good name for this extended feature. The idea is more along the lines of, "here is a pointer SSA value, I have to store it in this ABI-prescribed struct layout, but I'm passing it on the side in a bundle using appropriate aliasing annotations to inform optimizations".</div><div><br></div><div>I think there is something to this idea, but please don't let me derail your proposal by increasing the scope too much.</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Nov 8, 2021 at 2:53 PM Johannes Doerfert via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">NOTE: This was originally send in January 2021 [0]. The rational is <br>
still the same,<br>
       there are two different proposed solutions based on the <br>
conversations back then.<br>
<br>
TL;DR: A pointer stored in memory is not necessarily captured, let's add <br>
a way to express this in IR.<br>
<br>
<br>
--- Rational (copied mostly from [0]) ---<br>
<br>
This would solve PR48475.<br>
<br>
Runtime functions, as well as regular functions, might require a pointer<br>
to be passed in memory even though the memory is simply a means to pass<br>
(multiple) arguments. That is, the indirection through memory is only<br>
used on the call edge and not otherwise relevant. However, such pointers<br>
are currently assumed to escape as soon as they are stored in memory<br>
even if the callee only reloads them and use them in a "non-escaping" way.<br>
Generally, storing a pointer might not cause it to escape if all "uses of<br>
the memory" it is stored to all have the "nocapture" property. While the<br>
Attributor is aware of this and tries to determine all "copies" that the<br>
store created, other passes are not and frontends cannot provide this<br>
information for known APIs.<br>
<br>
To allow optimizations in the presence of pointers stored to memory we<br>
introduce *two* IR extensions:<br>
  Option A) `!nocapture_store` metadata and `"nocapture_use"` operand<br>
              bundle tags.<br>
  Option B) `!nocapture_storage` metadata and `"nocapture_use"` operand<br>
              bundle tags.<br>
Option A) is what was proposed in [0]. Option B) is slightly different and<br>
based on the discussions from [0] as well as a prototype patch [2].<br>
<br>
Semantics Option A)<br>
If a store of a pointer is tagged with `!nocapture_store` it guarantees that<br>
the pointer is not captured by this store. To ensure we still "account" for the<br>
uses of the pointer once it is reloaded we add the `"nocapture_use"` operand<br>
bundle tag with the pointer as argument to callees that will interact with the<br>
pointer loaded from memory.<br>
<br>
Semantics Option B)<br>
If a memory allocation is tagged with `!nocapture_storage` it guarantees that<br>
stores of a pointer to that memory are not capturing the pointer. To ensure<br>
we still "account" for the uses of the pointer once it is reloaded we add the<br>
`"nocapture_use"` operand bundle tag with the pointer as argument to callees<br>
that will interact with the pointer loaded from memory.<br>
The difference to Option B) is that we do not tag stores but allocations.<br>
<br>
<br>
--- Previous Discussion ---<br>
<br>
The discussion as part of [0] did evolve around a way to handle yet another use case,<br>
basically what happens if the reloads of the stored away pointer are (partially)<br>
exposed rather than hidden behind a runtime function interface. The short answer is:<br>
That is not supported by this RFC alone. The longer answer contains different possible<br>
extensions to this RFC that would allow us to support such use cases. That said, the<br>
runtime use case seems relevant enough to be handled first, especially since there is<br>
no frontend/pass right now (in LLVM) that would rely on any of the extended use cases.<br>
<br>
<br>
--- Proposal ---<br>
<br>
Resurrect [1], or make [2] into a proper patch.<br>
I still think [1] is the way to go as it is more generic and has less lookup cost.<br>
<br>
---<br>
<br>
~ Johannes<br>
<br>
<br>
[0] <a href="https://lists.llvm.org/pipermail/llvm-dev/2021-January/147664.html" rel="noreferrer" target="_blank">https://lists.llvm.org/pipermail/llvm-dev/2021-January/147664.html</a><br>
[1] <a href="https://reviews.llvm.org/D93189" rel="noreferrer" target="_blank">https://reviews.llvm.org/D93189</a><br>
[2] <a href="https://reviews.llvm.org/D109749#3078176" rel="noreferrer" target="_blank">https://reviews.llvm.org/D109749#3078176</a><br>
<br>
<br>
-- <br>
───────────────────<br>
∽ Johannes (he/his)<br>
<br>
_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div>