Commit Graph

48 Commits

Author SHA1 Message Date
Slava Pestov
16d5716e71 SIL: Use the best resilience expansion when lowering types
This is a large patch; I couldn't split it up further while still
keeping things working. There are four things being changed at
once here:

- Places that call SILType::isAddressOnly()/isLoadable() now call
  the SILFunction overload and not the SILModule one.

- SILFunction's overloads of getTypeLowering() and getLoweredType()
  now pass the function's resilience expansion down, instead of
  hardcoding ResilienceExpansion::Minimal.

- Various other places with '// FIXME: Expansion' now use a better
  resilience expansion.

- A few tests were updated to reflect SILGen's improved code
  generation, and some new tests are added to cover more code paths
  that previously were uncovered and only manifested themselves as
  standard library build failures while I was working on this change.
2019-04-26 22:47:59 -04:00
Michael Gottesman
89a447a3de [ownership] When splitting destructures, simplify the instruction if possible.
I noticed that this is needed at -Onone to maintain the same codegen output.
2019-02-11 11:52:52 -08:00
Michael Gottesman
cec2b989c1 [ownership] When transforming destructures => projections, only create projections for used results.
This is not technically necessary, but it eliminates some differences in the
tests at -Onone.
2019-02-10 22:46:25 -08:00
Michael Gottesman
85ef0b2340 [ownership] Once we strip ownership ignoring transparent functions from our module, strip ownership as we serialize. 2019-01-31 15:45:38 -08:00
Michael Gottesman
1375dd0a62 [ownership] Add a specialized version of ome that skips transparent functions.
This will let me strip ownership from non-transparent functions at the beginning
of the perf pipeline. Then after we serialize, I will run OME on the transparent
functions. Otherwise, we can not perform mandatory inlining successfully since
we can not inline ossa into non-ossa functions.
2019-01-31 00:27:08 -08:00
Michael Gottesman
f386dd0b6d [ome] Run even on deserialized functions.
This is needed since we are going to start eliminating ownership after SIL is
serialized. So we need to make sure that we strip those as well.

In terms of compile time, this shouldn't have much of an effect since we already
skip functions that already have ownership stripped.
2019-01-29 08:46:01 -08:00
Michael Gottesman
5d157c55e9 [ome] Make sure to skip functions that do not have ownership.
This is not an actual bug since given where OME is in the pipeline today it will
only process ossa functions. But philosophically this is the right thing to do.
2019-01-08 12:45:12 -08:00
Michael Gottesman
23378cc16f [sil] Rename QualifiedOwnership => Ownership.
Done using Xcode's refactoring engine.
2018-12-16 15:21:52 -08:00
Michael Gottesman
f38b782145 [ome] Add an option sil-dump-before-ome-to-path to dump the current module to a path before stripping ownership.
This will simplify analyzing SIL as we move the ownership model eliminator back
through the pipeline.
2018-12-05 16:39:18 -08:00
Michael Gottesman
97367d3dd4 [ownership] Do not lower copy_unowned_value to strong_retain_unowned.
The major important thing here is that by using copy_unowned_value we can
guarantee that the non-ownership SIL ARC optimizer will treat the release
associated with the strong_retain_unowned as on a distinc rc-identity from its
argument. As an example of this problem consider the following SILGen like
output:

----
%1 = copy_value %0 : $Builtin.NativeObject
%2 = ref_to_unowned %1
%3 = copy_unowned_value %2
destroy_value %1
...
destroy_value %3
----

In this case, we are converting a strong reference to an unowned value and then
lifetime extending the value past the original value. After eliminating
ownership this lowers to:

----
strong_retain %0 : $Builtin.NativeObject
%1 = ref_to_unowned %0
strong_retain_unowned %1
strong_release %0
...
strong_release %0
----

From an RC identity perspective, we have now blurred the lines in between %3 and
%1 in the previous example. This can then result in the following miscompile:

----
%1 = ref_to_unowned %0
strong_retain_unowned %1
...
strong_release %0
----

In this case, it is possible that we created a lifetime gap that will then cause
strong_retain_unowned to assert. By not lowering copy_unowned_value throughout
the SIL pipeline, we instead get this after lowering:

----
strong_retain %0 : $Builtin.NativeObject
%1 = ref_to_unowned %0
%2 = copy_unowned_value %1
strong_release %0
...
strong_release %2
----

And we do not miscompile since we preserved the high level rc identity
pairing.

There shouldn't be any performance impact since we do not really optimize
strong_retain_unowned at the SIL level. I went through all of the places that
strong_retain_unowned was referenced and added appropriate handling for
copy_unowned_value.

rdar://41328987

**NOTE** I am going to remove strong_retain_unowned in a forthcoming commit. I
just want something more minimal for cherry-picking purposes.
2018-06-27 13:02:58 -07:00
Andrew Trick
0dfce6c0f6 Don't rerun mandatory lowering passes on deserialized SIL. 2018-02-09 09:55:47 -08:00
Michael Gottesman
fa4873ae74 [ome] Teach the eliminator how to lower destructure_{struct,tuple}.
I am doing this to ensure that parts of the optimizer that have not yet been
updated for such operations do not see said instructions. This will ensure no
surprise perf regressions from this work.

