[AMDGPU][GlobalISel] f16 -> i8 fptosi/fptoui goes through f32 on targets with 16-bit insts
On targets with 16-bit instructions, G_FPTOSI/G_FPTOUI from f16 to i8 widen the i8 side to 32 bits, so the conversion goes f16 -> f32 -> i32. SelectionDAG uses the native v_cvt_i16_f16 / v_cvt_u16_f16.
Reproducer:
define i8 @v_fptosi_f16_to_i8(half %arg0) {
%cvt = fptosi half %arg0 to i8
ret i8 %cvt
}
llc -global-isel -mtriple=amdgpu8.02-- test.ll -o -
Current GlobalISel output:
s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
v_cvt_f32_f16_e32 v0, v0
v_cvt_i32_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_i16_f16_e32 v0, v0
s_setpc_b64 s[30:31]
fptoui is the same: v_cvt_f32_f16 + v_cvt_u32_f32 instead of v_cvt_u16_f16.
The legalizer has the same behavior for G_UITOFP/G_SITOFP from i8 to f16 (see test_uitofp_s8_to_s16 in llvm/test/CodeGen/AMDGPU/GlobalISel/legalize-uitofp.mir: it converts through f32 + G_FPTRUNC). For uitofp a later combine already selects v_cvt_f32_ubyte0 + v_cvt_f16_f32, but SelectionDAG uses a single v_cvt_f16_u16_sdwa.
Possible fix: in AMDGPULegalizerInfo, when 16-bit instructions are available, widen the narrow integer side of these conversions to i16 instead of i32.
@llvm/issue-subscribers-backend-amdgpu
Author: Petar Avramovic (petar-avramovic)
Reproducer:
define i8 @v_fptosi_f16_to_i8(half %arg0) { %cvt = fptosi half %arg0 to i8 ret i8 %cvt }
llc -global-isel -mtriple=amdgpu8.02-- test.ll -o -
Current GlobalISel output:
s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
v_cvt_f32_f16_e32 v0, v0
v_cvt_i32_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_i16_f16_e32 v0, v0
s_setpc_b64 s[30:31]
fptoui is the same: v_cvt_f32_f16 + v_cvt_u32_f32 instead of v_cvt_u16_f16.
The legalizer has the same behavior for G_UITOFP/G_SITOFP from i8 to f16 (see test_uitofp_s8_to_s16 in llvm/test/CodeGen/AMDGPU/GlobalISel/legalize-uitofp.mir: it converts through f32 + G_FPTRUNC). For uitofp a later combine already selects v_cvt_f32_ubyte0 + v_cvt_f16_f32, but SelectionDAG uses a single v_cvt_f16_u16_sdwa.
Possible fix: in AMDGPULegalizerInfo, when 16-bit instructions are available, widen the narrow integer side of these conversions to i16 instead of i32.
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:
- 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.
- 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.
- 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.
- Run the test suite locally. Remember that the subdirectories under
test/create fine-grained testing targets, so you can e.g. usemake check-clang-astto only run Clang's AST tests. - Create a Git commit.
- Run
git clang-format HEAD~1to format your changes. - 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)
Reproducer:
define i8 @v_fptosi_f16_to_i8(half %arg0) { %cvt = fptosi half %arg0 to i8 ret i8 %cvt }
llc -global-isel -mtriple=amdgpu8.02-- test.ll -o -
Current GlobalISel output:
s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
v_cvt_f32_f16_e32 v0, v0
v_cvt_i32_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_i16_f16_e32 v0, v0
s_setpc_b64 s[30:31]
fptoui is the same: v_cvt_f32_f16 + v_cvt_u32_f32 instead of v_cvt_u16_f16.
The legalizer has the same behavior for G_UITOFP/G_SITOFP from i8 to f16 (see test_uitofp_s8_to_s16 in llvm/test/CodeGen/AMDGPU/GlobalISel/legalize-uitofp.mir: it converts through f32 + G_FPTRUNC). For uitofp a later combine already selects v_cvt_f32_ubyte0 + v_cvt_f16_f32, but SelectionDAG uses a single v_cvt_f16_u16_sdwa.
Possible fix: in AMDGPULegalizerInfo, when 16-bit instructions are available, widen the narrow integer side of these conversions to i16 instead of i32.
Hi @petar-avramovic I have come across this issue in the good first issue. I am starting my open source contribution in LLVM with issue. Thanks.
hi @petar-avramovic Sorry it took a little more time than expected to fix this but I have come up with the solution. I will be soon raising the PR and taking your suggestions
@Piyush-Goenka are you still working on it
@Piyush-Goenka are you still working on it
Yes, I am on it.