Commit Graph

582 Commits

Author SHA1 Message Date
Joe Groff
dc0f770364 remove todo warnings, oops 2019-10-26 10:49:47 -07:00
Joe Groff
1a1d9e63f7 Merge pull request #27887 from jckarter/subst-sil-function-type-interface
SIL: Add fields to SILFunctionType for substituted function types.
2019-10-26 10:44:48 -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
Michael Gottesman
26a734e58e [sil] Rename ValueOwnershipKind::{Any,None} 2019-10-25 10:28:25 -07:00
Robert Widmann
5a8d0744c3 [NFC] Adopt TypeBase-isms for GenericSignature
Structurally prevent a number of common anti-patterns involving generic
signatures by separating the interface into GenericSignature and the
implementation into GenericSignatureBase.  In particular, this allows
the comparison operators to be deleted which forces callers to
canonicalize the signature or ask to compare pointers explicitly.
2019-09-30 14:04:36 -07:00
Slava Pestov
1e94466bcc AST: Replace GenericSignature::createGenericEnvironment() with getGenericEnvironment()
This memoizes the result, which is fine for all callers; the only
exception is open existential types where each new open existential
now explicitly gets a unique generic environment, allocated by
calling GenericEnvironment::getIncomplete().
2019-09-06 17:16:03 -04:00
Doug Gregor
0e2bb0fbe2 Prune includes of GenericSignatureBuilder.h. 2019-08-26 09:54:20 -07:00
Doug Gregor
4685f9dd0d [SILGen] Use abstract generic signature request for reabstraction thunks 2019-08-26 09:54:19 -07:00
Slava Pestov
8f22da205a Sema: Stop wrapping Self returns in protocols with DynamicSelfType 2019-06-26 01:10:11 -04:00
Slava Pestov
a31248997c SILGen: Correctly emit vtables when an override is more visible than the base
If an override B.f() is more visible than a base method A.f(), it is
possible that an override C.f() of B.f() cannot see the original method
A.f().

In this case, we would encounter linker errors if we referenced the
method descriptor or method dispatch thunk for A.f().

Make this work by treating B.f() as the least derived method in this
case, and ensuring that the vtable thunk for B.f() dispatches through
the vtable again.

Fixes <rdar://problem/48330571>, <https://bugs.swift.org/browse/SR-10648>.
2019-06-01 00:08:05 -04:00
Slava Pestov
b4487348d0 SILGen: Support vtable thunks for 'modify' accessors
When checking if a vtable override is ABI compatible with the
base class method, make sure to check yields too.

Also, add support for coroutines to vtable thunks, using code
that I've copy and pasted and tweaked from witness thunks.

(It would be nice to combine witness thunks and vtable thunks
into a single code path for 'method thunks', but that requires
some additional refactoring, so live with the copy and paste
for now).
2019-06-01 00:08:05 -04:00
Michael Gottesman
179251d167 [sil-combine] Do not perform existential type propagation on @in parameters when we have a copy of the underlying value.
Otherwise, we may get use-after-frees as in the added test.

rdar://50609145
2019-05-09 23:01:43 -07:00
Michael Gottesman
580ca729d3 [silgen] Always ensure that we have a +1 value when emitting reabstraction thunks.
The machinery assumes that it will always have a +1 value. I am attempting to do
the minimal fix here for cherry-picking purposes.

There isn't a test case with this commit since the immutable address verifier
(in the next commit) verifies that this is done correctly. The specific test
that will trip is vtable_thunks_reabstraction.swift.

