Commit Graph

11193 Commits

Author SHA1 Message Date
Michael Gottesman
d94e4c4434 [region-isolation] Using the print method from the previous commit, ensure that we dump out SentNeverSendable, InOutSendingNotDisconnectedAtExit, AssignNeverSendableIntoSendingResult earlier when we initially detect them.
This just improves the ability to quickly triage bugs in SendNonSendable. It
used to be this way, but in the process of doing some refactoring, I moved the
logging too late by mistake.
2024-11-19 12:48:30 -08:00
Michael Gottesman
d33f819038 [region-isolation] Move freeform logging on the specific error we are emitting into a method on the error itself.
I am doing this since I discovered that we are not printing certain errors as
early as we used to (due to the refactoring I did here), which makes it harder
to see the errors that we are emitting while processing individual instructions
and before we run the actual dataflow.

A nice side-effect of this is that it will make it easy to dump the error in the
debugger rather than having to wait until the point in the code where the normal
logging takes place.
2024-11-19 12:48:30 -08:00
Andrew Trick
ff21ddefa6 Merge pull request #77675 from atrick/endapply_utils
[NFC] Utilities for lifetime dependent coroutines
2024-11-18 09:44:25 -08:00
Andrew Trick
4612728581 [NFC] Add BeginApplyInst::getEndApplyUses() API.
A begin_apply token may be used by operands that do not end the coroutine:
mark_dependence.

We need an API that gives us only the coroutine-ending uses. This blocks
~Escapable accessors.

end_borrow is considered coroutine-ending even though it does not actually
terminate the coroutine.

We cannot simply ask isLifetimeEnding, because end_apply and abort_apply do not
end any lifetime.
2024-11-18 01:37:00 -08:00
Slava Pestov
47156e006b AST: Introduce ProtocolConformanceRef::forAbstract() 2024-11-16 16:16:06 -05:00
Slava Pestov
2c3ee09678 SILOptimizer: Fix invariant violation in usePrespecialized()
The conformances no longer match the replacement types after the
transformation here, so we must look them up again. This is now
flagged by SubstitutionMap::verify().
2024-11-15 17:27:24 -05:00
eeckstein
972ac6ff28 Merge pull request #77610 from eeckstein/fix-array-element-propagation
ArrayElementValuePropagation: require that the initialization and element-get are in the same block for non-trivial values.
2024-11-15 07:53:11 +01:00
Meghana Gupta
434461d8f4 Fix DCE of load_borrow when it is derived from another borrow scope
Fixes rdar://138663452
2024-11-14 12:09:11 -08:00
Meghana Gupta
5486b7a289 Merge pull request #77571 from meg-gupta/fixsimplifycfgunreachable
Fix simplifySwitchEnumUnreachableBlocks for default cases in ossa
2024-11-14 07:44:19 -08:00
eeckstein
a964cc0ba1 Merge pull request #77590 from eeckstein/fix-enum-data-simplification
Optimizer: fix simplification of unchecked_enum_data
2024-11-14 16:18:54 +01:00
Erik Eckstein
16c77d97dc ArrayElementValuePropagation: require that the initialization and element-get are in the same block for non-trivial values.
In OSSA we only insert a copy_value of the element at the array initialization point.
This would result in an over-consume if the getElement is in a loop.
Therefore require that both semantic calls are in the same block.

Fixes an ownership verifier crash.
2024-11-14 13:30:06 +01:00
Erik Eckstein
99ef6f727d Optimizer: replace unchecked_enum_data simplification in SILCombine with the corresponding instruction simplification from SwiftCompilerSources
The optimization in SILCombine had a bug (which is already fixed in the instruction simplification).
2024-11-14 09:18:29 +01:00
Nate Chandler
e76dc2d5e4 [Devirtualizer] Replace begin_apply allocations.
When devirtualizing a yield_once_2 begin_apply, replace all uses of the
allocation address, just as is done with the token.
2024-11-13 21:33:07 -08:00
eeckstein
6d4db0e649 Merge pull request #77583 from eeckstein/fix-dce
DeadCodeElimination: don't remove end_lifetime instructions with address operands
2024-11-13 18:51:45 +01:00
Andrew Trick
eb8d9f4960 Merge pull request #77575 from atrick/preserve_extend_lifetime
Preserve extend_lifetime during dead instruction code elimination.
2024-11-13 05:22:30 -08:00
Meghana Gupta
452ae3b994 Merge pull request #77568 from meg-gupta/dceupdate2
Insert end_borrow for dead phi operands only when required
2024-11-13 04:10:30 -08:00
Erik Eckstein
64698ca6bb DeadCodeElimination: don't remove end_lifetime instructions with address operands
DCE cannot reason about values in memory.

Fixes a memory lifetime verification error
rdar://139779406
2024-11-13 12:04:41 +01:00
Andrew Trick
b30fd77906 Remove all trivial extend_lifetime instructions
during the MoveOnlyWrappedTypeEliminator pass.

Such instructions are only valid in RAW SIL. This pass generally removes marker
instructions that are only used for ownership diagnostics.

