Commit Graph

965 Commits

Author SHA1 Message Date
Michael Gottesman
13d01394d4 [region-isolation] Change the region -> transferring operand map to use a MapVector instead of a DenseMap.
This ensures that we can efficiently iterate over the map which we will need to
do for equality queries.

I am going to add the equality queries in a subsequent commit. Just chopping off
a larger commit.
2024-04-09 10:56:19 -07:00
Michael Gottesman
cc1a873b9e Fix another mocking issue with PartitionUtils unittest. 2024-04-06 01:39:00 -07:00
Michael Gottesman
6844b995a4 [region-isolation] Wire up history diagnostics through the checker, but do not use it to emit diagnostics yet.
Specifically:

1. I copy the history that we have been tracking from the transferring operand
value at the transfer point. This is then available for use to emit diagnostics.

2. I added the ability for SILIsolationInfo to not only track the ActorIsolation
of an actor isolated value, but also if we have a value, we can track that as
well. Since we now track a value for task isolated and actor isolated
SILIsolationInfo, I just renamed the field to isolatedValue and moved it out of
the enum.

In a subsequent commit, I am going to wire it up to a few diagnostics.

rdar://123479934
2024-04-06 00:58:10 -07:00
Michael Gottesman
d6b4f16382 [region-isolation] Add a SILLocation to SequenceBoundary in IsolationHistory.
We package all isolation history nodes from a single instruction by placing a
sequence boundary at the bottom. When ever we pop, we actually pop a PartitionOp
at a time meaning that we pop until we see a SequenceBoundary. Thus the sequence
boundary will always be the last element visited when popping meaning that it is
a convenient place to stick the SILLocation associated with the entire
PartitionOp. As a benefit, there was some unused space in IsolationHistory::Node
for that case since we were not using the std::variant field at all.
2024-04-06 00:58:10 -07:00
Michael Gottesman
aee5a37d9d [region-isolation] Add isolation history support but do not wire it up to the checker.
This means that I added an IsolationHistory field to Partition. Just upstreaming
the beginning part of this work. I added some unittests to exercise the code as
well. NOTE: This means that I did need to begin tracking an
IsolationHistoryFactory and propagating IsolationHistory in the pass
itself... but we do not use it for anything.

A quick overview of the design.

IsolationHistory is the head of an immutable directed acyclic graph. It is
actually represented as an immutable linked list with a special node that ties
in extra children nodes. The users of the information are expected to get a
SmallVectorImpl and process those sibling nodes afterwards. The reason why we
use an immutable approach is that it fits well with the problem and saves space
since different partitions could be pointing at the same linked list
node. Operations occur on an isolation history by pushing/popping nodes. It is
assumed that the user will push nodes in batches with a sequence boundary at the
bottom of the addition which signals to stop processing nodes.

Tieing this together, each Partition within it contains an IsolationHistory. As
the PartitionOpEvaluator applies PartitionOps to Partition in
PartitionOpEvaluator::apply, the evaluator also updates the isolation history in
the partition by first pushing a SequenceBoundary node and then pushing nodes
that will undo the operation that it is performing. This information is used by
the method Partition::popHistory. This pops linked list nodes from its history,
performing the operation in reverse until it hits a SequenceBoundary node.

This allows for one to rewind Partition history. And if one stashes an isolation
history as a target, one can even unwind a partition to what its state was at a
specific transfer point or earlier. Once we are at that point, we can begin
going one node back at a time and see when two values that we are searching for
no longer are apart of the same region. That is a place where we want to emit a
diagnostic. We then process until we find for both of our values history points
where they were the immediate reason why the two regions merge.

rdar://123479934
2024-04-06 00:58:05 -07:00
Michael Gottesman
c2350dfe7e [region-isolation] Change SILIsolationInfo to only traffic in ActorIsolation instead of that or nominal type decls.
This should be NFC since the only case where I used this was with self... and I
found another way of doing that using the API I added in the previous commit.
2024-04-04 10:58:57 -07:00
Michael Gottesman
1cf4e99454 Propagate global actor-ness of functions/closures.
I fixed a bunch of small issues around here that resulted in a bunch of radars
being fixed. Specifically:

1. I made it so that we treat function_refs that are from an actor isolated
function as actor isolated instead of sendable.

2. I made it so that autoclosures which return global actor isolated functions
are treated as producing a global actor isolated function.

3. I made it so that we properly handle SILGen code patterns produced by
Sendable GlobalActor isolated things.

rdar://125452372
rdar://121954871
rdar://121955895
rdar://122692698
2024-03-29 14:39:34 -07:00
Michael Gottesman
77dccacbd3 Make ActorIsolation on SILFunction non-optional.
ActorIsolation already has a "I have no value case": unspecified. Lets just use
that.

