Commit Graph

2777 Commits

Author SHA1 Message Date
Slava Pestov
ced5548deb SIL: Add resilience expansion parameter to TypeConverter::countNumberOfFields() 2019-03-06 02:26:26 -05:00
Slava Pestov
5847e163c1 SIL: Use better type lowering APIs in a couple of spots 2019-03-05 20:59:58 -05:00
Ravi Kandhadai
04770f6124 [SIL Diagnostics] Fix undefined behavior due to bit shifting in ConstantFolding.cpp
<rdar://48605307>
2019-03-05 16:09:01 -08: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
Michael Gottesman
807b187302 [cast-opt] Update simplifyCheckedCastAddrBranchInst to use SILDynamicCastInst.
This is another step towards eliminating the many argument call to
optimizeBridgedCasts.
2019-03-03 16:33:11 -08:00
Michael Gottesman
aaf464485b [cast-opt] Change CheckedCastBranch metatype opts to use SILDynamicCastInst. 2019-03-03 14:50:21 -08:00
Michael Gottesman
e80c394e82 [cast-opt] Update optimization of unconditional checked cast to use SILDynamicCastInst. 2019-03-03 14:50:21 -08:00
Michael Gottesman
d4e910ce5c [cast-opt] Update optimizeUnconditionalCheckedCastAddrInst to use SILDynamicCastInst. 2019-03-03 13:42:04 -08:00
Slava Pestov
2813912c48 Merge pull request #23010 from pschuh/s-5
FloatLiteralExpr now is lowered directly into SIL.
2019-03-01 17:32:23 -05:00
Parker Schuh
5160da6a2e FloatLiteralExpr now is lowered directly into SIL.
For context, String, Nil, Bool, and Int already behave this way.

Note: Swift can compile against 80 or 64 bit floats as the builtin
literal type. Thus, it was necessary to capture this bit somehow in the
FloatLiteralExpr. This was done as another Type field capturing this
info.
2019-03-01 09:01:30 -08:00
Slava Pestov
1944254253 SIL: Use SILFunction type lowering APIs in various places 2019-03-01 02:07:16 -05:00
Slava Pestov
c5cf5c2c4b SILOptimizer: Remove isSimpleType()
This was checking if the type is a trivial type that is not a
class. However this was redundant because class types are not
trivial anyway.
2019-02-28 22:56:03 -05:00
Slava Pestov
112d1bd561 SILOptimizer: Small cleanups for devirtualizer
Where possible, pass around a ClassDecl or a CanType instead of a
SILType that might wrap a metatype; the unwrapping logic was
repeated in several places.

Also add a FIXME for a bug I found by inspection.
2019-02-28 22:40:54 -05:00
Erik Eckstein
e433759e73 SILOptimizer: fix a stupid bug in StackNesting which can cause a miscompile in functions with unreachable blocks.
rdar://problem/47973577
2019-02-27 10:17:46 -08:00
Michael Gottesman
98ed241ce5 [cast-optimizer] Add an "ApplySite" like data structure "SILDynamicCastInst" that abstracts over all dynamic cast instructions.
I am going to use this to refactor a bunch of the goop in the cast optimizer. At
a high level, we are really just performing a giant switch over the casts to
grab different state. We then take that state and we pass it into the bridge
cast optimizer.

To make such code more compact/easier to understand, I am adding in this commit
a type erased dynamic cast instruction type called "SILDynamicCastInst". In
subsequent commits, I wire up each of the individual instructions to it one at a
time.

As an additional advantage it will enable us to take advantage of covered
switches when ever in the future we introduce new casts.
2019-02-26 13:23:54 -08:00
Slava Pestov
23f7c4f6ee SILOptimizer: Fix potential crashes with uses of replaceLoadSequence()
This utility is generally a horrible idea but even worse the
callers were not doing anything to ensure the required
invariants actually held.

