Commit Graph

11193 Commits

Author SHA1 Message Date
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
Arnold Schwaighofer
423fef048e Merge pull request #82027 from aschwaighofer/fix_looprotate_densemap_subscript_bug_6.2
[6.2] LoopRotate: Fix a by reference map bug under reallocation
2025-06-06 12:56:21 -07:00
Allan Shortlidge
4c34f08b99 SILOptimizer: Fix an unused variable warning. 2025-06-05 15:40:30 -07:00
Arnold Schwaighofer
300ba4c051 [6.2] LoopRotate: Fix a by reference map bug under reallocation
Issue:
When using a densemap subscript expression on both sides of an
assignment in the same statement of the same map we run into the issue
that the map can reallocate because of the assignment but we are
referencing the value of the RHS map subscript by reference --
i.e we can reference deallocated memory.

Not good.

Scope: A "silent" memory error that one might run into including the
reporter of the bug.

Risk: Extremely, low. The fix is spliting an assignment from a map value
to a map entry into: A value assignment of the map value to a local. And
then storing the local in the map entry forgoing the reference of
reallocated memory bug.

```
  valueMap[bfi] = valueMap[bfi->getBorrowedValue()];

=>

  auto mappedMValue = valueMap[bfi->getBorrowedValue()];
  valueMap[bfi] = mappedValue;
```

Reviewed by: Meghana G, Erik E., Joe G.

Testing: The fix was tested on the reporting project.

rdar://151031297
2025-06-05 07:09:22 -07:00
Erik Eckstein
9be5e2dc3b LICM: fix a wrong tuple type when splitting loads
When creating a tuple, the type needs to be specified because otherwise, if the original tuple has labels, it will cause a type mismatch verification error.

rdar://152588539
2025-06-05 07:21:35 +02:00
nate-chandler
4225fbc5c8 Merge pull request #81959 from nate-chandler/cherrypick/release/6.2/rdar152431332
6.2: [DestroyAddrHoisting] Skip init_enum_data_addrs.
2025-06-04 10:42:21 -07:00
Nate Chandler
625e6d77a3 [DestroyAddrHoisting] Skip init_enum_data_addrs.
A destroy of an `init_enum_data_addr` is not equivalent to a destroy of
the whole enum's address.  Treat such destroys just like destroys of
`struct_element_addr`s are treated: by bailing out.

rdar://152431332
2025-06-03 15:54:11 -07:00
Michael Gottesman
7b434c2747 [rbi] Lookthrough an invocation of DistributedActor.asLocalActor when determining actor instances.
In this case, what is happening is that in SILGen, we insert implicit
DistributedActor.asLocalActor calls to convert a distributed actor to its local
any Actor typed form. The intention is that the actor parameter and result are
considered the same... but there is nothing at the SIL level to enforce that. In
this commit, I change ActorInstance (the utility that defines actor identity at
a value level) to look through such a call.

I implemented this by just recognizing the decl directly. We already do this in
parts of SILGen, so I don't really see a problem with doing this. It also
provides a nice benefit that we do not have to modify SILFunctionType to
represent this or put a @_semantic attribute on the getter.

NOTE: Generally, Sema prevents us from mixing together different actors. In this
case, Sema does not help us since this call is inserted implicitly by the
distributed actor implementation in SILGen. So this is not a problem in general.

rdar://152436817
(cherry picked from commit 331626e6fa)
2025-06-03 09:05:32 -07:00
nate-chandler
57651963f1 Merge pull request #81875 from nate-chandler/cherrypick/release/6.2/rdar152195094
6.2: [DestroyAddrHoisting] Don't destructure NE aggs.
2025-05-30 21:06:59 -07:00
Nate Chandler
c894bdffab [NFC] SIL: This utility takes a func not a module.
In preparation to use the function in the implementation.
2025-05-30 13:27:45 -07:00
Michael Gottesman
1b1cf0871e [send-non-sendable] Recurse to the full underlying value computation instead of just the object one when computing the underlying object of an address.
Otherwise, depending on the exact value that we perform the underlying look up
at... we will get different underlying values. To see this consider the
following SIL:

```sil
  %1 = alloc_stack $MyEnum<T>
  copy_addr %0 to [init] %1
  %2 = unchecked_take_enum_data_addr %1, #MyEnum.some!enumelt
  %3 = load [take] %2
  %4 = project_box %3, 0
  %5 = load_borrow %4
  %6 = copy_value %5
```

If one were to perform an underlying object query on %4 or %3, one would get
back an underlying object of %1. In contrast, if one performed the same
operation on %5, then one would get back %3. The reason why this happens is that
we first see we have an object but that it is from a load_borrow so we need to
look through the load_borrow and perform the address underlying value
computation. When we do that, we find project_box to be the value. project_box
is special since it is the only address base we ever look through since from an
underlying object perspective, we want to consider the box to be the underlying
object rather than the projection. So thus we see that the result of the
underlying address computation is that the underlying address is from a load
[take]. Since we then pass in load [take] recursively into the underlying value
object computation, we just return load [take]. In contrast, the correct
behavior is to do the more general recurse that recognizes that we have a load
[take] and that we need to look through it and perform the address computation.

