Commit Graph

446 Commits

Author SHA1 Message Date
Doug Gregor
1dd24a6175 [SILGen] Make sure we emit lazy conformances for @asyncHandlers.
Fixes rdar://73798572.
2021-02-01 22:23:35 -08:00
Erik Eckstein
ec64f2a255 SILLocation: replace CleanupLocation::get(loc) with CleanupLocation(loc)
No need to have a static get function - the constructor can be used directly.
NFC
2021-01-29 20:28:21 +01:00
Erik Eckstein
9987ae9b3e [Concurrency] fix symbol linkage of asyncHandler class methods.
Fixes a TBD verification error.

rdar://72329062
2020-12-16 13:28:54 +01:00
Erik Eckstein
8e03bd3e67 [concurrency] SILGen: emit @asyncHandler functions.
An asyncHandler function is split into two functions:
1. The asyncHandler body function: it contains the body of the function, but is emitted as an async function.
2. The original function: it just contains
      _runAsyncHandler(operation: asyncHandlerBodyFunction)

rdar://problem/71247879
2020-12-01 08:48:40 +01:00
Doug Gregor
dab6fb7098 [Concurrency] Implement SIL generation for "async let".
Implement SIL generation for "async let" constructs, which involves:

1. Creating a child task future at the point of declaration of the "async let",
which runs the initializer in an async closure.
2. Entering a cleanup to destroy the child task.
3. Entering a cleanup to cancel the child task.
4. Waiting for the child task when any of the variables is reference.
5. Decomposing the result of the child task to write the results into the
appropriate variables.

Implements rdar://71123479.
2020-11-27 22:50:39 -08:00
Doug Gregor
97c5e2484a [Concurrency] Emit @asyncHandler bodies as traps.
@asyncHandler is currently unimplemented in SILGen, and will cause
SIL verifier assertions if used. Rather than trigger assertions, emit
a trap for the body. Obviously, this is a temporary hack.
2020-11-17 23:59:31 -08:00
Michael Gottesman
c026e95cce [ownership] Extract out SILOwnershipKind from ValueOwnershipKind into its own type and rename Invalid -> Any.
This makes it easier to understand conceptually why a ValueOwnershipKind with
Any ownership is invalid and also allowed me to explicitly document the lattice
that relates ownership constraints/value ownership kinds.
2020-11-10 14:29:11 -08:00
Holly Borla
355fbb3a8b Merge pull request #34109 from hborla/local-property-wrappers
[Property Wrappers] Support local property wrappers
2020-10-01 22:45:41 -07:00
Holly Borla
4bb98baf13 [SILGen] Add a new CaptureEmission kind specifically for emitting
captured local variables for the assign_by_wrapper setter.

Since assign_by_wrapper will always be re-written to initialization
if the captured local variable is uninitialized, it's unnecessary
to mark the capture as an escape. This lets us support out-of-line
initialization for local property wrappers.
2020-09-30 10:40:44 -07:00
Varun Gandhi
6dfdb7b548 [NFC] Clean up construction of ExtInfo(Builder). 2020-09-24 00:38:45 -07:00
Varun Gandhi
7b967a8030 Merge pull request #33085 from varungandhi-apple/vg-clang-types-in-sil-retry
Propagate Clang function types through SIL
2020-09-22 08:48:34 -07:00
Varun Gandhi
5e9bf1f7c6 [SIL] Store ClangTypeInfo in SILFunctionType.
This patch includes a large number of changes to make sure that:
1. When ExtInfo values are created, we store a ClangTypeInfo if applicable.
2. We reduce dependence on storing SIL representations in ASTExtInfo values.
3. Reduce places where we sloppily create ASTExtInfo values which should
   store a Clang type but don't. In certain places, this is unavoidable;
   see [NOTE: ExtInfo-Clang-type-invariant].

Ideally, we would check that the appropriate SILExtInfo does always store
a ClangTypeInfo. However, the presence of the HasClangFunctionTypes option
means that we would need to condition that assertion based on a dynamic check.
Plumbing the setting down to SILExtInfoBuilder's checkInvariants would be too
much work. So we weaken the check for now; we should strengthen it once we
"turn on" HasClangFunctionTypes and remove the dynamic feature switch.
2020-09-16 10:34:42 -07:00
Vedant Kumar
ab817e83e0 [Profiler] Increment function body count prior to the prolog
This fixes another 'SILBuilder has no valid insertion point' assertion
failure seen when compiling Carthage.

