Commit Graph

170 Commits

Author SHA1 Message Date
Andrew Trick
f29bb44d07 OwnershipUseVisitor::visitInnerBorrowUses: support dependent values
Add liveness support for dependent values: borrowed-from & mark_dependence so
they aren't reported as unknown uses.

Centralize the logic in BorrowingOperand::getScopeIntroducingUserResult
and BorrowingOperand::getDependentUserResult().
2025-02-25 23:08:55 -08:00
Andrew Trick
e78aa38204 Remove BorrowingOperand.getImplicitUses.
It is buggy/misleading because of dead-def handling. The current operand may be
re-pushed if it is a dead borrow scope.
2025-02-25 23:08:54 -08:00
Andrew Trick
b3a445fb55 Fix BorrowingOperand::getBorrowIntroducingUserResult()
To handle borrowing operands that produce a dependence value but do not create a
nested borrow scope. This includes non-reborrow borrowed-from and guaranteed
mark_dependence [nonescaping].
2025-02-25 23:08:54 -08:00
Andrew Trick
f1792d80b3 Fix BorrowingOperand.visitScopeEndingUses
Only return false if the visitor returns false. Clients were ignoring the
result.

If the BorrowingOperand does not create a borrow scope, call visitUnknownUse
instead.

Until we have complete lifetimes, to avoid breaking code that cannot handle dead
defs, consider a dead borrow scope to be an unknown use.
2025-02-25 23:08:54 -08:00
Joe Groff
1e37b67a96 BorrowToDestructureTransform: Fix order of operations when placing borrows around InteriorPointerOperands.
The code here determined the borrow scope of an InteriorPointerOperand use of a borrow using
`visitBaseValueScopeEndingUses`, but it does so after rewriting the operand, so the base
value would sometimes be incorrect leading to missing `end_borrows` in the rewritten code.
Fixes rdar://133333278.
2025-02-04 18:43:40 -08:00
Meghana Gupta
27fc324c8c Fix AddressOwnership for unidentified access
AccessBase can have Unindentified kind where the base value maybe
invalid.

Bailout early for such accesses in ownership rauw.
2025-01-28 09:08:38 -08:00
Meghana Gupta
79b649854b Fix operand ownership of mark_dependence [nonescaping] of address values 2025-01-24 00:49:32 -08: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
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
83224b255e SIL: automatically set the reborrow flags for phi-arguments when creating an end_borrow or setting the operand of an end_borrow
This is done for all transitively incoming phi-arguments of an end_borrow operand.
2024-11-12 09:26:58 +01:00
Nate Chandler
29d046fa42 [SIL] Add BorrowedValueKind::BeginApplyToken.
The token is a guaranteed value introduced by the begin_apply and
consumed by its end_apply, abort_apply, and end_borrow users.
2024-04-25 17:03:43 -07:00
Erik Eckstein
e14c1d1f62 SIL, Optimizer: update and handle borrowed-from instructions
Compute, update and handle borrowed-from instruction in various utilities and passes.
Also, used borrowed-from to simplify `gatherBorrowIntroducers` and `gatherEnclosingValues`.
Replace those utilities by `Value.getBorrowIntroducers` and `Value.getEnclosingValues`, which return a lazily computed Sequence of borrowed/enclosing values.
2024-04-10 13:38:10 +02:00
Erik Eckstein
ac4bc89c9a SIL: add the borrowed-from instruction.
It declares from which enclosing values a guaranteed phi argument is borrowed from.
2024-04-10 13:38:10 +02:00
Andrew Trick
d7b9149ee5 Fix visitNonEscapingLifetimeEnds to handle mark_dependence uses
Now it visits unknown uses separately rather than asserting.
2024-03-22 14:29:57 -07:00
Andrew Trick
37171e698a Handle mark_dependence [nonescaping] like a borrowing instruction. 2024-01-22 23:57:03 -08:00
Joe Groff
1dcf271fe0 SIL: Treat store_borrow as borrowing its source, and have the move-only checker account for borrow scopes.
When rewriting uses of a noncopyable value, the move-only checker failed to take into account
the scope of borrowing uses when establishing the final lifetimes of values. One way this
manifested was when borrowed values get reabstracted from value to in-memory representations,
using a store_borrow instruction, the lifetime of the original borrow would be ended immediately
after the store_borrow begins rather than after the matching end_borrow. Fix this by, first,
changing `store_borrow` to be treated as a borrowing use of its source rather than an
interior-pointer use; this should be more accurate overall since `store_borrow` borrows the
entire source value for a well-scoped duration balanced by `end_borrow` instructions. That done,
change MoveOnlyBorrowToDestructureUtils so that when it sees a borrow use, it ends the borrow
at the end(s) of the use's borrow scope, instead of immediately after the beginning of the use.
2023-10-16 09:12:25 -07:00
Michael Gottesman
29350907fc Convert TransitiveAddressWalker to use CRTP instead of virtual functions. 2023-08-10 12:58:50 -07:00
Nate Chandler
4ea0155b06 [OwnershipUtils] Gardening: De-typo'd. 2023-07-19 08:00:08 -07:00
Nate Chandler
0675454267 [OwnershipUtils] NFC: Deleted dead declaration.
The two implementations of findPointerEscape were merged back in
https://github.com/apple/swift/pull/66723, so delete the declaration.
2023-07-11 08:23:10 -07:00
Meghana Gupta
28df449d38 Merge pull request #66723 from meg-gupta/improvefindpointerescape
Refactor swift::findPointerEscape and handle additional cases
2023-06-21 21:04:21 -07:00
Meghana Gupta
031255c2d8 Rename hasPointerEscape -> findPointerEscape 2023-06-20 21:13:55 -07:00
Meghana Gupta
7cb3733e29 Rename OwnershipForwardingMixin -> ForwardingInstruction 2023-06-14 10:40:32 -07:00
Meghana Gupta
1dc713e2f7 Add new flags "reborrow" and "escaping" to SILArgument.
"reborrow" flag on the SILArgument avoids transitive walk over the phi operandsi
to determine if it is a reborrow in multiple utilities.
SIL transforms must keep the flag up-to-date by calling SILArgument::setReborrow.
SILVerifier checks to ensure the flag is not invalidated.