Just a mistake I made that I am trying to fix before anything further depends on
this code.
2024-03-29 14:39:26 -07:00
Michael Gottesman
6f66849610 [region-isolation] Do not squelch errors in the unittests.
To squelch errors, we need access to functionality not available in the
unittests. The unittests do not require this functionality anyways, so just
disable squelching during the unittests.
2024-03-26 10:06:21 -07:00
Michael Gottesman
e36aab6301 [region-isolation] Squelch use after transfer if the use has effectively the same isolation as the transfer inst.
To be more specific this means that either:

1. The use is actually isolated to the same actor. This could mean that the
use is global actor isolated to the same function.

2. The use is nonisolated but is executing within a function that is globally
isolated to the same isolation domain.

rdar://123474616
2024-03-25 23:32:17 -07:00
Michael Gottesman
8243b5cb74 [region-isolation] If a value is dynamically actor isolated, do not consider it transferred if the transfer statically was to that same actor isolation.
This issue can come up when a value is initially statically disconnected, but
after we performed dataflow, we discovered that it was actually actor isolated
at the transfer point, implying that we are not actually transferring.

Example:

```swift
@MainActor func testGlobalAndGlobalIsolatedPartialApplyMatch2() {
  var ns = (NonSendableKlass(), NonSendableKlass())
  // Regions: (ns.0, ns.1), {(mainActorIsolatedGlobal), @MainActor}

  ns.0 = mainActorIsolatedGlobal
  // Regions: {(ns.0, ns.1, mainActorIsolatedGlobal), @MainActor}

  // This is not a transfer since ns is already main actor isolated.
  let _ = { @MainActor in
    print(ns)
  }

  useValue(ns)
}
```

To do this, I also added to SILFunction an actor isolation that SILGen puts on
the SILFunction during pre function visitation. We don't print it or serialize
it for now.

rdar://123474616
2024-03-25 22:58:17 -07:00
Michael Gottesman
0b13da8fa6 Merge pull request #72572 from gottesmm/pr-26c22d031b70b00c14d7990ff90f70b64f91e0f9
[region-isolation] Shrink PartitionUtils.h by moving large functions to cpp file.
2024-03-25 17:14:50 -07:00
Michael Gottesman
901c7c2b6c [region-isolation] Shrink PartitionUtils.h by moving large functions to cpp file.
Just making PartitionUtils.h a little easier to walk through by moving more of
the impl into the .cpp file. This reduces the header from ~1500 lines to ~950
lines which is more managable. This is especially important since I am going
to be adding IsolationHistory to the header file which will expand it even
further.
2024-03-25 14:15:10 -07:00
Kuba (Brecka) Mracek
89cd62604b Merge pull request #72472 from kubamracek/embedded-keypaths
[embedded] Compile-time (literal) KeyPaths for Embedded Swift
2024-03-25 10:58:51 -07:00
Michael Gottesman
357a53ab48 [region-isolation] Clean up use after transfer error to use the dynamic isolation information of the transfered operand value in its diagnostic message.
As an example of the change:

-  // expected-note @-1 {{'x' is transferred from nonisolated caller to main actor-isolated callee. Later uses in caller could race with potential uses in callee}}
+  // expected-note @-1 {{transferring disconnected 'x' to main actor-isolated callee could cause races in between callee main actor-isolated and local nonisolated uses}}

Part of the reason I am doing this is that I am going to be ensuring that we
handle a bunch more cases and I wanted to fix this diagnostic before I added
more incaranations of it to the tests.
2024-03-22 13:12:51 -07:00
Michael Gottesman
608493c15c [region-isolation] Move computation of SILIsolationInfo for a SILFunctionArgument onto SILIsolationInfo::get(...). 2024-03-21 14:16:20 -07:00
Michael Gottesman
5b8171240d [region-isolation] Rename IsolationRegionInfo -> SILIsolationInfo.
That is really what IsolationRegionInfo is.
2024-03-21 14:16:20 -07:00
Michael Gottesman
f991517644 [region-isolation] Refactor out dynamic getIsolationRegionInfo for an entire region into a helper.
I also did a little bit of renaming of variable names to make the code a little
clearer.
2024-03-21 14:16:20 -07:00
Michael Gottesman
e00fbfa6b6 [region-isolation] Refactor RegionAnalysis AST pattern matching onto a helper factory method on IsolationRegionInfo.
Long term I would like to get region analysis and transfer non sendable out of
the business of directly interpreting the AST... but if we have to do it now, I
would rather us do it through a helper struct. At least the helper struct can be
modified later to work with additional SIL concurrency support when it is added.
2024-03-21 14:16:20 -07:00
Michael Gottesman
d72f078be4 [region-isolation] Add ability to Profile an IsolationRegionInfo.
Just slicing down a larger diff to make it easier to review.
2024-03-21 14:16:20 -07:00
Michael Gottesman
12aad7c251 [region-isolation] Define an == on IsolationRegionInfo.
Importantly, if we have an actor isolation, we always defer to it.
2024-03-21 14:16:20 -07:00
Michael Gottesman
0d96a16491 [region-isolation] Move IsolationRegionInfo higher up in PartitionUtils.h
This is in preparation for beginning to track an IsolationRegionInfo in
TransferringOperand.

