Commit Graph

218 Commits

Author SHA1 Message Date
Michael Gottesman
415230b65b [silgenbuilder] Add routines for creating branches/returns with ManagedValue arguments. 2017-11-21 22:49:15 -08:00
Michael Gottesman
04056ce243 [silgen] When emitting convertBridgeObjectToRef, forward ownership through the instruction rather than unconditionally adding a cleanup.
Otherwise, we may put cleanups on borrowed values.

rdar://34222540
2017-11-19 16:00:45 -08:00
Michael Gottesman
af06156bd8 [silgen] Fix ownership when peepholing bridging and enable ownership verification on test/SILGen/objc_bridging_any.swift
The bridging function that we are calling here takes in the value to be bridged
at +0. We were storing that value via a normal store. This looks like a double
consume since we were destroying the value later. Now we use a store_borrow.

rdar://29791263
2017-10-25 17:08:57 -07:00
Doug Gregor
cd3c63cbfd [AST] Stop including GenericSignature.h in other headers.
Except GenericEnvironment.h, because you can't meaningfully use a
GenericEnvironment without its signature. Lots less depends on
GenericSignature.h now. NFC
2017-10-12 14:23:46 -07:00
Slava Pestov
7bf3b90b62 SIL: Split off objc_method / objc_super_method from class_method / super_method
This replaces the '[volatile]' flag. Now, class_method and
super_method are only used for vtable dispatch.

The witness_method instruction is still overloaded for use
with both ObjC protocol requirements and Swift protocol
requirements; the next step is to make it only mean the
latter, also using objc_method for ObjC protocol calls.
2017-10-03 22:13:31 -07:00
Joe Shajrawi
75939510cd PGO: Use ProfileCounter instead of Optional<uint64_t> to hold profile counts 2017-09-26 13:34:46 -07:00
Joe Shajrawi
f4e6bb3725 PGO: add support for checked_cast__br 2017-09-26 11:14:55 -07:00
Joe Shajrawi
f136ec12f4 PGO: Add support for for_each statements 2017-09-26 11:10:51 -07:00
Andrew Trick
18004f8729 [sil-opaque-values] Fix SILGen and ownership of open_existential_box_value. 2017-09-02 19:37:57 -07:00
Michael Gottesman
df36e82b72 [silgen] Use SILGenBuilder APIs when creating value metatypes.
rdar://33358110
2017-08-30 17:31:42 -07:00
Michael Gottesman
9d5f28039a [silgen] Update SILGen objc deallocators and enable ownership on objc_dealloc.swift
rdar://33358110
2017-08-29 15:16:38 -07:00
Slava Pestov
0ee80f4f1e SILGen: More ownership refactoring
Use the ManagedValue forms of the SILInstruction constructors
where possible. This ensures that after emitting an instruction
such as an upcast, we rewrite the value's cleanup to destroy
the new value, and not the old value. This is important because
the ownership verifier asserts that instructions dominated by
a destroy cannot use the destroyed value.

Should be NFC, since the ownership verifier is off by default
for now.
2017-08-26 01:56:09 -07:00
Michael Gottesman
ae4485b9ce [silgen] init_existential_value always returns a +1 value, so it should always have a cleanup.
rdar://33358110
2017-08-25 19:50:04 -07:00
Michael Gottesman
65395787e0 [silgen] Create SILGenBuilder::createSuperMethod.
Internally the function just gets the operand and returns the super method in
ManagedValue::forUnmanaged.

