[GlobalISel] Narrowed multiply materializes every carry with v_cndmask instead of chaining them through add-with-carry
LegalizerHelper::multiplyRegisters (used when narrowing G_MUL / G_UMULH / G_UMULO) builds each destination digit by adding its partial products with plain adds and collecting the carries into a running sum that is folded into the next digit. Each carry is turned into a full 32-bit value (v_cndmask_b32 0, 1) and costs an extra add. When G_UADDE is legal for the narrow type, each carry can instead be passed straight into an addition the next digit has to do anyway.
Reproducer (from llvm/test/CodeGen/AMDGPU/GlobalISel/udiv.i64.ll, v_udiv_i64_oddk_denom; the udiv by constant becomes a 64-bit umulh):
define i64 @v_udiv_i64_oddk_denom(i64 %num) {
%result = udiv i64 %num, 1235195
ret i64 %result
}
llc -global-isel -amdgpu-codegenprepare-disable-idiv-expansion=1 -mtriple=amdgpu6.00-amd-amdpal -denormal-fp-math-f32=preserve-sign -mattr=+mad-mac-f32-insts test.ll -o -
Current GlobalISel output (umulh part):
v_mul_hi_u32 v3, v0, v2
v_mul_lo_u32 v5, v0, v4
v_mul_lo_u32 v6, v1, v2
v_mul_hi_u32 v2, v1, v2
v_mul_hi_u32 v0, v0, v4
v_add_i32_e32 v3, vcc, v3, v5
v_cndmask_b32_e64 v5, 0, 1, vcc
v_add_i32_e32 v3, vcc, v3, v6
v_addc_u32_e32 v3, vcc, 0, v5, vcc
v_mul_lo_u32 v5, v1, v4
v_add_i32_e32 v0, vcc, v2, v0
v_cndmask_b32_e64 v2, 0, 1, vcc
v_add_i32_e32 v0, vcc, v0, v5
v_mul_hi_u32 v1, v1, v4
v_addc_u32_e32 v2, vcc, 0, v2, vcc
v_add_i32_e32 v0, vcc, v0, v3
v_addc_u32_e32 v2, vcc, 0, v2, vcc
v_add_i32_e32 v1, vcc, v1, v2
Desired output (carries chained through v_addc):
v_mul_hi_u32 v3, v0, v2
v_mul_lo_u32 v5, v0, v4
v_mul_lo_u32 v6, v1, v2
v_mul_hi_u32 v2, v1, v2
v_mul_hi_u32 v0, v0, v4
v_mul_lo_u32 v7, v1, v4
v_mul_hi_u32 v1, v1, v4
v_add_i32_e32 v3, vcc, v3, v5
v_addc_u32_e32 v0, vcc, v2, v0, vcc
v_addc_u32_e32 v1, vcc, 0, v1, vcc
v_add_i32_e32 v2, vcc, v3, v6
v_addc_u32_e32 v0, vcc, v0, v7, vcc
v_addc_u32_e32 v1, vcc, 0, v1, vcc
Whole function: 23 -> 18 instructions (SelectionDAG: 21). The 64-bit udiv/urem/sdiv/srem expansions shrink as well (udiv.i64.ll v_udiv_i64: 152 -> 137, SelectionDAG 140).
Possible fix: in multiplyRegisters, when G_UADDE is legal for the narrow type, feed each carry into the next digit's addition with G_UADDE, keep only one carry live at a time, and drop the carry out of the top digit. Keep the current expansion for targets without a legal G_UADDE.
@llvm/issue-subscribers-backend-amdgpu
Author: Petar Avramovic (petar-avramovic)
Reproducer (from llvm/test/CodeGen/AMDGPU/GlobalISel/udiv.i64.ll, v_udiv_i64_oddk_denom; the udiv by constant becomes a 64-bit umulh):
define i64 @<!-- -->v_udiv_i64_oddk_denom(i64 %num) {
%result = udiv i64 %num, 1235195
ret i64 %result
}
llc -global-isel -amdgpu-codegenprepare-disable-idiv-expansion=1 -mtriple=amdgpu6.00-amd-amdpal -denormal-fp-math-f32=preserve-sign -mattr=+mad-mac-f32-insts test.ll -o -
Current GlobalISel output (umulh part):
v_mul_hi_u32 v3, v0, v2
v_mul_lo_u32 v5, v0, v4
v_mul_lo_u32 v6, v1, v2
v_mul_hi_u32 v2, v1, v2
v_mul_hi_u32 v0, v0, v4
v_add_i32_e32 v3, vcc, v3, v5
v_cndmask_b32_e64 v5, 0, 1, vcc
v_add_i32_e32 v3, vcc, v3, v6
v_addc_u32_e32 v3, vcc, 0, v5, vcc
v_mul_lo_u32 v5, v1, v4
v_add_i32_e32 v0, vcc, v2, v0
v_cndmask_b32_e64 v2, 0, 1, vcc
v_add_i32_e32 v0, vcc, v0, v5
v_mul_hi_u32 v1, v1, v4
v_addc_u32_e32 v2, vcc, 0, v2, vcc
v_add_i32_e32 v0, vcc, v0, v3
v_addc_u32_e32 v2, vcc, 0, v2, vcc
v_add_i32_e32 v1, vcc, v1, v2
Desired output (carries chained through v_addc):
v_mul_hi_u32 v3, v0, v2
v_mul_lo_u32 v5, v0, v4
v_mul_lo_u32 v6, v1, v2
v_mul_hi_u32 v2, v1, v2
v_mul_hi_u32 v0, v0, v4
v_mul_lo_u32 v7, v1, v4
v_mul_hi_u32 v1, v1, v4
v_add_i32_e32 v3, vcc, v3, v5
v_addc_u32_e32 v0, vcc, v2, v0, vcc
v_addc_u32_e32 v1, vcc, 0, v1, vcc
v_add_i32_e32 v2, vcc, v3, v6
v_addc_u32_e32 v0, vcc, v0, v7, vcc
v_addc_u32_e32 v1, vcc, 0, v1, vcc
Whole function: 23 -> 18 instructions (SelectionDAG: 21). The 64-bit udiv/urem/sdiv/srem expansions shrink as well (udiv.i64.ll v_udiv_i64: 152 -> 137, SelectionDAG 140).
Possible fix: in multiplyRegisters, when G_UADDE is legal for the narrow type, feed each carry into the next digit's addition with G_UADDE, keep only one carry live at a time, and drop the carry out of the top digit. Keep the current expansion for targets without a legal G_UADDE.
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 (from llvm/test/CodeGen/AMDGPU/GlobalISel/udiv.i64.ll, v_udiv_i64_oddk_denom; the udiv by constant becomes a 64-bit umulh):
define i64 @<!-- -->v_udiv_i64_oddk_denom(i64 %num) {
%result = udiv i64 %num, 1235195
ret i64 %result
}
llc -global-isel -amdgpu-codegenprepare-disable-idiv-expansion=1 -mtriple=amdgpu6.00-amd-amdpal -denormal-fp-math-f32=preserve-sign -mattr=+mad-mac-f32-insts test.ll -o -
Current GlobalISel output (umulh part):
v_mul_hi_u32 v3, v0, v2
v_mul_lo_u32 v5, v0, v4
v_mul_lo_u32 v6, v1, v2
v_mul_hi_u32 v2, v1, v2
v_mul_hi_u32 v0, v0, v4
v_add_i32_e32 v3, vcc, v3, v5
v_cndmask_b32_e64 v5, 0, 1, vcc
v_add_i32_e32 v3, vcc, v3, v6
v_addc_u32_e32 v3, vcc, 0, v5, vcc
v_mul_lo_u32 v5, v1, v4
v_add_i32_e32 v0, vcc, v2, v0
v_cndmask_b32_e64 v2, 0, 1, vcc
v_add_i32_e32 v0, vcc, v0, v5
v_mul_hi_u32 v1, v1, v4
v_addc_u32_e32 v2, vcc, 0, v2, vcc
v_add_i32_e32 v0, vcc, v0, v3
v_addc_u32_e32 v2, vcc, 0, v2, vcc
v_add_i32_e32 v1, vcc, v1, v2
Desired output (carries chained through v_addc):
v_mul_hi_u32 v3, v0, v2
v_mul_lo_u32 v5, v0, v4
v_mul_lo_u32 v6, v1, v2
v_mul_hi_u32 v2, v1, v2
v_mul_hi_u32 v0, v0, v4
v_mul_lo_u32 v7, v1, v4
v_mul_hi_u32 v1, v1, v4
v_add_i32_e32 v3, vcc, v3, v5
v_addc_u32_e32 v0, vcc, v2, v0, vcc
v_addc_u32_e32 v1, vcc, 0, v1, vcc
v_add_i32_e32 v2, vcc, v3, v6
v_addc_u32_e32 v0, vcc, v0, v7, vcc
v_addc_u32_e32 v1, vcc, 0, v1, vcc
Whole function: 23 -> 18 instructions (SelectionDAG: 21). The 64-bit udiv/urem/sdiv/srem expansions shrink as well (udiv.i64.ll v_udiv_i64: 152 -> 137, SelectionDAG 140).
Possible fix: in multiplyRegisters, when G_UADDE is legal for the narrow type, feed each carry into the next digit's addition with G_UADDE, keep only one carry live at a time, and drop the carry out of the top digit. Keep the current expansion for targets without a legal G_UADDE.
Hi, I would like to work on this.
I have been working on understanding the internals(especially around generic MIR) and making relevant changes, I will be raising a pr soon, thanks!!