Commit Graph

555 Commits

Author SHA1 Message Date
Joe Groff
e2d4017a4b SILGen: Check for a null emission context before dereferencing.
Fixes https://bugs.swift.org/browse/SR-3090.
2016-10-31 13:46:57 -07:00
Michael Gottesman
bffa7addaf [semantic-arc] Eliminate default {Load,Store}OwnershipQualification argument to SILBuilder::create{Load,Store}(...)
Today, loads and stores are treated as having @unowned(unsafe) ownership
semantics. This leaves the user to specify ownership changes on the loaded or
stored value independently of the load/store by inserting ARC operations. With
the change to Semantic SIL, this will no longer be true. Instead loads, stores
have ownership semantics that one must reason about such as copy, take, and
trivial.

This change moves us closer to that world by eliminating the default
OwnershipQualification argument from create{Load,Store}. This means that the
compiler developer cannot ignore reasoning about the ownership semantics of the
memory operation that they are creating.

Operationally, this is a NFC change since I have just gone through the compiler
and updated all places where we create loads, stores to pass in the former
default argument ({Load,Store}OwnershipQualifier::Unqualified), to
SILBuilder::create{Load,Store}(...). For now, one can just do that in situations
where one needs to create loads/stores, but over time, I am going to tighten the
semantics up via the verifier.

rdar://28685236
2016-10-30 13:07:06 -07:00
Slava Pestov
8baf405e20 SILGen: Add AnyHashable erasure support for function conversions
When AnyHashable was added, SILGen gained support for lowering
AnyHashableErasureExpr, however we forgot to also add support
for AnyHashable parameter and result conversions to
FunctionConversionExpr.

Fixes <https://bugs.swift.org/browse/SR-2603>.
2016-10-27 22:23:08 -07:00
Michael Gottesman
8b41745b37 [semantic-arc] Change TypeLowering APIs to use the words {Copy,Destroy}Value instead of {Retain,Release}Value. NFC.
This is an attempt to separate functional from superficial changes in the
semantic arc changes.

rdar://28851920
2016-10-24 23:49:11 -07:00
Erik Eckstein
c65c1b5dda Re-apply "SILGen: Only give bridging and re-abstraction thunks a generic signature if necessary"
The commit was reverted because of a regression in the
Prototypes/CollectionTransformers test. I believe the root
cause was an escape analysis bug, which is fixed in my
previous commit.
2016-09-21 23:42:02 -07:00
Erik Eckstein
637fc63e09 Revert "SILGen: Only give bridging and re-abstraction thunks a generic signature if necessary"
This reverts commit 8c3d93503f.

It broke the Prototypes/CollectionTransformers.swift test
2016-09-17 16:35:02 -07:00
Slava Pestov
8c3d93503f SILGen: Only give bridging and re-abstraction thunks a generic signature if necessary
If the thunk's type otherwise did not involve type parameters, we
would still pass around the generic parameters from the caller's
context, which is wasteful.
2016-09-15 21:47:58 -07:00
Slava Pestov
b5decb6be4 SILGen: Fix some issues with @pseudogeneric thunks
This would manifest as crashes in IRGen when bridging a block
taking another block in generic context.

- When emitting a re-abstraction thunk, make it pseudogeneric if
  its parent function is pseudogeneric. This ensures we don't
  try to get runtime type metadata when it doesn't exist.

- Mangle pseudogeneric-ness of a reabstraction thunk correctly.
  Otherwise we could emit thunks with different signatures under
  the same mangling.

- Only set the pseudogeneric attribute if the thunk has a generic
  signature, since otherwise the mangling is no longer unique.

