Commit Graph

197 Commits

Author SHA1 Message Date
Nate Chandler
efc9c6df10 [AddressLowering] Consider all local archetypes.
When determining where the "latest opening instruction" is, consider not
just `OpenedArchetypeType`s but any `LocalArchetypeType` which includes
`PackArchetypeType`s.
2023-08-21 13:46:11 -07:00
Nate Chandler
84ab0eb548 [AddressLowering] Handle tuple_pack_extract.
By analogy with tuple_extract, transform the new instruction to
tuple_pack_element_addr.
2023-08-16 17:28:00 -07:00
Nate Chandler
35113d8a80 [AddressLowering] Fix partial_apply arg indexing.
When rewriting arguments, the index used is into the callee's argument
list.  For full applies, that is identical to the index into the
instruction's argument list.  For partial applies, it is not.

Previously, though, the index was used as if it were an index into the
instruction's argument list to get an argument ref.  Here, use instead
the newly added convenience to get the argument ref using the index into
the callee's arguments.
2023-08-15 13:06:32 -07:00
Nate Chandler
5c31339028 [AddressLowering] Loosen assert for partial_apply.
Full apply instructions have the same number of arguments as the callee
has parameters.  Partial apply instructions have some number less than
or equal to the number of callee parameters.
2023-08-15 13:06:32 -07:00
Nate Chandler
16bd80d945 [AddressLowering] Rewrite indirect partial_apply.
In opaque values mode, all arguments are passed to a partial_apply (just
like all other flavors of apply) directly.  AddressLowering needs to
rewrite the operands whose convention is indirect as it does for other
applies.
2023-08-14 10:40:40 -07:00
Nate Chandler
fa98212420 [AddressLowering] Look through opaque result type.
A function which returns a value of opaque result type

    func f() -> some P { S() }

has a lowered, substituted signature

    @convention(thin) () -> @out @_opaqueReturnTypeOf("$s4main1fQryF", 0) opaque

featuring an _opaqueReturnTypeOf attr.  The SILFunctionArgument for that
@out result, however, must be of the type that the opaque result
substitutes to in the expansion context of the function.
2023-08-11 08:10:38 -07:00
Nate Chandler
57c7c9e730 [AddressLowering] Handle unowned copies.
Lower the `strong_copy_unowned_value` and `unowned_copy_value` to
`load_unowned` and `store_unowned` respectively.
2023-08-08 15:49:17 -07:00
Nate Chandler
390d02f114 [AddressLowering] Handle weak copies.
Lower the `strong_copy_weak_value` and `weak_copy_value` to `load_weak`
and `store_weak` respectively.
2023-08-08 15:47:13 -07:00
Nate Chandler
6aa44d83c9 [AddressLowering] Rewrite lifetime-ends last.
Previously, `end_borrow`s were rewritten last in order to be able to
find them when inserting `end_borrow`s on behalf of newly created
`load_borrow`s. Generalize this to rewriting all lifetime-ending users
last.  This is necessary for the lifetime utilities used by `isLoadCopy`
to remain accurate when rewriting a `copy_value` previously determined
to be from a load-copy pair.
2023-08-01 14:51:00 -07:00
Nate Chandler
a27b3909d4 [AddressLowering] Sink projections to uses.
Visit the tree of projections in post-order, sinking each to the lowest
position in the least common ancestor of all uses.
2023-07-26 16:41:57 -07:00
Nate Chandler
604150c194 [AddressLowering] Place projections below types.
Projections may involve opened archetypes.  They must be dominated by
the instructions which open those archetypes.

When determining the earliest storage point for a value and the point at
which to insert its projections, look for such obstructions in the
projection chain.  The first one found is the earliest storage point.
2023-07-26 16:41:57 -07:00
Nate Chandler
c9d84295eb [AddressLowering] Project at storage.
The pass rewrites opaque values in reverse post order of their
definitions.  When an opaque value is rewritten, both its definition and
its uses are rewritten.  When a def or a use is rewritten, in order to
materialize an address with which to rewrite, projection instructions
are created.  Previously, these projections were created at the site of
the def or use.  Some of these projection instructions may be reused when
rewriting later opaque values.

As a result, it's possible to have two opaque values `A` and `B` (which
are rewritten in that order) such that rewriting a use of `A` which
occurs "after" the def (or a use) of `B` creates a projection `P` which
is then used by that "earlier" def (or use) of `B`:

    ```
    A =
    B =     // relies on P
    use B
    use A   // creates some projection P
    ```

