Commit Graph

136 Commits

Author SHA1 Message Date
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
Slava Pestov
c58899a212 SILGen: Combine emitOrigToSubstMetatype() and emitSubstToOrigMetatype(), NFC
Swift SVN r31525
2015-08-27 08:35:23 +00:00
Slava Pestov
a49198c791 SILGen: FunctionConversionExpr now uses thunking logic, NFC
Swift SVN r31487
2015-08-26 09:15:31 +00:00
Slava Pestov
e1f0f02480 SILGen: Three-parameter re-abstraction thunks
Right now, re-abstraction thunks are set up to convert values
as follows, where L is type lowering:

- OrigToSubst: L(origType, substType) -> L(substType)
- SubstToOrig: L(substType) -> L(origType, substType)

This assumes there's no AST-level type conversion, because
when we visit a type in contravariant position, we flip the
direction of the transform but we're still converting *to*
substType -- which will now equal to the type of the input,
not the type of the expected result!

This caused several problems:

- VTable thunk generation had a bunch of special logic to
  compute a substOverrideType, and wrap the thunk result
  in an optional, duplicating work done in the transform

- Witness thunk generation similarly had to handle the case
  of upcasting to a base class to call the witness, and
  casting the result of materializeForSet back to the right
  type for properties defined on the base.

  Now the materializeForSet cast sequence is a bit longer,
  we unpack the returned tuple and do a convert_function
  on the function, then pack it again -- before we would
  unchecked_ref_cast the tuple, which is technically
  incorrect since the tuple is not a ref, but IRGen didn't
  seem to care...

To handle the conversions correctly, we add a third AST type
parameter to a transform, named inputType. Now, transforms
perform these conversions:

- OrigToSubst: L(origType, inputType) -> L(substType)
- SubstToOrig: L(inputType) -> L(origType, substType)

When we flip the direction of the transform while visiting
types in contravariant position, we also swap substType with
inputType.

Note that this is similar to how bridging thunks work, for
the same reason -- bridging thunks convert between AST types.

This is mostly just a nice cleanup that fixes some obscure
corner cases for now, but this functionality will be used
in a subsequent patch.

Swift SVN r31486
2015-08-26 09:15:29 +00:00
Slava Pestov
6c944a694e SIL: Move checkForABIDifferences() from SILFunctionType to TypeConverter, NFC
Also make it more accurate by lowering optional payload types, but
nothing depends on this right now.

Apologies for the recent stream of unrelated-looking NFCs -- the goal
here is to make a big upcoming patch easier to review.

Swift SVN r31480
2015-08-26 04:24:27 +00:00
Slava Pestov
689be69345 SILGen: Split off cleanup of transforms code from bigger patch
- Generalize::transformFunction() had a couple of little optimizations
  for emitting convert_function or thin_to_thick_function instead of
  a thunk, if possible -- move this into code shared by all
  transforms

- Nuke Generalize since it doesn't do anything special anymore

Swift SVN r31423
2015-08-24 00:23:57 +00:00
Slava Pestov
074a5577ca SIL: Extract SILFunctionType::checkForABIDifferences(), NFC
There are three implementations of this in SIL so far:
- in emitGeneralizedFunctionValue()
- in emitVTableMethod()
- in the SILVerifier

I haven't touched the latter yet, though.

Swift SVN r31422
2015-08-23 23:10:54 +00:00
Slava Pestov
7a2018ef52 SILGen: fix conversions from @convention(c) to @convention({thick,block})
Also clean up some code and add some tests for FunctionConversionExpr,
which will be very important in subsequent patches.

Swift SVN r31400
2015-08-22 03:17:02 +00:00
John McCall
807f7f5434 Reimplement materializeForSet emission in SILGen instead of
the type-checker.  The strategy for now is to just use this
for protocol witness thunk emission, where it is required
when generating a materializeForSet for storage that is
either implemented in a protocol extension or requires
reabstraction to the requirement's pattern.

Eventually, this should be generalized to the point that
we can use it for all materializeForSet emission, which
I don't think will take much.  However, that's not really
the sort of instability we want to embrace for the current
release.

