Commit Graph

4896 Commits

Author SHA1 Message Date
Arnold Schwaighofer
4873f6c431 Fix assert 2019-05-22 10:16:05 -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
Slava Pestov
4038781f84 SIL: Resolve interface types when lowering constant info
This is just a stop-gap until getInterfaceType() becomes a request.
2019-05-18 11:35:05 -04:00
Slava Pestov
6e3609dc22 AST: ProtocolConformanceRef::getTypeWitnessByName() should be an instance method 2019-05-14 19:28:55 -04:00
Andrew Trick
c1bda8f090 Replace AccessedStorage projection with an index.
Further simplify AccessedStorage. Shrink it to two words. Remove the
Projection abstraction and streamline the projection logic.
2019-05-14 12:44:46 -07:00
Andrew Trick
0d3c6144ed Remove RefElementAddr field from AccessedStorage.
- code simplification critical for comprehension
- substantially improves the overhead of AccessedStorage comparison
- as a side effect improves precision of analysis in some cases

AccessedStorage is meant to be an immutable value type that identifies
a storage location with minimal representation. It is used in many global
interprocedural data structures.

The RefElementAddress instruction that it was derived from does not
contribute to the uniqueness of the storage location. It doesn't
belong here. It was being used to create a ProjectionPath, which is an
extremely inneficient way to compare access paths.

Just delete all the code related to that extra field.
2019-05-14 10:45:54 -07:00
Andrew Trick
6befb10d35 Cache struct/class field offsets in SIL.
The field's ordinal value is used by the Projection abstraction, which is
the basis of efficiently comparing and sorting access paths in SIL. It must
be cached before it is used by any SIL passes, including the verifier, or it
causes widespread quadratic complexity.

Fixes <rdar://problem/50353228> Swift compile time regression with optimizations enabled

In production code, a file that was taking 40 minutes to compile now
takes 1 minute, with more than half of the time in LLVM.

Here's a short script that reproduces the problem. It used to take 30s
and now takes 0.06s:

// swift genlazyinit.swift > lazyinit.sil
// sil-opt ./lazyinit.sil --access-enforcement-opts

var NumProperties = 300

print("""
      sil_stage canonical

      import Builtin
      import Swift
      import SwiftShims

      public class LazyProperties {
      """)

for i in 0..<NumProperties {
  print("""
          //  public lazy var i\(i): Int { get set }
          @_hasStorage @_hasInitialValue final var __lazy_storage__i\(i): Int? { get set }
        """)
}

print("""
      }

     // LazyProperties.init()
     sil @$s4lazy14LazyPropertiesCACycfc : $@convention(method) (@owned LazyProperties) -> @owned LazyProperties {
     bb0(%0 : $LazyProperties):
       %enum = enum $Optional<Int>, #Optional.none!enumelt
     """)

for i in 0..<NumProperties {
  let adr = (i*4) + 2
  let access = adr + 1
  print("""
          %\(adr) = ref_element_addr %0 : $LazyProperties, #LazyProperties.__lazy_storage__i\(i)
          %\(access) = begin_access [modify] [dynamic] %\(adr) : $*Optional<Int>
          store %enum to %\(access) : $*Optional<Int>
          end_access %\(access) : $*Optional<Int>
        """)
}

print("""
        return %0 : $LazyProperties
      } // end sil function '$s4lazy14LazyPropertiesCACycfc'
      """)
2019-05-13 16:54:55 -07:00
swift-ci
f243484180 Merge pull request #24716 from gottesmm/pr-ada147d650664b6f8f9c93845c53a9fab1380abf 2019-05-11 15:54:13 -07:00
Michael Gottesman
f385834823 [sil] When the immutable address use verifier errors due to an unknown instruction, print the instruction to improve debuggability. 2019-05-11 14:38:06 -07:00
Michael Gottesman
da37a56896 [sil] Change the immutable address use verifier to ignore br/cond_br uses that introduce address phis and re-enable the test that shows this behavior.
We want to eventually remove address phi arguments from SIL. This will enable
all sorts of nice IRGen optimizations and in general make life better. We are
not there yet, but given that is the direction we are going in, I don't think
there is much use in having to implement this sort of checking for SIL phi
arguments.