Add a new canReplaceLoadSequence() method and chek it in the
right places.
2019-02-21 23:06:21 -05:00
Michael Gottesman
4a1b98c873 Merge pull request #22757 from gottesmm/pr-6e89f4298efb94406a3ad6b55617ae741dfb65e0
[const-expr] Teach ConstExpr how to handle switch_enum/unchecked_enum…
2019-02-20 17:46:08 -08:00
Joe Groff
0cfca9496a Give opened archetypes a generic environment.
And maybe allow nested types to live on them.
2019-02-20 12:52:48 -08:00
Michael Gottesman
c0988d1ccb [const-expr] Teach ConstExpr how to handle switch_enum/unchecked_enum_data in ossa. 2019-02-20 12:39:52 -08:00
Michael Gottesman
f6313d1b18 [cast-opt] When simplifying checked_cast_br, be sure to forward the failing value as the argument to the failure block. 2019-02-18 21:46:00 -08:00
Michael Gottesman
c4cc6e42d9 [const-expr] Teach const-expr evaluator about destructures and translate its main test into an ownership test. 2019-02-18 19:40:05 -08:00
Parker Schuh
b12fcb50db IntegerLiteralExpr now is lowered directly into SIL.
For context, String, Nil, and Bool already behave this way.

Note: Before it used to construct (call, ... (integer_literal)), and the
call would be made explicit / implicit based on if you did eg: Int(3) or
just 3. This however did not translate to the new world so this PR adds
a IsExplicitConversion bit to NumberLiteralExpr. Some side results of
all this are that some warnings changed a little and some instructions are
emitted in a different order.
2019-02-14 11:54:16 -08:00
Michael Gottesman
0cdc2d6a35 [sil] Teach constant folding how to handle destructures and multiple value instructions.
I also ported the constant_propagation.sil tests over for ownership and updated
a few parts of the cast optimizer so that those tests pass with and without
ownership. I purposely only updated the parts of the cast optimizer that crashed
with ownership in the relevant test so that I can add new sil code coverage for
those uncovered code paths.
2019-02-12 20:15:02 -08:00
swift-ci
aead660d91 Merge pull request #22147 from ravikandhadai/consteval-string-append 2019-02-12 16:11:41 -08:00
Michael Gottesman
c4b701980b [ownership] Do not specialize transparent ossa functions called from non-ossa functions. 2019-02-11 19:25:22 -08:00
Slava Pestov
dffa29fd0d AST: Remove a few uses of FunctionType::Param::getOldType() 2019-02-07 23:46:31 -05:00
Doug Gregor
cffe92cc8a Merge pull request #22347 from DougGregor/sil-witness-method-self-proto
[SIL] Eliminate SILFunctionType::getDefaultWitnessMethodProtocol().
2019-02-04 22:49:14 -08:00
Doug Gregor
6609864f4e Drop the ModuleDecl parameter from SILFunctionType::getWitnessMethodClass().
Nothing uses this parameter.
2019-02-04 19:52:16 -08:00
Doug Gregor
2d1fc680ed [SIL] Eliminate SILFunctionType::getDefaultWitnessMethodProtocol().
This method wasn’t returning the protocol on which the that the witness
method would satisfy, as documented. Rather, it was returning the protocol
to which the `Self` type conforms, which could be completely unrelated. For
example, in IndexingIterator’s conformance to IteratorProtocol, this method
would produce the protocol “Collection”, because that’s where the witness
itself was implemented. However, there isn’t necessarily a single such
protocol, so checking for/returning a single protocol was incorrect.

It turns out that there were only a few SIL verifier assertions of it
(that are trivially true) and two actual uses in code:
(1) The devirtualizer was using this computation to decide when it didn’t
need to perform any additional substitutions, but it’s predicate for doing
so was essentially incorrect. Instead, it really wanted to check whether
the Self type is still a type parameter. 
(2) Our polymorphic convention was using it to essentially check whether 
the ’Self’ instance type of a witness_method was a GenericTypeParamType,
which we can check directly.


