Commit Graph

196 Commits

Author SHA1 Message Date
Meghana Gupta
43b20ca3d8 Remove special case for MoveOnlyWrapperToCopyableValueInst and MoveOnlyWrapperToCopyableBoxInst 2023-06-14 12:10:21 -07:00
Meghana Gupta
0e27cd18d7 Remove FirstArgOwnershipForwardingSingleValueInst, GuaranteedFirstArgForwardingSingleValueInst, OwnedFirstArgForwardingSingleValueInst, AllArgForwardingSingleValueInst
Replace their uses with OwnershipForwardingSingleValueInstruction.
2023-06-14 12:04:58 -07:00
Meghana Gupta
7cb3733e29 Rename OwnershipForwardingMixin -> ForwardingInstruction 2023-06-14 10:40:32 -07:00
Michael Gottesman
70ab38d9da [move-only] Add a new instruction: moveonlywrapper_to_copyable_box.
I am going to use this to unwrap ${ @moveOnly T } so that I can pass it to
partial_apply that expect a ${ T }
2023-06-06 14:26:22 -04: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
c40f7c3d0a [OwnershipUtils] Undef isn't introduced/enclosed.
Previously, calling `visitEnclosingDefs` or `visitBorrowIntroducers`
would crash if the `SILValue` argument were an instance of `SILUndef`
because both instantiate a `::FindEnclosingDefs` with the the result of
of calling `getFunction()` on that argument and `::FindEnclosingDefs` in
turn uses that result to create a `ValueSet`; but
`SILUndef::getFunction` returns `nullptr`, which is illegal to use as
the function for a `ValueSet`(or any other data structure relying on
`NodeBits`).

If the function specified as a separate argument, no values would be
visited and `true` would be returned.  Instead of burdening the API with
a separate argument, check for `SILUndef` up front and return `true`.
2023-04-19 13:57:59 -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
6a5330b1d7 [SIL] NFC: Duplicated assertions.
Added the assertions from visitBorrowIntroducingUserResults to
getBorrowIntroducingUserResult.  In preparation for deleting the former
in favor of the latter.
2023-04-12 13:41:31 -07:00
Nate Chandler
0fceb33905 [SIL] NFC: Marked function const.
Make it available on const instances.
2023-04-12 13:29:05 -07:00
swift-ci
9807267e02 Merge pull request #64708 from meg-gupta/fixinfinite
Fix infinite loop in the new hasPointerEscape(SILValue) api
2023-03-29 01:27:10 -07:00
Meghana Gupta
84d643fc85 Fix infinite loop in the new hasPointerEscape(SILValue) api 2023-03-28 20:47:27 -07:00
Meghana Gupta
64d344ab07 Support all BorrowedValueKind in hasPointerEscape(BorrowedValue) api 2023-03-28 20:31:27 -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
b7f447da6c [NFC] Readability for isRedundantMoveValue.
Improved comments and flattened conditions.
2023-03-21 07:57:43 -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
Michael Gottesman
a24d46097f [sil] Teach the various address verifiers about ExplicitCopyAddrInst.
Just treating it the same as copy_addr as expected.
2023-02-17 16:04:47 -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
30f59c7367 Remove an assert from visitForwardedGuaranteedOperands
Some guaranteed forwarding instructions have multiple operands:
mark_dependence, ref_to_bridge_object.

The corresponding instruction types checked here already have
documentation that the forwarded operand is the first operand. The
assert is overly cautious, and checking for indiviudal opcodes would be
tedious maintenance.
2023-02-13 18:04:39 -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
403c37a680 Tweak findTransitiveUsesForAddress implementation
Record uses even when they escape a pointer. This is more consistent
and helps avoids corner cases involving dead address producers.
2023-02-06 11:16:18 -08:00
nate-chandler
2fc7659ed7 Merge pull request #60670 from nate-chandler/lexical_lifetimes/owned_arguments
[SIL] Maintain owned argument lifetimes at inlining.
2023-01-26 18:30:24 -08:00
Michael Gottesman
89176928ee Merge pull request #63213 from gottesmm/rdar103271138
[move-only] Implement borrow+struct_extract to restructure transform
2023-01-26 07:19:10 -08:00
Nate Chandler
4422cdcc06 [OwnershipUtils] Match complex nested borrows.
Recognize lexical borrows as nested when their borrowee's guaranteed
reference roots are all lexical borrows.

