Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SIL] Maintain owned argument lifetimes at inlining. #60670

Merged

Conversation

nate-chandler
Copy link
Contributor

@nate-chandler nate-chandler commented Aug 20, 2022

Previously, begin_borrow [lexical] were created during SILGen for @owned arguments. Such borrows could be deleted if trivially dead, which was the original reason why @owned arguments were considered lexical and could not have their destroys hoisted.

Those borrows were however important during inlining because they would maintain the lifetime of the owned argument. Unless of course the borrow scope was trivially dead. In which case the owned argument's lifetime would not be maintained. And if the caller's value was non-lexical, destroys of the value could be hoisted over deinit barriers.

Here, during inlining, move_value [lexical]s are introduced during inlining whever the caller's value is non-lexical. This maintains the lifetime of the owned argument even after inlining.

Additionally, took the opportunity to improve the handling of guaranteed function argument lifetimes: (1) during inlining they don't get lexical borrow_scopes when the caller's value is already lexical and (2) they are recongized as lexical.

@nate-chandler nate-chandler requested a review from atrick August 20, 2022 17:08
@nate-chandler nate-chandler force-pushed the lexical_lifetimes/owned_arguments branch 6 times, most recently from 23ab07c to 4e4143d Compare August 30, 2022 00:06
@nate-chandler
Copy link
Contributor Author

@swift-ci please benchmark

@nate-chandler
Copy link
Contributor Author

@swift-ci please test

@nate-chandler
Copy link
Contributor Author

@swift-ci please test source compatibility

@nate-chandler nate-chandler force-pushed the lexical_lifetimes/owned_arguments branch 2 times, most recently from 95e846a to bdbcb8f Compare January 12, 2023 00:08
@nate-chandler
Copy link
Contributor Author

@swift-ci please benchmark

@nate-chandler
Copy link
Contributor Author

------- Performance (x86_64): -O -------

REGRESSION                                  OLD       NEW       DELTA     RATIO
ArrayAppendGenericStructs                   593.333   1260.0    +112.4%   **0.47x (?)**
Data.append.Sequence.64kB.Count             2.0       2.667     +33.3%    **0.75x (?)**
MapReduceClass2                             10.663    12.506    +17.3%    **0.85x (?)**
FlattenListFlatMap                          3926.0    4495.0    +14.5%    **0.87x (?)**

IMPROVEMENT                                 OLD       NEW       DELTA     RATIO
Set.isDisjoint.Int100                       208.083   129.611   -37.7%    **1.61x**
SetIsSubsetInt0                             206.3     129.133   -37.4%    **1.60x**
Set.isDisjoint.Int25                        204.1     129.667   -36.5%    **1.57x**
Set.isDisjoint.Int50                        202.6     129.733   -36.0%    **1.56x**
Set.isSubset.Empty.Int                      123.067   95.867    -22.1%    **1.28x**
Set.isStrictSubset.Empty.Int                125.231   98.0      -21.7%    **1.28x (?)**
Data.hash.Medium                            29.167    24.477    -16.1%    **1.19x (?)**
FlattenListLoop                             1616.0    1386.0    -14.2%    **1.17x (?)**
NormalizedIterator_nonBMPSlowestPrenormal   446.607   383.542   -14.1%    **1.16x (?)**
NormalizedIterator_emoji                    373.12    322.143   -13.7%    **1.16x (?)**
NormalizedIterator_slowerPrenormal          298.571   271.186   -9.2%     **1.10x (?)**
Set.isStrictSubset.Int.Empty                43.846    40.286    -8.1%     **1.09x (?)**
------- Performance (x86_64): -Osize -------

REGRESSION                       OLD         NEW         DELTA    RATIO
SetSymmetricDifferenceInt100     105.929     145.273     +37.1%   **0.73x**
SetSymmetricDifferenceInt50      120.385     146.75      +21.9%   **0.82x (?)**
FlattenListLoop                  1386.0      1613.0      +16.4%   **0.86x (?)**
SetSymmetricDifferenceInt25      121.333     139.154     +14.7%   **0.87x (?)**
MapReduceString                  42.333      46.88       +10.7%   **0.90x (?)**
PrefixAnySeqCRangeIterLazy       121.4       133.9       +10.3%   **0.91x (?)**