rdar://50676315
2019-05-11 14:37:11 -07:00
Slava Pestov
644b6adcd2 AST: Simplify ProtocolConformanceRef::getTypeWitnessByName() 2019-05-10 15:00:48 -04:00
Michael Gottesman
afe3114d3d [sil] Expand immutable address verification to in_guaranteed parameters.
rdar://50212579
2019-05-09 23:01:43 -07:00
Andrew Trick
6b1af3de0e Merge pull request #24458 from atrick/clean-accessed-storage
Remove macros and visitors from AccessedStorage.
2019-05-08 16:23:08 -07:00
Andrew Trick
21f11abe09 Merge pull request #24581 from atrick/cleanup-end-borrow
[NFC] Add LoadBorrow::getEndBorrows() and minor cleanup.
2019-05-07 15:28:05 -07:00
Andrew Trick
207a4f714b Add LoadBorrow::getEndBorrows() and minor cleanup.
Instructions that start a scope should have a (discoverable) method
that retrieves the end of scope. This is a basic structural property
of the instruction.

I removed the makeEndBorrowRange helper because it adds overall
complexity and doesn't provide any value. If some code wants to be
generic over BeginBorrow/LoadBorrow, then that code should have it's
own trivial generic helper:

EndBorrowRange getEndBorrows<T>(T *beginBorrow) {
  return beginBorrow->getEndBorrows()
}
2019-05-07 11:35:10 -07:00
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
Andrew Trick
ca9ae0dda8 Canonicalize nontrivial loads.
This adds support to the load->struct_extract canonicalization for
nontrivial element types which look like:

load [copy]
borrow
struct_extract
...uses...
end_borrow
destroy
2019-05-06 17:23:08 -07:00
Andrew Trick
5cd187d834 Remove macros and visitors from AccessedStorage.
Cleaning up in preparation for making changes that improve
compile-time issues in AccessEnforcementOpts.

This is a simple but important enum with a handful of cases. The cases
need to be easily referenced from the header. Don't define them in a
separate .def. Remove the visitor biolerplate because it doesn't serve
any purpose.

This enum is meant to be used with covered switches. The enum cases do
not have their own types, so there's no performance reason to use a
Visitor pattern.

It should not be possible to add a case to this enum without carefully
considering the impact on the encoding of this class and the impact on
each and every one of the uses. We always want covered switches at the
use sites.

This is a major improvement in readability and usability both in the
definition of the class and in the one place where a visitor was used.
2019-05-02 18:08:27 -07:00
ravikandhadai
d912e3312e Merge pull request #24113 from ravikandhadai/constexpr-skip
[Const evaluator] Add support to "skip" instructions in step-wise evaluation
2019-05-02 15:52:05 -07:00
Arnold Schwaighofer
a0892f98b4 Merge pull request #24443 from aschwaighofer/fix_circular_dependency
Fix circular dependency between SIL and AST libraries
2019-05-02 13:37:48 -07:00
Arnold Schwaighofer
52dc2e6d5b Fix circular dependency between SIL and AST libraries 2019-05-02 12:12:50 -07:00
Michael Gottesman
293f49ed47 Merge pull request #24414 from gottesmm/pr-9e785198e1505f9322f31451df995019d47bb5d2
[sil] Refactor the open_existential_addr immutable use checking into …
2019-05-02 10:39:07 -07:00
Arnold Schwaighofer
aa376a4e66 Merge pull request #24224 from aschwaighofer/specialize_opaque_result_types
Add a pass to specialize opaque type archetypes.
2019-05-02 04:40:54 -07:00
Michael Gottesman
8925b5b510 [sil] Refactor the open_existential_addr immutable use checking into its own verifier utility.
My intention is to use this checker to also verify that in_guaranteed arguments
are used in the same manner immutably. Beyond improving that in_guaranteed
parameters are properly used immutably (which is just goodness), by using the
same check it ensures that we can always inline a callee into a caller with an
open_existential_addr without violating any check on the oea instruction.

I am doing this separately from adding more checks/applying it to
in_guaranteed/fixing some exposed bugs for ease of review. This change is
completely mechanical.

