Skip to content

SILOptimizer: allow function pointers in static globals and static global arrays. #35780

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 7 commits into from
Feb 10, 2021

Conversation

eeckstein
Copy link
Contributor

@eeckstein eeckstein commented Feb 5, 2021

For example, generate a static global for the array literal in:

func foo(_ i: Int) -> Int { ... }
func bar(_ i: Int) -> Int { ... }

func returnFunctionArray() -> [(Int) -> Int] {
  return [foo, bar]
}

rdar://73570149
https://bugs.swift.org/browse/SR-14101

This PR also contains an improvement of DeadFunctionElimination: it now can also remove dead global variables.
Handling function references in global initializers is required in DeadFunctionElimination anyway. It was only a small addition to implement dead global variable elimination in the same change.

rdar://32956923

@eeckstein eeckstein force-pushed the static-function-arrays branch from 4f058ba to 279c1b9 Compare February 5, 2021 13:15
Copy link
Contributor

@aschwaighofer aschwaighofer left a comment

Choose a reason for hiding this comment

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

LGTM

@eeckstein eeckstein force-pushed the static-function-arrays branch from 279c1b9 to b00399d Compare February 8, 2021 11:54
@eeckstein
Copy link
Contributor Author

@swift-ci test

@swift-ci
Copy link
Contributor

swift-ci commented Feb 8, 2021

Build failed
Swift Test Linux Platform
Git Sha - b00399d20ad70cc323d5e2e9bf93f1124b622e9f

@swift-ci
Copy link
Contributor

swift-ci commented Feb 8, 2021

Build failed
Swift Test OS X Platform
Git Sha - b00399d20ad70cc323d5e2e9bf93f1124b622e9f

@eeckstein eeckstein force-pushed the static-function-arrays branch 2 times, most recently from 92771d2 to 383f8f6 Compare February 8, 2021 14:08
@eeckstein
Copy link
Contributor Author

@swift-ci test

@eeckstein
Copy link
Contributor Author

@swift-ci benchmark

@swift-ci
Copy link
Contributor

swift-ci commented Feb 8, 2021

Build failed
Swift Test Linux Platform
Git Sha - 383f8f6ea819450f42d617be5219ae4344957e8e

@swift-ci
Copy link
Contributor

swift-ci commented Feb 8, 2021

Performance: -O

Regression OLD NEW DELTA RATIO
Data.init.Sequence.64kB.Count.I 39 58 +48.7% 0.67x (?)
Data.init.Sequence.64kB.Count 39 58 +48.7% 0.67x
Data.init.Sequence.2047B.Count.I 73 102 +39.7% 0.72x (?)
Data.init.Sequence.2049B.Count.I 73 102 +39.7% 0.72x (?)
Data.init.Sequence.809B.Count 73 95 +30.1% 0.77x
Data.init.Sequence.809B.Count.I 73 95 +30.1% 0.77x
Data.init.Sequence.511B.Count.I 90 109 +21.1% 0.83x (?)
Data.init.Sequence.513B.Count.I 94 111 +18.1% 0.85x (?)
DataCreateEmpty 170 200 +17.6% 0.85x
SuffixCountableRange 8 9 +12.5% 0.89x
CharIteration_punctuatedJapanese_unicodeScalars 720 800 +11.1% 0.90x
Breadcrumbs.IdxToUTF16Range.longASCII 38 42 +10.5% 0.90x (?)
CharIteration_japanese_unicodeScalars 5000 5440 +8.8% 0.92x (?)
CharIteration_korean_unicodeScalars 3640 3960 +8.8% 0.92x
CharIteration_chinese_unicodeScalars 2960 3200 +8.1% 0.93x
ArrayAppendLatin1Substring 16056 17352 +8.1% 0.93x (?)
ArrayAppendUTF16Substring 15804 17064 +8.0% 0.93x
ArrayAppendAsciiSubstring 15804 17064 +8.0% 0.93x
 
