Commit Graph

759 Commits

Author SHA1 Message Date
Doug Gregor
d3d20bbb4f Remove an unused LValue path component for opening opaque existentials.
NFC; this was dead code, and we're not going to be seeing these in
SILGen once the type checker is opening existentials.

Swift SVN r27084
2015-04-07 16:29:10 +00:00
Doug Gregor
441fb9856c Handle lvalue OpaqueValueExprs in SILGen.
Tests coming once Sema starts creating such entities.

Swift SVN r27066
2015-04-07 06:01:16 +00:00
Chris Lattner
95b7b4d5e4 rename CleanupLocation::getCleanupLocation -> CleanupLocation::get, NFC.
Swift SVN r26994
2015-04-04 22:56:01 +00:00
John McCall
dc4b8ff2c2 Incorporate an optional Clang type into AbstractionPattern.
This is necessary for correctly dealing with non-standard
ownership conventions in secondary positions, and it should
also help with non-injective type imports (like BOOL/_Bool).
But right now we aren't doing much with it.

Swift SVN r26954
2015-04-03 21:39:31 +00:00
John McCall
35b7db3ae1 Parsing support for error results from SILFunctionType.
Swift SVN r26566
2015-03-26 00:01:32 +00:00
Jordan Rose
d6bc8971c7 If imported calls return nil for __nonnull NSString *, pretend it was "".
The string version of r26479. There's a lot of backstory and justification
there, so just read that commit message again. The one addition for String
is that global NSString constants are loaded as String as well, so that
also has to go through the bridging code even though there's no function
call involved.

Finishes rdar://problem/19734621.

Swift SVN r26510
2015-03-25 01:16:45 +00:00
John McCall
ee4aa14703 Stop reordering blocks in SILBuilder::emitBlock.
This change permits SILGen to make smarter decisions about
block placement by keeping related blocks together instead
of always inserting to the end to the function.  The
flipside is that SILGen needs to be somewhat careful to
create blocks in the right order.  Counter-intuitively,
that order is the reverse of the order in which the blocks
should be laid out, since blocks created later will be
inserted before blocks created earlier.  Note, however,
that this produces the right results for recursive
emission.

To that end, adjust a couple of places in SILGen to
create blocks in properly nested order.

All of the block-order differences in the tests seem
to be desirable; several of them even had confused
comments wondering how on earth a block got injected
where it did.

Also, fix the implementation of SILBuilder::moveBlockTo,
and fix a latent bug in epilogue emission where epilogBB
was erased from its parent (deleting it) and then
queried multiple times (!).

Swift SVN r26428
2015-03-23 06:38:20 +00:00
Joe Groff
962a87f444 SIL: Rename address-only existential instructions to '{init,deinit,open}_existential_addr'.
For better consistency with other address-only instruction variants, and to open the door to new exciting existential representations (such as a refcounted boxed representation for ErrorType).

Swift SVN r25902
2015-03-09 23:55:31 +00:00
Michael Gottesman
77b2e21eb5 Move buildForwardingSubstitutions onto GenericParamList::getForwardingSubstitutions(). NFC.
Swift SVN r25358
2015-02-17 23:01:02 +00:00
Dmitri Hrybenko
61286f0260 Fix warnings produced by a newer version of Clang
Swift SVN r25257
2015-02-12 23:50:47 +00:00
Chris Lattner
9c7417edc2 fix <rdar://problem/19782264> Immutable, optional class members can't have their subproperties read from during init()
The problem here was that the _preconditionImplicitlyUnwrappedOptionalHasValue
compiler intrinsic was taking the optional/IUO argument as inout as a performance
optimization, but DI would reject it (in narrow cases, in inits) because the inout
argument looks like a mutation.  

We could rework this to take it as an @in argument or something, but it is better
to just define this problem away: the precondition doesn't actually care about the
optional, it is just testing its presence, which SILGen does all the time.  Have
SILGen open code the switch_enum and just have the stdlib provide a simpler
_diagnoseUnexpectedNilOptional() to produce the error message.

This avoids the problem completely and produces slightly better -O0 codegen.



Swift SVN r25254
2015-02-12 22:35:51 +00:00
Joe Groff
735bd354a5 SILGen: Remove !hasDifferentTypeOfRValue assertion from emitConversionFromSemanticValue.
We "convert" unowned to unowned without decaying to the semantic strong type when an unowned reference is captured by a nested closure. If we remove the assert, the codegen looks correct, and external projects walk this code path oblivious to the assertion.

