Commit Graph

256 Commits

Author SHA1 Message Date
Kuba (Brecka) Mracek
d6bc48acde Merge pull request #39313 from kubamracek/conditional
Implement conditional stripping of type descriptors, protocols and protocol conformances via !llvm.used.conditional
2021-10-01 09:18:41 -07:00
Konrad `ktoso` Malawski
6834f61980 [Distributed] Emit dist thunk symbol in TBD 2021-10-01 11:11:03 +09:00
Kuba Mracek
d0e05003f5 For individual runtime records for types/protocols/conformances, add and use new mangling suffixes 2021-09-29 13:14:58 -07:00
Steven Wu
5dae683933 [swift-api-extract] Handle @_spi interfaces with correct APIAccess
rdar://83506338
2021-09-24 12:40:52 -07:00
Nate Chandler
2c7fb70530 [IRGen] Give decl ref to LinkEntity::forAFP.
Previously, a LinkEntity for an AST async function pointer was built by
passing an AbstractFunctionDecl.  Later, decl was used to construct a
SILDeclRef.

That arrangement meant that clients could not construct such a
LinkEntity whose SILDeclRef::Kind could not be inferred from the dynamic
type of the decl from which the SILDeclRef was constructed.  In
particular, clients could not construct a LinkEntity for the initializer
corresponding to a ConstructorDecl.

Here, the arrangment is changed so that the LinkEntity for an AST async
function pointer is built by passing a SILDeclRef.
2021-08-17 18:23:58 -07:00
Robert Widmann
f6b2294d5a Undo Force Load + Incremental Ban on Darwin Platforms
Gather 'round to hear tell of the saga of autolinking in incremental
mode.

In the beginning, there was Swift code, and there was Objective-C code.
To make one import bind two languages, a twinned Swift module named the same as an
Objective-C module could be imported as an overlay. But all was not
well, for an overlay could be created which had no Swift content, yet
required Swift symbols. And there was much wailing and gnashing of teeth
as loaders everywhere disregarded loading these content-less Swift
libraries.

So, a solution was found - a magical symbol _swift_FORCE_LOAD_$_<MODULE>
that forced the loaders to heed the dependency on a Swift library
regardless of its content. It was a constant with common linkage, and it
was good. But, along came COFF which needed to support autolinking but
had no support for such matters. It did, however, have support for
COMDAT sections into which we placed the symbol. Immediately, a darkness
fell across the land as the windows linker loudly proclaimed it had
discovered a contradiction: "_swift_FORCE_LOAD_$_<MODULE> cannot be
a constant!", it said, gratingly, "for this value requires rebasing."
Undeterred, we switched to a function instead, and the windows linker
happily added a level of indirection to its symbol resolution procedure
and all was right with the world.

But this definition was not all right. In order to support multiple
translation units emitting it, and to prevent the linker from dead
stripping it, Weak ODR linkage was used. Weak ODR linkage has the nasty
side effect of pessimizing load times since the dynamic linker must
assume that loading a later library could produce a more definitive
definition for the symbol.

A compromise was drawn up: To keep load times low, external linkage was
used. To keep the linker from complaining about multiple strong
definitions for the same symbol, the first translation unit in the
module was nominated to recieve the magic symbol. But one final problem
remained:

Incremental builds allow for files to be added or removed during the
build procedure. The placement of the symbol was therefore dependent
entirely upon the order of files passed at the command line. This was no
good, so a decree was set forth that using -autolink-force-load and
-incremental together was a criminal offense.

So we must compromise once more: Return to a symbol with common linkage,
but only on Mach-O targets. Preserve the existing COMDAT-friendly
approach everywhere else.

This concludes our tale.

rdar://77803299
2021-05-24 15:53:13 -07:00
Nate Chandler
d765434e2b [IRGen] Restored non-constant async function calls in PAFs.
Previously, because partial apply forwarders for async functions were
not themselves fully-fledged async functions, they were not able to
handle dynamic functions.  Specifically, the reason was that it was not
possible to produce an async function pointer for the partial apply
forwarder because the size to be used was not knowable.

Thanks to https://github.com/apple/swift/pull/36700, that cause has been
eliminated.  With it, partial apply forwarders are fully-fledged async
functions and in particular have their own async function pointers.
Consequently, it is again possible for these partial apply forwarders to
handle non-constant function pointers.

Here, that behavior is restored, by way of reverting part of
ee63777332 while preserving the ABI it
introduced.

