Commit Graph

11193 Commits

Author SHA1 Message Date
Ellie Shin
6b5df5225c Merge pull request #76832 from swiftlang/elsh/pcmo-refactor
[PackageCMO] Use InstructionVisitor to check inst serializability.
2024-10-16 12:00:33 -07:00
swift-ci
0462851544 Merge remote-tracking branch 'origin/main' into rebranch 2024-10-16 09:56:16 -07:00
nate-chandler
b5e3f42cad Merge pull request #77044 from nate-chandler/rdar137960229
[DiagnoseUnreachable] Ignore dead_end destroys.
2024-10-16 09:41:48 -07:00
Nate Chandler
ce368e8220 [DiagnoseUnreachable] Ignore dead_end destroys.
Such destroys mark the lifetime end of their operands along their
availability boundary.  They are currently inserted in this test case
by the ClosureLifetimeFixup pass, but in the fullness of time they will
be present for every value which is not explicitly destroyed (that's
what complete OSSA lifetimes is mostly about).

Currently, such destroys are diagnosed by DiagnoseUnreachable.  Fix the
diagnostic pass not to diagnose these valid instructions.

rdar://137960229
2024-10-15 21:00:16 -07:00
Andrew Trick
af86ad1e16 Test predictable memopt mark_dependence semantics.
Unit tests for mark_dependence support. Required to fix unsafeAddress.
2024-10-15 16:52:07 -07:00
Andrew Trick
19c1617059 PredictableMemOpts: handle mark_dependence source promotion 2024-10-15 16:48:36 -07:00
elsh
7b358c25f4 Override getMappedValue with operand type checks. 2024-10-15 15:29:52 -07:00
Andrew Trick
2e9daa444d PredictableMemOpts: handle MarkDependence base uses. 2024-10-15 11:18:43 -07:00
Andrew Trick
830a3664f1 Rename overloaded computeAvailableValues.
Overloaded names create confusion.
2024-10-15 11:18:43 -07:00
swift-ci
01e1e21703 Merge remote-tracking branch 'origin/main' into rebranch 2024-10-15 08:54:47 -07:00
Erik Eckstein
709dfc2d21 MandatoryPerformanceOptimization: don't let not-inlinable functions to be inlined
Also refactor canInline.

Fixes a compiler crash.
rdar://137544788
2024-10-15 12:19:50 +02:00
swift-ci
075ad5bcad Merge remote-tracking branch 'origin/main' into rebranch 2024-10-14 19:27:07 -07:00
Andrew Trick
e767d7ec26 Merge pull request #76996 from atrick/predmem_refactor
[NFC] PredictableMemOpts: refactor to allow extension
2024-10-14 19:07:07 -07:00
elsh
f87c5ba69a [Package CMO] Use InstructionVisitor to check if inst can be serialized.
Currently, InstructionVisitor is only used during the serialization pass,
that comes after a pass that checks if instructions are serializable. This
causes inconsistencies as some types bypass the first pass but are processed
in the second, leading to assert fails.

This PR uses InstructionVisitor in the first pass to be exhaustive and to
be consistent with the coverage in the second pass. The downside is the visitor
is SILCloner, which introduces overhead of cloning and discarding insts per pass
-- a potential area for future refactoring.

Resolves rdar://130788545.
2024-10-14 17:33:55 -07:00
swift-ci
8cce69d0ad Merge remote-tracking branch 'origin/main' into rebranch 2024-10-14 11:35:01 -07:00
Erik Eckstein
5c8fe55449 embedded: fix several issues with vtable specialization
* fix a false error if a derived class has different generic parameters than its base class
* fix a similar problem if a non-generic class derives from a generic class
* fix a compiler crash for calling a class method on a class metatype

rdar://137692055
2024-10-14 14:43:11 +02:00
Erik Eckstein
e0533e6125 SIL: add an API to replace all entries of a VTable
* add `ModulePassContext.replaceVTableEntries()`
* add `ModulePassContext.notifyFunctionTablesChanged()`
2024-10-14 14:43:11 +02:00
Andrew Trick
1ceebec1a0 [NFC] move a block of code into the correct heading.
No source change here, only reorganization that was deferred to avoid
making diffs hard to read.
2024-10-13 16:36:20 -07:00
Andrew Trick
2fd9e5e196 PredictableMemOpt: remove non-OSSA support
PredictableMemOpt only runs with OSSA. The recent refactoring did not
preserve non-OSSA conditions because it adds complexity. This commit
just makes it official.

