Commit Graph

1275 Commits

Author SHA1 Message Date
Hamish Knight
90bc9a12c7 [SILGen] Add assert to emitApplyAllocatingInitializer
Make sure we reliably assert in release builds if `getBuiltinInitDecl`
passes a null value here.
2025-11-11 15:58:24 +00:00
Slava Pestov
522a6b7c80 SILGen: Fix break/continue inside 'for ... in ... repeat' loop
We were creating the JumpDests too early, so lowering a 'break' or 'continue'
statement would perform cleanups that were recorded while evaluating the
pack expansion expression, which would cause SIL verifier errors and
runtime crashes.

- Fixes https://github.com/swiftlang/swift/issues/78598
- Fixes rdar://131847933
2025-11-10 20:51:45 -05:00
Meghana Gupta
290d2f00a3 Fix mark_unresolved_noncopyable_value instruction introduced for mutate accessor result 2025-11-06 10:55:43 -08:00
Meghana Gupta
d50aa8ad87 [NFC] Comment on the use of unchecked_ownership 2025-11-06 10:55:41 -08:00
John McCall
a7d7970e29 Turn finishAsyncLet into a builtin.
This is necessary because we need to model its stack-allocation
behavior, although I'm not yet doing that in this patch because
StackNesting first needs to be taught to not try to move the
deallocation.

I'm not convinced that `async let` *should* be doing a stack allocation,
but it undoubtedly *is* doing a stack allocation, and until we have an
alternative to that, we will need to model it properly.
2025-11-03 16:33:40 -08:00
John McCall
1d95fe14de Remove the deprecated and unused startAsyncLet builtin 2025-11-03 13:44:58 -08:00
Slava Pestov
4677512a74 Merge pull request #84664 from jamieQ/fix-iso-default-param-idx-bug
[SILGen]: fix a bug in isolated default argument handling
2025-10-30 03:40:49 -04:00
Meghana Gupta
bef207521d Use unchecked_ownership during SILGen of borrow accessors 2025-10-23 05:19:10 -07:00
Meghana Gupta
a150c67896 Remove unchecked_ownership_conversion from borrow accessor silgen 2025-10-23 05:19:04 -07:00
Meghana Gupta
a0c939bd3b Use @inout result convention for mutate accessors 2025-10-20 09:05:32 -07:00
Michael Gottesman
390afe3e7d [concurrency] Implement bit masking for TBI when available or in tagged pointer bits otherwise.
Specifically, when TBI is available we use the bottom two bits of the top nibble
(bits 60,61). On platforms without TBI, we use the bottom two tagged pointer
bits (bits 0, 1).

rdar://156525771
2025-10-16 10:52:05 -07:00
Michael Gottesman
788abd0b96 [silgen] Use Builtin.ImplicitActor instead of Optional<any Actor> to represent the implicit isolated parameter.
NOTE: We are not performing any bitmasking at all now. This is so that we can
transition the code base/tests to expect Builtin.ImplicitActor instead
of Optional<any Actor>.

NOTE: The actual test changes are in the next commit. I did this to make it
easier to review the changes.

This should not have any user visible changes.
2025-10-16 10:52:05 -07:00
Erik Eckstein
3309809429 SILGen: improve code generation for array literals
It used to be done with a library intrinsic which returns the array and an element address pointer.
A `mark_dependence` was used to "connect" the two results.
But this resulted in completely wrong OSSA after inlining.
It just didn't get caught be the verifier because the SIL was obfuscated by address-pointer conversions.

Now we don't use the second result (= the element address) of the intrinsic but generate a correct borrow scope from the first (= array) result. Within that borrow scope we project out the element address with a `ref_tail_addr` instruction.

This relies on knowledge of the internal Array data structures. However those data structures are baked into the ABI since a long time and will not change.
2025-10-10 14:20:36 +02:00
Kavon Farvardin
a528c5c19c Merge pull request #84734 from kavon/opaque-values/fixes-2
OpaqueValues: support typed throws
2025-10-08 21:17:23 -07:00
Meghana Gupta
5e325632ed Merge pull request #84746 from meg-gupta/mutateaccessorspr
Add support for mutate accessors
2025-10-08 11:08:59 -07:00
Gábor Horváth
0959bce25e Merge pull request #84612 from swiftlang/fix-overrelease-destroy-hoist
[cxx-interop] Delay lowering unowned convention until ownership elimination
2025-10-08 18:42:02 +01:00
Meghana Gupta
282e3351f6 Handle mutate accessors similar to borrow accessors in call emission 2025-10-07 14:12:11 -07:00
Kavon Farvardin
00ecc6c2ce SILGen: fix typed throws emission for opaque values
We use a direct @error emission style when opaque values
is enabled, relying on AddressLowering to satisfy the
formal @error_indirect convention.
2025-10-07 09:30:32 -07:00
Gabor Horvath
8c197ebd8e [cxx-interop] Delay lowering unowned convention until ownership elimination
Unowned result conventions do not work well with OSSA. Retain the result
right after the call when we come out of OSSA so we can treat the
returned value as if it was owned when we do optimizations.