IMPROVEMENT                      OLD         NEW         DELTA    RATIO
Set.isDisjoint.Int100            208.0       129.611     -37.7%   **1.60x**
Set.isDisjoint.Int25             208.0       129.615     -37.7%   **1.60x**
Set.isDisjoint.Int50             202.6       129.667     -36.0%   **1.56x**
SetIsSubsetInt0                  204.8       132.933     -35.1%   **1.54x**
Dictionary4                      234.857     158.75      -32.4%   **1.48x (?)**
Dictionary4OfObjects             271.833     202.333     -25.6%   **1.34x (?)**
Set.isStrictSubset.Empty.Int     126.333     98.0        -22.4%   **1.29x (?)**
Set.isSubset.Empty.Int           123.067     95.824      -22.1%   **1.28x (?)**
StringWalk                       1530.435    1253.793    -18.1%   **1.22x**
SequenceAlgosArray               2511.111    2076.364    -17.3%   **1.21x (?)**
Set.filter.Int100.16k            24.979      20.963      -16.1%   **1.19x (?)**
Data.hash.Medium                 28.727      24.385      -15.1%   **1.18x (?)**
ObjectiveCBridgeStubFromNSDate   3255.0      2790.0      -14.3%   **1.17x (?)**
Set.filter.Int100.28k            42.885      36.79       -14.2%   **1.17x (?)**
Set.filter.Int100.20k            29.653      25.837      -12.9%   **1.15x (?)**
Chars2                           3580.952    3152.273    -12.0%   **1.14x (?)**
Set.filter.Int100.24k            34.5        30.907      -10.4%   **1.12x (?)**
Calculator                       152.846     136.933     -10.4%   **1.12x (?)**
NormalizedIterator_emoji         356.741     321.778     -9.8%    **1.11x (?)**
StringInterpolation              6536.842    5924.242    -9.4%    **1.10x (?)**
NopDeinit                        32683.333   29714.286   -9.1%    **1.10x (?)**

------- Code size: -Osize -------

REGRESSION                     OLD     NEW     DELTA   RATIO
NopDeinit.o                    4219    4401    +4.3%   **0.96x**
ClassArrayGetter.o             3629    3739    +3.0%   **0.97x**
DictionaryGroup.o              10896   11059   +1.5%   **0.99x**
SortLettersInPlace.o           8014    8122    +1.3%   **0.99x**
COWTree.o                      11141   11270   +1.2%   **0.99x**
DictionarySubscriptDefault.o   13418   13566   +1.1%   **0.99x**

IMPROVEMENT                    OLD     NEW     DELTA   RATIO
DictionaryCompactMapValues.o   13370   13155   -1.6%   **1.02x**
MapReduce.o                    20180   19943   -1.2%   **1.01x**
------- Performance (x86_64): -Onone -------

REGRESSION                   OLD       NEW       DELTA    RATIO
SubstringRemoveFirst1        0.143     0.167     +16.7%   **0.86x (?)**
CharacterLiteralsLarge       400.2     448.2     +12.0%   **0.89x (?)**

IMPROVEMENT                  OLD       NEW       DELTA    RATIO
Data.hash.Medium             33.125    28.824    -13.0%   **1.15x**
ArrayAppendUTF16Substring    25788.0   22764.0   -11.7%   **1.13x (?)**
ArrayAppendAsciiSubstring    25752.0   22764.0   -11.6%   **1.13x**
ArrayAppendLatin1Substring   26118.0   23088.0   -11.6%   **1.13x (?)**
ProtocolDispatch             506.0     457.5     -9.6%    **1.11x (?)**
SetIsSubsetInt100            448.0     412.167   -8.0%    **1.09x (?)**
SetIsSubsetInt25             136.294   125.571   -7.9%    **1.09x (?)**
Set.isStrictSubset.Int50     260.778   241.5     -7.4%    **1.08x (?)**
Set.isStrictSubset.Int25     136.588   127.111   -6.9%    **1.07x (?)**

------- Code size: -swiftlibs -------

@nate-chandler
Copy link
Contributor Author

@swift-ci please test

@nate-chandler
Copy link
Contributor Author

@swift-ci please benchmark

docs/SIL.rst Outdated Show resolved Hide resolved
@nate-chandler
Copy link
Contributor Author

------- Performance (x86_64): -O -------

