Skip to content

Commit ff0e5a9

Browse files
authored
Merge pull request #60829 from atrick/comment-bindmem
[comment] Add a semantics note to SIL BindMemory
2 parents e22fc3e + c14059e commit ff0e5a9

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

docs/SIL.rst

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4363,11 +4363,22 @@ bind_memory
43634363
Binds memory at ``Builtin.RawPointer`` value ``%0`` to type ``$T`` with enough
43644364
capacity to hold ``%1`` values. See SE-0107: UnsafeRawPointer.
43654365

4366-
Produces a opaque token representing the previous memory state. For
4367-
memory binding semantics, this state includes the type that the memory
4368-
was previously bound to. The token cannot, however, be used to
4369-
retrieve a metatype. It's value is only meaningful to the Swift
4370-
runtime for typed pointer verification.
4366+
Produces a opaque token representing the previous memory state for
4367+
memory binding semantics. This abstract state includes the type that
4368+
the memory was previously bound to along with the size of the affected
4369+
memory region, which can be derived from ``%1``. The token cannot, for
4370+
example, be used to retrieve a metatype. It only serves a purpose when
4371+
used by ``rebind_memory``, which has no static type information. The
4372+
token dynamically passes type information from the first bind_memory
4373+
into a chain of rebind_memory operations.
4374+
4375+
Example::
4376+
4377+
%_ = bind_memory %0 : $Builtin.RawPointer, %numT : $Builtin.Word to $T // holds type 'T'
4378+
%token0 = bind_memory %0 : $Builtin.RawPointer, %numU : $Builtin.Word to $U // holds type 'U'
4379+
%token1 = rebind_memory %0 : $Builtin.RawPointer, %token0 : $Builtin.Word // holds type 'T'
4380+
%token2 = rebind_memory %0 : $Builtin.RawPointer, %token1 : $Builtin.Word // holds type 'U'
4381+
43714382

43724383
rebind_memory
43734384
`````````````
@@ -4386,8 +4397,8 @@ This instruction's semantics are identical to ``bind_memory``, except
43864397
that the types to which memory will be bound, and the extent of the
43874398
memory region is unknown at compile time. Instead, the bound-types are
43884399
represented by a token that was produced by a prior memory binding
4389-
operation. ``%in_token`` must be the result of bind_memory or
4390-
rebind_memory.
4400+
operation. ``%in_token`` must be the result of ``bind_memory`` or
4401+
``rebind_memory``.
43914402

43924403
begin_access
43934404
````````````

include/swift/SIL/SILInstruction.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5048,7 +5048,14 @@ class ExplicitCopyAddrInst
50485048
/// to hold %1 values.
50495049
///
50505050
/// %token is an opaque word representing the previously bound types of this
5051-
/// memory region, before binding it to a contiguous region of type $T.
5051+
/// memory region, before binding it to a contiguous region of type $T. This
5052+
/// token has no purpose unless it is consumed be a rebind_memory instruction.
5053+
///
5054+
/// Semantics: changes the type information assocated with a memory region. This
5055+
/// affects all memory operations that alias with the given region of memory,
5056+
/// regardless of their type or address provenance. For optimizations that query
5057+
/// side effects, this is equivalent to writing and immediately reading an
5058+
/// unknown value to memory at `%0` of `%1` bytes.
50525059
class BindMemoryInst final : public InstructionBaseWithTrailingOperands<
50535060
SILInstructionKind::BindMemoryInst,
50545061
BindMemoryInst, SingleValueInstruction> {
@@ -5096,6 +5103,9 @@ class BindMemoryInst final : public InstructionBaseWithTrailingOperands<
50965103
///
50975104
/// %out_token represents the previously bound types of this memory region,
50985105
/// before binding it to %in_token.
5106+
///
5107+
/// This has the same semantics as bind_memory except that the size of memory
5108+
/// affected must be derived from `%in_token`.
50995109
class RebindMemoryInst final : public SingleValueInstruction {
51005110
FixedOperandList<2> Operands;
51015111

0 commit comments

Comments
 (0)