Commit Graph

405 Commits

Author SHA1 Message Date
Slava Pestov
41971fec99 SILGen: Introduce MaterializedLValue abstraction
This is used when materializing an LValue to share state between
the read and write phases of the access, replacing the 'temporary'
and 'extraInfo' parameters that were previously being passed around.

It adds two new fields, origSelfType and genericSig, which will be
used in an upcoming patch to actually apply the callback with the
current generic signature.

This will finally allow us to make use of materializeForSet
implementations in protocol extensions, which is a prerequisite
for enabling resilient default implementations of property and
subscript requirements.
2016-03-09 16:33:22 -08:00
Slava Pestov
08196c9f9a SILGen: Better type safety in SILGenApply.cpp, NFC 2016-03-07 17:05:06 -08:00
Slava Pestov
5c6d986b50 SILGen: When emitting a partial application thunk for a dynamic method, convert @autoreleased result to @owned
In IRGen, @autoreleased return values are always converted to +1 by
calling objc_retainAutoreleasedReturnValue(), so a partial application
thunk cannot have a result with @autoreleased convention. Just turn
it into @owned instead, since that's what it is, using similar logic
as the @unowned_inner_pointer => @unowned case.

Fixes <rdar://problem/24805609>.
2016-02-25 12:09:11 -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
Daniel Duan
efe230774b [AST] rename some isXXX methods to getAsXXX
There's a group of methods in `DeclContext` with names that start with *is*,
such as `isClassOrClassExtensionContext()`. These names suggests a boolean
return value, while the methods actually return a type declaration. This
patch replaces the *is* prefix with *getAs* to better reflect their interface.
2016-02-11 16:23:40 -08:00
saisi
08abcd92c3 fixed niggling typos 2016-01-29 22:22:12 -05:00
Slava Pestov
27da265abb Refactor some random usages of contextual types, NFC 2016-01-27 23:22:33 -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
845b3fe08e SIL: remove isValid() from SILValue. NFC 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
782a871fe6 SILGen: Clean up some code to use getRValueInstanceType() instead of hand-rolled logic, NFC
This also fixes a warning in release builds.
2016-01-24 22:51:44 -08:00
practicalswift
71e00fefa1 [gardening] Fix typos: "word word" (two spaces) → "word word" (one space) 2016-01-24 21:27:16 +01:00
Slava Pestov
9aba84417a Re-apply "SILGen: Move even more abstraction patterns over to interface types, NFC"
Now with a fix for a crash with rvalue references to local functions
that had generic types in their signature.

This reverts commit 31b97bcc15.
2016-01-22 11:35:02 -08:00
Mark Lacey
31b97bcc15 Revert "SILGen: Move even more abstraction patterns over to interface types, NFC"
This reverts commit 0188258a87 because we
now assert when compiling a performance test.
2016-01-21 21:52:48 -08:00
Slava Pestov
0188258a87 SILGen: Move even more abstraction patterns over to interface types, NFC
This eliminates several usages of constantInfo.{FormalType,LoweredType}
and PolymorphicFunctionType.
2016-01-21 18:47:11 -08:00
practicalswift
6d0eee9b8c Remove unused variables. 2016-01-21 10:33:17 +01:00
David Farler
0825841ef6 Emit static dispatch for super methods of same-module implementations
When the nearest implementation of a superclass's implementation of a
method is in the same module, eagerly emit a direct call to the method
instead of relying on the devirtualizer for these, since this is a very
lightweight check and can make -Onone builds faster.
2016-01-19 19:27:32 -08:00
Erik Eckstein
8110b1ebc8 [SIL] Let alloc_box return a single value.
And use project_box to get to the address value.
SILGen now generates a project_box for each alloc_box.
And IRGen re-uses the address value from the alloc_box if the operand of project_box is an alloc_box.
This lets the generated code be the same as before.

Other than that most changes of this (quite large) commit are straightforward.
2016-01-19 08:59:24 -08:00
Slava Pestov
df38bd1764 SILGen: More interface type abstraction pattern goodness, NFC 2016-01-18 23:17:17 -08:00
David Farler
bd8d85da0a Turn on dynamic super method dispatch by default
This removes the -use-native-super-method flag and turns on dynamic
dispatch for native method invocations on super by default.

