Skip to content

Commit b39cebd

Browse files
committed
[docs] LibraryEvolution: @always_emit_into_client
Describe a new attribute, @always_emit_into_client, which covers the behavior of helpers for inlineable functions: any client that makes use of the body of an inlineable function should use that implementation, including any helper functions, local functions, or closures referenced therein. This is also a tool a library developer can use. This is a nice change because it formalizes several things that were previously special-cased; however, there are still some open questions. It's not totally clear whether we want separate @inlineable and @always_emit_into_client annotations; we could attempt to get away with just the latter. However, as noted in the diff, there are some entities that can't really be completely eliminated from the ABI, so marking them @always_emit_into_client would be disingenuous.
1 parent d4f704b commit b39cebd

File tree

1 file changed

+115
-49
lines changed

1 file changed

+115
-49
lines changed

docs/LibraryEvolution.rst

Lines changed: 115 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ published should not limit its evolution in the future.
4848

4949
- Drew Crawford is concerned about inlineable code breaking modularity;
5050
you can't just release a new dylib to fix a bug.
51-
- Joe Groff has brought up always-emitted-into-the-client code, which has
52-
fewer restrictions and different behavior than may-inline code.
5351
- David Owens wants it to be clearer that this document doesn't really
5452
cover behavior changes and proper semantic versioning, just binary
5553
compatibility.
@@ -322,7 +320,6 @@ available to clients as part of the module's public interface. ``@inlineable``
322320
is a `versioned attribute`; clients may not assume that the body of the
323321
function is suitable when deploying against older versions of the library.
324322

325-
326323
Clients are not required to inline a function marked ``@inlineable``.
327324

328325
.. note::
@@ -369,11 +366,13 @@ and the ``x`` and ``y`` properties have now disappeared. To avoid this, the bodi
369366

370367
- They may not define any local types (other than typealiases).
371368

372-
- They must not reference any ``private`` entities, except for local functions
373-
declared within the inlineable function itself.
369+
- They must not reference any ``private`` entities, except for those marked
370+
``@always_emit_into_client`` (see below). This includes local functions
371+
defined within the inlineable function.
374372

375373
- They must not reference any ``internal`` entities except for those that have
376-
been `versioned`_. See below for a discussion of versioning internal API.
374+
been `versioned`_ and those declared ``@always_emit_into_client``. See below
375+
for a discussion of versioning internal API.
377376

378377
- They must not reference any entities from the current module introduced
379378
after the function was made inlineable.
@@ -386,29 +385,85 @@ as the current body makes sense when deploying against an earlier version of
386385
the library.
387386

388387

389-
Local Functions
390-
---------------
388+
``@always_emit_into_client``
389+
----------------------------
390+
391+
The normal ``@inlineable`` attribute states that a function *may* be inlined
392+
into a client binary. There are a few cases where it is worth *guaranteeing*
393+
that the function is emitted into the client:
394+
395+
- The function is used to determine which version of the library a client was
396+
compiled against.
397+
398+
- The function is a helper for an ``@inlineable`` function, but should not be
399+
part of the library's ABI.
400+
401+
This is handled by the ``@always_emit_into_client`` attribute. If one of these
402+
functions is referenced by a client module, its implementation is always copied
403+
into the client module. ``@always_emit_into_client`` functions are subject to
404+
the same restrictions as regular ``@inlineable`` functions, as described above.
405+
The description "inlineable" collectively refers to declarations marked with
406+
``@inlineable`` and declarations marked with ``@always_emit_into_client``.
407+
408+
.. note::
409+
410+
This is represented by a ``shared`` function in SIL.
411+
412+
.. admonition:: TODO
413+
414+
All of these names are provisional.
415+
416+
Any local functions or closures within an function marked ``@inlineable`` or
417+
``@always_emit_into_client`` are themselves treated as
418+
``@always_emit_into_client``. This is important in case it is necessary to
419+
change the inlineable function later; existing clients should not be depending
420+
on internal details of the previous implementation.
421+
422+
``@always_emit_into_client`` is a `versioned attribute`; clients may not assume
423+
that the body of the function is suitable when deploying against older versions
424+
of the library. Local functions and closures implicitly have the same
425+
availability as the enclosing function's ``@always_emit_into_client`` or
426+
``@inlineable`` attribute. An existing ``@inlineable`` function may not be
427+
changed to an ``@always_emit_into_client`` function or vice versa.
391428

