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

[AMDGPU][GlobalISel] Constant shift amounts of split packed 16-bit shifts are not folded after RegBankLegalize

Open petar-avramovic opened this issue 1 month ago • 10 comments

RegBankLegalize lowers a uniform <2 x i16> shift into two 32-bit SALU shifts. The shift amounts come from the packed constant vector: a G_BITCAST of the G_BUILD_VECTOR of constants, then G_LSHR / G_SEXT_INREG of that for each half. Nothing after RegBankLegalize folds these back to constants, so the low half is shifted by the packed value (0xb000b) and the amounts are recomputed on the SALU.

Reproducer (from llvm/test/CodeGen/AMDGPU/GlobalISel/sext_inreg.ll, s_sext_inreg_v2i16_11):

define amdgpu_ps i32 @s_sext_inreg_v2i16_11(<2 x i16> inreg %value) {
  %shl = shl <2 x i16> %value, <i16 11, i16 11>
  %ashr = ashr <2 x i16> %shl, <i16 11, i16 11>
  %cast = bitcast <2 x i16> %ashr to i32
  ret i32 %cast
}

llc -global-isel -mtriple=amdgpu9.00-amd-amdpal test.ll -o -

MIR after amdgpu-regbank-combiner:

    %9:sgpr(i16) = G_CONSTANT i16 11
    %10:sgpr(<2 x i16>) = G_BUILD_VECTOR %9(i16), %9(i16)
    ...
    %14:sgpr(i32) = G_BITCAST %10(<2 x i16>)
    %15:sgpr(i32) = G_LSHR %14, %12(i32)
    %16:sgpr(i32) = G_SHL %11, %14(i32)
    %17:sgpr(i32) = G_SHL %13, %15(i32)
    ...
    %21:sgpr(i32) = G_BITCAST %10(<2 x i16>)
    %22:sgpr(i32) = G_SEXT_INREG %21, 16
    %23:sgpr(i32) = G_ASHR %21, %12(i32)
    %24:sgpr(i32) = G_ASHR %19, %22(i32)
    %25:sgpr(i32) = G_ASHR %20, %23(i32)

Current GlobalISel output:

	s_lshr_b32 s1, s0, 16
	s_lshl_b32 s0, s0, 0xb000b
	s_lshl_b32 s1, s1, 11
	s_pack_ll_b32_b16 s0, s0, s1
	s_sext_i32_i16 s1, s0
	s_ashr_i32 s0, s0, 16
	s_sext_i32_i16 s2, 0xb000b
	s_ashr_i32 s1, s1, s2
	s_ashr_i32 s0, s0, 11
	s_pack_ll_b32_b16 s0, s1, s0

Desired: every shift by the immediate 11, with no s_sext_i32_i16 of the constant and no 0xb000b literal.

Possible fix: in the AMDGPU regbank combiner, fold

  • G_BITCAST of a G_BUILD_VECTOR of constants to one constant,
  • binary operations / G_SEXT_INREG on constants (constant_fold_binop and similar generic rules),
  • a constant shift amount >= the shift width to (amount & (width - 1)), since the 32-bit shift only reads those bits.

A local patch with the first rule, constant_fold_binop and the shift amount mask only fixes the s_lshl_b32 by 0xb000b here (it becomes 11). The s_sext_i32_i16 of 0xb000b and the shift by s2 remain. Same pattern in GlobalISel/{fshl,fshr,saddsat,ssubsat,uaddsat,usubsat,shl-ext-reduce}.ll.

petar-avramovic avatar Oct 28 '26 17:10 petar-avramovic

Hi!

This issue may be a good introductory issue for people new to working on LLVM. If you would like to work on this issue, your first steps are:

  1. Check that no other contributor is working on this issue. If someone is assigned to the issue or claimed to be working on it, ping the person. After one week without a response, the assignee may be changed.
  2. Leave a comment indicating that you are working on the issue, or just create a pull request after following the steps below. Mention this issue in the description of the pull request.
  3. Fix the issue locally. Be aware that LLVM's AI policy forbids the use of AI tools to fix good first issues since it squanders a learning opportunity. Using LLMs to search and learn is permitted, since the goal is learning.
  4. Run the test suite locally. Remember that the subdirectories under test/ create fine-grained testing targets, so you can e.g. use make check-clang-ast to only run Clang's AST tests.
  5. Create a Git commit.
  6. Run git clang-format HEAD~1 to format your changes.
  7. Open a pull request to the upstream repository on GitHub. Detailed instructions can be found in GitHub's documentation. Mention this issue in the description of the pull request.

