Commit Graph

94 Commits

Author SHA1 Message Date
Joe Shajrawi
ec1e3ee20e Add support for unconditional checked cast instruction for opaque value types + SILGen support for it 2017-02-22 16:35:46 -08:00
Joe Shajrawi
1e521c453b Add support for Init Existentials for opaque value types 2017-02-20 16:40:02 -08:00
Joe Shajrawi
df5297d877 Merge pull request #7349 from shajrawi/open_existential
Support for Open Existentials that do no take an address
2017-02-09 12:39:16 -08:00
Joe Shajrawi
c478828de7 Support for Open Existentials that do no take an address 2017-02-09 11:25:34 -08:00
Michael Gottesman
0bf0acd7f2 [ownership-verifier] Make unchecked_enum_data a propagating instruction.
This allows for an unchecked_enum_data to be either a consumed instruction or a
borrowed instruction. The reason why this makes sense in contrast to other value
projection operations like struct_extract and tuple_extract is that an enum
payload is essentially a tuple. This means that we are extracting the entire
value when we perform a struct_extract. So forwarding is viable from a semantic
perspective since if we destroy the payload, there is nothing left to destroy.

This contrasts with struct_extract and tuple_extract where we may have other
parts of the struct/tuple to destroy.

rdar://29791263
2017-02-08 19:13:54 -08:00
Michael Gottesman
49506557e3 [sil] Change SILResultInfo::getOwnershipKind to always take a CanGenericSignature.
This caused a crasher when running the ownership verifier. I don't have a test
case right now, since it happened several weeks ago.

The bug can not happen again since I eliminated the nullptr default argument.

rdar://29791263
2017-02-08 16:43:38 -08:00
Michael Gottesman
4139b716fc [ownership-verifier] Builtin "unsafeGuaranteed" returns its value at +1.
I thought this was modeling a value returned at +0. But in reality, the
instruction is created a guaranteed region by performing the following
instruction sequence:

  %1 = copy_value %0 : $GuaranteedValue
  (%2, %3) = Builtin "unsafeGuaranteed" (%1)
  ... *STUFF* ...
  Builtin "endUnsafeGuaranteed" (%2) : $Token
  destroy_value %3 : $GuaranteedValue

rdar://29791263
2017-02-06 14:59:48 -08:00
Michael Gottesman
554feff463 [semantic-sil] Create unmanaged_autorelease_value.
This is an autorelease for use with Builtin.autorelease that does not need to be
balanced as part of the ownership model.