Currently "escaping" is not used anywhere.
2023-05-11 12:31:37 -07:00
nate-chandler
2984f0a236 Merge pull request #65417 from nate-chandler/rdar108514447
[SemanticARCOpts] Promote moved load [copy]s to load_borrows.
2023-04-27 06:59:02 -07:00
Nate Chandler
b686e81085 [OwnershipUtils] Add move as OwnedValueIntroducer. 2023-04-25 11:37:30 -07:00
Michael Gottesman
c577055100 [move-only] Instead of using AccessUseVisitor to visit addresses use TransitiveAddressVisitor.
This makes it so that the move address checker is not dependent on starting the
traversal at a base object. I also included verifier checks that the API can
visit all address uses for:

1. project_box.
2. alloc_stack.
3. ref_element_addr.
4. ref_tail_addr.
5. global_addr_inst.

this is because this visitor is now apart of the SIL API definition as being
able to enumerate /all/ addresses derived from a specific chosen address value.

This is a refactoring NFCI change.

rdar://108510644
2023-04-25 10:51:03 -07:00
nate-chandler
28bbbd5614 Merge pull request #65114 from nate-chandler/nfc/20230412/2/delete-duplicate-api
[SIL] NFC: Removed visitBorrowIntroducingUserResults.
2023-04-24 15:20:16 -07:00
Nate Chandler
6ad9581bd6 [SIL] NFC: Clarified doc comment. 2023-04-24 10:38:11 -07:00
Meghana Gupta
264d86d26c Implement InteriorLivenessVisitor::handleInnerBorrow for all BorrowingOperand kinds
Not all BorrowingOperand introduce a BorrowedValue. Add a new api to get the defining value
for use in InteriorLivenessVisitor::handleInnerBorrow.
2023-04-12 14:42:23 -07:00
Nate Chandler
b1ccb59f01 [SIL] NFC: Removed visitBorrowIntroUserResults.
It's equivalent to getBorrowIntroducingUserResult except that it's less
convenient to use.  There's only ever one result, so there's no need for
a visitor.  Updated all users to call getBorrowIntroducingUserResult
instead.
2023-04-12 13:41:42 -07:00
Nate Chandler
0fceb33905 [SIL] NFC: Marked function const.
Make it available on const instances.
2023-04-12 13:29:05 -07:00
Nate Chandler
6f5114ef68 [OwnershipUtils] Classify more moves as redundant.
Moves from limited use values are redundant.  When a move separates a
non-escaping lifetime from an escaping lifetime, it is still redundant
if the original lifetime couldn't be optimized because it's already as
small as possible.
2023-03-16 18:22:58 -07:00
Nate Chandler
ceb941d1d1 [NFC] Extracted isRedundantMoveValue.
Extracted the predicate from inline in RedundantMoveValueElimination
into the new function which can now be called from elsewhere
(CopyPropagation).
2023-03-15 07:38:59 -07:00
Nate Chandler
af1ac900f9 [SIL] Add hasPointerEscape(SILValue).
There is a preexisting function with this name that takes a
BorrowedValue.  The new function calls that preexisting function if a
BorrowedValue can be constructed from the SILValue.  Otherwise, it looks
for direct uses of the value which qualify as "pointer escapes".
2023-03-10 10:49:53 -08:00
Joe Groff
69e4b95fb8 SIL: Model noescape partial_applys with ownership in OSSA.
Although nonescaping closures are representationally trivial pointers to their
on-stack context, it is useful to model them as borrowing their captures, which
allows for checking correct use of move-only values across the closure, and
lets us model the lifetime dependence between a closure and its captures without
an ad-hoc web of `mark_dependence` instructions.