Improvement OLD NEW DELTA RATIO
Data.append.Sequence.64kB.Count.I 58 39 -32.8% 1.49x
Data.append.Sequence.64kB.Count 58 39 -32.8% 1.49x
Data.append.Sequence.809B.Count 113 91 -19.5% 1.24x
Data.append.Sequence.809B.Count.I 113 91 -19.5% 1.24x (?)
SuffixCountableRangeLazy 9 8 -11.1% 1.12x
LazilyFilteredArrayContains 16400 14900 -9.1% 1.10x (?)
CharIndexing_tweet_unicodeScalars 10400 9680 -6.9% 1.07x (?)
CharIndexing_korean_unicodeScalars_Backwards 6360 5920 -6.9% 1.07x (?)
CharIteration_korean_unicodeScalars_Backwards 6360 5920 -6.9% 1.07x (?)
CharIndexing_korean_unicodeScalars 6360 5920 -6.9% 1.07x (?)
CharIndexing_ascii_unicodeScalars 5240 4880 -6.9% 1.07x
CharIteration_russian_unicodeScalars_Backwards 5400 5040 -6.7% 1.07x
CharIndexing_punctuated_unicodeScalars 1200 1120 -6.7% 1.07x
CharIndexing_punctuatedJapanese_unicodeScalars 1200 1120 -6.7% 1.07x (?)

Code size: -O

Improvement OLD NEW DELTA RATIO
Radix2CooleyTukey.o 7464 6232 -16.5% 1.20x
LuhnAlgoEager.o 12774 11282 -11.7% 1.13x
LuhnAlgoLazy.o 12774 11282 -11.7% 1.13x
CharacterProperties.o 23547 20963 -11.0% 1.12x
Diffing.o 8691 7899 -9.1% 1.10x
AngryPhonebook.o 9666 8786 -9.1% 1.10x
OpaqueConsumingUsers.o 2407 2231 -7.3% 1.08x
PolymorphicCalls.o 13557 12765 -5.8% 1.06x
SuperChars.o 1601 1513 -5.5% 1.06x
IndexPathTest.o 9064 8584 -5.3% 1.06x
Chars.o 1710 1622 -5.1% 1.05x
RandomShuffle.o 3582 3406 -4.9% 1.05x
Ackermann.o 1916 1828 -4.6% 1.05x
IterateData.o 1992 1904 -4.4% 1.05x
StringComparison.o 38856 37272 -4.1% 1.04x
ArrayInClass.o 4319 4143 -4.1% 1.04x
LinkedList.o 2295 2207 -3.8% 1.04x
InsertCharacter.o 4747 4571 -3.7% 1.04x
StringReplaceSubrange.o 5259 5069 -3.6% 1.04x
UTF8Decode.o 24154 23362 -3.3% 1.03x
Substring.o 19128 18512 -3.2% 1.03x
RomanNumbers.o 5830 5654 -3.0% 1.03x
FindStringNaive.o 8899 8635 -3.0% 1.03x
Array2D.o 3021 2933 -2.9% 1.03x
Histogram.o 3032 2944 -2.9% 1.03x
FloatingPointParsing.o 18722 18194 -2.8% 1.03x
CString.o 6527 6351 -2.7% 1.03x
DiffingMyers.o 6685 6509 -2.6% 1.03x
DictionaryRemove.o 13736 13384 -2.6% 1.03x
ClassArrayGetter.o 3456 3368 -2.5% 1.03x
SequenceAlgos.o 21020 20492 -2.5% 1.03x
DictionaryCompactMapValues.o 14197 13845 -2.5% 1.03x
RC4.o 3593 3505 -2.4% 1.03x
StringWalk.o 38239 37359 -2.3% 1.02x
FlattenList.o 4002 3914 -2.2% 1.02x
DictionaryOfAnyHashableStrings.o 8016 7840 -2.2% 1.02x
ArrayAppend.o 24927 24399 -2.1% 1.02x
StringMatch.o 4202 4114 -2.1% 1.02x
TwoSum.o 4248 4160 -2.1% 1.02x
DictionaryCopy.o 8678 8502 -2.0% 1.02x
ReversedCollections.o 8726 8550 -2.0% 1.02x
BucketSort.o 8879 8703 -2.0% 1.02x
SortIntPyramids.o 8976 8800 -2.0% 1.02x
DictionarySwap.o 18137 17785 -1.9% 1.02x
DictTest.o 13908 13644 -1.9% 1.02x
SetTests.o 129285 126909 -1.8% 1.02x
NIOChannelPipeline.o 4827 4739 -1.8% 1.02x
Phonebook.o 10111 9935 -1.7% 1.02x
ObjectiveCBridgingStubs.o 15469 15205 -1.7% 1.02x
DictionaryBridgeToObjC.o 5289 5201 -1.7% 1.02x
ObjectiveCBridging.o 60221 59253 -1.6% 1.02x
RemoveWhere.o 16435 16171 -1.6% 1.02x
ArrayOfGenericPOD.o 5565 5477 -1.6% 1.02x
DataBenchmarks.o 61101 60223 -1.4% 1.01x
RandomTree.o 12271 12095 -1.4% 1.01x
SortStrings.o 24770 24418 -1.4% 1.01x
MapReduce.o 27817 27465 -1.3% 1.01x
LazyFilter.o 8080 7992 -1.1% 1.01x
Differentiation.o 8123 8035 -1.1% 1.01x
ObjectiveCNoBridgingStubs.o 8312 8224 -1.1% 1.01x