Fixes <rdar://problem/27718566>.
2016-09-15 21:45:52 -07:00
John McCall
0bdc8b2a3b Merge pull request #4689 from rjmccall/optional-abstraction
Abstract the object type of optional types
2016-09-09 10:19:36 -07:00
John McCall
34fb15e375 Abstract the object type of an optional type according to the
abstraction pattern of the type rather than always using the
most-general pattern, and erase ImplicitlyUnwrappedOptional from
the SIL type system.
2016-09-08 23:26:19 -07:00
Slava Pestov
b677a2e6a7 AST: Use the new Type::subst() in a few places
SILType substitutions are still done with the old form, and until
BoundGenericTypes hold conformances, we still have to pass around
a ModuleDecl in a few places we really shouldn't, but one step
at a time.
2016-09-08 21:59:13 -07:00
Slava Pestov
07790d3e5e SILGen: Re-work witness thunk emission a bit
Build the witness thunk signature in a more principled manner, and
clean up how the GenericEnvironment is constructed.
2016-09-06 11:51:13 -07:00
Slava Pestov
ca0b548584 SIL: Replace SILFunction::ContextGenericParams with a GenericEnvironment
This patch is rather large, since it was hard to make this change
incrementally, but most of the changes are mechanical.

Now that we have a lighter-weight data structure in the AST for mapping
interface types to archetypes and vice versa, use that in SIL instead of
a GenericParamList.

This means that when serializing a SILFunction body, we no longer need to
serialize references to archetypes from other modules.

Several methods used for forming substitutions can now be moved from
GenericParamList to GenericEnvironment.

Also, GenericParamList::cloneWithOuterParameters() and
GenericParamList::getEmpty() can now go away, since they were only used
when SILGen-ing witness thunks.

Finally, when printing generic parameters with identical names, the
SIL printer used to number them from highest depth to lowest, by
walking generic parameter lists starting with the innermost one.
Now, ambiguous generic parameters are numbered from lowest depth
to highest, by walking the generic signature, which means test
output in one of the SILGen tests has changed.
2016-08-28 13:51:37 -07:00
John McCall
a6e1e87585 Add implicit conversions and casts from T:Hashable <-> AnyHashable.
rdar://27615802
2016-08-04 23:13:27 -07:00
Doug Gregor
b9363fe6bd [SE-0111] Enable SE-0111 by default. 2016-07-29 17:28:24 -07:00
Doug Gregor
f82f9fb7a2 [SE-0111 SILGen] Dropping labels can make TupleTypes disappear; deal with it. 2016-07-29 01:49:55 -07:00
Jordan Rose
53118e9a5f Split the "Foreign" flag into a ForeignKind enum.
This flag tracks whether we have a special kind of imported class
that has limitations in what you can do with it. Currently it's
used for two things: CF classes, and the magic "Protocol" class used
to represent Objective-C protocol metadata. I'm planning to add a
third to handle classes with the recently-added objc_runtime_visible
attribute, which describes an Objective-C class whose runtime symbols
are hidden (forcibly preventing categories and subclassing). This is
used for some of the types in Dispatch, which has exposed some of the
classes that were considered implementation details on past OSs.

I'm splitting the flag into an enum rather than just marking the
Dispatch classes with the existing flag because we still need to
be able to /cast/ to the Dispatch types (which you can't do with CF
types today) and because they deserve better than to be lumped in
with CF for diagnostic purposes.

Groundwork for rdar://problem/26850367, which is that Swift will
happily let you extend the new Dispatch classes but then fails to find
the symbols at link-time.
2016-06-29 14:20:21 -07:00
practicalswift
fa40ad2f0c [gardening] "Objective C" → "Objective-C" 2016-04-09 12:22:18 +02:00
Doug Gregor
a31edf53d0 Simplify the interface to Module::lookupConformance.
Rather than returning a weird PointerIntPair, return an
Optional<ProtocolConformanceRef>. NFC
2016-03-15 22:08:24 -07:00
Slava Pestov
3e7f2e195c SILGen: Refactoring MaterializeForSet to support default witness thunk emission
Previously we would emit two types of MaterializeForSet implementations
in SILGen:

- materializeForSet for a concrete storage declaration

- materializeForSet witness thunk in a conformance

This refactoring decouples the code from taking a conformance, which is
needed for two new types of materializeForSet that we need:

