Commit Graph

68 Commits

Author SHA1 Message Date
Kavon Farvardin
4a943d464d sil: provide ability to run CopyPropagation in -Onone
This does not enable it by default. Use either of the flags:

```
-enable-copy-propagation
-enable-copy-propagation=always
```

to enable it in -Onone. The previous frontend flag
`-enable-copy-propagation=true` has been renamed to
`-enable-copy-propagation=optimizing`, which is currently default.

rdar://107610971
2025-09-19 16:23:19 -07:00
Nate Chandler
5da38cb04b [NFC] OSSACanonicalizeGuaranteed: Renamed. 2025-08-18 09:45:27 -07:00
Nate Chandler
8f1d6616af [NFC] OSSACanonicalizeOwned: Renamed. 2025-08-18 09:45:21 -07:00
Nate Chandler
6e2f09f225 [NFC] CopyPropagation: Add subpass bailouts.
Before each transformation is done, check whether to continue with the
next subpass.
2025-01-16 08:18:29 -08:00
Nate Chandler
8ace8b9ef9 [NFC] CopyPropagation: Extract transformation.
Separate the propagation of copies from the setup and cleanup.
Facilitates bailing after completing N subpasses.
2025-01-16 08:18:29 -08:00
Erik Eckstein
51e3e5ed80 Optimizer: rename BorrowArgumentsUpdater -> GuaranteedPhiUpdater
NFC
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
Erik Eckstein
80a0da9615 Optimizer: update borrowed-from instructions in some passes
Fixes verifier crashes (once the borrowed-from verifier is fixed).
2024-08-07 18:02:38 +02:00
Nate Chandler
4a397cc018 [NFC] OwnedLifetimeCan: Take DeadEndBlocksAnalysis
All clients of OwnedLifetimeCanonicalization pass an instance of the
analysis in.  For now, it's unused.
2024-07-22 21:51:28 -07:00
Nate Chandler
b5e4fcf100 [NFC] CopyPropagation: Extracted verification.
It makes it easier to temporarily hack in new calls to verify.
2024-07-22 20:35:30 -07:00
Nate Chandler
d55c169d80 [CopyPropagation] Destroy hoist -> canonicalize.
Replace usages of lexical destroy hoisting with owned value
canonicalization now that the latter supports lexical values.
2024-07-22 20:35:30 -07:00
Tim Kientzle
1d961ba22d Add #include "swift/Basic/Assertions.h" to a lot of source files
Although I don't plan to bring over new assertions wholesale
into the current qualification branch, it's entirely possible
that various minor changes in main will use the new assertions;
having this basic support in the release branch will simplify that.
(This is why I'm adding the includes as a separate pass from
rewriting the individual assertions)
2024-06-05 19:37:30 -07:00
Nate Chandler
aa3cbe0c67 [NFC] OwnedValueCan: Typed maximizeLifetime. 2024-06-03 15:45:23 -07:00
Nate Chandler
df008924da [NFC] OwnedValueCan: Typed pruneDebugMode. 2024-06-03 15:45:20 -07:00
Nate Chandler
7fd4311d7d [SIL] Verify complete lifetimes when option set.
Only pass DeadEndBlocks to verifyOwnership if OSSAVerifyComplete is not
set.  The verifier keys off of the nullness of DeadEndBlocks to decide
whether to verify complete lifetimes or not.
2024-04-24 12:59:45 -07:00
Nate Chandler
bb3dc295d4 [Gardening] Tweaked comment. 2023-12-14 13:35:26 -08:00
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
Nate Chandler
4f0d1dab5e [CopyPropagation] Canonicalize copies of lexical.
Currently, CopyPropagation only canonicalizes defs that are "canonical",
that is, the root of the copy_value tree.  When that canonical
def is lexical, however, the canonicalization respects deinit barriers.
But copies of lexical values are not themselves lexical, so their
lifetimes can be shortened without respect to deinit barriers.

Here, immediate copies of lexical values are canonicalized before the
lexical values themselves are.

