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

[AMDGPU][GlobalISel] Constants used only by divergent phis are materialized in SGPRs

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

A uniform G_CONSTANT whose only uses are divergent (VGPR) phis gets the sgpr bank. Phi elimination then emits an sgpr->vgpr copy in the incoming block, and the immediate is never folded into it (si-fold-operands runs before phi-node-elimination). The result is s_mov_b32 + v_mov_b32 where a single v_mov_b32 with the immediate would do. SelectionDAG emits the single v_mov.

Reproducer (from llvm/test/CodeGen/AMDGPU/GlobalISel/divergent-control-flow.ll, constrained_if_register_class):

@external_constant = external addrspace(4) constant i32, align 4
@const.ptr = external addrspace(4) constant ptr, align 4

define void @constrained_if_register_class() {
bb:
  %tmp = load i32, ptr addrspace(4) @external_constant
  %tmp1 = icmp ne i32 %tmp, 0
  br i1 %tmp1, label %bb12, label %bb2

bb2:
  %ptr = load ptr, ptr addrspace(4) @const.ptr
  %tmp4 = load float, ptr %ptr, align 4
  %tmp5 = fcmp olt float %tmp4, 1.0
  %tmp6 = or i1 %tmp5, false
  br i1 %tmp6, label %bb8, label %bb7

bb7:
  br label %bb8

bb8:
  %tmp9 = phi i32 [ 0, %bb7 ], [ -1, %bb2 ]
  %tmp10 = icmp eq i32 %tmp9, 0
  br i1 %tmp10, label %bb11, label %bb12

bb11:
  store float 4.0, ptr addrspace(5) poison, align 4
  br label %bb12

bb12:
  ret void
}

llc -global-isel -mtriple=amdgpu9.00-amd-amdhsa test.ll -o -

Current GlobalISel output (phi part):

	flat_load_dword v0, v[0:1]
	s_mov_b32 s4, -1
	s_waitcnt vmcnt(0) lgkmcnt(0)
	v_cmp_ngt_f32_e32 vcc, 1.0, v0
	v_mov_b32_e32 v0, s4
	s_and_saveexec_b64 s[4:5], vcc
	s_mov_b32 s6, 0
	v_mov_b32_e32 v0, s6
	s_or_b64 exec, exec, s[4:5]

Desired output (same as SelectionDAG):

	flat_load_dword v0, v[0:1]
	s_waitcnt vmcnt(0) lgkmcnt(0)
	v_cmp_ngt_f32_e32 vcc, 1.0, v0
	v_mov_b32_e32 v0, -1
	s_and_saveexec_b64 s[4:5], vcc
	v_mov_b32_e32 v0, 0
	s_or_b64 exec, exec, s[4:5]

Possible fix: in the AMDGPU regbank combiner (after RegBankLegalize, so the uniformity-keyed rules are not violated), rewrite the bank of a G_CONSTANT / G_FCONSTANT to vgpr when all its uses are vgpr phis.

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

A uniform G_CONSTANT whose only uses are divergent (VGPR) phis gets the sgpr bank. Phi elimination then emits an sgpr->vgpr copy in the incoming block, and the immediate is never folded into it (si-fold-operands runs before phi-node-elimination). The result is s_mov_b32 + v_mov_b32 where a single v_mov_b32 with the immediate would do. SelectionDAG emits the single v_mov.

Reproducer (from llvm/test/CodeGen/AMDGPU/GlobalISel/divergent-control-flow.ll, constrained_if_register_class):

@external_constant = external addrspace(4) constant i32, align 4 @const.ptr = external addrspace(4) constant ptr, align 4

define void @constrained_if_register_class() { bb: %tmp = load i32, ptr addrspace(4) @external_constant %tmp1 = icmp ne i32 %tmp, 0 br i1 %tmp1, label %bb12, label %bb2

bb2: %ptr = load ptr, ptr addrspace(4) @const.ptr %tmp4 = load float, ptr %ptr, align 4 %tmp5 = fcmp olt float %tmp4, 1.0 %tmp6 = or i1 %tmp5, false br i1 %tmp6, label %bb8, label %bb7

bb7: br label %bb8

bb8: %tmp9 = phi i32 [ 0, %bb7 ], [ -1, %bb2 ] %tmp10 = icmp eq i32 %tmp9, 0 br i1 %tmp10, label %bb11, label %bb12

bb11: store float 4.0, ptr addrspace(5) poison, align 4 br label %bb12

bb12: ret void }

