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

[AMDGPU][GlobalISel][True16] G_ANYEXT 16 -> 32 in VGPRs is selected as a size-mismatched COPY

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

With real true16, a vgpr G_ANYEXT from 16 to 32 bits is selected as a plain COPY from a vgpr_16 to a vgpr_32. Later passes then have to deal with a copy whose sizes do not match. Selecting it as

  %undef:vgpr_16 = IMPLICIT_DEF
  %dst:vgpr_32   = REG_SEQUENCE %src, %subreg.lo16, %undef, %subreg.hi16

says the value is the low half and gives better code in some cases. The returned f16 value below is an example.

Reproducer (from llvm/test/CodeGen/AMDGPU/llvm.frexp.ll, test_frexp_f16_i32):

define { half, i32 } @test_frexp_f16_i32(half %a) {
  %result = call { half, i32 } @llvm.frexp.f16.i32(half %a)
  ret { half, i32 } %result
}

llc -global-isel -mtriple=amdgpu11.00 -mattr=+real-true16 test.ll -o -

MIR after instruction-select:

    %14:vgpr_16 = nofpexcept V_FREXP_MANT_F16_t16_e64 0, %10, 0, 0, 0, implicit $mode, implicit $exec
    ...
    %13:vgpr_32 = COPY %14
    $vgpr0 = COPY %13

Current GlobalISel output:

	s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
	v_frexp_mant_f16_e32 v1.l, v0.l
	v_frexp_exp_i16_f16_e32 v0.l, v0.l
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_mov_b16_e32 v2.l, v1.l
	v_bfe_i32 v1, v0, 0, 16
	s_delay_alu instid0(VALU_DEP_2)
	v_mov_b32_e32 v0, v2
	s_setpc_b64 s[30:31]

Desired output (same as SelectionDAG):

	s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
	v_frexp_exp_i16_f16_e32 v1.l, v0.l
	v_frexp_mant_f16_e32 v0.l, v0.l
	s_delay_alu instid0(VALU_DEP_2)
	v_bfe_i32 v1, v1, 0, 16
	s_setpc_b64 s[30:31]

Note: switching all such G_ANYEXTs to REG_SEQUENCE is not a pure win. It fixes this case but makes others worse, e.g. llvm.fma.f16 (fma.f16.ll, GlobalISel/fma.ll v_fma_f16):

	v_fma_f16 v0.l, v0.l, v1.l, v2.l

becomes

	v_fmac_f16_e32 v2.l, v0.l, v1.l
	v_mov_b32_e32 v0, v2

So the fix needs to be more selective, or later passes need to handle the REG_SEQUENCE form as well as they handle the COPY.

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

@llvm/issue-subscribers-backend-amdgpu

Author: Petar Avramovic (petar-avramovic)

With real true16, a vgpr G_ANYEXT from 16 to 32 bits is selected as a plain COPY from a vgpr_16 to a vgpr_32. Later passes then have to deal with a copy whose sizes do not match. Selecting it as
  %undef:vgpr_16 = IMPLICIT_DEF
  %dst:vgpr_32   = REG_SEQUENCE %src, %subreg.lo16, %undef, %subreg.hi16

says the value is the low half and gives better code in some cases. The returned f16 value below is an example.

Reproducer (from llvm/test/CodeGen/AMDGPU/llvm.frexp.ll, test_frexp_f16_i32):

define { half, i32 } @<!-- -->test_frexp_f16_i32(half %a) {
  %result = call { half, i32 } @<!-- -->llvm.frexp.f16.i32(half %a)
  ret { half, i32 } %result
}

llc -global-isel -mtriple=amdgpu11.00 -mattr=+real-true16 test.ll -o -

MIR after instruction-select:

    %14:vgpr_16 = nofpexcept V_FREXP_MANT_F16_t16_e64 0, %10, 0, 0, 0, implicit $mode, implicit $exec
    ...
    %13:vgpr_32 = COPY %14
    $vgpr0 = COPY %13

