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

AMDGPU: can optimize away v_readfirstlane_b32 on implicit_def input

Open rampitec opened this issue 1 year ago • 10 comments

For the small testcase:

; RUN: llc -march=amdgcn -mcpu=gfx900 < %s

; Only one readfistlane is really needed

define amdgpu_ps void @test(i32 %in, ptr %out) {
  %v1 = insertelement <2 x i32> <i32 poison, i32 poison>, i32 %in, i32 0
  %v2 = bitcast <2 x i32> %v1 to i64
  %v3 = call i64 @llvm.amdgcn.s.quadmask.i64(i64 %v2)
  %p = inttoptr i64 %v2 to ptr addrspace(4)
  store i64 %v3, ptr %out
  ret void
}

We are producing code which has 2 v_readfirstlane_b32 instructions. Only one is needed as second one reads undef:

        v_readfirstlane_b32 s0, v0
        v_readfirstlane_b32 s1, v1
        s_quadmask_b64 s[0:1], s[0:1]
        v_mov_b32_e32 v4, s1
        v_mov_b32_e32 v3, s0
        flat_store_dwordx2 v[1:2], v[3:4]
        s_endpgm

Although backend does not see it is undef behind the REG_SEQUENCE.

rampitec avatar Feb 05 '25 22:02 rampitec

@llvm/issue-subscribers-backend-amdgpu

Author: Stanislav Mekhanoshin (rampitec)

For the small testcase: ``` ; RUN: llc -march=amdgcn -mcpu=gfx900 < %s

; Only one readfistlane is really needed

define amdgpu_ps void @test(i32 %in, ptr %out) { %v1 = insertelement <2 x i32> <i32 poison, i32 poison>, i32 %in, i32 0 %v2 = bitcast <2 x i32> %v1 to i64 %v3 = call i64 @llvm.amdgcn.s.quadmask.i64(i64 %v2) %p = inttoptr i64 %v2 to ptr addrspace(4) store i64 %v3, ptr %out ret void }


We are producing code which has 2 v_readfirstlane_b32 instructions. Only one is needed as second one reads undef:

    v_readfirstlane_b32 s0, v0
    v_readfirstlane_b32 s1, v1
    s_quadmask_b64 s[0:1], s[0:1]
    v_mov_b32_e32 v4, s1
    v_mov_b32_e32 v3, s0
    flat_store_dwordx2 v[1:2], v[3:4]
    s_endpgm
Although backend does not see it is undef behind the REG_SEQUENCE.
</details>

llvmbot avatar Feb 05 '25 22:02 llvmbot

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 has already been assigned to this issue. If you believe that no one is actually working on it despite an assignment, ping the person. After one week without a response, the assignee may be changed.
  2. In the comments of this issue, request for it to be assigned to you, 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.
  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.

llvmbot avatar Feb 06 '25 03:02 llvmbot

@llvm/issue-subscribers-good-first-issue

Author: Stanislav Mekhanoshin (rampitec)

For the small testcase: ``` ; RUN: llc -march=amdgcn -mcpu=gfx900 < %s

; Only one readfistlane is really needed

define amdgpu_ps void @test(i32 %in, ptr %out) { %v1 = insertelement <2 x i32> <i32 poison, i32 poison>, i32 %in, i32 0 %v2 = bitcast <2 x i32> %v1 to i64 %v3 = call i64 @llvm.amdgcn.s.quadmask.i64(i64 %v2) %p = inttoptr i64 %v2 to ptr addrspace(4) store i64 %v3, ptr %out ret void }


We are producing code which has 2 v_readfirstlane_b32 instructions. Only one is needed as second one reads undef:

    v_readfirstlane_b32 s0, v0
    v_readfirstlane_b32 s1, v1
    s_quadmask_b64 s[0:1], s[0:1]
    v_mov_b32_e32 v4, s1
    v_mov_b32_e32 v3, s0
    flat_store_dwordx2 v[1:2], v[3:4]
    s_endpgm
Although backend does not see it is undef behind the REG_SEQUENCE.
</details>

llvmbot avatar Feb 06 '25 03:02 llvmbot

@kerbowa are you working on this, if not can I give it a go?

bababuck avatar Feb 21 '25 03:02 bababuck

Most recently @JoshHuttonCode was looking at this issue.

kerbowa avatar Feb 21 '25 17:02 kerbowa

Yes, I am taking a look at this issue.

JoshHuttonCode avatar Feb 21 '25 18:02 JoshHuttonCode

@JoshHuttonCode are you still working on it?

divyanshsinghvi avatar Jun 28 '25 20:06 divyanshsinghvi

Yes, I am. Now that my school term is done, I've got more time to work on it. Since this is my first issue, it's been taking me some time.

I have located the source of the issue, and I believe I have mostly implemented the fix. I am in the process of ensuring my fix does not break other use cases or have other side effects.

JoshHuttonCode avatar Jun 29 '25 15:06 JoshHuttonCode

@JoshHuttonCode ping?

johnnyb2543 avatar Jul 14 '26 00:07 johnnyb2543

There is a PR open for this issue which is waiting for reviewer feedback.

JoshHuttonCode avatar Jul 17 '26 18:07 JoshHuttonCode