Commit Graph

136 Commits

Author SHA1 Message Date
Slava Pestov
fd256e70e3 EmitGuaranteedSelf is always on now
NFC except for removal of flag

Swift SVN r28427
2015-05-11 18:21:37 +00:00
John McCall
58b5e1dc0f Implement error handling in protocol witness and
reabstraction thunks.

rdar://20782111

Swift SVN r28075
2015-05-02 04:37:30 +00:00
Joe Groff
21890883ac SILGen: Remove obsolete 'stripInputTupleLabels' hack from witness generation.
Keyword labels are now required to match between a protocol requirement and witness, so this hack is dead code.

Swift SVN r27536
2015-04-21 20:12:49 +00:00
Chris Lattner
d874bc8592 fix comment. NFC.
Swift SVN r27456
2015-04-18 22:13:33 +00:00
Michael Gottesman
38b1818b1b Convert code to use a covered switch. NFC.
Swift SVN r27310
2015-04-15 04:05:41 +00:00
Michael Gottesman
0bef564982 Loosen an assert.
The translation code already handles translating in_guaranteed arguments
correctly. The assert was just too conservative.

<rdar://problem/20522878>

Swift SVN r27307
2015-04-15 02:40:37 +00:00
Michael Gottesman
75ea31dba9 Turn on +0 self by default.
The only caveat is that:

1. We do not properly recognize when we have a let binding and we
perform a guaranteed dynamic call. In such a case, we add an extra
retain, release pair around the call. In order to get that case I will
need to refactor some code in Callee. I want to make this change, but
not at the expense of getting the rest of this work in.

2. Some of the protocol witness thunks generated have unnecessary
retains or releases in a similar manner.

But this is a good first step.

I am going to send a large follow up email with all of the relevant results, so
I can let the bots chew on this a little bit.

rdar://19933044

Swift SVN r27241
2015-04-12 22:23:37 +00:00
Michael Gottesman
a79b97f0a1 Fix typo. NFC.
Swift SVN r27153
2015-04-09 01:38:12 +00:00
Joe Groff
ad0d20c07a Fold "AbstractCC" into SILFunctionType::Representation.
These aren't really orthogonal concerns--you'll never have a @thick @cc(objc_method), or an @objc_block @cc(witness_method)--and we have gross decision trees all over the codebase that try to hopscotch between the subset of combinations that make sense. Stop the madness by eliminating AbstractCC and folding its states into SILFunctionTypeRepresentation. This cleans up a ton of code across the compiler.

I couldn't quite eliminate AbstractCC's information from AST function types, since SIL type lowering transiently created AnyFunctionTypes with AbstractCCs set, even though these never occur at the source level. To accommodate type lowering, allow AnyFunctionType::ExtInfo to carry a SILFunctionTypeRepresentation, and arrange for the overlapping representations to share raw values.

In order to avoid disturbing test output, AST and SILFunctionTypes are still printed and parsed using the existing @thin/@thick/@objc_block and @cc() attributes, which is kind of gross, but lets me stage in the real source-breaking change separately.

Swift SVN r27095
2015-04-07 21:59:39 +00:00
Joe Groff
4821f594bb SIL: Separate SILFunctionType::Representation and ExtInfo from AST FunctionTypes.
The set of attributes that make sense at the AST level is increasingly divergent from those at the SIL level, so it doesn't really make sense for these to be the same. It'll also help prevent us from accidental unwanted propagation of attributes from the AST to SIL, which has caused bugs in the past. For staging purposes, start off with SILFunctionType's versions exactly the same as the FunctionType versions, which necessitates some ugly glue code but minimizes the potential disruption.

Swift SVN r27022
2015-04-05 17:04:55 +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
Chris Lattner
79ed57f9f2 standardize naming of tuples and tuple patterns on "elements".
Previously some parts of the compiler referred to them as "fields",
and most referred to them as "elements".  Use the more generic 'elements'
nomenclature because that's what we refer to other things in the compiler
(e.g. the elements of a bracestmt).

At the same time, make the API better by providing "getElement" consistently
and using it, instead of getElements()[i].

NFC.



Swift SVN r26894
2015-04-02 20:23:49 +00:00
John McCall
35b7db3ae1 Parsing support for error results from SILFunctionType.
Swift SVN r26566
2015-03-26 00:01:32 +00:00
Mark Lacey
1f23ff27bb Remove the transparent bit from apply instructions.
We no longer need or use it since we can always refer to the same bit on
the applied function when deciding whether to inline during mandatory
inlining.