Just splitting this into a separate commit to make it easier to read.
2024-03-21 14:16:20 -07:00
Michael Gottesman
e0fdce1fa3 [region-isolation] Convert TransferringOperand to be a bump allocated ptr type from a pointer type.
I need to start tracking the dynamic IsolationRegionInfo for the transferring
operand so I can ignore uses that are part of the same
IsolationRegionInfo. IsolationRegionInfo doesn't fit into a pointer, so just to
keep things the same, I am going to just allocate it.

This is an initial staging commit that tests out the bump ptr allocating without
expanding the type yet.
2024-03-21 14:16:20 -07:00
Meghana Gupta
3d8f714ed4 Add a new api findUnreferenceableStorage to check if struct or its fields have unreferenceable storage 2024-03-20 20:06:43 -07:00
Kuba Mracek
b642d771be [embedded] Compile-time (literal) KeyPaths for Embedded Swift
Enable KeyPath/AnyKeyPath/PartialKeyPath/WritableKeyPath in Embedded Swift, but
for compile-time use only:

- Add keypath optimizations into the mandatory optimizations pipeline
- Allow keypath optimizations to look through begin_borrow, to make them work
  even in OSSA.
- If a use of a KeyPath doesn't optimize away, diagnose in PerformanceDiagnostics
- Make UnsafePointer.pointer(to:) transparent to allow the keypath optimization
  to happen in the callers of UnsafePointer.pointer(to:).
2024-03-20 15:35:46 -07:00
Michael Gottesman
a2d1adfcd0 Add include <variant> in a place that I missed. 2024-03-18 14:50:03 -07:00
Michael Gottesman
f796f11c97 [region-isolation] Rename ValueIsolationRegionInfo -> IsolationRegionInfo. 2024-03-18 12:24:34 -07:00
Michael Gottesman
afbcf85727 [region-isolation] Change named transfer non transferable error to use the dynamic merged IsolationRegionInfo found during dataflow.
I also eliminated the very basic "why is this task isolated" part of the warning
in favor of the larger, better, region history impl that I am going to land
soon. The diagnostic wasn't that good in the first place and also was fiddly. So
I removed it for now.

rdar://124960994
2024-03-18 12:13:36 -07:00
Michael Gottesman
f7d1f2acb9 [region-isolation] Define getIsolationRegionInfo(Element elt) on PartitionOpEvaluator.
This will let me look up the dynamic isolation region associated with \p elt
while performing dataflow.
2024-03-18 12:13:30 -07:00
Michael Gottesman
b23b791c1b [region-isolation] Move ValueIsolationRegionInfo from RegionAnalysis.h -> PartitionUtils.h.
I am going to need this so that I can use it when evaluating partition
ops. Specifically, so I can when I find two values that do not merge correctly,
emit an error.
2024-03-18 12:13:30 -07:00
Michael Gottesman
465bb230c4 [region-isolation] Rename callback handleFailure -> handleLocalUseAfterTransfer.
Now that we have other forms of error callbacks, having such a general name for
any specific failure is misleading and hinds intent.
2024-03-18 12:13:30 -07:00
Meghana Gupta
2d27ec581c Merge pull request #72267 from meg-gupta/enablesilcombineempty
Enable SILCombine of inject_enum_addr for empty types
2024-03-12 20:34:09 -07:00
Michael Gottesman
f2b5b86e2e [region-isolation] Learn how to find variable names around phi arguments.
With this, the only remaining "task or actor isolated" error is due to changes I
need to land around async let.
2024-03-12 13:43:02 -07:00
Meghana Gupta
50f0fd232c Enable SILCombine of inject_enum_addr for empty types 2024-03-12 13:01:44 -07:00
Michael Gottesman
806cd7940e [region-isolation] Fix actor isolated parameters to get an actor isolated error instead of a task isolated error.
Now that we actually know the region that non transferrable things belong to, we
can use this information to give a better diagnostic here.