Swift SVN r25248
2015-02-12 20:41:56 +00:00
Joe Groff
f1b97a1ead SILGen: Admit inout aliasing of polymorphic properties.
A stored property of a class may be overridden by a computed one, and a property requirement may be witnessed by a computed property, but rejecting inout aliasing in these cases isn't very helpful. Only reject cases that are definitely computed and will always behave incorrectly. Fixes rdar://problem/19633414.

Swift SVN r24986
2015-02-05 01:22:02 +00:00
John McCall
bf75beeb7a Begin formal accesses on l-value arguments immediately before
the call instead of during the formal evaluation of the argument.

This is the last major chunk of the semantic changes proposed
in the accessors document.  It has two purposes, both related
to the fact that it shortens the duration of the formal access.

First, the change isolates later evaluations (as long as they
precede the call) from the formal access, preventing them from
spuriously seeing unspecified behavior.  For example::

  foo(&array[0], bar(array))

Here the value passed to bar is a proper copy of 'array',
and if bar() decides to stash it aside, any modifications
to 'array[0]' made by foo() will not spontaneously appear
in the copy.  (In contrast, if something caused a copy of
'array' during foo()'s execution, that copy would violate
our formal access rules and would therefore be allowed to
have an arbitrary value at index 0.)

Second, when a mutating access uses a pinning addressor, the
change limits the amount of arbitrary code that falls between
the pin and unpin.  For example::

  array[0] += countNodes(subtree)

Previously, we would begin the access to array[0] before the
call to countNodes().  To eliminate the pin and unpin, the
optimizer would have needed to prove that countNodes didn't
access the same array.  With this change, the call is evaluated
first, and the access instead begins immediately before the call
to +=.  Since that operator is easily inlined, it becomes
straightforward to eliminate the pin/unpin.

A number of other changes got bundled up with this in ways that
are hard to tease apart.  In particular:

  - RValueSource is now ArgumentSource and can now store LValues.

  - It is now illegal to use emitRValue to emit an l-value.

  - Call argument emission is now smart enough to emit tuple
    shuffles itself, applying abstraction patterns in reverse
    through the shuffle.  It also evaluates varargs elements
    directly into the array.

  - AllowPlusZero has been split in two.  AllowImmediatePlusZero
    is useful when you are going to immediately consume the value;
    this is good enough to avoid copies/retains when reading a 'var'.
    AllowGuaranteedPlusZero is useful when you need a stronger
    guarantee, e.g. when arbitrary code might intervene between
    evaluation and use; it's still good enough to avoid copies
    from a 'let'.  The upshot is that we're now a lot smarter
    about generally avoiding retains on lets, but we've also
    gotten properly paranoid about calling non-mutating methods
    on vars.

    (Note that you can't necessarily avoid a copy when passing
    something in a var to an @in_guaranteed parameter!  You
    first have to prove that nothing can assign to the var during
    the call.  That should be easy as long as the var hasn't
    escaped, but that does need to be proven first, so we can't
    do it in SILGen.)

Swift SVN r24709
2015-01-24 13:05:46 +00:00
Joe Groff
a449948275 SILGen: Emit vtable thunks to handle optional variance.
If a subclass overrides methods with variance in the optionality of non-class-type members, emit a thunk to handle wrapping more optional parameters or results and force-unwrapping any IUO parameters made non-optional in the derived. For this to be useful, we need IRGen to finally pay attention to SILVTables, but this is a step on the way to fixing rdar://problem/19321484.

Swift SVN r24705
2015-01-24 05:21:26 +00:00
Chris Willmore
94bf316fc2 <rdar://problem/16937737> global NSString constants showing as NSString, not String
Import global variables of type NSString * as String instead of
NSString. Emit bridging code in SIL when such a variable is loaded.

Swift SVN r24495
2015-01-17 04:00:55 +00:00
John McCall
cae0f6e3db Add the ability for a owning addressor to return
a non-native owner.  This is required by Slice, which
will use an ObjC immutable array object as the owner
as long as all the elements are contiguous.

As part of this, I decided it was best to encode the
native requirement in the accessor names.  This makes
some of these accessors really long; we can revisit this
if we productize this feature.

Note that pinning addressors still require a native
owner, since pinning as a feature is specific to swift
refcounting.

Swift SVN r24420
2015-01-14 19:14:20 +00:00
John McCall
48f7a19d74 Enter a writeback scope before performing an assignment.
We need this in order to unpin at the correct moment.

