Commit Graph

442 Commits

Author SHA1 Message Date
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
5e86341f79 Add a verifier check that we never upcast from an address -> an object or vis-a-versa. Discovered while making +0 self tests.
Swift SVN r27239
2015-04-12 21:10:48 +00:00
Doug Gregor
997136962e Always open existential types in the type checker.
Consistently open all references into existentials into
opened-existential archetypes within the constraint solver. Then,
during constraint application, use OpenExistentialExprs to record in
the AST where an existential is opened into an archetype, then use
that archetype throughout the subexpression. This simplifies the
overall representation, since we don't end up with a mix of operations
on existentials and operations on archetypes; it's all archetypes,
which tend to have better support down the line in SILGen already.

Start simplifying the code in SILGen by taking away the existential
paths that are no longer needed. I suspect there are more
simplifications to be had here.

The rules for placing OpenExistentialExprs are still a bit ad hoc;
this will get cleaned up later so that we can centralize that
information. Indeed, the one regression in the compiler-crasher suite
is because we're not closing out an open existential along an error
path.

Swift SVN r27230
2015-04-11 03:20:22 +00:00
Joe Groff
15bf3123af SIL: Tighten verifier checks to prevent unowned/unmanaged function types.
These aren't yet supported.

Swift SVN r27146
2015-04-08 23:55:42 +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
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
Joe Groff
3bfbd3c8b7 SIL: Relax verifier constraints on unowned operations.
The condition we want is that the operand type has reference semantics (or is an Optional of a reference type), not necessarily that it is a single retainable pointer. The latter disallows class existentials and functions from being unowned. Fixes rdar://problem/20374500.

Swift SVN r26883
2015-04-02 19:30:56 +00:00
John McCall
6d8fff9c06 Parsing and basic structure of try_apply. Not yet properly
threaded into IRGen; tests to follow when that's done.

I made a preliminary effort to make the inliner do the
right thing with try_apply, but otherwise tried to avoid
touching the optimizer any more than was required by the
removal of ApplyInstBase.

Swift SVN r26747
2015-03-31 02:41:03 +00:00
Joe Groff
751724c3fa SILGen: Special-case NSError to ErrorType erasure.
We want this to be a "toll-free" adoption of the class reference without requiring a second box allocation.

Swift SVN r26711
2015-03-30 04:52:41 +00:00
John McCall
1ffb87bb1f Implement a 'throw' instruction in SIL.
Swift SVN r26668
2015-03-28 02:00:20 +00:00
Doug Gregor
a8d1532e28 Use the native method calling convention for methods in protocol extensions.
The witness calling convention is for dispatching via the witness
table, which is not the case for protocol extensions. At some point,
it might make sense for protocol extensions to use the witness calling
convention, but for now the native method calling convention
suffices.

Swift SVN r26616
2015-03-27 00:10:20 +00:00
Doug Gregor
3d77855b31 Start allowing extensions of protocol types.
Remove the semantic restrictions that prohibited extensions of
protocol types, and start making some systematic changes so that
protocol extensions start to make sense:
  - Replace a lot of occurrences of isa<ProtocolDecl> and
    dyn_cast<ProtocolDecl> on DeclContexts to use the new
    DeclContext::isProtocolOrProtocolExtensionContext(), where we want
    that behavior to apply equally to protocols and protocol extensions.
  - Eliminate ProtocolDecl::getSelf() in favor of
    DeclContext::getProtocolSelf(), which produces the appropriate
    generic type parameter for the 'Self' of a protocol or protocol
    extension. Update all of the callers of ProtocolDecl::getSelf()
    appropriately.
  - Update extension validation to appropriately form generic
    parameter lists for protocol extensions.
  - Methods in protocol extensions always use the witnesscc calling
  convention.

At this point, we can type check and SILGen very basic definitions of
protocol extensions with methods that can call protocol requirements,
generic free functions, and other methods within the same protocol
extension.

Regresses four compiler crashers but improves three compiler
crashers... we'll call that "progress"; the four regressions all hit
the same assertion in the constraint system that will likely be
addressed as protocol extensions starts working.

Swift SVN r26579
2015-03-26 04:50:51 +00:00
John McCall
35b7db3ae1 Parsing support for error results from SILFunctionType.
Swift SVN r26566
2015-03-26 00:01:32 +00:00
Joe Groff
6cb75be0ca SIL: Add verifier checks for existential_box instructions.
Swift SVN r26310
2015-03-19 19:31:59 +00:00
Joe Groff
7f886c924b SIL: Make 'isReferenceCounted' a bit on TypeLowering.
This allows types to be lowered as scalar reference-counted types without requiring them to have AST-level reference semantics. For now, put in a staging assertion to ensure isReferenceCounted == hasReferenceSemantics to make sure we set the bit properly everywhere.

Swift SVN r26238
2015-03-17 21:51:06 +00:00
Joe Groff
d50b2826e2 SIL verifier: Don't allow open_existential_ref to apply to metatypes anymore.
Swift SVN r26177
2015-03-16 04:41:26 +00:00
Chris Lattner
4f708c049b fix const correctness and standardize on names for the successor list of
TerminatorInsts.  Now you can walk over the successor list of a terminator
and actually modify the SILSuccessor directly, allowing better CFG
transformations.  NFC.