Resolves rdar://problem/19478366.

Swift SVN r26534
2015-03-25 08:36:34 +00:00
Michael Gottesman
3c2216b115 [+0 self] Add the deallocating parameter convention.
The deallocating parameter convention is a new convention put on a
non-trivial parameter if the caller function guarantees to the callee
that the parameter has the deallocating bit set in its object header.

This means that retains and releases do not need to be emitted on these
parameters even though they are non-trivial. This helps to solve a bug
in +0 self and makes it trivial for the optimizer to perform
optimizations based on this property.

It is not emitted yet by SILGen and will only be put on the self
argument of Deallocator functions.

Swift SVN r26179
2015-03-16 07:51:11 +00:00
Joe Groff
5fa20867e9 SILGen: Implement thunking for C function pointer conversions.
If we have a C function pointer conversion, generate a thunk using the same logic we use for ObjC method thunks, and emit a pointer to that thunk as the C function pointer value. (This works for nongeneric, nonmember functions; generics will additionally need to apply generic parameters within the thunks. Static functions would need to gather the metatype as well.)

Swift SVN r25653
2015-03-01 06:18:58 +00:00
Adrian Prantl
45544727ac Mark reabstraction thunk helpers as auto-generated.
Fixes <rdar://problem/18670152> Stack trace is a lie.

Swift SVN r25362
2015-02-18 00:46:25 +00:00
Joe Groff
b0fabcef7b Sema: Force dynamic materializeForSet accessors to be statically dispatched.
The materializeForSet accessor for a `dynamic` property needs to dynamically invoke the getter and setter of the property in order to allow for runtime modification, so it doesn't need to be dynamically dispatched itself. If the property came from an imported ObjC class, then we can't dynamically dispatch it without polluting the selector namespace. Introduce a new 'ForcedStaticDispatch' bit and set it in order to force `dynamic` materializeForSet accessors to be statically dispatched. (They can't be `final` because it's legal to override a dynamic property.) If the property came from ObjC, register materializeForSet as an external declaration so it gets generated by SIL. Fixes rdar://problem/18706056.

Swift SVN r24930
2015-02-04 00:46:24 +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
Mark Lacey
5826c34439 Fix isTransparent() in SILGenPoly.cpp.
It was checking the current function's transparency rather than the
referenced function's transparency.

Fixing this did not seem to have a measureable impact on the performance
of -Onone code.

Fixes rdar://problem/19477711.

Swift SVN r24452
2015-01-15 20:53:15 +00:00
Joe Groff
43dfdab9df SILGen: Balance the retain counts of guaranteed reabstraction and witness thunk params.
For now, conservatively retain them so they can be forwarded as +1 values, until we can push +0 rvalue support down into the forwarding and function application implementations. Preserve the old, technically incorrect behavior unless -enable-guaranteed-self is enabled to avoid punishing -Onone code until guaranteed self is enabled. (With guaranteed self and +0 rvalue support, the overhead this introduces should be avoidable in the code generator.)

Swift SVN r24374
2015-01-12 22:14:23 +00:00
Chris Lattner
4618e7d2fb remove some now-dead code. This computation is now done based on
SIL function types, not on AST types.


Swift SVN r23865
2014-12-11 20:43:37 +00:00
Chris Lattner
d844671de5 Revise our AST to represent the self argument of a non-mutating protocol method
as passing self by value, not by inout.  This is the correct representation at
the AST level, and we now lower self references as the new @in_guaranteed
parameter convention.  This allows SIL clients (like DI) to know that a nonmutating
protocol method does not mutate the pointee passed into the method.

This fixes:
<rdar://problem/19215313> let properties don't work with protocol method dispatch
<rdar://problem/15821762> Self argument of generic curried nonmutating instance methods is inout



Swift SVN r23864
2014-12-11 20:41:37 +00:00
Chris Lattner
5d8613c7c9 Introduce a new "Indirect_In_Guaranteed" SIL parameter convention. This
isn't used yet, but will be for modeling the self argument passed to an 
address-only witness implementation.   NFC since all this code is dead :-)



Swift SVN r23857
2014-12-11 01:41:29 +00:00
Michael Gottesman
1afc987739 Refactor the SILArgument API on SILBasicBlock so we can insert bb arguments anywhere in the argument list. Also clean up the API names so that they all match.
Swift SVN r23543
2014-11-22 00:24:40 +00:00
Joe Groff
e7287289e4 SILGen: Redispatch protocol class method and init requirements.
Fixes a bug where dynamic dispatches of class methods or initializers through generic interfaces didn't redispatch to subclasses. Also fix up some logic errors noticed by inspection.