rdar://76122027
2021-04-06 15:51:32 -07:00
Nate Chandler
3b6c070c06 [IRGen] Added mechanism to refer to known AFPs. 2021-04-05 18:08:33 -07:00
Nate Chandler
ee63777332 [IRGen] Fix ABI for thick async functions.
Previously, thick async functions were represented sometimes as a pair
of (AsyncFunctionPointer, nullptr)--when the thick function was produced
via a thin_to_thick_function, e.g.--and sometimes as a pair of
(FunctionPointer, ThickContext)--when the thick function was produced by
a partial_apply--with the size stored in the slot of the ThickContext.

That optimized for the wrong case: partial applies of dynamic async
functions; in that case, there is no appropriate AsyncFunctionPointer to
form when lowering the partial_apply instruction.  The far more common
case is to know exactly which function is being partially applied.  In
that case, we can form the appropriate AsyncFunctionPointer.

Furthermore, the previous representation made calling a thick function
more complex: it was always necessary to check whether the context was
in fact null and then proceed along two different paths depending.

Here, that behavior is corrected by creating a thunk in a mandatory
IRGen SIL pass in the case that the function that is being partially
applied is dynamic.  That new thunk is then partially applied in place
of the original partial_apply of the dynamic function.
2021-03-15 13:37:40 -07:00
Nate Chandler
bf728e74d1 [IRGen] Added LinkEntity for AFP to PAF.
The new AFP entity follows the pattern set by previous AFP entities.
2021-03-15 13:36:56 -07:00
Nate Chandler
218b9c029b [IRGen] Added LinkEntity for partial apply forwarder.
Up to now, there had been no need to define a LinkEntity for a partial
apply forwarder.  Now that async partial apply forwarders will each have
their own async function pointer, an entity is needed to pass to the
code that generates the async function pointers.

No demangling or remangling changes are required because that code has
existed for as long as partial apply forwarders to support demangling
their symbols.
2021-03-15 13:36:56 -07:00
Slava Pestov
53e06d69b5 AST: Factor out a new Requirement::getProtocolDecl() utility method 2021-02-25 17:21:18 -05:00
Richard Wei
e494df2ee6 [AutoDiff] Add differentiability kind to differentiability witnesses and mangle them.
Differentiability witnesses are now keyed by the original function name, the differentiability kind, and the autodiff config.

Updated SIL syntax:
```
differentiability-kind ::= 'forward' | 'reverse' | 'normal' | 'linear'
sil-differentiability-witness ::=
    'sil_differentiability_witness'
    sil-linkage?
    '[' differentiability-kind ']'
    '[' 'parameters' sil-differentiability-witness-function-index-list ']'
    '[' 'results' sil-differentiability-witness-function-index-list ']'
    generic-parameter-clause?
    sil-function-name ':' sil-type
    sil-differentiability-witness-body?
sil-instruction ::=
    'differentiability_witness_function'
    '[' sil-differentiability-witness-function-kind ']'
    '[' differentiability-kind ']'
    '[' 'parameters' sil-differentiability-witness-function-index-list ']'
    '[' 'results' sil-differentiability-witness-function-index-list ']'
    generic-parameter-clause?
    sil-function-name ':' sil-type
```
```console
sil_differentiability_witness [reverse] [parameters 0 1] [results 0] <T where T: Differentiable> @foo : <T> $(T) -> T
differentiability_witness_function [vjp] [reverse] [parameters 0] [results 0] <T where T: Differentiable> @foo : $(T) -> T
```

New mangling:
```swift
  global ::= global generic-signature? 'WJ' DIFFERENTIABILITY-KIND INDEX-SUBSET 'p' INDEX-SUBSET 'r' // differentiability witness
```
```console
$s13test_mangling3fooyS2f_S2ftFWJrSpSr ---> reverse differentiability witness for test_mangling.foo(Swift.Float, Swift.Float, Swift.Float) -> Swift.Float with respect to parameters {0} and results {0}
```

Resolves rdar://74380324.
2021-02-17 18:27:42 -05:00
Richard Wei
2f883a3599 [AutoDiff] Fix TBDGen issues for derivative dispatch thunks and method descriptors.
- Add `DispatchThunkDerivative` and `MethodDescriptorDerivative` as link entities. The derivative functions of initializers, subscripts, properties, and methods are **all methods**, so we don't need other link entities for this purpose.
- Mangle dispatch thunks and method descriptors. Make `AutoDiffFunction` a context node since it can be nested.

