Commit Graph

2864 Commits

Author SHA1 Message Date
Andrew Trick
7a29d9d8b6 Fix MoveOnlyObjectCheckerPImpl::check() for mark_dependence.
Handle the presence of mark_dependence instructions after a begin_apply.

Fixes a compiler crash:
"copy of noncopyable typed value. This is a compiler bug. ..."
2025-06-22 17:39:13 -07:00
Andrew Trick
c41715ce8c Fix MoveOnlyObjectCheckerPImpl::check() changed flag
Extract the special pattern matching logic that is otherwise unrelated to the
check() function. This makes it obvious that the implementation was failing to
set the 'changed' flag whenever needed.
2025-06-22 16:38:06 -07:00
eeckstein
116a453556 Merge pull request #82177 from eeckstein/fix-lha
LowerHopToActor: insert a borrow scope for an `Optional<Actor>.none` value
2025-06-12 06:16:45 +02:00
Michael Gottesman
e95b8570d6 Merge pull request #82149 from gottesmm/pr-4cb0aff1ceb16d75edcadd4de2ed612b97924226
Change send-never-sendable of isolated partial applies to use SIL level info instead of AST info.
2025-06-11 13:52:43 -07:00
Erik Eckstein
5489627310 LowerHopToActor: insert a borrow scope for an Optional<Actor>.none value
An optional-none value has "none" ownership, but still a borrow scope is needed.
Fixes a SIL verifier crash.
rdar://153066034
2025-06-11 13:35:21 +02:00
Michael Gottesman
f31236931b Change send-never-sendable of isolated partial applies to use SIL level info instead of AST info.
The reason I am doing this is that we have gotten reports about certain test
cases where we are emitting errors about self being captured in isolated
closures where the sourceloc is invalid. The reason why this happened is that
the decl returned by getIsolationCrossing did not have a SourceLoc since self
was being used implicitly.

In this commit I fix that issue by using SIL level information instead of AST
level information. This guarantees that we get an appropriate SourceLoc. As an
additional benefit, this fixed some extant errors where due to some sort of bug
in the AST, we were saying that a value was nonisolated when it was actor
isolated in some of the error msgs.

rdar://151955519
2025-06-10 08:09:32 -07:00
Andrew Trick
16fffd1704 Fix MoveOnlyWrappedTypeEliminator handling of store_borrow.
Defer visiting an instruction until its operands have been visited. Otherwise,
this pass will crash during ownership verification with invalid operand
ownership.

Fixes rdar://152879038 ([moveonly] MoveOnlyWrappedTypeEliminator ownership
verifier crashes on @_addressableSelf)
2025-06-09 19:45:09 -07:00
Doug Gregor
bc4cf1236b [SIL] Generalize CastingIsolatedConformances to CheckedCastInstOptions
We are going to need to add more flags to the various checked cast
instructions. Generalize the CastingIsolatedConformances bit in all of
these SIL instructions to an "options" struct that's easier to extend.

Precursor to rdar://152335805.
2025-06-04 17:12:28 -07:00
Daniil Kovalev
1e403ecf5c [AutoDiff] Support custom derivatives for @_alwaysEmitIntoClient functions (#78908)
Consider an `@_alwaysEmitIntoClient` function and a custom derivative
defined
for it. Previously, such a combination resulted different errors under
different
circumstances.

Sometimes, there were linker errors due to missing derivative function
symbol -
these occurred when we tried to find the derivative in a module, while
it
should have been emitted into client's code (and it did not happen).

Sometimes, there were SIL verification failures like this:

```
SIL verification failed: internal/private function cannot be serialized or serializable: !F->isAnySerialized() || embedded
```

Linkage and serialization options for the derivative were not handled
properly,
and, instead of PublicNonABI linkage, we had Private one which is
unsupported
for serialization - but we need to serialize `@_alwaysEmitIntoClient`
functions
so the client's code is able to see them.

This patch resolves the issue and adds proper handling of custom
derivatives
of `@_alwaysEmitIntoClient` functions. Note that either both the
function and
its custom derivative or none of them should have
`@_alwaysEmitIntoClient`
attribute, mismatch in this attribute is not supported.

The following cases are handled (assume that in each case client's code
uses
the derivative).

1. Both the function and its derivative are defined in a single file in
   one module.

2. Both the function and its derivative are defined in different files
which
   are compiled to a single module.

3. The function is defined in one module, its derivative is defined in
another
   module.

4. The function and the derivative are defined as members of a protocol
extension in two separate modules - one for the function and one for the
   derivative. A struct conforming the protocol is defined in the third
   module.

5. The function and the derivative are defined as members of a struct
extension in two separate modules - one for the function and one for the
   derivative.

The changes allow to define derivatives for methods of `SIMD`.

Fixes #54445
<!--
If this pull request is targeting a release branch, please fill out the
following form:

https://github.com/swiftlang/.github/blob/main/PULL_REQUEST_TEMPLATE/release.md?plain=1

Otherwise, replace this comment with a description of your changes and
rationale. Provide links to external references/discussions if
appropriate.
If this pull request resolves any GitHub issues, link them like so:

  Resolves <link to issue>, resolves <link to another issue>.

For more information about linking a pull request to an issue, see:

https://docs.github.com/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue
-->

<!--
Before merging this pull request, you must run the Swift continuous
integration tests.
For information about triggering CI builds via @swift-ci, see:

https://github.com/apple/swift/blob/main/docs/ContinuousIntegration.md#swift-ci

Thank you for your contribution to Swift!
-->
2025-05-25 09:47:15 -04:00
Erik Eckstein
e0f5888a8d 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-21 20:03:53 +02:00
Erik Eckstein
9052652651 add the prepareInitialization builtin.
It is like `zeroInitializer`, but does not actually initialize the memory.
It only indicates to mandatory passes that the memory is going to be initialized.
2025-05-20 20:46:33 +02:00
nate-chandler
8959f60ab1 Merge pull request #81566 from nate-chandler/rdar151325025
[MoveOnlyChecker] Don't complete phis.
2025-05-19 07:21:21 -07:00
Erik Eckstein
ddbc3a2fad SILGen: insert an end_lifetime in the throw-branch of a Builtin.emplace
When the called closure throws an error, it needs to clean up the buffer.
This means that the buffer is uninitialized at this point.

We need an `end_lifetime` so that the move-only checker doesn't insert a wrong `destroy_addr` because it thinks that the buffer is initialized.

Fixes a mis-compile.

rdar://151461109
2025-05-16 21:54:39 +02:00
Nate Chandler
9b3db3646a [MoveOnlyChecker] Don't complete phis.
Apply the MoveOnlyAddressChecker change from
https://github.com/swiftlang/swift/pull/73358 to the
MoveOnly[Value]Checker.

After 7713eef817, before running value
checking, all lifetimes in the function are completed.  That doesn't
quite work because lifetime completion expects not to encounter
reborrows or their adjacent phis.

rdar://151325025
2025-05-16 11:58:44 -07:00
Nate Chandler
d18a0178bb [Gardening] Whitespace cleanup. 2025-05-16 11:36:22 -07:00
Nate Chandler
1765f0b1e0 [MoveOnly] Fix consume of addr with mutated field.
Don't fail out of use visitation when encountering an apply which uses a
field of the value as an inout parameter.

rdar://139666145
2025-05-15 15:19:26 -07:00
Nate Chandler
dcde328fe9 [Gardening] MoveOnly: Move and add comment. 2025-05-15 15:18:50 -07:00
Pavel Yaskevich
778be225e1 Merge pull request #81277 from ludwwwig/remove-unused-header
[gardening] Delete an empty header
2025-05-06 00:23:06 -07:00
Ludwig Hollmann
39aa950660 Update file header comments for headers in lib. 2025-05-04 22:26:26 +02:00
Ludwig Hollmann
9482a1be6d [gardening] Delete an empty header.
This header was supposed to be deleted by #63783.
2025-05-03 22:43:48 +02:00
Meghana Gupta
35d62a4a36 Introduce end_cow_mutation_addr instruction 2025-04-30 13:39:45 -07: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
nate-chandler
e4d3207b40 Revert "MoveOnlyChecker: Treat trivial stores as reinitializations rather than initializations." 2025-04-24 12:10:07 -07:00
Anthony
c9b17383c8 Grammatical corrections for compound modifiers 2025-04-24 09:21:32 +02:00
nate-chandler
3e35fa423d Merge pull request #81015 from nate-chandler/rdar141279635
[MoveOnly] Fix consumption of opened existentials.
2025-04-23 13:26:47 -07:00
Nate Chandler
4786bede68 [MoveOnly] Fix consumption of opened existentials.
Enable walking into `TypeOffsetSizePair`s from an existential into an
archetype.  And set the access kind on `open_existential_addr`
instructions which are the sources of rewritten `copy_addr`s to mutable.

rdar://141279635
2025-04-22 17:59:37 -07:00
Nate Chandler
6669b2497d [NFC] MoveOnly: Add parameter to function.
For now, it is unused.
2025-04-22 17:59:37 -07:00
Nate Chandler
3fbf10913c [NFC] MoveOnly: Delete some dead code.
Before adding a new parameter to the function being called.
2025-04-22 17:59:36 -07:00
Meghana Gupta
3547ba5e48 Merge pull request #80961 from meg-gupta/fixoslogcrash
Fix crash in OSLogOptimization
2025-04-21 21:21:56 -07:00
Meghana Gupta
71bb088b80 Fix crash in OSLogOptimization
A metatype need not always come from a metatype instruction. It can come from
a SILArgument. Fix the invalid cast operation in OSLogOptimization.

Fixes rdar://146160325
2025-04-21 11:49:31 -07:00
Meghana Gupta
b76047a846 Merge pull request #80930 from meg-gupta/relandpr
Reland #79707
2025-04-21 11:19:33 -07:00
Meghana Gupta
d81b5f7e27 Reland #79707
Revert "Merge pull request #80767 from meg-gupta/reverttransparent"

This reverts commit 198a802719, reversing
changes made to 8eb43af590.
2025-04-19 09:55:06 -07:00
Erik Eckstein
6c31eb0c43 embedded: rewrite the diagnostic pass for embedded swift
1. move embedded diagnostics out of the PerformanceDiagnostics pass. It was completely separated from the other logic in this pass, anyway.
2. rewrite it in swift
3. fix several bugs, that means: missed diagnostics, which led to IRGen crashes
  * look at all methods in witness tables, including base protocols and associated conformances
  * visit all functions in the call tree, including generic functions with class bound generic arguments
  * handle all instructions, e.g. concurrency builtins
4. improve error messages by adding meaningful call-site information. For example:
  * if the error is in a specialized function, report where the generic function is originally specialized with concrete types
  * if the error is in a protocol witness method, report where the existential is created
2025-04-18 06:58:40 +02:00
Joe Groff
3860023081 Merge pull request #80839 from jckarter/moveonly-trivial-field-as-reinitialize
MoveOnlyChecker: Treat trivial stores as reinitializations rather than initializations.
2025-04-16 17:38:55 -07:00
Meghana Gupta
198a802719 Merge pull request #80767 from meg-gupta/reverttransparent
Revert #79707
2025-04-15 15:45:15 -07:00
Joe Groff
a1ff2e38dc MoveOnlyChecker: Treat trivial stores as reinitializations rather than initializations.
A trivial store is allowed to occur on an existing live value, and should not
trigger an attempt to destroy the original value completely. Fixes rdar://147791932.
2025-04-15 13:40:22 -07:00
Meghana Gupta
f462bc5d93 Revert "Merge pull request #79707 from DougGregor/transparent-integer-conversions"
This reverts commit 9c2c4ea07f, reversing
changes made to 829e03c104.
2025-04-11 10:42:17 -07:00
Michael Gottesman
ecb745ea18 Merge pull request #80745 from gottesmm/rdar149019222
[rbi] Teach RBI how to handle non-Sendable bases of Sendable values
2025-04-10 19:57:01 -07:00
Michael Gottesman
23b6937cbc [rbi] Make it so that we correctly do not error on uses of Sendable values that are projected from non-Sendable bases.
Specifically, we only do this if the base is a let or if it is a var but not
captured by reference.

rdar://149019222
2025-04-10 14:53:56 -07:00
Michael Gottesman
a045c9880a [rbi] Add the ability to add flags to PartitionOp.
I am doing this so I can mark requires as being on a mutable non-Sendable base
from a Sendable value.

I also took this as an opportunity to compress the size of PartitionOp to be 24
bytes instead of 40 bytes.
2025-04-10 14:53:56 -07:00
Michael Gottesman
c846c2279e [rbi] Refactor getUnderlyingTrackedValue so that for addresses we return both a value and a base in certain situations.
Previously, when we saw any Sendable type and attempted to look up an underlying
tracked value, we just bailed. This caused an issue in situations like the
following where we need to emit an error:

```swift
func test() {
  var x = 5
  Task.detached { x += 1 }
  print(x)
}
```

The problem with the above example is that despite value in x being Sendable,
'x' is actually in a non-Sendable box. We are passing that non-Sendable box into
the detached task by reference causing a race against the read from the
non-Sendable box later in the function. In SE-0414, this is explicitly banned in
the section called "Accessing Sendable fields of non-Sendable types after weak
transferring". In this example, the box is the non-Sendable type and the value
stored in the box is the Sendable field.

To properly represent this, we need to change how the underlying object part of
our layering returns underlying objects and vends TrackableValues to the actual
analysis for addresses. NOTE: We leave the current behavior alone for SIL
objects.

By doing this, in situations like the above, despite have a Sendable value (the
integer), we are able to ensure that we require that the non-Sendable box
containing the integer is not used after we have sent it into the other Task
despite us not actually using the box directly.

Below I describe the representation change in more detail and describe the
various cases here. In this commit, I only change the representation and do not
actually use the new base information. I do that in the next commit to make this
change easier for others to read and review. I made sure that change was NFC by
leaving RegionAnalysis.cpp:727 returning an optional.none if the value found was
a Sendable value.

----

The way we modify the representation is that we instead of just returning a
single TrackedValue return a pair of tracked values, one for the base and one
for the "value". We return this pair in what is labeled a
"TrackableValueLookupResult":

```c++
struct TrackableValueLookupResult {
  TrackableValue value;

  std::optional<TrackableValue> base;

  TrackableValueLookupResult(TrackableValue value)
    : value(value), base() {}
  TrackableValueLookupResult(TrackableValue value, TrackableValue base)
    : value(value), base(base) {}
};
```

In the case where we are accessing a projection path out of a non-Sendable type
that contains all non-Sendable fields, we do not do anything different than we
did previously. We just walk up from use->def until we find the access path base
which we use as the representative of the leaf of the chain and return
TrackableValueLookupResult(access path base).

In the case where we are accessing a Sendable leaf type projected from a
non-Sendable base, we store the leaf type as our value and return the actual
non-Sendable base in TrackableValueLookupResult. Importantly this ensures that
even though our Sendable value will be ignored by the rest of the analysis, the
rest of the analysis will ensure that our base is required if our base is a var
that had been escaped into a closure by reference.

In the case where we are accessing a non-Sendable leaf type projected from a
Sendable type (which we may have continued to be projected subsequently out of
additional Sendable types or a non-Sendable type), we make the last type on the
projection path before the Sendable type, the value of the leaf type. We return
the eventual access path base as our underlying value base. The logic here is
that since we are dealing with access paths, our access path can only consist of
projections into a recursive value type (e.x.: struct/tuple/enum... never a
class). The minute that we hit a pointer or a class, we will no longer be along
the access path since we will be traversing a non-contiguous piece of
memory (consider a class vs the class's storage) and the traversal from use->def
will stop. Thus, we know that there are only two ways we can get a field in that
value type to be Sendable and have a non-Sendable field:

1. The struct can be @unchecked Sendable. In such a case, we want to treat the
leaf field as part of its own disconnected region.

2. The struct can be global actor isolated. In such a case, we want to treat the
leaf field as part of the global actor's region rather than whatever actor.

The reason why we return the eventual access path base as our tracked value base
is that we want to ensure that if the var value had been escaped by reference,
we can require that the var not be sent since we are going to attempt to access
state from the var in order to get the global actor guarded struct that we are
going to attempt to extract our non-Sendable leaf value out of.
2025-04-10 10:01:18 -07:00
Michael Gottesman
77a86a525b Merge pull request #79891 from gottesmm/pr-9df6772bb44656cbd85cad72ee5c3637caab8956
Make Feature a struct enum so we can put methods on it.
2025-04-07 10:32:23 -07:00
Anthony Latsis
5c190b9613 AST: Cut down on DescriptiveDeclKind usage in DiagnosticsSIL.def 2025-04-05 12:31:20 +01:00
Michael Gottesman
3ff9463957 Make Feature a struct enum so we can put methods on it.
Just noticed this as I was looking at making other changes.
2025-04-05 10:08:29 +01:00
Joe Groff
367ad789c5 Merge pull request #80470 from jckarter/trivial-borrow-transitive-uses
MoveOnlyChecker: Don't follow trivial transitive uses of borrows.
2025-04-03 13:13:24 -07:00
Joe Groff
b5d242ad2c MoveOnlyChecker: Don't follow trivial transitive uses of borrows.
Trivial values don't have ownership tracked, so their uses can't affect the
lifetime of the original borrow. Fixes rdar://148457155.
2025-04-02 13:25:46 -07:00
Allan Shortlidge
8f2d5a759e Merge pull request #80321 from tshortli/warnings 2025-03-30 16:14:21 -07:00
Arnold Schwaighofer
0222f90cf0 Merge pull request #80256 from aschwaighofer/disable_cond_fail_by_function
Disable cond_fail instructions by filtering the instruction's parent function
2025-03-28 13:18:19 -07:00
Allan Shortlidge
43fcb2cecd SIL/SILOptimizer: Fix unused variable warnings. 2025-03-28 12:33:39 -07:00
Doug Gregor
e0b52cd20e [SIL] Extend checked-cast instructions with "prohibit isolated conformances" flag
When performing a dynamic cast to an existential type that satisfies
(Metatype)Sendable, it is unsafe to allow isolated conformances of any
kind to satisfy protocol requirements for the existential. Identify
these cases and mark the corresponding cast instructions with a new flag,
`[prohibit_isolated_conformances]` that will be used to indicate to the
runtime that isolated conformances need to be rejected.
2025-03-26 22:31:47 -07:00