Commit Graph

42 Commits

Author SHA1 Message Date
Tim Kientzle
f09fb7dfa4 Update a couple of files to pull assertion helpers from the new header 2024-05-16 12:50:23 -07:00
Nate Chandler
d28a12d356 [MoveOnlyAddressChecker] Handle enum deinits.
Previously, the enum representation was fixed to represent the different
cases payloads separately with the unchecked_take_enum_data_addr
instruction consuming all fields but that whose address is obtained.
In a few places, handling for enum deinits was left undone with an
assertion that the enum not have one.

Here, the deinit bit of the enum is shifted to the end.  And the
assertions are replaced with handling.  Finally, the logic for inserting
destroys after switch_enum_addr instructions is fixed.
2024-04-19 12:37:34 -07:00
Joe Groff
3b830bffb5 Merge pull request #72509 from jckarter/fspl-def-edge-generalization
FSPL: Generalize `try_apply` specific condition to handle other hypothetical def-on-edge cases.
2024-03-22 13:49:03 -07:00
Joe Groff
a717c35a2e FSPL: Generalize try_apply specific condition to handle other hypothetical def-on-edge cases. 2024-03-21 15:07:47 -07:00
Nate Chandler
a0a1ff2a04 [MoveOnlyAddressChecker] Fix enum repr.
Change FieldSensitive's enum representation to allow distinguishing
among the elements with associated value.  Consider
`unchecked_take_enum_data_addr` to consume all other fields than that
taken.

rdar://125113258
2024-03-21 10:48:32 -07:00
Nate Chandler
4f6f8d9459 [NFC] MoveOnly: Map operand to list of ranges. 2024-03-20 12:52:57 -07:00
Nate Chandler
2ab2252233 [NFC] MoveOnly: Map inst to array of ranges. 2024-03-20 12:52:57 -07:00
Nate Chandler
76348df0cf [MoveOnlyAddressChecker] Fix repr for consumes.
An instruction can consume multiple (discontiguous) fields.  Use a
SmallBitVector to track the fields consumed by an instruction rather
than a TypeTreeLeafRange.

rdar://125103951
2024-03-20 12:08:48 -07:00
Nate Chandler
7feb7e2675 [NFC] FSPrunedLiveness: Move impl to cpp file.
The function is long and will get longer.
2024-03-20 08:20:16 -07:00
Nate Chandler
234e8bce1d [Gardening] FSPrunedLiveness: Delete dead decl. 2024-03-20 08:20:16 -07:00
Joe Groff
1166f2e0d8 FieldSensitivePrunedLiveness: Handle conditionality of try_apply defs.
A `try_apply` with indirect out arguments is only a def for those arguments on
the success path. Model this by sinking the def-ness of the instruction into the
success branch of the try_apply, and introducing a new `DeadToLiveEdge` mode for
block liveness which stops propagation of use-before-def conditions into the
block that introduced the def. Fixes rdar://118567869.
2024-03-19 22:46:26 -07:00
Joe Groff
e06f811e6a FieldSensitivePrunedLiveness: inject_enum_addr only initializes the tag of enums.
Fixes a crash when optional-chaining a move-only function that produces an
address-only value; FSPL would previously see the apply that initializes the
payload and the `inject_enum_tag` as overlapping defs, leading to mishandling
when trying to maximize the lifetime of the enum.
2024-03-07 16:24:01 -08:00
Nate Chandler
6100a1b495 Allow partial consumption of self in deinit.
Basic support for partial consumption in deinit without full support for
all future uses of drop_deinit.
2024-02-28 15:57:09 -08:00
Ben Barham
ef8825bfe6 Migrate llvm::Optional to std::optional
LLVM has removed llvm::Optional, move over to std::optional. Also
clang-format to fix up all the renamed #includes.
2024-02-21 11:20:06 -08:00
Guillaume Lessard
114f235d17 Merge pull request #71167 from vanvoorden/vanvoorden/inclusive-language
[Inclusive Language][Comments][Documentation] migrate "sanity" checks to "soundness" checks
2024-02-02 10:27:34 -08:00
Nate Chandler
b757b2e99d [FSPrunedLiveness] Return bitfield from isDefBlock 2024-01-29 11:49:30 -08:00
Nate Chandler
22a5a3973d [FSPrunedLiveness] Returned bitfield from isDef 2024-01-29 11:49:29 -08:00
Nate Chandler
330f300c4b [NFC] FSPrunedLiveness: Deduplicated initDef impls
Add a new SILNode taking overload and change the SILInstruction and
SILValue taking overloads to call it.
2024-01-29 11:49:29 -08:00
Nate Chandler
87abaaa850 [FSPrunedLiveness] Returned bitfield from isDef.
Whether a node is a def on a collection of bits (whether a range or a
bit vector) isn't exhaustively characterized by the values {true,
false}.  A node may be a def on some but not others of the bits in the
collection.  Changed the range-taking isDef to write back the bits in
the range at which the node is a def.  The caller can then decide how to
react with full information.
2024-01-29 11:49:29 -08:00
Nate Chandler
667a78a314 [NFC] FSPrunedLiveness: Deduplicated isDef impls.
The version for Value and Instruction are identical except for the type
of the source of the cast to SILNode.  Add an overload for SILNode
through which the other two call.
2024-01-29 11:49:29 -08:00
Rick van Voorden
f8ae46b3f3 [inclusive-language] changed sanity to soundness 2024-01-25 18:18:02 -08:00
Nate Chandler
05bec97203 [FSPrunedLiveness] Add extendToNonUse.
And use it in lifetime maximization.