Add an assertion that there's a scope active when pushing
an unpin writeback, as well as an assertion to ensure that
we never finish a function with writebacks active.

Swift SVN r24411
2015-01-14 02:17:27 +00:00
John McCall
3b89751b08 Merge the subscript r-value path with the l-value path.
Fixes a dumb bug where r-value subscripts would
unnecessarily emit their bases as ReadWrite.

Swift SVN r24410
2015-01-14 01:38:56 +00:00
John McCall
6a46350546 Bugfixes for the new addressor kinds.
Permit non-Ordinary accesses on references to functions,
with the semantics of devirtualizing the call if the
function is a class member.  This is important for
constructing direct call to addressors from synthesized
materializeForSet accessors: for one, it's more
performant, and for another, addressors do not currently
appear in v-tables.

Synthesize trivial accessors for addressed class members.
We weren't doing this at all before, and I'm still not
sure we're doing it right in all cases.  This is a mess.

Assorted other fixes.  The new addressor kinds seem
to work now.

Swift SVN r24393
2015-01-13 10:37:22 +00:00
John McCall
dc4431ebff Split addressors into unsafe, owning, and pinning variants.
Change all the existing addressors to the unsafe variant.

Update the addressor mangling to include the variant.

The addressor and mutable-addressor may be any of the
variants, independent of the choice for the other.

SILGen and code synthesis for the new variants is still
untested.

Swift SVN r24387
2015-01-13 03:09:16 +00:00
John McCall
f3dc58667d Improve the typing of materializeForSet callbacks to
use a thin function type.

We still need thin-function-to-RawPointer conversions
for generic code, but that's fixable with some sort of
partial_apply_thin_recoverable instruction.

Swift SVN r24364
2015-01-11 21:13:35 +00:00
Joe Groff
40d4710d61 SILGen: Fix overly restrictive assertion on lvalue-derived-from-rvalue bases.
The base value can be an address if it's any address-only type, not exclusively archetypes or existentials. Fixes nonmutating setters of address-only structs (rdar://problem/17841127), which I ran into while writing test cases for +0 self.

Swift SVN r24327
2015-01-09 21:41:08 +00:00
David Farler
3530e542ca Static class stored properties
rdar://problem/19422120

Allow static/class final stored properties to get through to the
mangled global property implementations.

Swift SVN r24303
2015-01-09 09:54:18 +00:00
John McCall
44eec49842 Change the signature of materializeForSet to return an
optional callback; retrofit existing implementations.

There's a lot of unpleasant traffic in raw pointers here
which I'm going to try to clean up.

Swift SVN r24123
2014-12-23 22:14:38 +00:00
Chris Lattner
7530f406fd Implement <rdar://problem/19290065> retains and releases of 'let' values aren't necessary
teach SILGenLValue that let values guarantee the lifetime of their value for at least
the duration of whatever expression references the let value.  This allows us to eliminate
retains/release pairs in a lot of cases, and provides more value for people to use let
instead of var.  This combines particularly well with +0 self arguments (currently just
protocol/archetype dispatches, but perhaps someday soon all method dispatches).

Thanks to John for suggesting this.



Swift SVN r24004
2014-12-18 06:14:35 +00:00
Chris Lattner
8c916c31f4 Fix <rdar://problem/19275047> Extraneous retains/releases of self are bad
This teaches SILGenLValue that base expressions referring to self can always
be done at +0, avoiding retain/release traffic.  This eliminates some hacks 
where init() needed special code, and also reduces r/r traffic in general.

Before the example in the radar silgen'd to:

sil hidden @_TFC1x4Base3foofS0_FT_T_ : $@cc(method) @thin (@owned Base) -> () {
bb0(%0 : $Base):
  debug_value %0 : $Base  // let self             // id: %1
  strong_retain %0 : $Base                        // id: %2
  // function_ref x.Base.__allocating_init (x.Base.Type)() -> x.Base
  %3 = function_ref @_TFC1x4BaseCfMS0_FT_S0_ : $@thin (@thick Base.Type) -> @owned Base // user: %5
  %4 = metatype $@thick Base.Type                 // user: %5
  %5 = apply %3(%4) : $@thin (@thick Base.Type) -> @owned Base // user: %7
  %6 = ref_element_addr %0 : $Base, #Base.f       // user: %7
  assign %5 to %6 : $*Base                        // id: %7
  strong_release %0 : $Base                       // id: %8
  strong_release %0 : $Base                       // id: %9
  %10 = tuple ()                                  // user: %11
  return %10 : $()                                // id: %11
}