rdar://50212579
2019-05-01 13:57:25 -07:00
Michael Gottesman
4374754217 [sil] Teach the verifier that open_existential_addr immutable is safe to pass as Indirect_InoutAliasable.
We do not consider inout_aliasable to be "truly mutating" since today it is just
used as a way to mark a captured argument and not that something truly has
mutating semantics. The reason why this is safe is that the typechecker
guarantees that if our value was immutable, then the use in the closure must be
immutable as well.

In a future SIL, we want to remove Inout_Aliasable in favor of just using
inout/in_guaranteed using the capture info from the type checker.

rdar://50212579
2019-05-01 10:36:24 -07:00
Arnold Schwaighofer
cec1a5268e More fixes for opaque types specializer
* Opaque types are abi compatible with their substituted types
* Insert casts in more places
* Respect no optimization attribute
2019-05-01 09:31:07 -07:00
Arnold Schwaighofer
768d1c51a1 Address review feedback 2019-05-01 09:31:07 -07:00
Arnold Schwaighofer
fbf09031e4 Add a pass to specialize opaque type archetypes.
Clones functions containing opaque type archetypes replacing the opaque
type by a concrete type.

rdar://46140751
2019-05-01 09:31:07 -07:00
Ravi Kandhadai
b0e56f70f9 [Const evaluator] Enable stepwise constant evaluator to skip
instructions without evaluating them while conservatively accounting
for the effects of the skipped instructions on the interpreter state.
2019-04-30 15:36:29 -07:00
Slava Pestov
b275a88976 SIL: Correctly handle @_alwaysEmitIntoClient properties and subscripts
Fixes <rdar://problem/50058883>.
2019-04-29 16:50:29 -04:00
Slava Pestov
d069cc4147 SIL: Remove SILType::isAddressOnly()/isLoadable() overloads that take a SILModule
Finally we can remove the old overloads.
2019-04-26 22:48:09 -04: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
fb8bd3a056 Merge pull request #24267 from slavapestov/unused-conformances
Remove per-SourceFile "used conformances" lists
2019-04-26 18:10:28 -04:00
Slava Pestov
400063eafe SIL: Handle incomplete conformances in TypeConverter::getLoweredCBridgedType()
Currently Sema completes all _ObjectiveCBridgeable conformances it thinks
are needed in SILGen and runtime dynamic casts, but that's about to change.

Make sure SIL passes a LazyResolver down so that if a conformance doesn't
have a type witness for _ObjectiveCType yet, one can be resolved.

Note that in practice, ClangImporter-synthesized conformances use a special
LazyMemberLoader implementation to fill in type witnesses without any
intervention from the type checker. However, that might go away now that
we have a long-lived type checker instance. Furthermore, various tests use
-enable-source-import and start to fail once Sema no longer completes
_ObjectiveCBridgeable conformances.
2019-04-25 22:27:41 -04:00
Slava Pestov
9ffe12dad2 SIL: Better error when linker finds SILGen did not emit a conformance
I hit this assert a few times while working on making conformance
emission lazier, so let's at least print out the missing conformance
in question.
2019-04-25 22:22:27 -04:00
Saleem Abdulrasool
83b290438c Windows: bridge BOOL to Bool
This allows the conversion of the Windows `BOOL` type to be converted to
`Bool` implicitly.  The implicit bridging allows for a more ergonomic
use of the native Windows APIs in Swift.

Due to the ambiguity between the Objective C `BOOL` and the Windows
`BOOL`, we must manually map the `BOOL` type to the appropriate type.
This required lifting the mapping entry for `ObjCBool` from the mapped
types XMACRO definition into the inline definition in the importer.

Take the opportunity to simplify the mapping code.

Adjust the standard library usage of the `BOOL` type which is now
eclipsed by the new `WindowsBool` type, preferring to use `Bool`
whenever possible.

