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

Rewrite UnsafeRawBufferPointer.Iterator.next #76294

Merged
merged 2 commits into from
Sep 20, 2024

Conversation

meg-gupta
Copy link
Contributor

@meg-gupta meg-gupta commented Sep 5, 2024

In the SIL generated from the current implementation, it isn't clear to the optimizer that _position == nil && _end != nil can never happen. This ends up creating a non-natural loop and none of the SIL and LLVM loop passes work for such loops. We have to find a way to fix this in SIL. Until then, rewrite so we get a natural loop.

Note: UnsafeBufferPointer.Iterator.next does not have this issue

This function was unavailable in the debugger because it was getting
optimized away.
@meg-gupta meg-gupta marked this pull request as ready for review September 5, 2024 22:14
@meg-gupta meg-gupta requested review from a team and jckarter as code owners September 5, 2024 22:14
@glessard
Copy link
Contributor

glessard commented Sep 5, 2024

@swift-ci please test

…p in SIL

The current implementation creates a non-natural loop and none of the SIL and
LLVM loop passes will work for such loops. We have to find a way to fix this in
SIL. Until then, rewrite so we get a natural loop in SIL.
@meg-gupta meg-gupta force-pushed the rewritenext branch 2 times, most recently from d9f979b to 2f37e10 Compare September 5, 2024 22:39
@meg-gupta meg-gupta changed the title Rewrite UnsafeRawBufferPointer.Iterator.next to avoid non natural loop in SIL Rewrite UnsafeRawBufferPointer.Iterator.next Sep 5, 2024
@meg-gupta
Copy link
Contributor Author

@swift-ci test

@meg-gupta
Copy link
Contributor Author

@swift-ci Please Apple Silicon benchmark

@meg-gupta
Copy link
Contributor Author

------- Performance (arm64): -O -------

REGRESSION                                OLD       NEW        DELTA    RATIO    
ArrayAppendGenericStructs                 947.5     1053.636   +11.2%   **0.90x (?)**
SIMDRandomMask.Int8x16                    308.6     340.167    +10.2%   **0.91x (?)**
Diffing.Same                              2.577     2.84       +10.2%   **0.91x (?)**

IMPROVEMENT                               OLD       NEW        DELTA    RATIO    
UTF8Decode_InitDecoding                   131.25    96.238     -26.7%   **1.36x**
UTF8Decode_InitFromData                   132.067   96.95      -26.6%   **1.36x**
UTF8Decode_InitFromCustom_contiguous      131.706   97.957     -25.6%   **1.34x**
UTF8Decode_InitFromBytes                  137.8     103.526    -24.9%   **1.33x**
UTF8Decode_InitFromCustom_noncontiguous   151.0     116.368    -22.9%   **1.30x**
EqualSubstringString                      21.021    17.752     -15.6%   **1.18x**
EqualStringSubstring                      20.452    17.325     -15.3%   **1.18x**
EqualSubstringSubstring                   20.826    17.75      -14.8%   **1.17x**
LessSubstringSubstringGenericComparable   20.8      17.752     -14.7%   **1.17x**
LessSubstringSubstring                    20.78     17.752     -14.6%   **1.17x**
EqualSubstringSubstringGenericEquatable   20.281    17.752     -12.5%   **1.14x**
ObserverUnappliedMethod                   283.333   249.767    -11.8%   **1.13x (?)**

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


####### Testing optimization level -Osize #######

------- Performance (arm64): -Osize -------

REGRESSION                                OLD       NEW       DELTA    RATIO    
Diffing.Same                              2.538     2.846     +12.1%   **0.89x**
FlattenListFlatMap                        2465.0    2678.0    +8.6%    **0.92x (?)**

IMPROVEMENT                               OLD       NEW       DELTA    RATIO    
UTF8Decode_InitDecoding                   132.938   96.286    -27.6%   **1.38x**
UTF8Decode_InitFromData                   131.941   96.619    -26.8%   **1.37x**
UTF8Decode_InitFromCustom_contiguous      131.706   96.545    -26.7%   **1.36x**
UTF8Decode_InitFromBytes                  137.313   101.9     -25.8%   **1.35x**
UTF8Decode_InitFromCustom_noncontiguous   227.6     190.083   -16.5%   **1.20x**
EqualStringSubstring                      20.391    17.41     -14.6%   **1.17x**
EqualSubstringString                      20.427    17.481    -14.4%   **1.17x**
EqualSubstringSubstringGenericEquatable   20.604    17.639    -14.4%   **1.17x**
EqualSubstringSubstring                   20.5      17.635    -14.0%   **1.16x**
LessSubstringSubstring                    20.396    17.619    -13.6%   **1.16x (?)**
LessSubstringSubstringGenericComparable   20.521    18.053    -12.0%   **1.14x**

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

------- Performance (arm64): -Onone -------

REGRESSION                                OLD        NEW        DELTA    RATIO      
RawBufferCopyBytes                        10.015     12.455     +24.4%   **0.80x (?)**
Diffing.Same                              2.519      2.826      +12.2%   **0.89x (?)**
RawBuffer.1000.findLast                   44984.0    50271.0    +11.8%   **0.89x**  
RawBuffer.128.findLast                    6272.0     6943.0     +10.7%   **0.90x (?)**
RawBuffer.39.findLast                     2381.0     2571.0     +8.0%    **0.93x (?)**

IMPROVEMENT                               OLD        NEW        DELTA    RATIO      
ObjectiveCBridgeASCIIStringFromFile       0.12       0.0        -99.2%   **121.00x (?)**
UTF8Decode_InitFromData                   132.333    97.211     -26.5%   **1.36x**  
UTF8Decode_InitDecoding                   134.714    100.0      -25.8%   **1.35x**  
UTF8Decode_InitFromBytes                  138.063    102.526    -25.7%   **1.35x**  
UTF8Decode_InitFromCustom_contiguous      135.353    100.8      -25.5%   **1.34x**  
EqualSubstringSubstringGenericEquatable   22.892     19.54      -14.6%   **1.17x**  
LessSubstringSubstringGenericComparable   22.775     19.539     -14.2%   **1.17x**  
EqualSubstringString                      24.35      20.891     -14.2%   **1.17x**  
EqualStringSubstring                      24.048     20.723     -13.8%   **1.16x**  
LessSubstringSubstring                    23.592     20.413     -13.5%   **1.16x**  
EqualSubstringSubstring                   23.013     20.261     -12.0%   **1.14x**  
ArrayAppendGenericStructs                 1199.091   1080.0     -9.9%    **1.11x (?)**
DataAppendBytesMedium                     1714.615   1563.846   -8.8%    **1.10x (?)**
ArrayAppendToGeneric                      201.667    184.706    -8.4%    **1.09x (?)**

Copy link
Contributor

@glessard glessard left a comment

Choose a reason for hiding this comment

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

Nice!

@meg-gupta meg-gupta merged commit 961e6ea into swiftlang:main Sep 20, 2024
6 checks passed
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