REGRESSION                                  OLD       NEW       DELTA     RATIO
ArrayAppendGenericStructs                   623.333   1264.0    +102.8%   **0.49x (?)**
StringWithCString2                          0.0       0.001     +100.0%   **0.50x (?)**
NormalizedIterator_nonBMPSlowestPrenormal   391.017   449.02    +14.8%    **0.87x (?)**
NormalizedIterator_slowerPrenormal          274.699   301.5     +9.8%     **0.91x (?)**
NormalizedIterator_fastPrenormal            485.385   530.851   +9.4%     **0.91x (?)**
NormalizedIterator_emoji                    329.0     356.154   +8.3%     **0.92x (?)**

IMPROVEMENT                                 OLD       NEW       DELTA     RATIO
Set.isDisjoint.Seq.Int.Empty                53.512    47.192    -11.8%    **1.13x (?)**

------- Code size: -O -------
------- Performance (x86_64): -Osize -------

IMPROVEMENT                      OLD        NEW        DELTA    RATIO
Dictionary4                      232.0      158.167    -31.8%   **1.47x**
Dictionary4OfObjects             271.0      199.286    -26.5%   **1.36x**
Set.filter.Int100.20k            32.44      26.438     -18.5%   **1.23x (?)**
StringWalk                       1532.857   1251.765   -18.3%   **1.22x (?)**
ObjectiveCBridgeStubFromNSDate   3320.0     2865.714   -13.7%   **1.16x (?)**
Set.filter.Int100.28k            43.82      38.267     -12.7%   **1.15x**
FlattenListFlatMap               3427.0     3000.0     -12.5%   **1.14x (?)**
Set.filter.Int100.24k            35.258     31.45      -10.8%   **1.12x**
Set.filter.Int100.16k            24.12      21.528     -10.7%   **1.12x (?)**
StrComplexWalk                   3121.429   2797.143   -10.4%   **1.12x (?)**
Set.subtracting.Seq.Empty.Int    161.727    150.333    -7.0%    **1.08x (?)**

------- Code size: -Osize -------
------- Performance (x86_64): -Onone -------

REGRESSION                              OLD       NEW       DELTA    RATIO
ObjectiveCBridgeStubToNSStringRef       115.667   129.526   +12.0%   **0.89x (?)**
ObjectiveCBridgeStubNSDateRefAccess     2787.0    3092.0    +10.9%   **0.90x (?)**
CreateObjects                           1252.0    1361.0    +8.7%    **0.92x (?)**

IMPROVEMENT                             OLD       NEW       DELTA    RATIO
NSStringConversion.InlineBuffer.ASCII   5555.0    4925.0    -11.3%   **1.13x (?)**
NSStringConversion.InlineBuffer.UTF8    3501.0    3172.0    -9.4%    **1.10x (?)**
RC4                                     12397.0   11436.0   -7.8%    **1.08x (?)**

------- Code size: -swiftlibs -------

@nate-chandler nate-chandler force-pushed the lexical_lifetimes/owned_arguments branch from 8f3a2b7 to 683a749 Compare January 13, 2023 00:31
@nate-chandler
Copy link
Contributor Author

------- Performance (x86_64): -O -------

REGRESSION                     OLD      NEW      DELTA     RATIO
StringWithCString2             0.0      0.001    +100.0%   **0.50x (?)**

IMPROVEMENT                    OLD      NEW      DELTA     RATIO
FlattenListLoop                1616.0   1010.5   -37.5%    **1.60x (?)**
FlattenListFlatMap             4325.0   3949.0   -8.7%     **1.10x (?)**
Set.isDisjoint.Seq.Int.Empty   51.186   47.208   -7.8%     **1.08x (?)**
Data.hash.Medium               29.167   27.0     -7.4%     **1.08x (?)**

------- Code size: -O -------
------- Performance (x86_64): -Osize -------

REGRESSION                            OLD       NEW       DELTA    RATIO
Breadcrumbs.MutatedUTF16ToIdx.Mixed   192.333   211.636   +10.0%   **0.91x**
Breadcrumbs.MutatedIdxToUTF16.Mixed   197.364   217.1     +10.0%   **0.91x (?)**