During ownership elimination, We eliminate copy/destroy_value instructions and
end the partial_apply's lifetime with an explicit dealloc_stack as before,
for compatibility with existing IRGen and non-OSSA aware passes.
2023-02-16 21:43:53 -08:00
Andrew Trick
13e1aa4467 Add OwnershipLiveness utilities
Encapsulate all the complexity of reborrows and guaranteed phi in 3
ownership liveness interfaces:

LinerLiveness, InteriorLiveness, and ExtendedLiveness.
2023-02-10 09:39:18 -08:00
Andrew Trick
4224d940ec Add visitInnerAdjacentPhis OSSA helper
This API is the inverse of visitEnclosingDefs when called on a phi.

This replaces the visitAdjacentReborrowsOfPhi algorithm with a small
loop that simply checks all the phis in the current block.

This should all be fairly efficient once SILArgument has a "reborrow"
flag.
2023-02-06 23:11:23 -08:00
Andrew Trick
0a9484597f Add findEnclosingDefs and findBorrowIntroducers utilities.
These APIs are essential for complete OSSA liveness analysis.  The
existing ad-hoc OSSA logic always misses some of the cases handled by
these new utilities. We need to start replacing that ad-hoc logic with
new utilities built on top of these APIs to define away potential
latent bugs.

Add FIXMEs to the inverse API: visitAdjacentBorrowsOfPhi. It should
probably be redesigned in terms of these new APIs.
2023-01-03 09:33:39 -08:00
Andrew Trick
c2ae7e0359 Add visitForwardedGuaranteedOperands utility.
Factors a mess of code in MemAccessUtils to handle forwarding
instruction types into a simpler utility. This utility is also needed
for ownership APIs, which need to be extended to handle these cases.
2023-01-03 09:33:39 -08:00
Meghana Gupta
b10563909c Update isGuaranteedForwarding api and delete isGuaranteedForwardingPhi api 2022-12-13 12:51:25 -08:00
Meghana Gupta
79496ab513 Return BorrowedValueKind::Invalid for GuaranteedForwardingPhi 2022-10-21 11:46:31 -07:00
Meghana Gupta
ebd7a90587 NFC: Remove unused code + reorg 2022-10-19 19:54:28 -07:00
Meghana Gupta
786eb94853 Support @guaranteed forwarding phis 2022-10-19 19:54:27 -07:00
Meghana Gupta
b1f719709b Rename ForwardingBorrow -> GuaranteedForwarding 2022-10-19 19:54:27 -07:00
Andrew Trick
eee184ae4e Delete isValueAddressOrTrivial
If we want a short-circuit test for address types, that should be done
within getOwnershipKind() itself.
2022-10-05 06:30:02 -07:00
Andrew Trick
0fbca762fa Decorative comments in OwnershipUtils 2022-10-05 06:30:02 -07:00
Andrew Trick
9e0b615593 Add a comment on liveness terminology 2022-10-04 13:27:48 -07:00
Andrew Trick
6346bf55c5 Rename areUsesWithinTransitiveScope to ...ExtendedScope
Start using consistent terminolfy in ownership utils.

A transitive use set follows transitive uses within an ownership
lifetime. It does not rely on complete inner scopes. An extended use
set is not necessarilly transitive but does look across
lifetime-ending uses: copies of owned values and/or reborrows of
guaranteed values. Whether lifetime extension refers to copies or
reborrow is context dependent.
2022-10-04 13:27:48 -07:00
Andrew Trick
da4f1b9a70 Add InnerBorrowKind and AddressUseKind to PrunedLiveness API
The API for computing simple liveness now returns a
SimpleLiveRangeSummary. Callers need to decide how to handle reborrows
and pointer escapes. If either condition exists then the resulting
liveness does not necessarily encapsulate the definition's ownership.

Fixes some number of latent bugs w.r.t. liveness clients.
2022-10-04 13:27:47 -07:00
Andrew Trick
40e03ef782 Update passes to use SSAPrunedLiveness or MultiDefPrunedLiveness 2022-10-04 13:27:47 -07:00