rdar://33358110
2017-08-24 13:00:59 -07:00
Andrew Trick
0a36f9dee7 Fix SIL ownership for open_existential_value.
CoW existentials means that we can't forward ownership.
2017-08-23 22:15:10 -07:00
Michael Gottesman
8d32122ed2 [silgen] Move CleanupCloner from SILGenBuilder.{h,cpp} => Cleanup.{h,cpp}
CleanupCloner never seemed like it should be in SILGenBuilder.{h,cpp}.
2017-08-18 22:05:10 -07:00
Andrew Trick
5aac427ee0 SIL Ownership: Remove consumptionKind from SIL unconditional cast and related logic.
Remove the cast consumption kind from all unconditional casts. It
doesn't make sense for unconditional casts, complicates SIL ownership,
and wasn't fully supported for all variants. Copies should be
explicit.
2017-08-18 20:44:54 -07:00
Andrew Trick
f657ad2d3a Rename *ExistentialOpaque instructions to *ExistentialValue.
These instructions have the same semantics as the *ExistentialAddr instructions
but operate directly on the existential value, not its address.

This is in preparation for adding ExistentialBoxValue instructions.
The previous name would cause impossible confusion with "opaque existentials"
and "opaque existential boxes".
2017-07-17 23:46:41 -07:00
Andrew Trick
2b16660b96 [sil-opaque-values] Silence an assert in createLoadCopy + test. 2017-07-17 14:06:27 -07:00
Michael Gottesman
10f9eaf89c [silgen] Cleanup ownership around block creation.
Previously we were storing a pointer to the non-yet blockified function and not
cleaning it up. This was safe since we were performing a copy_block on the block
and destroying the SSA value of the non-yet blockified function. This looks like
a double consume to the ownership verifier. To fix this, I changed the bridging
code to use a new entrypoint for storing on SILGenBuilder that just does the
right thing by forwarding the cleanup on the SSA value and transfering it to the
address. This means the address will be destroyed instead of the SSA value,
yielding proper ownership.

rdar://31880847
2017-07-10 12:57:27 -07:00
Michael Gottesman
ae2bb3e49f [silgen] Fix createUnsafeCopyUnownedValue to be correct from an ownership perspective.
When I originally implemented this, I did not understand why an unowned(unsafe)
value was unsafe. I thought it was unsafe since the value was not meant to be
paired with (which is why I used the unmanaged_retain_value). Instead it just
means that we perform an unconditional copy without checking if the unowned
value is actually live. In contrast, an unowned(safe) value, we use
copy_unowned_value which performs an assert that the unowned value is still
alive before copying.

From a non-semantic sil perspective, since I still was retaining the value
properly, there was not a bug by using the unmanaged_retain_value instruction.

rdar://31880847
2017-07-08 22:43:52 -07:00
Michael Gottesman
fc4079ec5d [silgen] Add support for creating guaranteed phi arguments.
The main thing here is that we create a cleanup that inserts the
end_borrow_argument. Once I merge end_borrow with end_borrow_argument, there
will only be one cleanup.

rdar://31880847
2017-07-05 17:57:32 -07:00
Robert Widmann
3b202c18d8 Use 'hasAssociatedValues'
Use 'hasAssociatedValues' instead of computing and discarding the
interface type of an enum element decl.  This change has specifically not
been made in conditions that use the presence or absence of the
interface type, only conditions that depend on the presence or absence
of associated values in the enum element decl.
2017-05-22 09:54:47 -07:00
Roman Levenstein
45c2c4af0e Re-factoring: Get rid of useless arguments in "create*Apply" functions
Till now createApply, createTryApply, createPartialApply were taking some arguments like SubstCalleeType or ResultType. But these arguments are redundant and can be easily derived from other arguments of these functions. There is no need to put the burden of their computation on the clients of these APIs.

The removal of these redundant parameters simplifies the APIs and reduces the possibility of providing mismatched types by clients, which often happened in the past.
2017-05-10 08:03:37 -07:00
Slava Pestov
f5b6f22ed6 Revert "[sil] Add a new API SILFunction::getTypeLowering(SILType)."
The new API is broken. Popping a generic context frees all
dependent type lowerings, so this function returns a pointer
to freed memory.

This reverts commit 24dfae0716.
2017-04-02 19:55:19 -07:00
John McCall
305c94fc5b Pare down the Initialization interface and allow implementations
to insert code before performing an in-place initialization.