IMPROVEMENT                           OLD       NEW       DELTA    RATIO
FlattenListFlatMap                    4375.0    2799.0    -36.0%   **1.56x (?)**
Dictionary4                           225.5     156.6     -30.6%   **1.44x**
Dictionary4OfObjects                  276.0     200.111   -27.5%   **1.38x (?)**
Set.filter.Int100.20k                 33.24     26.448    -20.4%   **1.26x (?)**
StringWalk                            1536.0    1249.6    -18.6%   **1.23x (?)**
Set.filter.Int100.28k                 44.961    38.281    -14.9%   **1.17x (?)**
FlattenListLoop                       1615.0    1387.0    -14.1%   **1.16x (?)**
ObjectiveCBridgeStubFromNSDate        3275.0    2890.0    -11.8%   **1.13x (?)**
StringFromLongWholeSubstring          2.939     2.606     -11.3%   **1.13x (?)**
Set.filter.Int100.16k                 24.236    21.583    -10.9%   **1.12x (?)**
Set.filter.Int100.24k                 34.953    31.4      -10.2%   **1.11x (?)**
DictionaryBridgeToObjC_BulkAccess     100.857   93.625    -7.2%    **1.08x (?)**

------- Code size: -Osize -------
------- Performance (x86_64): -Onone -------

REGRESSION                            OLD       NEW       DELTA     RATIO
ArrayAppendGenericStructs             634.0     1440.0    +127.1%   **0.44x (?)**
Breadcrumbs.MutatedUTF16ToIdx.Mixed   205.1     225.222   +9.8%     **0.91x (?)**

IMPROVEMENT                           OLD       NEW       DELTA     RATIO
ObjectiveCBridgeStubToNSStringRef     124.786   115.4     -7.5%     **1.08x (?)**
RC4                                   12390.0   11471.0   -7.4%     **1.08x (?)**
StrComplexWalk                        4877.5    4558.0    -6.6%     **1.07x (?)**

------- Code size: -swiftlibs -------

@nate-chandler nate-chandler force-pushed the lexical_lifetimes/owned_arguments branch 6 times, most recently from 5de700f to e8727d4 Compare January 24, 2023 23:55
@nate-chandler
Copy link
Contributor Author

------- Performance (x86_64): -O -------

REGRESSION           OLD      NEW      DELTA    RATIO
FlattenListFlatMap   2518.0   2787.0   +10.7%   **0.90x (?)**
Data.hash.Medium     26.567   28.731   +8.1%    **0.92x (?)**

------- Code size: -O -------

------- Performance (x86_64): -Osize -------

REGRESSION                     OLD      NEW        DELTA     RATIO
StringWithCString2             0.0      0.001      +100.0%   **0.50x (?)**
FlattenListFlatMap             3966.0   4377.0     +10.4%    **0.91x (?)**
StrComplexWalk                 2800.0   3037.143   +8.5%     **0.92x (?)**
StringFromLongWholeSubstring   2.717    2.935      +8.0%     **0.93x (?)**

IMPROVEMENT                    OLD      NEW        DELTA     RATIO
FlattenListLoop                1620.0   1386.0     -14.4%    **1.17x (?)**
DictionaryKeysContainsCocoa    17.0     15.46      -9.1%     **1.10x (?)**

------- Code size: -Osize -------

------- Performance (x86_64): -Onone -------

REGRESSION                     OLD     NEW     DELTA    RATIO
SetSymmetricDifferenceInt100   405.8   443.0   +9.2%    **0.92x (?)**

IMPROVEMENT                    OLD     NEW     DELTA    RATIO
CharacterLiteralsLarge         471.5   416.2   -11.7%   **1.13x (?)**

------- Code size: -swiftlibs -------

@nate-chandler nate-chandler marked this pull request as ready for review January 25, 2023 05:08
Previously, only @owned function arguments were treated as lexical.
Here, all function arguments which report themselves to be lexical (i.e.
based on their type and annotation) are regarded as lexical.
Add move_value to the list of ownership instructions through which
lookThroughOwnershipInsts looks.
Previously, just copies and borrows were stripped.  All ownership
instructions, specifically, newly, move_value instructions, should be
stripped.
Add move_value to the list of instructions through which
getSingleValueCopyOrCast looks.
Just like begin_borrow and copy_value, move_value should be looked
through because the interpreter doesn't model the memory operation it
encodes.
@nate-chandler nate-chandler force-pushed the lexical_lifetimes/owned_arguments branch from e8727d4 to 2171216 Compare January 25, 2023 19:37
Enables the outlining of the ContiguousArrayStorage<StaticString> used
when initializing a RawRepresentable enum whose RawValue is String into
a global value to continue even when ContiguousArrayStorage has a
lexical lifetime.

Addresses the following regressions

