Commit Graph

717 Commits

Author SHA1 Message Date
Michael Gottesman
f245389bb3 [concurrency] Fix a few issues with @execution(caller)/@execution(concurrent).
Specifically:

1. I made it so that thunks from caller -> concurrent properly ignore the
isolated parameter of the thunk when calling the concurrent function.

rdar://148112362

2. I made it so that thunks from concurrent -> caller properly create a
Optional<any Actor>.none and pass that into the caller function.

rdar://148112384

3. I made it so that in cases where we are assigning an @Sendable caller to a
non-sendable caller variable, we allow for the conversion as long as the
parameters/results are sendable as well.

rdar://148112532

4. I made it so that when we generate a thunk from @execution(caller) ->
@GlobalActor, we mangle in @GlobalActor into the thunk.

rdar://148112569

5. I discovered that due to the way we handle function conversion expr/decl ref
expr, we were emitted two thunks when we assigned a global @caller function to a
local @caller variable. The result is that we would first cast from @caller ->
@concurrent and then back to @caller. The result of this would be that the
@caller function would always be called on the global queue.

rdar://148112646

I also added a bunch of basic tests as well that showed that this behavior was
broken.
2025-03-30 20:00:05 -07:00
Amritpan Kaur
3c30d68d2e Merge pull request #78823 from amritpan/method-keypaths
[Sema/SILGen/IRGen] Implement method & initializer keypaths.
2025-03-19 18:59:17 -07:00
Amritpan Kaur
45a7b45ad9 [SILGen] Lower unapplied methods. 2025-03-19 10:54:09 -07:00
Amritpan Kaur
60e03a85d9 [SILGen] Lower applied keypath methods. 2025-03-19 10:54:09 -07:00
Amritpan Kaur
e5362c761d [NFC] Refactor subscript argument evaluation for reuse. 2025-03-19 10:54:09 -07:00
Amritpan Kaur
e7e354d989 [NFC] Generalize subscript code for methods. 2025-03-19 10:54:09 -07:00
Joe Groff
85425fbe9c SILGen: Adjust allocation point for addressable representations.
Allocate the stack buffer at the point where the cleanup is introduced, so that
the stack allocations always follow scope stack discipline.
2025-03-17 21:37:08 -07:00
Joe Groff
da813458a6 SILGen: Emit an addressable representation for immutable bindings on demand.
To ensure that dependent values have a persistent-enough memory representation
to point into, when an immutable binding is referenced as an addressable
argument to a call, have SILGen retroactively emit a stack allocation and
materialization that covers the binding's scope.
2025-03-12 18:35:42 -07:00
Michael Gottesman
f6349aaf1e Revert "SILGen: Emit an addressable representation for immutable bindings on demand."
This reverts commit 19a99ea42b.

rdar://146864906
2025-03-12 13:24:33 -07:00
Joe Groff
2df3211690 Merge pull request #79903 from jckarter/addressable-representation-silgen
SILGen: Emit an addressable representation for immutable bindings on demand.
2025-03-12 08:02:05 -07:00
Joe Groff
19a99ea42b SILGen: Emit an addressable representation for immutable bindings on demand.
To ensure that dependent values have a persistent-enough memory representation
to point into, when an immutable binding is referenced as an addressable
argument to a call, have SILGen retroactively emit a stack allocation and
materialization that covers the binding's scope.
2025-03-11 15:15:48 -07:00
Rintaro Ishizaki
002d7d7cdf [Parse/AST] Remove PoundDiagnosticDecl
There is no reson to make a AST node for '#error' and '#warning'
directives. Parser knows the diagnostics should be emitted or not.
2025-03-07 21:48:01 -08:00
Alexis Laferrière
e97db1c844 Merge pull request #79487 from xymus/limit-nserror-optimization-to-import
SILGen: Ensure Foundation is publicly imported before referencing `NSError` in code gen
2025-02-28 15:21:11 -08:00
Alexis Laferrière
db7126c390 SILGen: Limit references to NSError in inlinable code optimization
Revisit the optimization that provides a fast path for instances of
`NSError` when erasing the `Error` type in `emitExistentialErasure`. It
generated references to `NSError` when the `Foundation` module was
loaded, no matter how it was imported. This lead to deserialization
failures at reading the swiftmodule when that reference was added to
inlinable code while `Foundation` was not a public dependency.

Fix this crash by limiting the optimization to all non-inlinable code
and only inlinable code from a module with a public dependency on
`Foundation`. This is the similar check we apply to user written
inlinable code, however here we use the module-wide dependency instead
of per file imports.