Fixes rdar://problem/47767506 and possibly the hard-to-reproduce
rdar://problem/47772899.
2019-02-04 16:18:00 -08:00
Michael Gottesman
0c7f5b3064 [cast-opt] Change optimizeMetatypeConversion to return a value and use the new
replace value uses action to make sure that we properly notify passes that we
made the change.

This fixes a latent bug where we were not notifying SILCombine about this
replacement.

Specifically:

-  auto replaceCast = [&](SingleValueInstruction *NewCast) {
-    assert(Ty.getAs<AnyMetatypeType>()->getRepresentation()
-           == NewCast->getType().getAs<AnyMetatypeType>()->getRepresentation());
-    MCI->replaceAllUsesWith(NewCast);
-    EraseInstAction(MCI);
-    return NewCast;
+  auto replaceCast = [&](SILValue newValue) -> SILValue {
+    assert(ty.getAs<AnyMetatypeType>()->getRepresentation() ==
+           newValue->getType().getAs<AnyMetatypeType>()->getRepresentation());
+    ReplaceValueUsesAction(mci, newValue);
+    EraseInstAction(mci);
+    return newValue;
   };

Notice how we use MCI->replaceAllUsesWith instead of one of our replace call
backs. SILCombine hooks these to know if it should re-run users.
2019-02-04 16:13:07 -08:00
Michael Gottesman
21e51edfad [cast-opt] Allow users to pass in a SILBuilderContext to the CastOptimizer.
NOTE: I changed all places that the CastOptimizer is created to just pass in
nullptr for now so this is NFC.

----

Right now the interface of the CastOptimizer is muddled and confused. Sometimes
it is returning a value that should be used by the caller, other times it is
returning an instruction that is meant to be reprocessed by the caller.

This series of patches is attempting to clean this up by switching to the
following model:

1. If we are optimizing a cast of a value, we return a SILValue. If the cast
fails, we return an empty SILValue().

2. If we are optimizing a cast of an address, we return a boolean value to show
success/failure and require the user to use the SILBuilderContext to get the
cast if they need to.
2019-02-04 12:59:44 -08:00
Michael Gottesman
a5b47e127d [cast-opt] Add a new callback: ReplaceValueUsesAction.
This is the first in a series of patches to update the cast optimizer for
ownership and multiple value instructions.

This specific patch is NFC.
2019-02-04 11:26:46 -08:00
Erik Eckstein
767ad5e70a Inliner: fix a stack nesting problem when inlining coroutines
Beside fixing the compiler crash, this change also improves the stack-nesting correction mechanisms in the inliners:

* Instead of trying to correct the nesting after each inlining of a callee, correct the nesting once when inlining is finished for a caller function.
This fixes a potential compile time problem, because StackNesting iterates over the whole function.
In worst case this can lead to quadratic behavior in case many begin_apply instructions with overlapping stack locations are inlined.

* Because we are doing it only once for a caller, we can remove the complex logic for checking if it is necessary.
We can just do it unconditionally in case any coroutine gets inlined.
The inliners iterate over all instruction of a function anyway, so this does not increase the computational complexity (StackNesting is roughly linear with the number of instructions).

rdar://problem/47615442
2019-02-01 08:32:19 -08:00
Michael Gottesman
ed1cd46d25 [ownership] Update devirtualization utilities for ownership.
I discovered this due to the mandatory inliner doing devirtualization. I ported
all of the relevant SIL tests to increase code coverage of this code when
ownership is enabled.
2019-01-31 13:34:08 -08:00
Michael Gottesman
05b6aceb88 Merge pull request #22085 from gottesmm/pr-7a133e2f8de39c6e863d7fc48daf761e36994bfa
[mandatory-inlining] Update for ownership.
2019-01-25 22:06:27 -08:00