rdar://31521023
2017-10-25 19:21:24 -07:00
John McCall
ab3f77baf2 Make SILInstruction no longer a subclass of ValueBase and
introduce a common superclass, SILNode.

This is in preparation for allowing instructions to have multiple
results.  It is also a somewhat more elegant representation for
instructions that have zero results.  Instructions that are known
to have exactly one result inherit from a class, SingleValueInstruction,
that subclasses both ValueBase and SILInstruction.  Some care must be
taken when working with SILNode pointers and testing for equality;
please see the comment on SILNode for more information.

A number of SIL passes needed to be updated in order to handle this
new distinction between SIL values and SIL instructions.

Note that the SIL parser is now stricter about not trying to assign
a result value from an instruction (like 'return' or 'strong_retain')
that does not produce any.
2017-09-25 02:06:26 -04:00
Michael Gottesman
b2550ecb44 [ownership] Do not print out ownership convention on BBArgs once ownership is stripped.
rdar://31521023
2017-08-31 17:53:54 -07:00
Erik Eckstein
020a283bbb SIL: Let the OpenedArchetypesTracker be constructed with a null SILFunction.
NFC
2017-08-23 09:15:00 -07:00
Roman Levenstein
202de40f05 [sil-opened-archetype-tracker] Improve tracking of archetypes in SILBuilder
Fixes rdar://problem/31749245
2017-04-24 08:50:55 -07:00
Roman Levenstein
a60e037c48 Revert "[sil-opened-archetype-tracker] Improve tracking of archetypes in SILBuilder" 2017-04-22 20:41:31 -07:00
Roman Levenstein
33c8bde859 [sil-opened-archetype-tracker] Improve tracking of archetypes in SILBuilder
Fixes rdar://problem/31749245
2017-04-22 10:03:37 -07:00
Michael Gottesman
ad7705ed42 [semantic-sil] Update OME for having mark_uninitialized on the alloc_box instead of project_box.
This consists of just removing support from OME for ensuring that all uses of a
box go through the project_box that has as its user a mark_uninitialized. Since
the lowering of mark_uninitialized onto the relevant project_box is done by
MarkUninitializedFixup pass, I can just rip out the hacks from the OME!

rdar://31521023
2017-04-17 21:10:24 -07:00
Andrew Trick
be1881aa1f Remove redundant Transform.getName() definitions.
At some point, pass definitions were heavily macro-ized. Pass
descriptive names were added in two places. This is not only redundant
but a source of confusion. You could waste a lot of time grepping for
the wrong string. I removed all the getName() overrides which, at
around 90 passes, was a fairly significant amount of code bloat.

Any pass that we want to be able to invoke by name from a tool
(sil-opt) or pipeline plan *should* have unique type name, enum value,
commend-line string, and name string. I removed a comment about the
various inliner passes that contradicted that.

Side note: We should be consistent with the policy that a pass is
identified by its type. We have a couple passes, LICM and CSE, which
currently violate that convention.
2017-04-09 15:20:28 -07:00
Michael Gottesman
c28a62d97c [ome] Make sure to rewire all project_box we insert through the mark_uninitialized project_box to preserve DI behavior.
Once I update DI in a bit, this will no longer be necessary.

rdar://29870610
2017-04-03 12:17:44 -07:00
Michael Gottesman
85688b87bb [ome] Change the eliminator to be a module pass instead of a function pass.
This ensures that the pass manager runs the eliminator on all functions once
before running any further passes. Otherwise, due to the way the pass manager
runs functions, sometimes a function with unqualified ownership is visible from
a function with qualified ownership. This makes it impossible to have an
invariant that a pass either sees an entire qualified or unqualified world. This
is a useful feature that I would like to keep if I can.

rdar://29870610
2017-04-03 09:43:37 -07:00
Michael Gottesman
f301044384 [ownership-model] Teach the ownership model eliminator how to eliminate the default argument of a switch_enum and tighten up assertions in SILVerifier.
rdar://31145255
2017-03-27 16:25:37 -07:00
Michael Gottesman
cfb5893663 [silgen] Fix destroying destructor to use proper ownership with its @owned return value.
rdar://29791263
2017-03-02 17:17:17 -08:00
Michael Gottesman
4bc12aedbd [sil] Add end_lifetime.
This is the lifetime ending variant of fix_lifetime. It is a lie to the
ownership verifier that a value is being consumed along a path. Its intention is
to be used to allow for the static verification of ownership in deallocating
deinits which for compatibility with objective-c have weird ownership behavior.
See the commit merged with this commit for more information.
2017-03-01 18:30:23 -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
91b8db5e11 Replace newly introduced Atomicity::Atomic 2017-02-27 12:17:53 +09:00
Mikio Takeuchi
488d531846 Enhance -assume-single-threaded option (SR-3945) 2017-02-27 12:17:53 +09:00
Andrew Trick
eed44abd1d OwnershipModelEliminator should not rewrite address-only copy/destroy. 2017-02-16 18:50:31 -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
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
practicalswift
6d1ae2a39c [gardening] 2016 → 2017 2017-01-06 16:41:22 +01:00
Michael Gottesman
6213550239 [semantic-sil] Create copy_unowned_value.
This was in the first high level ARC instruction proposal, but I have not needed
it until now. The use case for this is to ahandle strong_retain_unowned (which
takes in an unowned value, asserts it is still alive, performs a strong_retain,
and returns the @owned value). This @owned value needs a destroy_value.