Resolves SR-13866 (rdar://71318828) and SR-13125 (rdar://65240599).
2021-01-30 16:47:00 -08:00
Slava Pestov
c79de40225 IRGen: Emit async function pointers for resilient class and protocol dispatch thunks
This adds new kinds of link entities corresponding to the three
dispatch thunk link entity kinds:

- DispatchThunkAsyncFunctionPointer
- DispatchThunkInitializerAsyncFunctionPointer
- DispatchThunkAllocatorAsyncFunctionPointer
2021-01-27 15:21:22 -05:00
Slava Pestov
8440a8226f IRGen: More refactoring in preparation for emitting async function pointers for dispatch thunks 2021-01-27 13:50:34 -05:00
Nate Chandler
db8183d7a1 [IRGen] Added LinkEntities for AsyncFunctionPointer.
Two LinkEntities are needed to enable the construction during both IRGen
and TBDGen.
2020-11-12 18:20:09 -08:00
Nate Chandler
a68fcb155b [prespecialized metadata] Add token for caching.
Emit a once token when adding canonical prespecialized metadata records
to a nominal type descriptor and add the token itself as a trailing
object to the type descriptor.  The new token will, in subsequent
commits, enable the canonical prespecialized metadata records attached
to the type descriptor to be added to the metadata cache exactly once.
2020-11-01 13:34:22 -08:00
Saleem Abdulrasool
fc164ce098 IRGen: be less aggressive about applying COMDAT
COMDAT can only be applied to definitions, not declarations.  This
manifested in builds of llbuild with SwiftPM on Windows.  The nominal
type descriptor accessor declaration was marked as COMDAT.
2020-08-13 10:42:03 -07:00
Joe Groff
d2bff927ef IRGen: Don't emit method descriptors for derivative functions.
The symbol mangling for the descriptors doesn't incorporate the derivative information, leading to symbol collisions.
2020-08-03 16:12:05 -07:00
Nate Chandler
d4b82bacca [metadata prespecialization] Cross-module: enums and structs.
When a generic type from a different module is not resilient within the
current module and at least one of its arguments is from the current
module, emit a non-canonical prespecialized record, and access that
metadata via a call to swift_getCanonicalSpecializedMetadata, passing in
the non-canonical record.

rdar://problem/56996727
rdar://problem/56997022
2020-07-16 14:14:01 -07:00
Nate Chandler
bdef1cef23 [metadata prespecialization] Support for classes.
When generic metadata for a class is requested in the same module where
the class is defined, rather than a call to the generic metadata
accessor or to a variant of typeForMangledNode, a call to a new
accessor--a canonical specialized generic metadata accessor--is emitted.
The new function is defined schematically as follows:

    MetadataResponse `canonical specialized metadata accessor for C<K>`(MetadataRequest request) {
      (void)`canonical specialized metadata accessor for superclass(C<K>)`(::Complete)
      (void)`canonical specialized metadata accessor for generic_argument_class(C<K>, 1)`(::Complete)
      ...
      (void)`canonical specialized metadata accessor for generic_argument_class(C<K>, count)`(::Complete)
      auto *metadata = objc_opt_self(`canonical specialized metadata for C<K>`);
      return {metadata, MetadataState::Complete};
    }

where generic_argument_class(C<K>, N) denotes the Nth generic argument
which is both (1) itself a specialized generic type and is also (2) a
class.  These calls to the specialized metadata accessors for these
related types ensure that all generic class types are registered with
the Objective-C runtime.

To enable these new canonical specialized generic metadata accessors,
metadata for generic classes is prespecialized as needed. So are the
metaclasses and the corresponding rodata.

Previously, the lazy objc naming hook was registered during process
execution when the first generic class metadata was instantiated. Since
that instantiation may occur "before process launch" (i.e. if the
generic metadata is prespecialized), the lazy naming hook is now
installed at process launch.
2020-05-29 13:20:33 -07:00
David Ungar
9dcd2b52b2 Allocate LLVMContext in IRGenModule. 2020-04-16 11:57:44 -07:00
Slava Pestov
9ec80df97e SIL: Remove curried SILDeclRefs 2020-03-19 02:20:21 -04:00
Saleem Abdulrasool
29c00f6969 IRGen: improve DLLStorage computation for Windows
This slightly regresses the standard library build (intentionally) while
generally improving the build of dispatch, foundation, xctest.

Rather than continuing to rely on the short-term hack of special casing
the standard library, identify the module where the decl originates
from.  If the module is the current module, then assume that the symbol
need not be imported (static linking does not currently work on Windows
anyways).  This allows for properly identifying the module where the
symbol will be homed.

Because we no longer special case the standard library here, a few known
metadata types will be incorrectly marked as being imported rather than
local.

Since linked entities which have `Shared` SILLinkage should be module
local, special case them to always be local.  Without this the metadata
access function is still marked incorrectly.

With this computation we now get nearly all the cases correct.  Dispatch
no longer has to rely on the linker relaxing the import to a local
binding.  XCTest is also clean.  Foundation misses the following case:
- `$sSS10FoundationE19_bridgeToObjectiveCAA8NSStringCyF`

The regressed cases in swiftCore are:
- `$sBi64_WV`
- `$sBi8_WV`
- `$sBi16_WV`
- `$sBi32_WV`
- `$sBpWV`
- `$syycWV`
- `$sBoWV`
- `$sBOWV`
- `$sBbWV`
- `$sytWV`
2020-03-08 19:24:29 -07:00
Kuba Mracek
84c4864911 [arm64e] Add Swift compiler support for arm64e pointer authentication 2020-02-27 16:10:31 -08:00
Hamish Knight
4867c5fc93 NFC: Remove unused member from UniversalLinkageInfo 2020-02-13 20:44:38 -08:00
Slava Pestov
86175996d2 IRGen: Remove LinkEntity::isAvailableExternally() 2020-02-11 18:59:21 -05:00
Slava Pestov
ff283d5c4f IRGen: Remove LinkEntity::isNominalTypeDescriptor() 2020-02-11 18:59:21 -05:00
Slava Pestov
e6dc10accd IRGen: More accurate getAddrOfLLVMVariableOrGOTEquivalent()
The logic here used to consist of a couple of ad-hoc checks,
followed by a general assumption that if something had already
been emitted, it could be referenced directly, whereas everything
else had to go through a GOT entry.

This is way too conservative. Instead, let's try to correctly
calculate what translation unit an entity is going to end up in.
2020-02-11 18:59:21 -05:00
Slava Pestov
8bdabe81ad IRGen: Add LinkEntity::getDeclContextForEmission()
This replaces getSourceFileForEmission().
2020-02-11 18:59:21 -05:00
Dan Zheng
a174243159 [AutoDiff upstream] Add SIL differentiability witness IRGen. (#29704)
SIL differentiability witnesses are a new top-level SIL construct mapping
an "original" SIL function and derivative configuration to derivative SIL
functions.

This patch adds `SILDifferentiabilityWitness` IRGen.

`SILDifferentiabilityWitness` has a fixed `{ i8*, i8* }` layout:
JVP and VJP derivative function pointers.

Resolves TF-1146.
2020-02-07 14:10:34 -08:00
Robert Widmann
eb61931012 [NFC] Grab the private discriminator for a file in IRGenDebugInfo
This was being done at an odd point in the frontend presumably because by that point the private discriminator had been fully computed.  Instead, push the conditions for generating the prefix data down to debug info generation and stop mutating IRGenOptions::DebugFlag in the frontend.
2020-01-13 22:01:41 -08:00
Nate Chandler
a9f16c7757 [gardening] Fixed typo. 2020-01-09 17:25:30 -08:00
Harlan Haskins
ccd534d139 [TBD] Ensure swift-abi-version is kept in sync with IRGen (#27347)
The TAPI_SWIFT_ABI_VERSION macro was never updated in sync with
IRGen::swiftVersion, so just expose that value through
irgen::getSwiftABIVersion() and use it in TBDGen.

Fixes rdar://55643763
2019-09-24 18:48:29 -07:00
Slava Pestov
a0e160e215 AST: Refactor Decl::isWeakImported() API a little bit
First, remove the AvailabilityContext parameter; it was confusing because
we actually always want to use the deployment target here.

Then, split this method up into three methods:

- isAlwaysWeakImported(): simply checks for a @_weakLinked attribute, either
  on the declaration itself or one of its parent contexts.

- getAvailabilityForLinkage(): returns the OS version availability when
  this declaration was introduced, or if the declaration does not have
  explicit availability, check it's storage (if its an accessor), or its
  parent contexts.

- isWeakImported(ModuleDecl *fromModule): combines these two checks to
  determine if the declaration should be weak linked when referenced from
  the given module, or if it might be weak referenced from some module
  (if the module parameter is null).
2019-09-06 20:08:40 -04:00
Joe Groff
f0e5e1911d IRGen: Access concrete type metadata by mangled name.
When we generate code that asks for complete metadata for a fully concrete specific type that
doesn't have trivial metadata access, like `(Int, String)` or `[String: [Any]]`,
generate a cache variable that points to a mangled name, and use a common accessor function
that turns that cache variable into a pointer to the instantiated metadata. This saves a bunch
of code size, and should have minimal runtime impact, since the demangling of any string only
has to happen once.

This mostly just works, though it exposed a couple of issues:

- Mangling a type ref including objc protocols didn't cause the objc protocol record to get
  instantiated. Fixed as part of this patch.
- The runtime type demangler doesn't correctly handle retroactive conformances. If there are
  multiple retroactive conformances in a process at runtime, then even though the mangled string
  refers to a specific conformance, the runtime still just picks one without listening to the
  mangler. This is left to fix later, rdar://problem/53828345.

There is some more follow-up work that we can do to further improve the gains:

- We could improve the runtime-provided entry points, adding versions that don't require size
  to be cached, and which can handle arbitrary metadata requests. This would allow for mangled
  names to also be used for incomplete metadata accesses and improve code size of some generic
  type accessors. However, we'd only be able to take advantage of the new entry points in
  OSes that ship a new runtime.
- We could choose to always symbolic reference all type references, which would generally reduce
  the size of mangled strings, as well as make runtime demangling more efficient, since it wouldn't
  need to hit the runtime caches. This would however require that we be able to handle symbolic
  references across files in the MetadataReader in order to avoid regressing remote mirror
  functionality.
2019-08-02 14:28:53 -07:00
Slava Pestov
2d18ba2ed5 IRGen: Simplify a bit of code with ProtocolDecl::getAssociatedTypeMembers() 2019-06-19 22:12:55 -04:00
Slava Pestov
c947eda18f AST: Use ProtocolDecl::getAssociatedTypeMembers() where possible 2019-05-28 22:08:31 -04:00
Joe Groff
a6b5a41a32 IRGen: Generate anonymous contexts for properties with opaque return types.
They aren't normally decl contexts, but if one has an opaque type, we want to be able to record
the property as a context so that we can reconstruct it in RemoteAST.
2019-05-02 17:28:08 -07:00
Bob Wilson
f33b990446 Merge pull request #24242 from bob-wilson/rdar50110036
References to _swift_FORCE_LOAD* symbols should be weak
2019-04-24 17:05:17 -07:00
Bob Wilson
dd94a9f90f References to _swift_FORCE_LOAD* symbols should be weak
When Swift always copied the overlay dylibs into app bundles, it was OK
for these symbol references to be non-weak, but with the overlays now
part of the OS on Apple platforms, we need to handle backward deployment
scenarios where a new overlay does not exist on an old OS version.
A weak reference will serve to pull in the overlay dylib if it exists,
without causing a fatal error if it does not. rdar://problem/50110036
2019-04-24 09:45:07 -07:00
Arnold Schwaighofer
7d0bcdac2e Add comments 2019-04-22 11:21:54 -07:00
Arnold Schwaighofer
84c7b77d02 Make opaque type descriptors dynamically replaceable
This is to support dynamic function replacement of functions with opaque
result type.

This approach requires that all state is thrown away (that could contain the
old returned type for an opaque type) between replacements.

rdar://48887938
2019-04-22 07:31:07 -07:00
Joe Groff
42e1824a30 Mangle opaque result types. 2019-04-17 14:43:32 -07:00
Slava Pestov
dd80f588dd IRGen: Emit foreign type metadata using the lazy metadata mechanism
Instead of a wholly separate lazyness mechanism for foreign metadata where
the first call to getAddrOfForeignTypeMetadataCandidate() would emit the
metadata, emit it using the lazy metadata mechanism.

This eliminates some code duplication. It also ensures that foreign
metadata is only emitted once per SIL module, and not once per LLVM
module, avoiding duplicate copies that must be ODR'd away in multi-threaded
mode.

This fixes the test case from <rdar://problem/49710077>.
2019-04-12 01:46:23 -04:00
adrian-prantl
e622ea6980 Merge pull request #23465 from adrian-prantl/48018240-irgen
Add a -public-linkage-for-metadata-accessors IRGen flag for LLDB.
2019-03-27 12:13:41 -07:00
Adrian Prantl
e1f92cc39e Add a -force-public-linkage IRGen flag for LLDB.
In LLDB expressions, references to private metadata accessors may be
emitted and need to be bound to symbols available in the attached
program, even if these symbols are only supposed to have private
visibility within the program.

Also rdar://problem/48018240
2019-03-27 09:10:23 -07:00
Slava Pestov
4207738e17 Add mangling and IRGen LinkEntity for Objective-C resilient class stubs 2019-03-26 18:44:44 -04:00
Arnold Schwaighofer
8d9b9f328b SILGen: Fix the logic of dynamic replacements for class constructors
To correctly call designated super class initializers the designated
intializer (and not the allocator) is dynamically replaceable.
Convenience allocators are dynamically replaceable as before.
2019-02-22 10:15:06 -08:00