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

Missed oppurtunity to fold vmerge into using vadd

Open preames opened this issue 4 years ago • 4 comments

This is a performance opportunity noted when analyzing sqlite3. Filing this with reproduction instructions for the original observation and some analysis below, plan to follow up with reduced test cases when I have some time.

$ clang -isystem $GNU_TOOLCHAIN_DIR/sysroot/usr/include/ --target=riscv64 -mllvm -riscv-v-vector-bits-min=256 -mllvm -scalable-vectorization=on -Xclang -target-feature -Xclang +v,+f,+m,+c,+d,+zba sqlite-autoconf-3380500/sqlite3.c -c -O2 -g

The amalgamation file is taken from the sqlite3 website.

Non optimal assembly observed:

    vmv.v.i    v16, 9
.LBB785_98: <-- this is a loop header, v16 does not change in loop
    vsetvli    zero, zero, e8, m1, ta, mu
    vluxei64.v    v17, (s4), v8
    vand.vx    v18, v17, a4
    vmseq.vi    v0, v18, 0
    vmerge.vim    v18, v16, 0, v0
    vadd.vv    v17, v18, v17

We're using the vmerge to materialize a vector of either 9 or zero depending on a mask. This can be folded into the consuming add by simply doing a masked add of 9.

This assembly should look something like:

    vmv.v.i    v16, 9
.LBB785_98:
    vsetvli    zero, zero, e8, m1, ta, mu
    vluxei64.v    v17, (s4), v8
    vand.vx    v18, v17, a4
    vmseq.vi    v0, v18, 0
    vadd.vv    v17, v17, 9, v0

preames avatar Jun 30 '22 19:06 preames

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

llvmbot avatar Jun 30 '22 19:06 llvmbot

Worth noting is that we appear to have many variations on this missed optimization. I see many variations of sub-optimal code generation around vectors which are conditionally one of two small constants.

preames avatar Jun 30 '22 19:06 preames

I suppose given example is not correct for the suggested optimization. From riscv-v-spec:

vmerge.vim vd, vs2, imm, v0 # vd[i] = v0.mask[i] ? imm : vs2[i]

In this example it means that vadd will be processed as

vadd.vv v17, v17, v16, v0 # v17[i] = v0.mask[i] ? v16[i] + v17[i] : v17[i] or
                          # v17[i] += v0.mask[i] ? v16[i] : 0

instead of

vmerge.vim    v18, v16, 0, v0 # v18[i] = v0.mask[0] ? 0 : v16[i]
vadd.vv       v17, v18, v17   # v17[i] = v17[i] + v18[i] or
                              # v17[i] += v0.mask[i] ? 0 : v16[i]

i.e. as if mask is inverted. By the way, if we replace vadd with xor in this example, it will work correctly since 0 xor 0 = 1 and 1 xor 0 = 0 that means inverted mask.

But I think similar optimization for vadd is still possible. For example, if we get following input

vmv           v19, 0            # any available register
vmerge.vvm    v18, v19, v16, v0 # v18[i] = v0.mask[i] ? v16[i] : 0
vadd.vv       v17, v18, v17     # v17[i] = v17[i] + v18[i] or
                                # v17[i] += v0.mask[i] ? v16[i] : 0

we can fold it to

vadd.vv       v17, v17, v16, v0 # v17[i] = v0.mask[i] ? v16[i] + v17[i] : v17[i] 

Similar transformation can be applied to other operations that have identity constant that is passed as RHS operand to vmerge.

yt-sc avatar Aug 11 '22 12:08 yt-sc

I think this revision https://reviews.llvm.org/D131551 could perform that transformation.

jacquesguan avatar Aug 18 '22 09:08 jacquesguan