Commit Graph

1047 Commits

Author SHA1 Message Date
Doug Gregor
d59c23fa2c Merge pull request #28869 from DougGregor/assoc-type-witness-canon
[IRGen] Canonicalize associated type witnesses.
2019-12-19 00:27:25 -08:00
Doug Gregor
4e71ce53a9 [IRGen] Canonicalize associated type witnesses.
Associated type witnesses were not getting canonicalized with respect to
their appropriate generic signatures, causing types to be emitted into
the metadata that could not be properly demangled. Be consistent about
providing a generic signature for canonicalization.

Fixes SR-11642 / rdar://problem/56466693.
2019-12-18 22:21:50 -08:00
Xi Ge
25376025ae IRGen: keep emitting protocol witness table symbols for refactored protocols
When Protocol P and Struct S are in a same module and S conforms to P, the protocol
witness table is emitted directly as a symbol. If we move P to a lower-level module,
the protocol witness table symbol isn't emitted, breaking users' existing executable as
a result.

This change checks whether the protocol used to be defined in the same
module and marks it as non-resilient if so. The compiler will continue
emitting witness table as symbols to maintain cross-module ABI stability.
2019-12-12 22:22:28 -08:00
Arnold Schwaighofer
2ea1c5cc41 Use IGM.getMaximalTypeExpansionContext() in more places 2019-11-11 14:21:52 -08:00
Arnold Schwaighofer
4cba76309f IRGen: Add TypeExpansionContext to IRGen 2019-11-11 14:21:52 -08:00
Robert Widmann
b849e51768 Use operator bool to claw back some readability 2019-10-29 16:56:21 -07:00
Robert Widmann
37e82a6133 [NFC] getWitnessMethodConformanceOrNone -> getWitnessMethodConformanceOrInvalid 2019-10-29 16:56:20 -07:00
Robert Widmann
3e1a61f425 [NFC] Fold The Tri-State In Optional<ProtocolConformanceRef>
ProtocolConformanceRef already has an invalid state.  Drop all of the
uses of Optional<ProtocolConformanceRef> and just use
ProtocolConformanceRef::forInvalid() to represent it.  Mechanically
translate all of the callers and callsites to use this new
representation.
2019-10-29 16:55:56 -07:00
Joe Groff
03c7919b4a SIL: Add fields to SILFunctionType for substituted function types.
https://forums.swift.org/t/improving-the-representation-of-polymorphic-interfaces-in-sil-with-substituted-function-types/29711

This prepares SIL to be able to more accurately preserve the calling convention of
polymorphic generic interfaces by letting the type system represent "substituted function types".
We add a couple of fields to SILFunctionType to support this:

- A substitution map, accessed by `getSubstitutions()`, which maps the generic signature
  of the function to its concrete implementation. This will allow, for instance, a protocol
  witness for a requirement of type `<Self: P> (Self, ...) -> ...` for a concrete conforming
  type `Foo` to express its type as `<Self: P> (Self, ...) -> ... for <Foo>`, preserving the relation
  to the protocol interface without relying on the pile of hacks that is the `witness_method`
  protocol.

- A bool for whether the generic signature of the function is "implied" by the substitutions.
  If true, the generic signature isn't really part of the calling convention of the function.
  This will allow closure types to distinguish a closure being passed to a generic function, like
  `<T, U> in (*T, *U) -> T for <Int, String>`, from the concrete type `(*Int, *String) -> Int`,
  which will make it easier for us to differentiate the representation of those as types, for
  instance by giving them different pointer authentication discriminators to harden arm64e
  code.

This patch is currently NFC, it just introduces the new APIs and takes a first pass at updating
code to use them. Much more work will need to be done once we start exercising these new
fields.

This does bifurcate some existing APIs:

- SILFunctionType now has two accessors to get its generic signature.
  `getSubstGenericSignature` gets the generic signature that is used to apply its
  substitution map, if any. `getInvocationGenericSignature` gets the generic signature
  used to invoke the function at apply sites. These differ if the generic signature is
  implied.
- SILParameterInfo and SILResultInfo values carry the unsubstituted types of the parameters
  and results of the function. They now have two APIs to get that type. `getInterfaceType`
  returns the unsubstituted type of the generic interface, and
  `getArgumentType`/`getReturnValueType` produce the substituted type that is used at
  apply sites.
2019-10-25 13:38:51 -07:00
Slava Pestov
b1ffa19c7b AST: Refine recursive property checks on type alias types 2019-09-24 17:39:53 -04:00
Robert Widmann
aad82fd9c5 [NFC] Remove unnecessary validation noise
Drop some callers to validateDecl and resolveDeclSignature that did not
actually need the interface type.
2019-09-21 09:56:31 -07:00
Slava Pestov
8eb97bcb6c IRGen: Validate declarations to get a valid generic signature 2019-08-09 19:08:47 -04:00
Joe Groff
8bd2319530 IRGen: Peephole metadata requests for opaque types.
If we're allowed to know at IRGen time what the underlying type of an opaque type is, we can
satisfy references to the opaque type's metadata or protocol witness tables by directly referencing
the underlying type instead.
2019-08-07 19:57:04 -07: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
Joe Groff
e12f935b9a IRGen: Use correct archetype conformance code path for opaque associated types.
The code here was not correct in a situation where an opaque type had constraints that were
refinements of the protocol requirements of an associated type, as in:

