@@ -1379,6 +1379,36 @@ Currently, only the following parameter attributes are defined:
13791379 function, returning a pointer to allocated storage disjoint from the
13801380 storage for any other object accessible to the caller.
13811381
1382+ ``captures(...)``
1383+ This attributes restrict the ways in which the callee may capture the
1384+ pointer. This is not a valid attribute for return values. This attribute
1385+ applies only to the particular copy of the pointer passed in this argument.
1386+
1387+ The arguments of ``captures`` is a list of captured pointer components,
1388+ which may be ``none``, or a combination of:
1389+
1390+ - ``address``: The integral address of the pointer.
1391+ - ``provenance``: The ability to access the pointer for both read and write
1392+ after the function returns.
1393+ - ``read_provenance``: The ability to access the pointer only for reads
1394+ after the function returns.
1395+
1396+ Additionally, it is possible to specify that the pointer is captured via
1397+ the return value only, by using ``caputres(ret: ...)``.
1398+
1399+ The `pointer capture section <pointercapture>` discusses these semantics
1400+ in more detail.
1401+
1402+ Some examples of how to use the attribute:
1403+
1404+ - ``captures(none)``: Pointer not captured.
1405+ - ``captures(address, provenance)``: Equivalent to omitting the attribute.
1406+ - ``captures(address)``: Address may be captured, but not provenance.
1407+ - ``captures(address, read_provenance)``: Both address and provenance
1408+ captured, but only for read-only access.
1409+ - ``captures(ret: address, provenance)``: Pointer captured through return
1410+ value only.
1411+
13821412.. _nocapture:
13831413
13841414``nocapture``
@@ -3318,10 +3348,91 @@ Pointer Capture
33183348---------------
33193349
33203350Given a function call and a pointer that is passed as an argument or stored in
3321- the memory before the call, a pointer is *captured* by the call if it makes a
3322- copy of any part of the pointer that outlives the call.
3323- To be precise, a pointer is captured if one or more of the following conditions
3324- hold:
3351+ memory before the call, the call may capture two components of the pointer:
3352+
3353+ * The address of the pointer, which is its integral value. This also includes
3354+ parts of the address or any information about the address, including the
3355+ fact that it does not equal one specific value.
3356+ * The provenance of the pointer, which is the ability to perform memory
3357+ accesses through the pointer, in the sense of the :ref:`pointer aliasing
3358+ rules <pointeraliasing>`. We further distinguish whether only read acceses
3359+ are allowed, or both reads and writes.
3360+
3361+ For example, the following function captures the address of ``%a``, because
3362+ it is compared to a pointer, leaking information about the identitiy of the
3363+ pointer:
3364+
3365+ .. code-block:: llvm
3366+
3367+ @glb = global i8 0
3368+
3369+ define i1 @f(ptr %a) {
3370+ %c = icmp eq ptr %a, @glb
3371+ ret i1 %c
3372+ }
3373+
3374+ The function does not capture the provenance of the pointer, because the
3375+ ``icmp`` instruction only operates on the pointer address. The following
3376+ function captures both the address and provenance of the pointer, as both
3377+ may be read from ``@glb`` after the function returns:
3378+
3379+ .. code-block:: llvm
3380+
3381+ @glb = global ptr null
3382+
3383+ define void @f(ptr %a) {
3384+ store ptr %a, ptr @glb
3385+ ret void
3386+ }
3387+
3388+ The following function captures *neither* the address nor the provenance of
3389+ the pointer:
3390+
3391+ .. code-block:: llvm
3392+
3393+ define i32 @f(ptr %a) {
3394+ %v = load i32, ptr %a
3395+ ret i32
3396+ }
3397+
3398+ While address capture includes uses of the address within the body of the
3399+ function, provenance capture refers exclusively to the ability to perform
3400+ accesses *after* the function returns. Memory accesses within the function
3401+ itself are not considered pointer captures.
3402+
3403+ We can further say that the capture only occurs through a specific location.
3404+ In the following example, the pointer (both address and provenance) is captured
3405+ through the return value only:
3406+
3407+ .. code-block:: llvm
3408+
3409+ define ptr @f(ptr %a) {
3410+ %gep = getelementptr i8, ptr %a, i64 4
3411+ ret ptr %gep
3412+ }
3413+
3414+ However, we always consider direct inspection of the pointer address
3415+ (e.g. using ``ptrtoint``) to be location-independent. The following example
3416+ is *not* considered a return-only capture, even though the ``ptrtoint``
3417+ ultimately only contribues to the return value:
3418+
3419+ .. code-block:: llvm
3420+
3421+ @lookup = constant [4 x i8] [i8 0, i8 1, i8 2, i8 3]
3422+
3423+ define ptr @f(ptr %a) {
3424+ %a.addr = ptrtoint ptr %a to i64
3425+ %mask = and i64 %a.addr, 3
3426+ %gep = getelementptr i8, ptr @lookup, i64 %mask
3427+ ret ptr %gep
3428+ }
3429+
3430+ This definition is chosen to allow capture analysis to continue with the return
3431+ value in the usual fashion.
3432+
3433+ The following describes possible ways to capture a pointer in more detail,
3434+ where unqualified uses of the word "capture" refer to capturing both address
3435+ and provenance.
33253436
332634371. The call stores any bit of the pointer carrying information into a place,
33273438 and the stored bits can be read from the place by the caller after this call
@@ -3360,30 +3471,30 @@ hold:
33603471 @lock = global i1 true
33613472
33623473 define void @f(ptr %a) {
3363- store ptr %a, ptr* @glb
3474+ store ptr %a, ptr @glb
33643475 store atomic i1 false, ptr @lock release ; %a is captured because another thread can safely read @glb
33653476 store ptr null, ptr @glb
33663477 ret void
33673478 }
33683479
3369- 3. The call's behavior depends on any bit of the pointer carrying information.
3480+ 3. The call's behavior depends on any bit of the pointer carrying information
3481+ (address capture only).
33703482
33713483.. code-block:: llvm
33723484
33733485 @glb = global i8 0
33743486
33753487 define void @f(ptr %a) {
33763488 %c = icmp eq ptr %a, @glb
3377- br i1 %c, label %BB_EXIT, label %BB_CONTINUE ; escapes %a
3489+ br i1 %c, label %BB_EXIT, label %BB_CONTINUE ; captures address of %a only
33783490 BB_EXIT:
33793491 call void @exit()
33803492 unreachable
33813493 BB_CONTINUE:
33823494 ret void
33833495 }
33843496
3385- 4. The pointer is used in a volatile access as its address.
3386-
3497+ 4. The pointer is used as the pointer operand of a volatile access.
33873498
33883499.. _volatile:
33893500
0 commit comments