Skip to content

SIL optimizer: remove unbalanced retains/releases from immortal objects #38973

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 4 commits into from
Aug 23, 2021

Conversation

eeckstein
Copy link
Contributor

ARC operations don't have an effect on immortal objects, like the empty array singleton or statically allocated arrays.
Therefore we can freely remove and retain/release instructions on such objects, even if there is no paired balanced ARC operation.

This optimization can only be done with a minimum deployment target of Swift 5.1, because in that version we added immortal ref count bits.

The optimization is implemented in libswift. Additionally, the remaining logic of simplifying strong_retain and strong_release is also ported to libswift.

This PR also includes some infrastructure improvements in libswift. For details see the commit list.

rdar://81482156

@eeckstein
Copy link
Contributor Author

@swift-ci test

@eeckstein eeckstein force-pushed the simplify-retain-release branch from 6e06be5 to b5d32b9 Compare August 20, 2021 14:07
@eeckstein
Copy link
Contributor Author

@swift-ci test

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - b5d32b9d765c0dc69f092331539d136886d6e18b

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - b5d32b9d765c0dc69f092331539d136886d6e18b

return isNotReferenceCounted(value: urc.operand, context: context)
case is GlobalValueInst:
// Since Swift 5.1, statically allocated objects have "immortal" reference
// counts. Therefore we can savely eliminate unbalaced retains and
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// counts. Therefore we can savely eliminate unbalaced retains and
// counts. Therefore we can safely eliminate unbalaced retains and

And add `UnaryInstruction` which adds a property `operand` to all unary instructions.
This replaces the existing single-operand properties, which simplifies the code.
* unify FunctionPassContext and InstructionPassContext
* add a modification API: PassContext.setOperand
* automatic invalidation notifications when the SIL is modified
ARC operations don't have an effect on immortal objects, like the empty array singleton or statically allocated arrays.
Therefore we can freely remove and retain/release instructions on such objects, even if there is no paired balanced ARC operation.

This optimization can only be done with a minimum deployment target of Swift 5.1, because in that version we added immortal ref count bits.

The optimization is implemented in libswift. Additionally, the remaining logic of simplifying strong_retain and strong_release is also ported to libswift.

rdar://81482156
@eeckstein eeckstein force-pushed the simplify-retain-release branch from b5d32b9 to 8be0ca0 Compare August 23, 2021 08:24
@eeckstein
Copy link
Contributor Author

@swift-ci test

@eeckstein
Copy link
Contributor Author

@swift-ci benchmark

@swift-ci
Copy link
Contributor

Performance (x86_64): -O

Regression OLD NEW DELTA RATIO
DictionaryOfAnyHashableStrings_insert 3220 5586 +73.5% 0.58x
Set.isDisjoint.Box25 364 506 +39.0% 0.72x (?)
Set.isDisjoint.Int50 268 339 +26.5% 0.79x (?)
DictionaryKeysContainsNative 23 27 +17.4% 0.85x (?)
StringRemoveDupes 263 289 +9.9% 0.91x (?)
UTF8Decode_InitFromBytes_ascii 275 296 +7.6% 0.93x (?)
 
Improvement OLD NEW DELTA RATIO
FlattenListFlatMap 6627 6122 -7.6% 1.08x (?)

Code size: -O

Performance (x86_64): -Osize

Regression OLD NEW DELTA RATIO
FlattenListFlatMap 3970 7161 +80.4% 0.55x (?)
NSStringConversion.Rebridge.LongUTF8 49 53 +8.2% 0.92x (?)
 
Improvement OLD NEW DELTA RATIO
ObjectiveCBridgeStubFromNSDate 7350 6220 -15.4% 1.18x (?)

Code size: -Osize

Performance (x86_64): -Onone

Regression OLD NEW DELTA RATIO
DataAppendDataMediumToMedium 4980 5500 +10.4% 0.91x (?)
NSError 625 680 +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 Pro
  Model Identifier: MacPro6,1
  Processor Name: 12-Core Intel Xeon E5
  Processor Speed: 2.7 GHz
  Number of Processors: 1
  Total Number of Cores: 12
  L2 Cache (per Core): 256 KB
  L3 Cache: 30 MB
  Memory: 64 GB

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 8be0ca0

@eeckstein
Copy link
Contributor Author

@swift-ci test macOS

@eeckstein eeckstein merged commit 6752925 into swiftlang:main Aug 23, 2021
@eeckstein eeckstein deleted the simplify-retain-release branch August 23, 2021 17:32
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.

3 participants