rdar://29791263
2017-02-06 12:11:46 -08:00
Michael Gottesman
0bea544c34 [ownership-verifier] alloc_value_buffer returns a trivial value... 2017-02-05 20:18:24 -08:00
Michael Gottesman
e9f073761e [ownership-verifier] All builtins that do not return a value, actually return an empty tuple which is trivial.
rdar://29791263
2017-02-05 20:18:24 -08:00
Michael Gottesman
b0e4c68f61 [ownership-verifier] When we determine the OwnershipKind that select enum forwards, we should not look at the condition operand, just the case operands.
rdar://29791263
2017-02-05 20:18:24 -08:00
Andrew Trick
e9c559b718 [SILGen] Bootstrapping opaque values (#7113)
[NFC] Add -enable-sil-opaque-values frontend option.

This will be used to change the SIL-level calling convention for opaque values,
such as generics and resilient structs, to pass-by-value.  Under this flag,
opaque values have SSA lifetimes, managed by copy_value and destroy_value.

This will make it easier to optimize copies and verify ownership.

* [SILGen] type lowering support for opaque values.

Add OpaqueValueTypeLowering.
Under EnableSILOpaqueValues, lower address-only types as opaque values.

* [SIL] Fix ValueOwnershipKind to support opaque SIL values.

* Test case: SILGen opaque value support for Parameter/ResultConvention.

* [SILGen] opaque value support for function arguments.

* Future Test case: SILGen opaque value specialDest arguments.

* Future Test case: SILGen opaque values: emitOpenExistential.

* Test case: SIL parsing support for EnableSILOpaqueValues.

* SILGen opaque values: prepareArchetypeCallee.

* [SIL Verify] allow copy_value for EnableSILOpaqueValues.

* Test cast: SIL serializer support for opaque values.

* Add a static_assert for ParameterConvention layout.

* Test case: Mandatory SILOpt support for EnableSILOpaqueValues.

* Test case: SILOpt support for EnableSILOpaqueValues.

* SILGen opaque values: TypeLowering emitCopyValue.

* SILBuilder createLoad. Allow loading opaque values.

* SIL Verifier. Allow loading and storing opaque values.

* SILGen emitSemanticStore support for opaque values.

* Test case for SILGen emitSemanticStore.

* Test case for SIL mandatory support for inout assignment.

* Fix SILGen opaque values test case after rebasing.
2017-01-27 18:56:53 -08:00
Michael Gottesman
10ad10e8bc [ownership-verifier] mark_uninitialized is a forwarding instruction, not always trivial.
This can be seen in the case of a [rootself] variable.

rdar://29791263
2017-01-26 16:46:42 -08:00
Andrew Trick
1abeddcc5d [SILType] SILFunctionConventions API.
Separate formal lowered types from SIL types.
The SIL type of an argument will depend on the SIL module's conventions.
The module conventions are determined by the SIL stage and LangOpts.

Almost NFC, but specialized manglings are broken incidentally as a result of
fixes to the way passes handle book-keeping of aruments. The mangler is fixed in
the subsequent commit.

Otherwise, NFC is intended, but quite possible do to rewriting the logic in many
places.
2017-01-26 15:35:48 -08:00
Michael Gottesman
ea8d11f6a1 [ownership-verifier] WillThrow has a trivial tuple return value.
rdar://29791263
2017-01-25 18:03:05 -08:00
Michael Gottesman
f78ffdb941 [ownership-verifier] Ignore type dependent operands when checking for uses since we don't care about them.
This also avoids undefined behavior when we try to look up the
ParameterConvention of a type dependent operand. Since type dependent operands
do not have a convention, this causes us to hit an assertion.

rdar://29791263
2017-01-25 18:03:05 -08:00
Michael Gottesman
0a4618e93e [semantic-sil] Implement parsing and printing of ownership attributes on SILArguments.
This is only enabled when semantic sil is enabled /and/ we are not parsing
unqualified SIL.

*NOTE* To properly write tests for this, I had to rework how we verified
Branch/CondBranch insts to be actually correct (instead of pseudo-correct). I
have to put this functionality together in order to write tests.

rdar://29791263
2017-01-22 22:02:31 -08:00
Michael Gottesman
d045f20867 [semantic-sil] Add a new instruction end_borrow_argument.
I need this instruction in order to model the end of life of a guaranteed phi
arguments.

rdar://29791263
2017-01-22 20:46:25 -08:00
Michael Gottesman
682087e376 [value-ownership-kind] Builtin.unreachable /does/ return a value.
Specifically, Builtin.unreachable returns a Never value.

rdar://29791263
2017-01-22 19:54:19 -08:00
practicalswift
a9d6d8938c [gardening] Fix recently introduced typos 2017-01-22 20:40:45 +01:00
Michael Gottesman
2da18c7b47 [semantic-sil] Add special unmanaged_{retain,release}_value instructions for unpaired retain/release operations in semantic sil.
The reason why I am introducing special instructions is so I can maintain the
qualified ownership API wedge in between qualified SIL and the rest of the ARC
instructions that are pervasively used in the compiler.

These instructions in the future /could/ be extended to just take @sil_unmanaged
operands directly, but I want to maintain flexibility to take regular
non-trivial operands in the short term.

rdar://29791263
2017-01-19 13:23:08 -08:00
Michael Gottesman
d873926108 [semantic-sil] Value projections of a trivial value are trivial. Otherwise, they are guaranteed.
Also added some tests to verify that this behavior does not change.
2017-01-18 16:19:05 -08:00
Michael Gottesman
042e6481c3 [ownership-verifier] store_borrow has a result which is trivial. Treat it as such.
Originally, we were going to use the result to enforce end_borrow lifetimes for
store_borrow. But with Andy's changes, this should no longer be necessary since
@in_guaranteed and @guaranteed will both take values.

rdar://29791263
2017-01-17 19:18:40 -08:00
Michael Gottesman
4063694e85 [semantic-sil] Change SILResultInfo::getOwnershipKind(SILModule &) to also take a Generic Signature.
The way SILResultInfo::getOwnershipKind(SILModule &) was implemented before was
incorrect, resulting in getTypeLowering being called on a lowered type without a
proper generic signature being pushed on the type lowering stack. This commit
fixes that issue locally.

The real fix is in a PR that Slava is preparing that provides a getTypeLowering
that takes a SILType and a GenericSignature so that the user does not need to
perform a push/pop.

rdar://29791263
2017-01-17 18:01:17 -08:00
Michael Gottesman
2f9a90d124 [semantic-sil] objc_{existential_,}metatype_to_object should be treated as owned.
Metatypes in SIL are trivial, until we conver them to objects (mainly for cases
where one converts the metatype to an object for use in objc). Thus these
conversion functions must be treated as returning an @owned value.
2017-01-15 17:54:19 -08:00
Michael Gottesman
8a7e535b18 [semantic-sil] Fix semantics of unchecked_bitwise_cast for SILValue::getOwnershipKind().
This is used in SILGen in a few different cases:

1. trivial -> non-trivial. This should have unowned semantics since one should
retain before use.
2. non-trivial -> trivial. This should have trivial semantics.
3. trivial -> trivial. This should have trivial semantics.
4. non-trivial -> non-trivial. This should have forwarding semantics.
2017-01-15 17:46:34 -08:00
Michael Gottesman
8ede336cd2 [semantic-sil] thin_to_thick_function should be treated as returning an @owned value. 2017-01-15 17:23:28 -08:00
Hugh Bellamy
2445862e1b FIx recently introduced MSVC control path warnings 2017-01-14 12:40:41 +00:00
Michael Gottesman
6b9aba8d2c [semantic-sil] Add a constructor for making a ValueOwnershipKind from a SILArgumentConvention. 2017-01-08 00:22:54 -08:00
Michael Gottesman
f23086bfaf [semantic-sil] Change ValueOwnershipKind into a struct enum and change helper functions to be helper methods. 2017-01-08 00:22:54 -08:00
practicalswift
6d1ae2a39c [gardening] 2016 → 2017 2017-01-06 16:41:22 +01:00
Michael Gottesman
ef6462de47 [semantic-sil] Add the SILOwnershipUseVerifier behind the -enable-semantic-sil flag
This is the first verifier for SemanticSIL. The verifier is very simple and
verifies that given a SILValue V, V->getOwnershipKind() returns an ownership
kind compatible with all of V's user instructions.

This is implemented by adding a new method to SILInstruction:

    SILInstruction::verifyOperandOwnership()

This method creates an instance of the visitor OwnershipCompatibilityUseChecker
and then has the instance visit this.

The OwnershipCompatibilityUseChecker is a SILInstructionVisitor that for a given
instruction verifies that the given SILInstruction's operand SILValue's produce
ValueOwnershipKind that are compatible with the SILInstruction. The reason why
it is implemented as a visitor is to ensure that a warning is produced if a new
instruction is added and a method on the OwnershipCompatibleUseChecker isn't
added.

Keep in mind that this is just the first verifier and the full verifier (that
also verifies dataflow) is built on top of it. The reason why this separate API
to the use verifier is exposed is that exposing the checker enables us to place
an assert in SILBuilder to diagnose any places where SIL ownership is violated
immediately when the violation occurs allowing for an easy debugging experience
for compiler writers. This assert is a key tool that I am going to be using to
make SILGen conform to the SIL Ownership Model.

Again, this will be behind the -enable-semantic-sil flag, so normal development
will be unaffected by this change.

rdar://29671437
2016-12-18 21:04:26 -08:00
Michael Gottesman
4e8ff35df5 [semantic-sil] Add ValueOwnershipKind field to SILPHIArgument and split Argument creation methods into one for SILPHIArgument and another for SILFunctionArgument.
We preserve the current behavior of assuming Any ownership always and use
default arguments to hide this change most of the time. There are asserts now in
the SILBasicBlock::{create,replace,insert}{PHI,Function}Argument to ensure that
the people can only create SILFunctionArguments in entry blocks and
SILPHIArguments in non-entry blocks. This will ensure that the code in tree
maintains the API distinction even if we are not using the full distinction in
between the two.

Once the verifier is finished being upstreamed, I am going to audit the
createPHIArgument cases for the proper ownership. This is b/c I will be able to
use the verifier to properly debug the code. At that point, I will also start
serializing/printing/parsing the ownershipkind of SILPHIArguments, but lets take
things one step at a time and move incrementally.

In the process, I also discovered a CSE bug. I am not sure how it ever worked.
Basically we replace an argument with a new argument type but return the uses of
the old argument to refer to the old argument instead of a new argument.

rdar://29671437
2016-12-18 14:48:35 -08:00
Michael Gottesman
19f0f6e686 [semantic-sil] Reify the split in SILArgument in between function and block arguments via subclasses.
For a long time, we have:

1. Created methods on SILArgument that only work on either function arguments or
block arguments.
2. Created code paths in the compiler that only allow for "function"
SILArguments or "block" SILArguments.

This commit refactors SILArgument into two subclasses, SILPHIArgument and
SILFunctionArgument, separates the function and block APIs onto the subclasses
(leaving the common APIs on SILArgument). It also goes through and changes all
places in the compiler that conditionalize on one of the forms of SILArgument to
just use the relevant subclass. This is made easier by the relevant APIs not
being on SILArgument anymore. If you take a quick look through you will see that
the API now expresses a lot more of its intention.

The reason why I am performing this refactoring now is that SILFunctionArguments
have a ValueOwnershipKind defined by the given function's signature. On the
other hand, SILBlockArguments have a stored ValueOwnershipKind. Rather than
store ValueOwnershipKind in both instances and in the function case have a dead
variable, I decided to just bite the bullet and fix this.

rdar://29671437
2016-12-18 01:11:28 -08:00
Michael Gottesman
a178ac97e0 [sil-ownership] Fix some cases in SILValue::getOwnershipKind().
I fixed a few things. Now I am finally hitting cases where we have
guaranteed/owned arguments, so I really need to do the SILGen work. But this was
a good first step.

rdar://29671437
2016-12-17 01:23:52 -08:00
Michael Gottesman
4bfaef8ae0 [semantic-sil] Add a new pass that dumps out the ownership of all SILValue in a function and performs some minor checks upon them.
rdar://29671437
2016-12-16 17:53:49 -08:00
Michael Gottesman
3df612b0d6 [semantic-sil] Implement ValueOwnershipKindBuiltinVisitor to compute ValueOwnershipKind for Builtins/llvm intrinsics.
This is just a first pass through. We know that builtins can only be used in the
standard library and trivially so only in non-generic contexts.

This means that anything that I marked trivial that isn't really will be caught
and there are very very few builtins that traffic in ownership, so I can sight
verify them.

rdar://29671437
2016-12-16 17:41:43 -08:00
Michael Gottesman
7d25db5603 [semantic-sil] Implement support for most SILNodes in ValueOwnershipKindVisitor.
The remaining nodes will be defined in subsequent commits. They are:

1. SILArgument.
2. BuiltinInst.

rdar://29671437
2016-12-16 17:12:27 -08:00
Michael Gottesman
d4fb21d50f [semantic-sil] Define ValueOwnershipKind SILValue::getOwnershipKind().
The implementation will rely on a SILVisitor to ensure that we properly handle
all relevant cases. Right now, there are only stubs and we assert in all of
them.

rdar://29671437
2016-12-16 17:00:54 -08:00
Michael Gottesman
57d3f848a6 [semantic-sil] Define ValueOwnershipKind enum.
This enum models the various froms of ownership semantics that a specific
SILValue can have.

rdar://29671437
2016-12-16 16:50:49 -08:00
Michael Gottesman
26b37f1641 [gardening] Move ValueBase::RAUW from SIL.cpp => SILValue.cpp. 2016-12-14 15:44:31 -08:00
practicalswift
797b80765f [gardening] Use the correct base URL (https://swift.org) in references to the Swift website
Remove all references to the old non-TLS enabled base URL (http://swift.org)
2016-11-20 17:36:03 +01:00
Michael Gottesman
a8c4cc60e8 [gardening] Rename ValueBase::getParentBB() => getParentBlock(). 2016-11-14 00:39:47 -08:00
Michael Gottesman
c6c99141b5 [gardening] Change ValueBase::{getParentBB,getFunction,getModule}() to be const methods so in the debugger one can use it with const ValueBase. 2016-11-14 00:39:41 -08:00
Michael Gottesman
3eaa25bcd6 [sil] Complement ValueBase::getParentBB() with ValueBase::get{Function,Module}().
These APIs work just like getParentBB does, namely they attempt to cast
self to either SILInstruction/SILArgument and if the instance is one of
those classes, using the APIs on said classes to get the relevant
Function or Module. If the dynamic casts fail, then nullptr is returned.
2016-10-23 14:47:12 -07:00
Erik Eckstein
f18c8431fd SIL: remove unneeded includes 2016-01-25 15:00:49 -08:00
Erik Eckstein
b745691a38 SIL refactoring: Move some functions out of SILValue and Operand
SILValue.h/.cpp just defines the SIL base classes. Referring to specific instructions is a (small) kind of layering violation.
Also I want to keep SILValue small so that it is really just a type alias of ValueBase*.
NFC.
2016-01-25 10:37:03 -08:00
Erik Eckstein
ec172cde5b Remove SILValue::replaceAllUsesWith.
It's not needed anymore because we can use ValueBase::replaceAllUses
2016-01-21 16:04:30 -08:00
Erik Eckstein
25e52a0ba0 treat project_box as projection in SILValue and SideEffectAnalysis 2016-01-15 12:35:29 -08:00
Michael Gottesman
2f3709443d [rc-id] Make RCIdentity strip off single-pred arguments.
In a bunch of use-cases we use stripSinglePredecessorArgs to eliminate this
case. There is no reason to assume that this is being done in the caller of
RCIdentity. Lets make sure that we handle this case here.

rdar://24156136
2016-01-14 18:19:54 -08:00