- materializeForSet witness thunk in a default witness table -- this is
  necessary in order to be able to resiliently add storage requirements
  with default implementations to protocols

- materializeForSet vtable thunk -- this is necessary to fix a missing
  re-abstraction case with overriding storage in a subclass

This patch brings us closer to implementing these two. For default
implementations, we still have an issue in that the materializeForSet
has a different "generic signature abstraction pattern" in concrete
and default witnesses, so default and concrete witnesses for
materializeForSet are currently ABI-incompatible because the type
metadata for the storage is passed differently to the callback.
2016-03-07 17:05:06 -08:00
Slava Pestov
4887d4fdc8 SILGen: Prepare witness thunk emission for default witnesses, NFC
In this case we do not have a conformance, and the default witness
thunk uses the same signature and context archetypes as the protocol
requirement.

There might still be an abstraction change between the requirement
and witness, though.

Tests are in the next patch that actually adds the ability to emit
these thunks. This is just a refactoring.
2016-03-03 07:36:59 -08:00
practicalswift
1666b33908 [gardening] Follow LLVM commenting guidelines: Avoid C style comments. 2016-02-21 07:49:07 +01:00
Joe Groff
a1ef412815 Sema/SILGen: Get property behavior implementations to codegen.
Fix some interface type/context type confusion in the AST synthesis from the previous patch, add a unique private mangling for behavior protocol conformances, and set up SILGen to emit the conformances when property declarations with behaviors are visited. Disable synthesis of the struct memberwise initializer if any instance properties use behaviors; codegen will need to be redesigned here.
2016-02-20 15:01:06 -08:00
Slava Pestov
7fdada8ee5 SIL: Add SILFunction::mapTypeOutOfContext(), NFC 2016-02-19 18:53:28 -08:00
John McCall
e249fd680e Destructure result types in SIL function types.
Similarly to how we've always handled parameter types, we
now recursively expand tuples in result types and separately
determine a result convention for each result.

The most important code-generation change here is that
indirect results are now returned separately from each
other and from any direct results.  It is generally far
better, when receiving an indirect result, to receive it
as an independent result; the caller is much more likely
to be able to directly receive the result in the address
they want to initialize, rather than having to receive it
in temporary memory and then copy parts of it into the
target.

The most important conceptual change here that clients and
producers of SIL must be aware of is the new distinction
between a SILFunctionType's *parameters* and its *argument
list*.  The former is just the formal parameters, derived
purely from the parameter types of the original function;
indirect results are no longer in this list.  The latter
includes the indirect result arguments; as always, all
the indirect results strictly precede the parameters.
Apply instructions and entry block arguments follow the
argument list, not the parameter list.

A relatively minor change is that there can now be multiple
direct results, each with its own result convention.
This is a minor change because I've chosen to leave
return instructions as taking a single operand and
apply instructions as producing a single result; when
the type describes multiple results, they are implicitly
bound up in a tuple.  It might make sense to split these
up and allow e.g. return instructions to take a list
of operands; however, it's not clear what to do on the
caller side, and this would be a major change that can
be separated out from this already over-large patch.

Unsurprisingly, the most invasive changes here are in
SILGen; this requires substantial reworking of both call
emission and reabstraction.  It also proved important
to switch several SILGen operations over to work with
RValue instead of ManagedValue, since otherwise they
would be forced to spuriously "implode" buffers.
2016-02-18 01:26:28 -08:00
Slava Pestov
d60cc798aa SILGen: Another small witness thunk emission cleanup, NFC 2016-02-07 21:40:10 -08:00
Slava Pestov
b34edc30ae SILGen: Clean up witness thunk re-abstraction
We can re-abstract directly from the requirement signature to the
witness signature, now that re-abstraction thunks take an
abstraction pattern for both the input and the output.