The preexisting member function updateForUse has been updated to match
PrunedLiveness and gravitate towards lifetimeEnding=false.

For a fixed instruction and bit, if called with lifetimeEnding=true and
then lifetimeEnding=false, the lifetime-ending-ness of the instruction
at the bit will be false; and if it is again called with
lifetimeEnding=true, the lifetime-ending-ness of the instruction at the
bit will remain false.

In contrast the new member function extendToUse does not alter the
lifetime-ending-ness if it is already set.  If it is unset, the function
sets the bit to lifetimeEnding=false.
2023-09-26 11:45:23 -07:00
Michael Gottesman
d7d7ab6ae2 [move-only] Make sure that we mask out liveness bits and use |= when merging successors
Both of these can cause us to insert destroy_addr in the wrong locations.

1. The first causes us to insert destroys for parts of values that are not
actually on the boundary since we didn't use our mask and instead used all of
the liveness information.

2. We were merging successor information using '&=' instead of '|=. This caused
a problem if we had multiple regions for the same successor. In such a case, we
would not have anything in common for the regions causing us to not have any
bits in common, resulting in us inserting too many destroy_addr instead of
skipping as we were supposed to.

rdar://112434492
2023-07-17 16:10:43 -07:00
Michael Gottesman
69a03c93f9 [move-only] Ban partial reinitialization after consuming a value.
This is similar to our ban on partial consuming a value for this release. The
reason for this is that, one can achieve a similar affect as partial consumption
via a consumption of the entire value and then a partial reinitialization. Example:

```swift
struct X : ~Copyable { var i = 5, var i2 = Klass() }
var x = X()
_ = consume x
x.i = 5
```

in the case above, we now have a value that is in a partially initialized state.

We still allow for move only types to have their fields initialized as long as
there is an intervening init.

rdar://111498740
2023-07-05 20:26:26 -07:00
Nate Chandler
0b30bd7361 [FieldSensitivePL] NFC: Added initDef(bit vector).
The new overload iterates over the contiguous ranges in the bit vector
and calls through to the overload that takes a range.
2023-06-27 12:10:52 -07:00
Evan Wilde
250082df25 [NFC] Reformat all the LLVMs
Reformatting everything now that we have `llvm` namespaces. I've
separated this from the main commit to help manage merge-conflicts and
for making it a bit easier to read the mega-patch.
2023-06-27 09:03:52 -07:00
Evan Wilde
f3ff561c6f [NFC] add llvm namespace to Optional and None
This is phase-1 of switching from llvm::Optional to std::optional in the
next rebranch. llvm::Optional was removed from upstream LLVM, so we need
to migrate off rather soon. On Darwin, std::optional, and llvm::Optional
have the same layout, so we don't need to be as concerned about ABI
beyond the name mangling. `llvm::Optional` is only returned from one
function in
```
getStandardTypeSubst(StringRef TypeName,
                     bool allowConcurrencyManglings);
```
It's the return value, so it should not impact the mangling of the
function, and the layout is the same as `std::optional`, so it should be
mostly okay. This function doesn't appear to have users, and the ABI was
already broken 2 years ago for concurrency and no one seemed to notice
so this should be "okay".

I'm doing the migration incrementally so that folks working on main can
cherry-pick back to the release/5.9 branch. Once 5.9 is done and locked
away, then we can go through and finish the replacement. Since `None`
and `Optional` show up in contexts where they are not `llvm::None` and
`llvm::Optional`, I'm preparing the work now by going through and
removing the namespace unwrapping and making the `llvm` namespace
explicit. This should make it fairly mechanical to go through and
replace llvm::Optional with std::optional, and llvm::None with
std::nullopt. It's also a change that can be brought onto the
release/5.9 with minimal impact. This should be an NFC change.
2023-06-27 09:03:52 -07:00
Nate Chandler
92fd8da3c3 [FieldSensitivePL] Fixed use-before-def handling.
rdar://111118843
2023-06-23 10:16:53 -07:00
nate-chandler
de45ab165e Merge pull request #66739 from nate-chandler/gardening/20230617/1/fix-warning
Gardening: Silenced Optional::hasValue warning.
2023-06-17 21:07:54 -07:00
Nate Chandler
6fecae9c5a [FieldSensitivePL] Silenced warning.
```
../include/swift/SIL/FieldSensitivePrunedLiveness.h:531:54: warning: 'hasValue' is deprecated: Use has_value instead. [-Wdeprecated-declarations]
  bool isInitialized() const { return numBitsToTrack.hasValue(); }
                                                     ^~~~~~~~
                                                     has_value
```
2023-06-17 17:26:17 -07:00
Nate Chandler
66867c02d1 [MoveOnlyAddressChecker] Fix used fields repr.
The address checker records uses in its livenessUses map.  Previously,
that map mapped from an instruction to a range of fields of the type.
But an instruction can use multiple discontiguous fields of a single
value.  Here, such instructions are properly recorded by fixing the map
to store a bit vector for each instruction.

