Skip to content

Peephole optimization for VPCMPEQD + VGATHERDPS - #6011

Merged
Sonicadvance1 merged 4 commits into
FEX-Emu:mainfrom
javelina-pkwy:perf/gather-all-ones
Oct 7, 2026
Merged

Sonicadvance1 merged 4 commits into
FEX-Emu:mainfrom
javelina-pkwy:perf/gather-all-ones

Conversation

@javelina-pkwy

@javelina-pkwy javelina-pkwy commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

In the hot loop for GB7 Asset Compression, a code sequence like this is very common:

vpcmpeqd ymm1, ymm1, ymm1
vgatherdps ymm2, dword ptr [...], ymm1

This 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.

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.
@Sonicadvance1

Copy link
Copy Markdown
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 Sonicadvance1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

@Sonicadvance1
Sonicadvance1 merged commit e9373bf into FEX-Emu:main Oct 7, 2026
13 checks passed
@javelina-pkwy

Copy link
Copy Markdown
Contributor Author

@Sonicadvance1 Thanks for taking a look!

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