When rewriting the def (or that use, respectively) of `B`, the
projection which was created for the use of `A` will be reused.  And
previously, the projection would be created at the use of A.  But that
projection instruction was "after" the place where it is used when
rewriting the def or use of `B`.  That's invalid!

To address this, rather than creating projections at the instruction
being rewritten, instead create them "as early as possible".  The
locations where they will be created are chosen so as to dominate all
uses.
2023-07-26 16:41:57 -07:00
Nate Chandler
de8e1829c3 [AddressLowering] Prevent existential multi-init. 2023-07-25 08:41:07 -07:00
Nate Chandler
5076e7be3c [AddressLowering] NFC: Moved assert above NPE.
Moved the assert that the value which opens an archetype exists before
the call to get its defining instruction which would be an NPE if the
value were in fact null.
2023-07-25 08:40:53 -07:00
Nate Chandler
a23bcef5f3 [AddressLowering] NFC: Extracted function.
Promoted the functionality from createStackAllocation to a named member
AddressLoweringState::getLatestOpeningInst  so that it can be employed
elsewhere.
2023-07-25 08:40:50 -07:00
Nate Chandler
e3428aeb95 [AddressLowering] NFC: Add more dump functions. 2023-07-25 08:40:39 -07:00
Nate Chandler
053cc775a3 [AddressLowering] Gardening: Undefaulted argument.
That there was a default wasn't used anywhere.
2023-07-24 16:57:14 -07:00
Nate Chandler
64ac0bda85 [AddressLowering] Gardening: Removed comment.
Per Andrew Trick, its author.
2023-07-24 16:57:14 -07:00
Nate Chandler
473985ff12 [AddressLowering] Gardening: Detypo'd. 2023-07-24 16:57:14 -07:00
Nate Chandler
c9768e612f [AddressLowering] Gardening: Detypo'd. 2023-07-24 16:57:14 -07:00
Nate Chandler
a1d10997da [SIL] Promoted least-common ancestor to Dominance.
It's a general purpose helper and there will soon be another user.
2023-07-24 16:57:14 -07:00
Nate Chandler
253cd4b055 [AddressLowering] NFC: Removed loop of length 1. 2023-07-20 16:02:01 -07:00
nate-chandler
8cb61d1b04 Merge pull request #67423 from nate-chandler/opaque-values/20230719/2/all_stores_within_guaranteed_argument_lifetime
[AddressLowering] Exit range check for args early.
2023-07-20 06:58:16 -07:00
nate-chandler
b1c5631519 Merge pull request #67384 from nate-chandler/opaque-values/20230718/1/emit_end_borrows/destructure_and_extract
[AddressLowering] End borrow scopes for extracts/destructures at enclosing guaranteed boundary.
2023-07-20 06:57:44 -07:00
Nate Chandler
52dd6a54ec [AddressLowering] Exit range check for args early.
When determining whether a copy_value is part of a copy->store pair, if
the value being copied is guaranteed, it is checked whether the store is
within the lifetime of all its guaranteed roots.  If one of those
guaranteed roots is a function argument, the store is certainly within
the lifetime, so exit early.
2023-07-19 17:34:28 -07:00
Nate Chandler
4c5ce62c7f [AddressLowering] Fix extract's end_borrows.
An extract may have users that have escaping operand ownership.
Consequently, we can't rely on findInnerTransitiveGuaranteedUses as used
by emitEndBorrows.
2023-07-19 16:07:21 -07:00
Nate Chandler
04978e2cd0 [AddressLowering] Fixed destructures' end_borrows.
A destructure may have users that have escaping operand ownership.
Consequently, we can't rely on findInnerTransitiveGuaranteedUses as used
by emitEndBorrows.
2023-07-19 16:06:52 -07:00
Nate Chandler
d98994e64f [AddressLowering] Fix begin_apply's end_borrows.
When a `load_borrow` is created on behalf of a `begin_apply`, it's
possible that it may have escaping users.  When it does,
`findInnerTransitiveGuaranteedUses` returns `false` and the uses it
finds are incomplete.  As a result the boundary computed in
`emitEndBorrows` will be incomplete.
2023-07-19 08:04:01 -07:00
Nate Chandler
d3461e58c0 [AddressLowering] Gardening: De-typo'd. 2023-07-19 08:00:08 -07:00
Nate Chandler
d4c83bce7b [AddressLowering] Handle move_value.
It's handled just like begin_borrow: ignored unless lexical.
2023-07-11 07:14:59 -07:00
Nate Chandler
bb46f74928 [AddressLowering] NFC: Extracted common visitor.
In preparation for supporting move_value which should be treated exactly
the same way as begin_borrow, factored visitBeginBorrow through the new
visitLifetimeIntroducer.
2023-07-11 07:14:59 -07:00
Meghana Gupta
5d401fb70a Remove select_value SIL instruction 2023-06-13 14:13:43 -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
John McCall
d25a8aec8b Add explicit lowering for value packs and pack expansions.
- SILPackType carries whether the elements are stored directly
  in the pack, which we're not currently using in the lowering,
  but it's probably something we'll want in the final ABI.
  Having this also makes it clear that we're doing the right
  thing with substitution and element lowering.  I also toyed
  with making this a scalar type, which made it necessary in
  various places, although eventually I pulled back to the
  design where we always use packs as addresses.