Performance: -Osize

Regression OLD NEW DELTA RATIO
DropLastAnyCollection 59 71 +20.3% 0.83x
DropWhileAnySeqCRangeIter 183 218 +19.1% 0.84x
DictionaryKeysContainsCocoa 31 35 +12.9% 0.89x (?)
ObjectiveCBridgeStubDateAccess 228 257 +12.7% 0.89x
DropWhileAnyCollectionLazy 247 276 +11.7% 0.89x
DropFirstAnySeqCntRangeLazy 216 241 +11.6% 0.90x
DropFirstAnySeqCRangeIterLazy 216 240 +11.1% 0.90x (?)
DropFirstAnyCollection 164 182 +11.0% 0.90x (?)
CharIteration_russian_unicodeScalars 3280 3600 +9.8% 0.91x (?)
DataCountMedium 31 34 +9.7% 0.91x
PrefixAnySeqCntRange 176 193 +9.7% 0.91x (?)
PrefixAnyCollection 182 199 +9.3% 0.91x (?)
DropLastCountableRange 11 12 +9.1% 0.92x
ReversedDictionary2 315 342 +8.6% 0.92x
DropWhileAnySeqCntRange 201 218 +8.5% 0.92x
CharIteration_chinese_unicodeScalars 2880 3120 +8.3% 0.92x (?)
PrefixWhileAnySeqCntRangeLazy 176 190 +8.0% 0.93x (?)
 
Improvement OLD NEW DELTA RATIO
MapReduceLazyCollectionShort 88 64 -27.3% 1.37x
LuhnAlgoEager 948 803 -15.3% 1.18x (?)
RandomShuffleLCG2 480 416 -13.3% 1.15x
ArrayAppendLatin1Substring 32832 28728 -12.5% 1.14x (?)
ArrayAppendAsciiSubstring 32112 28152 -12.3% 1.14x
ArrayAppendUTF16Substring 32040 28152 -12.1% 1.14x
LuhnAlgoLazy 965 852 -11.7% 1.13x
ConvertFloatingPoint.MockFloat64Exactly 9 8 -11.1% 1.12x
PrefixAnySeqCRangeIter 193 175 -9.3% 1.10x (?)
DataCountSmall 34 31 -8.8% 1.10x (?)
SuffixCountableRange 12 11 -8.3% 1.09x
SuffixCountableRangeLazy 12 11 -8.3% 1.09x
ProtocolDispatch 371 342 -7.8% 1.08x (?)
ConvertFloatingPoint.MockFloat64Exactly2 13 12 -7.7% 1.08x (?)