Current GlobalISel output:

	s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
	v_frexp_mant_f16_e32 v1.l, v0.l
	v_frexp_exp_i16_f16_e32 v0.l, v0.l
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_mov_b16_e32 v2.l, v1.l
	v_bfe_i32 v1, v0, 0, 16
	s_delay_alu instid0(VALU_DEP_2)
	v_mov_b32_e32 v0, v2
	s_setpc_b64 s[30:31]

Desired output (same as SelectionDAG):

	s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
	v_frexp_exp_i16_f16_e32 v1.l, v0.l
	v_frexp_mant_f16_e32 v0.l, v0.l
	s_delay_alu instid0(VALU_DEP_2)
	v_bfe_i32 v1, v1, 0, 16
	s_setpc_b64 s[30:31]

Note: switching all such G_ANYEXTs to REG_SEQUENCE is not a pure win. It fixes this case but makes others worse, e.g. llvm.fma.f16 (fma.f16.ll, GlobalISel/fma.ll v_fma_f16):

v_fma_f16 v0.l, v0.l, v1.l, v2.l becomes

	v_fmac_f16_e32 v2.l, v0.l, v1.l
	v_mov_b32_e32 v0, v2

So the fix needs to be more selective, or later passes need to handle the REG_SEQUENCE form as well as they handle the COPY.

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)

With real true16, a vgpr G_ANYEXT from 16 to 32 bits is selected as a plain COPY from a vgpr_16 to a vgpr_32. Later passes then have to deal with a copy whose sizes do not match. Selecting it as
  %undef:vgpr_16 = IMPLICIT_DEF
  %dst:vgpr_32   = REG_SEQUENCE %src, %subreg.lo16, %undef, %subreg.hi16

says the value is the low half and gives better code in some cases. The returned f16 value below is an example.

Reproducer (from llvm/test/CodeGen/AMDGPU/llvm.frexp.ll, test_frexp_f16_i32):

define { half, i32 } @<!-- -->test_frexp_f16_i32(half %a) {
  %result = call { half, i32 } @<!-- -->llvm.frexp.f16.i32(half %a)
  ret { half, i32 } %result
}

llc -global-isel -mtriple=amdgpu11.00 -mattr=+real-true16 test.ll -o -

MIR after instruction-select:

    %14:vgpr_16 = nofpexcept V_FREXP_MANT_F16_t16_e64 0, %10, 0, 0, 0, implicit $mode, implicit $exec
    ...
    %13:vgpr_32 = COPY %14
    $vgpr0 = COPY %13

Current GlobalISel output:

	s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
	v_frexp_mant_f16_e32 v1.l, v0.l
	v_frexp_exp_i16_f16_e32 v0.l, v0.l
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_mov_b16_e32 v2.l, v1.l
	v_bfe_i32 v1, v0, 0, 16
	s_delay_alu instid0(VALU_DEP_2)
	v_mov_b32_e32 v0, v2
	s_setpc_b64 s[30:31]

Desired output (same as SelectionDAG):

	s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
	v_frexp_exp_i16_f16_e32 v1.l, v0.l
	v_frexp_mant_f16_e32 v0.l, v0.l
	s_delay_alu instid0(VALU_DEP_2)
	v_bfe_i32 v1, v1, 0, 16
	s_setpc_b64 s[30:31]

Note: switching all such G_ANYEXTs to REG_SEQUENCE is not a pure win. It fixes this case but makes others worse, e.g. llvm.fma.f16 (fma.f16.ll, GlobalISel/fma.ll v_fma_f16):

v_fma_f16 v0.l, v0.l, v1.l, v2.l becomes

	v_fmac_f16_e32 v2.l, v0.l, v1.l
	v_mov_b32_e32 v0, v2

So the fix needs to be more selective, or later passes need to handle the REG_SEQUENCE form as well as they handle the COPY.

@tgymnich for awareness

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

I like to work on this issue

kulkarni-aditya-01 avatar Oct 29 '26 09:10 kulkarni-aditya-01