rdar://142438679
2025-02-19 15:34:20 -08:00
Anton Korobeynikov
1e7a1d91fc Emit reabstraction thunks for implicit conversions between T.TangentType and Optional<T>.TangentType (#78076) 2025-02-05 20:57:52 -08:00
Joe Groff
a25edb2185 Make Builtin.addressForBorrow work with addressable parameters. 2025-01-02 21:33:55 -08:00
Joe Groff
5c4406b5e8 Add an @_addressableForDependencies type attribute.
This attribute makes it so that a parameter of the annotated type, as well as
any type structurally containing that type as a field, becomes passed as
if `@_addressable` if the return value of the function has a dependency on
the parameter. This allows nonescapable values to take interior pointers into
such types.
2025-01-02 21:33:51 -08:00
John McCall
e99ec88ce3 Use common code paths for constructor concurrency prologs if we're not in
the async self-isolated actor initializer case.

Fixes rdar://138394497, a bug where we didn't set up isolation correctly for
an async parameter-isolated initializer, but also probably a non-trivial number
of other latent differences between initializers and normal functions.
2024-11-13 17:10:32 -05:00
Nate Chandler
a3d607965b [CoroutineAccessors] Fix begin_apply for protos.
When the CoroutineAccessors feature is enabled, `begin_apply`
instructions produce an additional result representing the allocation
done by the callee.  Fix a couple of cases where this additional result
was not being handled.
2024-10-29 14:24:35 -07:00
John McCall
af8115ffa3 Hop to the current isolation properly in delegating async actor initializers.
This requires two major changes.

The first is that we need to teach SILGen that the isolation of an initializer
is essentially dynamic (as far as SILGen is concerned) --- that it needs to emit
code in order to get the isolation reference.  To make this work, I needed to
refactor how we store the expected executor of a function so that it's not
always a constant value; instead, we'll need to emit code that DI will lower
properly.  Fortunately, I can largely build on top of the work that Doug previously
did to support #isolation in these functions.  The SIL we emit here around delegating
initializer calls is not ideal --- the breadcrumb hop ends up jumping to the
generic executor, and then DI actually emits the hop to the actor.  This is a little
silly, but it's hard to eliminate without special-casing the self-rebinding, which
honestly we should consider rather than the weirdly global handling of that in
SILGen today.  The optimizer should eliminate this hop pretty reliably, at least.

The second is that we need to teach DI to handle the pattern of code we get in
delegating initializers, where the builtin actually has to be passed the self var
rather than a class reference.  This is because we don't *have* a class reference
that's consistently correct in these cases.  This ended up being a fairly
straightforward generalization.

I also taught the hop_to_executor optimizer to skip over the initialization of
the default-actor header; there are a lot of simple cases where we still do emit
the prologue generic-executor hop, but at least the most trivial case is handled.
To do this better, we'd need to teach this bit of the optimizer that the properties
of self can be stored to in an initializer prior to the object having escaped, and
we don't have that information easily at hand, I think.

Fixes rdar://87485045.
2024-10-04 22:23:00 -04:00
Konrad `ktoso` Malawski
7d1ce789ad Revert "Revert "Isolated synchronous deinit"" 2024-09-17 17:35:38 +09:00
Alex Hoppen
c5aa49ba64 Revert "Isolated synchronous deinit" 2024-09-03 18:11:26 -07:00
Mykola Pokhylets
e0ad7bde82 Merge branch 'main' into mpokhylets/isolated-deinit
# Conflicts:
#	include/swift/Basic/Features.def
#	lib/AST/ASTPrinter.cpp
#	lib/AST/FeatureSet.cpp
2024-08-29 11:28:43 +02:00
Pavel Yaskevich
080675170b [SILGen] Emit dynamic actor isolation checks for closures
For synchronous isolated closures passed to unsafe APIs
(the ones that have not been fully concurrency checked)
emit an expected executor check in prolog that would make
sure that they are always used in the expected context.

Resolves: rdar://133415157
2024-08-22 09:57:17 -07:00
Mykola Pokhylets
50b1313175 Merge branch 'main' into mpokhylets/isolated-deinit
# Conflicts:
#	lib/SILGen/SILGenDistributed.cpp
#	lib/Sema/TypeCheckConcurrency.cpp
2024-08-15 16:58:43 +02:00
Allan Shortlidge
6fb6a56b20 SILGen: Introduce macCatalyst support for if #available.
Upstream the necessary changes to compile `if #available` queries correctly
when a `macabi` target triple or a `-target-variant` is specified.
2024-07-23 17:00:10 -07:00
Mykola Pokhylets
816d62c972 Merge remote-tracking branch 'upstream/main' into mpokhylets/isolated-deinit
# Conflicts:
#	include/swift/Basic/Features.def
#	lib/SILGen/SILGenDestructor.cpp
#	test/Concurrency/flow_isolation.swift
#	test/abi/macOS/arm64/concurrency.swift
#	test/abi/macOS/x86_64/concurrency.swift
2024-07-11 13:11:59 +02:00
Mykola Pokhylets
c28df0dc05 Don't isolate deinit of remote actor proxies 2024-07-11 13:09:07 +02:00
Mykola Pokhylets
9005e9b625 Removed some dead code 2024-07-11 13:09:06 +02:00
Mykola Pokhylets
28a06feaad Added isolated deallocator to TBDGen 2024-07-11 13:09:06 +02:00
Mykola Pokhylets
95079aff06 Don't generate isolating destructor if dealloc was explicitly isolated in ObjC 2024-07-11 13:09:06 +02:00
Mykola Pokhylets
d4e449b62d SILGen for isolated deinit 2024-07-11 13:09:05 +02:00
Michael Gottesman
f781ad366d [sending] Compensate in SILGen for a typechecker bug where we do not properly mark actor isolated async closures passed to an inheritActorContext argument as being Sendable.
The reason why I am fixing this is that otherwise, we get a warning when one
creates an actor isolated closure and pass it into a task, e.x.:

```swift
@MainActor func test() {
  // We would get a warning on the closure below saying that we are sending
  // a closure that is MainActor isolated.
  Task {
    ...
  }
}
```
2024-06-21 02:24:03 -07:00
Tim Kientzle
1d961ba22d Add #include "swift/Basic/Assertions.h" to a lot of source files
Although I don't plan to bring over new assertions wholesale
into the current qualification branch, it's entirely possible
that various minor changes in main will use the new assertions;
having this basic support in the release branch will simplify that.
(This is why I'm adding the includes as a separate pass from
rewriting the individual assertions)
2024-06-05 19:37:30 -07:00
Slava Pestov
23be369c0e SILGen: Update for PackElementExpr captures
Fixes https://github.com/apple/swift/issues/68941.
Fixes https://github.com/apple/swift/issues/69282.
Fixes https://github.com/apple/swift/issues/72153.
Fixes rdar://110711746.
Fixes rdar://119212867.
Fixes rdar://124202697.
2024-05-30 16:39:49 -04:00
Doug Gregor
39f4e38027 Diagnose inout uses of 'lets' in constructors in the type checker.
Stored `let` properties of a struct, class, or actor permit
'inout' modification within the constructor body after they have been
initialized. Tentatively remove this rule, only allowing such `let`
properties to be initialized (assigned to) and not treated as `inout`.

Fixes rdar://127258363.
2024-05-14 15:59:50 -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
Pavel Yaskevich
d887249366 Merge pull request #72173 from xedin/dont-emit-executor-check-if-target-is-too-old
[SILGen] Emit `_checkExpectedExecutor` only if its available on a dep…
2024-03-11 09:52:46 -07:00
Nate Chandler
dff0b2efaa [SILGen] Allocs for VDs are var_decl.
Annotate alloc_stack instructions that correspond to VarDecls with the
var_decl flag.
2024-03-08 22:28:22 -08:00
Nate Chandler
c6eb3112b5 [NFC] SIL: Typed alloc_stack's isLexical. 2024-03-08 21:24:39 -08:00
Nate Chandler
dfa5c9814b [NFC] SIL: Typed alloc_stack's hasDynamicLifetime. 2024-03-08 21:24:39 -08:00
Pavel Yaskevich
8e93ae9775 [SILGen] Don't emit executor references when '_checkExpectedExecutor' is unavailable
Avoid emitting an unused executor reference for dynamic actor
isolation checking.
2024-03-08 13:04:48 -08:00
John McCall
8499640224 Merge pull request #72143 from rjmccall/builtin-create-task
Unify and simplify the task-creation builtins
2024-03-07 21:45:06 -05:00
John McCall
0a282c044f Unify all of the task-creation builtins coming out of SILGen.
We've been building up this exponential explosion of task-creation
builtins because it's not currently possible to overload builtins.
As long as all of the operands are scalar, though, it's pretty easy
to peephole optional injections in IRGen, which means we can at
least just use a single builtin in SIL and then break it apart in
IRGen to decide which options to set.

I also eliminated the metadata argument, which can easily be recreated
from the substitutions.  I also added proper verification for the builtin,
which required (1) getting `@Sendable` right more consistently and (2)
updating a bunch of tests checking for things that are not actually
valid, like passing a function that returns an Int directly.
2024-03-06 22:21:12 -05:00
Joe Groff
828abb3f75 SILGen: Forward move-only ownership through optional chains.
Allow noncopyable values to be consumed, borrowed, or mutated transitively
through optional chains.
2024-03-06 13:44:02 -08:00
John McCall
43b9b28797 Fix a pair of bugs with @isolated(any) closure erasure.
First, and I really should've checked this, but global actors do not
require `shared` to return `Self`; adjust the logic to propagate the
right formal type to the erasure logic.

Second, handle attempts to erase the isolation of something isolated to
a distributed actor using the magic Actor conformance that Doug added.
This only works when the actor is local, but it shouldn't be difficult to
enforce that we only attempt to erase isolation what that's true --- we
need to prevent partial application of distributed actors, and we need to
disallow explicit isolated captures of distributed actors when we're not
currently isolated to it.  Otherwise, it's not possible to get an
@isolated(any) function value with an isolation that isn't the current
function's isolation, which means it always has to be local.

Fixed rdar://123734019
2024-03-04 04:15:21 -05:00
John McCall
7b3415aa23 Properly erase closure isolation to @isolated(any).
We do this by pushing the conversion down to the emission of the
closure expression, then teaching closure emission to apply the isolation
to the closure.  Ideally, we combine the isolation along with the rest of
the conversion peephole, but if necessary, we make sure we emit the
isolation.
2024-02-26 22:50:58 -05: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
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