A really nice effect of this is that we now emit that actor isolated parameters
are actually actor isolated instead of task isolated.
2024-03-10 22:08:40 -07:00
Michael Gottesman
b2c85a8294 [region-isolation] Rather than tracking task isolated values via a separate non-transferrable array... just track it by using ValueIsolationRegionInfo on a value.
In a subsequent commit, this is going to let me begin handling parameters with
actor regions in a nice way (and standardize all of the errors).

This is meant to be a refactoring commit that uses the current tests in tree to
make sure I did it correctly, so no tests need to be updated.
2024-03-10 22:08:40 -07:00
Meghana Gupta
eb7772ca1a Update canTriviallyDeleteOSSAEndScopeInst and a related assertion
A destroy_value of Optional.none can be deleted.

A move-only struct with deinit has a non trivial SILType but OwnershipKind::None,
such values cannot be deleted.
2024-03-01 12:15:14 -08:00
Michael Gottesman
11f0ff6e32 [sil] Ensure that all SILValues have a parent function by making it so that SILUndef is uniqued at the function instead of module level.
For years, optimizer engineers have been hitting a common bug caused by passes
assuming all SILValues have a parent function only to be surprised by SILUndef.
Generally we see SILUndef not that often so we see this come up later in
testing. This patch eliminates that problem by making SILUndef uniqued at the
function level instead of the module level. This ensures that it makes sense for
SILUndef to have a parent function, eliminating this possibility since we can
define an API to get its parent function.

rdar://123484595
2024-02-27 13:14:47 -08:00
Michael Gottesman
e21c0a6c39 [sil] Teach variable name inference how to look through more instructions. 2024-02-22 13:50:06 -08:00
Michael Gottesman
9154082d12 [sil] Teach VariableNameInferrer how to look through /all/ accessors. 2024-02-22 13:50:06 -08:00
Ben Barham
ef8825bfe6 Migrate llvm::Optional to std::optional
LLVM has removed llvm::Optional, move over to std::optional. Also
clang-format to fix up all the renamed #includes.
2024-02-21 11:20:06 -08:00
Michael Gottesman
f3edb5730a [transferring] Add support for transferring results.
rdar://121324697
2024-02-14 14:39:02 -08:00
Michael Gottesman
7df13b3c1f [sil] Refactor VariableNameUtils for handling more kinds of writes to temporaries. 2024-02-06 16:18:34 -08:00
Michael Gottesman
a569160f21 [sil] Refactor out the variable name inferer from MoveOnlyDiagnostics.cpp -> VariableNameUtils.
I am going to reuse this for TransferNonSendable. In the process I made a few
changes to make it more amenable to both use cases and used the current set of
tests that we have for noncopyable types to validate that I didn't break
anything.
2024-02-06 13:42:35 -08:00
Michael Gottesman
434261b851 [region-isolation] When changing an elements region, if that element was the last element in a transferred regionl, remove that region from the transferredOpMap.
I also added some validation that we properly do this. It only runs when NDEBUG
is not set.

rdar://122280930
2024-02-04 14:16:24 -08:00
Michael Gottesman
8178eb0a6e Merge pull request #70514 from gottesmm/pr-763529d8ee9f4164b82d03087fec25439cc1d315
[region-isolation] Fix the dataflow and add support for project_block_storage
2023-12-19 10:21:59 -08:00
Meghana Gupta
890ec7d855 Merge pull request #70477 from meg-gupta/fixpredmemopt
Use OSSALifetimeCompletion in PredictableMemOpt
2023-12-15 19:18:22 -08:00
Michael Gottesman
bcb8f1b0d8 [region-isolation] Implement the dataflow correctly.
This involved fixing a few different bugs.

1. We were just performing dataflow by setting that only the initial block needs
to be updated. This means that if there isn’t anything in the initial dataflow
block, we won’t visit any successor blocks. Instead the correct thing to do here
is to visit all blocks in the initial round.

2. I also needed to fix a separate issue where we were updating our union-find
data structure incorrectly as found by an assert on transfernonsendable.swift
that was triggered once I fixed 1. Put simply, we needed to set a new max label
+ 1 when our new max element is less than or equal to the old max label + 1…
before we just did less than so if we had a new max element that is the same as
our fresh label, we wouldn’t increment the fresh label.

rdar://119584497
2023-12-15 17:14:09 -08:00
Meghana Gupta
06f0d15c45 Use OSSALifetimeCompletion in PredictableMemOpt
The current algorithm to complete lifetimes is incorrect in a few cases.
Use OSSALifetimeCompletion instead.

Fixes rdar://119204768
2023-12-15 15:16:55 -08:00
nate-chandler
680c737ddb Merge pull request #64789 from nate-chandler/more-move-values
[SILGen] Used move_value for more lexical values.
2023-12-15 07:07:54 -08:00