This fix a miscompilation due to the DestroyAddrHoisting pass hoisting
destroys above copies with unowned sources. When the destroyed object
was the last reference to the pointed memory the copy is happening too
late resulting in a use after free.

rdar://160462854
2025-10-03 13:15:51 +01:00
Jamie
7a4bc72d8e [SILGen]: fix a bug in isolated default argument handling
Changes in https://github.com/swiftlang/swift/pull/81370 addressed a
number of issues with isolated default argument handling. However, there
seemed to still be a problem when dealing with isolated default
arguments of instance/static methods under the following conditions:

- The method contained an isolated default parameter
- The method contained a defaulted parameter that expanded to 0 or >1
  values
- Said parameter was followed by another defaulted parameter

The attempted fix here was to adjust the isolated default argument
iteration bookkeeping.
2025-10-03 06:23:34 -05:00
Meghana Gupta
e4d1123d10 Fix SILGen of borrow accessors returning guaranteed values from within a local borrow
Borrow accessor result can sometimes be generated from a local borrow and
returning the result produced within the local borrow scope will cause ownership errors.

We have to introduce new SIL semantics to make this possible.
Until then use a pair of unchecked_ownership_conversion instructions to silence the ownership errors.
We encounter this when we have:

Address-only self and @guaranteed result
Loadable self and @guaranteed result derived from an unsafe pointer stored property

This change also updates the result convention of borrow accessors with loadable result types
and indirect self argument type.
2025-10-02 07:42:14 -07:00
Meghana Gupta
19ab4cce0e Update SILGen for borrow accessors under library evolution
Under library evolution, loadable self arguments are passed as @in_guaranteed.
SILGen generates load_borrow for such self arguments proactively.
load_borrow creates an artifical scope and returning values produced within this scope
will be illegal without resorting to unsafe operations today.

This change avoids creating a load_borrow proactively for borrow accessors.
2025-10-02 07:42:13 -07:00
Meghana Gupta
c2dab58876 Update SILGen for ~Copyable borrow accessors
Introduce copy_value + mark_unresolved_non_copyable_value + begin_borrow at the return value of
borrow accessor apply to drive move-only diagnostics.

Also strip the copy_value + mark_unresolved_non_copyable_value + begin_borrow trio in a few places, since
they create an artificial scope out of which we cannot return values in a borrow accessor
without resorting to unsafe SIL operations currently.

Borrow accessor diagnostics allow stripping these instructions safely in the following places:

- return value of a borrow accessor
- self argument reference in the borrow accessor return expression and borrow accessor apply
2025-10-02 07:18:23 -07:00
Gábor Horváth
e8784b8c10 Merge pull request #83827 from Xazax-hun/const-ref-crash-take-4
[SILGen] Fix the type of closure thunks that are passed const references
2025-10-01 09:56:06 +01:00
Meghana Gupta
8c1231e86b SILGen support for borrow accessors on Copyable types and address-only ~Copyable types 2025-09-09 14:45:44 -07:00
Meghana Gupta
6b8c91efe2 [NFC] Move StorageRefResult to a header 2025-09-09 14:45:43 -07:00
Meghana Gupta
1cff471c57 Introduce ResultConvention::Guaranteed and ResultConvention::GuaranteedAddress
ResultConvention::Guaranteed will be used by borrow accessors when the storage type can be returned by value.

ResultConvention::GuaranteedAddress will be used by mutate accessors and borrow accessors when the storage type
cannot be returned by value.
2025-09-09 14:45:40 -07:00
Meghana Gupta
9fe489ce22 Introduce borrow and mutate as new accessor kinds
And handle them in various covered switches
2025-09-09 14:30:26 -07:00
Gabor Horvath
f26749245b [SILGen] Fix the type of closure thunks that are passed const reference structs
This PR is another attempt at landing #76903. The changes compared to
the original PR:
* Instead of increasing the size of SILDeclRef, store the necessary type
  information in a side channel using withClosureTypeInfo.
* Rely on SGFContext to get the right ClangType
* Extend BridgingConversion with an AbstractionPattern to store the
  original clang type.