rdar://110676577
2023-06-17 16:04:43 -07:00
Nate Chandler
f4e3292a2f [FieldSensitivePL] Fix vectorization.
FieldSensitivePrunedLiveness is used as a vectorization of
PrunedLiveness.  An instance of FSPL with N elements needs to be able to
represent the same states as N instances of PL.

Previously, it failed to do that in two significant ways:

(1) It attempted to save space for which elements were live by using
    a range.  This failed to account for instructions which are users of
    non-contiguous fields of an aggregate.

    apply(
      @owned (struct_element_addr %s, #S.f1),
      @owned (struct_element_addr %s, #S.f3)
    )

(2) It used a single bit to represent whether the instruction was
    consuming.  This failed to account for instructions which consumed
    some fields and borrowed others.

    apply(
      @owned (struct_element_addr %s, #S.f1),
      @guaranteed (struct_element_addr %s, #S.f2)
    )

The fix for (1) is to use a bit vector to represent which elements
are used by the instruction.  The fix for (2) is to use a second bit
vector to represent which elements are _consumed_ by the instruction.

Adapted the move-checker to use the new representation.

rdar://110909290
2023-06-16 21:13:07 -07:00
Michael Gottesman
d5aea69335 [sil] Refactor field sensitive pruned liveness so it can be used with different sized root values. 2023-02-07 16:27:28 -08:00
Michael Gottesman
3b72951148 [sil] Provide FieldSensitivePrunedLiveness with its own implementation of PrunedLiveBlocks called FieldSensitivePrunedLiveBlocks.
This will let the non-field sensitive version use a more performant
implementation internally. This is important since PrunedLiveBlocks is used in
the hot path when working with Ownership SSA, while the field sensitive version
is only used for certain diagnostics.

NOTE: I did not refactor PrunedLiveness to use the faster implementation... this
is just a quick pass over the code to prepare for that change.
2023-02-07 16:26:41 -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
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
Michael Gottesman
4ad2bc93ba [field-sensitive-pruned-liveness] Some small changes.
Specifically:

1. I added to the documentation at the top of the file that our representation
allows for partial init/reinit of structs/tuples from parts.

2. I renamed SubElementNumber to SubElementOffset. This I think fits the actual
use case better and makes it clearer what one is working with (the offset inside
a type of a subelement of the type).

3. I added some small helpers to TypeSubElementCount and SubElementOffset for
adding/subtracting from them.

4. I added the ability to iterate over just consuming/nonconsuming users in
FieldSensitivePrunedLiveness. Just a useful little helper.
2023-01-24 15:28:12 -08:00
Michael Gottesman
b405782727 Revert change to a comment. 2023-01-09 11:25:51 -08:00
Michael Gottesman
1c459b6c5b [move-only-addr] Wire up the move only address checker to Field Sensitive Pruned Liveness Boundary.
This let me fix an issue around hoisting destroy_addr/store [assign] when
working in multi-block cfgs.

I also added some .sil tests.
2023-01-08 21:51:40 -08:00
Michael Gottesman
0e7b04acf3 [field-pruned-liveness] Add two helper constructors to TypeTreeLeafTypeRange.
The first is a default constructor that initializes both startEltOffset and
endEltOffset to 0. This is used to ensure that we can use certain standard
algorithms that expect to be able to perform a default constructor when
inserting an initial value into a collection.

The second adds the ability to construct a TypeTreeLeafTypeRange for a
SILType/SILFunction if one has a type without an actual value.
2023-01-08 14:54:48 -08:00
Michael Gottesman
c2adaa9b75 [field-pruned-liveness] Add helper for TypeSubElementCount that takes a SILFunction.
This just calls TypeSubElementCount(SILType, SILModule &, TypeExpansionContext)
using the module and type expansion context from the SILFunction.
2023-01-08 14:53:04 -08:00
Michael Gottesman
3f90afb901 [move-addr] Move Field Sensitive Pruned Liveness into its own header/cpp impl. 2022-12-08 10:29:48 -08:00