rdar://151598281
(cherry picked from commit 904ebc6784)
2025-05-27 11:42:08 -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
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
Meghana Gupta
58300798ca Merge pull request #81521 from meg-gupta/disableverificationcp
[6.2] Add a new semantics attribute to disable SIL verification on a function
2025-05-19 18:01:34 -07: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
Meghana Gupta
06f681777d [6.2] Add a new semantics attribute to disable SIL verification on a function 2025-05-15 14:32:09 -07:00
Pavel Yaskevich
ee3aca2ef3 Merge pull request #81518 from xedin/rdar-148124973-6.2
[6.2][Frontend] Add a way to print features supported by the compiler
2025-05-15 09:46:15 -07:00
Meghana Gupta
4fbee45ba2 Merge pull request #81512 from meg-gupta/fixdropdeinitcp
[6.2] Fix InstructionDeleter for drop_deinit instruction
2025-05-14 19:56:55 -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
9f0f1da076 [6.2] Fix InstructionDeleter for drop_deinit instruction
Currently we delete dead drop_deinit instructions in InstructionDeleter. For address results, we may end up with ownership errors after being promoted to value forms. For value results, fixLifetimes mode of InstructionDeleter will insert an illegal destroy_value

rdar://151104993
2025-05-14 10:50:43 -07:00
Erik Eckstein
881351bcb5 FunctionSignatureOptimization: don't convert indirect @in to @in_guaranteed if the argument is mutated
This fixes a verifier error because an `@in_guaranteed` argument must not be mutated.

https://github.com/swiftlang/swift/issues/81444
rdar://151147971
2025-05-14 07:09:33 +02:00
Meghana Gupta
cc72edb119 Introduce end_cow_mutation_addr instruction 2025-04-30 14:38:48 -07:00
Erik Eckstein
2041712eb7 LICM: handle memory dependency for store sinking correctly
Prevent sinking of stores if there are instructions other than `load` which may read from memory.
This kind of memory dependencies were ignored.
Fixes SIL verifier crashes or - in worst case - miscompiles.

rdar://150205299
2025-04-30 07:24:50 +02:00
Erik Eckstein
ca75b6ec86 Optimize keypaths in language 6 mode
In language 6 mode keypath instructions are created as existentials and the optimizer needs to look through the `open_existential_ref` instructions to recognize a keypath.

rdar://150173106
2025-04-29 11:37:02 +02:00
nate-chandler
e539d0f996 Revert "[6.2] MoveOnlyChecker: Treat trivial stores as reinitializations rather than initializations." 2025-04-24 12:11:32 -07:00
Michael Gottesman
da57f9665c Merge pull request #80953 from gottesmm/release/6.2-135459885
[6.2][sil-isolation-info] When determining isolation of a function arg, use its VarDecl.
2025-04-24 10:14:41 -07:00
Evan Wilde
9fd85fdc01 Merge pull request #81050 from etcwilde/ewilde/6.2-cxxonly-disable-broken-passes
[6.2🍒] SILOptimizer: Disable invalid passes in C++-only compiler
2025-04-24 10:13:25 -07:00
nate-chandler
307f330a42 Merge pull request #81060 from nate-chandler/cherrypick/release/6.2/rdar149896608
6.2: Revert "[DCE] Verify liveness of completed lifetimes."
2025-04-24 07:00:10 -07:00
nate-chandler
e2d59bdb5b Merge pull request #81044 from nate-chandler/rdar141279635
6.2: [MoveOnly] Fix consumption of opened existentials.
2025-04-23 20:53:04 -07:00
Nate Chandler
d249ed60c4 Revert "[DCE] Verify liveness of completed lifetimes."
This reverts commit 3ec9b269f5.

rdar://149896608
2025-04-23 17:02:06 -07:00
nate-chandler
05b997d9aa Merge pull request #81013 from nate-chandler/cherrypick/release/6.2/rdar147586673
6.2: [OSSACanOwned] Don't dead-end extend if consumed.
2025-04-23 15:40:54 -07:00
Evan Wilde
30b4aa5a58 SILOptimizer: Disable invalid passes in C++-only compiler
The SimplifyCFG and LoopRotate passes result in verification failures
when built in a compiler that is not built with Swift sources enabled.

 - Scope: Disables optimizers passes in C++-only compiler for
   bootstrapping purposes. Has no impact on full Swift compiler build.
 - Risk: Low. This change has no impact in build environments where a
   Swift compiler is available.
 - Reviewers: @eeckstein
 - Testing: PR testing -- Full PR testing fails if passes are
   accidentally disabled.

Fixes: rdar://146357242

(cherry picked from commit bca1378fdb)
2025-04-23 14:07:35 -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
Nate Chandler
f2a00f0fcb [OSSACanOwned] Don't dead-end extend if consumed.
When the utility is used by the ConsumeOperatorCopyableValuesChecker,
the checker guarantees that the lifetime can end at the consumes, that
there are no uses after those consumes.  In that circumstance, the
utility maintains liveness to those consumes and as far as possible
without introducing a copy everywhere else.

The lack of complete lifetimes has forced the utility to extend liveness
of values to dead-ends.  That extension, however, is in tension with the
use that the checker is putting the utility to.  If there is a dead-end
after a consume, liveness must not be maintained to that dead-end.

rdar://147586673
2025-04-22 16:49:42 -07:00
Nate Chandler
19242dffde [NFC] OSSACanOwned: Extracted predicate. 2025-04-22 16:49:42 -07:00
Nate Chandler
6b977dc43b [NFC] OSSACanOwned: Renamed field. 2025-04-22 16:49:41 -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
Matt Seaman
ae1072a438 Fix assertion failure when performing constant-evaluation through basic blocks with non-constant arguments 2025-04-21 11:44:53 -07:00