392-
If an inlineable function contains local functions or closures, these are
393-
implicitly made inlineable as well. This is important in case the library
394-
author decides to change the inlineable function later. If the inlineable
395-
function is emitted into a client module as described above, the local
396-
functions must be as well. (At the SIL level, these local functions are
397-
considered to have ``shared`` linkage.)
429+
.. note::
430+
431+
If the ``@always_emit_into_client`` attribute is added to a function that
432+
was available in a previous version of the library, clearly existing
433+
clients will still depend on the presence of that function in the library's
434+
ABI. However, allowing ``@always_emit_into_client`` to be converted into
435+
``@inlineable`` in *some* cases but not *all* makes for a more confusing
436+
model with very little gain.
437+
438+
Although they are not a supported feature for arbitrary libraries at this time,
439+
`transparent`_ functions are implicitly marked ``@always_emit_into_client``.
440+
(Note that ``@_transparent`` is *not* a versioned attribute and cannot be added
441+
to a function after the fact.)
442+
443+
.. _transparent: https://github.com/apple/swift/blob/master/docs/TransparentAttr.rst
444+
445+
.. note::
446+
447+
Why have both ``@inlineable`` and ``@always_emit_into_client``? Because for
448+
a larger function, like ``MutableCollectionType.sort``, it may be useful to
449+
provide the body to clients for analysis, but not duplicate code when not
450+
necessary.
398451

399-
Local functions are subject to the same restrictions as the inlineable
400-
functions containing them, as described above.
452+
.. admonition:: TODO
453+
454+
What does it mean for an ``@always_emit_into_client`` declaration to
455+
satisfy a protocol requirement?
401456

402457

403458
Default Argument Expressions
404459
----------------------------
405460

406-
Default argument expressions are subject to the same restrictions as inlineable
407-
functions, because they are implemented as inlineable functions. However,
408-
default arguments are *always* inlined into their callers and cannot be
409-
referenced as first-class functions, so they do not need to be versioned. (More
410-
explicitly, a default argument implicitly has the same availability as the
411-
function it is attached to.)
461+
Default argument expressions are implemented as ``@always_emit_into_client``
462+
functions and thus are subject to the same restrictions as inlineable
463+
functions. However, default arguments are *always* inlined into their callers
464+
and cannot be referenced as first-class functions, so they do not need to be
465+
versioned. (More explicitly, a default argument implicitly has the same
466+
availability as the function it is attached to.)
412467

413468
.. note::
414469

@@ -494,8 +549,9 @@ clients to access them more efficiently. This restricts changes a fair amount:
494549
inlineable while still allowing the setter to change. This would need
495550
syntax, though.
496551

497-
Any inlineable accessors must follow the rules for `inlineable functions`_,
498-
as described above.
552+
Any inlineable accessors must follow the rules for `inlineable functions`_, as
553+
described above. Top-level computed variables may be marked
554+
``@always_emit_into_client``.
499555

500556
Note that if a constant's initial value expression has any observable side
501557
effects, including the allocation of class instances, it must not be treated
@@ -546,10 +602,11 @@ Methods and Initializers
546602

547603
For the most part struct methods and initializers are treated exactly like
548604
top-level functions. They permit all of the same modifications and can also be
549-
marked ``@inlineable``, with the same restrictions. Inlineable initializers
550-
must always delegate to another initializer, since new properties may be added
551-
between new releases. For the same reason, initializers declared outside of the
552-
struct's module must always delegate to another initializer.
605+
marked ``@inlineable`` or ``@always_emit_into_client``, with the same
606+
restrictions. Inlineable initializers must always delegate to another
607+
initializer, since new properties may be added between new releases. For the
608+
same reason, initializers declared outside of the struct's module must always
609+
delegate to another initializer.
553610

554611

555612
Properties
@@ -559,10 +616,10 @@ Struct properties behave largely the same as top-level bindings. They permit
559616
all of the same modifications, and also allow adding or removing an initial
560617
value entirely.
561618

562-
Struct properties can also be marked ``@inlineable``, with the same
563-
restrictions as for top-level bindings. An inlineable stored property may not
564-
become computed, but the offset of its storage within the struct is not
565-
necessarily fixed.
619+
Struct properties can also be marked ``@inlineable`` or
620+
``@always_emit_into_client``, with the same restrictions as for top-level
621+
bindings. An inlineable stored property may not become computed, but the offset
622+
of its storage within the struct is not necessarily fixed.
566623

567624
.. note::
568625

@@ -571,6 +628,8 @@ necessarily fixed.
571628
be fixed. This would have to be balanced against other goals for struct
572629
layout.
573630

631+
Only computed properties may be marked ``@always_emit_into_client``.
632+
574633
Like top-level constants, it is *not* safe to change a ``let`` property into a
575634
variable or vice versa. Properties declared with ``let`` are assumed not to
576635
change for the entire lifetime of the program once they have been initialized.
@@ -586,8 +645,8 @@ stored subscripts. This means that the following changes are permitted:
586645
- Adding or removing a non-public, non-versioned setter.
587646
- Changing the body of an accessor.
588647

589-
Like properties, subscripts can be marked ``@inlineable``, which restricts the
590-
set of changes:
648+
Like properties, subscripts can be marked ``@inlineable`` or
649+
``@always_emit_into_client``, which restricts the set of changes:
591650

592651
- Adding a versioned setter is still permitted.
593652
- Adding or removing a non-public, non-versioned setter is still permitted.
@@ -684,8 +743,9 @@ still be modified in limited ways:
684743
- A versioned ``internal`` property may be made ``public`` (without changing
685744
its version).
686745

