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

[RISCV][CodeGen] Vector `Not(Sne)` sequences produced that can be simplified to `Seq`

Open bababuck opened this issue 2 months ago • 10 comments

Found when reviewing #221321.

llc -mtriple=riscv64 -mcpu=sifive-p670 -verify-machineinstrs

define i1 @all_8(ptr %input) {
entry:
 %tmp0 = load <8 x i8>, ptr %input, align 1
 %tmp1 = trunc <8 x i8> %tmp0 to <8 x i1>
 %tmp2 = call i1 @llvm.vector.reduce.and.v32i1(<8 x i1> %tmp1)
 ret i1 %tmp2
}

results in

        vsetivli        zero, 8, e8, mf2, ta, ma
        vle8.v  v8, (a0)
        vand.vi v8, v8, 1
        vmsne.vi        v8, v8, 0
        vmnot.m v8, v8
        vcpop.m a0, v8
        seqz    a0, a0
        ret

Two separate issues here:

  1. The vmsne.vi - vmnot sequence can be simplified to vmseq
        vsetivli        zero, 8, e8, mf2, ta, ma
        vle8.v  v8, (a0)
        vand.vi v8, v8, 1
        vmseq.vi        v8, v8, 0
        vcpop.m a0, v8
        seqz    a0, a0
        ret
  1. Could be instead lower to. Might be better handled in vector-combine (i.e. reverse the ordering of the trunc and reduce.and)
        vsetivli        zero, 8, e8, mf2, ta, ma
        vle8.v  v8, (a0)
        vredand.vs      v8, v8, v8
        vmv.x.s a0, v8
        ret

bababuck avatar Sep 08 '26 21:09 bababuck

@llvm/issue-subscribers-backend-risc-v

Author: Ryan Buchner (bababuck)

Found when reviewing #221321.

llc -mtriple=riscv64 -mcpu=sifive-p670 -verify-machineinstrs

define i1 @<!-- -->all_8(ptr %input) {
entry:
 %tmp0 = load &lt;8 x i8&gt;, ptr %input, align 1
 %tmp1 = trunc &lt;8 x i8&gt; %tmp0 to &lt;8 x i1&gt;
 %tmp2 = call i1 @<!-- -->llvm.vector.reduce.and.v32i1(&lt;8 x i1&gt; %tmp1)
 ret i1 %tmp2
}

results in

        vsetivli        zero, 8, e8, mf2, ta, ma
        vle8.v  v8, (a0)
        vand.vi v8, v8, 1
        vmsne.vi        v8, v8, 0
        vmnot.m v8, v8
        vcpop.m a0, v8
        seqz    a0, a0
        ret

Two separate issues here:

  1. The vmsne.vi - vmnot sequence can be simplified to vmseq
        vsetivli        zero, 8, e8, mf2, ta, ma
        vle8.v  v8, (a0)
        vand.vi v8, v8, 1
        vmseq.vi        v8, v8, 0
        vcpop.m a0, v8
        seqz    a0, a0
        ret
  1. Could be instead lower to. Might be better handled in vector-combine (i.e. reverse the ordering of the trunc and reduce.and)
        vsetivli        zero, 8, e8, mf2, ta, ma
        vle8.v  v8, (a0)
        vredand.vs      v8, v8, v8
        vmv.x.s a0, v8
        ret

Hi! I'd like to work on this issue. If no one else is already working on it, please assign it to me. Thanks!

Adarsh-Me avatar Sep 09 '26 00:09 Adarsh-Me

Hi! I'd like to work on this issue. If no one else is already working on it, please assign it to me. Thanks!

Assigned. I'm not quite sure of the direction to take this. My understanding is that the trunc and the reduce.and get lowered separately, creating the vmseq and the vmnot independently. I'm not 100% of the best path to fixing this, might be worth getting input from some of the other RISCV contributors if you're not sure either.

Edit: Forgot to say thanks for picking this up :)

bababuck avatar Sep 09 '26 01:09 bababuck

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: Ryan Buchner (bababuck)

Found when reviewing #221321.

llc -mtriple=riscv64 -mcpu=sifive-p670 -verify-machineinstrs

define i1 @<!-- -->all_8(ptr %input) {
entry:
 %tmp0 = load &lt;8 x i8&gt;, ptr %input, align 1
 %tmp1 = trunc &lt;8 x i8&gt; %tmp0 to &lt;8 x i1&gt;
 %tmp2 = call i1 @<!-- -->llvm.vector.reduce.and.v32i1(&lt;8 x i1&gt; %tmp1)
 ret i1 %tmp2
}

results in

        vsetivli        zero, 8, e8, mf2, ta, ma
        vle8.v  v8, (a0)
        vand.vi v8, v8, 1
        vmsne.vi        v8, v8, 0
        vmnot.m v8, v8
        vcpop.m a0, v8
        seqz    a0, a0
        ret

Two separate issues here:

  1. The vmsne.vi - vmnot sequence can be simplified to vmseq
        vsetivli        zero, 8, e8, mf2, ta, ma
        vle8.v  v8, (a0)
        vand.vi v8, v8, 1
        vmseq.vi        v8, v8, 0
        vcpop.m a0, v8
        seqz    a0, a0
        ret
  1. Could be instead lower to. Might be better handled in vector-combine (i.e. reverse the ordering of the trunc and reduce.and)
        vsetivli        zero, 8, e8, mf2, ta, ma
        vle8.v  v8, (a0)
        vredand.vs      v8, v8, v8
        vmv.x.s a0, v8
        ret

@Adarsh-Me This looks like a good first issue for contributors to learn, typically these don't need to be assigned to anyone but feel free to open up a PR for it regardless. As a heads up, LLMs aren't allowed to fix good first issues as per the AI policy: https://llvm.org/docs/AIToolPolicy.html#details

lukel97 avatar Sep 09 '26 02:09 lukel97

Hi im new to this and trying to learn LLVM. Can I try to do this?

anandkvinu avatar Sep 09 '26 03:09 anandkvinu

Hi, Please can you assigned this to me if Your not working. Please Assign it to me. I am new here

bilal-512 avatar Sep 10 '26 06:09 bilal-512

Hey, If no one is assigned, i'd like to try working on it. Thanks,

vovosta avatar Sep 10 '26 07:09 vovosta

Hi @bababuck @lukel97 I opened a PR :)

martonmoro avatar Sep 10 '26 08:09 martonmoro