rdar://107197935
2023-03-27 07:08:01 -07:00
Nate Chandler
ec3f005f31 [CanonOSSALifetime] Run on lexical lifetimes.
Previously, the utility bailed out on lexical lifetimes because it
didn't respect deinit barriers.  Here, deinit barriers are found and
added to liveness if the value is lexical.  This enables copies to be
propagated without hoisting destroys over deinit barriers.

rdar://104630103
2023-03-25 21:17:26 -07:00
Andrew Trick
119e712c32 Merge pull request #64534 from atrick/liveblocks-bitfield
Cleanup PrunedLiveBlocks
2023-03-22 16:33:08 -07:00
Andrew Trick
15796e3ff9 PrunedLiveness: add a SILFunction argument
So that liveness can migrate to using a SILBitfield.
2023-03-22 01:36:48 -07:00
Nate Chandler
6b1bee7324 [CopyPropagation] Canonicalize moved-from values.
If a move_value is determined to be redundant and removed, take the
opportunity its removal makes available to canonicalize the moved-from
value without the obstruction of the move_value.
2023-03-20 18:04:57 -07:00
nate-chandler
3644d98d5f Merge pull request #64400 from nate-chandler/copy-propagation/run-side-effect-analysis-first
CopyPropagation: Compute side-effects first.
2023-03-16 18:17:39 -07:00
Nate Chandler
ade29db6bd CopyPropagation: Compute side-effects first.
Add a run of ComputeSideEffects before the first run of CopyPropagation.
Allow hoisting over applies of functions that are able to be analyzed
not to be deinit barriers at this early point.
2023-03-16 13:01:05 -07:00
Nate Chandler
16216924b1 [CopyPropagation] Eliminate redundant moves.
SemanticARCOpts already eliminates move values that are redundant that
block its optimizations.  But it's always run after CopyPropagation.

Because move_values divide copy-extended lifetimes, move_values obstruct
lifetime canonicalization.  If a move_value isn't separating lifetimes
with different characteristics (specifically: lexicallity, escaping),
then it is only obstructing lifetime canonicalization.  Remove it
before canonicalizing the lifetime of the moved-from value.
2023-03-15 11:56:27 -07:00
Nate Chandler
e9294539f0 [NFC] Switched to StackList.
No need for a worklist of any sort, a simple list suffices.
2023-03-15 07:38:59 -07:00
Nate Chandler
4b85d0a9ca [DestroyHoisting] Barriers use callee analysis.
Pass a BasicCalleeAnalysis instance to isDeinitBarrier.  This enables
LexicalDestroyHoisting to hoist destroys over applies of functions which
are not deinit barriers.
2022-10-18 21:23:22 -07:00
Nate Chandler
f85074d1ba [ShrinkBorrowScope] Barriers use callee analysis.
Pass a BasicCalleeAnalysis instance to isDeinitBarrier.  This will allow
ShrinkBorrowScope to hoist end_borrows over applies of functions which
are not deinit barriers.
2022-10-18 21:23:22 -07:00
Nate Chandler
03253dbf48 [CanonicalizeOSSALifetime] Extend Onone lifetimes.
To improve the debugging experience of values whose lifetimes are
canonicalized without compromising the semantics expressed in the source
language, when canonicalizing OSSA lifetimes at Onone, lengthen
lifetimes as much as possible without incurring copies that would be
eliminated at O.

