Fixes the missed optimization behind #91665.
Issue
For a small String the UTF-8 bytes live inline in the String value, so String.utf8Span
has to return a pointer into that value. That makes the lifetime dependence a dependence on
the argument’s address, which is what @_addressableForDependencies on _StringGuts
expresses and what shows up in SIL as:
(@in_guaranteed String) -> @lifetime(borrow address_for_deps 0) @owned UTF8Span
In the caller this leaves a strong_release of a compile-time constant small string in the
hot loop, which is a call to swift_bridgeObjectRelease on a tagged pointer that can never
do anything.
The chain is:
walkDownCallee(EscapeUtils.swift) skips the precise “indirect arguments cannot escape” early out whenapply.isAddressable(operand:)is true, which is correct, because the address really is captured into the returned UTF8Span. It then falls through to an unboundedreturn isEscaping.AliasAnalysis.getApplyEffectsees the aborted walk and falls back to the callee’s global effects, which say “may write”.- RedundantLoadElimination therefore won’t forward the stored literal into the reload that the destroy was expanded into, so the release survives.
The escape answer in step 1 is right. The problem is that the client is asking a narrower question than the one being answered: it doesn’t need to know where the address went, only what this call can do to it.
Patch
This patch relies on the following rule: constructing a mutable address from an
@in_guaranteed argument is undefined behavior, even with unsafe pointers. So a value whose
lifetime is a scoped dependence on the address of an @in_guaranteed argument, and anything
derived from it, may be used to read that storage, never to write it. This is the same
assumption the optimizer already makes for a callee’s own @in_guaranteed arguments (see
SideEffects.GlobalEffects.restrictedTo), except that @_rawLayout types are excluded here
(see below).
It adds a hook to EscapeVisitor, visitBorrowedAddressableCapture(operand:path:), which
walkDownCallee calls for such an argument instead of falling through to isEscaping. It’s
gated on the argument being @in_guaranteed, on the apply’s direct result being the only
thing that depends on its address, and on the argument not being a non-copyable type that may
be or contain a @_rawLayout type. The default implementation returns .continueWalk, so
every existing visitor keeps today’s behaviour.
The only override is in FullApplyEffectsVisitor, and only for the address query (the
ownership query follows loads and never reaches it). It doesn’t follow the borrowing value.
Instead, it records a read if the callee can read memory at all. That covers reads through
the borrowing value by later calls, e.g.
%span = apply %utf8Span(%s) // captures %s
apply %sumSpanBytes(%span) // reads %s through %span
because %sumSpanBytes gets %span as an argument and its read is included in its own
effects. A write is impossible by the rule above. Stack promotion, the escape-to-instruction
queries, getOwnershipEffect, getPartialApplyEffect and ComputeEscapeEffects use the
default, so they are unaffected.
It’s a branch prune and not a short circuit, so the walk still visits every other use of the
address and still aborts on a genuine escape, for example an independent
address_to_pointer feeding an opaque write. And it is gated on the parameter convention
rather than on the dependence, because the dependence does not carry the distinction: a
.scope dependence can be a borrow or an inout dependence, so by the time the optimizer
sees it the convention is the only carrier left. Compare:
(@in_guaranteed String) -> @lifetime(borrow address_for_deps 0) @owned UTF8Span
(@inout Array<UInt8>) -> @lifetime(borrow 0) @owned MutableSpan<UInt8>
Without the @in_guaranteed gate this is a straightforward miscompile on the second shape,
since a MutableSpan producer’s dependent value is allowed to write. There are regression
tests for it.
Assumptions
The exception to the rule is @_rawLayout types (e.g. Atomic, or the _Cell inside
Mutex), which are mutated through the opaque Builtin.addressOfRawLayout. They are excluded
explicitly: onlyResultDependsOnAddress returns false for a non-copyable argument whose
lowered type may be or contain one, using a new SILType::isOrContainsRawLayout. It checks
containment because Mutex isn’t @_rawLayout itself, it only stores one. The query is
conservative, so ~Copyable generic and opaque-layout types are excluded too. Copyable types
skip the check, since a @_rawLayout type must be non-copyable and so can’t be contained in a
copyable type. That way copyable generic and resilient code still gets optimized.
SideEffects.GlobalEffects.restrictedTo is unchanged.
One gap I know of: opaque existentials like any P & ~Copyable don’t report raw layout in
TypeLowering (this also affects isGlobalLet). This path can only reach one when it’s passed
unopened through an @_addressable parameter, not through address_for_deps. I’d rather fix
that in TypeLowering in a separate PR.
A program that launders the address out of a borrow-derived value and writes through it
currently observes the write, and with this change it doesn’t. This means this is a
behaviour change, but only for programs that already have undefined behavior.
This also matches the semantics in https://github.com/swiftlang/swift-evolution/pull/3145, “Compile-time lifetime dependency annotations”, which states:
The dependent value is treated as an ongoing access to the source value. Following Swift’s usual exclusivity rules, the source value may not be mutated during the lifetime of the dependent value; if the access is a mutating access, the source value is further prohibited from being accessed at all during the lifetime of the dependent value.
The
borrowspecifier means that the caller enforces exclusive read-only access over the scope of the dependent value’s uses.
A
borrowdependency specifier requiresborrowingownership. It extends borrowing access, prohibiting mutations of the argument, but allowing other simultaneous borrowing accesses. A&dependency specifier requiresinoutownership.
The alternative I considered is per-API @_effects(escaping ...) clauses on the addressable
accessors, but that needs a clause for every accessor and for every opaque callee downstream
of it, each one an exhaustive promise nothing checks, and it does nothing for types outside
the standard library. That’s why I’d rather do this.
Testing
test/SILOptimizer/memory-effects_addressable_unit.silchecks the memory effects on the borrowed address: a read and no write for the capturing apply and for an apply that gets the borrowing value, no effect for areadnoneapply, and a read and a write when the dependence is on an@inoutargument. It also checks a read and a write for a@_rawLayouttype, a type containing one and a~Copyablegeneric type, and a read only for a copyable generic type.test/SILOptimizer/redundant_load_elim_addressable.silchecks that RedundantLoadElimination forwards a stored value across a borrowed capture, but not across an inout one or a borrowed capture of a type containing a@_rawLayouttype.
Numbers
On macOS the cost of the surviving release is small and additive. Measuring it by emitting .s,
deleting exactly the two instructions ldr x0, [sp, #8] / bl _swift_bridgeObjectRelease, and
assembling both, so the binaries are byte-identical apart from that pair:
6.3.3 toolchain 6.34 ns/iter
6.5-dev, release removed 6.37 ns/iter
6.5-dev as shipped 7.11 ns/iter 1.12x
The release costs ~0.75 ns/iter, constant regardless of surrounding work.
On Linux it is worth considerably more as the frameless assembly fast path in stdlib/public/SwiftDirectRuntime/RetainRelease.s is guarded by
#if __arm64__ && __LP64__ && defined(__APPLE__) && defined(__MACH__), so it is Apple only.
Originally opened as a GitHub pull request, swiftlang/swift#92538, on September 23, 2026.