rdar://50212579
2019-05-09 14:57:39 -07:00
Slava Pestov
9a1abf705a SILGen: Remove SILGenSILBuilder
This reverts commit 59cc3c1216fbb1719e5357dcef3f8b249528fc74.
2019-04-25 02:06:14 -04:00
Slava Pestov
8a74e52273 SILGen: Add post-processing pass to lazily emit ClangImproter-synthesized conformances 2019-04-25 02:05:20 -04:00
Slava Pestov
099bd8e055 SILGen: Fix withoutActuallyEscaping for DynamicSelfType
We can reuse the same logic that we have for reabstraction thunks here.
2019-04-14 20:13:49 -04:00
Slava Pestov
a5675a8edd SILGen: Fix function conversions involving DynamicSelfType
This was partially implemented but the check looked at the lowered
types and not the AST types, and DynamicSelfType is erased at the
top level of a lowered type.

Also use the new mangling for reabstraction thunks with self, to
ensure we don't emit the same symbol with two different lowered
types.

Fixes <https://bugs.swift.org/browse/SR-10309>, <rdar://problem/49703441>.
2019-04-14 19:17:32 -04:00
Slava Pestov
18e8feac8f SILGen: Kill OpaqueValueState and clean up code for opening existentials
OpaqueValueState used to store a SILValue, so back then the IsConsumable flag
was meaningful. But now we can just check if the ManagedValue has a cleanup
or not.

Also, we were passing around an opened ArchetypeType for no good reason.
2019-03-27 17:41:40 -04:00
Michael Gottesman
7246806ef6 When emitting no-escape closures, make sure that we copy the closure before we pass it into the partial apply to eliminate an ownership violation.
The problem here is that without this patch we emit code like this:

bb0(%0 : @owned $T):
  %1 = partial_apply %foo(%0)
  %2 = mark_dependence %1 on %0

Since a partial_apply consumes the object, the mark_dependence is a use after
free (especially if one has any further uses of %0 after the mark_dependence).
So what I did was I copied the value before creating the partial_apply. So
now we get this:

bb0(%0 : @owned $T):
  %1 = copy_value %0
  %2 = partial_apply %foo(%1)
  %3 = mark_dependence %2 on %0
  ...
  destroy_value %0

This ensures that one can actually have uses after the mark_dependence of both
operands.

This enables ownership verification to be enabled on
Interpreter/enforce_exclusive_access.

rdar://48521061
2019-03-21 19:39:51 -07:00
Slava Pestov
123fee960e Merge pull request #23228 from slavapestov/type-lowering-is-trivial
Replace SILType::isTrivial(SILModule) with isTrivial(SILFunction)
2019-03-12 13:44:10 -04:00
Slava Pestov
8915f96e3e SIL: Replace SILType::isTrivial(SILModule) with isTrivial(SILFunction) 2019-03-12 01:16:04 -04:00
Michael Gottesman
7c9dfad65c [silgen] Fix a bug where when reabstracting T:P -> P we wrapped a +0 value in an init_existential_ref.
Found via the ownership verifier running on IRGen/outlined_copy_addr.swift.

We treat initialization of an existential as a +1 operation, so there is the
possibility that we would have double destroyed a value. In fixing this I
followed what manageOpaqueValue did on the first code path, since it is handling
this case correctly (and using the SGFContext to do so to boot!).

To ensure this is tested, I enabled ownership verification on the test and since
it seems to be exposing a SILGen code path that we aren't testing currently,
converted it into a SILGen test.
2019-03-11 13:40:26 -07:00
John McCall
ecac7624b8 Remove "canonical" thunks in favor of reabstraction thunks.
There are instances of currying that require full reabstraction,
such as when partially-applying a concrete override of a generic
class method.

Fixes rdar://45671537 and SR-4425.
2019-01-16 16:43:48 -05:00
Michael Gottesman
3551c43d97 [sil] Add SILBuilder helpers for updating passes to handle both ossa and non-ossa code.
Specifically we add a groups of APIs for destructure operations. The destructure
helpers are a family of functions built around
emitDestructureValueOperation. These in ossa produce destructures and pass the
results off to the caller in some manner that hides the internal destructure
instruction. In non-ossa, the appropriate projections are created and passed off
to the caller.
2019-01-15 09:01:34 -08:00
Slava Pestov
5ade432e6f SILGen: Emit reabtraction thunks that can capture DynamicSelfType
Fixes <https://bugs.swift.org/browse/SR-9429>.
2019-01-11 15:55:45 -05:00
Slava Pestov
439bc5010e SILGen: Strip off DynamicSelfType when tranforming values
We lower away a top-level DynamicSelfType, so let's just strip them off
before trying the other conversions. For now this is NFC, but we can end
up here after the next patch.