- Pack boundaries are a core ABI concept, so the lowering has
  to wrap parameter pack expansions up as packs.  There are huge
  unimplemented holes here where the abstraction pattern will
  need to tell us how many elements to gather into the pack,
  but a naive approach is good enough to get things off the
  ground.

- Pack conventions are related to the existing parameter and
  result conventions, but they're different on enough grounds
  that they deserve to be separated.
2023-01-29 03:29:06 -05:00
Valeriy Van
55c3759ef6 Merge identical cases of switch 2023-01-25 20:16:13 +02:00
Erik Eckstein
7d8bf37e5e change to the new llvm::Optional APIs
This is a follow-up of https://github.com/apple/swift/pull/62217
2023-01-25 09:18:36 +01:00
Nate Chandler
356f2b78db [AddressLowering] Added TODO. 2023-01-04 07:54:02 -08:00
Nate Chandler
b3ee1fdc34 [AddrLowering] Copy for out-of-range owned stores.
Skip the store-copy optimization if the store instruction occurs outside
the lifetime of the value being copied.
2023-01-04 07:49:37 -08:00
Nate Chandler
2a81238b88 [AddrLowering] Copy for out-of-range guar stores.
When a copy_value's only use is a store, in some cases, as an
optimization, creating a copy_addr can be skipped.  Whether a value is
such a copy_value is determined by the isStoreCopy predicate.

If the operand of the copy_value is a guaranteed value and any of its
guaranteed roots have live ranges which the store is outside of, skip
this optimization and create the copy_addr.
2023-01-04 07:47:54 -08:00
Nate Chandler
1264e6d4fd [NFC] AddressLowering: Promote copy-store check.
Move a cheap check that the one user is a store over the expensive check
done when the value copied has guaranteed ownership.
2023-01-04 07:46:38 -08:00
Nate Chandler
8d17dc43bf [Gardening] Fixed typo. 2023-01-04 07:46:38 -08:00
Nate Chandler
0943693bbe [Gardening] Fix typo. 2023-01-04 07:46:38 -08:00
Nate Chandler
cab17c7ced [NFC] AddressLowering: Reuse old operand index.
Rather than hard-coding an operand number, ask the operand for it.
2023-01-04 07:46:38 -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
Nate Chandler
e23665e1c6 [AddressLowering] Map-into-ctx type for yield.
Previously, the type for the storage into which a loadable value which
was yielded via an indirect convention was obtained from the
SILFunctionConventions and the SILYieldIfo but not mapped into the
context of the current function.  Here, that's fixed.
2022-12-15 13:12:36 -08:00
John McCall
6d4588c40b Change SIL to track all LocalArchetypes and not just OpenedArchetypes. 2022-12-14 19:43:23 -05:00
John McCall
f524f3de69 [NFC] Support instructions that define multiple opened archetypes
I've also fixed this so that it should work on instructions that
define multiple values.  Someday we'll change all the open_existential
instructions to produce different values for the type dependency and
the value result; today is not that day, though.
2022-12-13 13:28:13 -05:00
eeckstein
cb6737269d Merge pull request #62480 from eeckstein/instruction-iteration
SIL: simplify deleting instructions while iterating over instructions.
2022-12-13 10:45:06 +01: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
Erik Eckstein
2ca9a3b9ce SIL: fix APIs for (re)moving instructions
Instructions can only be moved and erased, but never _removed_ from a block.
2022-12-12 19:05:59 +01:00