In the near future, this pass will be replaced by an OSSA-only pass
anyway, so this brings us one small step closer to that.
2024-10-12 23:16:18 -07:00
Andrew Trick
60cb13e164 [NFC] PredictableMemOpts: refactor to allow extension
Fully encapsulate ownership fixup so we can extend it.
2024-10-12 23:16:12 -07:00
swift-ci
8a2edbc4c5 Merge remote-tracking branch 'origin/main' into rebranch 2024-10-11 17:05:06 -07:00
Michael Gottesman
3050916efc Merge pull request #76982 from gottesmm/pr-635c4050850f061c438b2dc1c396806e8754666c
When printing logging, dump out the whole function.
2024-10-11 16:56:02 -07:00
nate-chandler
249831f9f3 Merge pull request #76942 from nate-chandler/general-coro/20240927/1
[CoroutineAccessors] SIL represents callee alloc.
2024-10-11 16:39:53 -07:00
Michael Gottesman
9a6090fc43 When printing logging, dump out the whole function.
Just to make it a little quicker to debug/get this information when debugging
the pass. I have been wanting this and just hadn't gotten around to adding it.
It just centralizes the last piece of information that one wants to reach for
when debugging.
2024-10-11 13:19:05 -07:00
Nate Chandler
71239d6357 [CoroutineAccessors] SIL represents callee alloc.
When its operand has coroutine kind `yield_once_2`, a `begin_apply`
instruction produces an additional value representing the storage
allocated by the callee.  This storage must be deallocated by a
`dealloc_stack` on every path out of the function.  Like any other stack
allocation, it must obey stack discipline.
2024-10-11 08:25:03 -07:00
swift-ci
cb83584ebd Merge remote-tracking branch 'origin/main' into rebranch 2024-10-11 06:34:12 -07:00
Erik Eckstein
eed8645610 Optimizer: improve the load-copy-to-borrow optimization and implement it in swift
The optimization replaces a `load [copy]` with a `load_borrow` if possible.

```
  %1 = load [copy] %0
  // no writes to %0
  destroy_value %1
```
->
```
  %1 = load_borrow %0
  // no writes to %0
  end_borrow %1
```

The new implementation uses alias-analysis (instead of a simple def-use walk), which is much more powerful.

rdar://115315849
2024-10-11 09:41:37 +02:00
Erik Eckstein
52deb58251 Optimizer: add the FunctionPassContext.completeLifetime(of: Value) utility
Implemented by bridging the OSSALifetimeCompletion utility
2024-10-11 09:41:37 +02:00
swift-ci
25f0382ca5 Merge remote-tracking branch 'origin/main' into rebranch 2024-10-09 18:07:23 -07:00
Michael Gottesman
131ab89f49 Merge pull request #76920 from gottesmm/pr-4ab85a74d0641a99b3a480971f93b2f98f726e31
[region-isolation] Add a special error for when a closure captures a non-Sendable box.
2024-10-09 17:36:29 -07:00
Michael Gottesman
ce7a0db192 [region-isolation] Add a special error for when a closure captures a non-Sendable box.
The reason that I am modifying this error is that in situations like the
following one can have a Sendable type that triggers this error since the box
containing the value is non-Sendable.

```
func methodConsuming(x: consuming SendableKlass) async throws {
  try await withThrowingTaskGroup(of: Void.self) { group in
    group.addTask { // expected-tns-warning {{passing closure as a 'sending' parameter risks causing data races between code in the current task and concurrent execution of the closure}}
      useValue(x) // expected-tns-note {{closure captures reference to mutable var 'x' which is accessible to code in the current task}}
    }

    try await group.waitForAll()
  }
}
```

rdar://133813644
(cherry picked from commit 36c2b3cc1330c07dcf9715f8ae88d31f9dba58c4)
2024-10-09 11:14:28 -07:00
swift-ci
c2fff1cc0f Merge remote-tracking branch 'origin/main' into rebranch 2024-10-09 03:14:46 -07:00
Erik Eckstein
e50a93a34c Generic specializer: don't use pre-specialization for Array._endMutation
Pre-specialization of `Array._endMutation` (for AnyObject) prevents inlining this function and that results in sub-optimal code.
This function is basically a no-op. So it should be inlined.

Unfortunately we cannot remove the specialize-attributes anymore because the pre-specialized function(s) are now part of the stdlib's ABI.
Therefore make an exception for `Array._endMutation` in the generic specializer.
2024-10-08 17:37:17 +02:00
Erik Eckstein
c97502374b Optimizer: add constant folding of classify_bridge_object
Constant fold `classify_bridge_object` to `(false, false)` if the operand is known to be a swift class.
2024-10-08 16:24:46 +02:00
swift-ci
8d3265a328 Merge remote-tracking branch 'origin/main' into rebranch 2024-10-07 11:34:54 -07:00
Erik Eckstein
c05234e677 MandatoryPerformanceOptimizations: specialize witness_method instructions
In Embedded Swift, witness method lookup is done from specialized witness tables.
For this to work, the type of witness_method must be specialized as well.
Otherwise the method call would be done with wrong parameter conventions (indirect instead of direct).
2024-10-07 09:00:31 +02:00
Erik Eckstein
792dee2f5f SIL: support specialized witness_method instructions
In Embedded Swift, witness method lookup is done from specialized witness tables.
For this to work, the type of witness_method must be specialized as well.
Otherwise the method call would be done with wrong parameter conventions (indirect instead of direct).
2024-10-07 09:00:31 +02:00
swift-ci
cd3016368e Merge remote-tracking branch 'origin/main' into rebranch 2024-10-05 17:14:49 -07:00
John McCall
ddcceb3d43 Merge pull request #76880 from rjmccall/actor-delegating-init-isolation
Hop to the current isolation properly in delegating async actor initializers
2024-10-05 20:10:31 -04:00
John McCall
af8115ffa3 Hop to the current isolation properly in delegating async actor initializers.
This requires two major changes.