Addresses the following regressions

Breadcrumbs.MutatedUTF16ToIdx.Mixed         188     882     +369.1%   **0.21x**
Breadcrumbs.MutatedIdxToUTF16.Mixed         230     926     +302.6%   **0.25x**

seen when enabling lexical lifetimes in the standard library.
2023-01-25 11:39:42 -08:00
Andrew Trick
7990dda02b Cleanup PrunedLiveness interface.
In preparation for adding OwnershipLiveness.

Rename Simple LiveRangeSummary to LiveRangeSummary.

Add initializeDefNode helpers to avoid confusion about the argument
type.

Add defBegin/defEnd iterators in MultiDefPrunedLiveness.
2023-01-24 23:26:40 -08:00
Michael Gottesman
e15eab5020 [ownership] Change ForwardingOperand::visitForwardingValues to handle ForwardingOwned terminators that are function exiting correctly.
Previously, we would have just asserted when we passed the operand to
PhiOperand on the next line of the code.
2023-01-24 16:55:54 -08:00
Andrew Trick
599b1d1ae4 Handle guaranteed phis conservatively in a few more places. 2023-01-13 08:55:16 -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
e2c53dd469 Merge pull request #62570 from meg-gupta/fixossaapi
[OSSA] Minor fixes to swift::findTransitiveUsesForAddress
2022-12-15 10:30:17 -08:00
Meghana Gupta
9360b7bfdb Record debug_value and end_borrow as leaf uses in swift::findTransitiveUsesForAddress
Otherwise the default is AddressUseKind::Unknown
2022-12-13 22:18:14 -08:00
Meghana Gupta
208298ed3e Fix findTransitiveUsesForAddress when foundUses vector is nullptr
The api should not behave differently when foundUses is nullptr.
The only difference should be in populating it.

No tests. Found by code inspection.
2022-12-13 22:18:08 -08:00
Meghana Gupta
511739b494 Delete OperandOwnership::GuaranteedForwardingPhi
Use OperandOwnership::GuaranteedForwarding instead.
2022-12-13 12:51:31 -08:00
Meghana Gupta
b10563909c Update isGuaranteedForwarding api and delete isGuaranteedForwardingPhi api 2022-12-13 12:51:25 -08:00
Andrew Trick
f9861ec9c0 Add APIs for terminator results that forward ownership.
Add TermInst::forwardedOperand.

Add SILArgument::forwardedTerminatorResultOperand. This API will be
moved into a proper TerminatorResult abstraction.

Remove getSingleTerminatorOperand, which could be misused because it's
not necessarilly forwarding ownership.

Remove the isTransformationTerminator API, which is not useful or well
defined.

Rewrite several instances of complex logic to handle block arguments
with the simple terminator result API. This defines away potential
bugs where we don't detect casts that perform implicit conversion.

Replace uses of the SILPhiArgument type and code that explicitly
handle block arguments. Control flow is irrelevant in these
situations. SILPhiArgument needs to be deleted ASAP. Instead, use
simple APIs like SILArgument::isTerminatorResult(). Eventually this
will be replaced by a TerminatorResult type.
2022-12-12 12:37:35 -08: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
6861b318ca Make PrunedLiveness::computeSimple one-level transitive
Computing simple liveness is distinct from computing transitive
liveness. But for safety and consistency, always handle the first
level of liveness transitively. This way, computeSimple can be used on
guaranteed values that need OSSA lifetime fixup.

Simple liveness just means that *inner* borrow and address scopes are
assumed to be complete.

This utility is still only used conservatively because OSSA lifetime
completion is not yet enabled. But, in the future, computeSimple can
be used in the common case.
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
4ef7f8590f Fix findTransitiveUsesForAddress to handle incomplete scopes
This fixes ScopedAddressValue::computeLiveness in unreachable code scenarios.
For example:

   %storeBorrow = store_borrow %_ to %adr
   %loadBorrow  = load_borrow %storeBorrow
   apply %f(%loadBorrow) : $@convention(thin) (...) -> Never

Now recursively process uses of load_borrow as if they are address
uses.

Ultimately, this would be more efficiently handled by a recursive
lifetime completion utility which would fixup the load_borrow scope
before computing the store_borrow liveness.

Fixes rdar://99874173: unreachable assert "No user in LiveWithin block"
2022-10-04 13:27:47 -07:00