Commit Graph

296 Commits

Author SHA1 Message Date
Nate Chandler
2701a0809b [metadata prespecialization] Ptrauth for compared protocol conformances.
Two protocol conformance descriptors are passed to
swift_compareProtocolConformanceDecriptors from generic metadata
accessors when there is a canonical prespecialization and one of the
generic arguments has a protocol requirement.

Previously, the descriptors were incorrectly being passed without
ptrauth processing: one from the witness table in the arguments that are
passed in to the accessor and one known statically.

Here, the descriptor in the witness table is authed using the
ProtocolConformanceDescriptor schema.  Then, both descriptors are signed
using the ProtocolConformanceDescriptorsAsArguments schema.  Finally, in
the runtime function, the descriptors are authed.
2020-07-09 11:45:16 -07:00
David Zarzycki
3952715bed [SIL] NFC: Move #include of CanTypeVisitor.h
This improves incremental rebuild performance.
2020-07-02 11:04:25 -04:00
Nate Chandler
a5f0069b8d [metadata prespecialization] Check conformances.
Previously, the metadata accessor for a generic type for which some
metadata prespecialization was done only tested that the type metadata
arguments were equal to those of the prespecialization.  If the generic
type had an argument which was constrained to conform to a protocol, the
particular conformance to that protocol was determined at compile time,
but the conformance was ignored in the metadata accessor.  As a result
it was possible--in certain multi-module cases--for the metadata
accessor to incorrectly return a prespecialized metadata record whose
type arguments matched the type arguments passed to the accessor but
whose conformance arguments did not.

For example, given the following,

  Base:
    struct K {}
    protocol P {}

  Conformance1:
    import Base
    struct G<T : P> {}
    extension K : P {} // first conformance
    prespecialize( G<K>.self )

  Conformance2:
    import Base
    extension K : P {} // second conformance

the metadata accessor for G defined in Conformance1 would behave like

  MetadataResponse `metadata accessor for G`(
      MetadataRequest request,
      const Metadata *M,
      const WitnessTable *WT) {
    if (M == `type metadata for K`) {
      return `canonical prespecialized type metadata for G<K>`
    }
    return swift_getGenericMetadata(request, {M, WT});
  }

Here, the WitnessTable argument is the witness table describing a
conformance of the type whose metadata is provided to the protocol P.

The incorrect behavior occurs when calling the metadata accessor with
these arguments:

    `some request`
    `type metadata for K`
    `protocol witness table for Base.K : Base.P in Conformance2`

The accessor would return the `canonical prespecialized type metadata
for G<K>`.  The problem is that the prespecialized metadata contains the
following generic arguments:

    `type metadata for K`
    `protocol witness table for Base.K : Base.P in Conformance1`

Specificallly, the witness table is for the conformance from
Conformance1, not the conformance from Conformance2.

Here, the problem is addressed by testing that the witness tables passed
into the accessor are for the same conformance as the witness table
referred to by the prespecialized record.  Now, the metadata accessor
for G will behave like

  MetadataResponse `metadata accessor for G`(
      MetadataRequest request,
      const Metadata *M,
      const WitnessTable *WT) {
    if (M == `type metadata for K`
        swift_compareProtocolConformanceDescriptors(
          WT->getDescription(),
          `protocol conformance descriptor for Base.K : Base.P in Conformance1`)
       ) {
      return `canonical prespecialized type metadata for G<K>`
    }
    return swift_getGenericMetadata(request, {M, WT});
  }