StringEnumRawValueInitialization    400     7680    +1820.0%   **0.05x**
ArrayLiteral2                       78      647     +729.5%    **0.12x**
DataCreateSmallArray                1750    8850    +405.7%    **0.20x**

seen when enabling lexical lifetimes in the standard library.
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.
Addresses the following regressions

StackPromo                                10100   14400   +42.6%   **0.70x**

seen when enabling lexical lifetimes in the standard library.
@nate-chandler nate-chandler force-pushed the lexical_lifetimes/owned_arguments branch from 2171216 to 3de985f Compare January 25, 2023 19:39
@nate-chandler
Copy link
Contributor Author

@swift-ci please benchmark

@nate-chandler
Copy link
Contributor Author

@swift-ci please smoke test

@nate-chandler nate-chandler force-pushed the lexical_lifetimes/owned_arguments branch 2 times, most recently from 1d426e5 to a9bbb1d Compare January 25, 2023 22:20
When encountering inside a borrow scope a non-lexical move_value or a
move_value [lexical] where the borrowed value is itself already lexical,
delete the move_value and regard its uses as uses of the moved-from
value.
Changes to teach the optimizer pipeline about move_value require some
corresponding test updates.
@nate-chandler nate-chandler force-pushed the lexical_lifetimes/owned_arguments branch from a9bbb1d to a3fa246 Compare January 26, 2023 00:32
Borrow scopes were moved when lexical borrow scopes were no longer
emitted for owned arguments.  Now, they are emitted when guaranteed
values are needed.  Fixing these diagnostics will be tracked separately.
The same issue is already visible elsewhere in this test file, e.g. in
castTestSwitch1, castTestSwitch2, and castTestSwitchInLoop.
@nate-chandler
Copy link
Contributor Author

@swift-ci please test

@nate-chandler
Copy link
Contributor Author

@swift-ci please benchmark

@nate-chandler
Copy link
Contributor Author

------- Performance (x86_64): -O -------

REGRESSION                  OLD      NEW      DELTA    RATIO
Data.hash.Medium            26.778   28.964   +8.2%    **0.92x (?)**

IMPROVEMENT                 OLD      NEW      DELTA    RATIO
ArrayAppendGenericStructs   1092.0   580.0    -46.9%   **1.88x (?)**

------- Code size: -O -------

------- Performance (x86_64): -Osize -------

REGRESSION                     OLD      NEW        DELTA    RATIO
StringWalk                     1267.2   1394.118   +10.0%   **0.91x (?)**
StringFromLongWholeSubstring   2.606    2.826      +8.4%    **0.92x (?)**
Data.hash.Medium               26.556   28.733     +8.2%    **0.92x (?)**

IMPROVEMENT                    OLD      NEW        DELTA    RATIO
FlattenListLoop                1617.0   1240.0     -23.3%   **1.30x (?)**
FlattenListFlatMap             4389.0   3993.0     -9.0%    **1.10x (?)**
Data.hash.Empty                54.45    50.095     -8.0%    **1.09x (?)**

------- Code size: -Osize -------

------- Performance (x86_64): -Onone -------

REGRESSION                     OLD     NEW      DELTA     RATIO
ArrayAppendGenericStructs      610.0   1442.0   +136.4%   **0.42x (?)**
SetSymmetricDifferenceInt100   406.2   443.6    +9.2%     **0.92x (?)**

IMPROVEMENT                    OLD     NEW      DELTA     RATIO
CharacterLiteralsLarge         466.0   414.6    -11.0%    **1.12x (?)**

------- Code size: -swiftlibs -------

@nate-chandler
Copy link
Contributor Author

@swift-ci please test macOS platform

Copy link
Contributor

@atrick atrick left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!

@nate-chandler
Copy link
Contributor Author

@swift-ci please test macOS platform

@nate-chandler nate-chandler merged commit 2fc7659 into swiftlang:main Jan 27, 2023
@nate-chandler nate-chandler deleted the lexical_lifetimes/owned_arguments branch January 27, 2023 02:30
nate-chandler added a commit to nate-chandler/swift that referenced this pull request Feb 18, 2023
…_lifetimes/owned_arguments"

This reverts commit 2fc7659, reversing
changes made to d896877.
nate-chandler added a commit to nate-chandler/swift that referenced this pull request Feb 18, 2023
…_lifetimes/owned_arguments"

This reverts commit 2fc7659, reversing
changes made to d896877.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants