Commit Graph

2862 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
Jamie
3c3f837830 [SILOptimizer]: fix some missing use after consume diagnostics
Updates ConsumeOperatorCopyableValuesChecker to identify store_borrow
instructions as a liveness-affecting use so that patterns that would
previously slip through undiagnosed are correctly identified. e.g.

```swift
func use<V>(_ v: borrowing V) {}

func f() {
  let a = A()
  _ = consume a
  use(a) // previously would not be diagnosed
}
```

(cherry picked from commit 754a3007d5)
2025-08-04 12:30:06 -05:00
Michael Gottesman
48b88b0847 [micro-opt] Flip a check to improve perf. 2025-07-16 12:18:40 -07:00
Michael Gottesman
182149978e [concurrency] Make optimize hop to executor more conservative for 6.2 around caller isolation inheriting functions.
Specifically for 6.2, we are making optimize hop to executor more conservative
around caller isolation inheriting functions. This means that we are:

1. No longer treating calls to caller isolation inheriting functions as having a
hop in their prologue. In terms of this pass, it means that when determining
dead hop to executors, we no longer think that a caller isolation inheriting
function means that an earlier hop to executor is not required.

2. Treating returns from caller isolation inheriting callees as requiring a
hop. The reason why we are doing this is that we can no longer assume that our
caller will hop after we return.

Post 6.2, there are three main changes we are going to make:

* Forward Dataflow

Caller isolation inheriting functions will no longer be treated as suspension
points meaning that we will be able to propagate hops over them and can assume
that we know the actor that we are on when we enter the function. Practically
this means that trees of calls that involve just nonisolated(nonsending) async
functions will avoid /all/ hop to executor calls since we will be able to
eliminate all of them since the dataflow will just propagate forward from the
entrance that we are already on the actor.

* Backwards Dataflow

A caller isolation inheriting call site will still cause preceding
hop_to_executor functions to be live. This is because we need to ensure that we
are on the caller isolation inheriting actor before we hit the call site. If we
are already on that actor, the hop will be eliminated by the forward pass. But
if the hop has not been eliminated, then the hop must be needed to return us to
the appropriate actor.

We will also keep the behavior that returns from a caller isolation inheriting
function are considered to keep hop to executors alive. If we were able to
propagate to a hop to executor before the return inst with the forward dataflow,
then we know that we are guaranteed to still be on the relevant actor. If the
hop to executor is still there, then we need it to ensure that our caller can
treat the caller isolation inheriting function as a non-suspension point.

rdar://155905383
(cherry picked from commit b3942424c8)
2025-07-15 17:32:19 -07:00
Michael Gottesman
f920aba2f9 [rbi] Use interned StringRefs for diagnostics instead of SmallString<64>.
This makes the code easier to write and also prevents any lifetime issues from a
diagnostic outliving the SmallString due to diagnostic transactions.

(cherry picked from commit 010fa39f31)
2025-07-09 12:25:06 -07:00
Michael Gottesman
ca8cdc1fd7 [rbi] Thread through a SILFunction into print routines so we can access LangOpts.Features so we can change how we print based off of NonisolatedNonsendingByDefault.
We do not actually use this information yet though... This is just to ease
review.

(cherry picked from commit 4433ab8d81)
2025-07-09 12:24:17 -07:00
Michael Gottesman
0ec2527bdc [rbi] Wrap use ActorIsolation::printForDiagnostics with our own SILIsolationInfo::printActorIsolationForDiagnostics.
I am doing this so that I can change how we emit the diagnostics just for
SendNonSendable depending on if NonisolatedNonsendingByDefault is enabled
without touching the rest of the compiler.

This does not actually change any of the actual output though.

(cherry picked from commit 4ce4fc4f95)
2025-07-09 12:24:17 -07:00
Anthony Latsis
f5d547cd44 AST: Cut down on DescriptiveDeclKind usage in DiagnosticsSIL.def
(cherry picked from commit 5c190b9613)
2025-07-09 12:24:17 -07:00
nate-chandler
0cd265d0f8 Merge pull request #82903 from nate-chandler/cherrypick/release/6.2/rdar155059418
6.2: [ODL] Visit objc_method insts.
2025-07-09 11:16:10 -07:00
Doug Gregor
6637960bdc [SE-0470] Track the potential introduction of isolated conformances in regions
When we introduce isolation due to a (potential) isolated conformance,
keep track of the protocol to which the conformance could be
introduced. Use this information for two reasons:

1. Downgrade the error to a warning in Swift < 7, because we are newly
diagnosing these
2. Add a note indicating where the isolated conformance could be introduced.

(cherry picked from commit 02c34bb830)
2025-07-08 17:32:06 -07:00
Nate Chandler
ba0b2e7c51 [ODL] Visit objc_method insts.
No update is needed for the values they produce.  This pass should
really be refactored not to crash on instructions that aren't explicitly
listed or at least not to compile if not every instruction is listed.

rdar://155059418
2025-07-08 17:24:41 -07:00
Erik Eckstein
a538252a77 MoveOnlyWrappedTypeEliminator: handle EndCOWMutationAddr
fixes a compiler crash
rdar://154416511
2025-07-03 13:10:00 +02:00
Andrew Trick
2052d2c61e 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. ..."

