llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[X86] Missed fold: `vgf2p8affineqb` + 8-bit shifts

Open WalterKruger opened this issue 10 months ago • 4 comments

Performing a 8-bit shift on vgf2p8affineqb can be folded by together by shifting its matrix by 8 times the shift amount in the opposite direction. The position of each matrix byte determines a corresponding bit in the output, in reverse order, so shifting the matrix is equivalent. (Further reading: https://wunkolo.github.io/post/2020/11/gf2p8affineqb-int8-shifting/)

This applies to logical, rotational, and arithmetic shifts in either direction (but with the arithmetic shifting-in the entire least significant byte rather then LS bit). Only seem beneficial when the matrix and shift amount are constant.

bitRevSHL1_clang:
        gf2p8affineqb   xmm0, xmmword ptr [rip + .LCPI0_0], 0
        paddb   xmm0, xmm0
        ret
bitRevSHL1_tgt:
        gf2p8affineqb   xmm0, xmmword ptr [rip + .LCPI0_0], 0
        ret

https://godbolt.org/z/rd7frehEd

WalterKruger avatar Jan 30 '26 00:01 WalterKruger

@llvm/issue-subscribers-backend-x86

Author: Walter (WalterKruger)

Performing a 8-bit shift on `vgf2p8affineqb` can be folded by together by shifting its matrix by 8 times the shift amount in the opposite direction. The position of each matrix byte determines a corresponding bit in the output, in reverse order, so shifting the matrix is equivalent. (Further reading: https://wunkolo.github.io/post/2020/11/gf2p8affineqb-int8-shifting/)

This applies to logical, rotational, and arithmetic shifts in either direction (but with the arithmetic shifting-in the entire least significant byte rather then LS bit). Only seem beneficial when the matrix and shift amount are constant.

bitRevSHL1_clang:
        gf2p8affineqb   xmm0, xmmword ptr [rip + .LCPI0_0], 0
        paddb   xmm0, xmm0
        ret
bitRevSHL1_tgt:
        gf2p8affineqb   xmm0, xmmword ptr [rip + .LCPI0_0], 0
        ret

https://godbolt.org/z/rd7frehEd

llvmbot avatar Jan 30 '26 00:01 llvmbot

I forgot to mention, the immediate also needs to be shifted in the same way as the unfolded for correctness.

WalterKruger avatar Jan 30 '26 01:01 WalterKruger

@WalterKruger Can i work on this ?

Abhiramjampani avatar Feb 03 '26 07:02 Abhiramjampani

Hi @WalterKruger @RKSimon, I’ve submitted a PR addressing this issue: #180019. Thanks!

Abhiramjampani avatar Feb 06 '26 12:02 Abhiramjampani