rdar://problem/22749732
2016-01-15 13:37:53 -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
32b797d744 SILGen: Remove trivial usages of constantInfo.{Formal,Lowered}Type, NFC 2016-01-13 23:30:01 -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
Joe Groff
5e206f35a1 SILGen: Generate closure contexts as guaranteed.
When enabled, generate closure functions with guaranteed conventions as their context parameters, and pass context arguments to them as guaranteed when possible. (When forming a closure by partial_apply, the partial apply still needs to take ownership of the parameters, regardless of their convention.)
2015-12-31 15:10:27 -08:00
Joe Groff
b76c0abc37 SIL: Lower closure capture params out-of-band from the formal type.
Instead of bodging a representation of the SIL capture parameters for a closure into the formal type of closure SILDeclRefs, introduce those parameters in a separate lowering step. This lets us clean up some TypeLowering code that was tolerating things like SILBoxTypes and naked LValueTypes in formal types for nefarious ends (though requires some hacks in SILGen to keep the representation of curry levels consistent, which is something I hope to clean up next). This also decouples the handling of captures from the handling of other parameters, which should enable us to make them +0. For now, there should be NFC.
2015-12-30 10:32:36 -08:00
practicalswift
fa0b339a21 Fix typos. 2015-12-26 17:51:59 +01:00
practicalswift
36d7072013 Remove immediately adjacent repeated words ("the the", "for for", "an an", etc.). 2015-12-21 22:16:04 +01:00
practicalswift
8ab8847684 Fix typos. 2015-12-16 22:09:32 +01:00
Davide Italiano
2e78fdf58f Fix a bunch of pessimizing moves which prevent copy elision. 2015-12-11 19:09:13 +00:00
Slava Pestov
832a637c04 SIL: Start cleaning up getMethodDispatch() / requiresObjCDispatch()
Move these to SILDeclRef, maybe not the best place but a good home for now.
Factor out a new requiresForeignToNativeThunk() function, which cleans up
some code duplication introduced by the following patch:

478e1c7513

This is a small step towards consolidating duplicated logic for figuring out
method dispatch semantics and emitting curry thunks.
2015-12-11 08:58:52 -08:00
David Farler
e6d1d9748b SILGen: Use the base SILDeclRef for super_method override constant info
getOverriddenVTableEntry only goes one level up in the class hierarchy,
but getConstantOverrideInfo requires that the next level up not itself be an
override.

A little bit of refactoring:

SILDeclRef::getOverriddenVTableEntry()
  -> SILDeclRef::getNextOverriddenVTableEntry()

static findOverriddenFunction()
  -> SILDeclRef::getBaseOverriddenVTableEntry()

rdar://problem/22749732
2015-12-10 15:47:50 -08:00
David Farler
09c6210afa SILGen: Use vtable thunk abstraction for super_method instructions
The constant provided to the callee for super methods reference the
backing implementation, but the vtable entry may point to a thunk with
different abstraction when using mixed concrete and generic classes in
the hierarchy. The SIL devirtualizer expects super method references to
match what's in the vtable.

Also update the verifier for SuperMethodInst types - before it required
that the types be the same, but they may not be for the same reasons noted
above. Instead, do a similar check as for ClassMethodInst.

https://bugs.swift.org/browse/SR-134
2015-12-10 13:53:09 -08:00
David Farler
a04a557404 SILGen: Substitute generics for partial super_method instructions
These need to be provided to partial_apply instructions.
2015-12-10 12:12:00 -08: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
David Farler
6825b27316 Use calculated uncurry level to determine how to emit partial super_method
There was a mis-refactoring when removing the LatePartialSuperEmitter,
using the wrong uncurryLevel check when using the inlined emitter.
This fixes a crash with partially applied super methods.

The test was updated for this case.
2015-12-08 13:20:37 -08:00
David Farler
b53ff3b580 SILGen: Refactor LatePartialSuperEmitter
We proabably aren't going to continue using this pattern, so just
inline the code at its single point of use. NFC.
2015-12-07 21:57:37 -08:00
David Farler
a53a31cb6e SILGen: Use super_method for native non-final methods in classes
Use the `super_method` instruction for non-final `func` and `class func`
declarations in native Swift classes. Previously, we would always emit
a static `function_ref` for these, which prevents resilient dynamic
dispatch.