Thanks to Jordan Rose for the suggestion to do this and a couple of
hints along the way.
2019-04-25 17:52:08 -07:00
swift-ci
d24bc38797 Merge pull request #23701 from DougGregor/property-delegates-as-custom-attributes 2019-04-24 22:51:18 -07:00
Arnold Schwaighofer
43a7b44577 Merge pull request #24201 from aschwaighofer/opaque_result_types_dyn_replacement_and_specialization
Opaque result types: dynamic replacement
2019-04-24 07:58:50 -07:00
ravikandhadai
faa6693eec Merge pull request #24004 from ravikandhadai/constexpr-diagnostics-refactoring
[Const evaluator] Improve diagnostics for unknown symbolic values
2019-04-23 17:10:43 -07:00
Arnold Schwaighofer
f55d68db83 Remove SpecializeOpaqueArchetypes for now 2019-04-23 12:26:13 -07:00
Doug Gregor
2e9f8cf981 Capture a placeholder opaque value expression when needed. 2019-04-23 11:32:28 -07:00
Erik Eckstein
86fb74a34e DI: support assign_by_delegate instruction 2019-04-23 11:32:28 -07:00
Erik Eckstein
2e01b0edeb SIL: add assign_by_delegate instruction
Used for property delegates.
2019-04-23 11:32:28 -07:00
Doug Gregor
cc68b12d1a [SILGen] Initialization of instance properties with property delegates
The initialization of an instance property that has an attached
property delegate involves the initial value written on the property
declaration, the implicit memberwise initializer, and the default
arguments to the implicit memberwise initializer. Implement SILGen
support for each of these cases.

There is a small semantic change to the creation of the implicit
memberwise initializer due to SE-0242 (default arguments for the
memberwise initializer). Specifically, the memberwise initializer will
use the original property type for the parameter to memberwise
initializer when either of the following is true:

  - The corresponding property has an initial value specified with the
    `=` syntax, e.g., `@Lazy var i = 17`, or
  - The corresponding property has no initial value, but the property
    delegate type has an `init(initialValue:)`.

The specific case that changed is when a property has an initial value
specified as a direct initialization of the delegate *and* the
property delegate type has an `init(initialValue:)`, e.g.,

```swift
struct X {
  @Lazy(closure: { ... })
  var i: Int
}
```

Previously, this would have synthesized an initializer:

```swift
init(i: Int = ???) { ... }
```

However, there is no way for the initialization specified within the
declaration of i to be expressed via the default argument. Now, it
synthesizes an initializer:

```swift
init(i: Lazy<Int> = Lazy(closure: { ... }))
```
2019-04-23 11:31:59 -07:00
Doug Gregor
9c62420809 [AST] Generalize PatternBindingEntry's "Lazy" flag to "Subsumed"
The initializer associated with a lazy property should not be executed
directly, because it is subsumed by code synthesized into the
getter. Generalize the terminology here so we can re-use this path for
property delegate initialization.
2019-04-23 11:31:58 -07:00
Arnold Schwaighofer
7ccdd00fb1 Add a pass to specialize opaque type archetypes.
Clones functions containing opaque type archetypes replacing the opaque
type by a concrete type.

rdar://46140751
2019-04-22 07:38:09 -07:00
Arnold Schwaighofer
84c7b77d02 Make opaque type descriptors dynamically replaceable
This is to support dynamic function replacement of functions with opaque
result type.

This approach requires that all state is thrown away (that could contain the
old returned type for an opaque type) between replacements.

rdar://48887938
2019-04-22 07:31:07 -07:00
Ravi Kandhadai
d9a3f9c1b8 [const evaluator] Improving diagnostics generation for
unknown symbolic values by renaming some diagnostics and
creating new unknown reasons for each type of failure that
can happen during constant evaluation.
2019-04-18 13:17:17 -07:00
Joe Groff
0255baa97f SILGen: Start supporting opaque result types resiliently.
Tear out the hacks to pre-substitute opaque types before they enter the SIL type system.
Implement UnderlyingToOpaqueExpr as bitcasting the result of the underlying expression from the
underlying type to the opaque type.
2019-04-17 14:43:32 -07:00
Joe Groff
e3bbd8ce9e Remove ResilienceExpansion from substOpaqueTypes for now.
It's currently meaningless, and it'll require thought to pass the correct value when it becomes
meaningful.
2019-04-17 14:43:32 -07:00