Commit Graph

354 Commits

Author SHA1 Message Date
Joe Groff
4f1e895e56 SILGen: Use lowered function type parameters in objc-to-swift thunks for initializers.
Fixes a regression caused by the `self` declaration being given a `DynamicSelfType`
even though the formal type of the enclosing function sometimes still uses the
concrete base class type.

Fixes #83876 | rdar://158956768.
2025-09-08 09:42:32 -07:00
Michael Gottesman
61ff4a9b33 [concurrency] Teach NativeToForeign thunk how to handle implicit isolated parameters of a native function. 2025-04-17 21:56:59 -07:00
Michael Gottesman
f309d1b5f9 [concurrency] Add an implicit isolated parameter to @nonobjc foreign to native thunks.
We do not actually hop since we know that
2025-04-17 21:56:59 -07:00
John McCall
e9ebc7c848 Pass a zero normal result to ObjC async completion handlers on the error path.
Fixes #65199
2025-03-26 00:58:27 -04:00
Erik Eckstein
d225c47d25 AST: rename OpenArchetypeType -> ExistentialArchetypeType
NFC
2025-03-11 20:21:46 +01:00
Akira Hatanaka
f73c2e51bd Revert "[SILGen] Fix the type of closure thunks that are passed const reference structs (#76903)" (#77309)
This reverts commit 9c44b79189.

The commit caused swift's deserialization code to crash.

rdar://138726860
2024-10-31 15:16:00 -07:00
Akira Hatanaka
9c44b79189 [SILGen] Fix the type of closure thunks that are passed const reference structs (#76903)
The thunk's parameter needs the @in_guaranteed convention if it's a
const reference parameter. However, that convention wasn't being used
because clang importer was removing the const reference from the
type and SILGen was computing the type of the parameter based on the
type without const reference.

This commit fixes the bug by passing the clang function type to
SILDeclRef so that it can be used to compute the correct thunk type.

This fixes a crash when a closure is passed to a C function taking a
pointer to a function that has a const reference struct parameter.

This recommits e074426 with fixes to
serialization/deserialization of function types. The fixes prevent clang
types of functions from being dropped during serialization.

rdar://131321096
2024-10-08 23:44:49 -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
Slava Pestov
9632979879 Revert "[SILGen] Fix the type of closure thunks that are passed const referen…"
This reverts commit e074426058.
2024-09-29 22:32:04 -04:00
Akira Hatanaka
e074426058 [SILGen] Fix the type of closure thunks that are passed const reference structs (#75491)
The thunk's parameter needs the @in_guaranteed convention if it's a
const reference parameter. However, that convention wasn't being used
because clang importer was removing the const reference from the
type and SILGen was computing the type of the parameter based on the
type without const reference.

This commit fixes the bug by passing the clang function type to
SILDeclRef so that it can be used to compute the correct thunk type.

This fixes a crash when a closure is passed to a C function taking a
pointer to a function that has a const reference struct parameter.

rdar://131321096
2024-09-27 07:04:47 -07:00
Slava Pestov
0c2f28fd3d AST: Remove GenericSignature parameter from OpenedArchetypeType::get() 2024-08-20 12:15:27 -04:00
Slava Pestov
375363a473 AST: Move global conformance lookup entry points to ConformanceLookup.h 2024-08-08 23:35:58 -04:00
Slava Pestov
86d567f95a AST: ModuleDecl::lookupConformance() is a static method 2024-07-06 12:05:47 -04:00
Slava Pestov
fae01d9776 AST: Remove ModuleDecl parameter from more places 2024-07-06 12:05:46 -04:00
Akira Hatanaka
42bc49d3fe Add a new parameter convention @in_cxx for non-trivial C++ classes that are passed indirectly and destructed by the caller (#73019)
This corresponds to the parameter-passing convention of the Itanium C++
ABI, in which the argument is passed indirectly and possibly modified,
but not destroyed, by the callee.

@in_cxx is handled the same way as @in in callers and @in_guaranteed in
callees. OwnershipModelEliminator emits the call to destroy_addr that is
needed to destroy the argument in the caller.

rdar://122707697
2024-06-27 09:44:04 -07:00
Akira Hatanaka
11696cd452 [SILGen] Fix assertion failure when opaque value is enabled (#74676)
emitManagedParameter assumes the passed value has an address type and
calls forBorrowedAddressRValue when the parameter convention is
Indirect_In_Guaranteed.

Call forBorrowedObjectRValue instead when the type isn't an address
type.

rdar://130456931
2024-06-25 11:49:34 -07:00
Tim Kientzle
1098054291 Merge branch 'main' into tbkka-assertions2 2024-06-18 17:52:00 -07:00
Akira Hatanaka
1b0df4eefe [SILGen] Fix a crash when a closure is converted to a pointer to a function returning a non-trivial C++ type (#73561)
When emitting a native-to-foreign thunk, pass the thunk's result address parameter to the native function if both the thunk and the native function return their results indirectly and the thunk is not for an async function.

Also, remove an outdated assertion.

rdar://124501345
2024-06-18 15:03:44 -07:00
Pavel Yaskevich
ce6bac43f7 Merge pull request #74483 from xedin/fix-sendable-to-any-bridging
[SILGen] Fix `any Sendable` to `Any` bridging when result should be p…
2024-06-18 09:41:57 -07:00
Akira Hatanaka
d92f181ace Create two versions (for caller and callee) of the functions that answer questions about parameter convention (#74124)
Create two versions of the following functions:

isConsumedParameter
isGuaranteedParameter
SILParameterInfo::isConsumed
SILParameterInfo::isGuaranteed
SILArgumentConvention::isOwnedConvention
SILArgumentConvention::isGuaranteedConvention

These changes will be needed when we add a new convention for
non-trivial C++ types as the functions will return different answers
depending on whether they are called for the caller or the callee. This
commit doesn't change any functionality.
2024-06-18 09:06:09 -07:00
Pavel Yaskevich
1e9e351a3f [SILGen] Fix any Sendable to Any bridging when result should be placed in a buffer
If the context indicates that the result of `id-to-Any` should be
placed in some pre-allocated buffer, let's not attempt marker existential
transformation because that would much the expected layout already.
2024-06-17 09:45:00 -07:00
John McCall
71eb55a8d2 Properly handle @objc thunks for generic classes and actors
Fixes rdar://129187133
2024-06-13 04:44:36 -04: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
Ellie Shin
9392a69810 Merge pull request #73754 from apple/elsh/serialized_kind
Support inlinability with [serialized_for_package]
2024-05-24 10:13:36 -07:00
Akira Hatanaka
b5cc5615bd [SILGen] Fix a crash when a closure is converted to a block returning a value indirectly (#73520) 2024-05-23 21:22:32 -07:00
Ellie Shin
5ccc4cd394 SIL function can be serialized with different kinds: [serialized] or
[serialized_for_package] if Package CMO is enabled. The latter kind
allows a function to be serialized even if it contains loadable types,
if Package CMO is enabled. Renamed IsSerialized_t as SerializedKind_t.

The tri-state serialization kind requires validating inlinability
depending on the serialization kinds of callee vs caller; e.g. if the
callee is [serialized_for_package], the caller must be _not_ [serialized].
Renamed `hasValidLinkageForFragileInline` as `canBeInlinedIntoCaller`
that takes in its caller's SerializedKind as an argument. Another argument
`assumeFragileCaller` is also added to ensure that the calle sites of
this function know the caller is serialized unless it's called for SIL
inlining optimization passes.

The [serialized_for_package] attribute is allowed for SIL function, global var,
v-table, and witness-table.

Resolves rdar://128406520
2024-05-23 15:53:02 -07:00
Pavel Yaskevich
72eb8abadb [Sema/SILGen] Don't emit dynamic actor isolation checks when -disable-dynamic-actor-isolation flag is used 2024-04-15 09:19:22 -07:00
Erik Eckstein
3c76464c1c rename withConcurrent -> withSendable
That was missed when "concurrent" was renamed to "sendable"
2024-03-13 09:58:31 +01: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
Michael Gottesman
11f0ff6e32 [sil] Ensure that all SILValues have a parent function by making it so that SILUndef is uniqued at the function instead of module level.
For years, optimizer engineers have been hitting a common bug caused by passes
assuming all SILValues have a parent function only to be surprised by SILUndef.
Generally we see SILUndef not that often so we see this come up later in
testing. This patch eliminates that problem by making SILUndef uniqued at the
function level instead of the module level. This ensures that it makes sense for
SILUndef to have a parent function, eliminating this possibility since we can
define an API to get its parent function.

rdar://123484595
2024-02-27 13:14:47 -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
Pavel Yaskevich
db7959668e [Frontend] NFC: Rename PreconcurrencyConformances to DynamicActorIsolation 2024-02-01 13:28:25 -08:00
Pavel Yaskevich
68baadc253 [SILGen] Emit runtime actor isolation checks for @objc thunks of closures 2024-02-01 13:28:25 -08:00
Pavel Yaskevich
0e6dbd5c65 [SILGen] Emit expected executor preconditions to synchronous isolated @objc thunks
When `PreconcurrencyConformances` feature is enabled, emit a
precondition into every synchronous isolated @objc thunk to
make sure that it's always called on the right executor.
2024-01-16 11:51:49 -08:00
Allan Shortlidge
e5c6bb649c Remove support for Swift 3 @objc inference.
Obsolete the `-enable-swift3-objc-inference` option and related options by
removing support for inferring `@objc` attributes using Swift 3 rules.
Automated migration from Swift 3 has not been supported by the compiler for
many years.
2024-01-11 15:40:04 -08:00
nate-chandler
bf50e2f81b Merge pull request #70729 from nate-chandler/rdar119732084
[SILGen] Fix leak in thunk for async throwing swift conformance to ObjC requirement with optional completion.
2024-01-05 00:37:33 -08:00
Nate Chandler
9f5a7c87c3 [SILGen] Fix leak in objc async thunk.
Fix a leak when emitting the native to foreign thunk for an async
function which fulfills an Objective-C protocol requirement which can be
fulfilled with either a value or an error via a nullable completion.

Previously, the SIL in question used to look like this:

```sil
  %maybe_completion = ...
  try_apply %impl..., normal success, ...

success(%value):
  switch_enum %maybe_completion...
      case some!enumelt: invoke
      case none!enumelt: ignore

ignore:
  br join

invoke(%completion):
  %some_value = enum Optional, some!enumelt, %value // consumes %value
  %guaranteed_some_value = begin_borrow %some_value
  %none_error = enum Optional, none!enumelt
  apply %completion(%guaranteed_some_value, %none_error)
  end_borrow %guaranteed_some_value
  destroy_value %some_value
  br join

join:
  destroy_value %maybe_completion
  ...
```

which leaks %value on the codepath through `ignore`.

Note that `%value` is consumed by the `enum` instruction, but
`%completion` is invoked with `%guaranteed_some_value`, a guaranteed
value.  So there is no need to consume %value in `invoke`.

Here, `%value` itself is borrowed and forwarded into an enum instruction
whose result is passed to `%completion`:

```sil
  %maybe_completion = ...
  try_apply %impl..., normal success, ...

success(%value):
  switch_enum %maybe_completion...
      case some!enumelt: invoke
      case none!enumelt: ignore

ignore:
  br join

invoke(%completion):
  %guaranteed_value = begin_borrow %value
  %guaranteed_some_value = enum Optional, some!enumelt, %guaranteed_value
  %none_error = enum Optional, none!enumelt
  apply %completion(%guaranteed_some_value, %none_error)
  end_borrow %guaranteed_some_value
  br join

join:
  destroy_value %maybe_completion
  destroy_value %value
  ...
```

Because an argument scope was already being created and a cleanup was
already being pushed for `%value`, nothing more is required to fix the
issue than to reorder the enum and the borrow.

rdar://119732084
2024-01-04 19:10:17 -08:00
Holly Borla
df858a5ab9 Merge pull request #70553 from hborla/bridge-to-sendable 2023-12-20 06:10:35 -08:00
Holly Borla
d3c42649b7 [SILGen] Handle marker existentials in emitCBridgedToNativeValue. 2023-12-19 19:02:21 -08:00
Kavon Farvardin
f0e17af495 [NCGenerics] fix _bridgeAnythingToObjectiveC
Now that there's an actual conformance requirement in the generic
signature, we need a real conformance lookup function.
2023-12-18 18:55:56 -08:00
Slava Pestov
581776b997 SILGen: Emit indirect error result argument 2023-10-27 17:37:43 -04:00
Doug Gregor
b6b999abd4 [Typed throws] Basic SIL lowering and SIL generation for typed throws
Lower the thrown error type into the SIL function type. This requires
very little code because the thrown error type was already modeled as
a SILResultInfo, which carries type information. Note that this
lowering does not yet account for error types that need to passed
indirectly, but we will need to do so for (e.g.) using resilient error
types.

Teach a few places in SIL generation not to assume that thrown types
are always the existential error type, which primarily comes down to
ensuring that rethrow epilogues have the thrown type of the
corresponding function or closure.

Teach throw emission to implicitly box concrete thrown errors in the
error existential when needed to satisfy the throw destination. This
is a temporary solution that helps translate typed throws into untyped
throws, but it should be replaced by a better modeling within the AST
of the points at which thrown errors are converted.
2023-09-29 10:51:55 -07:00
Michael Gottesman
543063ef0e [silgen] Make ManagedValue::forUnmanaged private and change users to use other more specific APIs.
I have been doing this over the past couple of days in preparation for changing
ManagedValue to specify the type of scope its cleanup is connected to.
2023-08-19 14:11:44 -07:00
Michael Gottesman
104379adab [silgen] Rename SILGenFunction::emitManagedRetain -> emitManagedCopy.
This is an old API that should have been renamed a long time ago. It just kept
its early name due to inertia.
2023-08-19 14:11:44 -07:00
Michael Gottesman
ff948f7309 [silgen] Convert even more cases of using trivial and values without ownership to use for*RValueWithoutOwnership APIs. 2023-08-15 11:04:01 -07:00
Michael Gottesman
85e38afbac Merge pull request #67780 from gottesmm/pr-d9b586775f6ea84fe9868857eb8dd2eeaeb39f6f
[silgen] Rename forTrivialObjectRValue -> forObjectRValueWithoutOwnership and change a bunch of forUnmanaged to use this API
2023-08-08 09:58:33 -07:00
Michael Gottesman
2208caac32 [silgen] Convert a bunch of forUnmanaged -> forObjectRValueWithoutOwnership.
NFCI.
2023-08-07 13:36:02 -07:00
Allan Shortlidge
ca8bf981a4 NFC: Hoist queries for unavailable decl optimizations into the AST library.
Moving the query implementation up to the AST library from SIL will allow
conveniences to be written on specific AST element classes. For instance, this
will allow `EnumDecl` to expose a convenience that enumerates element decls
that are available during lowering.

Also, improve naming and documentation for these queries.
2023-08-04 17:39:26 -07:00
Nate Chandler
51ff57856d [OpaqueValues] Don't emit temporary in one case.
When creating a foreign-to-native thunk, create a temporary when the
foreign convention is indirect-non-inout only when using lowered
addresses.
2023-08-02 20:16:58 -07:00
Evan Wilde
250082df25 [NFC] Reformat all the LLVMs
Reformatting everything now that we have `llvm` namespaces. I've
separated this from the main commit to help manage merge-conflicts and
for making it a bit easier to read the mega-patch.
2023-06-27 09:03:52 -07:00