WIP towards rdar://21836671; currently disabled, so NFC.

Swift SVN r31072
2015-08-07 09:22:27 +00:00
John McCall
ed0681004c Remove the almost-pointless Materialize type from SILGen,
and stop forcing optionals to memory in a bunch of places
where the callee is perfectly capable of handling a
non-address value.

Swift SVN r30107
2015-07-11 02:58:49 +00:00
Slava Pestov
ee2aacceb9 SILGen: Fix vtable thunk generation for overrides changing Optional to IUO
Actually invoking such an override on an instance of the derived type
still doesn't work, unless you cast to the base type first. Fixing this
requires emitting a new vtable entry for the new signature and is
tracked in <rdar://problem/21435542>.

Fixes <rdar://problem/21364764>.

Swift SVN r29935
2015-07-07 04:41:52 +00:00
Slava Pestov
5c76bb35a6 SILGen: Simpler fix for crash when protocol witness is unary method with default argument
This patch attempts to fix rdar://problem/21452981, which Joe worked on
in r29572, but reverted because that patch had other changes that broke
stuff.

Swift SVN r29934
2015-07-07 04:41:51 +00:00
Slava Pestov
35e55940ca SILGen: Fix miscompile if witness is a property defined in base class
Fixes <rdar://problem/21375219>.

Swift SVN r29901
2015-07-02 21:31:07 +00:00
Joe Groff
2be344aa33 Revert "SILGen: Make witness parameter translation handle single-element tuples."
This reverts commit r29572, which is breaking some external projects.

Swift SVN r29605
2015-06-24 13:59:33 +00:00
Joe Groff
7a4b80ce4d SILGen: Make witness parameter translation handle single-element tuples.
These still show up if you have a witness with a default argument, which the AST still represents as a single-element tuple, matched against a non-tuple orig type in the witness signature. Handle this by allowing getTupleElementType(0) on non-tuple AbstractionPatterns to work, and tweaking the parallel translation code to drill through single-element tuples and line up the contained scalars properly. Fixes rdar://problem/21452981. (Still left broken is the obnoxious single-element-tuple-of-tuple case, rdar://problem/21496105.)

Now with less Xcode autosave breakage.

Swift SVN r29572
2015-06-23 17:25:47 +00:00
Dave Abrahams
4d7feb72e0 Revert "SILGen: Make witness parameter translation handle single-element tuples."
This reverts commit r29561; JoeG says it was a mistake.

Swift SVN r29562
2015-06-23 00:57:08 +00:00
Joe Groff
87ba8f5123 SILGen: Make witness parameter translation handle single-element tuples.
These still show up if you have a witness with a default argument, which the AST still represents as a single-element tuple, matched against a non-tuple orig type in the witness signature. Handle this by allowing getTupleElementType(0) on non-tuple AbstractionPatterns to work, and tweaking the parallel translation code to drill through single-element tuples and line up the contained scalars properly. Fixes rdar://problem/21452981. (Still left broken is the obnoxious single-element-tuple-of-tuple case, rdar://problem/21496105.)

Swift SVN r29561
2015-06-23 00:27:43 +00:00
Joe Groff
6ef872d471 SILGen: Handle optional variance in overrides before abstracting lowering their signatures.
Fixes rdar://problem/21154055.

Swift SVN r29406
2015-06-16 23:01:01 +00:00
Joe Groff
96638e3028 SILGen: Properly type reabstracted overrides in calls and witness thunks.
Fixes most of rdar://problem/20874966, though curry thunks are still broken (filed as rdar://problem/21167978).

Swift SVN r29160
2015-05-30 00:41:55 +00:00
Joe Groff
66e1f37e62 SILGen: Reabstraction for vtable entries.
When a derived class specializes its base class, e.g. 'class Derived: Base<Int>', the natural abstraction levels of its methods may differ from the original base class's more abstract methods. Handle this by using the reabstraction machinery to thunk values when necessary. Merge the existing optionality thunking support into the reabstraction code, where witness thunking and similar convention adjustments may also be able to use it, if we desire. Fixes rdar://problem/19760292.

Swift SVN r28505
2015-05-13 03:25:05 +00:00