Commit Graph

133 Commits

Author SHA1 Message Date
Robert Widmann
b849e51768 Use operator bool to claw back some readability 2019-10-29 16:56:21 -07:00
Robert Widmann
3e1a61f425 [NFC] Fold The Tri-State In Optional<ProtocolConformanceRef>
ProtocolConformanceRef already has an invalid state.  Drop all of the
uses of Optional<ProtocolConformanceRef> and just use
ProtocolConformanceRef::forInvalid() to represent it.  Mechanically
translate all of the callers and callsites to use this new
representation.
2019-10-29 16:55:56 -07:00
Michael Gottesman
26a734e58e [sil] Rename ValueOwnershipKind::{Any,None} 2019-10-25 10:28:25 -07:00
Andrew Trick
bddc69c8a6 Organize SILOptimizer/Utils headers. Remove Local.h.
The XXOptUtils.h convention is already established and parallels
the SIL/XXUtils convention.

New:
- InstOptUtils.h
- CFGOptUtils.h
- BasicBlockOptUtils.h
- ValueLifetime.h

Removed:
- Local.h
- Two conflicting CFG.h files

This reorganization is helpful before I introduce more
utilities for block cloning similar to SinkAddressProjections.

Move the control flow utilies out of Local.h, which was an
unreadable, unprincipled mess. Rename it to InstOptUtils.h, and
confine it to small APIs for working with individual instructions.
These are the optimizer's additions to /SIL/InstUtils.h.

Rename CFG.h to CFGOptUtils.h and remove the one in /Analysis. Now
there is only SIL/CFG.h, resolving the naming conflict within the
swift project (this has always been a problem for source tools). Limit
this header to low-level APIs for working with branches and CFG edges.

Add BasicBlockOptUtils.h for block level transforms (it makes me sad
that I can't use BBOptUtils.h, but SIL already has
BasicBlockUtils.h). These are larger APIs for cloning or removing
whole blocks.
2019-10-02 11:34:54 -07:00
Slava Pestov
d434188157 SIL: Refactor TypeConverter to not require a SILModule
This mostly requires changing various entry points to pass around a
TypeConverter instead of a SILModule. I've left behind entry points
that take a SILModule for a few methods like SILType::subst() to
avoid creating even more churn.
2019-09-06 21:50:15 -04:00
Slava Pestov
22cb6f1176 AST: Introduce ProtocolDecl::get{AssociatedType,ProtocolRequirement}() 2019-09-03 22:39:35 -04:00
Erik Eckstein
b58f54c311 CastOptimizer: fix a minor bug with memory lifetime.
This was not a real leak, because it only happened in an error condition where the block ends in an trap instructon anyway.
But it must be fixed to make the MemoryLifetimeVerifier happy.
2019-08-13 09:29:05 +02:00
Michael Gottesman
5d9675392e [cast-opt] Improve handling of arguments when optimizing Swift -> ObjC bridging casts.
Specifically:

1. I removed an extra defensive copy that we put in place some time ago that
isn't really warranted. We know that we have an @owned value, so can safely just
pass the value as a @guaranteed parameter. This also eliminates an ownership
error that would occur due to my not having updated this code for ownership in
tree.

2. I also ensured that if we are performing a loadable address bridging cast ->
value bridging cast that we store the loadable value back into memory after we
perform the cast. Otherwise, it appears to leak to the ownership verifier.