Intended to be NFC, but some of the clients needed more rewriting
than others.
2017-03-31 18:55:09 -04:00
Michael Gottesman
79b225ffa8 [silgen] Fix up EnumElementPatternInitialization::emitEnumMatch to use ownership.
This commit does a few things:

1. It uses SwitchEnumBuilder so we are not re-inventing any wheels.
2. Instead of hacking around not putting in a destroy for .None on the fail
pass, just *do the right thing* and recognize that we have a binary case enum
and in such a case, just emit code for the other case rather than use a default
case (meaning no cleanup on .none).

rdar://31145255
2017-03-29 15:36:41 -07:00
Michael Gottesman
fdfa53bacc Small mistakes in SILGenBuilder.
Noticed on inspection. We shouldn't be forwarding the input value if we are
going to be performing a copy.

rdar://31145255
2017-03-26 10:47:12 -07:00
Joe Shajrawi
cd38903450 Support reabstraction / partial apply of protocols under opaque value mode 2017-03-23 14:53:38 -07:00
Michael Gottesman
2e75d56adf [silgen] Move CleanupCloner into SILGenBuilder.h so it is available as a utility to other parts of SILGen. 2017-03-22 12:28:50 -07:00
Michael Gottesman
41f425a503 Revert "[silgen] If we have a direct guaranteed convention and have an owned value, perform a borrow."
This reverts commit 4c8595b002.
2017-03-20 05:00:31 -07:00
Michael Gottesman
4c8595b002 [silgen] If we have a direct guaranteed convention and have an owned value, perform a borrow.
Now that all arguments are properly scoped, we do not need to worry about this
extending the lifetimes of COW times inappropriately.

rdar://30955427
2017-03-18 18:59:31 -07:00
Joe Shajrawi
9a87c07bc2 Add support for creating optionals to optionals during translation / reabstraction 2017-03-16 16:59:40 -07:00
Joe Shajrawi
ea4ba25b8a Add consumption kind to UnconditionalCheckedCastValueInst 2017-03-14 13:53:05 -07:00
Michael Gottesman
68c581f729 [gardening] As per discussion, begin standardizing in SILGen that the SILGenFunction variable is passed around as SGF.
The reason that this is being done is that:

1. SILGenFunction is passed around all throughout SILGen, including in between
APIs some of which call the SILGenFunction variable SGF and others that call it
gen.
2. Thus when one is debugging code in SILGen, one wastes time figuring out what
the variable name of SILGenFunction is in the current frame.

I did not do this by hand. I did this by:

1. Grepping for "SILGenFunction &gen".
2. By hand inspecting that the match was truly a SILGenFunction &gen site.
3. If so, use libclang tooling to rename the variable to SGF.

So I did not update any use sites.
2017-03-11 23:38:17 -08:00
Michael Gottesman
0769239daf [silgen] Fix ownership of several uses of init_existential_ref in SILGenBuilder.
We were just passing along the previously used cleanup, rather than destroying
the old cleanup and propagating a new one along.

rdar://29791263
2017-03-09 14:22:25 -08:00
Michael Gottesman
9f1daaa023 [semantic-sil] Fix ownership forwarding in emitCastToReferenceType.
rdar://29791263
2017-03-08 15:34:34 -08:00
Slava Pestov
5465c8ca8f SIL: Remove most usages of TypeBase::gatherAllSubstitutions() 2017-03-08 13:54:28 -08:00
Joe Shajrawi
33b0cf653f Rename unconditional_checked_cast_opaque to unconditional_checked_cast_value 2017-03-07 18:53:52 -08:00
Joe Shajrawi
ca77872ba8 Merge CheckedCastValueBranch with new master 2017-03-06 17:32:09 -08:00
Joe Shajrawi
1f626304f1 Add support for conditional checked cast instruction for opaque value types + SILGen support for it 2017-03-06 16:35:27 -08:00
Michael Gottesman
e849900a7b [silgen] Add a new API for building switches: SwitchEnumBuilder.
This is a closure based API for creating switches that obey ownership
convensions. The way you use it with objects is as follows:

   SwitchEnumBuilder S(...);

   S.addCase(Decl, Block, [](ManagedValue Arg) -> void {
     ...
   });
   S.addCase(Decl, Block, [](ManagedValue Arg) -> void {
     ...
   });
   S.addDefaultCase(Block, [](ManagedValue Arg) -> void {
     ...
   });
   std::move(S).emit();