Fixes rdar://139450982 ([GH:#77451] Assert failure on
`extend_lifetime` verify with `onone-simplification` pass disabled)
2024-11-12 23:53:56 -08:00
eeckstein
4934b79bed Merge pull request #77527 from eeckstein/fix-reborrow-flag
Fix the computation of the re-borrow flags for guaranteed phi arguments
2024-11-13 07:21:13 +01:00
Meghana Gupta
106dc8a7e0 Fix simplifySwitchEnumUnreachableBlocks for default cases in ossa
Unlike non-ossa, ossa's switch_enum accepts an argument for the default case
When all other cases are unreachable, replace the default block's phi with
the switch_enum's operand and transform the switch_enum to a branch.

Fixes rdar://139441002
2024-11-12 21:11:54 -08:00
Meghana Gupta
fd6d29d10b Insert end_borrow for dead phi operands only when required
DCE inserts end_borrow at phi operands when a guaranteed phi becomes dead.
This should be done for reborrows which end the lifetime of the incoming value.
The existing check was not accurate and ended up inserting end_borrow for forwarded values as well.

Fixes rdar://139283745
2024-11-12 14:05:23 -08:00
Erik Eckstein
63141b6a8b Optimizer: when propagating the concrete type of an existential, make sure to not violate dominance order
Make sure that an enum is only initialized once before it is taken. This implies that the initialization must dominate the take.

Fixes a verifier crash: rdar://139381701
2024-11-12 21:34:58 +01:00
Erik Eckstein
156f1fc759 Optimizer: prevent duplicating blocks with dead-end begin-borrow instructions.
In case the control flow ends in a dead-end block there can be begin-borrow instructions which have no corresponding end-borrow uses.
After duplicating such a block, the re-borrow flags cannot be recomputed correctly for inserted phi arguments.
Therefore just disable duplicating such blocks, e.g. in jump-threading.
2024-11-12 10:04:09 +01:00
Erik Eckstein
51e3e5ed80 Optimizer: rename BorrowArgumentsUpdater -> GuaranteedPhiUpdater
NFC
2024-11-12 09:26:59 +01:00
Erik Eckstein
626b40c329 LoopRotate: don't verify the optimized function within the pass
With the new re-borrow flag computation, verification can fail before the re-borrow flags are updated at the end of the pass.
2024-11-12 09:26:59 +01:00
Erik Eckstein
7bf0010c92 SIL: remove computeIsReborrow
This API computes the re-borrow flags for guaranteed phis based on the presence of forwarding instructions in the incoming values.
This is not correct in all cases, because optimizations can optimize away forwarding instructions.

Fixes a verifier crash: rdar://139280579
2024-11-12 09:26:59 +01:00
Erik Eckstein
6b8c6a3c3b SIL: rename updateBorrowedFrom to updateBorrowArguments
NFC
2024-11-12 09:26:58 +01:00
Michael Gottesman
b5ce28fc57 [region-isolation] Cache getUnderlyingTrackedValue.
TLDR: Was looking at some performance traces and saw that we need to cache the
result of this value.

----

Specifically, I noticed that we were spending a lot of time computing this
operation. When I looked at the code I saw that we already had a cache along the
relevant code paths... but the cache was from equivalence class representative
-> state. Before we hit that cache, we were performing the work to map the value
to the equivalence class representative... so the work to perform the relevant
lookup from value -> state (which goes through the equivalence class
representative) was not just a hash table lookup. This operation makes it
cheaper by making it two cache lookups.

It may be possible to make this cheaper by redoing the actual mapping of
information so that we can go straight from value to state. I think it would be
slightly different since we would probably need to represent the state in a
separate array and map with indices... which is really just a more efficient
hash table. We could also use malloc/etc but lets not even talk about that.

