Commit Graph

11193 Commits

Author SHA1 Message Date
Mishal Shah
0fc5035ee1 Merge pull request #74574 from eeckstein/fix-devirtualizer 2024-06-22 07:53:17 -07:00
Mishal Shah
790d4da2e4 Merge pull request #74593 from Azoy/why-are-we-copying
[SILOptimizer] Handle the mark_dependence chain when eliminating copies in ClosureLifetimeFixup
2024-06-21 15:44:57 -07:00
Erik Eckstein
864c1434e8 Devirtualizer: fix a crash due to a not supported bitcast of ABI compatible types
When devirtualizing witness method calls, it can happen that we need a cast between ABI compatible return types.
We were missing supporting type casts between nominal types which are ABI compatible.

This comes from whole-module reasoning of protocol conformances.
If a protocol only has a single conformance where the associated type (`ID`) is some concrete type (e.g. `Int`), then the devirtualizer knows that `p.get()` can only return an `Int`:
```
public struct X2<ID> {
  let p: any P2<ID>
  public func testit(i: ID, x: ID) -> S2<ID> {
    return p.get(x: x)
  }
}
```
and after devirtualizing the `get` function, its result must be cast from `Int` to `ID`.

The `layoutIsTypeDependent` utility is basically only used here to assert that this cast can only happen between layout compatible types.

rdar://129004015
2024-06-21 17:28:33 +02:00
Erik Eckstein
9cb011322d SILOptimizer: add a utility to check if a generic nominal type's layout is dependent on its generic parameters
This is usually the case. Some examples, where they layout is _not_ dependent:
```
   struct S<T> {
     var x: Int // no members which depend on T
   }

   struct S<T> {
     var c: SomeClass<T> // a class reference does not depend on the layout of the class
   }
```
2024-06-21 17:28:33 +02:00
Michael Gottesman
43e1c5499f [sending] Make the operation of Builtin.createAsyncTask/friends a sending non-Sendable function instead of an @Sendable function.
This matches the interface of the public stdlib APIs that wrap these builtin calls.
2024-06-21 02:24:03 -07:00
Alejandro Alonso
8ed84e8aed Handle the mark_dependence chain when eliminating copies in ClosureLifetimeFixup 2024-06-20 14:44:11 -07:00
Tim Kientzle
1098054291 Merge branch 'main' into tbkka-assertions2 2024-06-18 17:52:00 -07:00
Michael Gottesman
e3de3da703 Merge pull request #74529 from gottesmm/pr-4f6b5f5b9db8d8885b36c19ba57b8de45e93cc80
[region-isolation] Check if we have a self param before attempting to get the self type.
2024-06-18 17:37:08 -07:00
Michael Gottesman
d032bc033a [region-isolation] Check if we have a self param before attempting to get the self type.
Otherwise in asserts builds we get an assert and in no-asserts we get a crash.