rdar://99618502
2022-10-08 16:08:35 -07:00
Nate Chandler
79c2f1e693 [Gardening] Tweaked comments. 2022-10-08 15:16:41 -07:00
Nate Chandler
4fc42a63a3 [CanonicalizeOSSALifetime] Renamed file.
Matched to the name of the utility.
2022-10-05 17:07:05 -07:00
Josh Soref
730b16c569 Spelling siloptimizer
* access
* accessed
* accesses
* accessor
* acquiring
* across
* activated
* additive
* address
* addresses'
* aggregated
* analysis
* and
* appropriately
* archetype
* argument
* associated
* availability
* barriers
* because
* been
* beginning
* belongs
* beneficial
* blocks
* borrow
* builtin
* cannot
* canonical
* canonicalize
* clazz
* cleanup
* coalesceable
* coalesced
* comparisons
* completely
* component
* computed
* concrete
* conjunction
* conservatively
* constituent
* construct
* consuming
* containing
* covered
* creates
* critical
* dataflow
* declaration
* defined
* defining
* definition
* deinitialization
* deliberately
* dependencies
* dependent
* deserialized
* destroy
* deterministic
* deterministically
* devirtualizes
* diagnostic
* diagnostics
* differentiation
* disable
* discipline
* dominate
* dominates
* don't
* element
* eliminate
* eliminating
* elimination
* embedded
* encounter
* epilogue
* epsilon
* escape
* escaping
* essential
* evaluating
* evaluation
* evaluator
* executing
* existential
* existentials
* explicit
* expression
* extended
* extension
* extract
* for
* from
* function
* generic
* guarantee
* guaranteed
* happened
* heuristic
* however
* identifiable
* immediately
* implementation
* improper
* include
* infinite
* initialize
* initialized
* initializer
* inside
* instruction
* interference
* interferes
* interleaved
* internal
* intersection
* intractable
* intrinsic
* invalidates
* irreducible
* irrelevant
* language
* lifetime
* literal
* looks
* materialize
* meaning
* mergeable
* might
* mimics
* modification
* modifies
* multiple
* mutating
* necessarily
* necessary
* needsmultiplecopies
* nonetheless
* nothing
* occurred
* occurs
* optimization
* optimizing
* original
* outside
* overflow
* overlapping
* overridden
* owned
* ownership
* parallel
* parameter
* paths
* patterns
* pipeline
* plottable
* possible
* potentially
* practically
* preamble
* precede
* preceding
* predecessor
* preferable
* preparation
* probably
* projection
* properties
* property
* protocol
* reabstraction
* reachable
* recognized
* recursive
* recursively
* redundant
* reentrancy
* referenced
* registry
* reinitialization
* reload
* represent
* requires
* response
* responsible
* retrieving
* returned
* returning
* returns
* rewriting
* rewritten
* sample
* scenarios
* scope
* should
* sideeffects
* similar
* simplify
* simplifycfg
* somewhat
* spaghetti
* specialization
* specializations
* specialized
* specially
* statistically
* substitute
* substitution
* succeeds
* successful
* successfully
* successor
* superfluous
* surprisingly
* suspension
* swift
* targeted
* that
* that our
* the
* therefore
* this
* those
* threshold
* through
* transform
* transformation
* truncated
* ultimate
* unchecked
* uninitialized
* unlikely
* unmanaged
* unoptimized key
* updataflow
* usefulness
* utilities
* villain
* whenever
* writes

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>
2022-10-03 18:31:33 -04:00
Nate Chandler
129dadbe70 [CopyPropagation] Removed poison mode.
The Onone strategy will be to shorten lifetimes as little as possible
while eliminating as many copies as are eliminated at -O.
2022-09-28 17:13:24 -07:00
Michael Gottesman
1e6187c4f4 [sil] Update all usages of old API SILValue::getOwnershipKind() in favor of new ValueBase::getOwnershipKind().
Andy some time ago already created the new API but didn't go through and update
the old occurences. I did that in this PR and then deprecated the old API. The
tree is clean, so I could just remove it, but I decided to be nicer to
downstream people by deprecating it first.
2022-07-26 11:46:23 -07:00
Nate Chandler
50a0cfa406 [CopyPropagation] Canonicalize all values.
Previously, the lifetimes of only values which were copied were
canonicalized during the non-mandatory CopyPropagation pass.  The
mandatory pass (i.e. MandatoryCopyPropagation) has been disabled,
meaning that non-copied values lifetimes were  never canonicalized.  So
enable canonicalization of all lifetimes during regular CopyPropagation.

