Commit Graph

2472 Commits

Author SHA1 Message Date
Nate Chandler
a67c22d905 [CopyPropagation] Only delete if canonicalized.
Owned lifetime canonicalization bails on move-only values.

Previously, though, every value that was fed to canonicalization was
then attempted to be deleted.  For dead move-only values, the result
could be to shorten move-only lifetimes, which is illegal per language
rules.

Here, this is fixed by not attempting to delete owned values for which
canonicalization bailed.

rdar://114323803
2023-08-23 11:42:57 -07:00
swift-ci
174b6bc00c Merge remote-tracking branch 'origin/main' into rebranch 2023-08-18 19:34:12 -07:00
swift-ci
978395bc11 Merge pull request #62959 from meg-gupta/simplifycfgossapr3
Migrate simple and dominator based jump threading to OSSA
2023-08-18 17:18:10 -07:00
Meghana Gupta
9311e948a5 Avoid unnecessary block arguments in SimplifyCFG::threadEdge and SimplifyCFG::simplifyThreadedTerminators 2023-08-17 10:33:22 -07:00
Meghana Gupta
bbd6a87e2b Fix simplifyArguments for a few edges cases in OSSA 2023-08-16 23:14:36 -07:00
swift-ci
d3fe8a6f97 Merge remote-tracking branch 'origin/main' into rebranch 2023-08-15 06:54:22 -07:00
Nate Chandler
0403ab102d [Mem2Reg] Don't canonicalize erased vals.
To eliminate copies which become newly spurious, Mem2Reg canonicalizes
the lifetimes of values that are stored and of newly introduced phis
after rewriting.

It's possible, however, for the values that are stored to be deleted
during canonicalization if a value and its copy are both stored to the
address.  Such values must not be canonicalized.  So check whether
values have been erased before canonicalizing them.

rdar://113762355
2023-08-14 15:08:57 -07:00
Meghana Gupta
93d6f93435 Fixes to SimplifyCFG::threadEdge for OSSA 2023-08-14 11:36:05 -07:00
Meghana Gupta
05c7d64511 We don't consider it profitable to jump thread when we could optimize arc in OSSA.
Copy propagation should be able to handle such cases without jump threading.
2023-08-14 11:36:04 -07:00
Meghana Gupta
e3a98e88f3 Add new flags to enable specific SimplifyCFG operations 2023-08-14 11:36:04 -07:00
Meghana Gupta
a303bb4519 Disable SimplifyCFG::tailDuplicateObjCMethodCallSuccessorBlocks in OSSA
Support for this needs to be added
2023-08-14 11:36:04 -07:00
Meghana Gupta
bf68e3ff39 Disable ArgumentSplitter for non trivial types in ossa 2023-08-14 11:36:04 -07:00
swift-ci
3561327bdb Merge remote-tracking branch 'origin/main' into rebranch 2023-08-08 21:33:48 -07:00
Meghana Gupta
1075f77822 Merge pull request #67521 from meg-gupta/migratedoe
Migrate DeadObjectElimination to OSSA
2023-08-08 21:27:06 -07:00
swift-ci
9742461a27 Merge remote-tracking branch 'origin/main' into rebranch 2023-08-08 13:17:04 -07:00
Meghana Gupta
848466acbe Ensure no module transform is added in the function pipeline 2023-08-08 11:04:46 -07:00
Meghana Gupta
996fd8b978 Bail out from deleting a dead objection on seeing stores with lexical non-trivial source 2023-08-08 10:48:09 -07:00
Meghana Gupta
abdb9f0dba DeadObjectElimination: Handle allocating apply in ossa 2023-08-07 14:58:57 -07:00
Meghana Gupta
2831f2f528 Add findPointerEscape check on store source, before shortening its lifetime by inserting destroy at the store 2023-08-07 10:21:40 -07:00
Meghana Gupta
c3c78fa8f8 DeadObjectElimination: Handle alloc_ref in ossa 2023-08-07 10:21:40 -07:00
Meghana Gupta
f5c5c9e36b In non-ossa, ban non-trivial stores to alloc_ref's projections even the dtor is not inlined
There is no implementation to handle insertion of compensating releases of the non-trivial store's src for this case.
This never really happens, because destructor analysis is very limited and we end up banning alloc_ref with ref count instructions.
2023-08-07 10:21:40 -07:00
Meghana Gupta
768d98e1c4 Add a TODO in destructor analysis 2023-08-07 10:21:40 -07:00
Meghana Gupta
15e5364d25 DeadObjectElimination: Handle keypaths with non-trivial operands in OSSA 2023-08-07 10:21:40 -07:00
Meghana Gupta
41697b061c DeadObjectElimination: Handle non-trivial stores while processing alloc_stack 2023-08-07 10:21:40 -07:00
swift-ci
8308afaaf0 Merge remote-tracking branch 'origin/main' into rebranch 2023-08-01 17:33:45 -07:00
Joe Groff
03eec7da2b Remove redundant SILType::isMoveOnlyNominalType (NFC)
and implement `SILType::isPureMoveOnly` in terms of `Type::isPureMoveOnly`.
2023-08-01 14:07:38 -07:00
swift-ci
7bef9fd31a Merge remote-tracking branch 'origin/main' into rebranch 2023-07-31 21:34:34 -07:00
Nate Chandler
4f16bb1a74 [Mem2Reg] Don't canonicalize undef.
They aren't the kind of values whose lifetimes are eligible for
canonicalization.