* The PR above introduced a crash during indexing system modules that
  references foreign types coming from modules imported as
  implementation only. These entities are implementation details so they
  do not need to be included during serialization. This PR adds a test
  and adds logic to exclude such clang types in the serialization
  process.

rdar://131321096&141786724
2025-09-09 12:07:52 +01:00
Joe Groff
ee2ee0b560 SILGen: Global constants are addressable.
A global or static `let` has a stable address, which is addressable when its
representation is naturally fully abstracted.
2025-09-03 08:38:13 -07:00
Slava Pestov
11f727e143 SIL: Add no-arg overload of SILFunction::getLoweredFunctionTypeInContext() 2025-08-25 16:45:45 -04:00
Slava Pestov
b1150924a3 SILGen: Remove a usage of replaceCovariantResultType() 2025-08-15 18:37:15 -04:00
John McCall
8ad4aae6e0 Fix some major SILGen bugs with pack handling:
- Calls to variadic-generic protocol requirements weren't applying
  substitutions properly, so expansion-sensitive types in the callee
  signature weren't pairing properly with their expansions in the
  caller.

- emitPackTransform had an over-destroy if the transformation function
  actually emitted into the temporary element directly.

- There were some MV ownership assertions that were wrong, which
  revealed that the corresponding code really didn't handle consuming/
  borrowing mismatches properly at all.

- We were completely mishandled consuming packs.

Fixes #81002, #80995, and #81600.
2025-08-06 12:38:50 -04:00
John McCall
2f6f3aae5f Merge pull request #83490 from rjmccall/sil-type-properties
Extract and use SILTypeProperties without a TypeLowering
2025-08-02 02:12:49 -04:00
John McCall
46be95847b Extract TypeLowering's recursive type properties into a header, add
functions to compute them directly without a TypeLowering object, and
change a lot of getTypeLowering call sites to just use that.

There is one subtle change here that I think is okay: SILBuilder used to
use different TypeExpansionContexts when inserting into a global:
- getTypeLowering() always used a minimal context when inserting into
  a global
- getTypeExpansionContext() always returned a maximal context for the
  module scope
The latter seems more correct, as AFAIK global initializers are never
inlinable. If they are, we probably need to configure the builder with
an actual context properly rather than making global assumptions.

This is incremental progress towards computing this for most types
without a TypeLowering, and hopefully eventually removing TL entirely.
2025-08-01 15:00:57 -04:00
Michael Gottesman
3871d22257 [concurrency] Emit nonisolated(nonsending) async throw initializers correctly.
Specifically, we were not inserting the implicit isolated parameter and were not
setting up the actor prologue. To keep this specific to nonisolated(nonsending)
code, I only setup the actor prologue if we know that we have something that is
nonisolated(nonsending).

I also ported some async initializer tests to run with/without
nonisolated(nonsending) just to increase code coverage.

rdar://156919493
2025-07-29 19:15:08 -07:00
Gabor Horvath
9525aeec18 [cxx-interop] Enable addressable-self unconditionally
This was behind a feature flag. Unfortunately, this flag is viral, if
the module we consume and the consuming module had inconsistent settings
that could lead to deserialization errors. To avoid this, we need to
move this out of the flag and apply the attribute unconditionally. This
PR moves addressable-self out of the experimental flag and addresses a
couple of the fallouts:
* This attribute should not be applied to types with reference semantics
  like foreign reference types or Obj-C classes.
* There was a SILGen assertion failure which is solved by pealing off
  the @lvalue specifier from the type behind a load expression.

This fixes part of rdar://155971658
2025-07-26 08:37:03 +01:00
Joe Groff
37bfabdce8 Merge pull request #82901 from jckarter/preconcurrency-indirect-return
SILGen: Bitcast indirect returns that differ only in concurrency annotations.
2025-07-09 07:49:59 -07:00
Joe Groff
8ac81dc755 SILGen: Bitcast indirect returns that differ only in concurrency annotations.
A call to a `@preconcurrency` function goes through a function conversion
that removes `Sendable` from existentials among other things. Implement
support for this by bitcasting indirect return slots whose type differs
from the formal indirect return type in concurrency markings only.

Fixes rdar://154240007
2025-07-08 17:02:15 -07:00
Joe Groff
3b98bcb0a3 SILGen: Don't try to project static stored properties as addressable from their base.
An oversight from https://github.com/swiftlang/swift/pull/82288. Fixes #82368 / rdar://153837014.
2025-07-07 19:50:05 -07:00
John McCall
2eee30dfbe [NFC] Encapsulate the parameter index of an ActorIsolation 2025-06-27 19:48:12 -04:00
Joe Groff
761faaa169 SILGen: Handle struct fields and addressors as addressable storage.
When accessing stored properties out of an addressable variable or parameter
binding, the stored property's address inside the addressable storage of the
aggregate is itself addressable. Also, if a computed property is implemented
using an addressor, treat that as a sign that the returned address should be
used as addressable storage as well. rdar://152280207
2025-06-16 20:23:47 -07:00
Michael Gottesman
662dbdb55a [silgen] Teach SILGen how to emit a forced dynamic member ref given nonisolated(nonsending) casts.
Specifically, I taught SILGen how to emit an AST like the following:

```
(force_value_expr implicit type="nonisolated(nonsending) (Date?) async -> Void" implicit_iuo_unwrap
  (open_existential_expr implicit type="(nonisolated(nonsending) (Date?) async  -> Void)?"
    (opaque_value_expr implicit type="AnyObject")
    (declref_expr type="AnyObject" decl="test.(file).repro().anyObject@test.swift:6:7" function_ref=unapplied)
    (optional_evaluation_expr type="(nonisolated(nonsending) (Date?) async -> Void)?"
      (inject_into_optional type="(nonisolated(nonsending) (Date?) async -> Void)?"
        (function_conversion_expr type="nonisolated(nonsending) (Date?) async -> Void"
          (bind_optional_expr type="(Date?) async -> Void" depth=0
            (dynamic_member_ref_expr type="((Date?) async -> Void)?" decl="__ObjC.(file).Foo.start(at:)"
              (opaque_value_expr type="AnyObject"))))))))
```

Since we are emitting an objc async function, there isn't an extra implicit
parameter like if we were using a swift async function. So, I just reused code
that was already used locally to look through these sorts of conversions. I
just had to add to that code support for conversions that add
nonisolated(nonsending). Previously it only supported looking through global
actor conversions.

rdar://152596823
2025-06-05 13:34:42 -07:00
John McCall
93b593bb5c Fix argument index calculations for isolated default arguments.
Fixes rdar://150060837
2025-05-07 20:04:38 -04:00
Slava Pestov
2fa49b0458 AST: SubstitutionMap overload of substOpaqueTypesWithUnderlyingTypes()
This replaces the oddly-named mapIntoTypeExpansionContext() method
on SubstitutionMap itself in favor of a global function, just like
the ones that take Type and ProtocolConformanceRef.
2025-04-28 13:48:35 -04:00
Anthony Latsis
925b72eaec Merge pull request #76337 from DePasqualeOrg/grammar-compound-modifiers
Grammatical corrections for compound modifiers
2025-04-25 13:50:52 +01:00
Anthony
c9b17383c8 Grammatical corrections for compound modifiers 2025-04-24 09:21:32 +02:00
Michael Gottesman
227ab376cf [silgen] When emitting a foreign async completion handler for a method, use merge isolation region to tie self and the block storage into the same region.
This is an extension of a similar problem that I had fixed earlier where due to
the usage of intermediate Sendable types we do not propagate regions correctly.

The previous issue I fixed was that we were not properly tieing the result of a
foreign async completion handler to the block storage since we used an
intervening UnsafeContinuation (which is Sendable) to propagate the result into
the block storage. I fixed this by changing SILGen to insert a
merge_isolation_region that explicitly ties the result to the block storage.

This new issue is that the block that we create and then pass as the completion
handler is an @Sendable block. Thus when we call the actual objc_method, the
block storage and self are not viewed as being in the same region. In this PR, I
change it so that we add a merge_isolation_region from self onto the block
storage.

The end result of this is that we have that self, the result of the call, and
the block storage are all in the same region meaning that we properly diagnose
that returning an NSObject from the imported Objective-C function is task
isolated and thus we cannot return it as a sending result.

rdar://131422332
2025-04-23 10:01:24 -07:00
Nate Chandler
7697a49ee9 [CoroutineAccessors] Only reference when available
Don't bind references to storage to use (new ABI) coroutine accessors
unless they're guaranteed to be available.  For example, when building
against a resilient module that has coroutine accessors, they can only
be used if the deployment target is >= the version of Swift that
includes the feature.

rdar://148783895
2025-04-15 21:13:57 -07:00
Nate Chandler
b76a76bdf1 [CoroutineAccessors] Unwind based on feature.
Now that coroutine kind (and consequently ABI) for the accessors is
keyed off a SIL option, it's no longer possible to read whether a given
SILFunction arose from a read/modify coroutine just by checking its
coroutine kind.  Regardless of ABI, read/modify coroutines may only
unwind (i.e. are only permitted not to "run to completion") if the
relevant experimental (soon to be deleted) feature is enabled.
2025-04-10 07:41:37 -07:00