Commit Graph

2410 Commits

Author SHA1 Message Date
Gábor Horváth
40bea13e68 [6.2][cxx-interop] Delay lowering unowned convention until ownership elimination
Explanation: 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.

Issues: rdar://160462854
Original PRs: #84612
Risk: We change where retaining of the unowned return values
happen in the optimization pipeline. It is hard to anticipate all the
possible effects but it should make the optimizer more correct.
Testing: Added a compiler test.
Reviewers: @eeckstein, @atrick
2025-10-08 18:57:54 +01:00
John McCall
2bd4c8c0ac At the SIL level, allow synchronous functions to be nonisolated(nonsending). 2025-09-11 21:51:44 -04:00
John McCall
6f376c2468 Fix SILGen's computation of default argument generator isolation 2025-09-11 21:51:36 -04:00
Egor Zhdan
8178aa8b65 [cxx-interop] Pass foreign reference types with correct level of indirection
When calling a C++ function that takes a reference to a pointer to a foreign reference type, Swift would previously pass a pointer to the foreign reference type as an argument (instead of a reference to a pointer), which resulted in invalid memory accesses.

This was observed when using `std::vector<ImmortalRef*>::push_back`.

rdar://150791778
(cherry picked from commit 0a766e59ce)
2025-09-03 20:42:06 +01:00
Michael Gottesman
e73396bc8a [sil] Add the ability to mark a function with isolation just so we can write more concurrency tests in SIL.
Specifically, we write a string out like:

sil [isolation "$REPRESENTATION OF ISOLATION"] @function : $@convention(thin) ...

The idea is that by using a string, we avoid parsing issues of the isolation and
have flexibility. I left in the way we put isolation into the comment above
functions so I did not break any tests that rely on it. I also made it so that
we only accept this with sil tests that pass in the flag
"sil-print-function-isolation-info". I am going to for the next release put in a
full real implementation of this that allows for actor isolation to become a
true first class citizen in SIL. But for now this at least lets us write tests
in the short term.

Since this is temporary and behind a flag, I did not add support for
serialization since this is just for writing textual SIL tests.

(cherry picked from commit ee3027c2ca)

Conflicts:
	lib/SIL/Parser/ParseSIL.cpp
2025-07-15 17:12:01 -07:00
Joe Groff
79b59f1977 Lower imported C structs and unions as addressable-for-dependencies.
C code is highly likely to want to use pointers as references between dependent
structs, and we would like to be able to readily map these to lifetime-dependent
Swift values. Making C types addressable-for-dependencies ensures that any function
producing a dependency on such a value receives a stable in-memory address for that
value, allowing borrows and inout accesses to always be representable as pointers.

rdar://153648393
2025-07-07 08:46:43 -07:00
nate-chandler
4051ea2e78 Merge pull request #82044 from nate-chandler/cherrypick/release/6.2/rdar152580661
6.2: [TypeLowering] Record pack used in aggregate signature.
2025-06-06 14:53:18 -07:00
Nate Chandler
d8c6f9817c [TypeLowering] Record pack used in aggregate sig.
Every `LowerType::visit*` function eventually calls through to a
`LowerType::handle*` function.  After
https://github.com/swiftlang/swift/pull/81581, every
`LowerType::handle*` needs to set the `hasPack` flag based on the
passed-in type by calling `mergeHasPack`.  Add the missing call in the
`handleAggregateByProperties` function.

rdar://152580661
2025-06-05 17:45:14 -07:00
Pavel Yaskevich
608a37ad04 Merge pull request #81997 from xedin/using-for-default-isolation-in-file-context-6.2
[6.2][AST/Sema] SE-0478: Implement using declaration under an experimental flag
2025-06-05 00:44:21 -07:00
Pavel Yaskevich
d057429e9a [AST] Add new declaration - using
Initially this declaration is going to be used to determine
per-file default actor isolation i.e. `using @MainActor` and
`using nonisolated` but it could be extended to support other
file-global settings in the future.

