Commit Graph

119 Commits

Author SHA1 Message Date
Arnold Schwaighofer
dce6ca766b CastOptimizer: Fix for opaque archetypes
The fallthrough path of this function assumes failure :(.

rdar://50544445
2019-05-07 11:08:00 -07:00
Slava Pestov
1864f6f679 AST: Introduce ClassDecl::isSuperclassOf() 2019-03-26 18:42:59 -04: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
1944254253 SIL: Use SILFunction type lowering APIs in various places 2019-03-01 02:07:16 -05:00
Michael Gottesman
42d184995e [ownership] Update emitIndirectConditionalCastWithScalar for ownership.
This comes up because when we perform mandatory inlining, we perform the
transform as we inline. So the tests for this are in mandatory_inlining
naturally.
2019-02-18 16:16:44 -08:00
Andrew Trick
0976c1f76e Teach the SIL cast optimizer to handle conditional conformance.
Previously the cast optimizer bailed out on any conformance with
requirements.

We can now constant-propagate this:

```
protocol P {}
struct S<E> {
  var e: E
}

extension S : P where E == Int {}

func specializeMe<T>(_ t: T) {
  if let p = t as? P {
    // do fast things.
  }
}

specializeMe(S(e: 0))
```

This turns out to be as simple as calling the TypeChecker.

<rdar://problem/46375150> Inlining does not seem to handle
specialization properly for Data.

This enabled two SIL transformations required to optimize
the code above:

(1) The witness method call can be devirtualized.

(2) The allows expensive dynamic runtime checks such as:

  unconditional_checked_cast_addr Array<UInt8> in %array : $*Array<UInt8> to ContiguousBytes in %protocol : $*ContiguousBytes

Will be converted into:

  %value = init_existential_addr %existential : $*ContiguousBytes, $Array<UInt8>
  store %array to %value : $*Array<UInt8>
2018-12-11 17:37:54 -08:00
Joe Groff
1475d76756 Be conservative with classifying some casts that could change in the future.
- Existentials may eventually be able to conform to protocols
- Functions may grow new arms we haven't designed and implemented yet,
  which could influence how they cast in the future.

rdar://problem/38684452
2018-11-16 12:09:44 -08:00
Michael Gottesman
d57a88af0d [gardening] Rename references to SILPHIArgument => SILPhiArgument. 2018-09-25 22:23:34 -07:00
Doug Gregor
d07fa5ab69 Switch many callers of ClassDecl::getSuperclass() to ClassDecl::getSuperclassDecl().
ClassDecl::getSuperclass() produces a complete interface type describing the
superclass of a class, including any generic arguments (for a generic type).
Most callers only need the referenced ClassDecl, which is (now) cheaper
to compute: switch those callers over to ClassDecl::getSuperclassDecl().

Fixes an existing test for SR-5993.
2018-07-31 10:14:44 -07:00
Mark Lacey
defc293e1b [SIL] Replace a use of getInput() in classifyDynamicCast.
Use getParams() instead.
2018-07-28 23:34:25 -07:00
Mark Lacey
4ffffb110a [SIL] Remove what amounts to dead code from classifyDynamicCast().
The archetype tests in the code being removed will never succeed
because there is code earlier in the function testing for any
archetypes in either type, and that path always returns.

The remaining test is broken (it's comparing source input and target
input and then target input to target result rather than comparing the
source input and target input and then source result and target
result), and is just duplicating the test that happens above.

If we really want to do a better test for substitutability we can do
so at a later time. In the meantime, I don't think it makes sense to
leave redundant comparisons and an unreachable return in place.
2018-07-28 23:00:49 -07:00
Lily Vulcano
1377ab7d7e Turn on ‘as’ bridging on Darwin. 2018-05-30 15:07:22 -07:00
Joe Shajrawi
c49a1ff14e [DynamicCasts] Fix a bug in dynamic casting from optionals to protocols
Assume we have:
Protocol P
An intenal extension to Optional - conforming to P
Compiling under whole module mode
Use of Optional type T?

Given a dynamic cast from T? to P, that cast might succeed even if T itself does not conform to P

rdar://problem/40500142
2018-05-24 16:30:51 -07:00
Ben Langmuir
628b6a1fc7 Revert "Turn on ‘as’ bridging on Linux." 2018-05-17 14:54:35 -07:00
Lily Vulcano
b9455930ee Turn on ‘as’ bridging on Darwin. 2018-05-17 09:59:39 -07:00
David Zarzycki
8c0c55539f [SIL] NFC: Rename misleading getSwiftRValueType() to getASTType()
Reference storage types are not RValues. Also, use more SILType helper
methods to avoid line wrap.
2018-05-04 08:14:38 -04:00
Slava Pestov
175b40919f AST: Fewer headers include Expr.h, Module.h, Stmt.h 2018-04-26 22:55:26 -07:00
Huon Wilson
c08085f359 [SILOptimizer] Don't optimize casts to protocols with a conditional conformance.
The conformance existing isn't enough to be sure the cast will succeed,
there may be dynamic information, so we just assume they're always
dynamic and always "MaySucceed".

Fixes rdar://problem/38694450.
2018-03-28 14:58:13 +11:00
Mark Lacey
d63bb3fc53 Remove most uses of OptionalTypeKind.
What remains are places where we are conflating optionality with
either nullability or failability.
2018-02-10 16:24:09 -08:00
Hamish
ab820497ad [Sema] Don't treat non-class archetypes as non-optional when casting to them 2018-02-07 11:34:30 +00:00
Mark Lacey
b4b66bc8e8 Replace getAnyOptionalObjectType with getOptionalObjectType. 2018-02-05 23:59:00 -08:00
Arnold Schwaighofer
52fb2775c7 Fix the Some Decl case too. 2017-12-18 18:16:40 -08:00
Arnold Schwaighofer
e0ce894fd0 DynamicCasts: Emit a direct copy if soure and target SIL types match
unconditional_checked_cast_addr ImplicitlyUnwrappedOptional<AnyObject> in %136 : *Optional<AnyObject> to *Optional<AnyObject> in %135

rdar://35987160
2017-12-18 15:35:30 -08:00
Erik Eckstein
27acad2159 ConstantPropagation: fix dynamic cast from AnyHashable to a protocol.
rdar://problem/35364339
2017-11-09 14:35:03 -08: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
64830c2d5e PGO: add support for checked_cast_addr_br 2017-09-26 11:14:31 -07:00
Slava Pestov
9f8760b942 AST: Remove unused 'resolver' parameter from ModuleDecl::lookupConformance()
... as well as a bunch of downstream plumbing that is no
longer necessary.
2017-09-07 03:36:17 -07:00
Jordan Rose
1c651973c3 Excise "Accessibility" from the compiler (2/3)
"Accessibility" has a different meaning for app developers, so we've
already deliberately excised it from our diagnostics in favor of terms
like "access control" and "access level". Do the same in the compiler
now that we aren't constantly pulling things into the release branch.

This commit changes the 'Accessibility' enum to be named 'AccessLevel'.
2017-08-28 11:34:44 -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
Roman Levenstein
a0891d64a3 [cast-optimizer] Fix casting of P.Protocol to P.Type
If the source and target are the same existential type, but the source is P.Protocol and the dest is P.Type, then we need to consider whether the protocol is self-conforming.
The only cases where a protocol self-conforms are objc protocols, but we're going to expect P.Type to hold a class object. And this case doesn't matter since for a self-conforming protocol type there can't be any type-level methods.

Thus we consider this kind of cast to always fail. The only exception from this rule is when the target is Any.Type, because *.Protocol can always be casted to Any.Type.

Fixes rdar://32682967
2017-06-15 19:00:25 -07:00
Roman Levenstein
8bf00142ec [cast-optimizer] Dynamic cast of an archetype <T: P> to P will always succeed
This was overlooked by the cast-optimizer so far.
2017-05-25 08:17:36 -07:00
Roman Levenstein
e13ef4182b [cast-optimizer] ObjC-to-Swift casts may fail. And in most cases it is impossible to statically predict the outcome.
So, let's be conservative here and never promise anything.
2017-05-22 16:31:19 -07:00
Joe Groff
4e9851b032 Don't classify bridging casts as WillSucceed if the object-to-value cast can fail.
When casting from an object type to a bridged Swift value type, classifyDynamicCast would use the cast classification for the target type's bridged object type, which would be trivially WillSucceed for thinks like NSNumber-to-Int or NSError-to-SomeError, even though the bridging itself could fail. Fixing this fixes SR-2920|rdar://problem/31404281.
2017-05-12 10:39:39 -07:00
Slava Pestov
f4b91cd118 AST: Remove unused 'resolver' argument from TypeBase::getSuperclass() 2017-04-20 00:37:38 -07:00
John McCall
897f5ab7c5 Restore CanType-based micro-optimizations.
This reverts commit 5036806e5a.
However, it preserves a pair of changes to the SIL optimizer
relating to walking through optional types.
2017-03-14 11:38:11 -04:00
Slava Pestov
5036806e5a AST: Remove some unnecessary getCanonicalType() calls 2017-03-13 02:24:36 -07:00
Joe Groff
d998692b60 Sema/SIL: NSError has no special powers without ObjC interop.
In particular, it doesn't "toll-free bridge" to the Error existential on non-ObjC-interop platforms, and we would miscompile as if it could. This should fix SR-585.
2017-02-25 09:20:24 -08:00
Michael Gottesman
ea1f804207 [semantic-sil] Eliminate ValueOwnershipKind::Any from SILPHIArguments in Semantic SIL.
Most of this involved sprinkling ValueOwnershipKind::Owned in many places. In
some of these places, I am sure I was too cavalier and I expect some of them to
be trivial. The verifier will help me to track those down.

On the other hand, I do expect there to be some places where we are willing to
accept guaranteed+trivial or owned+trivial. In those cases, I am going to
provide an aggregate ValueOwnershipKind that will then tell SILArgument that it
should disambiguate using the type. This will eliminate the ackwardness from
such code.

I am going to use a verifier to fix such cases.

This commit also begins the serialization of ValueOwnershipKind of arguments,
but does not implement parsing of value ownership kinds. That and undef are the
last places that we still use ValueOwnershipKind::Any.

rdar://29791263
2017-01-10 20:05:23 -08:00
Brian Gesiak
663b92ece9 [AST] Completely replace Module with ModuleDecl
The typedef `swift::Module` was a temporary solution that allowed
`swift::Module` to be renamed to `swift::ModuleDecl` without requiring
every single callsite to be modified.

Modify all the callsites, and get rid of the typedef.
2017-01-08 00:36:08 -05:00
practicalswift
6d1ae2a39c [gardening] 2016 → 2017 2017-01-06 16:41:22 +01:00
Roman Levenstein
830e1c36d4 [sil-cast-optimizer] Fix casting between Swift and CF types
If a Swift type T needs to be casted to a CF type, then we first cast T to its bridged NS type and then ref_cast the result to a corresponding CF type.
For example, if we need to cast String to CFString, we first cast String to NSString and then ref_cast the NSString to CFString.

Fixes rdar://problem/29745498
2017-01-04 12:35:51 -08:00
Slava Pestov
064fda52d2 AST: Remove Type::getCanonicalTypeOrNull()
Not sure why but this was another "toxic utility method".
Most of the usages fell into one of three categories:

- The base value was always non-null, so we could just call
  getCanonicalType() instead, making intent more explicit

- The result was being compared for equality, so we could
  skip canonicalization and call isEqual() instead, removing
  some boilerplate

- Utterly insane code that made no sense

There were only a couple of legitimate uses, and even there
open-coding the conditional null check made the code clearer.

Also while I'm at it, make the SIL open archetypes tracker
more typesafe by passing around ArchetypeType * instead of
Type and CanType.
2017-01-04 01:08:29 -08:00
Michael Gottesman
4e8ff35df5 [semantic-sil] Add ValueOwnershipKind field to SILPHIArgument and split Argument creation methods into one for SILPHIArgument and another for SILFunctionArgument.
We preserve the current behavior of assuming Any ownership always and use
default arguments to hide this change most of the time. There are asserts now in
the SILBasicBlock::{create,replace,insert}{PHI,Function}Argument to ensure that
the people can only create SILFunctionArguments in entry blocks and
SILPHIArguments in non-entry blocks. This will ensure that the code in tree
maintains the API distinction even if we are not using the full distinction in
between the two.

Once the verifier is finished being upstreamed, I am going to audit the
createPHIArgument cases for the proper ownership. This is b/c I will be able to
use the verifier to properly debug the code. At that point, I will also start
serializing/printing/parsing the ownershipkind of SILPHIArguments, but lets take
things one step at a time and move incrementally.

In the process, I also discovered a CSE bug. I am not sure how it ever worked.
Basically we replace an argument with a new argument type but return the uses of
the old argument to refer to the old argument instead of a new argument.

rdar://29671437
2016-12-18 14:48:35 -08:00
practicalswift
38be6125e5 [gardening] C++ gardening: Terminate namespaces, fix argument names, ...
Changes:
* Terminate all namespaces with the correct closing comment.
* Make sure argument names in comments match the corresponding parameter name.
* Remove redundant get() calls on smart pointers.
* Prefer using "override" or "final" instead of "virtual". Remove "virtual" where appropriate.
2016-12-17 00:32:42 +01:00
Michael Gottesman
96837babda Merge pull request #5920 from gottesmm/vacation_gardening
Vacation gardening
2016-11-25 09:17:21 -06:00
Michael Gottesman
0a8c54d04f [gardening] Always create new SILArguments using SILBasicBlock::createArgument instead of inline placement new.
The reasoning here is the same as in e42bf07.
2016-11-25 01:14:45 -06: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
8b41745b37 [semantic-arc] Change TypeLowering APIs to use the words {Copy,Destroy}Value instead of {Retain,Release}Value. NFC.
This is an attempt to separate functional from superficial changes in the
semantic arc changes.

rdar://28851920
2016-10-24 23:49:11 -07:00