Commit Graph

1246 Commits

Author SHA1 Message Date
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
Slava Pestov
ec0dfc8716 AST: Add new form of SubstitutionMap::getProtocolSubstitutions() 2025-04-03 17:35:33 -04:00
Joe Groff
6b605f41cb Type substitution eliminates dependencies with Escapable targets.
When a generic function has potentially Escapable outputs, those outputs
declare lifetime dependencies, which have no effect when substitution
leads to those types becoming `Escapable` in a concrete context.
This means that type substitution should canonically eliminate lifetime
dependencies targeting Escapable parameters or returns, and that
type checking should allow a function value with potentially-Escapable
lifetime dependencies to bind to a function type without those dependencies
when the target of the dependencies is Escapable.

Fixes rdar://147533059.
2025-04-02 08:54:45 -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
91c94a608d [NFC] Refactor callee formation 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
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
Michael Gottesman
2340dba27b [silgen] Place hop_to_executor emission for an apply within a formal evaluation scope.
Without this, the borrow of the hop_to_executor lasts after the apply. Beyond
being unnecessary this results in an OSSA violation if we are passing an actor
as an isolated parameter to an initializer since we hop_to_executor the actor
and then pass it as a +1 parameter to the initializer causing the actor to be
consumed before its borrow ends.

rdar://144994837
2025-03-07 11:51:22 -08:00
Gabor Horvath
00fa738209 [cxx-interop] Fix calling convention for rvalue reference params
In C++, we always expected to invoke the dtor for moved-from objects.
This is not the case for swift. Fortunately, @inCxx calling convention
is already expressing that the caller supposed to destroy the object.
This fixes the missing dtor calls when calling C++ functions taking
rvalue references. Fixes #77894.

rdar://140786022
2025-03-03 11:47:12 +00:00
Michael Gottesman
7e350bb4ce Revert "[concurrency] Add Concurrent/ConcurrentUnsafe and use it instead of ActorIsolation::Nonisolated."
This reverts commit 0cb64638d0.
2025-02-06 14:05:06 -08:00
Michael Gottesman
f05f08c2c0 Revert "[concurrency] Make ActorIsolation::Nonisolated -> CallerIsolationInheriting and delete CallerIsolationInheriting."
This reverts commit 543b1e6ca3.
2025-02-06 14:04:30 -08:00
Michael Gottesman
543b1e6ca3 [concurrency] Make ActorIsolation::Nonisolated -> CallerIsolationInheriting and delete CallerIsolationInheriting. 2025-02-03 10:56:08 -08:00
Michael Gottesman
0cb64638d0 [concurrency] Add Concurrent/ConcurrentUnsafe and use it instead of ActorIsolation::Nonisolated.
This is just the first part of a larger transition.
2025-02-03 10:56:06 -08:00
Joe Groff
44b431690d SILGen: Materialize addressor bases for the formal access scope of the entire access.
The return pointer may point into the materialized base value, so if the base needs
materialization, ensure that materialization covers any futher projection of the
value.
2025-01-31 18:08:53 -08:00
Joe Groff
23747bbb8e SILGen: Coordinate access strategy determination with actual codegen in findStorageReferenceExprForMoveOnly.
Looking at the AST-level `getReadImpl` doesn't always correspond to what
accessor SILGen prefers to use, due to resilience, ABI rules, and other
concerns. In findStorageReferenceExprForMoveOnly, when determining whether
a storage reference is borrowable, use the same logic as SILGenLValue actually
uses to determine what storage or accessor access strategy to use.

Fixes rdar://142509673
2025-01-10 16:12:09 -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
Michael Gottesman
56f38c4172 [concurrency] Add support in SILGen/SIL for emitting and calling implicit leading parameters for CallerInheritingIsolation isolation.
This does not change region isolation yet to recognize these as effectively
nonisolated.
2025-01-02 13:18:54 -08:00
Michael Gottesman
5d4239af57 [concurrency] Add new isolation kind CallerIsolationInheriting.
Right now it is basically a version of nonisolated beyond a few simple cases
like constructors/destructors where we are pretty sure we want to not support
this.

This is part of my bringup strategy for changing nonisolated/unspecified to be
caller isolation inheriting.
2025-01-02 13:18:30 -08:00
Andrew Trick
b56a787c74 SILGen: emit mark_dependence for unsafeAddress
Fixes a correctness issue with unsafe addressors: `unsafeAddress` and
`unsafeMutableAddress`. Previously, the resulting `Unsafe[Mutable]Pointer` did
not depend on `self`, meaning that the compiler is allowed to destroy `self`
before any uses of the pointer. This happens to be valid for
`UnsafePointer.pointee` because, in that case, `self` does not have a lifetime
anyway; the correctness burden was on the programmer to use
`withExtendedLifetime` around all uses of `self`.

Now, unsafe addressors can be used for arbitrary `Self` types.

This also enables lifetime dependence diagnostics when the addressor points to a
`~Escapable` type.

Addressors can now be used as an implementation of borrowed properties.
2024-12-17 09:53:02 -08:00
Joe Groff
3c0b08dbcb Prototype an @_addressable attribute that puts an argument at a stable address.
Many APIs using nonescapable types would like to vend interior pointers to their
parameter bindings, but this isn't normally always possible because of representation
changes the caller may do around the call, such as moving the value in or out of memory,
bridging or reabstracting it, etc. `@_addressable` forces the corresponding parameter
to be passed indirectly in memory, in its maximally-abstracted representation.
[TODO] If return values have a lifetime dependency on this parameter, the caller must
keep this in-memory representation alive for the duration of the dependent value's
lifetime.
2024-12-03 20:39:23 -08:00
Michael Gottesman
ebcab8f2ee [silgen] Change ArgEmitter to use an OptionSet before I add another option.
NFCI.
2024-11-15 15:14:32 -08: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
Hamish Knight
7c3f965578 Merge pull request #76979 from hamishknight/regex-request
Requestify regex pattern parsing
2024-10-12 19:19:09 +01:00
Hamish Knight
6a435960b7 Requestify regex pattern parsing
Instead of doing the pattern parsing in both the
C++ parser and ASTGen, factor out the parsing into
a request that returns the pattern to emit, regex
type, and version. This can then be lazily run
during type-checking.
2024-10-11 19:25:58 +01:00