I'm not convinced that this is the way to go either.
The notion of capture is still mostly an implementation detail of LLVM's alias analysis. It boils down to the fact whether the current implementation can keep track of all aliases of the pointer. We do have a section about pointer capture in the LangRef (LLVM Language Reference Manual — LLVM 16.0.0git documentation), but it is specific to nocapture attribute on calls. It is defined by explicitly prohibiting specific operations on the pointer within the function.
We don't prohibit operations per se. We prohibit behavior. You can copy a pointer if the copy is never read.
Implementation detail is that we can often not proof a copy is never read and therefore not a capture.
In general all/most attributes limit the possible semantics of a program, I would not call these details of the AA, e.g., inaccessible_mem is not an AA detail either even if we use it (mostly) there.
I find proposed definition of nocapture_store metadata rather vague. From Login
"the pointer stored is not captured in the sense that all uses of the pointer are explicitly marked otherwise and the storing can be ignored during capture analysis".
This definition makes lax use of the term "capture". This term is only defined in the LangRef with respect to pointers and calls.
That is valid criticism but totally addressable. Up until Philip added the lang ref part we never defined what capture is but simply went along with some notion of it.
Defining it further can be part of this for sure.
Arguably, the "or stored in the memory before the call" part of the LangRef right now is simply misleading as it is.
This implies some connection of capturing with calls, which is not a thing, and further sounds like there is a connection wrt. the order of instructions, which is not a thing either.
I can will update the patch with a new lang ref if we agree on the general outline and we can discuss details there.
Summary:
An operation can capture a pointer in a given scope if the execution of the operation makes any bit of that pointer available to an observer that is not explicitly marked as user of the pointer.
So, when u mark an operation as "nocapture", e.g. a store, you need to ensure all copies that are made/used through that operation are explicitly marked, e.g., for the runtime call site use case with operand bundles which we can later also use for loads.
It refers to "capture analysis" which is an implementation detail of LLVM's alias analysis and not defined anywhere.
Being captured is a property with semantic implication. Capture analysis determines that property. While we can certainly rephrase this, it's not a conceptual problem in my book.
It also says "all uses of the pointer are explicitly marked otherwise". The proposed way of marking the uses is the nocapture_use operand bundle. So, essentially, the only way a "nocapture stored" pointer can be used today is in a call (this is because today we can only attach operand bundles to calls and invokes). This is very limiting.
All true but on purpose. You need to make sure to mark copies explicitly if you disable the implicit way (via an attribute).
Let's talk about alternatives and limits:
- Marking call sites with attributes avoids this but introduces extra lookup costs. It is hard to make it compose (pointer, in memory. in memory,...). It works only with calls.
- Marking allocations requires lookup costs (not even less than above as you need to get to the allocation first and then to all uses). It is composable. It works with call and other uses (e.g., loads) either via operand bundles or def-use chain traversal.
- Marking stores (or in general operations) does not require lookup costs. It is composable. It works with call and other uses (again via operand bundles *or* via def-use chain traversal).
So from all options we discussed, marking the operation is the most direct way of encoding with the least drawbacks and all possibilities to extend it as we go.
We can add `nocapture` annotations to other operations (e.g., compares), we can add operand bundles to other uses (e.g., loads), or we can have a `nocapture store` version which requires the user to traverse the def-use chain of the allocation to find the potential users.
The proposal at hand is not restricting us but a setup for most performance in the use case we first tackle.
This limitation can be lifted if we introduce operand bundles on arbitrary instructions. Assuming we do that, the definition above suggests that we need to mark *all* uses of the "nocapture stored" pointers. It means every single instruction, including geps, bitcasts, etc. need to bear this operand bundle. This looks verbose and fragile. Every transform now needs to preserve this marking, otherwise risking a miscompile.
We can extend operand bundles to instructions and there have been use cases before. Regardless of the way we encode it, e.g., as extra/optional pointer operand or operand bundle or intrinsic that ties the load and the stored value together, you never need to annotate everything.
I'm not sure where that idea comes from but the use case in which we do not have a runtime call (behind which all the uses of the stored value are hidden) requires you only to annotate the potential copies of the "nocapture stored" value. As mentioned above, we could even have a
version that requires you to traverse the def-use chains to find all uses but that is only slightly more helpful than no annotation at all. Instead you would do this:
store %p %mem, !nocapture
...
bc = bitcast %mem to ...
copy = load %bc, !nocapture_use(%p)
// or alternatively
copy_val = load %bc
copy = llvm.nocapture_use(%copy2_val, %p)
...
use(%copy)
Now, %copy is marked as potential copy of %p explicitly. If you see the store of %p you can ignore it effectively as you'll see the uses later.
No bitcast, gep, or anything else needs to be marked or is impacted, it's all about the value that goes into the operation (here into the store) and the potential copies that come from the operation performed (here the store).
Speaking of possible alternatives, we can probably define "nocapture storage" as memory that doesn't outlive the scope of the current function (it should probably have some other name, because it doesn't rely on term capture). Having this property we can extend capture tracking with flow-insensitive analysis to keep track of pointers stored into "nocapture storage".
Nocapture storage should not be limited by lifetime. We want this property for globals too.
The other alternative that was suggested previously was to explicitly mark aliasing properties in the IR similarly to (or using) noalias and alias.scope metadata.
This encodes uses of potential copies of values. That scales nicely and is generic, e.g., we can also go away from the "nocapture" part of all this and use a very similar mechanism to track potential copies of any value through memory (and calls).
~ Johannes