Commit Graph

11224 Commits

Author SHA1 Message Date
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
John McCall
db157c94ef Strengthen verification of the operand of hop_to_executor and extract_executor. 2024-10-30 14:41:50 -07:00
Meghana Gupta
58ddf0a884 Merge pull request #77279 from meg-gupta/ossasilcombine
Fix SILCombine of inject_enum_addr of ~Copyable values
2024-10-29 22:10:54 -07:00
Meghana Gupta
d58d97d97a Fix SILCombine of inject_enum_addr of ~Copyable values
Fixes rdar://138798467
2024-10-29 14:09:57 -07:00
elsh
60f0eac73a Fix canSerialize logic 2024-10-29 12:28:39 -07:00
elsh
6676288005 Revert "Revert "Merge pull request #76832 from swiftlang/elsh/pcmo-refactor""
This reverts commit 056d4474ad.
2024-10-28 17:24:24 -07:00
Ben Barham
f59b2f19e6 Merge branch 'main' into 2024-rebranch-to-main 2024-10-28 13:03:35 -07:00
Michael Gottesman
2b6b98d767 Merge pull request #77238 from gottesmm/region_isolation_refactoring
[region-isolation] Refactor code so that I can more easily add additional error kinds
2024-10-25 22:13:43 -07:00
Michael Gottesman
067dbadfef [region-isolation] Add a print command that emits errors of the form "*-isolated code" or "code in the current task"
This makes it so that one does not need to deal with the differences in text in
between the task isolated case and the actor isolated case. This is done by
swallowing the entire part of this message in one method rather than having the
caller do the work.
2024-10-25 16:57:55 -07:00
Michael Gottesman
e49ef778f1 [region-isolation] Rename RequireInOutSendingAtFunctionExit -> InOutSendingAtFunctionExit.
I am going to be doing more types of checks for such inout sending types, so it
makes sense to rename it to have a more general name.
2024-10-25 16:57:54 -07:00
Erik Eckstein
ed67e36ce5 bridging: reduce #ifdef USED_IN_CPP_SOURCE in bridging headers
Especially avoid any constructors in `#ifdef USED_IN_CPP_SOURCE` blocks, because this breaks Windows ARM64.
2024-10-25 09:47:56 +02:00
Michael Gottesman
df24fc41ce [region-isolation] Convert AssignIsolatedIntoSendingResult to be emitted as a verbatim error. 2024-10-24 16:27:21 -07:00
Michael Gottesman
4265aa1d71 [region-isolation] Refactor InOutSendingNotDisconnectedAtExit to use the error from PartitionUtils.h rather than have its own type. 2024-10-24 16:26:12 -07:00
Michael Gottesman
16bbd260ce [region-isolation] Refactor SentNeverSendable to use the new error type generated by PartitionUtils. 2024-10-24 16:02:57 -07:00
Michael Gottesman
2e403b9a7e [region-isolation] Remove dead code path.
This also has the nice effect of making the subsequent refactoring I am going to
do simpler.
2024-10-24 15:11:50 -07:00
Michael Gottesman
f23ad55acb [region-isolation] Eliminate CRTP for errors and just pass through an error struct instead.
This is going to let me just pass through the error struct to the diagnostic
rather than having the CRTP and then constructing an info object per CRTP.
Currently, to make it easier to refactor, I changed the code in
TransferNonSendable to just take in the new error and call the current CRTP
routines. In the next commit, I am going to refactor TransferNonSendable.cpp
itself. This just makes it easier to test that I did not break anything.
2024-10-24 15:00:43 -07:00
swift-ci
5bd2fd586a Merge remote-tracking branch 'origin/main' into rebranch 2024-10-22 23:14:53 -07:00
eeckstein
9a845a0f93 Merge pull request #77148 from eeckstein/reinstate-load-copy-to-borrow-opt
Reinstate the load-copy-to-borrow optimization
2024-10-23 08:03:14 +02:00
swift-ci
3d969906c5 Merge remote-tracking branch 'origin/main' into rebranch 2024-10-22 13:14:35 -07:00
Andrew Trick
36e14c7c9b Merge pull request #77160 from atrick/moveonly_markdep
Add move-only checker support for dependencies
2024-10-22 13:04:55 -07:00