Commit Graph

50 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
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
249242b08d SILGen: Always open-code materializeForSet
This improves MaterializeForSetEmitter to support emission
of static materializeForSet thunks, as well as witnesses.

This is now done by passing in a nullptr as the conformance
and requirement parameters, and adding some conditional code.

Along the way, I fixed a few limitations of the old code,
namely weak/unowned and static stored properties weren't
completely plumbed through. There was also a memory leak in
addressed materializeForSet, the valueBuffer was never freed.

Finally, remove the materializeForSet synthesis in Sema since
it is no longer needed, which fixes at least one known crash
case.
2016-01-11 19:53:16 -08:00
Zach Panzarino
e3a4147ac9 Update copyright date 2015-12-31 23:28:40 +00:00
practicalswift
d46c30a03e Fix typo: vlaue → value 2015-12-14 00:12:39 +01:00
John McCall
7e81f7e129 Fix a number of bugs with the new materializeForSet SILGen,
including one which triggered only for non-subscripts and
a number which triggered with different cases of classes.

Swift SVN r31198
2015-08-13 01:52:17 +00:00
John McCall
e56e4892a3 When performing a simple 'set' operation on an l-value,
peephole reabstraction components by applying them to
the r-value instead of materializing to a temporary
and then assigning to that.

Removes a completely unnecessary use of the getter
from simple assignments to properties or subscripts
at a different abstraction level.

Swift SVN r31197
2015-08-13 01:52:14 +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
29b49480f1 Emit writebacks during error-handling cleanup.
This requires some really ugly copies at -O0 in order to
avoid changing whether enclosing cleanups are active during
the emission of those cleanups.  (There may also be complex
situations where values are forwarded to the writeback but
the writeback cleanup can't take advantage of that --- not
sure yet.)  These should generally be trivial to clean up,
but I'm still thinking about how to avoid emitting them in
the first place.

Swift SVN r30180
2015-07-14 00:32:32 +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
John McCall
156a4c7ed0 SILGen for calls under foreign error conventions. WIP.
Swift SVN r27270
2015-04-14 02:35:54 +00:00
Doug Gregor
b8a7ebc20f Add LValue::dump(). NFC
Swift SVN r27104
2015-04-07 23:22:04 +00:00
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
Dmitri Hrybenko
61286f0260 Fix warnings produced by a newer version of Clang
Swift SVN r25257
2015-02-12 23:50:47 +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
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
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
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
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
5672a42e46 Ensure that LValues are uniquely used.
Swift SVN r23492
2014-11-20 23:01:15 +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
defaf07046 SILGen support for addressors.
Addressors now appear to pass a simple smoke-test; in
order to allow some parallel developement, I'll be
committing actual SILGen tests for this later and
separately.

Swift SVN r22243
2014-09-23 23:46:52 +00:00
John McCall
2fc49df319 Teach SILGen to take advantage of materializeForSet
accessors on non-dynamic storage.

This allows us to take advantage of dynamic knowledge
that a property is implemented as stored in order to
gain direct access to it instead of copying into a
temporary.  When a class or protocol-member property
is expensive to copy, and particularly when it's of
copy-on-write type, such direct accesses can massively
improve the performance of a program.

We are currently only able to apply this when the
property is implemented purely as stored.  A willSet
observer inherently blocks the optimization, but we
should be able to make it work even for properties
with didSet observers.  This could be done by returning
an arbitrary callback from materializeForSet instead
of returning a flag indicating whether to call the
setter.

rdar://17416120

Swift SVN r22122
2014-09-19 05:25:22 +00:00
Chris Lattner
dd803a39f8 change LValue from holding its segments as a DiverseList to holding them
as a std::vector<std::unique_ptr<PathComponent>>.   DiverseList memcpy's
around its buffer on copy and move operations.  This seems safe with all
of the current implementations of Cleanup, but is absolutely not with the
LValue PathComponents.  These things hold std::vectors, RValue (which has
an std::vector in it), and a bunch of other probably non-trivial things.

While we're in here, disable the copy constructor, since it isn't safe.
This doesn't harm mainline, but was burning me on a patch I'm working on.

When/if someone cares about performance optimizing this code, a better
approach would be to use an ilist + bump pointer allocator.



Swift SVN r21057
2014-08-06 00:50:09 +00:00
Chris Lattner
e466b4621b revert r20658, restoring us back to producing the "inout writeback to computed property"
error when detecting an inout writeback problem.


Swift SVN r20681
2014-07-29 18:12:51 +00:00
Chris Lattner
7676259ecc Fix http://swiftwtf.tumblr.com/post/91934970718/xcode-6-beta-3 to compile into the
behaviorly correct code by CSE'sing subscripts that are identical.

Also, add a dump() method to PathComponent to help visualize / debug LValue structures.



Swift SVN r20661
2014-07-29 00:27:43 +00:00
Chris Lattner
4d03ef63f7 Rip out my previous work that produced perplexing "inout writeback to
computed property" errors when SILGen could determine that there was
an inout writeback alias, and have the code instead perform CSE of the
writebacks directly.

This means that we produce more efficient code, that a lot of things
now "just work" the way users would expect, and that the still erroneous
cases now get diagnosed with the "inout arguments are not allowed to 
alias each other" error, which people have a hope of understanding.

There is still more to do here in terms of detecting identical cases,
but that was true of the previous diagnostic as well.




Swift SVN r20658
2014-07-28 23:55:14 +00:00
Chris Lattner
d473159814 Implement the start of a diagnostic to detect cases where inout aliasing violations are
introduced, as these are obvious miscompilations and clearly mystifying to our user base.

This is enough to emit diagnostics like this:

writeback_conflict_diagnostics.swift:58:70: error: inout writeback aliasing conflict detected on computed property 'c_local_struct_property'
  swap(&c_local_struct_property.stored_int, &c_local_struct_property.stored_int)
                                             ~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~
writeback_conflict_diagnostics.swift:58:33: note: concurrent writeback occurred here
  swap(&c_local_struct_property.stored_int, &c_local_struct_property.stored_int)
        ~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~

which isn't great, but is better than nothing (better wording is totally welcome!).

This doesn't handle subscripts (or many other kinds of logical lvalue) at all yet, so 
it doesn't handle the swift WTF case, but this is progress towards that.



Swift SVN r20297
2014-07-22 05:37:42 +00:00
Chris Lattner
247cc911cf add some assertions I forgot in r20281
Swift SVN r20282
2014-07-21 21:48:38 +00:00
Chris Lattner
2c1bfc6d74 add a "kind" discriminator to PathComponent to pave the way for new functionality, NFC.
Swift SVN r20281
2014-07-21 21:47:30 +00:00
John McCall
7c3f911ba3 Incremental work to try to hide the type of an AbstractionPattern.
Swift SVN r18936
2014-06-16 20:28:12 +00:00
Joe Groff
b3e9a17269 SILGen: Implement inout address conversion.
Implement lowering for the LValueToPointer and InOutConversion expressions. For the former, we emit the lvalue, then convert it to a RawPointer; for the latter, we introduce an InOutConversion scope, which suppresses any nested writeback conversion scopes.

This completes the implementation of inout address conversion, except that we don't implement reabstraction of the lvalue prior to taking its address. Simply report them unimplemented for now, since reabstraction should not come up for our immediate use case with C types.

Swift SVN r15595
2014-03-29 02:50:26 +00:00
Chris Lattner
48759e2ae1 strength reduce SILGen's handling of the "value" argument when calling
a setter to use an RValue instead of RValueSource.  NFC.


Swift SVN r14013
2014-02-17 23:35:02 +00:00
Chris Lattner
2cf6d814c7 more tidying:
- Strength reduce the interface to LogicalPathComponent::getMaterialized
   to now just return a SILValue for the address.  The full "Materialize"
   structure hasn't been needed since MaterializeExpr got removed.
 - Move 'struct Materialize' out of SILGen.h into SILGenLValues.cpp now
   that it is only used for logical property materialization.
 - Drop the dead 'loc' argument on DeallocStackCleanup.  The location is
   already specified when the cleanup is emitted.


Swift SVN r12827
2014-01-23 00:43:20 +00:00
Chris Lattner
1b86610e1a switch LValueWriteback to use ManagedValue now that it has access to it.
Swift SVN r11877
2014-01-04 00:29:36 +00:00
Chris Lattner
fdc3e70d6d move SILGenFunction::Writeback out of line and rename it to LValueWriteback
in preparation for more work on it.  NFC.


Swift SVN r11876
2014-01-04 00:17:09 +00:00
Chris Lattner
10ab56c140 mechanical transition of lvalue emission logic from trafficing in SILValue's
to trafficing in ManagedValues.  No functionality change (yet), we just needed
more management in the mix.


Swift SVN r11851
2014-01-03 05:40:30 +00:00
Chris Lattner
1b53a1bbd2 remove the concept of “settable” from SILGenLValue - all lvalues are now settable. Things that aren’t settable are rvalues now.
Swift SVN r11843
2014-01-03 01:20:15 +00:00
Joe Groff
706e7baac4 SILGen: Handle abstraction differences in stored property access.
When we produce a physical LValue with an abstraction difference, cap off the LValue with a logical "OrigToSubstComponent", which enacts the abstraction change on load or store, and introduces a writeback for the property when used in an @inout context.

Swift SVN r11498
2013-12-20 02:25:11 +00:00
Chris Lattner
5c8e9bc070 split SILGenFunction & SGFContext out to a new header, to trim down SILGen.h
to being global stuff.


Swift SVN r10926
2013-12-06 18:40:20 +00:00
John McCall
4bba9b38f8 Make several new interfaces traffic in AbstractionPatterns.
Swift SVN r10621
2013-11-21 02:19:46 +00:00
John McCall
20e58dcf93 Change the type of function values in SIL to SILFunctionType.
Perform major abstraction remappings in SILGen.  Introduce
thunking functions as necessary to map between abstraction
patterns.

Swift SVN r10562
2013-11-19 22:55:09 +00:00
John McCall
08171453da Make it a bit easier to propogate expressions around instead
of having to lower to an RValue.

This is valuable because we can often emit an expression to a
desired abstraction level more efficiently than just emitting
it to minimal abstraction and then generalizing.

Swift SVN r10455
2013-11-14 05:25:06 +00:00
Joe Groff
ad379929fd SILGen: Don't apply the copy_addr peephole to initializations from computed lvalues.
Materializing the result of the getter isn't worth it, I don't think.

Swift SVN r9351
2013-10-15 17:20:48 +00:00
Jordan Rose
e05c03d5bc Standardize terminology for "computed", "stored", "variable", and "property".
These are the terms sent out in the proposal last week and described in
StoredAndComputedVariables.rst.

variable
  anything declared with 'var'
member variable
  a variable inside a nominal type (may be an instance variable or not)
property
  another term for "member variable"
computed variable
  a variable with a custom getter or setter
stored variable
  a variable with backing storage; any non-computed variable

These terms pre-exist in SIL and IRGen, so I only attempted to solidify
their definitions. Other than the use of "field" for "tuple element",
none of these should be exposed to users.

field
  a tuple element, or
  the underlying storage for a stored variable in a struct or class
physical
  describes an entity whose value can be accessed directly
logical
  describes an entity whose value must be accessed through some accessor

Swift SVN r8698
2013-09-26 18:50:44 +00:00
John McCall
6aae6402ba Refactor in support of semantic vs. storage type differences.
Swift SVN r7939
2013-09-05 06:44:44 +00:00
Anna Zaks
422ab63a38 [SIL] Add proper location when copying a ManagedValue.
Swift SVN r7925
2013-09-05 00:01:54 +00:00
Stephen Lin
8d90466523 Reorganize SIL source tree: move lib/SIL/SILGen -> lib/SILGen, move lib/SIL/Passes -> lib/SILPasses, add lib/SILPasses/Utils
Swift SVN r7246
2013-08-14 23:47:29 +00:00