Commit Graph

11224 Commits

Author SHA1 Message Date
Meghana Gupta
ab3eaf2441 Merge pull request #71881 from meg-gupta/fixmem2reg
Fix mem2reg of lexical enum stack locations
2024-06-30 05:31:24 -07:00
Nate Chandler
7edf36ff6a [Mem2Reg] Cleanup and refactoring. 2024-06-29 14:49:06 -07:00
Meghana Gupta
5282194682 Fix isTriviallyDuplicatable to handle open_pack_element
SILInstruction::clone doesn't know how to clone instructions that produce
the archetype uuid. SILCloner is equipped to handle such instructions.

Optimizations like LoopRotate use  SILInstruction::clone and will be
incorrect for such instructions.

rdar://130047619
2024-06-29 13:42:05 -07:00
Nate Chandler
451a814363 [MoveOnlyAddressChecker] Complete block args.
Now that the underlying issue (PrunedLiveness' merging of summaries for
branch instructions) has been fixed, reinstate lifetime completion and
add a test to verify that it behaves correctly.

This reverts commit c552b90b61
("Temporarily turn off completing lifetimes of block arguments").

rdar://130427564
2024-06-28 15:07:31 -07:00
Ben Barham
d8f381e660 Merge pull request #74804 from bnbarham/rename-equals
Update `StringRef::equals` references to `operator==`
2024-06-28 11:22:20 -07:00
Meghana Gupta
b1fc313f22 Merge pull request #74802 from meg-gupta/temprvalueedgecase
Allow TempRValueOpt of lexical alloc_stacks when base of copy source is @in_guaranteed
2024-06-27 22:00:05 -07:00
Ben Barham
d72f5b12c4 Update StringRef::equals references to operator==
`equals` has been deprecated upstream, use `operator==` instead.
2024-06-27 19:14:06 -07:00
Meghana Gupta
e4566c84b0 Merge pull request #74768 from meg-gupta/clffix
Fixes to ClosureLifetimeFixup's copy elimination for noncopyable values
2024-06-27 18:52:08 -07:00
Meghana Gupta
42c1f9cb33 Fix mem2reg of lexical enum stack locations
When we have a non trivial enum lexical stack location, and we store a
 none value, after mem2reg we can end up with an ownership error
because of the leaking move_value [lexical].

This change avoids creating move_value [lexical] for such stores.

Do the same when we have store_borrow of none values, even though there
is no ownership error in this case, handle it like store for consistency
2024-06-27 17:34:56 -07:00
Meghana Gupta
32e72a6732 Allow TempRValueOpt of lexical alloc_stacks when base of copy source has guaranteed convention
A guaranteed function argument is live for the duration of the function.
It should be safe to allow TempRValueOpt when it is the base of the copy source of a lexical alloc_stack.
2024-06-27 16:18:15 -07:00
Meghana Gupta
e71cd4b005 Improve copy elimination of noncopyable captures in ClosureLifetimeFixup
For a mark_dependence user of the stack, look through the mark_dependence chain
to match the partial_apply.