To do this, we need to know the SILFunctionType of the witness
before we begin re-abstraction. Refactor getWitnessFunctionRef()
a bit to make this work.
2016-02-07 21:40:10 -08:00
Slava Pestov
cca5644a50 SIL: Replace TypeLowering::getInterfaceTypeOutOfContext() with ArchetypeBuilder::mapTypeOutOfContext(), NFC
This is where the AllowLoweredTypes flag to Type::subst() is important,
since this function is used on both canonical AST types, and lowered
AST types.
2016-02-01 20:49:13 -08:00
Erik Eckstein
d3c975391f SIL: remove dump and print from SILValue
If you want to dump a SILValue from the debugger, use Value->dump()
2016-01-25 15:00:49 -08:00
Erik Eckstein
506ab9809f SIL: remove getTyp() from SILValue 2016-01-25 15:00:49 -08:00
Slava Pestov
e35e30afad SILGen: Orient witness and vtable thunk emission toward interface types, NFC 2016-01-21 18:47:11 -08:00
Slava Pestov
548cd68b84 SILGen: Switch some abstraction patterns over to interface types
Now that all the pieces are in place, we can finally start seeing
some benefits. In particular, the code for witness thunk emission
is much simpler now.
2016-01-18 21:43:55 -08:00
Slava Pestov
879a8966ac SIL: Split up AbstractionPattern::isOpaque() into two predicates
- isTypeParameter() -- check if this is an archetype or dependent
  interface type.

- requiresClass() -- check if this is a class-constrained type
  parameter.

The old isOpaque() check has been replaced by
(isTypeParameter() && !requiresClass(moduleDecl)).

This allows us to pass the ModuleDecl on down to
GenericSignature::requiresClass(), enabling the use of
interface types in abstraction patterns.

NFC for now.
2016-01-14 14:07:56 -08:00
Slava Pestov
218792959d SILGen: Turn static collectParams() function into a method on SILGenFunction, NFC 2016-01-10 17:06:20 -08:00
John McCall
5112864dad Remove the archetype from Substitution.
This eliminates some minor overheads, but mostly it eliminates
a lot of conceptual complexity due to the overhead basically
appearing outside of its context.
2016-01-08 15:27:13 -08:00
John McCall
2df6880617 Introduce ProtocolConformanceRef. NFC.
The main idea here is that we really, really want to be
able to recover the protocol requirement of a conformance
reference even if it's abstract due to the conforming type
being abstract (e.g. an archetype).  I've made the conversion
from ProtocolConformance* explicit to discourage casual
contamination of the Ref with a null value.

As part of this change, always make conformance arrays in
Substitutions fully parallel to the requirements, as opposed
to occasionally being empty when the conformances are abstract.

As another part of this, I've tried to proactively fix
prospective bugs with partially-concrete conformances, which I
believe can happen with concretely-bound archetypes.

In addition to just giving us stronger invariants, this is
progress towards the removal of the archetype from Substitution.
2016-01-08 00:19:59 -08:00
Zach Panzarino
e3a4147ac9 Update copyright date 2015-12-31 23:28:40 +00:00
practicalswift
81e7439a9a Fix typos. 2015-12-23 11:16:34 +01:00
John McCall
23ea72b21e Teach IRGen to apply the ARC autorelease optimization implicitly
when working with autoreleased result conventions, and stop
emitting autorelease_return and strong_retain_autoreleased in
SILGen.

The previous representation, in which strong_retain_autoreleased
was divorced from the call site, allowed it to "wander off" and
be cloned.  This would at best would break the optimization, but
it could also lead to broken IR due to some heroic but perhaps
misguided efforts in IRGen to produce the exact required code
pattern despite the representational flaws.

The SIL pattern for an autoreleased result now looks exactly
like the pattern for an owned result in both the caller and
the callee.  This should be fine as long as interprocedural
optimizations are conservative about convention mismatches.
Optimizations that don't wish to be conservative here should
treat a convention mismatch as an autorelease (if the callee
has an autoreleased result) or a retain (if the formal type
of the call has an autoreleased result).

Fixes rdar://23810212, which is an IRGen miscompile after the
optimizer cloned a strong_retain_autoreleased.  There's no
point in adding this test case because the new SIL pattern
inherently prevents this transformation by construction.

