Commit Graph

228 Commits

Author SHA1 Message Date
Michael Gottesman
398f20c506 [+0-all-args] Add SILGenBuilder APIs for createRefToBridgeObject and use it in emitBuiltinCastToBridgeObject to ensure cleanups are on the actual bridge object.
This eliminates a case where we were not moving a cleanup onto a forwarding case
and instead just jammed the original cleanup and the new value into 1 managed
value.

NFC, but helps the ownership verifier.

rdar://34222540
2018-02-14 15:20:42 -08:00
Michael Gottesman
d6b4fabc0e [silgen] All ownership endowed APIs on SILGenBuilder should use SILGenBuilder SILValue APIs to ensure we get proper conformances.
I was thinking about this last night. I think the reason that this difference
came up is that people saw the SILGenBuilder SILValue based conformance APIs and
copied their calling of SILBuilder SILValue APIs. In the general case this is
not safe, since in some cases we need to add conformances.

Noticed by inspection.
2018-02-12 13:14:45 -08:00
Michael Gottesman
83349e7acf [silgen] Reorganize SILGenBuilder.cpp into two sections, one for SILValue APIs + conformance support and the other ManagedValue APIs.
NFC.
2018-02-12 13:14:45 -08:00
Michael Gottesman
3888b5b054 [+0-normal-args] Use SILGenBuilder in SILGenConvert when injecting an optional so we propagate through +0 vs +1.
This commit does two different things:

1. Changes some code paths in SILGenConvert to use SILGenBuilder APIs.
2. It eliminates a few bugs in this path exposed by running the SILGen tests
with +0 enabled.

The bug specifically is that we are assuming in this code that the input value
is always at +1, so we unconditionally forward the input value and then create
destroys on the output. This is not correct in general (since we have guaranteed
values) and occurs all the time with +0 arguments enabled.

This is tested by running the SILGen tests with +0 arguments enabled.

rdar://34222540
2018-02-12 11:17:55 -08:00
Michael Gottesman
b70dd955a0 [silgen] Add ownership endowed APIs for convert_function, function_ref, partial_apply.
I did this work while doing some exploratory work around +0 all args. No reason
not to upstream this.
2017-12-12 13:15:35 -08:00
John McCall
364178f6f1 Blocks can only be converted to AnyObject with a cast under ObjCInterop.
I don't think there's a way to test this right now because we don't
actually support SwiftValue and so on except under ObjCInterop, but
it's certainly more future-proof.
2017-11-29 17:03:33 -05:00
John McCall
ad0f8ab3b2 Weaken an assertion: conversions can go through Any but still end up
at AnyObject?.

Also, ensure that @convention(block) functions can be erased to
AnyObject, and teach SILGen how to do this with unchecked_ref_cast.

Fixes a source compatibility suite regression.
2017-11-29 02:58:04 -05:00
swift-ci
371e1b8f27 Merge pull request #13096 from gottesmm/pr-2b9d20d5f94e2bcc9fff00ab39293a8c091f285a 2017-11-27 17:36:25 -08:00
Michael Gottesman
1a5b7e1e8e [+0-all-args] Change SGF::emitUncheckedGetOptionalValueFrom to use SILGenBuilder APIs instead of handrolling said APIs.
It additionally removes some code from SILGenBuilder::createUncheckedEnumData
that introduced extra borrows. That is really the builder's caller's
responsibility. Plus, it isn't that hard to do anymore now that one can just
call ManagedValue::borrow.

rdar://34222540
2017-11-27 16:38:56 -08:00
Michael Gottesman
35fd24ed74 [+0-all-args] Create SILGenBuilder::createTuple().
This takes a series of ManagedValues and combines them together into 1 tuple. It
assumes that all non-trivial ManagedValues are all at +1 or all at +0. It leaves
the verification to the ownership verifier since this would immediately trigger
the ownership verifier when the instruction is created by the SILBuilder.

rdar://34222540
2017-11-27 16:20:21 -08:00
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