[llvm] [LangRef] Clarify pointer capture spec (PR #194647)
Antonio Frighetto via llvm-commits
llvm-commits at lists.llvm.org
Wed Apr 29 02:53:37 PDT 2026
================
@@ -3683,9 +3683,53 @@ memory before the call, the call may capture two components of the pointer:
rules <pointeraliasing>`. We further distinguish whether only read accesses
are allowed, or both reads and writes.
+These two cases are discussed in more detail in the following.
+
+Provenance capture
+^^^^^^^^^^^^^^^^^^
+
+If an argument does not capture the provenance the pointer, accesses that are
+based on the argument and are performed after the function returns (or unwinds)
+cause undefined behavior. In the case where only read provenance is captured,
+only stores cause undefined behavior.
+
+More formally, an argument that does not capture provenance will return a new
+pointer with the same address and a derived provenance, where the derived
+provenance initially has the same permissions as the original, but all access
+permissions are removed once the function returns (or unwinds). Or in the case
+of only capturing read provenance, the permissions are changed to only allow
+read accesses.
+
+This means that the following code is well-defined in isolation:
+
+.. code-block:: llvm
+
+ define void @f(ptr captures(address) %a, ptr %b) {
+ store ptr %a, ptr %b
+ ret void
+ }
+
+Even though the pointer is stored in another location that persists past the
+return of the function, this does not cause undefined behavior in itself. It
+depends on whether/how the pointer will be used in the future.
+
+.. code-block:: llvm
+
+ call void @f(ptr captures(address) %a, ptr %b)
+ %a2 = load ptr, ptr %b ; This is still well-defined
+ load i64, ptr %a2 ; This causes undefined behavior
+
+In this example, the persisted pointer is accessed after the return of the
+function, which causes undefined behavior.
----------------
antoniofrighetto wrote:
I would find it helpful to mention that, e.g., if an argument is annotated with `captures(address)`, this is essentially a promise that no dereference copies are done after the return (or otherwise UB), so the optimizer is allowed to make more aggressive assumptions (which I believe, among other things, would help understand why the spec is now defined as a semantic contract / in terms of UB); though not strong on this.
https://github.com/llvm/llvm-project/pull/194647
More information about the llvm-commits
mailing list