rdar://68759819
2020-09-15 11:09:24 -07:00
Vedant Kumar
43935527f2 [Profiler] Increment closure body count prior to the prolog (#33946)
This fixes a 'SILBuilder has no valid insertion point' assertion failure
seen when compiling various projects from the source compat suite.

rdar://68759819
2020-09-15 10:45:24 -07:00
Hamish Knight
efdc358eb4 Merge pull request #33806 from hamishknight/typed-def 2020-09-11 16:56:46 +01:00
Brent Royal-Gordon
cff4ddf13a [NFC] Adopt new ImportPath types and terminology
# Conflicts:
#	lib/IDE/CodeCompletion.cpp
2020-09-10 19:07:49 -07:00
Hamish Knight
28246a7596 [SILGen] Use getTypecheckedBody
Use `getTypecheckedBody` to allow lazy
type-checking when emitting function definitions.
2020-09-04 13:24:35 -07:00
Nate Chandler
94b5f76654 Revert "[SIL] Add SILFunctionType flag for async."
This reverts commit 9b8828848d.
2020-08-25 13:37:26 -07:00
Nate Chandler
9b8828848d [SIL] Add SILFunctionType flag for async. 2020-08-19 11:29:58 -07:00
Varun Gandhi
f219e58ada [NFC] Refactor ExtInfo to use a builder-pattern based API.
Since the two ExtInfos share a common ClangTypeInfo, and C++ doesn't let us
forward declare nested classes, we need to hoist out AnyFunctionType::ExtInfo
and SILFunctionType::ExtInfo to the top-level.

We also add some convenience APIs on (AST|SIL)ExtInfo for frequently used
withXYZ methods. Note that all non-default construction still goes through the
builder's build() method.

We do not add any checks for invariants here; those will be added later.
2020-07-31 13:55:55 -07:00
Josh Learn
768a404c0c Merge pull request #32911 from guitard0g/oslog_profiling
[Constant Evaluator] Skip code-coverage instrumentation during constant evaluation
2020-07-20 15:29:01 -04:00
Josh Learn
7cbc21b60a Allow constant interpreter to skip builtin instructions from profiling instrumentation 2020-07-17 13:30:30 -04:00
Holly Borla
28a817b271 [Property Wrappers] Treat default wrapped values that are noescape
autoclosures the same way we treat default arguments that are
noescape autoclosures.
2020-07-15 10:02:35 -07:00
Holly Borla
a6c33a9611 [Property Wrappers] Use autoclosure information from CSApply to compute
`VarDecl::getPropertyWrapperInitValueInterfaceType`.
2020-07-13 17:22:29 -07:00
Arnold Schwaighofer
825a2a259b Mark non-foreign entry points of @objc dynamic methods in generic classes dynamically_replaceable
```
class Generic<T> {
  @objc dynamic func method() {}
}

extension Generic {
  @_dynamicReplacement(for:method())
  func replacement() {}
}
```

The standard mechanism of using Objective-C categories for dynamically
replacing @objc methods in generic classes does not work.

Instead we mark the native entry point as replaceable.

Because this affects all @objc methods in generic classes (whether there
is a replacement or not) by making the native entry point
`[dynamically_replaceable]` (regardless of optimization mode) we guard this by
the -enable-implicit-dynamic flag because we are late in the release cycle.

* Replace isNativeDynamic and isObjcDynamic by calls to shouldUse*Dispatch and
  shouldUse*Replacement
  This disambiguates between which dispatch method we should use at call
  sites and how these methods should  implement dynamic function
  replacement.

* Don't emit the method entry for @_dynamicReplacement(for:) of generic class
  methods
  There is not way to call this entry point since we can't generate an
  objective-c category for generic classes.

rdar://63679357
2020-06-09 09:23:29 -07:00
Arnold Schwaighofer
147144baa6 SIL: Thread type expansion context through to function convention apis
This became necessary after recent function type changes that keep
substituted generic function types abstract even after substitution to
correctly handle automatic opaque result type substitution.

Instead of performing the opaque result type substitution as part of
substituting the generic args the underlying type will now be reified as
part of looking at the parameter/return types which happens as part of
the function convention apis.

rdar://62560867
2020-05-04 13:53:30 -07:00
Anthony Latsis
74252028ca AST: Rename getFullName -> getName on ValueDecl & MissingMemberDecl 2020-04-23 05:16:55 +03:00
Nate Chandler
81dc64fee3 @main: Enable main function to throw.
SE-0281 was accepted with the modification that the main function should
be allowed to be throwing.  Here support for enabling that is added.

Support is implemented in two steps:

(1) The $main wrapper function is modified to be throwing

static func $main() throws {
  return try main()
}

whenever the main function is throwing (it remains non-throwing when
$main is not throwing).

(2) The @main entry point is modified to be

sil [ossa] @main : $@convention(c) (Int32, UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>) -> Int32 {
entry(%argc : $Int32, %argv : $UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>):
  %the_main_type = metatype $@thin TheMainType.Type
  %the_main_func = function_ref @`TheMainType.main()` : $@convention(method) (@thin TheMainType.Type) -> @error Error
  try_apply %the_main_func(%the_main_type) : $@convention(method) (@thin TheMainType.Type) -> @error Error, normal success, error failure

success(%_ : $()):
  %success_code_builtin_int32 = integer_literal $Builtin.Int32, 0
  br bb1(%success_code_builtin_int32 : $Builtin.Int32)

failure(%error : @owned $Error):
  %_ = builtin "errorInMain"(%error : $Error) : $()
  end_lifetime %error : $Error
  %error_code_builtin_int32 = integer_literal $Builtin.Int32, 1
  br bb1(%error_code_builtin_int32 : $Builtin.Int32)

exit(%code_builtin_int32 : $Builtin.Int32):
  %code = struct $Int32 (%code_builtin_int32 : $Builtin.Int32)
  return %code : $Int32
}

whenever the main function is throwing (and consequently $main also is).

In the non-throwing case, (a) the try_apply instruction is replaced with an
apply instruction, (b) the body of the success block is appended to the
entry block, and (c) the success and failure blocks are removed.
2020-04-17 11:33:31 -07:00
Nate Chandler
2676c2aed2 @main: Allowed attribute on extensions.
Previously the @main attribute could only be applied to
NominalTypeDecls.  Here, that limitation is lifted so that the attribute
can be applied to ExtensionDecls.
2020-04-17 09:53:46 -07:00
Nate Chandler
c545e4d125 @main: Added $main func to @main type.
Previously, the function was being added to the list of top level decls
in the source file as that list was being iterated, leading to iterator
issues.

Here, the function is instead added to the nominal type decl which is
decorated with @main.  Doing so requires calling the $main function with
the metatype for the nominal type.

The workaround for the iterator invalidation issue which had been
applied previously, namely changing the type of the Decls member of
SourceFile from std::vector<Decl *> to SmallVector<Decl *, 16> is
reverted.
2020-04-17 09:53:46 -07:00
Nate Chandler
df99de804d Added executable entry-point via @main type.
When a type (class, enum, or struct) is annotated @main, it is required
to provide a function with the following signature:

  static func main() -> ()

That function will be called when the executable the type is defined
within is launched.
2020-04-17 09:53:46 -07:00
Anthony Latsis
c63b737e92 Collapse all indirect equivalents to ValueDecl::getBaseIdentifier 2020-03-29 00:36:01 +03:00
Roopesh Chander
9c2dca7d6a [Property wrappers] Fix handling of @autoclosure in init(wrappedValue:)
If the 'wrappedValue:' parameter is an escaping autoclosure, and a
struct property is marked with that property wrapper, the memberwise
initializer of the struct is now synthesized with an escaping
autoclosure for that property.
2020-03-20 12:20:52 -07:00
Meghana Gupta
8e800e49bf Recommit #29812 with fixes (#30342) 2020-03-13 19:34:16 -07:00
Rintaro Ishizaki
ccbc26d947 Revert "Use in_guaranteed for let captures (#29812)"
This reverts commit 13b9915c6f.
2020-03-10 16:08:08 -07:00
Meghana Gupta
13b9915c6f Use in_guaranteed for let captures (#29812)
* Use in_guaranteed for let captures

With this all let values will be captured with in_guaranteed convention
by the closure. Following are the main changes :

SILGen changes:
- A new CaptureKind::Immutable is introduced, to capture let values as in_guaranteed.
- SILGen of in_guaranteed capture had to be fixed.
  in_guaranteed captures as per convention are consumed by the closure. And so SILGen should not generate a destroy_addr for an in_guaranteed capture.
  But LetValueInitialization can push Dealloc and Release states of the captured arg in the Cleanup stack, and there is no way to access the CleanupHandle and disable the emission of destroy_addr while emitting the captures in SILGenFunction::emitCaptures.
  So we now create, temporary allocation of the in_guaranteed capture iduring SILGenFunction::emitCaptures without emitting destroy_addr for it.

SILOptimizer changes:
- Handle in_guaranteed in CopyForwarding.
- Adjust dealloc_stack of in_guaranteed capture to occur after destroy_addr for on_stack closures in ClosureLifetimeFixup.

IRGen changes :
  - Since HeapLayout can be non-fixed now, make sure emitSize is used conditionally
  - Don't consider ClassPointerSource kind parameter type for fulfillments while generating code for partial apply forwarder.
    The TypeMetadata of ClassPointSource kind sources are not populated in HeapLayout's NecessaryBindings. If we have a generic parameter on the HeapLayout which can be fulfilled by a ClassPointerSource, its TypeMetaData will not be found while constructing the dtor function of the HeapLayout.
    So it is important to skip considering sources of ClassPointerSource kind, so that TypeMetadata of a dependent generic parameters gets populated in HeapLayout's NecessaryBindings.
2020-03-10 12:23:02 -07:00
John McCall
ceff414820 Distinguish invocation and pattern substitutions on SILFunctionType.
In order to allow this, I've had to rework the syntax of substituted function types; what was previously spelled `<T> in () -> T for <X>` is now spelled `@substituted <T> () -> T for <X>`.  I think this is a nice improvement for readability, but it did require me to churn a lot of test cases.

Distinguishing the substitutions has two chief advantages over the existing representation.  First, the semantics seem quite a bit clearer at use points; the `implicit` bit was very subtle and not always obvious how to use.  More importantly, it allows the expression of generic function types that must satisfy a particular generic abstraction pattern, which was otherwise impossible to express.

As an example of the latter, consider the following protocol conformance:

```
protocol P { func foo() }
struct A<T> : P { func foo() {} }
```

The lowered signature of `P.foo` is `<Self: P> (@in_guaranteed Self) -> ()`.  Without this change, the lowered signature of `A.foo`'s witness would be `<T> (@in_guaranteed A<T>) -> ()`, which does not preserve information about the conformance substitution in any useful way.  With this change, the lowered signature of this witness could be `<T> @substituted <Self: P> (@in_guaranteed Self) -> () for <A<T>>`, which nicely preserves the exact substitutions which relate the witness to the requirement.

When we adopt this, it will both obviate the need for the special witness-table conformance field in SILFunctionType and make it far simpler for the SILOptimizer to devirtualize witness methods.  This patch does not actually take that step, however; it merely makes it possible to do so.

As another piece of unfinished business, while `SILFunctionType::substGenericArgs()` conceptually ought to simply set the given substitutions as the invocation substitutions, that would disturb a number of places that expect that method to produce an unsubstituted type.  This patch only set invocation arguments when the generic type is a substituted type, which we currently never produce in type-lowering.

My plan is to start by producing substituted function types for accessors.  Accessors are an important case because the coroutine continuation function is essentially an implicit component of the function type which the current substitution rules simply erase the intended abstraction of.  They're also used in narrower ways that should exercise less of the optimizer.
2020-03-07 16:25:59 -05:00
Robert Widmann
de72824b04 [Gardening] Canonicalize usages of ASTContext::Stats 2020-02-27 17:12:58 -08:00
Slava Pestov
3547122997 SILGen: Simplify prepareEpilog() utility method 2020-02-18 17:43:54 -05:00
Varun Gandhi
29cc1b6195 Revert "[AST] Store Clang type in SILFunctionType for @convention(c) functions."
This reverts commit 5f45820755.
2020-01-22 09:04:52 -08:00
Varun Gandhi
5f45820755 [AST] Store Clang type in SILFunctionType for @convention(c) functions. 2020-01-17 16:22:39 -08:00
Varun Gandhi
022314a640 Merge pull request #28643 from kitaisreal/using-located-instead-of-pair
[Compiler]: Using Located<T> instead of std::pair<SourceLoc, T>
2020-01-06 14:22:29 -08:00
Slava Pestov
a99f24f777 Merge pull request #28895 from slavapestov/prep-for-sr-75
Preparations for building curry thunks in Sema
2019-12-20 11:31:43 -05:00
Kita, Maksim
b7cb3b67bf SR-11889: Using Located<T> instead of std::pair<SourceLoc, T> 2019-12-20 17:18:58 +03:00
Slava Pestov
4017a16684 Revert "SILGen: Simplify SILGenFunction::emitClosure()"
This change has a subtle impact on debug info emission and causes
a regression with a subsequent patch I'm about to commit.

This reverts commit 62d1adb409.
2019-12-19 23:51:53 -05:00
Slava Pestov
8214e18850 SILGen: Clean up invariants around capture lists 2019-12-19 23:47:24 -05:00
Slava Pestov
62d1adb409 SILGen: Simplify SILGenFunction::emitClosure() 2019-12-13 15:39:47 -05:00
Brent Royal-Gordon
6a8598a99c [NFC] Remove DeclNameRef staging calls 2019-12-11 00:55:18 -08:00
Brent Royal-Gordon
da88512eda [NFC] Take DeclNameRef in UnqualifiedLookup and lookupQualified() 2019-12-11 00:55:17 -08:00
Doug Gregor
493bed0043 [Property wrappers] Fix crash due to wrapped type/wrapper type confusion.
We had two predicates that were used to determine whether the default
argument for a wrapped property in the memberwise initializer would be
of the wrapper type (e.g., Lazy<Int>) vs. the wrapped type
(Int). Those two predicates could disagree, causing a SILGen assertion
and crash. Collapse the two predicates into one correct one,
fixing rdar://problem/57545381.
2019-12-05 09:27:30 -08:00