now the %2 and %8 instructions are gone.



Swift SVN r23977
2014-12-17 06:09:56 +00:00
Chris Lattner
ec00695c19 remove emitSelfForDirectPropertyInConstructor, we can just use emitRValueForDecl
now that +0 expressions are plumbed through.


Swift SVN r23976
2014-12-17 05:26:16 +00:00
Chris Lattner
e824b0b9ba fix <rdar://problem/19254812> DI bug when referencing let member of a class
SILGen was emitting extraneous retains/releases on self when accessing let
properties in a class, leading to bogus diagnostics.  Fixing this just 
amounted to realizing that emitDirectIVarLValue is already safe w.r.t. +0
bases.



Swift SVN r23975
2014-12-17 05:16:56 +00:00
John McCall
dd07c8ca10 Add 'mark_dependence', which indicates that an address
or pointer depends on another for validity in a
non-obvious way.

Also, document some basic value-propagation rules
based roughly on the optimization rules for ARC.

Swift SVN r23695
2014-12-04 22:38:09 +00:00
John McCall
6aa60ed1a8 Document how WritebackScope plays into the formal access
model and remove SuppressWritebackScope.

Swift SVN r23662
2014-12-03 22:59:00 +00:00
John McCall
84cb0faaf3 Forward base rvalues directly to accessors instead of
conservatively copying them.

Also, fix a number of issues with mutating getters that
I noticed while examining and changing this code.  In
particular, stop relying on suppressing writeback scopes
during loads.

Fixes rdar://19002913, a bug where an unnecessary copy of
an array for a getter call left the array in a non-unique
state when a subsequent mutation occurred.

Swift SVN r23642
2014-12-03 05:13:11 +00:00
John McCall
a884e045c4 Bind optional l-values during their formal evaluation.
Previously, we were binding optional l-values only when
performing an access.  This meant that other evaluations
emitted before the formal access were not being
short-circuited, even if the language rules said they
should be.  For example, consider this code::

  var array : [Int]? = ...
  array?[foo()] = bar()

Neither foo nor bar should be called if the array is
actually nil, because those calls are sequenced after
the optional-chaining operator ?.

The way that we currently do this is to project out
the optional address during formal evaluation.  This
means that there's a formal access to that storage
beginning with the formal evaluation of the l-value
and lasting until the operation is complete.  That's
a little controversial, because it means that other
formal accesses during that time to the optional
storage will have unspecified behavior according to
the rules I laid out in the accessors proposal; we
should talk about it and make a decision about
whether we're okay with this behavior.  But for now,
it's important to at least get the right short-circuiting
behavior from ?.

Swift SVN r23608
2014-12-02 01:16:53 +00:00
John McCall
b0adca8115 Base prepareAccessorBaseArg's behavior in a more principled
way on whether the accessor consumes its self argument, rather
than a simple type analysis.

Swift SVN r23601
2014-12-01 23:50:00 +00:00
John McCall
df3006f785 Track the unique use of l-value components and avoid
cloning subscript values aggressively.

Swift SVN r23550
2014-11-22 06:17:35 +00:00
John McCall
c47693292f Hide the management of lvalue components inside LValue. NFC.
There's really no reason for this to be exposed.

Swift SVN r23549
2014-11-22 06:17:33 +00:00
John McCall
80d0ab3762 Evaluate subscript index expressions during the formal
evaluation of the l-value, not lazily at the first point
of access.

Swift SVN r23542
2014-11-22 00:21:29 +00:00
John McCall
5672a42e46 Ensure that LValues are uniquely used.
Swift SVN r23492
2014-11-20 23:01:15 +00:00
Joe Groff
f2658bf152 SILGen: Use select_enum_addr instead of a library function for "does optional have value" queries.
Thanks Arnold for fixing the crashes this exposed in the perf suite.

Swift SVN r22954
2014-10-26 22:34:23 +00:00
Dave Abrahams
69735ae0d0 Revert "SILGen: Use select_enum_addr instead of a library function for "does optional have value" queries."
This reverts r22828 because it was apparently causing an assertion on
the bot:

Swift SVN r22831
2014-10-19 19:54:37 +00:00
Joe Groff
05bacc48e0 SILGen: Use select_enum_addr instead of a library function for "does optional have value" queries.
This causes a regression in specialize_checked_cast_branch.swift that ought to be recovered by rdar://problem/18603827.