rdar://113121424
2023-07-31 16:20:37 -07:00
Nate Chandler
f276ebf37e [Mem2Reg] Only lifetime canonicalize in OSSA.
Bail both on collecting stores and canonicalizing values if the function
being optimized doesn't have ownership.
2023-07-31 16:16:28 -07:00
swift-ci
1c1be7d116 Merge remote-tracking branch 'origin/main' into rebranch 2023-07-28 15:36:57 -07:00
swift-ci
1cca22bafe Merge pull request #67526 from nate-chandler/mem2reg/canonicalize-values
[Mem2Reg] Canonicalize promoted lifetimes.
2023-07-28 15:30:59 -07:00
Nate Chandler
b563c4ea3d [Mem2Reg] Canonicalize new phis.
Mem2Reg introduces new OSSA values in the form of phis.  Take
responsibility for canonicalizing their lifetimes.
2023-07-28 13:21:13 -07:00
Nate Chandler
52ce100ef4 [Mem2Reg] Canonicalize stored values.
Stored values may now be canonicalizable, now that they have one less
consuming use.
2023-07-28 13:19:52 -07:00
swift-ci
1ead4ffed7 Merge remote-tracking branch 'origin/main' into rebranch 2023-07-28 10:57:58 -07:00
Nate Chandler
e5d87f75a8 [SIL] Add source formal type to checked_cast_br.
It is necessary for opaque values where for casts that will newly start
out as checked_cast_brs and be lowered to checked_cast_addr_brs, since
the latter has the source formal type, IRGen relies on being able to
access it, and there's no way in general to obtain the source formal
type from the source lowered type.
2023-07-27 15:04:15 -07:00
Nate Chandler
7fb9bcade8 [Mem2Reg] NFC: Lifted livePhiBlocks higher.
Move it all the way up into MemoryToRegisters::run in preparation for
using it to canonicalize new phis.
2023-07-26 21:27:35 -07:00
Nate Chandler
c9e0bb5a67 [Mem2Reg] NFC: Removed spurious typealias.
`BlockSetVector` is not much shorter than `BasicBlockSetVector`, and there's
no reason to tie the type to `StackAllocationPromoter`.
2023-07-26 21:27:35 -07:00
Nate Chandler
496761407c [Mem2Reg] NFC: Fixed comments.
Corrected the comments describing what the
`promoteSingleBlockAllocation` and `promoteMultiBlockAllocation`
functions do since they were no longer accurate.
2023-07-26 21:27:35 -07:00
Nate Chandler
e321fc4dc8 [Mem2Reg] NFC: Improved function name.
Previously there were the functions `promoteSingleAllocation` and
`removeSingleBlockAllocation`.  The distinction between these based on
the name alone is a bit subtle.  Shortened the name of
`promoteSingleAllocation` to `promoteAllocation`.
2023-07-26 21:27:35 -07:00
Evan Wilde
0f38040832 [NFC] Update more APIs for std::optional
`std::optional` doesn't have `hasValue` or `getPointer`. Using the
implicit decay to boolean, and grabbing the pointer to the element by
expanding the optional and grabbing the reference address.
2023-07-25 12:28:28 -07:00
Evan Wilde
fd156f2717 More APInt API updates
Updating more APInt and bit-manipulation API changes.
2023-07-25 12:28:27 -07:00
Evan Wilde
309aed4925 Add SmallSetVector replacement
llvm::SmallSetVector changed semantics
(https://reviews.llvm.org/D152497) resulting in build failures in Swift.
The old semantics allowed usage of types that did not have an
`operator==` because `SmallDenseSet` uses `DenseSetInfo<T>::isEqual` to
determine equality. The new implementation switched to using
`std::find`, which internally uses `operator==`. This type is used
pretty frequently with `swift::Type`, which intentionally deletes
`operator==` as it is not the canonical type and therefore cannot be
compared in normal circumstances.

This patch adds a new type-alias to the Swift namespace that provides
the old semantic behavior for `SmallSetVector`. I've also gone through
and replaced usages of `llvm::SmallSetVector` with the
`Swift::SmallSetVector` in places where we're storing a type that
doesn't implement or explicitly deletes `operator==`. The changes to
`llvm::SmallSetVector` should improve compile-time performance, so I
left the `llvm::SmallSetVector` where possible.
2023-07-25 12:28:27 -07:00
Erik Eckstein
4d20423e00 Optimizer: re-implement the RedundantLoadElimination pass in Swift
The new implementation has several benefits compared to the old C++ implementation:

* It is significantly simpler. It optimizes each load separately instead of all at once with bit-field based dataflow.
* It's using alias analysis more accurately which enables more loads to be optimized
* It avoids inserting additional copies in OSSA

The algorithm is a data flow analysis which starts at the original load and searches for preceding stores or loads by following the control flow in backward direction.
The preceding stores and loads provide the "available values" with which the original load can be replaced.
2023-07-21 07:19:56 +02:00
Evan Wilde
83b044f5fb Migrating LLVM API usage on main
This patch migrates the compiler off of the deprecated LLVM APIs where I
can.

 - APInt::getAllOnesValue -> APInt::getAllOnes
 - APInt::getNullValue -> APInt::getZero
 - APInt::isNullValue -> APInt::isZero
 - APInt::getMinSignedBits -> APInt::getSignificantBits
 - clang::Module::submodule_{begin,end} -> clang::Module::submodules
2023-07-13 11:22:35 -07:00
nate-chandler
f423115b71 Merge pull request #67218 from nate-chandler/rdar112078094
[AllocBoxToStack] Computing partial_apply frontier looks thru copies+moves.
2023-07-12 07:11:20 -07:00
Nate Chandler
6cf2bb4151 [AllocBoxToStack] PAI frontier looks thru moves.
When determining where to destroy a captured alloc_box, the frontier of
the capturing partial_apply is computed.  Previously, that computation
just used the uses of the partial_apply.  If the partial_apply were
moved and the original destroyed before the apply, however, the result
would be a miscompile where the destroy_addr/dealloc_stack for the
alloc_stack to which the alloc_box was promoted was destroyed before the
apply of the closure which used the address.
2023-07-11 16:27:47 -07:00
Nate Chandler
15ce42ae09 [AllocBoxToStack] PAI frontier looks thru copies.
When determining where to destroy a captured alloc_box, the frontier of
the capturing partial_apply is computed.  Previously, that computation
just used the uses of the partial_apply.  If the partial_apply were
copied and the original destroyed before the apply, however, the result
would be a miscompile where the destroy_addr/dealloc_stack for the
alloc_stack to which the alloc_box was promoted was destroyed before the
apply of the closure which used the address.
2023-07-11 16:27:27 -07:00
Joe Groff
3d5285be6f Arrange for closure bodies promoted by AllocBoxToStack to have their originals removed by MoveOnlyChecker.
This is an improvement of #67031 which avoids deleting the closure function
body during AllocBoxToStack, which still breaks pass invariants by modifying
functions other than the currently-analyzed function. As a function pass,
AllocBoxToStack also doesn't really know with certainty whether the original
closure function is unused after stack promotion or not. We still want to
eliminate the original when it may contain invalid SIL for move-only values
that rely on the escape analysis for correct semantics, so rather than mark the
original function to be *ignored* during move-only checking, mark it to be
*deleted* by move-only checking if the function is in fact unused at that
point.

If the marked function is still used, we let it pass through move-only
checking normally, which may cause redundant diagnostics but is the right
thing to do since code is still potentially using the closure with escaping
semantics. We should rearrange things to make this situation impossible in
the future.

rdar://110675352
2023-07-10 15:18:16 -07:00
Joe Groff
8d974212f1 Revert "AllocBoxToStack: Remove bodies of closure functions left unused after specialization."
This reverts commit bd5f0a7af0.
2023-07-10 12:43:21 -07:00
Erik Eckstein
baaf5565b0 Optimizer: reimplement DeadStoreElimination in swift
The old C++ pass didn't catch a few cases.
Also:
* The new pass is significantly simpler: it doesn't perform dataflow for _all_ memory locations at once using bitfields, but handles each store separately. (In both implementations there is a complexity limit in place to avoid quadratic complexity)
* The new pass works with OSSA
2023-07-05 21:33:25 +02:00