Commit Graph

1275 Commits

Author SHA1 Message Date
Joe Groff
15973e3b87 Merge pull request #73041 from jckarter/silgen-borrow-noncopyable-global-lets
SILGen: Emit references to noncopyable global storage directly as a borrow.
2024-04-16 07:51:50 -07:00
Joe Groff
4077c75f8a SILGen: Emit references to noncopyable global storage directly as a borrow.
Later analyses are too conservative to remove a copy, but it should be fairly safe to
elide the copy for noncopyable globals, since accesses are tightly scoped and dynamically checked,
so consumes aren't possible, and borrows and inout accesses of mutable globals are dynamically
guarded and not subject to exclusivity checks. rdar://114329759
2024-04-15 16:12:12 -07:00
Konrad `ktoso` Malawski
460a030860 [Distributed] Avoid infinite recursion in distributed thunk on protocol extensions (#73032) 2024-04-15 03:43:49 -07:00
Anton Korobeynikov
d84847ac9d Reland Allow normal function results of @yield_once coroutines (#71645)
* Allow normal function results of @yield_once coroutines

* Address review comments

* Workaround LLVM coroutine codegen problem: it assumes that unwind path never returns.
This is not true to Swift coroutines as unwind path should end with error result.
2024-03-27 13:09:02 -07:00
Konrad `ktoso` Malawski
6132386371 [Distributed] Complete handling of protocol calls and witnesses using adjusted mangling scheme (#72416) 2024-03-23 23:54:23 +09:00
John McCall
c7d3e8f559 [NFC] Generalize how Conversion works with reabstraction conversions and
force callers to specify the input lowered type as well.
2024-03-21 12:47:56 -04:00
Holly Borla
2260957ac5 [Concurrency] Don't attempt to hop to executor inside default argument
generators and stored property initializers.
2024-03-14 09:33:55 -07:00
Konrad `ktoso` Malawski
143d8f9801 [Distributed] Handle distributed thunk requirements/witnesses (#72151)
Co-authored-by: Pavel Yaskevich <pyaskevich@apple.com>
2024-03-11 02:07:22 -07:00
Ben Barham
f292ec9784 Use the new template deduction guides rather than makeArrayRef
LLVM has removed `make*ArrayRef`, migrate all references to their
constructor equivalent.
2024-02-23 20:04:51 -08:00
John McCall
5c2cd18a5b Teach SILGen to try to peephole a function conversion into the emission of
a closure expression, then don't actually do it.  The long term plan is
to actually do this, which should just be a matter of taking some of the
code out of reabstraction thunk emission and using it in prolog/epilog/return
emission.  In the short term, the goal is just to get the conversion
information down to the closure emitter so that we can see that we're
erasing into an `@isolated(any)` type and then actually erase the
closure's isolation properly instead of relying on type-based erasure,
which can't handle parameter/capture isolation correctly.
2024-02-23 02:59:39 -05:00
Joe Groff
7f9b18432a Merge pull request #71795 from jckarter/yield-read-coroutine-result
SILGen: Emit the base of a read coroutine as a borrow in a `yield`/`borrow`/noncopyable context.
2024-02-21 20:40:36 -08:00
Ben Barham
5637284e48 Merge pull request #71368 from bnbarham/std-optional-all-the-things
Migrate llvm::Optional to std::optional
2024-02-21 16:54:00 -08:00
Allan Shortlidge
898c024c2d Merge pull request #71771 from tshortli/magic-literal-unknown-loc-assert
SILGen: Avoid expanding #line directives with invalid source locations
2024-02-21 16:21:29 -08:00
Joe Groff
eca40f3d63 SILGen: Emit the base of a read coroutine as a borrow in a yield/borrow/noncopyable context. 2024-02-21 14:35:53 -08:00
Joe Groff
165b3e8e03 Merge pull request #71784 from jckarter/move-only-wrapped-yield
SILGen: Unwrap `@moveOnly` wrapped values before yielding them.
2024-02-21 13:31:39 -08:00
Ben Barham
ef8825bfe6 Migrate llvm::Optional to std::optional
LLVM has removed llvm::Optional, move over to std::optional. Also
clang-format to fix up all the renamed #includes.
2024-02-21 11:20:06 -08:00
Allan Shortlidge
cf3031794f SILGen: Avoid expanding #line directives with invalid source locations.
Synthesized stub constructors contain `#line` directives and under certain
circumstances these directives can have invalid source locations. SILGen would
trigger an assert (or invoke UB in non-asserts builds) when attempting to
expand these directives into literal values. Check the source location for
validity before translating the location to a location in the outermost source
file.

Resolves rdar://121971741
2024-02-20 20:07:16 -08:00
Joe Groff
88192ffb70 SILGen: Unwrap @moveOnly wrapped values before yielding them. 2024-02-20 16:40:36 -08:00
John McCall
868fc6ad46 Basic SILGen for @isolated(any).
The main piece that's still missing here is support for closures;
they actually mostly work, but they infer the wrong isolation for
actor-isolated closures (it's not expressed in the type, so obviously
they're non-isolated), so it's not really functional.  We also have
a significant problem where reabstraction thunks collide incorrectly
because we don't mangle (or represent!) formal isolation into
SILFunctionType; that's another follow-up.  Otherwise, I think SILGen
is working.
2024-02-19 21:21:03 -05:00
John McCall
2d71c604ed Emit enum element arguments directly into the enum payload.
This generates significantly better code directly out of SILGen, at
the cost of having to reimplement a little bit of the argument-emission
logic to handle default arguments.  But it also neatly sidesteps the
problems we have with splitting tuple RValues when the tuple contains
a pack expansion, which will require some significant surgery to RValue
to fix.  That, in turn, fixes rdar://121489308.
2024-02-15 01:09:12 -05:00
Andrew Trick
da3f583492 Add mark_dependence [unresolved]
In preparation for inserting mark_dependence instructions for lifetime
dependencies early, immediately after SILGen. That will simplify the
implementation of borrowed arguments.

Marking them unresolved is needed to make OSSA verification
conservative until lifetime dependence diagnostics runs.
2024-02-08 22:53:16 -08:00
Nate Cook
e317febc9d Revert "Allow normal function results of @yield_once coroutines (#69843)"
This reverts commit aa5b505014.
2024-02-07 14:57:31 -06:00
John McCall
60807a76a4 Merge pull request #71433 from rjmccall/isolated-any-typechecking
Experimental type-checking support for `@isolated(any)` function types.
2024-02-07 14:07:53 -05:00
Anton Korobeynikov
aa5b505014 Allow normal function results of @yield_once coroutines (#69843)
This adds SIL-level support and LLVM codegen for normal results of a coroutine.

The main user of this will be autodiff as VJP of a coroutine must be a coroutine itself (in order to produce the yielded result) and return a pullback closure as a normal result.

For now only direct results are supported, but this seems to be enough for autodiff purposes.
2024-02-06 22:13:15 -08:00
John McCall
2f8a33cf0a Experimental type-checking support for @isolated(any) function types. 2024-02-06 22:54:27 -05:00
Joe Groff
2092240072 Merge pull request #71406 from jckarter/borrowing-switch-5
SILGen: Emit borrowing switch subjects under a formal access.
2024-02-06 08:12:32 -08:00
Joe Groff
2f519f42c6 SILGen: Emit borrowing switch subjects under a formal access.
Ensure that dependent accesses are properly nested when a subject isn't directly
a borrowed parameter binding.
2024-02-05 19:39:10 -08:00
Nate Chandler
9f134ea0e3 [SILGen] Look thru loads for reference storage.
When emitting a guaranteed argument, a search is made for storage which
can be borrowed.  Look through LoadExprs during this search.
2024-02-02 16:04:00 -08:00
Allan Shortlidge
0d25e6ea73 SILGen: Avoid unnecessary use of a back deployment thunk for calls to _diagnoseUnavailableCodeReached().
When the deployment target is Swift 5.9 aligned or higher, we should not need
to call `_diagnoseUnavailableCodeReached()` via a back-deployment thunk since
the function will always be available in the standard library.

Resolves rdar://121878128
2024-01-31 16:12:36 -08:00
Arnold Schwaighofer
edd7eaa0d7 OSLog: os_log strings should be in the __oslogstring section on machO
rdar://121384422
2024-01-26 08:30:56 -08:00
Kavon Farvardin
107475b224 SILType: Avoid asking ASTType if it is Noncopyable
It's better to ask SILType if it is MoveOnly than go to the AST type and
 ask if it is noncopyable, because some types in SIL do not have a
 well-defined notion of conformance in the AST.
2024-01-23 22:42:38 -08:00
Holly Borla
47afd21d85 [Concurrency] Remove ActorIsolation::GlobalActorUnsafe. 2024-01-21 21:05:33 -08:00
Slava Pestov
14d1fcb51a AST: TypeChecker::conformsToProtocol() => ModuleDecl::checkConformance() 2024-01-16 17:08:00 -05:00
Holly Borla
5ac839b11d [SILGen] Only hop to the callee isolation domain once when evaluating isolated
default arguments.
2024-01-10 20:18:00 -08:00
Andrew Trick
119d40d99b Merge pull request #70705 from atrick/markdep-nonescaping
Add mark_dependence [nonescaping] flag.
2024-01-05 11:33:33 -08:00
Andrew Trick
264cbaea42 Add mark_dependence [nonescaping] flag.
The dependent 'value' may be marked 'nonescaping', which guarantees that the
lifetime dependence is statically enforceable. In this case, the compiler
must be able to follow all values forwarded from the dependent 'value', and
recognize all final (non-forwarded, non-escaping) use points. This implies
that `findPointerEscape` is false. A diagnostic pass checks that the
incoming SIL to verify that these use points are all initially within the
'base' lifetime. Regular 'mark_dependence' semantics ensure that
optimizations cannot violate the lifetime dependence after diagnostics.
2024-01-04 14:47:35 -08:00
John McCall
d3058cbf98 Fix expanded argument emission for tuple expressions in pack context.
Fixes #70187
2024-01-04 15:55:44 -05:00
John McCall
5f0357fdb0 Fix the abstraction pattern for calls to methods on variadically generic
classes.
2024-01-03 21:00:56 -05:00
Allan Shortlidge
9a0eb73302 Merge pull request #70547 from tshortli/diagnose-unreachable-cant-back-deploy
AST/SILGen: Use @_alwaysEmitIntoClient diagnostic helper in unavailable code
2023-12-20 00:16:29 -08:00
Allan Shortlidge
6d22433d0f AST/SILGen: Use @_alwaysEmitIntoClient diagnostic helper in unavailable code.
The `_diagnoseUnavailableCodeReached()` function was introduced in the Swift
5.9 standard library and employs `@backDeployed` to support compilation of
binaries that target OS releases aligned with earlier Swift releases.
Unfortunately, though, this backdeployment strategy doesn't work well for some
unusual build environments. Specifically, in some configurations code may be
built with a compiler from a recent Swift toolchain and then linked against the
dylibs in an older toolchain. When linking against the older dylibs, the
`_diagnoseUnavailableCodeReached()` function does not exist but the
`@backDeployed` thunks emitted into the binary reference that function and
therefore linking fails.

The idea of building with one toolchain and then linking to the dylibs in a
different, older toolchain is extremely dubious. However, it exists and for now
we need to support it. This PR introduces an alternative
`_diagnoseUnavailableCodeReached()` function that is annotated with
`@_alwaysEmitIntoClient`. Calls to the AEIC variant are now emitted by the
compiler when the deployment target is before Swift 5.9.

Once these unusual build environments upgrade and start linking against a Swift
5.9 toolchain or later we can revert all of this.

Resolves rdar://119046537
2023-12-19 16:26:56 -08:00
Kavon Farvardin
06ac850d74 [SILGen] avoid hop before autoreleased foreign error is retained
For an isolated ObjC function that is not async, we
emit a hops around the call. But if that function
returns an autoreleased pointer, we need to ensure
we're retaining that pointer before hopping back
after the call. We weren't doing that in the case
of an autoreleased NSError:

```
%10 = alloc_stack $@sil_unmanaged Optional<NSError>
%19 = ... a bunch of steps to wrap up %10 ...
%20 = enum $Optional<AutoreleasingUnsafeMutablePointer<Optional<NSError>>>, #Optional.some!enumelt, %19 : $AutoreleasingUnsafeMutablePointer<Optional<NSError>>
hop_to_executor $MainActor
%26 = apply X(Y, %20) : $@convention(objc_method) (NSObject, Optional<AutoreleasingUnsafeMutablePointer<Optional<NSError>>>) -> @autoreleased Optional<NSString>
hop_to_executor $Optional<Builtin.Executor>
// retain the autoreleased pointer written-out.
%28 = load [trivial] %10 : $*@sil_unmanaged Optional<NSError>
%29 = unmanaged_to_ref %28 : $@sil_unmanaged Optional<NSError> to $Optional<NSError>
%30 = copy_value %29 : $Optional<NSError>
assign %31 to %7 : $*Optional<NSError>
```

This patch sinks the hop emission after the call
so it happens after doing that copy.

rdar://114049646
2023-12-19 13:16:59 -08:00
Joe Groff
1b9a0712bf Move-only check the value projected from addressors.
Mark the result of a move-only addressor as unresolved. The pointed-at value
cannot be consumed so ensure that only [read] or [modify] accesses are
performed. Update the move-only checker to recognize code patterns
from addressors.
2023-12-15 10:10:44 -08:00
Joe Groff
957443e16c Merge pull request #70404 from jckarter/modify-coroutines-yielding-noncopyable-values
Move-only-check the result of modify coroutines.
2023-12-12 14:38:48 -08:00
Joe Groff
a0b133020c Merge pull request #70333 from jckarter/read-coroutines-yielding-noncopyable-values
Move-only-check the yielded result from read coroutines when they're noncopyable.
2023-12-12 09:35:39 -08:00
Joe Groff
18e31157fd Move-only-check the result of modify coroutines.
Following https://github.com/apple/swift/pull/70333, do the same thing for
modify coroutines, marking the result so that we check uses of the result to
ensure it isn't consumed (without being reinitialized).
2023-12-12 09:02:24 -08:00
Joe Groff
96c87dbf81 Move-only-check the yielded result from read coroutines when they're noncopyable.
Mark the result of starting a read coroutine to be checked by the move-only checker, and then
update the pattern matching in the move checker itself so that it recognizes code patterns
involving yielding from and receiving yields from read coroutines. Teach move only diagnostics
to get the property name for an access through a read coroutine from the referenced declaration.
2023-12-11 10:54:52 -08:00
Meghana Gupta
86b651330b Revert "Merge pull request #69807 from apple/revert-69450-uninarrayfix"
This reverts commit cabb5e109f, reversing
changes made to 09688abb02.
2023-12-06 08:29:50 -08:00
Meghana Gupta
fd588ab509 Remove SelfAccessKind::ResultDependsOnSelf 2023-11-29 12:11:57 -08:00
Mishal Shah
e8de333daf Revert "Add a mark_dependence while emitting SIL for uninitialized array allocation " 2023-11-12 09:43:13 -08:00
Doug Gregor
2159073c40 Merge pull request #69758 from DougGregor/typed-throws-rethrows-cleanup
Typed throws rethrows cleanup
2023-11-09 18:56:13 -08:00