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

[AMDGPU][GlobalISel] uchar_to_float combine turns legal uitofp i16 -> f16 into cvt_f32_ubyte0 + fptrunc

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

The post-legalizer uchar_to_float combine also matches 16-bit sources. It rewrites a legal G_UITOFP i16 -> f16, whose source has only the low byte set, into G_AMDGPU_CVT_F32_UBYTE0 + G_FPTRUNC. That is one instruction more than the native v_cvt_f16_u16. SelectionDAG's performUCharToFloatCombine only fires for i32 sources.

Reproducer:

define half @v_uitofp_i16_mask255_to_f16(i16 %arg0) {
  %m = and i16 %arg0, 255
  %cvt = uitofp i16 %m to half
  ret half %cvt
}

llc -global-isel -mtriple=amdgpu8.02-- test.ll -o -

Current GlobalISel output:

	s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
	v_cvt_f32_ubyte0_sdwa v0, v0 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:BYTE_0
	v_cvt_f16_f32_e32 v0, v0
	s_setpc_b64 s[30:31]

Desired output (same as SelectionDAG):

	s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
	v_cvt_f16_u16_sdwa v0, v0 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:BYTE_0
	s_setpc_b64 s[30:31]

Possible fix: in AMDGPUPostLegalizerCombiner's uchar_to_float match, require a 32-bit source, as SelectionDAG does.

petar-avramovic avatar Oct 28 '26 15: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)

The post-legalizer uchar_to_float combine also matches 16-bit sources. It rewrites a legal G_UITOFP i16 -> f16, whose source has only the low byte set, into G_AMDGPU_CVT_F32_UBYTE0 + G_FPTRUNC. That is one instruction more than the native v_cvt_f16_u16. SelectionDAG's performUCharToFloatCombine only fires for i32 sources.

Reproducer:

define half @<!-- -->v_uitofp_i16_mask255_to_f16(i16 %arg0) {
  %m = and i16 %arg0, 255
  %cvt = uitofp i16 %m to half
  ret half %cvt
}

llc -global-isel -mtriple=amdgpu8.02-- test.ll -o -

Current GlobalISel output:

	s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
	v_cvt_f32_ubyte0_sdwa v0, v0 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:BYTE_0
	v_cvt_f16_f32_e32 v0, v0
	s_setpc_b64 s[30:31]

Desired output (same as SelectionDAG):

	s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
	v_cvt_f16_u16_sdwa v0, v0 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:BYTE_0
	s_setpc_b64 s[30:31]

Possible fix: in AMDGPUPostLegalizerCombiner's uchar_to_float match, require a 32-bit source, as SelectionDAG does.

@llvm/issue-subscribers-backend-amdgpu

Author: Petar Avramovic (petar-avramovic)

The post-legalizer uchar_to_float combine also matches 16-bit sources. It rewrites a legal G_UITOFP i16 -> f16, whose source has only the low byte set, into G_AMDGPU_CVT_F32_UBYTE0 + G_FPTRUNC. That is one instruction more than the native v_cvt_f16_u16. SelectionDAG's performUCharToFloatCombine only fires for i32 sources.

Reproducer:

define half @<!-- -->v_uitofp_i16_mask255_to_f16(i16 %arg0) {
  %m = and i16 %arg0, 255
  %cvt = uitofp i16 %m to half
  ret half %cvt
}

llc -global-isel -mtriple=amdgpu8.02-- test.ll -o -

Current GlobalISel output:

	s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
	v_cvt_f32_ubyte0_sdwa v0, v0 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:BYTE_0
	v_cvt_f16_f32_e32 v0, v0
	s_setpc_b64 s[30:31]

Desired output (same as SelectionDAG):

	s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
	v_cvt_f16_u16_sdwa v0, v0 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:BYTE_0
	s_setpc_b64 s[30:31]

Possible fix: in AMDGPUPostLegalizerCombiner's uchar_to_float match, require a 32-bit source, as SelectionDAG does.

can i take it

Popie52 avatar Oct 31 '26 17:10 Popie52

Hello, @petar-avramovic I would love to work on it can you assign it to me

6. Hello, @petar-avramovic I would love to work on it can you assign it to me

please check other comments first, it goes to the one that comments first, or check with them in case they are no longer working on it

petar-avramovic avatar Nov 02 '26 10:11 petar-avramovic

ok sir

What is current status like is this done by him and how much amount of time is given to solve a issue to a person

AshikAHegde avatar Nov 14 '26 15:11 AshikAHegde

@Popie52 if you are not working on it then I would love to work on it

@AshikAHegde @competitive-programmer04 little more time

Popie52 avatar Nov 14 '26 15:11 Popie52

@Popie52 that's totally fine take your complete time. I am just asking if you are not working on it but if you are working that's totally fine