llvm-project
llvm-project copied to clipboard
AMDGPU fails to form BFE with 16-bit patterns
These functions should be able to equivalently use v_bfe_u32, but the 8/16-bit version does not:
https://godbolt.org/z/T3EaTb11e
; v_bfe_u32 v0, v0, 4, 4
define i32 @bfe_32(i32 %a) {
%shr.i = lshr i32 %a, 4
%and = and i32 %shr.i, 15
ret i32 %and
}
; v_lshrrev_b16_e32 v0, 4, v0
; v_and_b32_e32 v0, 15, v0
define i16 @bfe_16(i16 %a) {
%shr.i = lshr i16 %a, 4
%and = and i16 %shr.i, 15
ret i16 %and
}
; v_lshrrev_b16_e32 v0, 4, v0
; v_and_b32_e32 v0, 15, v0
define i8 @bfe_8(i8 %a) {
%shr.i = lshr i8 %a, 4
%and = and i8 %shr.i, 15
ret i8 %and
}
@llvm/issue-subscribers-backend-amdgpu
Author: Matt Arsenault (arsenm)
These functions should be able to equivalently use v_bfe_u32, but the 16-bit version does not:
https://godbolt.org/z/n5EG4Mh3z
; v_bfe_u32 v0, v0, 4, 4
define i32 @<!-- -->bfe_32(i32 %a) {
%shr.i = lshr i32 %a, 4
%and = and i32 %shr.i, 15
ret i32 %and
}
; v_lshrrev_b16_e32 v0, 4, v0
; v_and_b32_e32 v0, 15, v0
define i16 @<!-- -->bfe_16(i16 %a) {
%shr.i = lshr i16 %a, 4
%and = and i16 %shr.i, 15
ret i16 %and
}
Any progress on this?
Added a PR.
Added some optimization to prevent BFE combines for cases where I saw performance regression in the tests. These are those, let me know if anything feels too much optimization,
- Mask == 1: single-bit extracts from scalarized
vectors fold better as a combined wider AND mask. - True16 targets (i16): native v_lshrrev_b16 + v_and_b16 is cheaper than promoting to i32 for BFE.
- SDWA targets (8/16-bit fields): the SDWA peephole pass can fold the mask into a consumer instruction's operand selector.
resolved on main