rdar://54335055
2022-07-22 08:17:13 -07:00
Nate Chandler
9492619319 [CopyPropagation] Delete dead copy_values.
When canonicalizing an owned value's lifetime, also check whether the
value is dead.  If it is, track it for deletion.  In particular, this
eliminates dead copy_values.
2022-03-23 17:02:59 -07:00
Andrew Trick
4a30118ce1 Fix an assert in canonicalizeFunctionArgument.
Assertion failed: (succeed && "should be filtered by
FindBorrowScopeUses"), function canonicalizeFunctionArgument, file
CanonicalizeBorrowScope.cpp, line 798

Canonicalization for guaranteed function arguments is triggered by
SILCombine without any up-front analysis. Because the canonicalization
rewrites the function argument's copies in place, it must always
succeed.

Fix the visitBorrowScopeUses utility to be aware that it is being
invoked on a function argument and avoid bailing out.
2022-02-18 16:52:30 -08:00
Andrew Trick
7cf9145e91 Disable MandatoryCopyPropagation.
Mandatory copy propagation was primarily a stop-gap until lexcial
lifetimes were implemented. It supposedly made variables lifetimes
more consistent between -O and -Onone builds. Now that lexical
lifetimes are enabled, it is no longer needed for that purpose (and
will never satisfactorily meet that goal anyway).

Mandatory copy propagation may be enabled again later as a -Onone "
optimization. But that requires a more careful audit of the effect on
debug information.

For now, it should be disabled.
2022-02-18 16:52:30 -08:00
Nate Chandler
62dc05fc51 [CopyPropagation] Hoist owned lexical destroys. 2022-02-16 10:31:24 -08:00
Nate Chandler
41fdbc1285 [ShrinkBorrowScope] Adopt BackwardReachability.
Replaced ShrinkBorrowScope's own data flow with the general
BackwardReachability.

Took this opportunity to refactor and document the utility.

Taken together these changes make ShrinkBorrowScope serve as a template
for a future LexicalDestroyHoisting which will operate on owned lexical
values (rather than guaranteed as here) and hoist destroy_values (rather
than end_borrows as here) but should otherwise be quite similar.
2022-02-14 18:38:31 -08:00
Nate Chandler
9e5e935b20 [CopyPropagation] Run LexicalDestroyFolding. 2022-02-04 14:41:37 -08:00
Nate Chandler
1c106eb6e8 [ShrinkBorrowScope] Return modified copy_values.
So that CopyPropagation and other clients can react accordingly, pass
back a list of copy_value instructions that were rewritten by
ShrinkBorrowScope.  In CopyPropagation, add each modified copy to the
copy worklist.
2021-12-19 08:14:45 -08:00
Nate Chandler
214e890e0f [CopyPropagation] Cleanup after borrow shrinking. 2021-12-19 08:14:45 -08:00
Nate Chandler
646dc136fc [CopyPropagation] Note ShrinkBorrowScope changes.
Use the result returned from shrinkgBorrowScope to update the changed
flag.
2021-12-07 17:04:49 -08:00
Nate Chandler
cde250a3e3 [CopyPropagation] Add ShrinkBorrowScope.
During copy propagation (for which -enable-copy-propagation must still
be passed), also try to shrink borrow scopes by hoisting end_borrows
using the newly added ShrinkBorrowScope utility.

Allow end_borrow instructions to be hoisted over instructions that are
not deinit barriers for the value which is borrowed.  Deinit barriers
include uses of the value, loads of memory, loads of weak references
that may be zeroed during deinit, and "synchronization points".

rdar://79149830
2021-12-07 09:43:57 -08:00
Meghana Gupta
6bafc8498d Remove end_lifetime being considered as an end of scope marker (#38851)
OSSA rauw cleans up end of scope markers before rauw'ing.
This can lead to inadvertant deleting of end_lifetime, later
resulting in an ownership verifier error indicating a leak.

This PR stops treating end_lifetime scope ending like end_borrow/end_access.
2021-08-12 13:49:06 -07:00
Andrew Trick
2c6d5afd28 Copy propagation redesign
This rewrites functionality that was mostly disabled but is now ready
to be enabled.

Allow lifetime canonicalization of owned values and function arguments
as a simple stand-alone utility. This is now being called from within
SILCombine, so we should only do the kind of canonicalization that
makes sense in that context.

Canonicalizing other borrow scopes should *not* be invoked as a
single-value cleanup because it affects other lifetimes outside the
borrow scope boundary. It is a somewhat complicated process that
hoists and sinks forwarding instructions and can generate surrounding
compensation code. The copy propagation pass knows how to post-process
the related lifetimes in just the right order. So borrow scope
rewriting should only be done in the copy propagation pass.

Similarly, only do simple canonicalization of owned values and
function arguments at -Onone.

The feature to canoncalize borrow scopes is now ready to be
enabled (-canonical-ossa-rewrite-borrows), but flipping the switch
should be a separate commit. So most of the functionality that was
affected is not exposed by this PR.

Changes:

Split canonicalization of owned lifetimes vs. borrowed lifetimes into
separate utilities. The owned lifetime utility is now back to being
the simple utility that I originally envisioned. So not much happened
to it other than removing complexity.

We now have a separate entry point for finding the starting point for
rewriting borrow scopes:
CanonicalizeBorrowScope::getCanonicalBorrowedDef.

We now have a utility that defines forwarding instructions that we can
treat consistently as part of a guaranteed lifetime,
CanonicalizeBorrowScope::isRewritableOSSAForward.

We now have a utility that defines the uses of a borrowed value that
are considered part of its lifetime,
CanonicalizeBorrowScope::visitBorrowScopeUses. This single utility is
used to implement three different parts of the alogrithm:

1. Find any uses of the borrowed value that need to be propagated
outside the borrow scope

2. RewriteInnerBorrowUses for SILFunction arguments and borrow scopes
with no outer uses.

3. RewriteOuterBorrowUses for borrow scopes with outer uses. Handling
these involves creating new copies outside the borrow scope and
hoisting forwarding instructions.

The end result is that a lot of borrow scopes can be eliminated and
owned values can be forwarded to destructures, reducing copies and
destroys.

If we stop generating borrow scopes for all interior pointers, then
we'll need to design a comparable optimization that works on
"implicit" borrow scopes:

  %ownedDef = ...
  %element struct_extract %ownedDef
  %copy = copy_value %element
  apply(@guaranteed %element)
  apply(@owned %copy)
  destroy %ownedDef

Should be:

  %ownedDef = ...
  %borrowedElement = destructure_struct @guaranteed %ownedDef
  apply(@guaranteed %borrowedElement)
  %ownedElement = destructure_struct %ownedDef
  apply(@owned %copy)
2021-07-01 21:17:27 -07:00
Andrew Trick
340feb41df CanonicalOSSALifetime: rewrite consolidateBorrowScope
It now handles looking through forwarding consumes such as
destructures.

Expected to be NFC since borrow consolidation is still disabled by
default.

TODO: Write unit tests for various forwarding consumes in addition to
destructure.
2021-04-21 14:39:48 -07:00
Andrew Trick
3c045a85d5 CanonicalOSSALifetime: Add convertExtractToDestructure utility
struct_extract and tuple_extract do not belong in OSSA (except to
workaround certain extreme cases). They completely defeat
simplification that OSSA provides for optimizing owned lifetimes.

Copy propagation uses this utility to canonicalize owned values before
canonicalizing their lifetime.
2021-04-19 23:26:16 -07:00
Andrew Trick
0b3027a37e Add destroy_value [poison] support to mandatory copy propagation.
Ensure that any object lifetime that will be shortened ends in
destroy_value [poison], indicating that the reference should not be
dereferenced (e.g. by the debugger) beyond this point.

This has no way of knowing whether the object will actually be
deallocated at this point. It conservatively avoids showing garbage to
debuggers.

Part 1/2: rdar://75012368 (-Onone compiler support for early object
deinitialization with sentinel dead references)
2021-03-12 19:32:55 -08:00