[AMDGPU][GlobalISel] Unmerge of an sgpr->vgpr copy of a 64-bit constant is not split into 32-bit constants
After regbank selection a 64-bit VALU operation is split into 32-bit halves through a G_UNMERGE_VALUES of a vgpr COPY of an sgpr 64-bit G_CONSTANT. The generic unmerge_cst combine in the regbank combiner does not look through the COPY, so the halves are never seen as constants. In the example below the xor with -1 is not recognized as a not, and v_bfi_b32 (and-not) is not formed.
Reproducer (from llvm/test/CodeGen/AMDGPU/GlobalISel/andn2.ll, v_andn2_i64):
define i64 @v_andn2_i64(i64 %src0, i64 %src1) {
%not.src1 = xor i64 %src1, -1
%and = and i64 %src0, %not.src1
ret i64 %and
}
llc -global-isel -mtriple=amdgpu9.00-mesa-mesa3d test.ll -o -
MIR after amdgpu-regbank-combiner:
%14:sgpr(i64) = G_CONSTANT i64 -1
%19:vgpr(i64) = COPY %14(i64)
%22:vgpr(i32), %23:vgpr(i32) = G_UNMERGE_VALUES %19(i64)
%24:vgpr(i32) = G_XOR %12, %22
%25:vgpr(i32) = G_XOR %13, %23
Current GlobalISel output:
s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
v_xor_b32_e32 v2, -1, v2
v_xor_b32_e32 v3, -1, v3
v_and_b32_e32 v0, v0, v2
v_and_b32_e32 v1, v1, v3
s_setpc_b64 s[30:31]
Desired output (same as SelectionDAG):
s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
v_bfi_b32 v0, v2, 0, v0
v_bfi_b32 v1, v3, 0, v1
s_setpc_b64 s[30:31]
Many more cases: GlobalISel/{and,orn2,xnor,udiv.i64,urem.i64}.ll, bfi_int.ll, div_i128.ll, fptoi.i128.ll, ...
Possible fix: a regbank combine that folds G_UNMERGE_VALUES (COPY (G_CONSTANT C)) into per-piece G_CONSTANTs in the reg bank of the unmerge defs, when the unmerge source has a single use.
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-backend-amdgpu
Author: Petar Avramovic (petar-avramovic)
Reproducer (from llvm/test/CodeGen/AMDGPU/GlobalISel/andn2.ll, v_andn2_i64):
define i64 @<!-- -->v_andn2_i64(i64 %src0, i64 %src1) {
%not.src1 = xor i64 %src1, -1
%and = and i64 %src0, %not.src1
ret i64 %and
}
llc -global-isel -mtriple=amdgpu9.00-mesa-mesa3d test.ll -o -
MIR after amdgpu-regbank-combiner:
%14:sgpr(i64) = G_CONSTANT i64 -1
%19:vgpr(i64) = COPY %14(i64)
%22:vgpr(i32), %23:vgpr(i32) = G_UNMERGE_VALUES %19(i64)
%24:vgpr(i32) = G_XOR %12, %22
%25:vgpr(i32) = G_XOR %13, %23
Current GlobalISel output:
s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
v_xor_b32_e32 v2, -1, v2
v_xor_b32_e32 v3, -1, v3
v_and_b32_e32 v0, v0, v2
v_and_b32_e32 v1, v1, v3
s_setpc_b64 s[30:31]
Desired output (same as SelectionDAG):
s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
v_bfi_b32 v0, v2, 0, v0
v_bfi_b32 v1, v3, 0, v1
s_setpc_b64 s[30:31]
Many more cases: GlobalISel/{and,orn2,xnor,udiv.i64,urem.i64}.ll, bfi_int.ll, div_i128.ll, fptoi.i128.ll, ...
Possible fix: a regbank combine that folds G_UNMERGE_VALUES (COPY (G_CONSTANT C)) into per-piece G_CONSTANTs in the reg bank of the unmerge defs, when the unmerge source has a single use.
@llvm/issue-subscribers-good-first-issue
Author: Petar Avramovic (petar-avramovic)
Reproducer (from llvm/test/CodeGen/AMDGPU/GlobalISel/andn2.ll, v_andn2_i64):
define i64 @<!-- -->v_andn2_i64(i64 %src0, i64 %src1) {
%not.src1 = xor i64 %src1, -1
%and = and i64 %src0, %not.src1
ret i64 %and
}
llc -global-isel -mtriple=amdgpu9.00-mesa-mesa3d test.ll -o -
MIR after amdgpu-regbank-combiner:
%14:sgpr(i64) = G_CONSTANT i64 -1
%19:vgpr(i64) = COPY %14(i64)
%22:vgpr(i32), %23:vgpr(i32) = G_UNMERGE_VALUES %19(i64)
%24:vgpr(i32) = G_XOR %12, %22
%25:vgpr(i32) = G_XOR %13, %23
Current GlobalISel output:
s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
v_xor_b32_e32 v2, -1, v2
v_xor_b32_e32 v3, -1, v3
v_and_b32_e32 v0, v0, v2
v_and_b32_e32 v1, v1, v3
s_setpc_b64 s[30:31]
Desired output (same as SelectionDAG):
s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
v_bfi_b32 v0, v2, 0, v0
v_bfi_b32 v1, v3, 0, v1
s_setpc_b64 s[30:31]
Many more cases: GlobalISel/{and,orn2,xnor,udiv.i64,urem.i64}.ll, bfi_int.ll, div_i128.ll, fptoi.i128.ll, ...
Possible fix: a regbank combine that folds G_UNMERGE_VALUES (COPY (G_CONSTANT C)) into per-piece G_CONSTANTs in the reg bank of the unmerge defs, when the unmerge source has a single use.
Hi, I’m working on this issue. I’ll investigate the root cause, implement a fix, and submit a pull request following the LLVM contribution guidelines.
Hi Peter, Just wanted to share a quick update on the issue. I have been going through AMDGPU implementation to understand how the constant is copied from an SGPR to a VGPR and why the optimization is failing. I am currently examining the code and trying to understand the existing handling and possible edge cases before making the changes. I will keep you updated on my progress. Thanks!
@ankush-sharma0910 are you still working on it
@competitive-programmer04 No I mean I solved the issue and open the pull request it need's to be merged
Hi! I'm learning LLVM internals and would love to explore this issue as a learning exercise. Is it okay if I follow along with the discussion and ask questions?
Hi @competitive-programmer04 , I worked on this issue to understand LLVM internals, using AI to help me learn the codebase. I implemented and tested my own approach: https://github.com/Neeraj3008/llvm-project/tree/unmerge-constant-copy-fix Would appreciate your feedback!
@Neeraj3008 I not worked on this issue since @ankush-sharma0910 was about to raise the or but still I can see your implementation
@competitive-programmer04 Thanks for taking a look I'm just getting started with open source and learning to navigate LLVM's codebase. AI helped me understand parts of the implementation, and I'm learning along the way. Would it be okay if I opened a PR for this, or would you recommend I explore other areas of LLVM first?
@Neeraj3008 please contact with @ankush-sharma0910 as he was going to open the pr for this issue
how can i contact @ankush-sharma0910 ?
Hi @Neeraj3008 I've already opened a PR for this issue, and it's currently awaiting review. I'd be happy to discuss the implementation and share what I've learned while working on it. Feel free to ask me anything about the approach or LLVM internals. We can connect here in the issue thread!
Sure!