If you have any further questions about this issue, don't hesitate to ask via a comment in the thread below.

@llvm/issue-subscribers-good-first-issue

Author: Petar Avramovic (petar-avramovic)

RegBankLegalize lowers a uniform <2 x i16> shift into two 32-bit SALU shifts. The shift amounts come from the packed constant vector: a G_BITCAST of the G_BUILD_VECTOR of constants, then G_LSHR / G_SEXT_INREG of that for each half. Nothing after RegBankLegalize folds these back to constants, so the low half is shifted by the packed value (0xb000b) and the amounts are recomputed on the SALU.

Reproducer (from llvm/test/CodeGen/AMDGPU/GlobalISel/sext_inreg.ll, s_sext_inreg_v2i16_11):

define amdgpu_ps i32 @<!-- -->s_sext_inreg_v2i16_11(&lt;2 x i16&gt; inreg %value) {
  %shl = shl &lt;2 x i16&gt; %value, &lt;i16 11, i16 11&gt;
  %ashr = ashr &lt;2 x i16&gt; %shl, &lt;i16 11, i16 11&gt;
  %cast = bitcast &lt;2 x i16&gt; %ashr to i32
  ret i32 %cast
}

llc -global-isel -mtriple=amdgpu9.00-amd-amdpal test.ll -o -

MIR after amdgpu-regbank-combiner:

    %9:sgpr(i16) = G_CONSTANT i16 11
    %10:sgpr(&lt;2 x i16&gt;) = G_BUILD_VECTOR %9(i16), %9(i16)
    ...
    %14:sgpr(i32) = G_BITCAST %10(&lt;2 x i16&gt;)
    %15:sgpr(i32) = G_LSHR %14, %12(i32)
    %16:sgpr(i32) = G_SHL %11, %14(i32)
    %17:sgpr(i32) = G_SHL %13, %15(i32)
    ...
    %21:sgpr(i32) = G_BITCAST %10(&lt;2 x i16&gt;)
    %22:sgpr(i32) = G_SEXT_INREG %21, 16
    %23:sgpr(i32) = G_ASHR %21, %12(i32)
    %24:sgpr(i32) = G_ASHR %19, %22(i32)
    %25:sgpr(i32) = G_ASHR %20, %23(i32)

Current GlobalISel output:

	s_lshr_b32 s1, s0, 16
	s_lshl_b32 s0, s0, 0xb000b
	s_lshl_b32 s1, s1, 11
	s_pack_ll_b32_b16 s0, s0, s1
	s_sext_i32_i16 s1, s0
	s_ashr_i32 s0, s0, 16
	s_sext_i32_i16 s2, 0xb000b
	s_ashr_i32 s1, s1, s2
	s_ashr_i32 s0, s0, 11
	s_pack_ll_b32_b16 s0, s1, s0

Desired: every shift by the immediate 11, with no s_sext_i32_i16 of the constant and no 0xb000b literal.

Possible fix: in the AMDGPU regbank combiner, fold

  • G_BITCAST of a G_BUILD_VECTOR of constants to one constant,
  • binary operations / G_SEXT_INREG on constants (constant_fold_binop and similar generic rules),
  • a constant shift amount >= the shift width to (amount & (width - 1)), since the 32-bit shift only reads those bits.

A local patch with the first rule, constant_fold_binop and the shift amount mask only fixes the s_lshl_b32 by 0xb000b here (it becomes 11). The s_sext_i32_i16 of 0xb000b and the shift by s2 remain. Same pattern in GlobalISel/{fshl,fshr,saddsat,ssubsat,uaddsat,usubsat,shl-ext-reduce}.ll.

@llvm/issue-subscribers-backend-amdgpu

Author: Petar Avramovic (petar-avramovic)