The 'autorelease_return' and 'strong_retain_autoreleased'
instructions are now dead, and I will remove them in a
follow-up commit.
2015-12-09 00:49:42 -08:00
Joe Groff
b1667ec705 SIL: Introduce a new @inout_aliasable parameter convention.
Modeling nonescaping captures as @inout parameters is wrong, because captures are allowed to share state, unlike 'inout' parameters, which are allowed to assume to some degree that there are no aliases during the parameter's scope. To model this, introduce a new @inout_aliasable parameter convention to indicate an indirect parameter that can be written to, not only by the current function, but by well-typed, well-synchronized aliasing accesses too. (This is unrelated to our discussions of adding a "type-unsafe-aliasable" annotation to pointer_to_address to allow for safe pointer punning.)
2015-12-08 14:35:47 -08:00
Slava Pestov
e7a3877a42 SILGen: Fix re-abstraction of single-element tuples to opaque pattern
This triggered on the PerfTestSuite but there weren't any tests for it.
Add a simple test, but probably its still wrong, eg opaque-to-opaque
tuple conversions.

A better fix would be to have transforms support conversions where
only one side is a tuple in transformTuple().
2015-11-06 15:36:43 -08:00
Slava Pestov
1b55becf76 SILGen: Fix issues with one-element tuples in thunks
Take apart exploded one-element tuples and be more careful with
passing around tuple abstraction patterns.

Also, now we can  remove the inputSubstType parameter from
emitOrigToSubstValue() and emitSubstToOrigValue(), making the
signatures of these functions nice and simple once again.

Fixes <rdar://problem/19506347> and <rdar://problem/22502450>.
2015-11-06 13:51:15 -08:00
Slava Pestov
dd5feb616f SILGen: More general re-abstraction thunks
Kill the two Transform subclasses, OrigToSubst and SubstToOrig. Instead,
Transform::transform() and other functions take an abstraction pattern
for both the input and the output value.

This simplifies the code in preparation for resilience expansion thunks.
2015-11-06 11:08:55 -08:00
Slava Pestov
91cf4c11da SIL: Move description of abstraction patterns to AbstractionPattern.h and write new comment in SILGenPoly describing function thunks, NFC
Probably SILGenPoly.cpp should be named SILGenThunk.cpp, but I'm saving
that for if I ever extract the duplication between bridging thunks and
re-abstraction thunks.
2015-11-05 21:49:23 -08:00
Slava Pestov
b3251796ec SILGen: Combine Transform::transformMetatype() implementations, NFC
They're basically doing the same thing, bringing us closer to eliminating
the OrigToSubst and SubstToOrig subclasses of Transform.
2015-11-05 21:46:42 -08:00
Slava Pestov
cbdab7dfca SILGen: Big re-abstraction thunk renaming, NFC
I'm making some big changes here to support resilience and splitting off
the renaming will make code easier to review.
2015-11-05 21:46:41 -08:00
Andrew Trick
57a450b28f Generate unchecked_ref_cast, not unchecked_ref_bit_cast.
This improves support for promoting to and generating
unchecked_ref_cast so we no longer need unchecked_ref_bit_cast, which
will just go away in the next commit.

Swift SVN r32597
2015-10-10 05:42:53 +00:00
Slava Pestov
6d8e600a10 SILGen: Add remaining type conversions to thunk emission
I doubt these are useful at all, but they're easy enough to add
after all the refactoring was done.

Swift SVN r31545
2015-08-27 21:53:58 +00:00
Slava Pestov
0af82e6345 SILGen: Move optional and existential conversions to SILGenConvert.cpp, NFC
This is a better place for code shared between expression emission
and thunks.

Swift SVN r31544
2015-08-27 21:53:49 +00:00
Slava Pestov
4a09d72420 SILGen: Refactor existentials a bit more for use in thunks, NFC
Swift SVN r31526
2015-08-27 08:35:25 +00:00