This is hidden behind a -use-native-super-dispatch flag while I
survey the effects on devirtualization and stack promotion. When
that's figured out, I'll add more tests and update test cases that
still assume static dispatch.

rdar://problem/22749732
2015-12-05 13:06:31 -08:00
Slava Pestov
d0d038bdab SILGen: Fix early return from enum payload initialization
This is a regression from the following commit, caught by
validation-tests/stdlib/String:

7496730f69

I'll add SILGen tests for this case later, for now just fix
the broken build.
2015-12-05 03:40:03 -08:00
Slava Pestov
a7a9e16298 SILGen: Open code calls of enum case constructors
Sema models enum case constructors as ApplyExprs. Formerly SILGen
would emit a case constructor function for each enum case,
constructing the enum value in the constructor body. ApplyExprs
of case constructors were lowered like any other call.

This is nice and straightforward but has several downsides:

1) Case constructor functions are very repetitive and trivial,
   in particular for no-payload cases. They were declared
   @_transparent and so were inlined at call sites, but for
   public enums they still had to be emitted into the final
   object file.

2) If the enum is generic, the substituted type may be loadable
   even if the unsubstituted type is not, but since the case
   constructor is polymorphic we had to allocate stack buffers
   anyway, to pass the payload and result at the right abstration
   level. This meant that for example Optional.Some(foo)
   generated less-efficient SIL than the equivalent implicit
   conversion.

3) We were missing out on peephole optimization opportunities when
   the payload of an indirect case or address-only enum could be
   emitted directly into the destination buffer, avoiding a copy.
   One example would be when an enum payload is the result of
   calling a function that returns an address-only value indirectly.
   It appears we had unnecessary copies and takes even with -O.
   Again, optional implicit conversions special-cased this.

This patch implements a new approach where a fully-formed call to
a element constructor is handled via a special code path where
the 'enum' or 'init_enum_data_addr' / 'inject_enum_addr'
instructions are emitted directly. These always work on the
substituted type, avoiding stack allocations unless needed.
An additional optimization is that the ArgumentSource abstraction
is used to delay evaluation of the payload argument until the
indirect box or address-only payload was set up.

If a element constructor is partially applied, we still emit a
reference to the constant as before.

It may seem like case constructor functions are at least useful
for resilience, but case constructors are transparent, so making
them resilient would require a new "transparent but only in this
module, and don't serialize the SIL body" declaration.
@inline(always) is almost what we need here, but this affect
mandatory inlining, only the optimizer, so it would be a
regression for non-resilient enums, or usages of resilient enums
in the current module.

A better approach is to construct resilient enums with a new
destructiveInjectEnumTag value witness function, which is
coming soon, and the general improvement from that approach
is what prompted this patch.
2015-12-05 01:44:44 -08:00
Slava Pestov
7496730f69 SILGen: Add a new SILGenFunction::emitInjectEnum() function, NFC
This factors out code duplication between InjectIntoOptionalExpr and
enum case constructors, and will eventually be used in other places
too.
2015-12-05 01:44:44 -08:00
Slava Pestov
2fa0bf82a2 SILGen: Simplify instance variables of Callee class, NFC 2015-12-05 01:44:44 -08:00
Slava Pestov
9025cf881d SILGen: Add comments describing call decomposition in more detail, NFC 2015-12-05 01:37:55 -08:00
David Farler
6d831bd00e Add specialized emitter for partially applied super_method
This creates the heavy lifting in SILGen of emission of super methods
with the implicit self partially applied, which is still possible even
after removing curried function declaration syntax.

NFC for current IRGen - SILGen doesn't lead to here yet.

rdar://problem/22749732
2015-12-05 01:29:27 -08:00
Slava Pestov
c73a04d865 SILGen: ApplyOptions::Transparent and related support logic is dead, NFC 2015-11-30 20:33:22 -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
736beddf1b SILGen: Cleanup, NFC 2015-11-05 21:46:04 -08:00
Slava Pestov
533f42dd2f Closures and local functions only capture generic parameters if necessary
The CaptureInfo computed by Sema now records if the body of the
function uses any generic parameters from the outer context.
SIL type lowering only adds a generic signature if this is the
case, instead of unconditionally.

This might yield a marginal performance improvement in some cases,
but more interestingly will allow @convention(c) conversions from
generic context.

Swift SVN r32161
2015-09-22 21:08:28 +00:00