(cherry picked from commit aabfebec03)
2025-06-04 13:16:55 -07:00
Konrad 'ktoso' Malawski
9c023a2577 [Distributed] print distributed(_thunk) in SILPrinter 2025-06-04 21:07:06 +09:00
Joe Groff
5ca6ad9d5e SILGen: Emit property descriptors for conditionally Copyable and Escapable types.
Key paths can't reference non-escapable or non-copyable storage declarations,
so we don't need to refer to them resiliently, and can elide their property
descriptors.

However, declarations may still be conditionally Copyable and Escapable, and
if so, then they still need a property descriptor for resilient key path
references. When a property or subscript can be used in a context where it
is fully Copyable and Escapable, emit the property descriptor in a generic
environment constrained by the necessary conditional constraints.

Fixes rdar://151628396.
2025-05-27 20:18:58 -07:00
Meghana Gupta
b91b772b01 Merge pull request #81679 from meg-gupta/fixsubstne
[6.2] Fix use-after-free on substituting function type involving conditional ~Escapable with Escapable type
2025-05-27 12:46:52 -07:00
Erik Eckstein
30e138c48b SIL: define mark_dependence_addr to read and write to its address operand
This prevents simplification and SILCombine passes to remove (alive) `mark_dependence_addr`.
The instruction is conceptually equivalent to
```
  %v = load %addr
  %d = mark_dependence %v on %base
  store %d to %addr
```

Therefore the address operand has to be defined as writing to the address.
2025-05-23 07:53:14 +02:00
Pavel Yaskevich
e12201769d [AST/Sema/SIL] Implement @_inheritActorContext(always)
- Extend `@_inheritActorContext` attribute to support optional `always` modifier.
  The new modifier will make closure context isolated even if the parameter is not
  captured by the closure.
- Implementation `@_inheritActorContext` attribute validation - it could only be
  used on parameter that have `@Sendable` or `sending` and `@isolated(any)` or
  `async` function type (downgraded to a warning until future major Swift mode
  to avoid source compatibility issues).
- Add a new language feature that guards use of `@_inheritActorContext(always)` in swift interface files
- Update `getLoweredLocalCaptures` to add an entry for isolation parameter implicitly captured by `@_inheritActorContext(always)`
- Update serialization code to store `always` modifier