The first is that we need to teach SILGen that the isolation of an initializer
is essentially dynamic (as far as SILGen is concerned) --- that it needs to emit
code in order to get the isolation reference.  To make this work, I needed to
refactor how we store the expected executor of a function so that it's not
always a constant value; instead, we'll need to emit code that DI will lower
properly.  Fortunately, I can largely build on top of the work that Doug previously
did to support #isolation in these functions.  The SIL we emit here around delegating
initializer calls is not ideal --- the breadcrumb hop ends up jumping to the
generic executor, and then DI actually emits the hop to the actor.  This is a little
silly, but it's hard to eliminate without special-casing the self-rebinding, which
honestly we should consider rather than the weirdly global handling of that in
SILGen today.  The optimizer should eliminate this hop pretty reliably, at least.

The second is that we need to teach DI to handle the pattern of code we get in
delegating initializers, where the builtin actually has to be passed the self var
rather than a class reference.  This is because we don't *have* a class reference
that's consistently correct in these cases.  This ended up being a fairly
straightforward generalization.

I also taught the hop_to_executor optimizer to skip over the initialization of
the default-actor header; there are a lot of simple cases where we still do emit
the prologue generic-executor hop, but at least the most trivial case is handled.
To do this better, we'd need to teach this bit of the optimizer that the properties
of self can be stored to in an initializer prior to the object having escaped, and
we don't have that information easily at hand, I think.

Fixes rdar://87485045.
2024-10-04 22:23:00 -04:00
swift-ci
16fbc330a3 Merge remote-tracking branch 'origin/main' into rebranch 2024-10-04 03:15:55 -07:00
Andrew Trick
415d7ab7e2 [NFC] Fix a future PredictableMemOpt bug introduced by the previous commit.
This fixes a logic error that will only be exposed by the additional
functionality that motivated the cleanup. When the additional
functionality is checked in, it will contain a unit test for this fix.

This bug was introduced in:

commit 755a146730 (public/predmem_rewrite)
Author: Andrew Trick <atrick@apple.com>
Date:   Fri Sep 27 22:18:29 2024

    [NFC] rewrite PredictableMemOpts dead allocation elimination
2024-10-03 21:37:34 -07:00
swift-ci
b8a5d2d6d7 Merge remote-tracking branch 'origin/main' into rebranch 2024-10-03 01:35:42 -07:00
Konrad `ktoso` Malawski
dea3b59331 Merge pull request #76250 from ktoso/wip-experimental-isolated-deinit 2024-10-03 17:19:10 +09:00
swift-ci
23d53573f5 Merge remote-tracking branch 'origin/main' into rebranch 2024-10-02 19:30:23 -07:00
Michael Gottesman
f985b0ee03 [thunk-lowering] Add a pass that performs lowering of ThunkInsts.
Right now it just handles the "identity" case so we can validate the
functionality.
2024-10-02 14:15:49 -07:00
Michael Gottesman
561662d6cc [sil] Add a new instruction called ThunkInst.
For now this will only be used for HopToMainActorIfNeeded thunks. I am creating
this now since in the past there has only been one option for creating
thunks... to create the thunk in SILGen using SILGenThunk. This code is hard to
test and there is a lot of it. By using an instruction here we get a few benefits:

1. We decouple SILGen from needing to generate new kinds of thunks. This means
that SILGenThunk does not need to expand to handle more thunks.

2. All thunks implemented via ThunkInst will be easy to test in a decoupled way
with SIL tests.

3. Even though this stabilizes the patient, we still have many thunks in SILGen
and various parts of the compiler. Over time, we can swap to this model,
allowing us to hopefully eventually delete SILGenThunk.
2024-10-02 14:15:49 -07:00
swift-ci
7cc6111f96 Merge remote-tracking branch 'origin/main' into rebranch 2024-10-02 01:53:55 -07:00
Erik Eckstein
5502373018 SIL: rename AssociatedTypeProtocolWitness -> AssociatedConformanceWitness in SILWitnessTable
To be consistent with the naming convention in the AST
2024-10-02 07:10:30 +02:00
Erik Eckstein
10782cf42b SwiftCompilerSources: introduce the AST module
As the optimizer uses more and more AST stuff, it's now time to create an "AST" module.
Initially it defines following AST datastructures:
* declarations: `Decl` + derived classes
* `Conformance`
* `SubstitutionMap`
* `Type` and `CanonicalType`

Some of those were already defined in the SIL module and are now moved to the AST module.
This change also cleans up a few things:
* proper definition of `NominalTypeDecl`-related APIs in `SIL.Type`
* rename `ProtocolConformance` to `Conformance`
* use `AST.Type`/`AST.CanonicalType` instead of `BridgedASTType` in SIL and the Optimizer
2024-10-02 07:10:29 +02:00