I also centralized the non-ownership tests for this into one place
(const_fold_objc_bridge.sil => constant_propagation_objc.sil).
2019-06-23 13:17:10 -07:00
Michael Gottesman
4b31d37307 [cast-opt] Rename misnamed variable.
This is actually an out parameter of the apply that we are then switching upon.
2019-06-22 22:12:00 -07:00
Davidino Italiano
12ee5d3b48 [CastOptimizer] Set the correct debug scope for the SILBuilder.
This is a recommit with a change to only run the test for
optimized stdlib [as the bug reproduces only there [as the bug reproduces only there]
2019-06-17 08:33:29 -07:00
Ben Langmuir
3ac3ff0a6b Revert " [CastOptimizer] Set the correct debug scope for the SILBuilder. " 2019-06-17 07:18:06 -07:00
Davide Italiano
c4adb9bd16 Merge pull request #25490 from dcci/rdar48887384
[CastOptimizer] Set the correct debug scope for the SILBuilder.
2019-06-14 18:58:34 -07:00
Davide Italiano
9575a6e05c [CastOptimizer] Set the correct debug scope for the SILBuilder.
<rdar://problem/48887384>
2019-06-14 15:58:13 -07:00
Michael Gottesman
c5e9fc286a [cast-optimizer] Fix a leak in the cast optimizer when we perform load promotion and fail.
Specifically, in this part of the cast optimizer we are trying to optimize casts
that can be done in two parts. As an example consider: NSObject ->
Array<Any>. In this case, we first cast from NSObject -> NSArray and then try to
conditionally bridge to Array<Any> from NSArray.

The problem is we did not destroy the NSObject correctly if the first cast
failed. I couldn't figure out how to create an actual swift test case that
produces this problem since we are pretty conservative about triggering this
code path. But in SIL it is pretty easy and in ossa, we trigger the ownership
verifier.

This is another victory for the ownership verifier!

rdar://51753580
2019-06-14 10:33:24 -07:00
Michael Gottesman
39dbf54e19 [cast-opt] Update checked_cast_addr_br objc -> swift bridged cast optimization for ownership.
These fix assertions that we hit by even performing the optimization, some
making sure the test doesn't hit those is sufficient.
2019-05-26 18:30:21 -07:00
Michael Gottesman
ad676857ea [cast-opt] Fix miscompile when we tried to optimize take_on_success that resulted in invalid IR being emitted.
The specific problem here is that we are setting the insertion point of a
SILBuilder and not unsetting it. I fixed the problem by creating a separate
builder so the original builder stays put.

I originally came across this in my work on moving ownership stripping after the
diagnostic passes. This patch fixes it without my other changes to ease
cherry-picking to 5.1.

I also added more test coverage by expanding the test case to also handle
copy_on_success and take_always.

rdar://51093557
2019-05-24 09:58:24 -07:00
Arnold Schwaighofer
4985e0f818 SILCombine: We are not guaranteed to get a function_ref instruction here
Also fix an issue in that cast optimizer and objc bridging thunks that
are marked dynamic.

Same test case tests both issues.

rdar://51068786
2019-05-23 08:39:31 -07:00
Michael Gottesman
748bf88bbe [ownership] Convert SILOptimizer/cast_folding.swift to work with ownership and fix all issues exposed. 2019-05-21 09:07:13 -07:00
Michael Gottesman
08c25da322 [cast-opt] Fix another code path in the cast optimizer for ownership and add an additional run of cast_folding_objc.swift with stripping ownership after serialization. 2019-05-20 00:51:26 -07:00
Michael Gottesman
35c3a0fce3 [cast-opt] Change one pathway through the cast-optimizer to use higher level constructs that are both ossa and non-ossa compatible 2019-05-18 18:32:31 -07:00
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
Slava Pestov
472787bab7 SIL: isNonThrowing parameter of SILBuilder::create{Begin,}Apply() defaults to false
Also remove the overload of createApply() that does not take a SubstitutionMap.
It accomplishes nothing except creating ambiguity.
2019-04-25 22:27:38 -04:00
Michael Gottesman
1983368057 [cast-opt] Create an ownership version of bridged_cast_folding.sil and fix the cast optimizer so we can optimize it correctly. 2019-04-14 01:06:03 -07:00
Michael Gottesman
4304732553 Merge pull request #24020 from gottesmm/pr-435183b51ab766a85b8b66a35286dbc6f1d0e6fa
[cast-opt] Eliminate a leak along the cast failure path when bridging…
2019-04-14 00:28:02 -07:00
Michael Gottesman
de021aa32b [cast-opt] Eliminate a leak along the cast failure path when bridging from a swift to ns object derived class.
I added an interpreter test that exercises this code path using LifetimeTracked
to make sure we do not regress again.

rdar://49882071
2019-04-13 20:10:24 -07:00
Michael Gottesman
13a3dbbba0 [cast-opt] Simplify some code by hoisting some code/early exiting.
Importantly this lets us know further on in the function that as an invariant we
only can have checked_cast_addr_br.
2019-04-13 17:45:45 -07:00
Michael Gottesman
9caeaafd80 [cast-optimizer] Add some file level flags to section out the file into chunks. 2019-04-13 14:57:28 -07:00
Michael Gottesman
b6611eee35 [cast-opt] Combine two blocks with the same condition and invert that condition to eliminate a level of indentation.
NFCI.
2019-04-12 14:47:38 -07:00
Michael Gottesman
98c20d4946 [cast-opt] Slim down a large function by extracting out a part of it into a subroutine.
I also added documentation around what the code is actually trying to do/invariants/etc.

NFCI.
2019-04-12 12:23:28 -07:00
Michael Gottesman
d1389aded6 [cast-opt] Refactor some code onto SILDynamicCast and delete dead code.
NFC.
2019-04-12 12:23:28 -07:00
Michael Gottesman
f7b014b95c [cast-opt] Rename CastOptimizer member vars to match the rest of the CastOptimizer.
Done using Xcode's refactoring engine.
2019-04-12 11:09:29 -07:00
Michael Gottesman
57b923b983 [cast-opt] Add a long comment explaining how bridging cast opt from ObjC->Swift modifies the CFG.
I also renamed some variables to match the comment.
2019-03-31 13:43:13 -07:00
Slava Pestov
c791c4a137 SIL: SILUndef must be aware of the resilience expansion
The ownership kind is Any for trivial types, or Owned otherwise, but
whether a type is trivial or not will soon depend on the resilience
expansion.

This means that a SILModule now uniques two SILUndefs per type instead
of one, and serialization uses two distinct sentinel IDs for this
purpose as well.

For now, the resilience expansion is not actually used here, so this
change is NFC, other than changing the module format.
2019-03-12 00:30:35 -04:00
Slava Pestov
5847e163c1 SIL: Use better type lowering APIs in a couple of spots 2019-03-05 20:59:58 -05:00
Michael Gottesman
c3fd9e35d3 Merge pull request #23101 from gottesmm/objc_to_swift_cast_cleanups
[cast-opt] Small cleanups to optimizeBridgedObjCToSwiftCast in preparation for fixing it for ownership.
2019-03-05 12:35:50 -08:00
Michael Gottesman
5da46389c5 [cast-opt] Extract into a helper the SILFunction, SubstitutionMap searching code from optimizeBridgedSwiftToObjCCast. 2019-03-05 10:39:47 -08:00
Michael Gottesman
b8a7102c90 [cast-opt] Refactor finding the ObjectiveCBridgeable protocol substitutions into a helper function. 2019-03-05 09:17:58 -08:00
Michael Gottesman
c57a0324f2 [cast-opt] Move some values closer to their definitions to ease code changes. 2019-03-05 09:17:58 -08:00
Michael Gottesman
25f21de8dd [cast-opt] Delete dead code. 2019-03-05 09:17:58 -08:00
Michael Gottesman
eb8c9d9bf0 [dynamic-cast] Hoist early bail out if the source objc type is address only.
This could probably be an error, but I am leaving it to preserve previous
behavior. The reason I am hoisting this is that I am hoisting it before we
potentially split a basic block. When we evaluate if we can early exit, we
should never do work before we know that we will emit /something/.
2019-03-05 09:17:58 -08:00
Michael Gottesman
e3acbd0b84 [cast-opt] Perform initial argument/cast replacement with an inline constructor in optimizeBridgedObjCToSwiftCast.
This makes it easier to read and understand the control flow by using early
returns.
2019-03-05 09:17:58 -08:00
Michael Gottesman
19a7978ff5 [cast-opt] Eliminate some unneeded type checks in favor of just asserting the instructions we handle.
This code today only handles the following two instructions:

1. checked_cast_addr_br
2. unconditional_checked_cast_addr

We were asserting that the source/dest of these values were addresses... but
they are obviously are!
2019-03-05 08:44:47 -08:00
Michael Gottesman
b87e203853 [cast-opt] Hoist the AnyHashable bail-out as early as possible.
We were doing unneeded work and then bailing. This is NFC.
2019-03-05 08:44:47 -08:00
Michael Gottesman
aefbc490d6 [cast-opt] Extract out the retrieval of the ObjC -> Swift bridging functions into a helper function.
NFC. Extracted using Xcode.
2019-03-04 23:12:01 -08:00
swift-ci
0d9b1963c7 Merge pull request #23086 from gottesmm/pr-706deb33fc2fb07b5b920a486f7696ceb08a8fc3 2019-03-04 22:05:42 -08:00
Michael Gottesman
276455979e [cast-opt] Eliminate always true if statement.
I think that this is a result of previous refactorings. This code has a long
time assert in it that Src (since 2016) is an address and for a long time the bridged type has
always been an object (since 2015). So, thus we know that the following is always true.

  SILBridgedTy != Src->getType()

I also removed a level of indentation. NFC.

The 2015 commit hash is: 2dd38eee0e (the original commit).
The 2016 commit hash is: 506ab9809f
2019-03-04 20:59:19 -08:00
Michael Gottesman
4b250b2ae4 [cast-opt] Fix obvious bug.
Found via inspection. The code is supposed to check if either the source or the
target is NSError and in such a case bail.
2019-03-04 11:08:00 -08:00
Michael Gottesman
ee02174f71 [cast-opt] Change optimize{Swift,ObjC}to{ObjC,Swift} to use SILDynamicCastInst.
I also debrided dead variables that resulted from eliminating the trampoline
argument code in optimizeBridgedCasts().
2019-03-04 10:48:13 -08:00
Michael Gottesman
fa4c38bf0c [cast-opt] Now that all users of optimizeBridgedCasts use the SILDynamicCastInst entrypoint, delete the old entrypoint and inline its body into the new entrypoint. 2019-03-03 16:33:12 -08:00
Michael Gottesman
e833d11720 [cast-opt] Change simplifyCheckedCast{,Value}BranchInst to use SILDynamicCastInst.
This is the last change needed to simplify the API of optimizeBridgedCasts to
take SILDynamicCastInst.
2019-03-03 16:33:11 -08:00