Skip to content

SIL optimizer: a collection of optimizer changes to prepare for COW representation #32023

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

Merged
merged 10 commits into from
May 28, 2020

Conversation

eeckstein
Copy link
Contributor

@eeckstein eeckstein commented May 26, 2020

This PR contains a set of independent (and mostly small) optimizer changes which are needed - or beneficial - for the switch to the COW representation in Array types.

For details see the commit list.

I'd like to explicitly mention one larger addition: A new optimization pass which constant folds the uniqueness result of begin_cow_mutation instructions, if it can be proved that the buffer argument is uniquely referenced.
For example:

     %buffer = end_cow_mutation %mutable_buffer
     // ...
     // %buffer does not escape here
     // ...
     (%is_unique, %mutable_buffer2) = begin_cow_mutation %buffer
     cond_br %is_unique, ...

is replaced with

     %buffer = end_cow_mutation [keep_unique] %mutable_buffer
     // ...
     (%not_used, %mutable_buffer2) = begin_cow_mutation %buffer
     %true = integer_literal 1
     cond_br %true, ...

If someone is interested, https://github.com/apple/swift/pull/31730/commits contains all changes for supporting COW representation in Array types.

eeckstein added 10 commits May 26, 2020 18:01
Constant folds the uniqueness result of begin_cow_mutation instructions, if it can be proved that the buffer argument is uniquely referenced.
For example:

     %buffer = end_cow_mutation %mutable_buffer
     // ...
     // %buffer does not escape here
     // ...
     (%is_unique, %mutable_buffer2) = begin_cow_mutation %buffer
     cond_br %is_unique, ...

is replaced with

     %buffer = end_cow_mutation [keep_unique] %mutable_buffer
     // ...
     (%not_used, %mutable_buffer2) = begin_cow_mutation %buffer
     %true = integer_literal 1
     cond_br %true, ...

Note that the keep_unique flag is set on the end_cow_mutation because the code now relies on that the buffer is really uniquely referenced.

The optimization can also handle def-use chains between end_cow_mutation and begin_cow_mutation which involve phi-arguments.

An additional peephole optimization is performed: if the begin_cow_mutation is the only use of the end_cow_mutation, the whole pair of instructions is eliminated.
… owned-to-guarantee transformation.

If a function is annotated with @_semantics("optimize.sil.specialize.owned2guarantee.never") its arguments will not be converted from owned to guaranteed.
… are only destroyed.

If the only use of an upcast, unchecked_ref_cast or end_cow_mutation is a destroy/release, just destroy the operand and remove the cast/end_cow_mutation.
(end_cow_mutation (upcast X)) -> (end_cow_mutation X)
(end_cow_mutation (unchecked_ref_cast X)) -> (end_cow_mutation X)
…mantics

Used to "finalize" an array literal. It's not used, yet. So this is NFC.
Also handle the "array.finalize_intrinsic" function in various array specific optimizations.
…e optimizations.

Mostly this is about "looking through" a begin_cow_mutation or end_cow_mutation.
This is in preparation for COW support. More optimizer tests require an optimized non-assert stdlib build.
…nction.

Support a version of Array._checkSubscript which has no wasNativeTypeChecked parameter.
The COWOpts optimization relies more on LICM. This additional run of the pass ensures that there is no phase ordering issue between LICM and COWOpts
@eeckstein eeckstein requested a review from atrick May 26, 2020 17:49
@eeckstein
Copy link
Contributor Author

@swift-ci test

@eeckstein
Copy link
Contributor Author

@swift-ci benchmark

@swift-ci
Copy link
Contributor

Performance: -O

Code size: -O

Performance: -Osize

Improvement OLD NEW DELTA RATIO
SuffixArrayLazy 5 4 -20.0% 1.25x (?)

Code size: -Osize

Performance: -Onone

Regression OLD NEW DELTA RATIO
ObjectiveCBridgeStubToNSDateRef 2500 2720 +8.8% 0.92x (?)

Code size: -swiftlibs

How to read the data The tables contain differences in performance which are larger than 8% and differences in code size which are larger than 1%.

If you see any unexpected regressions, you should consider fixing the
regressions before you merge the PR.

Noise: Sometimes the performance results (not code size!) contain false
alarms. Unexpected regressions which are marked with '(?)' are probably noise.
If you see regressions which you cannot explain you can try to run the
benchmarks again. If regressions still show up, please consult with the
performance team (@eeckstein).

Hardware Overview
  Model Name: Mac mini
  Model Identifier: Macmini8,1
  Processor Name: 6-Core Intel Core i7
  Processor Speed: 3.2 GHz
  Number of Processors: 1
  Total Number of Cores: 6
  L2 Cache (per Core): 256 KB
  L3 Cache: 12 MB
  Memory: 64 GB

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 3f42ad7

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 3f42ad7

@eeckstein
Copy link
Contributor Author

@swift-ci test

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 3f42ad7

@eeckstein
Copy link
Contributor Author

@swift-ci test linux

@eeckstein eeckstein merged commit 5013a02 into swiftlang:master May 28, 2020
@eeckstein eeckstein deleted the cow-optimizer-changes branch May 28, 2020 07:40
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