Code size: -Osize

Improvement OLD NEW DELTA RATIO
LuhnAlgoEager.o 14909 12187 -18.3% 1.22x
LuhnAlgoLazy.o 14909 12187 -18.3% 1.22x
Radix2CooleyTukey.o 6846 5670 -17.2% 1.21x
CharacterProperties.o 22472 19993 -11.0% 1.12x
Diffing.o 8019 7263 -9.4% 1.10x
AngryPhonebook.o 9058 8218 -9.3% 1.10x
OpaqueConsumingUsers.o 2221 2053 -7.6% 1.08x
PolymorphicCalls.o 12373 11617 -6.1% 1.07x
IndexPathTest.o 7667 7210 -6.0% 1.06x
SuperChars.o 1468 1384 -5.7% 1.06x
Chars.o 1544 1460 -5.4% 1.06x
RandomShuffle.o 3475 3307 -4.8% 1.05x
IterateData.o 1740 1656 -4.8% 1.05x
Ackermann.o 1746 1662 -4.8% 1.05x
StringComparison.o 34545 33033 -4.4% 1.05x
InsertCharacter.o 4131 3963 -4.1% 1.04x
LinkedList.o 2139 2055 -3.9% 1.04x
StringReplaceSubrange.o 4709 4527 -3.9% 1.04x
ArrayInClass.o 4404 4236 -3.8% 1.04x
Histogram.o 2372 2288 -3.5% 1.04x
UTF8Decode.o 22484 21728 -3.4% 1.03x
Substring.o 17562 16974 -3.3% 1.03x
Array2D.o 2598 2514 -3.2% 1.03x
DictionaryRemove.o 10709 10373 -3.1% 1.03x
RomanNumbers.o 5446 5278 -3.1% 1.03x
FindStringNaive.o 8195 7943 -3.1% 1.03x
FloatingPointParsing.o 17148 16644 -2.9% 1.03x
RC4.o 2937 2853 -2.9% 1.03x
CString.o 6037 5869 -2.8% 1.03x
DictionaryCompactMapValues.o 12451 12115 -2.7% 1.03x
ClassArrayGetter.o 3187 3103 -2.6% 1.03x
DiffingMyers.o 6416 6248 -2.6% 1.03x
StringWalk.o 32539 31699 -2.6% 1.03x
DictionaryOfAnyHashableStrings.o 6531 6363 -2.6% 1.03x
TwoSum.o 3380 3296 -2.5% 1.03x
SequenceAlgos.o 20758 20254 -2.4% 1.02x
DictionaryCopy.o 7282 7114 -2.3% 1.02x
ArrayAppend.o 22249 21745 -2.3% 1.02x
FlattenList.o 3725 3641 -2.3% 1.02x
DictTest.o 11939 11687 -2.1% 1.02x
StringMatch.o 4015 3931 -2.1% 1.02x
DictionarySwap.o 16100 15764 -2.1% 1.02x
ReversedCollections.o 8055 7887 -2.1% 1.02x
BucketSort.o 8267 8099 -2.0% 1.02x
SetTests.o 111789 109521 -2.0% 1.02x
DictionaryBridgeToObjC.o 4275 4191 -2.0% 1.02x
SortIntPyramids.o 8662 8494 -1.9% 1.02x
NIOChannelPipeline.o 4331 4247 -1.9% 1.02x
RemoveWhere.o 13231 12979 -1.9% 1.02x
Phonebook.o 9123 8955 -1.8% 1.02x
ObjectiveCBridgingStubs.o 13914 13662 -1.8% 1.02x
ObjectiveCBridging.o 54734 53810 -1.7% 1.02x
ArrayOfGenericPOD.o 5268 5180 -1.7% 1.02x
DataBenchmarks.o 51548 50710 -1.6% 1.02x
MapReduce.o 20748 20412 -1.6% 1.02x
RandomTree.o 11581 11413 -1.5% 1.01x
SortStrings.o 26996 26660 -1.2% 1.01x
LazyFilter.o 7410 7326 -1.1% 1.01x
Differentiation.o 7472 7388 -1.1% 1.01x
ObjectiveCNoBridgingStubs.o 7727 7643 -1.1% 1.01x

