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

AMDGPU fails to form BFE with 16-bit patterns

Open arsenm opened this issue 10 months ago • 4 comments

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
}


arsenm avatar Feb 03 '26 16:02 arsenm

@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
}

llvmbot avatar Feb 03 '26 16:02 llvmbot

Any progress on this?

arsenm avatar Feb 19 '26 20:02 arsenm

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.

VigneshwarJ avatar Feb 20 '26 07:02 VigneshwarJ

resolved on main

tgymnich avatar Aug 14 '26 17:08 tgymnich