Repository navigation
Peephole optimization for VPCMPEQD + VGATHERDPS - #6011
Merged
Merged
Conversation
This peephole removes unnecessary masking operations when the mask is provably all 1s. This improves performance in GB7 'Asset Compression' by about 2% with SVE and about 4% without SVE.
Member
|
I think it is fine to do peephole optimizations like this. I'll do some additional testing today on this just in case although. |
Sonicadvance1
approved these changes
Oct 7, 2026
Sonicadvance1
left a comment
Member
There was a problem hiding this comment.
Added some detection logs to this just to ensure it was getting hit in real games. Tunic hits this optimization as expected and the music still works. So looks good to me.
Contributor
Author
|
@Sonicadvance1 Thanks for taking a look! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
In the hot loop for GB7 Asset Compression, a code sequence like this is very common:
vpcmpeqd ymm1, ymm1, ymm1vgatherdps ymm2, dword ptr [...], ymm1This is a compiler idiom, emitted by clang, for an unmasked gather. You can see an example of this codegen here: https://godbolt.org/z/4devEvfKf
By recognizing that the mask register was the result of a self-compare, and therefore is a mask of all ones, we can specialize the codegen for vgather* and skip expensive mask operations. This improves performance in GB7 Asset Compression by about 2% in the SVE case, and 4% in the non-SVE case, as tested on Snapdragon X2 Elite.
This is I believe the first optimization of its kind in FEX, so I am more than open to feedback here. The mechanism we add here to track where a value came from can also be used to fuse some other instructions. I believe that detecting this case in the "x64 domain" is simpler than adding a new pass over the IR.