(cherry picked from commit 04d46760bb)
(cherry picked from commit c050e8f75a)
(cherry picked from commit c0aca5384b)
(cherry picked from commit a4f6d710cf)
(cherry picked from commit 6c911f5d42)
(cherry picked from commit 17b8f7ef12)
2025-05-22 11:32:35 -07:00
Michael Gottesman
63a436ec04 Merge pull request #81615 from gottesmm/release/6.2-rdar151394209
[6.2] Fix a few issues around nonisolated(nonsending) and  protocol witness thunks/vtable thunks
2025-05-22 00:04:01 -07:00
Meghana Gupta
00dcffcd24 [6.2] Fix use-after-free on substituting function type involving conditional ~Escapable with Escapable type 2025-05-21 16:30:02 -07:00
Stephen Canon
85b304220f [6.2] Implement Builtin.select binding llvm select instruction (#81665)
Not used (yet), but needed to implement SIMD.replacing(with:where:)
idiomatically, and probably useful otherwise.

**Explanation:** Makes select available in Swift's builtin module, which
allows implementing concrete SIMD operations more efficiently.
**Risk:** Low. New builtin protected by a feature flag, currently
unused.
**Testing:** New tests added.
**Reviewers:** @eeckstein, @Azoy 
**Main branch PR:** https://github.com/swiftlang/swift/pull/81598
2025-05-21 19:14:33 -04:00
Nate Chandler
701ad64ca6 [TypeLowering] Record packs used in signatures.
To determine whether an instruction may require pack metadata, the types
of its operands are examined.

Previously, the top level type was checked for having a pack in its
signature, and the whole type was checked for having a type anywhere in
its layout (via TypeLowering).  This didn't account for the case where
the metadata was required for a resilient type which uses a pack in its
signature.

Here, during type lowering, a type having a pack in its signature is
counted towards the type having a pack.

Fixes a compiler crash.

rdar://147207926
2025-05-20 17:26:46 -07:00
Michael Gottesman
27cf33676f [silgen] Place the correct isolation on protocol witness thunks.
We were using the isolation from the witness not from the requirement which we
are supposed to do. The witness thunk thunks the isolation from the requirement
to the witness so from an ABI perspective it should have the interface implied
by the requirement's isolation since that is what callers of the witness method
will expect.

rdar://151394209
(cherry picked from commit 39a013f807)
2025-05-19 10:33:57 -07:00
Michael Gottesman
d3145b2380 [silgen] Begin placing isolation on protocol witness thunks.
This will cause tests today to crash since even though we are placing the
isolation now, to make it easier to read, I left in the old isolation selecting
code. This code uses the witness's isolation instead of the requirement's
isolation which is incorrect since the protocol witness thunk needs to look the
requirement from an ABI perspective since the two must be substitutable. The
crash comes from the ABI verification I added in earlier commits.

(cherry picked from commit ff1cbea576)
2025-05-19 10:33:46 -07:00
Michael Gottesman
d3de4a64c9 Merge pull request #81363 from gottesmm/release/6.2-rdar150209093
[6.2][concurrency] Ensure that we treat closures that are nonisolated(nonsending) via their ActorIsolation as nonisolated(nonsending).
2025-05-09 18:43:32 -07:00
Becca Royal-Gordon
51da65bebd Make inlinability non-ABI for @abi
Inlinability doesn’t affect the mangling except in function specializations, which are applied after the fact and should never mangle in information from an ABI-only decl. That means we can simply ban these from `@abi` instead of inferring them.

Also adds some assertions to help double-check that SIL never tries to directly mangle or retrieve inlinability info from an ABI-only decl.
2025-05-08 18:27:57 -07:00
Michael Gottesman
03d8c0ae6b [concurrency] Ensure that we treat closures that are nonisolated(nonsending) via their ActorIsolation as nonisolated(nonsending).
Some notes:

1. In most cases, I think we were getting lucky with this by just inferring the
closure's isolation from its decl context. In the specific case that we were
looking at here, this was not true since we are returning from an @concurrent
async function a nonisolated(nonsending) method that closes over self. This
occurs since even when NonisolatedNonsendingByDefault we want to start importing
objc async functions as nonisolated(nonsending).

2. I also discovered that in the ActorIsolationChecker we were not visiting the
inner autoclosure meaning that we never set the ActorIsolation field on the
closure. After some discussion with @xedin about potentially visiting the
function in the ActorIsolationChecker, we came to the conclusion that this was
likely to result in source stability changes. So we put in a targeted fix just
for autoclosures in this specific case by setting their actor isolation in the
type checker.

3. Beyond adding tests to objc_async_from_swift to make sure that when
NonisolatedNonsendingByDefault is disabled we do the right thing, I noticed that
we did not have any tests that actually tested the behavior around
objc_async_from_swift when NonisolatedNonsendingByDefault is enabled. So I added
the relevant test lines so we can be sure that we get correct behavior in such a
case.

rdar://150209093
(cherry picked from commit ced96aa5cd)
2025-05-07 13:03:45 -07:00
Artem Chikin
8aca9b115c Merge pull request #81146 from artemcm/ParameterizeWeakQueryForSwift-62
[6.2 🍒] Modify clang declaration weakly-imported query to use Swift's code-gen target triple
2025-05-05 09:21:44 -07:00
Artem Chikin
4ac9119bf7 Modify clang declaration weakly-imported query to use Swift's code-gen target triple
Similarly to how https://github.com/swiftlang/swift/pull/70564 configures 'ClangImporter's 'CodeGenerator' using Swift's compilation target triple, we must use the versioned version of the 'isWeakImported' query to determine linkage for imported Clang symbols.
2025-05-01 10:40:10 -07:00
Meghana Gupta
cc72edb119 Introduce end_cow_mutation_addr instruction 2025-04-30 14:38:48 -07:00
Slava Pestov
e3d8e356cb Merge pull request #81080 from slavapestov/fix-rdar149353285-6.2
[6.2] SIL: Use SubstitutionMap::mapIntoTypeExpansionContext() in SILTypeSubstituter
2025-04-29 08:02:37 -04:00
Slava Pestov
88814e4fbf SIL: Use SubstitutionMap::mapIntoTypeExpansionContext() in SILTypeSubstituter
I made a mistake in 47156e006b. There was a
call to call to forAbstract() in SILTypeSubstituter that passed in the
wrong subject ype.

This call was inside SILTypeSubstituter's own implementation of replacing
opaque types with underlying types in a substitution map. This duplicates
an existing utility method in SubstitutionMap anyway, so let's just use
that instead.

Fixes rdar://149353285.
2025-04-28 11:52:41 -04:00
Gábor Horváth
c3a7203221 [6.2][cxx-interop] Fix runtime crash passing FRTs as const FRT*
Explanation: There was an inconsistency between non-const and const FRT
pointers. The former used Direct_Unowned the latter used Indirect
calling convention. We want to use Direct_Unowned for both cases. The
crash was the result of a calling convention mismatch between the
SILFunctionType of a Swift closure and the SILFunctionType of the C++
function's formal parameter that is taking a function pointer. The
compiler tried to insert a conversion between the two function types
that does not exist and caused an assertion in debug compilers and
miscompilation in production compilers.
Issue: rdar://149398905
Risk: Low, the fix is targeted and we change to a well-tested behavior
with non-const FRT pointers.
Testing: Regression test added.
Original PR: #81070
Reviewer: @j-hui
2025-04-25 10:59:45 +01:00
Pavel Yaskevich
d007d7adfb [Frontend] Rename AsyncCallerExecution upcoming feature to NonisolatedNonsendingBeDefault
(cherry picked from commit c110941c27)
2025-04-22 00:33:45 -07:00
Pavel Yaskevich
4093171303 Merge pull request #80957 from xedin/rdar-145672343-6.2
[6.2][Sema/SILGen] Import ObjC async functions as nonisolated(nonsending) by default
2025-04-21 22:21:12 -07:00
Pavel Yaskevich
d40c37e20d [Sema/SILGen] Import ObjC async functions as nonisolated(nonsending) by default
These functions already have special code generation that keeps them
in the caller's isolation context, so there is no behavior change here.

Resolves: rdar://145672343
(cherry picked from commit 07bad98f6d)
2025-04-21 11:22:18 -07:00
Konrad 'ktoso' Malawski
694af3317a [Distributed] Distributed actor usage through protocol with lib-evo must work
This corrects how we were dealing with dispatch thunks -- mostly be
removing a lot of special casing we did but doesn't seem necessary and
instead we correct and emit all the necessary information int TBD.

This builds on  https://github.com/swiftlang/swift/pull/74935 by further refining how we fixed that issue, and adds more regression tests. It also removes a load of special casing of distributed thunks in library evolution mode, which is great.

Resolves and adds regression test for for rdar://145292018

This is also a more proper fix to the previously resolved but in a not-great-way which caused other issues:
- resolves rdar://128284016
- resolves rdar://128310903

Review followup, cleanup test
2025-04-21 20:33:22 +09:00
Pavel Yaskevich
06f880e65c [AST/ASTGen/Sema/Serialization] Remove @execution attribute
Complete the transition from `@execution` to `@concurrent` and `nonisolated(nonsending)`
2025-04-16 13:18:52 -07:00
Pavel Yaskevich
d8c64c1ff0 [AST/Sema] Intoduce nonisolated(nonsending) as a replacement for @execution(caller) 2025-04-16 10:06:08 -07:00
Pavel Yaskevich
4a973f7b4b [AST/Sema] Replace @execution(concurrent) with @concurrent 2025-04-16 10:06:08 -07:00
Andrew Trick
097dba34ff Merge pull request #80709 from atrick/62-irgen-addressable
[6.2] Fix IRGen for @_addressable params which may be "captured".
2025-04-11 18:03:01 -07:00
Nate Chandler
77055a5eb1 [CoroutineAccessors] Control ABI via flag. 2025-04-10 14:47:05 -07:00
Andrew Trick
e048dd1cf1 Fix IRGen for @_addressable params which may be "captured".
Simply omit the 'nocapture' attribute on the parameter.

Fixes rdar://148039510 ([nonescapable] IRGen: lower addressable
params to LLVM: captures(ret: address, provenance))