What is important is that it sets up the switch_enum destination blocks with the
proper cleanups for code emitted into the destination block and also provides
the default error with the passed in value with the appropriate cleanups.

It does not handle exits from the switch_enum on purpose since diamond
switch_enum APIs form a subset of APIs. It also expects the closure to create
terminators if appropriate.

In the switch_enum_addr case you have to do a bit more work, but it is still a
nicer API than doing it by hand as we do today.

rdar://29791263
2017-03-06 13:58:33 -08:00
Michael Gottesman
786c26b4df [silgen] Add a helper struct to SILGenBuilder for easing the recreation of cleanups for forwarding instructions.
rdar://29791263
2017-03-06 13:18:52 -08:00
Michael Gottesman
87ca878870 [silgen] Fix another tuple explosion -> borrow + copy since we do not have the destructure operation yet.
rdar://29791263
2017-03-05 11:44:11 -08:00
Michael Gottesman
80e297d6aa [silgen] Go through and fix up places where during bringup of load_borrow I just put in load_borrow without any end_borrow cleanups. Now use real cleanups.
rdar://29791263
2017-03-02 19:53:33 -08:00
Michael Gottesman
455c126238 [semantic-sil] Pass the uncasted argument as an @owned arg in the failed checked_cast_br cast.
Previously, we would put a destroy_value directly on the value that we tried to
cast. Since checked_cast_br is consuming, this would cause the destroy_value on
the failure path to be flagged as a double consume.

This commit causes SILGen to emit the value consumed by checked_cast_br as an
@owned argument to the failure BB, allowing semantic arc rules to be respected.

As an additional benefit, I also upgraded the ownership_model_eliminator test to
use semantic sil verification.

One issue that did come up though is that I was unable to use the new code in
all locations in the compiler. Specifically, there is one location in
SILGenPattern that uses argument unforwarding. I am going to need to undo
argument unforwarding in SILGenPattern in order to completely eliminate the old
code path.
2017-02-28 17:29:03 -05:00
Mikio Takeuchi
ad0c27edc5 Handle Unmanaged(Retain|Release)ValueInst as RefCountingInst 2017-02-27 12:25:57 +09:00
Michael Gottesman
5a15f880ee [silgen] Change a bunch of self accesses to use true formal evaluation scopes and formal accesses.
This is in preparation for removing the +0 self hack.

This commit in more detail does the following:

1. It adds Formal Evaluation Scopes to certain places where the scopes were
missing. Specifically:

   a. The SILGenLValue cases are places where we are invoking accessors. In each
      one of these cases, we had a formal evaluation scope in the accessor
      itself, but we did not have a scope that closed over the base access and
      the accessor access. The base access is a formal evaluation in the sense
      that just like with inout bases, we must create a new reference to the
      base and re-destroy the base in a chain of accesses. This is to ensure
      that we do not extend the lifetime of the base inappropriately.

   b. The SILGenPoly case is a place where we have never properly placed a
      Formal Evaluation Scope and have completely been relying on the +0 self
      hack to make sure that archetype callees are properly destroyed
      immediately after a +0 call.

2. It changes all of the places in SILGen that emit self to using formal access
cleanups instead of normal cleanups.

rdar://29791263
2017-02-23 10:36:48 -08:00
Michael Gottesman
784d5d16fc [silgen] Change emitClassConstructorInitializer to use ownership and make calling designated/chaining initializes use proper ownership.
rdar://29791263
2017-02-23 08:48:58 -08:00
Joe Shajrawi
147f844ce4 Merge pull request #7654 from shajrawi/init_opaque
Add support for Init Existentials for opaque value types
2017-02-20 19:37:01 -08:00