Swift SVN r26140
2015-03-14 17:52:27 +00:00
Roman Levenstein
1010148f53 [sil-verify] Fix checking of casts involving metatypes.
sil-verifier was using SIL types to check metatypes. But metatypes, especially metatypes of metatypes, use AST types. Therefore checks should be done based on AST types.

rdar://20153162

Swift SVN r26111
2015-03-13 21:13:37 +00:00
Roman Levenstein
51c69ae75e [sil-verify] Remove the requirement that "from" and "to" types of a cast should not be the same.
We discussed it with Joe and this requirement does not make sense, especially if casts are produced by one pass, but optimized by another one. Moreover, having equal from and to types is eventually not very efficient, but is not semantically wrong. Such casts can often be produced by the inliner or specialised.

Swift SVN r26110
2015-03-13 21:13:36 +00:00
Joe Groff
84e2d4e8ba SIL.rst: Add discussion of existential container representations.
And adjust the names of ExistentialRepresentation cases to match the existing nomenclature instead of needlessly inventing new names.

Swift SVN r26069
2015-03-12 22:26:00 +00:00
Joe Groff
5f6c7180a0 SIL: Provide SILType APIs to track the representation of existential types.
Give SILType 'getPreferredExistentialRepresentation' and 'canUseExistentialRepresentation' methods, which track what types of container particular existential types may use--fixed-sized, class-constrained, metatype, or box. Allow for existentials to use a specialized representation for certain known concrete types by allowing these methods to take a concrete type. NFC yet, except to replace some ad-hoc verifier conditions with canUseExistentialRepresentation checks where appropriate.

Swift SVN r26062
2015-03-12 20:54:06 +00:00
Dave Abrahams
e88572391e Revert "Make sure that we do not try to create shared versions of stdlib_binary_only functions when specializing."
This reverts commit r26001; it broke validation-tests/stdlib/String.swift

Swift SVN r26005
2015-03-12 00:52:38 +00:00
Michael Gottesman
d88f3767c3 Make sure that we do not try to create shared versions of stdlib_binary_only functions when specializing.
This can only happen in the closure specializer and the generic
specializer since all other specializations either copy the linkage of
the original function (function signature opts) or clone closures/thunks
which have shared linkage.

I put in a verifier check that makes sure we do not create shared
versions of these functions. The real problem has to do with serializing
these sorts of functions, but since we always serialize shared
functions, it makes sense to just ban it.

rdar://20082696

Swift SVN r26001
2015-03-11 23:18:56 +00:00
Roman Levenstein
d674034113 [sil-combine] Add a peephole to remove any code after an "unreachable" instruction.
This is useful for cleaning-up the code generated by intermediate transformations, e.g. for cases where we perform folding of always failing casts into traps followed by an unreachable instruction. I'll make use of it in my subsequence commits.

Swift SVN r25988
2015-03-11 20:34:43 +00:00
Michael Gottesman
5c9d7299b3 Put all of SILFunction verification into the SILVerifier.
This centralizes the code and also puts all of the verification code behind a
pretty stack trace.

Swift SVN r25984
2015-03-11 19:40:20 +00:00
Mark Lacey
92a85f00f9 Move isClassOrClassMetatype into SILType.h.
This will be useful for further devirtualizer clean-up.

Swift SVN r25974
2015-03-11 08:25:48 +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
Erik Eckstein
8795202be3 Removed a wrong check for init_existential in the SIL verifier.
Fixes rdar://problem/20062883



Swift SVN r25878
2015-03-09 08:28:38 +00:00
Arnold Schwaighofer
5113456d40 Verifier: The alloc_stack stack does not need to be consistent on purely unreachable paths.
When we clone loops with exit blocks that are unreachable the verifier
complained before this patch.

loop:
  = alloc_stack
  cond_br %overflow_cond, loop_latch, unreachable_only_exit

loop_latch:
  cond_br  loop, exit

unreachable_only_exit:
  ..
  unreachable

We don't care about stack deallocation on paths that must lead to
unreachable.

The alternative to weaken the verifier for cases like this would be to clone
loop exits that are guaranteed to lead to unreachable which would be a waste of
space.

Swift SVN r25732
2015-03-04 01:06:25 +00:00
Joe Groff
5559baf507 Tweak the SIL verifier to dump the whole function on verifier failures.
And use requireSameType in some places where it makes sense.

Swift SVN r25451
2015-02-21 01:33:41 +00:00
Joe Groff
372936efb1 SIL: Verify that "struct" instructions aren't used to build structs with unreferenceable storage.
We don't have enough structural information about these structs to build a value from elements. Part of rdar://problem/19807099.

Swift SVN r25367
2015-02-18 02:40:16 +00:00
Joe Groff
6e59195933 SILGen: Don't emit vtable entries for willSet/didSet.
Observers shouldn't ever be ABI. Fixes rdar://problem/19777115.