(cherry picked from commit 2d9df8ff78)
2025-04-10 09:43:13 -07:00
Andrew Trick
78c9fe4c56 Add SILFunctionType::isAddressable & ApplySite::isAddressable.
(cherry picked from commit 501abb0975)
2025-04-08 14:02:19 -07:00
Gábor Horváth
60dced5579 Merge pull request #80584 from swiftlang/gaborh/rvalue-ref-calling-conv-6.2
[6.2][cxx-interop] Fix calling rvalue ref of a trivial type
2025-04-08 16:44:39 +01:00
Gábor Horváth
56e0ea6b6d [6.2][cxx-interop] Fix calling rvalue ref of a trivial type
Explanation: Fixes a runtime crash in the generated binary due to
mismatched calling convention when calling a function taking an rvalue
reference.
Scope: Affects C++ APIs taking rvalue references to directly passed
types (e.g., trivially destructible types).
Issue: rdar://148585343
Risk: Low, targeted to rvalue references which is a newly supported
feature.
Testing: Added tests to test suite
Reviewer: John Hui
2025-04-07 11:42:02 +01:00
Erik Eckstein
eb6ff30be7 SILLinker: de-serialize Executor witness tables for the xExecutorRef builtins
Fixes unresolved symbol linker errors in embedded mode.
rdar://148538336
2025-04-07 11:07:56 +02:00
Erik Eckstein
8978e07b4a SILLinker: make sure to de-serialize base protocol witness tables in embedded mode
Fixes unresolved symbol linker errors or compiler crashes
rdar://148538336
2025-04-07 11:07:56 +02:00
eeckstein
70bc9cdb1f Merge pull request #80486 from eeckstein/fix-struct-extract
SIL: fix the ownership computation of `struct_extract` and `tuple_extract`
2025-04-03 18:32:07 +02:00
Erik Eckstein
0b9f5eb86c SIL: add some asserts to catch broken enum ASTs 2025-04-03 08:14:42 +02:00
Erik Eckstein
f1fc864dc6 SIL: fix the ownership computation of struct_extract and tuple_extract
A struct or tuple value can have "none" ownership even if its type is not trivial.
This happens when the struct/tuple contains a non-trivial enum, but it's initialized with a trivial enum case (e.g. with `Optional.none`).

```
  %1 = enum $Optional<String>, #Optional.none!enumelt
  %2 = struct $S (%32)               // has ownership "none"
  %3 = struct_extract %2, #S.x       // should also have ownership "none" and not "guaranteed"
```

So far it got "guaranteed" ownership which is clearly wrong.

Fixes an assertion crash in redundant load elimination.

https://github.com/swiftlang/swift/issues/80430
rdar://148311534
2025-04-03 06:40:26 +02:00
Joe Groff
562d7dc832 Merge pull request #80438 from jckarter/substitute-away-escapable-lifetime-deps
Type substitution eliminates dependencies with Escapable targets.
2025-04-02 16:56:54 -07: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