Consequently, when the accessor is called with the same arguments as
before, the call to swift_compareProtocolConformanceDescriptors will
return false and the non-matching prespecialized metadata will not be
returned.
2020-06-22 20:34:23 -07:00
Keith Smiley
134213852a Remove unused variables
These currently warn for being unused
2020-06-15 16:03:41 -07:00
nate-chandler
7c5a5e5041 Merge pull request #32174 from nate-chandler/generic-metadata-prespecialization-components/classes-nongeneric-superclasses
[metadata prespecialization] Support classes with non-generic ancestors.
2020-06-09 09:25:21 -07:00
nate-chandler
714e9557af Merge pull request #32242 from nate-chandler/generic-metadata-prespecialization-components/enum-not-bool
[metadata prespecialization] NFC: Replaced bool with enum.
2020-06-08 15:35:03 -07:00
Nate Chandler
8ec75d4933 [metadata prespecialization] Support classes with non-generic ancestors.
Previously, metadata prespecialization for classes only occurred when
all of a specialized generic class's ancestors were themselves generic.
Here, that requirement is lifted so that generic classes with concrete
ancestors are also eligible for prespecialization.
2020-06-08 14:57:50 -07:00
Nate Chandler
5ce546636e [metadata prespecialization] NFC: Replaced bool with enum.
Previously a bool argument was passed to
isCanonicalSpecializedNominalTypeMetadataStaticallyAddressable to
indicate whether the metadata was to be used only from a specialized
metadata accessor.  Here, that bool is replaced with an enum.
2020-06-08 10:55:16 -07:00
Arnold Schwaighofer
3f903b4891 Merge pull request #31986 from aschwaighofer/irgen_inherit_clangs_fp_elim
IRGen: Default to clang's frame pointer elimination settings
2020-06-03 07:38:24 -07:00
Varun Gandhi
c14e934563 [NFC] Remove redundant includes for llvm/ADT/SmallSet.h. 2020-05-31 13:07:45 -07:00
nate-chandler
5204af327a Merge pull request #30089 from nate-chandler/generic-metadata-prespecialization-components/classes
[metadata prespecialization] Support for classes.
2020-05-29 17:32:30 -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
Arnold Schwaighofer
b148e24186 Merge pull request #32027 from aschwaighofer/type_substitute_objc_generics
Type substitution: When substituting SILFunctionTypes we substitute u…
2020-05-28 16:33:05 -07:00
Arnold Schwaighofer
20f4ef93de IRGen: Default to clang's frame pointer elimination settings
Clang provides options to override that default value.
These options are accessible via the -Xcc flag.

Some Swift functions explicitly disable the frame pointer.

The clang options will not override those.
2020-05-28 12:21:42 -07:00
Arnold Schwaighofer
532f0cb865 Type substitution: When substituting SILFunctionTypes we substitute unbound Objective-C generic for bound ones
IRGen does so. So don't assert on this case.

rdar://63509292
2020-05-26 13:26:08 -07:00
Nate Chandler
ccf6209a6b [prespecialized metadata] Allow existential arguments. 2020-05-22 14:01:45 -07:00
Nate Chandler
63e66369e8 [metadata prespecialization] Prespecialize canonically only in-module.
Previously, prespecialization was incorrectly being performed for
non-resilient types defined by other modules.  This is incorrect for
statically canonical metadata records because in order to be canonical,
they need to be returned from the metadata accessor which is emitted by
the module which defines the type.
2020-05-21 15:49:49 -07:00
Anthony Latsis
44a92a926c [NFC] GenericSignatureImpl: Spell conformsToProtocol & getConformsTo in terms of requirements 2020-05-14 22:51:44 +03:00
Nate Chandler
6c178c7c62 [prespecialized metadata] Require same file without wmo.
Without whole module optimization, the metadata accessors are emitted on
a per-file basis.  The result is that if the file containing a generic
type is processed before the file containing a usage of that type that
would result in that prespecialization, the metadata accessor would have
already been emitted by the time that the usage is noted, making it
impossible for the newly created prespecialization to be returned from
the already-emitted metadata accessor.

