Commit Graph

2053 Commits

Author SHA1 Message Date
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
2603c847a5 Merge pull request #72476 from gottesmm/cleanups
[region-isolation] Some cleanups in preparation for later work
2024-03-21 18:16:47 -07:00
eeckstein
83b2593fba Merge pull request #72485 from eeckstein/node-bits
Some improvements for inline bitfields in SILNode, SILBasicBlock and Operand
2024-03-21 22:45:22 +01: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
c09b9f811b [region-isolation] Add a helper for getting an ApplyIsolationCrossing from an instruction.
I am making this specific API since I am going to make it so that
SILIsolationInfo::get(SILInstruction *) can infer isolation info from self even
from functions that are not apply isolation crossing points. For example, in the
following, we need to understand that test is main actor isolated and we
shouldn't emit an error.

```swift
@MainActor func test(_ x: NonSendable) {}

@OtherActor func doSomething() {
  let x = NonSendable()
  Task.init { @MainActor in print(x) }
  test(x)
}
```
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
Erik Eckstein
367dc1a299 SwiftCompilerSources: add OperandSet and OperandWorklist
Implemented by bridging the C++ OperandSet, similar to BasicBlockSet and NodeSet
2024-03-21 15:52:39 +01:00
Erik Eckstein
7afa419dd4 SIL: improve inline bitfields in SILNode, SILBasicBlock and Operand
* Let the customBits and lastInitializedBitfieldID share a single uint64_t. This increases the number of available bits in SILNode and Operand from 8 to 20. Also, it simplifies the Operand class because no PointerIntPairs are used anymore to store the operand pointer fields.
* Instead make the "deleted" flag a separate bool field in SILNode (instead of encoding it with the sign of lastInitializedBitfieldID). Another simplification
* Enable important invariant checks also in release builds by using `require` instead of `assert`. Not catching such errors in release builds would be a disaster.
* Let the Swift optimization passes use all the available bits and not only a fixed amount of 8 (SILNode) and 16 (SILBasicBlock).
2024-03-21 15:52:39 +01: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
Erik Eckstein
308b5f9ff7 Swift Optimizer: add bridging for dynamic cast utilities
* `func canDynamicallyCast`
* `var CheckedCastAddrBranchInst.dynamicCastResult`
2024-03-19 10:54:38 +01: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
Meghana Gupta
a312225f17 Merge pull request #72274 from meg-gupta/debugbreak
Add an option -sil-break-before-pass-count
2024-03-12 19:50:28 -07:00
Meghana Gupta
dacf5b47e3 Add an option -sil-break-before-pass-count to break before running a pass when in the debugger
We often look at the SIL output of -sil-print-function and may want to debug a specific pass
after looking at the output.

-sil-break-before-pass-count=<pass_number> will allow to automatically break in the debugger
after <pass_count> of passes are run.

Example:
From -sil-print-function dump:
"SIL function after  #6680, stage MidLevel,Function, pass 38: RedundantLoadElimination"

-Xllvm -sil-break-before-pass-count=6680 will break before running this pass in the debugger
2024-03-12 15:16:01 -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
258992cc71 Add an include of variant. 2024-03-11 00:07:52 -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
Michael Gottesman
93844934f9 [region-isolation] Begin tracking region information instead of just whether a value is actor derived.
To keep this as an NFC commit, I only modeled initially actor isolated using
this. I am going to make it so that we properly treat global actor isolated
values as actor isolated/etc in a subsequent commit.
2024-03-10 22:08:40 -07:00
Andrew Trick
31b3a91912 Merge pull request #71703 from atrick/lifetime-extend
LifetimeDependenceScopeFixup: fix handling of returned dependence
2024-03-05 23:17:21 -08:00
Andrew Trick
a451af14c9 Increase BlockCapacity from 8 to 16 for intrusive BasicBlockSet.
InstructionRange uses 3 sets. Some algorithms need two active ranges at the top-level. Additionally, utilities often have few levels of nesting that each require a block set.
2024-03-05 17:08:13 -08:00
Michael Gottesman
f06244f397 [region-isolation] Take into account that Swift's RPO order doesn't include blocks that are dead.
When we run RegionAnalysis, since it uses RPO order, we do not visit dead
blocks. This can create a problem when we emit diagnostics since we may merge in
a value into the region that was never actually defined. In this patch, if we
actually visit the block while performing dataflow, I mark a bit in its state
saying that it was live. Then when we emit diagnostics, I do not visit blocks
that were not marked live.

rdar://124042351
2024-03-04 17:09:51 -08:00
Michael Gottesman
2ad0c1aa4a Merge pull request #71998 from gottesmm/pr-ca981c22b983cdfc472ae9c1d737c76dae955aed
[region-isolation] Refactor out the stubify dead function if no longer used functionality from move only checker into its own pass and put it before region based isolation.
2024-03-01 17:21:45 -08:00
Michael Gottesman
3236bc26fa [region-isolation] Refactor out the stubify dead function if no longer used functionality from move only checker into its own pass and put it before region based isolation.
I am doing this since region based isolation hit the same issue that the move
checker did. So it makes sense to refactor the functionality into its own pass
and move it into a helper pass that runs before both.

It is very conservative and only stubifies functions that the specialization
passes explicitly mark as this being ok to be done to.
2024-03-01 13:11:07 -08: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
Meghana Gupta
7508a90af0 Enable destructure_struct/destructure_tuple simplifications in silcombine 2024-02-26 10:58:08 -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
Erik Eckstein
71fcae7fe8 SwiftCompilerSources: add the ability to implement SIL verification in swift 2024-02-22 07:12:10 +01:00
Ben Barham
5637284e48 Merge pull request #71368 from bnbarham/std-optional-all-the-things
Migrate llvm::Optional to std::optional
2024-02-21 16:54:00 -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
Nate Chandler
5a6873b98f [SILOptimizer] Remove DestroyHoisting pass.
It's no longer used.
2024-02-19 11:41:38 -08:00
Michael Gottesman
f3edb5730a [transferring] Add support for transferring results.
rdar://121324697
2024-02-14 14:39:02 -08:00
Meghana Gupta
75bd5b08dd Add LifetimeDependenceScopeFixup pass 2024-02-13 16:52:01 -08:00
Andrew Trick
8c092911ef Add the LifetimeDependenceInsertion pass.
Insert mark_dependence [nonescaping] markers at every lifetime
introducer that produces a lifetime-dependent value.
2024-02-12 09:57:14 -08:00