RegBankLegalize lowers a uniform <2 x i16> shift into two 32-bit SALU shifts. The shift amounts come from the packed constant vector: a G_BITCAST of the G_BUILD_VECTOR of constants, then G_LSHR / G_SEXT_INREG of that for each half. Nothing after RegBankLegalize folds these back to constants, so the low half is shifted by the packed value (0xb000b) and the amounts are recomputed on the SALU.

Reproducer (from llvm/test/CodeGen/AMDGPU/GlobalISel/sext_inreg.ll, s_sext_inreg_v2i16_11):

define amdgpu_ps i32 @<!-- -->s_sext_inreg_v2i16_11(&lt;2 x i16&gt; inreg %value) {
  %shl = shl &lt;2 x i16&gt; %value, &lt;i16 11, i16 11&gt;
  %ashr = ashr &lt;2 x i16&gt; %shl, &lt;i16 11, i16 11&gt;
  %cast = bitcast &lt;2 x i16&gt; %ashr to i32
  ret i32 %cast
}

llc -global-isel -mtriple=amdgpu9.00-amd-amdpal test.ll -o -

MIR after amdgpu-regbank-combiner:

    %9:sgpr(i16) = G_CONSTANT i16 11
    %10:sgpr(&lt;2 x i16&gt;) = G_BUILD_VECTOR %9(i16), %9(i16)
    ...
    %14:sgpr(i32) = G_BITCAST %10(&lt;2 x i16&gt;)
    %15:sgpr(i32) = G_LSHR %14, %12(i32)
    %16:sgpr(i32) = G_SHL %11, %14(i32)
    %17:sgpr(i32) = G_SHL %13, %15(i32)
    ...
    %21:sgpr(i32) = G_BITCAST %10(&lt;2 x i16&gt;)
    %22:sgpr(i32) = G_SEXT_INREG %21, 16
    %23:sgpr(i32) = G_ASHR %21, %12(i32)
    %24:sgpr(i32) = G_ASHR %19, %22(i32)
    %25:sgpr(i32) = G_ASHR %20, %23(i32)

Current GlobalISel output:

	s_lshr_b32 s1, s0, 16
	s_lshl_b32 s0, s0, 0xb000b
	s_lshl_b32 s1, s1, 11
	s_pack_ll_b32_b16 s0, s0, s1
	s_sext_i32_i16 s1, s0
	s_ashr_i32 s0, s0, 16
	s_sext_i32_i16 s2, 0xb000b
	s_ashr_i32 s1, s1, s2
	s_ashr_i32 s0, s0, 11
	s_pack_ll_b32_b16 s0, s1, s0

Desired: every shift by the immediate 11, with no s_sext_i32_i16 of the constant and no 0xb000b literal.

Possible fix: in the AMDGPU regbank combiner, fold

  • G_BITCAST of a G_BUILD_VECTOR of constants to one constant,
  • binary operations / G_SEXT_INREG on constants (constant_fold_binop and similar generic rules),
  • a constant shift amount >= the shift width to (amount & (width - 1)), since the 32-bit shift only reads those bits.

A local patch with the first rule, constant_fold_binop and the shift amount mask only fixes the s_lshl_b32 by 0xb000b here (it becomes 11). The s_sext_i32_i16 of 0xb000b and the shift by s2 remain. Same pattern in GlobalISel/{fshl,fshr,saddsat,ssubsat,uaddsat,usubsat,shl-ext-reduce}.ll.

I'd like to take this one 🙂

molaed avatar Oct 28 '26 18:10 molaed

@molaed hey you still working on this?

blenbot avatar Nov 10 '26 11:11 blenbot

I am making slow progress, so feel free to take it over. I've just been juggling a few other things.

molaed avatar Nov 15 '26 21:11 molaed

Hi, I would like to work on this!

Krishna-13-cyber avatar Nov 16 '26 16:11 Krishna-13-cyber

@petar-avramovic sir can I work on it

Hey @competitive-programmer04 , I have started working on the implementation, will open a PR soon, but if you have already worked on it, you can go ahead.

Krishna-13-cyber avatar Nov 17 '26 10:11 Krishna-13-cyber

@Krishna-13-cyber no I have not worked on it