Swift SVN r22828
2014-10-18 22:20:53 +00:00
Chris Lattner
c79e208eac enhance SILGenFunction::emitLoad to know how to produce a loadable value
as +0 when the client requests it.  This avoids emitting some pointless
retain/releases.

This finishes up:
<rdar://problem/17207456> Unable to access dynamicType of an object in a class initializer that isn't done



Swift SVN r22736
2014-10-14 21:57:49 +00:00
Joe Groff
bbcd3af39f SILGen: Use SIL instructions to directly inject optional values.
Use init_enum_data_addr and inject_enum_addr to construct optional values instead of the injection intrinsics, further simplifying -Onone IR. This not only avoids a call but also allows the frontend to emit optional payloads in-place in more cases, eliminating a lot of stack traffic.

Swift SVN r22549
2014-10-06 20:02:18 +00:00
Joe Groff
a6a68d49cc SILGen: Avoid using a stdlib function to get optional values.
When we've already established that the optional has a value, using unchecked_take_enum_data_addr to directly extract the enum payload is sufficient and avoids a redundant call and check at -Onone. Keep using the _getOptionalValue stdlib function for checked optional wrapping operations such as "x!", so that the stdlib can remain in control of trap handling policy.

The test/SIL/Serialization failures on the bot seem to be happening sporadically independent of this patch, and I can't reproduce failures in any configuration I've tried.

Swift SVN r22537
2014-10-06 15:46:21 +00:00
Joe Groff
069ad18620 Revert "SILGen: Avoid using a stdlib function to get optional values."
This reverts commit r22533. The optimizing bots seem to have problems with it.

Swift SVN r22534
2014-10-06 04:40:58 +00:00
Joe Groff
28805f0d2a SILGen: Avoid using a stdlib function to get optional values.
When we've already established that the optional has a value, using unchecked_take_enum_data_addr to directly extract the enum payload is sufficient and avoids a redundant call and check at -Onone. Keep using the _getOptionalValue stdlib function for checked optional wrapping operations such as "x!", so that the stdlib can remain in control of trap handling policy.

Swift SVN r22533
2014-10-06 04:31:52 +00:00
John McCall
8c303ef7a6 Representational changes towards get-and-mutableAddress
properties.

The main design change here is that, rather than having
purportedly orthogonal storage kinds and has-addressor
bits, I've merged them into an exhaustive enum of the
possibilities.  I've also split the observing storage kind
into stored-observing and inherited-observing cases, which
is possible to do in the parser because the latter are
always marked 'override' and the former aren't.  This
should lead to much better consideration for inheriting
observers, which were otherwise very easy to forget about.
It also gives us much better recovery when override checking
fails before we can identify the overridden declaration;
previously, we would end up spuriously considering the
override to be a stored property despite the user's
clearly expressed intent.

Swift SVN r22381
2014-09-30 08:39:38 +00:00
Joe Groff
e7f7033d31 SILGen: Fix up handling of 'self' parameter when opaque protocol requirements are accessed through class-constrained type.
"self" needs to be materialized in these cases. We were handling methods correctly, but not property accesses. Fixes rdar://problem/18454204.

Swift SVN r22309
2014-09-27 02:11:48 +00:00
John McCall
f20225a34b Access properties and subscripts in the most efficient
semantically valid way.

Previously, this decision algorithm was repeated in a
bunch of different places, and it was usually expressed
in terms of whether the decl declared any accessor
functions.  There are, however, multiple reasons why a
decl might provide accessor functions that don't require
it to be accessed through them; for example, we
generate trivial accessors for a stored property that
satisfies a protocol requirement, but non-protocol
uses of the property do not need to use them.

As part of this, and in preparation for allowing
get/mutableAddressor combinations, I've gone ahead and
made l-value emission use-sensitive.  This happens to
also optimize loads from observing properties backed
by storage.

rdar://18465527

Swift SVN r22298
2014-09-26 06:35:51 +00:00
John McCall
6923101341 Rename "AccessKind" to "AccessSemantics". NFC.
There are a lot of different ways to interpret the
"kind" of an access.  This enum specifically dictates
the semantic rules for an access:  direct-to-storage
and direct-to-accessor accesses may be semantically
different from ordinary accesses, e.g. if there are
observers or overrides.

Swift SVN r22290
2014-09-25 23:12:39 +00:00