Swift SVN r22945
2014-10-26 04:37:59 +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
Erik Eckstein
c16c510167 Set SILLinkage according to visibility.
Now the SILLinkage for functions and global variables is according to the swift visibility (private, internal or public).

In addition, the fact whether a function or global variable is considered as fragile, is kept in a separate flag at SIL level.
Previously the linkage was used for this (e.g. no inlining of less visible functions to more visible functions). But it had no effect,
because everything was public anyway.

For now this isFragile-flag is set for public transparent functions and for everything if a module is compiled with -sil-serialize-all,
i.e. for the stdlib.

For details see <rdar://problem/18201785> Set SILLinkage correctly and better handling of fragile functions.

The benefits of this change are:
*) Enable to eliminate unused private and internal functions
*) It should be possible now to use private in the stdlib
*) The symbol linkage is as one would expect (previously almost all symbols were public).

More details:

Specializations from fragile functions (e.g. from the stdlib) now get linkonce_odr,default
linkage instead of linkonce_odr,hidden, i.e. they have public visibility.
The reason is: if such a function is called from another fragile function (in the same module),
then it has to be visible from a third module, in case the fragile caller is inlined but not
the specialized function.

I had to update lots of test files, because many CHECK-LABEL lines include the linkage, which has changed.

The -sil-serialize-all option is now handled at SILGen and not at the Serializer.
This means that test files in sil format which are compiled with -sil-serialize-all
must have the [fragile] attribute set for all functions and globals.

The -disable-access-control option doesn't help anymore if the accessed module is not compiled
with -sil-serialize-all, because the linker will complain about unresolved symbols.

A final note: I tried to consider all the implications of this change, but it's not a low-risk change.
If you have any comments, please let me know.



Swift SVN r22215
2014-09-23 12:33:18 +00:00
Joe Groff
232cec4b61 SIL: Rename TypeConverter::getInterfaceType{In,OutOf}Context
It extracts an interface type out of a contextual type, but the name made it sound like it put it into a context. NFC.

Swift SVN r22188
2014-09-22 21:56:47 +00:00
Doug Gregor
78a28c01eb Materialize temporaries properly when translating between init? and init! in witnesses.
Thanks, Joe!


Swift SVN r21854
2014-09-10 22:51:02 +00:00
Doug Gregor
e762e73bf7 Handle failability differences in initializer witnesses.
A failable initializer can satisfy a failable initializer requirement
(regardless of whether either is spelled with init! or
init?). Handle the optional translation within the witness.

Additionally, a failable initializer requirement spelled with
"init!" can satisfy a non-failable initializer requirement, in which
case we force the optional within the witness.

Swift SVN r21812
2014-09-09 21:47:26 +00:00
Doug Gregor
a8a09fd2c2 In a protocol witness thunk for an 'init!' witness of an 'init' requirement, force the optional.
Swift SVN r21793
2014-09-08 23:51:14 +00:00
Joe Groff
620170e795 SILGen: Handle 'init' witnesses less failable than their requirement.
This fix should also extend to the general case of witnesses that return non-optional for optional-returning requirements, when/if we support that.

Swift SVN r21721
2014-09-04 22:24:16 +00:00
Doug Gregor
7cae0cfc8e Remove -enable-dynamic and its language option; it's always on anyway.
Swift SVN r21354
2014-08-21 15:15:12 +00:00
Joe Groff
623aba1786 Encapsulate Substitution's state.
Expose Substitution's archetype, replacement, and conformances only through getters so we can actually assert invariants about them. To start, require  replacement types to be materializable in order to catch cases where the type-checker tries to bind type variables to lvalue or inout types, and require the conformance array to match the number of protocol conformances required by the archetype. This exposes some latent bugs in the test suite I've marked as failures for now:

- test/Constraints/overload.swift was quietly suffering from <rdar://problem/17507421>, but we didn't notice because we never tried to codegen it.
- test/SIL/Parser/array_roundtrip.swift doesn't correctly roundtrip substitutions, which I filed as <rdar://problem/17781140>.

Swift SVN r20418
2014-07-23 18:00:38 +00:00
Joe Groff
45eec9a2e9 Remove 'interface' from the method names of SILFunctionType.
SILFunctionTypes are always interface types now. NFC.