```
protocol ParentProtocol {}
protocol SubProtocol: ParentProtocol {}

protocol P {
  associatedtype A: ParentProtocol
  func foo() -> A
}

struct S: P {
  func foo() -> some SubProtocol
}
```

because it assumed that the conformance could be found directly on the opaque type instead of
potentially via an arbitrary MetadataPath. Falling through to the code that already correctly
handles archetype conformances right below the removed code does the right thing. Fixes
rdar://problem/53081207.
2019-07-19 21:30:45 -07:00
Jordan Rose
4abefdb326 [IRGen] Canonicalize symbolic ref types in the right generic context (#25955)
When referencing a superclass type from a subclass, for example, the
type uses the subclass's generic parameters, not the superclass's.
This can be important if a nested type constrains away some of its
parent type's generic parameters.

This doesn't solve all the problems around mis-referenced generic
parameters when some are constrained away, though. That might
require a runtime change. See the FIXME comments in the test cases.

rdar://problem/51627403
2019-07-08 14:40:26 -07:00
Slava Pestov
4c499fd4ac AST: Stop passing around LazyResolvers in various places 2019-07-06 00:43:22 -04:00
Slava Pestov
b9f1175ce5 IRGen: Fix isDependentConformance() to resolve type witnesses if necessary 2019-05-28 22:08:38 -04:00
Joe Groff
29a8c67b8f IRGen: Bind local metadata before emitting opaque type assoc type witnesses. rdar://problem/49585457 2019-04-17 14:46:21 -07:00
Joe Groff
34620a9be0 IRGen: Use runtime calls to get opaque type metadata and conformances for associated types. 2019-04-17 14:44:40 -07:00
Joe Groff
09ca7a784b IRGen: Emit runtime calls to fetch opaque type metadata.
Introduce code to emit runtime calls to get the type metadata for the underlying type of an opaque type and to get its conformances.
2019-04-17 14:43:32 -07:00
Joe Groff
0255baa97f SILGen: Start supporting opaque result types resiliently.
Tear out the hacks to pre-substitute opaque types before they enter the SIL type system.
Implement UnderlyingToOpaqueExpr as bitcasting the result of the underlying expression from the
underlying type to the opaque type.
2019-04-17 14:43:32 -07:00
Joe Groff
e3bbd8ce9e Remove ResilienceExpansion from substOpaqueTypes for now.
It's currently meaningless, and it'll require thought to pass the correct value when it becomes
meaningful.
2019-04-17 14:43:32 -07:00
Joe Groff
325ba600c1 IRGen: Substitute out opaque types when emitting associated types. 2019-04-17 14:43:32 -07:00
Slava Pestov
5062a81e3d AST: Start returning SelfProtocolConformances from ModuleDecl::lookupConformance()
Fixes <rdar://problem/49241923>, <https://bugs.swift.org/browse/SR-10015>.
2019-04-16 23:02:50 -04: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
Slava Pestov
6ef9a5eb40 IRGen: Don't emit lazy accessor for witness table of conformance involving DynamicSelfType
Fixes <https://bugs.swift.org/browse/SR-9295>.
2019-04-02 00:25:24 -04:00
Arnold Schwaighofer
1bf1aad938 IRGen: Recover metadata from Objective-C metadata
rdar://49263374
2019-03-28 11:39:53 -07:00
Saleem Abdulrasool
8357457764 IRGen: avoid constant conformances on Windows
Windows does not permit cross-module data accesses to be direct.  This
is a problem for public protocols with root conformances which are
external.  Use a runtime initialiser for the root protocol conformance
chaining to alleviate this issue.  This shows up in the Foundation
build.
2019-03-11 14:12:55 -07:00
Slava Pestov
88bc29a511 Merge pull request #22775 from slavapestov/resilient-witness-table-cleanup
Resilient witness table emission cleanup
2019-02-22 21:55:52 -05:00
Slava Pestov
a00896a535 IRGen: Use conformance access paths to find inherited conformances from opened existentials
Thanks to Joe for the refactoring that made this possible.
2019-02-21 00:03:15 -05:00
Slava Pestov
bdac8ad633 IRGen: Remove considerResilience parameter to isDependentConformance() 2019-02-21 00:02:25 -05:00
Slava Pestov
d20ca1f80b IRGen: Remove ConformanceDescriptor::hasDependentAssociatedTypeWitnesses
This sets the 'requires instantiation' bit in a couple more cases, which
doesn't really matter because the runtime already checks a few other
conditions, such as the presence of resilient witnesses.
2019-02-21 00:02:19 -05:00
Slava Pestov
22259a64a2 IRGen: Don't emit generic witness table if conformance doesn't need one
The condition for "needs a generic witness table" was wrong; any
conformance with associated conformances would get one, even though
its only necessary if the associated conformances are dependent.

Fixing this also allows simplifying some code.
2019-02-20 23:34:01 -05:00
Slava Pestov
71ab1bbe77 IRGen: Fix isResilientConformance() check
If the conforming type is generic, we have to treat the conformance as
resilient if it is defined outside of the current module.

This is because it can resiliently change from being non-dependent
to dependent.
2019-02-20 20:00:46 -05:00
Slava Pestov
18ea8a11d1 IRGen: Move isResilientConformance() and isDependentConformance() to IRGenModule 2019-02-20 16:20:48 -05:00
Slava Pestov
dffa29fd0d AST: Remove a few uses of FunctionType::Param::getOldType() 2019-02-07 23:46:31 -05:00
swift-ci
e9c05b0292 Merge pull request #22410 from DougGregor/irgen-cyclic-metadata-sr-5958 2019-02-06 06:50:42 -08:00
Doug Gregor
229ddf570f [IRGen] Metadata for the conforming type in a witness table access need not be complete.
When calling a witness table accessor, IRGen was forcing the
conforming type to have complete metadata, even though only abstract
metadata is required for that query. This could cause cyclic metadata
dependencies when checking conditional conformances.

Fixes SR-5958.
2019-02-05 22:02:49 -08:00
Doug Gregor
2d1fc680ed [SIL] Eliminate SILFunctionType::getDefaultWitnessMethodProtocol().
This method wasn’t returning the protocol on which the that the witness
method would satisfy, as documented. Rather, it was returning the protocol
to which the `Self` type conforms, which could be completely unrelated. For
example, in IndexingIterator’s conformance to IteratorProtocol, this method
would produce the protocol “Collection”, because that’s where the witness
itself was implemented. However, there isn’t necessarily a single such
protocol, so checking for/returning a single protocol was incorrect.

It turns out that there were only a few SIL verifier assertions of it
(that are trivially true) and two actual uses in code:
(1) The devirtualizer was using this computation to decide when it didn’t
need to perform any additional substitutions, but it’s predicate for doing
so was essentially incorrect. Instead, it really wanted to check whether
the Self type is still a type parameter. 
(2) Our polymorphic convention was using it to essentially check whether 
the ’Self’ instance type of a witness_method was a GenericTypeParamType,
which we can check directly.


Fixes rdar://problem/47767506 and possibly the hard-to-reproduce
rdar://problem/47772899.
2019-02-04 16:18:00 -08:00
Slava Pestov
77e5b44560 IRGen: Ensure that default witness thunks are emitted in the same thread as the protocol descriptor
Protocol descriptors for resilient protocols relatively-reference
default witness thunks, so when using -num-threads N with N > 1,
we must ensure the default witness thunk is emitted in the same
LLVM module.
2019-01-15 21:42:24 -05:00
Slava Pestov
152fab22a1 Revert "IRGen: Ensure that default witness thunks are emitted in the same thread as the protocol descriptor" 2019-01-15 01:05:06 -05:00
Slava Pestov
50465688f8 IRGen: Ensure that default witness thunks are emitted in the same thread as the protocol descriptor
Protocol descriptors for resilient protocols relatively-reference
default witness thunks, so when using -num-threads N with N > 1,
we must ensure the default witness thunk is emitted in the same
LLVM module.
2019-01-14 16:26:07 -05:00
Arnold Schwaighofer
bf4e8ae651 IRGen: Handle self-conformances in defineAssociatedTypeWitnessTableAccessFunction
rdar://46664811
2018-12-12 13:22:03 -08:00
Slava Pestov
ad230a065d IRGen: Remove some InOutType usages 2018-12-10 00:00:49 -05:00
Slava Pestov
1210bb68a4 SIL: SILFunctionTypes don't allow generic signatures where all parameters are concrete 2018-12-08 22:21:12 -05:00
Slava Pestov
aa747dcd81 Remove property behaviors 2018-12-07 20:38:33 -05:00
John McCall
8112f68b96 Merge pull request #20629 from rjmccall/error-self-conformance
Allow Error to conform to itself
2018-12-05 19:58:08 -05:00
Adrian Prantl
ff63eaea6f Remove \brief commands from doxygen comments.
We've been running doxygen with the autobrief option for a couple of
years now. This makes the \brief markers into our comments
redundant. Since they are a visual distraction and we don't want to
encourage more \brief markers in new code either, this patch removes
them all.

Patch produced by

      for i in $(git grep -l '\\brief'); do perl -pi -e 's/\\brief //g' $i & done
2018-12-04 15:45:04 -08:00
Doug Gregor
e6620b055d [Mangling] Separate out base conformance descriptors.
Separate the mangling of base conformance descriptors from that of
associated conformance descriptors, and simplify it.
2018-12-04 00:13:54 -08:00