Even then we shouldn't end up here except when emitting protocol witnesses.
The relevant case is when a base class method (return @dynamic_self Base)
is used by a derived class to witness a requirement on a protocol
(which returns @dynamic_self Derived since its a derived class conformance).

In this case, we know both values have the same type, but the lowered
types are $Base and $Derived, respectively, so we must emit an unchecked
downcast.
2019-01-11 15:53:50 -05:00
Arnold Schwaighofer
cb0c53abee SIL: Remove isEscapedByUser flag on convert_escape_to_noescape instruction
It was only used for materializeForSet and is now dead code.
2019-01-04 09:21:38 -08:00
Joe Groff
89979137fc Push ArchetypeType's API down to subclasses.
And clean up code that conditionally works only with certain kinds of archetype along the way.
2018-12-12 19:45:40 -08: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
Michael Gottesman
0af0d5fddc [ownership] Replace ValueOwnershipKind::Trivial with ValueOwnershipKind::Any.
In a previous commit, I banned in the verifier any SILValue from producing
ValueOwnershipKind::Any in preparation for this.

This change arises out of discussions in between John, Andy, and I around
ValueOwnershipKind::Trivial. The specific realization was that this ownership
kind was an unnecessary conflation of the a type system idea (triviality) with
an ownership idea (@any, an ownership kind that is compatible with any other
ownership kind at value merge points and can only create). This caused the
ownership model to have to contort to handle the non-payloaded or trivial cases
of non-trivial enums. This is unnecessary if we just eliminate the any case and
in the verifier separately verify that trivial => @any (notice that we do not
verify that @any => trivial).

NOTE: This is technically an NFC intended change since I am just replacing
Trivial with Any. That is why if you look at the tests you will see that I
actually did not need to update anything except removing some @trivial ownership
since @any ownership is represented without writing @any in the parsed sil.

rdar://46294760
2018-12-04 23:01:43 -08:00
John McCall
49ba9c59d1 Allow Error to conform to itself.
Most of the foundation for this was laid in earlier patches.
2018-11-17 02:51:45 -05:00
John McCall
731da3b991 [NFC] Improve some SILGen functions for working with begin_apply. 2018-11-10 02:08:04 -05:00
Arnold Schwaighofer
b102c7f6b4 Parser/Sema/SILGen changes for @_dynamicReplacement(for:)
Dynamic replacements are currently written in extensions as

extension ExtendedType {
  @_dynamicReplacement(for: replacedFun())
  func replacement() { }
}

The runtime implementation allows an implementation in the future where
dynamic replacements are gather in a scope and can be dynamically
enabled and disabled.

For example:

dynamic_extension_scope CollectionOfReplacements {
  extension ExtentedType {
    func replacedFun() {}
  }

  extension ExtentedType2 {
    func replacedFun() {}
  }
}

CollectionOfReplacements.enable()
CollectionOfReplacements.disable()
2018-11-06 09:58:36 -08:00
Arnold Schwaighofer
c158106329 Allow dynamic without @objc in -swift-version 5
Dynamic functions will allow replacement of their implementation at
runtime.
2018-11-06 09:53:21 -08:00
John McCall
7da688d75a Always manage subobject projections with formal-access cleanups.
To make that work, enter appropriate scopes (ArgumentScopes and
FormalEvaluationScopes) at a bunch of places.  But note that l-value
emission generally can't enter such a scope, so in generic routines
like emitOpenExistentialExpr we have to just assert that we're
already in a scope.
2018-11-03 02:14:06 -04:00
Slava Pestov
084b0e07ee SILGen: Strip __owned from parameter types when emitting SE-0110 tuple splat
Instead of using composeInput(), build a tuple type containing
the element types only, dropping ownership qualifiers and
asserting that there are no inout or vararg elements.