Here, require that either whole module optimization is enabled so that
the metadata accessors are all emitted at once at the end, or else that
the usage of the prespecialization is in the same file as the type is
declared.
2020-04-03 15:37:36 -07:00
Dan Zheng
c1fe0e37ba [AutoDiff upstream] Add differentiable function type mangling. (#30675)
Add mangling scheme for `@differentiable` and `@differentiable(linear)` function
types. Mangling support is important for debug information, among other things.

Update docs and add tests.

Resolves TF-948.
2020-03-27 12:02:55 -07:00
Joe Groff
faec5866a4 Merge pull request #30489 from jckarter/swift-52-opaque-type-mangling
IRGen: Use mangled names to access opaque type associated types in Swift >=5.2.
2020-03-19 12:01:49 -07:00
Joe Groff
b4abd44e03 Merge pull request #30479 from jckarter/disable-mangled-name-metadata
IRGen: Add a flag to disable mangled name type metadata accessors.
2020-03-19 12:01:38 -07:00
Joe Groff
b636a07070 IRGen: Use mangled names to access opaque type associated types in Swift >=5.2. 2020-03-18 16:43:09 -07:00
Joe Groff
72b13e8c65 IRGen: Add a flag to disable mangled name type metadata accessors.
Useful as a workaround for runtime demangler bugs, or in rare cases where there are
performance problems with the demangler.
2020-03-18 13:55:01 -07:00
Fred Riss
259d78a350 Adapt to llvm.org StringRef API change 2020-03-13 19:08:22 +01:00
Kuba (Brecka) Mracek
5d918e5ee1 Merge branch 'master' into mracek/arm64e 2020-03-03 08:28:01 -08:00
Joe Groff
4abb548adb IRGen: Invoke objc_opt_self directly when available.
We don't need swift_getInitializedObjCClass on new enough Apple OSes because
the ObjC runtime provides an equivalent call for us.
2020-02-28 10:36:42 -08:00
Kuba Mracek
84c4864911 [arm64e] Add Swift compiler support for arm64e pointer authentication 2020-02-27 16:10:31 -08:00
Joe Groff
808d33d016 IRGen: Don't cache accesses to fixed class metadata.
The only initialization these class objects need is ObjC realization, which can be done
fast with `objc_opt_self` on recent Apple OSes. The cache check just adds code size and
dirties memory.
2020-02-26 15:10:18 -08:00
Joe Groff
7ec52e6329 IRGen: Separate the concept of "metadata should be cached" from "statically referenced"
Some metadata may require instantiation, but not in a way that requires us to put an additional
cache layer in front of it. `Self` metadata is also trivial to access from the local cache, but
isn't statically referenceable. Split these concepts and update code to use one or the other
appropriately. This catches an issue with metadata prespecialization where it would try to
make records for dynamic `Self` incorrectly.
2020-02-24 13:58:57 -08:00
Dan Zheng
1779632a6f [IRGen] NFC: silence llvm::MaybeAlign warnings.
Use `llvm::MaybeAlign` instead of `unsigned` to silence slew of warnings.
2020-02-20 08:49:28 +00:00
nate-chandler
b62871047d Merge pull request #29345 from nate-chandler/generic-metadata-prespecialization-components/enums
Generic metadata prespecialization: enums
2020-02-12 13:09:59 -08:00
Nate Chandler
40e17d9c6f [metadata prespecialization] Direct refs to enums.
When a specialized usage of a generic enum occurs in the same module
where the enum was defined, directly reference the prespecialized
metadata.

rdar://problem/56994321
2020-02-12 10:08:33 -08:00
Arnold Schwaighofer
90d942ca26 Merge pull request #29740 from aschwaighofer/irgen_cache_optional_metadata_construction
IRGen: Cache type metadata construction of Optional types
2020-02-12 08:28:29 -08:00
Robert Widmann
054d7b9913 Merge pull request #29713 from CodaFi/unused-unwanted-unloved
[Gardening] Silence Some Warning Spew
2020-02-11 15:46:16 -08:00
Arnold Schwaighofer
ea5fa5afd3 IRGen: Cache type metadata construction of Optional types
To achieve this replace the current implementation which recursively
constructs a layout compatible metadata by an implementation that
recursively constructs a layout compatible type and the use
emitTypeMetadataRef on that type to generate the metadata.
2020-02-11 13:32:53 -08:00
Robert Widmann
bd1dce89b8 [Gardening] Silence needless double-brace-initialization warnings
See also https://wg21.cmeerw.net/cwg/issue1270
2020-02-07 16:09:31 -08:00
swift-ci
1a6ffc8b0e Merge remote-tracking branch 'origin/master' into master-rebranch 2020-02-06 09:24:55 -08:00
Nate Chandler
2f36375a28 [metadata prespecialization] Standardize checks to ref tables.
Previously, some ad hoc checks were done in order to determine whether
the metadata access for a generic type was trivial.  Now, standard
predicates are used, specifically IRGenModule's member functions
isDependentConformance and isResilientConformance.
2020-02-04 11:59:36 -08:00
Nate Chandler
e49dd639fd [metadata prespecialization] Enumerate arguments as usual.
Previously, when emitting the metadata accessor, the generic arguments
of the type were enumerated one after the next.  That was fine in most
cases but is incorrect in cases where the actual number of generic
arguments is less than apparent number as when two arguments are required
to be equal.

Now, the arguments are enumerated according to the requirements vended by
the GenericTypeRequirements struct.
2020-01-30 11:30:02 -08:00
Erik Eckstein
1b312a85bd Merge remote-tracking branch 'origin/master' into master-rebranch 2020-01-16 10:39:20 +01:00
Slava Pestov
61872fa948 IRGen: Don't use the demangler to realize imported Objective-C class metadata for now
Recently we started using the runtime demangler to realize imported
Objective-C class metadata, to save on code size since no metadata
accessor function needs to be emitted in this case.

This introduced a regression where OBJC_CLASSREF symbols were no
longer being emitted in some cases, which breaks autolinking.
The fix for this was tracked by rdar://56136123, but unfortunately
had to be reverted because it caused other problems.

Until the original fix can be re-applied, let's put in a temporary
workaround where we avoid going through the runtime demangler for
imported Objective-C classes. This regresses code size but unblocks
everyone involved, until the fix for the demangling code path
(8247525471) can be re-applied.

Fixes <rdar://56621277>.
2020-01-14 00:44:30 -05:00
swift-ci
5dcb80b599 Merge remote-tracking branch 'origin/master' into master-rebranch 2020-01-10 15:24:32 -08:00
Nate Chandler
9e2e090623 [IRGen] Emit metadata accessors last.
Metadata accessors are dependent on prespecializations of the metadata
of generic, in-module types.  Those prespecializations are themselves
dependent on usages of the types in functions.  Consequently, the
accessors must be emitted after all the functions are emitted.
2020-01-09 17:25:34 -08:00
Nate Chandler
c4d13e4b4b [IRGen] Directly reference prespecializations.
When possible, directly reference metadata prespecializations.  Doing so
is possible when the type is defined in the same module, because in
those cases the metadata accessor can be modified to ensure that the
prespecialized metadata is canonical.

rdar://problem/56994171
2020-01-09 17:25:33 -08:00
Nate Chandler
840ded4923 [IRGen] Accessor returns prespecializations.
For every prespecialization of generic metadata that exists in the
module where the generic type is defined, the metadata accessor gains
code with the following effect

  switch arguments {
  case prespecialization1.genericArguments:
    return prespecialization1
  case prespecialization2.genericArguments:
    return prespecialization2
  ...
  default:
    return swift_getGenericMetadata(...)
  }

rdar://problem/56961700
2020-01-09 17:25:33 -08:00
Nate Chandler
9ea71d1114 [IRGen] Prepare to refer to prespecializations.
When emitting a reference to the metadata for a generic type, prepare
to, rather than always inserting calls to the type metadata access
function, emit direct references to static specializations when possible
and emit calls to the forthcoming swift_getCanonicalSpecializedMetadata
when not possible.

For now, the metadata access function is always called.
2020-01-09 17:25:32 -08:00
Joe Groff
fb34044408 Merge remote-tracking branch 'origin/master' into master-next 2019-12-10 12:46:41 -08:00
Arnold Schwaighofer
0d324d223f Add swift_getTypeByMangledNameInContextInMetadataState such that we can
use getTypeByMangledName when abstract metadata state is requested

This can significantly reduce the code size of apps constructing deeply
nested types with conditional conformances.

Requires a new runtime.

rdar://57157619
2019-11-18 14:41:35 -08:00
swift-ci
aa57f0c92b Merge remote-tracking branch 'origin/master' into master-next 2019-11-12 12:09:57 -08:00