687-
An initializer of a fixed-contents struct may be declared ``@inlineable`` even
688-
if it does not delegate to another initializer, as long as the ``@inlineable``
746+
An initializer of a fixed-contents struct may be declared ``@inlineable`` or
747+
``@always_emit_into_client`` even if it does not delegate to another
748+
initializer, as long as the ``@inlineable`` or ``@always_emit_into_client``
689749
attribute is not introduced earlier than the ``@fixed_contents`` attribute and
690750
the struct has no non-versioned stored properties.
691751

@@ -764,9 +824,10 @@ Initializers
764824

765825
For the most part enum initializers are treated exactly like top-level
766826
functions. They permit all of the same modifications and can also be marked
767-
``@inlineable``, with the same restrictions. Unlike struct initializers, enum
768-
initializers do not always need to delegate to another initializer, even if
769-
they are ``@inlineable`` or declared in a separate module.
827+
``@inlineable`` or ``@always_emit_into_client``, with the same restrictions.
828+
Unlike struct initializers, enum initializers do not always need to delegate to
829+
another initializer, even if they are inlineable or declared in a separate
830+
module.
770831

771832

772833
Methods and Subscripts
@@ -961,9 +1022,9 @@ convenience initializer; that initializer may only call existing ``required``
9611022
initializers. An existing initializer may not be marked ``required``.
9621023

9631024
All of the modifications permitted for top-level functions are also permitted
964-
for class initializers. Convenience initializers may be marked ``@inlineable``,
965-
with the same restrictions as top-level functions; designated initializers may
966-
not.
1025+
for class initializers. Convenience initializers may be marked ``@inlineable``
1026+
or ``@always_emit_into_client``, with the same restrictions as top-level
1027+
functions; designated initializers may not.
9671028

9681029

9691030
Methods
@@ -987,7 +1048,8 @@ top-level functions, but the potential for overrides complicates things a little
9871048

9881049
Class and instance methods may be marked ``@inlineable``, with the same
9891050
restrictions as struct methods. ``dynamic`` methods may not be marked
990-
``@inlineable``.
1051+
``@inlineable``. Only non-overriding ``final`` methods may be marked
1052+
``@always_emit_into_client``.
9911053

9921054
If an inlineable method is overridden, the overriding method does not need to
9931055
also be inlineable. Clients may only inline a method when they can devirtualize
@@ -1026,8 +1088,9 @@ Constant properties (those declared with ``let``) still permit changing their
10261088
value, as well as adding or removing an initial value entirely.
10271089

10281090
Both variable and constant properties (on both instances and classes) may be
1029-
marked ``@inlineable``. This behaves as described for struct properties.
1030-
``dynamic`` properties may not be marked ``@inlineable``.
1091+
marked ``@inlineable``; non-overriding ``final`` computed properties may also
1092+
be marked ``@always_emit_into_client``. This behaves as described for struct
1093+
properties. ``dynamic`` properties may not be marked ``@inlineable``.
10311094

10321095
If an inlineable property is overridden, the overriding property does not need
10331096
to also be inlineable. Clients may only inline a property access when they can
@@ -1050,7 +1113,9 @@ Adding a public setter to a subscript that may be overridden is a
10501113
know what to do with the setter and will likely not behave correctly.
10511114

10521115
Class subscripts may be marked ``@inlineable``, which behaves as described for
1053-
struct subscripts. ``dynamic`` subscripts may not be marked ``@inlineable``.
1116+
struct subscripts. Non-overriding ``final`` subscripts may also be marked
1117+
``@always_emit_into_client``. ``dynamic`` subscripts may not be marked
1118+
``@inlineable``.
10541119

10551120
If an inlineable subscript is overridden, the overriding subscript does not need
10561121
to also be inlineable. Clients may only inline a subscript access when they can
@@ -1235,7 +1300,8 @@ As the sole exception, it is safe to backdate ``@inlineable`` on a top-level
12351300
function, a method, a subscript, or a struct or enum initializer. It is not
12361301
safe to backdate ``@inlineable`` for a top-level variable or constant, a
12371302
property, or a class initializer. As usual, a library author may not assume
1238-
that a client will actually inline the call.
1303+
that a client will actually inline the call. It is not safe to backdate the
1304+
``@always_emit_into_client`` attribute.
12391305

12401306
.. note::
12411307

@@ -1298,8 +1364,8 @@ outside the current module, since once it's inlined it will be.
12981364

12991365
For inlineable code, the availability context is exactly the same as the
13001366
equivalent non-inlineable code except that the assumed version of the
1301-
containing library is the version attached to the ``@inlineable`` attribute,
1302-
and any `library version dependencies
1367+
containing library is the version attached to the ``@inlineable`` or
1368+
``@always_emit_into_client`` attribute, and any `library version dependencies
13031369
<#declaring-library-version-dependencies>`_ or minimum deployment target must
13041370
be specified explicitly using ``@available``. Code within this context must be
13051371
treated as if the containing library were just a normal dependency.

0 commit comments

Comments
 (0)