Performance: -Onone

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

@eeckstein
Copy link
Contributor Author

@swift-ci test linux

@swift-ci
Copy link
Contributor

swift-ci commented Feb 8, 2021

Build failed
Swift Test OS X Platform
Git Sha - 383f8f6ea819450f42d617be5219ae4344957e8e

@eeckstein
Copy link
Contributor Author

@swift-ci test macOS

@swift-ci
Copy link
Contributor

swift-ci commented Feb 8, 2021

Build failed
Swift Test Linux Platform
Git Sha - 383f8f6ea819450f42d617be5219ae4344957e8e

@eeckstein eeckstein force-pushed the static-function-arrays branch from 383f8f6 to 2cb0cf9 Compare February 9, 2021 16:16
@eeckstein
Copy link
Contributor Author

@swift-ci test

@swift-ci
Copy link
Contributor

swift-ci commented Feb 9, 2021

Build failed
Swift Test OS X Platform
Git Sha - 2cb0cf98e713d29b436ea6c6a9708ee9a46d3d86

If we know that we have a FunctionRefInst (and not another variant of FunctionRefBaseInst), we know that getting the referenced function will not be null (in contrast to FunctionRefBaseInst::getReferencedFunctionOrNull).

NFC
…uctions in an initializer of a SILGlobalVariable.

This means: don't require to have a SILFunction in the SILBuilder.
It makes sense to to this in a single pass, because there might be dead cycles for globals and functions, e.g. if a global references a function in its static initializer and the function references that global.

Another improvement: eliminate dead global-initializers. Before we had an explicit SIL representation of statically initialized globals, we had to keep them alive.

rdar://32956923
The main change is to rename DeadFunctionElimination -> DeadFunctionAndGlobalElimination, because the pass is now also doing dead-global elimination.
A second change is to remove the FunctionLivenessComputation base class. It’s not used anywhere else.
…in global variables.

We cannot replace a load from a global let-variable with a function_ref, if the referenced function would violate the resilience rules.
That means if a non-public function_ref would be inlined into a function which is serialized.
Just moving a few functions around and make emitConstantValue the main entry point for creating constants.

NFC
…obal arrays.

For example, generate a static global for the array literal in:

func foo(_ i: Int) -> Int { ... }
func bar(_ i: Int) -> Int { ... }

func returnFunctionArray() -> [(Int) -> Int] {
  return [foo, bar]
}

rdar://73570149
https://bugs.swift.org/browse/SR-14101
@eeckstein eeckstein force-pushed the static-function-arrays branch from 2cb0cf9 to 0a483a6 Compare February 9, 2021 18:57
@eeckstein
Copy link
Contributor Author

@swift-ci test

@eeckstein eeckstein merged commit a2996b7 into swiftlang:main Feb 10, 2021
@eeckstein eeckstein deleted the static-function-arrays branch February 10, 2021 08:02
eeckstein added a commit to eeckstein/swift that referenced this pull request Feb 16, 2021
In case a function is only referenced from a global without a declaration (e.g. an outlined global), we could end up with a null IRGenModule for the function (with multithreaded compilation). In this case, just use the primary IGM.
This fixes a IRGen crash, introduced with swiftlang#35780

rdar://74358251
eeckstein added a commit to eeckstein/swift that referenced this pull request Feb 16, 2021
…lobals.

This fixes a IRGen crash, introduced with swiftlang#35780

rdar://74358259
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