Swift SVN r19952
2014-07-14 22:03:46 +00:00
Joe Groff
893ca42780 SILGen: Have protocol witnesses honor 'dynamic'.
The witness table entry needs to dispatch through the ObjC entry point if the witness is dynamic. Slot this into the existing code path by consing up a small transparent thunk to exercise the existing code paths for adjusting calling convention from ObjC to Swift.

Swift SVN r19864
2014-07-11 23:13:51 +00:00
John McCall
ace9e8e406 Allow cleanups to declare that they should not be
completely destroyed when forwarded.

Also, make forwarding a cleanup a first-class operation
on cleanups, rather than setting the cleanup state directly.

Swift SVN r19332
2014-06-30 11:55:24 +00:00
John McCall
48d6a833a5 SILGen unconditional dynamic casts using the new
unconditional_dynamic_cast_addr instruction.

Also, fix some major semantic problems with the
existing specialization of unconditional dynamic
casts by handling optional types and being much
more conservative about deciding that a cast is
infeasible.

This commit regresses specialization slightly by
failing to turn indirect dynamic casts into scalar
ones when possible; we can fix that easily enough
in a follow-up.

Swift SVN r19044
2014-06-20 07:47:03 +00:00
Joe Groff
692ef04b72 SILGen: Always dispatch Swift class witnesses natively even when @objc.
Maybe we should wrap the @objc dispatch in another thunk in order to account for ObjC dynamic magic, but this is the shortest path to getting proper class dispatch behavior for witnesses in all cases and close <rdar://problem/14620454>.

Swift SVN r17757
2014-05-09 03:28:31 +00:00
Joe Groff
fcd0816179 SILGen: Emit witness function refs through 'emitGlobalFunctionRef'.
This ensures that if the witness is a foreign-to-native, curry, or other lazily-generated thunk, that the thunk gets generated. This fixes natively-ObjC witnesses to Swift protocols.

Swift SVN r17739
2014-05-09 00:14:43 +00:00
Joe Groff
aeb607ad5b Fix latent SIL verification errors.
r15824 overrode SILVerifier::visitSILBasicBlock without calling up to the super definition, causing us to never actually verify any instructions for the past three weeks. Awesome. Patch up the latent bugs that have crept in, except for three devirtualizer tests that fail. This doesn't reenable the verifier because I don't want to cause crashes until all the regressions have been cleared up.

Swift SVN r17121
2014-04-30 22:33:46 +00:00
Michael Gottesman
61d7421fba [sil-gen] Emit class methods for in protocol methods for classes so that
inherited conformances can result in virtual calls to overriding
subclass methods.

Previously given the following swift code:

  protocol P {
    func x()
  }
  class B : P {
    func x() { ... }
  }
  class B2 : B {
    func x() { ... }
  }
  func doX<T : P>(t : T) {
    t.x()
  }

  var b2 = B2()
  doX(b2)

We would have b2 reference the protocol method for B.x. But since the
protocol method B.x would have a direct function reference to B.x, we
would not get the correct behavior that doX(b2) should invoke B2.x. This
is fixed by changing SILGen to emit class_method calls in protocol
methods for classes.

Swift SVN r16645
2014-04-22 04:45:09 +00:00
Joe Groff
d90f9d46ae SILGen: Lifetime-extend the "self" parameters of inner-pointer methods.
Do this the lazy way, just autoreleasing "self" after the call. A future optimization pass may be able to eliminate this autorelease when it recognizes the lifetime of the derived value, but that's not immediately necessary.

Swift SVN r16635
2014-04-22 00:25:17 +00:00
Joe Groff
c73acc67f2 SIL: Add an "UnownedInnerPointer" result convention.
This will represent the return convention of imported __attribute__((objc_returns_inner_pointer)) methods. Leave it unimplemented for now until we can autorelease things sanely.

Swift SVN r16628
2014-04-21 21:57:13 +00:00
Joe Groff
e245f9ee89 SILGen: Thunk blocks into native Swift functions when bridging into Swift.
Emit Swift thunk functions to bridge blocks into native function values. On entry into @objc thunks, copy_block blocks to ensure they've been lifted off the stack.

Swift SVN r16177
2014-04-10 22:35:42 +00:00
Joe Groff
8adaab0233 Fold ExtInfo::isThin and ::isBlock into a "Representation" enum.
These bits are orthogonal to each other, so combine them into one, and diagnose attempts to produce a type that's both. Spot-fix a bunch of places this revealed by inspection that we would have crashed in SILGen or IRGen if blocks were be handled.

Swift SVN r16088
2014-04-09 00:37:26 +00:00