Since this is a superset of the `markDepChain` logic, remove that.
So that we don't have 2 ways to check for the same.
2024-06-27 10:34:29 -07:00
Akira Hatanaka
42bc49d3fe Add a new parameter convention @in_cxx for non-trivial C++ classes that are passed indirectly and destructed by the caller (#73019)
This corresponds to the parameter-passing convention of the Itanium C++
ABI, in which the argument is passed indirectly and possibly modified,
but not destroyed, by the callee.

@in_cxx is handled the same way as @in in callers and @in_guaranteed in
callees. OwnershipModelEliminator emits the call to destroy_addr that is
needed to destroy the argument in the caller.

rdar://122707697
2024-06-27 09:44:04 -07:00
Meghana Gupta
960619b0a0 Fix ClosureLifetimeFixup's copy elimination for noncopyable values
On finding unrecognized user, reset `initialization` of the stack so that we
don't inadvertently eliminate copy
2024-06-27 02:55:29 -07:00
Erik Eckstein
655494495e use the new ASSERT macro for two asserts which should also fire in a release-built compiler 2024-06-27 09:49:29 +02:00
Erik Eckstein
c576015d8a fix a crash when de-virtualizing class or actor methods with typed throws
The de-virtualizer utility didn't handle indirect error results when de-virtualizing class or actor methods.
This resulted in a missing argument for the indirect error result in the new try_apply instruction.

rdar://130545338
2024-06-27 09:47:33 +02:00
Michael Gottesman
f0fff2e5a0 [region-isolation] Treat sendable return values as Sendable when the returning function has a known actor isolation.
rdar://130544081
2024-06-26 16:31:45 -07:00
Ellie Shin
116abbb1dc Merge pull request #74641 from swiftlang/elsh/cmo-ufi 2024-06-26 09:06:39 -07:00
Joe Groff
636a19d11b Merge pull request #74707 from jckarter/consume-during-borrow-checks
MoveOnlyAddressChecker: More robust checking for consume-during-borrow.
2024-06-26 08:22:04 -07:00
eeckstein
31c07c95b0 Merge pull request #74689 from eeckstein/refactor-dynamic-self-check
SwiftCompilerSources: refactor `Function.mayBindDynamicSelf`
2024-06-26 08:47:18 +02:00
eeckstein
b68c55cd90 Merge pull request #74685 from eeckstein/assertion-improvements
Some assertion improvements
2024-06-26 08:45:50 +02:00
Michael Gottesman
2b5fa0e847 Merge pull request #74705 from gottesmm/pr-93a2750af4fb0c65ca129d545c4af30cd9e718f9
[region-isolation] Given a .none #isolation parameter, infer the isolation from the callee rather than the isolated parameter.
2024-06-25 19:38:19 -07:00
Ellie Shin
1e5266e7b6 [PackageCMO] Don't allow modifying AST.
Currently not all types are visited in canSerialize* calls, sometimes
 resulting in an internal type getting @usableFromInline, which is
 incorrect.

 For example, for `let q = P() as? Q`, where Q is an internal class
 inherting a public class P, Q is not visited in the canSerialize*
 checks, thus resulting in `@usableFromInline class Q`; this is not
 the intended behavior in the conservative mode used by PackageCMO
 as it modifies AST.

 To properly fix, instruction visitor needs to be refactored to do
 both the "canSerialize" check (that visits all types) and serialize
 or update visibility (modify AST in non-conservative modes).

 This PR provides a short-term fix that prevents modifying AST, and
 also ensures that the generated interfaces with PackageCMO flags
 are not affected by the optimization or contain modified AST.

rdar://130292190
2024-06-25 18:15:29 -07:00
Alejandro Alonso
cb2a766470 Handle existential_metatype in moveonlytype eliminator 2024-06-25 14:16:53 -07:00
Michael Gottesman
f43911b30f [region-isolation] Given a .none #isolation parameter, infer the isolation from the callee rather than the isolated parameter.
Otherwise, we will have differing isolation from other parameters since
the isolations will look different since one will have the .none value
as an instance and the other will not have one and instead will rely on
the AST isolation info. That is the correct behavior here since we do
not actually have an actor here.

I also removed some undefined behavior in the merging code. The way the
code should work is that we should check if the merge fails and in such
a case emit an unknown pattern error... instead of not checking
appropriately on the next iteration and hitting undefined behavior.

rdar://130396399
2024-06-25 14:12:15 -07:00
Joe Groff
27a8852290 MoveOnlyAddressChecker: More robust checking for consume-during-borrow.
- While an opaque borrow access occurs to part of a value, the entire scope of
  the access needs to be treated as a liveness range, so add the `EndAccess`es
  to the liveness range.
- The SIL verifier may crash the compiler on SILGen-generated code when the
  developer's source contains consume-during-borrow code patterns. Allow
  `load_borrow` instructions to be marked `[unchecked]`, which suppresses
  verifier checks until the move checker runs and gets a chance to properly
  diagnose these errors.

Fixes rdar://124360175.
2024-06-25 14:10:02 -07:00
Erik Eckstein
c61733f985 SwiftCompilerSources: refactor Function.mayBindDynamicSelf
Instead of bridging the whole function, just bridge `hasDynamicSelfMetadata` and do the other work in swift.
2024-06-25 17:59:23 +02:00
Erik Eckstein
718ea4b018 replace require with the new ASSERT macro 2024-06-25 10:45:55 +02:00
Michael Gottesman
dcd348fa5f Merge pull request #74673 from gottesmm/pr-820f4317d3273ec87c02ce9d16e1dc14a759dd40
[transfer-non-sendable] Teach SILIsolationInfo how to handle "look through instructions" when finding actor instances.
2024-06-24 21:48:27 -07:00
Michael Gottesman
c01f551ebe [transfer-non-sendable] Teach SILIsolationInfo how to handle "look through instructions" when finding actor instances.
From the perspective of the IR, we are changing SILIsolationInfo such that
inferring an actor instance means looking at equivalence classes of values where
we consider operands to look through instructions to be equivalent to their dest
value. The result is that cases where the IR maybe puts in a copy_value or the
like, we consider the copy_value to have the same isolation info as using the
actor directly. This prevents a class of crashes due to merge failings. Example:

```swift
actor MyActor {
  init() async {

  init(ns: NonSendableKlass) async {
    self.k = NonSendableKlass()
    self.helper(ns)
  }

  func helper(_ newK: NonSendableKlass) {}
}
```

Incidently, we already had a failing test case from this behavior rather than
the one that was the original genesis. Specifically:

1. If a function's SILIsolationInfo is the same as the isolation info of a
SILValue, we assume that no transfer actually occurs.

2. Since we were taking too static of a view of actor instances when comparing,
we would think that a SILIsolationInfo of a #isolation parameter to as an
argument would be different than the ambient's function isolation which is also
that same one. So we would emit a transfer non transferrable error if we pass in
any parameters of the ambient function into another isolated function. Example:

```swift
actor Test {

  @TaskLocal static var local: Int?

  func withTaskLocal(isolation: isolated (any Actor)? = #isolation,
                     _ body: (consuming NonSendableValue, isolated (any Actor)?) -> Void) async {
    Self.$local.withValue(12) {

      // We used to get these errors here since we thought that body's isolation
      // was different than the body's isolation.
      //
      //  warning: sending 'body' risks causing data races
      //  note: actor-isolated 'body' is captured by a actor-isolated closure...
      body(NonSendableValue(), isolation)
    }
  }
}
```

rdar://129400019
2024-06-24 18:16:11 -07:00
Michael Gottesman
baec06ed23 [variable-name-inference] Add support for handling EndInitLetRefInst. 2024-06-24 18:16:11 -07:00
Meghana Gupta
c552b90b61 Temporarily turn off completing lifetimes of block arguments 2024-06-24 08:14:18 -07:00
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