rdar://129975097
2024-06-18 13:39:20 -07:00
Akira Hatanaka
d92f181ace Create two versions (for caller and callee) of the functions that answer questions about parameter convention (#74124)
Create two versions of the following functions:

isConsumedParameter
isGuaranteedParameter
SILParameterInfo::isConsumed
SILParameterInfo::isGuaranteed
SILArgumentConvention::isOwnedConvention
SILArgumentConvention::isGuaranteedConvention

These changes will be needed when we add a new convention for
non-trivial C++ types as the functions will return different answers
depending on whether they are called for the caller or the callee. This
commit doesn't change any functionality.
2024-06-18 09:06:09 -07:00
Ellie Shin
9b53e20835 Merge pull request #74468 from apple/elsh/pcmo-conformance
[PackageCMO] Optimize witness thunks.
2024-06-17 19:47:33 -07:00
Kuba (Brecka) Mracek
4d52b58d44 Merge pull request #74480 from kubamracek/embedded-no-eager-specializer
[embedded] Skip EagerSpecializer on embedded Swift
2024-06-17 18:44:14 -07:00
Ellie Shin
534c35b08b [PackageCMO] Optimize witness thunks.
- Keep witness thunk linkage private for a package protocol member in SILGen.
- Optimize private/hidden functions during Package CMO; if they don't contain
references that have private/hidden symbols, serialize them and set the linkage
to shared. For unserialized witness thunks, set the linkage to package, so the
witness table itself can be serialized.
- Update witness table and vtable serialization.

Resolves rdar://129976582
2024-06-17 13:06:46 -07:00
Kuba Mracek
b0f3da65eb [embedded] Skip EagerSpecializer on embedded Swift 2024-06-17 08:58:46 -07:00
Andrew Trick
2f747ad607 Fix MoveOnlyWrappedTypeEliminator; handle dealloc_box
Speculatively fixes
rdar://129185450: Unhandled SIL Instruction: dealloc_box %3
2024-06-17 07:20:01 -07:00
eeckstein
22e75e077f Merge pull request #74421 from apple/fix-array-property-opt
ArrayPropertyOpt: update borrowed-from instructions
2024-06-14 21:00:09 +02:00
Erik Eckstein
35017dbe42 ArrayPropertyOpt: update borrowed-from instructions
Fixes a verifier crash when building with OSSA modules

rdar://129796888
2024-06-14 14:07:54 +02:00
Erik Eckstein
532535faad SIL: enable some SIL linkage related asserts in release builds
To catch wrong linkage bugs with a release-built compiler

related to rdar://129318806
2024-06-14 11:55:14 +02:00
Anton Korobeynikov
0f4dacd47b [AutoDiff] Fix custom derivative thunk for Optional (#74378)
Enable the nil coalescing operator (aka `??`) for Optional type.

Fixes #55882

Co-authored-by: Andrew Savonichev <andrew.savonichev@gmail.com>
2024-06-13 10:53:18 -07:00
Joe Groff
f564732804 Merge pull request #74374 from jckarter/dead-before-live-def-block-assertion-failure
MoveOnlyAddressChecker: Turn assertion into early exit.
2024-06-13 09:34:41 -07:00
nate-chandler
6694bbab90 Merge pull request #74360 from nate-chandler/rdar127518559
[ConsumeAddrChecker] Diagnose consumes of borrows.
2024-06-12 20:33:07 -07:00
Michael Gottesman
6d15e41a2f Merge pull request #74123 from gottesmm/pr-9e8378fdeee3204a34f48ea8d2ff8f0be40a4674
[region-isolation] Make store_borrow a store operation that does not require
2024-06-12 19:43:17 -07:00
Joe Groff
bb4c0d3d59 MoveOnlyAddressChecker: Turn assertion into early exit.
This condition can occur in practice if, while doing the walk back to find the liveness
reason for a consume-without-reinitialization of an `inout` binding through conditional
control flow, we visit a block that reinitializes the binding before any branch that
leaves the binding uninitialized. Fixes rdar://123604613.
2024-06-12 18:40:04 -07:00
Michael Gottesman
bd472b12be [region-isolation] Make store_borrow a store operation that does not require.
TLDR:

The reason why I am doing this is it ensures that temporary store_borrow that we
create when materializing a value before were treated as uses. So we would error
on this:

```swift
@MainActor func transferToMain<T>(_ t: T) async {}

func test() async {
  let x = NonSendableKlass()
  await transferToMain(x)
  await transferToMain(x)
}
```

----

store_borrow is an instruction intended to be used to initialize temporary
alloc_stack with borrows. Since it is a temporary, we do not want to error on
the temporaries initialization... instead, we want to error on the use of the
temporary parameter.

This is achieved by making it so that store_borrow still performs an
assign/merge, but does not require that src/dest be alive. So the regions still
merge (yielding diagnostics for later uses).

It also required me to make it so that PartitionOp::{Assign,Merge} do not
require by default. Instead, we want the individual operations to always emit a
PartitionOp::Require explicitly (which they already did).

One thing to be aware of is that when it comes to diagnostics, we already know
how to find a temporaries original value and how to handle that. So this is the
last part of making store_borrow behave nicely.

rdar://129237675
2024-06-12 15:01:38 -07:00
Slava Pestov
3889ede017 Merge pull request #74342 from slavapestov/flow-analysis-self-metatype-fix
SIL: Fix false positive in FlowIsolation with DynamicSelfType usage
2024-06-12 17:00:31 -04:00
Nate Chandler
cc40e29f2c [ConsumeAddrChecker] Diagnose consumes of borrows.
When checking a `mark_unresolved_move_addr` instruction, first check
whether it is legal to consume the source.

rdar://127518559&125817827
2024-06-12 13:13:45 -07:00
eeckstein
b7b93a12a0 Merge pull request #74329 from eeckstein/fix-block-cloning
SIL: update borrowed-from instructions when cloning a basic block
2024-06-12 20:38:13 +02:00
Slava Pestov
c270597fd0 SIL: Fix false positive in FlowIsolation with DynamicSelfType usage
If an instruction references the DynamicSelfType by calling a
static member with `Self.foo()`, we consider this a type-dependent
use of `self`. This means that at runtime we may need to load the
isa pointer, but we don't need to touch any other protected state
from the instance.

Therefore, we can skip type-dependent uses in the analysis to
avoid false positives in this case.

An existing test case already exercised the overly-conservative
behavior, so I just updated it to not expect an error.

Fixes rdar://129676769.
2024-06-12 12:01:18 -04:00
nate-chandler
3aca85bfb3 Merge pull request #74298 from nate-chandler/rdar129593468
[NoncopyableWrapperElim] Process undef values.
2024-06-12 06:55:48 -07:00
Erik Eckstein
d80813ee3b SIL: update borrowed-from instructions when cloning a basic block
Cloning blocks might split CFG edges which can "convert" terminator result arguments to phi-arguments.
In this case a borrowed-from instruction must be inserted.

Fixes a SIL verifier crash caused by SimplifyCFG's jump threading.
rdar://129187525
2024-06-12 12:56:34 +02:00
Michael Gottesman
88d420f561 Merge pull request #74321 from gottesmm/pr-5b09ff36784666ef5fde4abfcfbe138bf6135412
[region-isolation] Dont crash when processing global actor isolated init accessors.
2024-06-12 01:39:22 -07:00
Michael Gottesman
f035590784 [region-isolation] Dont crash when processing global actor isolated init accessors.
This just means that I stopped treating it like an actor instance isolated
thing. This was fun to track down since ActorIsolation has a union in it that
was being misinterpreted, leading to memory corruption... my favorite! = ).

rdar://129256560
2024-06-11 22:15:37 -07:00
Nate Chandler
5d63fcfef6 [MOWTE] Visit undef values.
Such values' types may also also be move-only wrapped.
2024-06-11 17:02:36 -07:00
Nate Chandler
69fa1ae2d2 [MOWTE] Start madeChange at false. 2024-06-11 17:02:24 -07:00
Nate Chandler
d0341ddc7d [NFC] MOWTE: Deleted dead array.
The modified arguments were recorded but never used.
2024-06-11 17:02:20 -07:00
Nate Chandler
8883e88f54 [NFC] MOWTE: Deduplicated code.
Replaced copy-pasted code with a twice-invoked closure.  In preparation
to add a third invocation.
2024-06-11 17:02:19 -07:00
Nate Chandler
78c18374c3 [NFC] SIL: Extracted has "any" move-only wrapping.
This predicate is used in several places.
2024-06-11 16:25:14 -07:00
Nate Chandler
ac5ae3cc2a [ClosureSpecializer] Bail on noncopyable argument.
The optimization currently always creates a retain of the argument.
This isn't legal for noncopyable values.

rdar://129622373
2024-06-11 14:57:23 -07:00
Nate Chandler
55eb29ad3a [Gardening] Detypo'd. 2024-06-11 14:46:03 -07:00
Nate Chandler
38db63c05e [NFC] ClosureSpecializer: Use llvm::enumerate.
Rather than manually tracking the index.
2024-06-11 14:46:03 -07:00
Max Desiatov
12f402f715 Revert "[AutoDiff] Fix custom derivative thunk for Optional (#71721)" (#74268)
This reverts commit aa5dddb952.

Fixes `preset=buildbot,tools=RA,stdlib=DA` CI job, which without this revert fails on `AutoDiff/SILGen/nil_coalescing.swift` test.
2024-06-11 09:24:19 +01:00
nate-chandler
0351ecd6fb Merge pull request #74196 from nate-chandler/lifetime-completion/20240606/1
[LifetimeCompletion] Removed "with leaks" mode.
2024-06-10 11:32:44 -07:00
Doug Gregor
25830d6bc3 Merge pull request #74225 from DougGregor/flow-sensitive-actor-init-isolation
Teach `#isolation` to respect the flow-sensitive nature of actor initializers
2024-06-10 01:52:38 -07:00
Doug Gregor
d1ae73f436 Handle flow-sensitive #isolation in distributed actor initializers.
Distributed actors can be treated as actors by accessing the `asLocalActor`
property. When lowering `#isolation` in a distributed actor initializer,
use a separate builtin `flowSensitiveDistributedSelfIsolation` to
capture the conformance to `DistributedActor`, and have Definite
Initialization introduce the call to the `asLocalActor` getter when
needed.
2024-06-09 22:48:43 -07:00
Doug Gregor
956243cd7e Teach #isolation to respect the flow-sensitive nature of actor initializers
Actor initializers have a flow-sensitive property where they are isolated
to the actor being initialized only after the actor instance itself is
fully-initialized. However, this behavior was not being reflected in
the expansion of `#isolation`, which was always expanding to `self`,
even before `self` is fully formed.

This led to a source compatibility issue with code that used the async
for..in loop within an actor initializer *prior* to the point where the
actor was fully initialized, because the type checker is introducing
the `#isolation` (SE-0421) but Definite Initialization properly rejects
the use of `self` before it is initialized.

Address this issue by delaying the expansion of `#isolation` until
after the actor is fully initialized. In SILGen, we introduce a new
builtin for this case (and *just* this case) called
`flowSensitiveSelfIsolation`, which takes in `self` as its argument
and produces an `(any Actor)?`. Definite initialization does not treat
this as a use of `self`. Rather, it tracks these builtins and
replaces them either with `self` (if it is fully-initialized at this
point) or `nil` (if it is not fully-initialized at this point),
mirroring the flow-sensitive isolation semantics described in SE-0327.

Fixes rdar://127080037.
2024-06-07 14:54:20 -07:00
Joe Groff
6210c2e9d2 Merge pull request #74187 from jckarter/moveonly-wrapped-type-eliminator-missing-case
Add missing OpenExistentialAddr no-changes-needed case to MoveOnlyWrappedTypeEliminator.
2024-06-07 07:57:09 -07:00
nate-chandler
033fc2354f Merge pull request #74157 from nate-chandler/rdar128710064
[SILGen] Store_borrow into in_guaranteed.
2024-06-07 06:47:16 -07:00
Nate Chandler
5dc3cf1296 [LifetimeCompletion] Removed "with leaks" mode.
Now that the two known issues which resulted in invalid SIL being
provided to lifetime completion have been addressed, tighten up the
completion done on the availability boundary not to allow leaks.
2024-06-06 16:48:46 -07:00
Joe Groff
49aac80da8 Add missing OpenExistentialAddr no-changes-needed case to MoveOnlyWrappedTypeEliminator.
Fixes rdar://126875325.
2024-06-06 13:06:24 -07:00
Nate Chandler
000a33f04f [MoveOnlyAddressChecker] Don't destroy borrows.
If a marked location is used by `store_borrow`s, then all its users are
`store_borrow`s or `dealloc_stack`s, so there's nothing to destroy.
2024-06-06 11:20:16 -07:00