Swift SVN r25119
2015-02-10 05:12:51 +00:00
Michael Gottesman
7acd7def16 [verifier] Make sure that applies of no-return functions have an unreachable as the next instruction.
This makes it easy to find no-return functions since one must iterate
through all BB with unreachable terminators and check the previous
instruction.

Swift SVN r25004
2015-02-05 19:11:33 +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
Joe Groff
842d8aee03 Recommit r24666, r24667 with a fix for Optional object reference variance.
Give our assumption that T: class and Optional<T> are ABI-compatible a pass.

Swift SVN r24677
2015-01-23 04:15:34 +00:00
Dmitri Hrybenko
e90e1c6ab5 Revert "SIL verifier: Require convert_function not to change function ABI."
This reverts commit r24666.  It broke the performance benchmarks.

Swift SVN r24672
2015-01-23 03:09:54 +00:00
Dmitri Hrybenko
2b6d1989da Revert "SIL Verifier: Verify that vtable entries match the ABI of their base."
This reverts commit r24667.  It broke the performance benchmarks.

Swift SVN r24671
2015-01-23 03:09:53 +00:00
Joe Groff
53d6243a53 SIL Verifier: Verify that vtable entries match the ABI of their base.
This exposes a problem with the sil_vtable parser, that it can't differentiate overloads (rdar://problem/19572342), and breaks a test that exposes the fact we don't reabstract overrides that have a less abstract native calling convention than their base (rdar://problem/19572664).

Swift SVN r24667
2015-01-23 01:15:19 +00:00
Joe Groff
67009e492c SIL verifier: Require convert_function not to change function ABI.
Write up a requireABICompatibleFunctionTypes check to make sure two function types share the same ABI. The real target here is to be able to apply this to vtable entries in order to uncover variance bugs like rdar://problem/19321484, but convert_function is a convenient testbed.

Swift SVN r24666
2015-01-23 01:15:00 +00:00
Mark Lacey
6170bbb0ed Minor code clean-up.
Primarily replacing getParent()->getParent() with getFunction().

Swift SVN r24596
2015-01-21 06:35:13 +00:00
Chris Lattner
0f08249883 fix isSingleBlockUsage to work in the face of dead code. It is used to reject
obviously broken cases (when an alloc stack has its dealloc_stack in the same block
as the allocation, it checks that there are no uses in other blocks) but this isn't
correct: uses in dead blocks are fine.  Just ignore them.


Swift SVN r24357
2015-01-11 00:25:17 +00:00
Mark Lacey
138bff2b1d Replace workaround for rdar://problem/19264143 with a real fix.
Rather than dropping generics on the cloned capture, retain them and
create the new partial apply with substitutions.

Thanks to Erik for some initial debugging, the verifier improvement, and
a small test case to start with!

Swift SVN r24024
2014-12-19 01:14:27 +00:00
John McCall
5c8fbc704c Add SIL instructions to convert between thin functions
and raw pointers.

Swift SVN r23992
2014-12-17 22:23:15 +00:00
John McCall
169e4fe319 Add Builtin.UnsafeValueBuffer, which provides opaque
storage for arbitrary values.

A buffer doesn't provide any way to identify the type of
value it stores, and so it cannot be copied, moved, or
destroyed independently; thus it's not available as a
first-class type in Swift, which is why I've labelled
it Unsafe.  But it does allow an efficient means of
opaquely preserving information between two cooperating
functions.  This will be useful for the adjustments I
need to make to materializeForSet to support safe
addressors.

I considered making this a SIL type category instead,
like $@value_buffer T.  This is an attractive idea because
it's generally better-typed.  The disadvantages are that:
- it would need its own address_to_pointer equivalents and
- alloc_stack doesn't know what type will be stored in
  any particular buffer, so there still needs to be
  something opaque.

This representation is a bit gross, but it'll do.

Swift SVN r23903
2014-12-13 01:27:12 +00:00
Erik Eckstein
3b9805b246 Disallow cond_br with identical destinations.
It avoids generation of llvm phi nodes with identical predecessors and differing values.
This change replaces my previous fix of this problem in r23580, where I handled it in IRGen.

There were some discussions about it with the conclusion that it's better to just disallow such cond_br instructions in SIL.
It makes the life easier for some SIL optimizations which can't deal with cond_br with identical destinations.

The original radar is <rdar://problem/18568272> Swift compiler fails with "PHI node has multiple entries for the same basic block with different incoming values!"



Swift SVN r23861
2014-12-11 15:03:49 +00:00
Mark Lacey
1859e5da52 Fix witness_method verification issue.
Verify that witness_method instructions with a lookup type that is an
opened archetype have the optional operand that represents the
open_existential instruction.

I ran into this working supporting substitution of existential types in
mandatory inlining (rdar://problem/17769717).

Swift SVN r23665
2014-12-03 23:34:45 +00:00
Mark Lacey
7335adb438 Fix another typo.
Swift SVN r23645
2014-12-03 06:08:10 +00:00
Michael Gottesman
652b5aeeea Add a verifier check to make sure that functions that have a function signature that should have a self parameter coming out of SILGen actually do so.
This was discovered while I was refactoring SILGen convention code.

Swift SVN r23611
2014-12-02 05:29:54 +00:00