rdar://139520959
2024-11-11 11:43:07 -08:00
Michael Gottesman
edd6bf5704 [region-isolation] A little code re-organization.
I am going to be adding more functionality to this that moves a bit of the
utilities code into it. So it really makes sense to move it to the top of the
file closer to that code. I am doing this separately to make the other
refactoring easier to see in the diff.
2024-11-11 11:43:07 -08:00
nate-chandler
aab880da05 Merge pull request #77429 from nate-chandler/general-coro/20241104/1
[CoroutineAccessors] Synthesize default requirement implementations.
2024-11-10 07:43:11 -08:00
Nate Chandler
742d5f25b4 [SILInliner] Fix yield_once_2 dealloc_stacks.
Don't iterate users while deleting them.
2024-11-08 15:46:33 -08:00
Kuba Mracek
b197db88b0 [ConstExpr] Relax expectations in ArrayAppendElement to allow direct-passed element 2024-11-08 14:37:15 -08:00
Nate Chandler
f1f0ccdeff [NFC] Improved predicate names. 2024-11-06 20:52:21 -08:00
Arnold Schwaighofer
34c417d9ff Merge pull request #77379 from aschwaighofer/enable_aggressive_reg2mem
Enable heuristic that tries to keep large values in memory
2024-11-06 11:36:33 -08:00
eeckstein
52cff19281 Merge pull request #77400 from eeckstein/fix-specialize-attr
SIL: avoid creating SIL functions for not used imported specialization attributes
2024-11-06 07:16:34 +01:00
nate-chandler
12a137a7b4 Merge pull request #77401 from nate-chandler/rdar139235335
[CapturePromotion] dealloc_box doesn't mutate.
2024-11-05 18:14:52 -08:00
Nate Chandler
027070a170 [NFC] CapturePromotion: Use macro.
Keep to a single spelling for an "always OK" instruction.
2024-11-05 11:43:21 -08:00
Nate Chandler
986d155db7 [CapturePromotion] dealloc_box doesn't mutate.
The instruction only deallocates the box, it doesn't destroy its
contents.  It's even less mutating than a `destroy_value`, which is
already regarded as non-mutating.
2024-11-05 11:42:21 -08:00
Erik Eckstein
15c6abf66d SIL: avoid creating SIL functions for not used imported specialization attributes
This ended up in creating a lot of Array functions, even if a program didn't use Array at all.
Now, only add specialization attributes if a function is already there.
Otherwise remember the attributes and add them to a function once it is created.
2024-11-05 20:37:15 +01:00
eeckstein
58d2259d83 Merge pull request #77390 from eeckstein/fix-rc-identity-analysis
RCIdentityAnalysis: don't let a non-copyable value be the RC root of a copyable value
2024-11-05 18:17:28 +01:00
Erik Eckstein
3b06bef250 RCIdentityAnalysis: don't let a non-copyable value be the RC root of a copyable value
Otherwise optimizations like retain-sinking might create retain_value instructions with a non-copyable operand.

Fixes a compiler crash.
rdar://139103557
2024-11-05 12:05:34 +01:00
Arnold Schwaighofer
dc3c19164a PMO: Don't block pmo for large types - rather only block expansion of tuples 2024-11-04 17:06:24 -08:00
Michael Gottesman
32b4de60a9 Rename transfer -> send.
Accomplished using clangd's rename functionality.
2024-11-04 15:17:51 -08:00
Michael Gottesman
0a56827073 [region-isolation] Rename TransferNonSendable.cpp -> SendNonSendable.cpp.
Just beginning the elimination of the word transferred from the code base.
2024-11-02 16:58:17 -07:00
Michael Gottesman
1966980c6b Merge pull request #77354 from gottesmm/rdar136593706
[region-isolation] Fix an off by one error when mapping AST capture indices to SIL level parameter indices.
2024-11-01 20:03:42 -07:00
Michael Gottesman
3c38c79f7a [region-isolation] Implement MergeIsolationRegionInst.
I am adding this instruction to express artificially that two non-Sendable
values should be part of the same region. It is meant to be used in cases where
due to unsafe code using Sendable, we stop propagating a non-Sendable dependency
that needs to be made in the same region of a use of said Sendable value. I
included an example in ./docs/SIL.rst of where this comes up with @out results
of continuations.
2024-11-01 11:25:53 -07:00
Arnold Schwaighofer
501f2be057 Merge pull request #77331 from aschwaighofer/large_types_reg2mem_aggressive
LargeTypesReg2Mem: Add a new heuristic that trys harder to keep large     values on the stack
2024-11-01 06:15:18 -07:00
Arnold Schwaighofer
787c996394 LargeTypesReg2Mem: Add a new heuristic that trys harder to keep large
values on the stack

This heuristic can be enabled by passing -Xfrontend
-enable-aggressive-reg2mem.

rdar://123916109
2024-10-31 13:22:06 -07:00
Arnold Schwaighofer
3ab4484145 SILOptimizer: Add destructure peepholes
```
 (%7, ...) = destructure_tuple %6 : $(A, ...)
 tuple_addr_constructor [assign] %9 : $*(A, ...) with (%7 : $A, ...)
   =>
 store [assign] %6 to %9
```

and

```
 (tuple (destructure_tuple %t))
  => %t
```
2024-10-31 09:47:27 -07:00
Michael Gottesman
c3d445831b [region-isolation] Fix an off by one error when mapping AST capture indices to SIL level parameter indices.
This problem comes up with the following example:

```swift
class A {
    var description = ""
}

class B {
    let a = A()

    func b() {
        let asdf = ""
        Task { @MainActor in
            a.description = asdf // Sending 'asdf' risks causing data races
        }
    }
}
```

The specific issue is that the closure we generate actually includes an
implicit(any) parameter at the SIL level which occurs after the callee operand
but before the captures. This caused the captured variable index from the AST
and the one we compute from the partial_apply to differ by 1. So we need to
subtract 1 in such a case. That is why we used to print 'asdf' instead of 'a'
above.

DISCUSSION: This shows an interesting difference between SIL applied arg indices
and AST indices. SIL applied arg indices would include the implicit(any)
parameter since it is a parameter in the SIL function type. In contrast, this
doesn't show up in the formal AST parameters or captures. To make it easier to
reason about this, I added a new API to ApplySite called
ApplySite::getASTAppliedArgIndex and added large comments to
getASTAppliedArgIndex and getAppliedArgIndex that explains the issue.

rdar://136593706
https://github.com/swiftlang/swift/issues/76648
2024-10-30 18:32:45 -07:00