llc -global-isel -mtriple=amdgpu9.00-amd-amdhsa test.ll -o -

Current GlobalISel output (phi part):

flat_load_dword v0, v[0:1]
s_mov_b32 s4, -1
s_waitcnt vmcnt(0) lgkmcnt(0)
v_cmp_ngt_f32_e32 vcc, 1.0, v0
v_mov_b32_e32 v0, s4
s_and_saveexec_b64 s[4:5], vcc
s_mov_b32 s6, 0
v_mov_b32_e32 v0, s6
s_or_b64 exec, exec, s[4:5]

Desired output (same as SelectionDAG):

flat_load_dword v0, v[0:1]
s_waitcnt vmcnt(0) lgkmcnt(0)
v_cmp_ngt_f32_e32 vcc, 1.0, v0
v_mov_b32_e32 v0, -1
s_and_saveexec_b64 s[4:5], vcc
v_mov_b32_e32 v0, 0
s_or_b64 exec, exec, s[4:5]

Possible fix: in the AMDGPU regbank combiner (after RegBankLegalize, so the uniformity-keyed rules are not violated), rewrite the bank of a G_CONSTANT / G_FCONSTANT to vgpr when all its uses are vgpr phis.

@llvm/issue-subscribers-backend-amdgpu

Author: Petar Avramovic (petar-avramovic)

A uniform G_CONSTANT whose only uses are divergent (VGPR) phis gets the sgpr bank. Phi elimination then emits an sgpr->vgpr copy in the incoming block, and the immediate is never folded into it (si-fold-operands runs before phi-node-elimination). The result is s_mov_b32 + v_mov_b32 where a single v_mov_b32 with the immediate would do. SelectionDAG emits the single v_mov.

Reproducer (from llvm/test/CodeGen/AMDGPU/GlobalISel/divergent-control-flow.ll, constrained_if_register_class):

@external_constant = external addrspace(4) constant i32, align 4 @const.ptr = external addrspace(4) constant ptr, align 4

define void @constrained_if_register_class() { bb: %tmp = load i32, ptr addrspace(4) @external_constant %tmp1 = icmp ne i32 %tmp, 0 br i1 %tmp1, label %bb12, label %bb2

bb2: %ptr = load ptr, ptr addrspace(4) @const.ptr %tmp4 = load float, ptr %ptr, align 4 %tmp5 = fcmp olt float %tmp4, 1.0 %tmp6 = or i1 %tmp5, false br i1 %tmp6, label %bb8, label %bb7

bb7: br label %bb8

bb8: %tmp9 = phi i32 [ 0, %bb7 ], [ -1, %bb2 ] %tmp10 = icmp eq i32 %tmp9, 0 br i1 %tmp10, label %bb11, label %bb12

bb11: store float 4.0, ptr addrspace(5) poison, align 4 br label %bb12

bb12: ret void }

llc -global-isel -mtriple=amdgpu9.00-amd-amdhsa test.ll -o -

Current GlobalISel output (phi part):

flat_load_dword v0, v[0:1]
s_mov_b32 s4, -1
s_waitcnt vmcnt(0) lgkmcnt(0)
v_cmp_ngt_f32_e32 vcc, 1.0, v0
v_mov_b32_e32 v0, s4
s_and_saveexec_b64 s[4:5], vcc
s_mov_b32 s6, 0
v_mov_b32_e32 v0, s6
s_or_b64 exec, exec, s[4:5]

Desired output (same as SelectionDAG):

flat_load_dword v0, v[0:1]
s_waitcnt vmcnt(0) lgkmcnt(0)
v_cmp_ngt_f32_e32 vcc, 1.0, v0
v_mov_b32_e32 v0, -1
s_and_saveexec_b64 s[4:5], vcc
v_mov_b32_e32 v0, 0
s_or_b64 exec, exec, s[4:5]

Possible fix: in the AMDGPU regbank combiner (after RegBankLegalize, so the uniformity-keyed rules are not violated), rewrite the bank of a G_CONSTANT / G_FCONSTANT to vgpr when all its uses are vgpr phis.

hi, I'd like to work on this one.

i'm getting started with GPU backends and want to learn the AMDGPU side :)

also, thanks for all the good first issues :D, it really is a kickstart for beginners and it takes a willing heart.

StarOne01 avatar Oct 28 '26 16:10 StarOne01

can i take this issue if no one is working on it ?

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

hi @kulkarni-aditya-01, i have already started working on it.

there are plenty of other good first issues from the same author if you want one.

StarOne01 avatar Oct 29 '26 09:10 StarOne01