rdar://29671437
2016-12-14 19:22:24 -08:00
Michael Gottesman
66cff7e37e [semantic-arc] Add StoreBorrowInst and BeginBorrowInst. 2016-12-12 13:06:18 -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
34ec32bc14 [semantic-arc] Handle the rest of the unqualified mem opts in SILGen.
Keep in mind that these are approximations that will not impact correctness
since in all cases I ensured that the SIL will be the same after the
OwnershipModelEliminator has run. The cases that I was unsure of I commented
with SEMANTIC ARC TODO. Once we have the verifier any confusion that may have
occurred here will be dealt with.

rdar://28685236
2016-11-09 11:37:52 -08:00
Michael Gottesman
bffa7addaf [semantic-arc] Eliminate default {Load,Store}OwnershipQualification argument to SILBuilder::create{Load,Store}(...)
Today, loads and stores are treated as having @unowned(unsafe) ownership
semantics. This leaves the user to specify ownership changes on the loaded or
stored value independently of the load/store by inserting ARC operations. With
the change to Semantic SIL, this will no longer be true. Instead loads, stores
have ownership semantics that one must reason about such as copy, take, and
trivial.

This change moves us closer to that world by eliminating the default
OwnershipQualification argument from create{Load,Store}. This means that the
compiler developer cannot ignore reasoning about the ownership semantics of the
memory operation that they are creating.

Operationally, this is a NFC change since I have just gone through the compiler
and updated all places where we create loads, stores to pass in the former
default argument ({Load,Store}OwnershipQualifier::Unqualified), to
SILBuilder::create{Load,Store}(...). For now, one can just do that in situations
where one needs to create loads/stores, but over time, I am going to tighten the
semantics up via the verifier.

rdar://28685236
2016-10-30 13:07:06 -07:00
Michael Gottesman
a1f489f450 [semantic-arc] Make sure to use TypeLowering methods in the OwnershipModelEliminator when creating lowered ARC operations.
Since we set the unqualified flag before we eliminate any instructions, this
will do the *right* thing and emit the most optimal lowered representation
whether it is a strong_* or *_value instruction.

This ensures that passes that expect values to have the most lowered
representation of instruction to be able to pattern match correctly.

This can up when alloc-box-to-stack was pattern matching on strong_retain on
Boxes, but I was being lazy and emitted retain_value.

rdar://28851920
2016-10-29 20:11:06 -07:00
Michael Gottesman
f9b4ac714b [semantic-arc] Make sure that the ownership model eliminator sets the unqualified flag before it runs.
Otherwise, we will trigger verification in SILBuilder?!

rdar://28851920
2016-10-25 22:31:05 -07:00
Michael Gottesman
bb9197c7ff [semantic-arc] Move the Ownership Model Eliminator and management of SILFunction::hasQualifiedOwnership in front of SILOptions::EnableSILOwnership.
This is a NFC change, since verification still will be behind the flag. But this
will allow me to move copy_value, destroy_value in front of the
EnableSILOwnership flag and verify via SILGen that we are always using those
instructions.

rdar://28851920
2016-10-23 18:30:43 -07:00
Michael Gottesman
813facfcff [gardening] Refactor out setting Insertion Point and Debug Scope from each visitor to beforeVisit in the OwnershipModelEliminator. 2016-10-22 22:26:07 -07:00
Michael Gottesman
271e3909df [semantic-arc] Implement OwnershipModelEliminator support for copy_value, destroy_value.
rdar://28851920
2016-10-22 22:26:06 -07:00
Michael Gottesman
b0539a9a15 [semantic-arc] Only run the ownership model eliminator on functions with qualified ownership and make it after running change such functions to have unqualified ownership.
rdar://28685236
2016-10-21 17:42:39 -07:00
Michael Gottesman
85269c767a [semantic-arc] Implement eliminating end_borrow and load_borrow from the OwnershipModelEliminator.
rdar://28685236
2016-10-20 12:30:18 -07:00
Michael Gottesman
abaed64e20 [semantic-arc] Refactor the ownership model eliminator to use a visitor type.
rdar://28685236
2016-10-20 12:24:15 -07:00
practicalswift
265a4605a0 [gardening] Fix typos. 2016-10-16 12:53:49 +02:00
practicalswift
5d7bf22381 [gardening] Improve header consistency 2016-10-16 12:41:09 +02:00
Michael Gottesman
e48e7b7fca [semantic-arc] Add a new pass called the OwnershipModelEliminator that eliminates SILOwnership from the IR.
It currently just breaks up qualified loads/stores into their unqualified
constituant parts.

rdar://28685236
2016-10-15 21:35:34 -07:00