(cherry picked from commit 7a29d9d8b6)
2025-06-24 12:54:39 -07:00
Andrew Trick
4cd7f450a3 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.

(cherry picked from commit c41715ce8c)
2025-06-24 12:54:39 -07:00
Michael Gottesman
d6891a7395 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
(cherry picked from commit f31236931b)
2025-06-11 14:02:01 -07:00
Andrew Trick
48ba887e6b 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)

(cherry picked from commit 16fffd1704)
2025-06-09 19:49:36 -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
eeckstein
ecdcef8c15 Merge pull request #81606 from eeckstein/fix-builtin-emplace-6.2
[6.2] SILGen: insert an `end_lifetime` in the throw-branch of a `Builtin.emplace`
2025-05-19 22:22:14 +02:00
nate-chandler
8e0a3896af Merge pull request #81574 from nate-chandler/cherrypick/release/6.2/rdar151325025
6.2: [MoveOnlyChecker] Don't complete phis.
2025-05-19 08:19:26 -07:00
Erik Eckstein
09d2464578 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-19 13:47:49 +02:00
nate-chandler
968b82e5fa Merge pull request #81544 from nate-chandler/cherrypick/release/6.2/rdar139666145
6.2: [MoveOnly] Fix consume of addr with mutated field.
2025-05-16 16:33:26 -07:00
Nate Chandler
f648171f0d [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 14:24:22 -07:00
Nate Chandler
acb6c7184c [Gardening] Whitespace cleanup. 2025-05-16 14:24:22 -07:00
Nate Chandler
b014950aac [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:23:00 -07:00
Nate Chandler
5bd7aa777f [Gardening] MoveOnly: Move and add comment. 2025-05-15 15:23:00 -07:00
Michael Gottesman
e92e9dff0c Make Feature a struct enum so we can put methods on it.
Just noticed this as I was looking at making other changes.

(cherry picked from commit 3ff9463957)
2025-05-14 16:07:04 -07:00
Meghana Gupta
cc72edb119 Introduce end_cow_mutation_addr instruction 2025-04-30 14:38:48 -07:00
nate-chandler
e539d0f996 Revert "[6.2] MoveOnlyChecker: Treat trivial stores as reinitializations rather than initializations." 2025-04-24 12:11:32 -07:00
Nate Chandler
875e67a2b9 [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-23 13:28:49 -07:00
Nate Chandler
f201ceebe1 [NFC] MoveOnly: Add parameter to function.
For now, it is unused.
2025-04-23 13:28:49 -07:00
Nate Chandler
b3d9e0a5aa [NFC] MoveOnly: Delete some dead code.
Before adding a new parameter to the function being called.
2025-04-23 13:28:49 -07:00
Meghana Gupta
e4b60db09a Merge pull request #80956 from meg-gupta/cposlog
[6.2] Reland #79707
2025-04-22 21:00:05 -07:00
Meghana Gupta
336f3b42d7 Merge pull request #80962 from meg-gupta/cpfixoslogcrash
[6.2] Fix crash in OSLogOptimization
2025-04-21 21:17:50 -07:00
Meghana Gupta
d295bf96c0 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 12:03:16 -07:00
Meghana Gupta
adb809369f Reland #79707
Revert "Merge pull request #80767 from meg-gupta/reverttransparent"

This reverts commit 198a802719, reversing
changes made to 8eb43af590.
2025-04-21 11:23:00 -07:00
Joe Groff
4fd61eeed6 Merge pull request #80854 from jckarter/moveonly-trivial-field-as-reinitialize-6.2
[6.2] MoveOnlyChecker: Treat trivial stores as reinitializations rather than initializations.
2025-04-21 09:48:50 -07:00
Michael Gottesman
d1470fe55b Merge pull request #80840 from gottesmm/release/6.2-149019222
[6.2][rbi] Teach RBI how to handle non-Sendable bases of Sendable values
2025-04-16 14:24:55 -07:00
Joe Groff
66764a3fc3 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-16 07:44:53 -07:00
Michael Gottesman
46926720a9 [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
(cherry picked from commit 23b6937cbc)
2025-04-15 14:58:04 -07:00
Michael Gottesman
a93e994593 [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.

(cherry picked from commit a045c9880a)
2025-04-15 14:57:57 -07:00
Michael Gottesman
ed98efa08a [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.

(cherry picked from commit c846c2279e)
2025-04-15 14:57:47 -07:00
Meghana Gupta
c22acc530e [6.2] Revert #79707
Revert "Merge pull request #79707 from DougGregor/transparent-integer-conversions"

This reverts commit 9c2c4ea07f, reversing
changes made to 829e03c104.
2025-04-11 10:57:14 -07: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
Andrew Trick
97b249bd11 Merge pull request #80263 from atrick/markdep-addr
SIL: add mark_dependence_addr
2025-03-26 10:33:42 -07:00
Arnold Schwaighofer
cc76edea15 Add a -print-cond-fail-messages-include-function-name option
Additionally, also consider the function name as the key to base cond_fail
removal on under `-cond-fail-config-file`.
2025-03-26 08:12:35 -07:00