This is correct because we already promote +0 values to +1, or
clean up +1 values that are only used as +0 as needed.

Fixes <rdar://problem/44915136>.
2018-10-03 02:43:28 -04:00
Slava Pestov
ebe769a58c SIL: Stop imploding parameter list into a single value with opaque abstraction pattern
The constraint solver support for the Swift 3 function type behavior
has been removed, so it's no longer possible to pun the same value as
both a function taking multiple parameters and a function taking a
single tuple argument.

This means the entire parameter list is no longer a target for
substitution as a single value, so the most general form of a function
value passes each parameter indirectly instead of passing a single
tuple parameter indirectly.
2018-09-26 23:20:54 -07:00
Slava Pestov
69cd7b3e05 SILGen: Simplify reabstraction thunk emission
Now that we handle the top-level parameter list specially, we never
end up with non-materializable tuples here, or other odd cases like
one-element tuples, so a bunch of complexity can be eliminated.
2018-09-25 23:14:52 -07:00
Slava Pestov
62a94f6660 SILGen: Wean reabstraction thunk emission off getFunctionInputType()
Treat the top level of the parameter list specially, instead of
looking at FunctionType::getInput().

There are three cases where we don't translate parameters 1-1:

- Imploding the parameter list into a single value when reabstracting
  a function with the opaque abstraction pattern. This will go away
  once Swift 3 compatibility is fully removed.

- Exploding the parameter list from a single value. This is the dual of
  the above.

- Exploding the parameter list for an SE-0110 "tuple splat" conversion.
  This is the one case that will need to be supported on an on-going
  basis, but it is not too bad to handle directly.
2018-09-25 23:13:20 -07:00
Slava Pestov
e399bd9b2e SILGen: VTable and witness thunk emission uses new function type representation
This eliminates the only usage of AbstractionPattern::dropLastTupleElement().

For now, we keep the argument tuple-based entry points in TransformArguments
as well, because they're used by re-abstraction thunks. Those require a bit more
refactoring.
2018-09-14 13:37:46 -07:00
Joe Groff
77a0923ca6 SILGen: Emit convenience initializers as allocating entry points.
And only dispatch designated inits by their allocating entry points. rdar://problem/29634243
2018-09-13 12:31:23 -07:00
Ben Cohen
6567438027 Squash release build warnings (#18986) 2018-08-28 09:56:17 -07:00
John McCall
b80618fc80 Replace materializeForSet with the modify coroutine.
Most of this patch is just removing special cases for materializeForSet
or other fairly mechanical replacements.  Unfortunately, the rest is
still a fairly big change, and not one that can be easily split apart
because of the quite reasonable reliance on metaprogramming throughout
the compiler.  And, of course, there are a bunch of test updates that
have to be sync'ed with the actual change to code-generation.

This is SR-7134.
2018-08-27 03:24:43 -04:00
Robert Widmann
014fd952ef [NFC] Silence a bunch of Wunused-variable diagnostics 2018-08-24 15:16:40 -07:00
John McCall
c491f1b721 Implement reabstraction of inouts in witness and reabstraction thunks.
This is a longstanding gap in the implementation that quite possibly
never bit anyone in the wild.  I've only gotten around to implementing
it now because this same code path will be used to reabstract inout
yields.  That means that, rather than only affecting functions with
an abstraction difference in an inout parameter type, this can affect
all storage with any abstraction difference in the stored value type.
So it's time to fill in this gap.
2018-08-22 01:55:17 -04:00
Michael Gottesman
dfc3265f73 [silgen] Change SILGenPoly::executeInnerTuple to use destructures instead of tuple_extracts to element a borrow.
rdar://43493020
2018-08-20 18:10:17 -07:00
Jordan Rose
537954fb93 [AST] Rename several DeclContext methods to be clearer and shorter (#18798)
- getAsDeclOrDeclExtensionContext -> getAsDecl

This is basically the same as a dyn_cast, so it should use a 'getAs'
name like TypeBase does.

- getAsNominalTypeOrNominalTypeExtensionContext -> getSelfNominalTypeDecl
- getAsClassOrClassExtensionContext -> getSelfClassDecl
- getAsEnumOrEnumExtensionContext -> getSelfEnumDecl
- getAsStructOrStructExtensionContext -> getSelfStructDecl
- getAsProtocolOrProtocolExtensionContext -> getSelfProtocolDecl
- getAsTypeOrTypeExtensionContext -> getSelfTypeDecl (private)

These do /not/ return some form of 'this'; instead, they get the
extended types when 'this' is an extension. They started off life with
'is' names, which makes sense, but changed to this at some point.  The
names I went with match up with getSelfInterfaceType and
getSelfTypeInContext, even though strictly speaking they're closer to
what getDeclaredInterfaceType does. But it didn't seem right to claim
that an extension "declares" the ClassDecl here.

- getAsProtocolExtensionContext -> getExtendedProtocolDecl

Like the above, this didn't return the ExtensionDecl; it returned its
extended type.

This entire commit is a mechanical change: find-and-replace, followed
by manual reformatted but no code changes.
2018-08-17 14:05:24 -07:00
Andrew Trick
c9033ed938 Add a SIL attribute [without_actually_escaping].
ConvertFunction and reabstraction thunks need this attribute. Otherwise,
there is no way to identify that withoutActuallyEscaping was used
to explicitly perform a conversion.

The destination of a [without_actually_escaping] conversion always has
an escaping function type. The source may have either an escaping or
@noescape function type. The conversion itself may be a nop, and there
is nothing distinctive about it. The thing that is special about these
conversions is that the source function type may have unboxed
captures. i.e. they have @inout_aliasable parameters. Exclusivity
requires that the compiler enforce a SIL data flow invariant that
nonescaping closures with unboxed captures can never be stored or
passed as an @escaping function argument. Adding this attribute allows
the compiler to enforce the invariant in general with an escape hatch
for withoutActuallyEscaping.
2018-08-14 17:14:25 -07:00
Slava Pestov
bf094b60f0 SIL: Fix some corner cases with tuple type lowering
1) It's possible to materialize a tuple value with an @escaping or
@autoclosure element in it.

I don't think this causes any bad behavior in 4.2 because these
flags have no semantic effect after the type checker, but now
I'm adding an assertion that will fire when such types are
serialized, so let's make sure it doesn't happen by explicitly
clearing out these flags when lowering tuples types.

2) It's also possible to materialize a tuple with a single vararg
element. Again, this was not a problem in 4.2, but with the above
change to start clearing tuple flags, we now end up in a
situation where the lowered type is not a tuple, because
TupleType::get() returns a ParenType if the tuple has one
element that is not vararg (which it no longer is, because we
just cleared all the flags).

Fix the second problem by treating one-element vararg tuples just
like tuples with inout, __shared and __owned elements, that is,
by always exploding them when they appear at the top level of a
function parameter list, ensuring we never try to materialize
a value whose type is the entire tuple type.

These problems all stem from the fact that lowering a function type
with the opaque abstraction pattern treats the top level argument
list as a single tuple argument. Once that is fixed, much of the
above will simplify down to assertions.
2018-08-12 01:09:46 -07:00
Slava Pestov
80a7ae100b SIL: Treat __shared and __owned just like inout when lowering function types
Even with an opaque abstraction pattern, we must explode a
parameter list containing __shared and __owned elements.

Otherwise, we produce invalid lowered SIL types.

Note that this is already an issue, because the stdlib has a
handful of declarations using __owned.
2018-08-12 01:09:45 -07:00