LLM Application Vectorization Challenge (2. Porting Indirect Memory Access (Gather) to RVV)

Continuing from last time.

https://msyksphinz-en.hatenablog.com/entry/2026/01/20/040000msyksphinz-en.hatenablog.com

2. Porting Indirect Memory Access (Gather) to RVV

The next case is a bit harder. Using array x as indices, we accumulate y[x[i]]. This is not a contiguous access, so instead of a normal vle we need a gather (indexed load).

floatima(size_t n,size_t x[],float y[]) {float sum =0;for (size_t i =0; i < n; i++) {
        sum += y[x[i]];
    }return sum;
}

When I asked the AI in the same way, the first implementation it produced looked like this (excerpt).

vy = __riscv_vluxei64_v_f32m1(y, vx, vl);
vsum = __riscv_vfredusum_vs_f32m1_f32m1(vy, vsum, vl);

However, this is incomplete as it stands. The reason is that it is easy to get wrong what the index of vluxei* represents.

The index of vluxei* is a "byte offset", not an "element number"

The RVV vluxei* family (indexed loads) computes the address as base address + index (offset in bytes) (at least in the general case).

So if x[i] is an "element number", you need to multiply by sizeof(float) to convert it to a byte offset before passing it in.

When I pointed this out, the AI fixed it as follows.

vx = __riscv_vle64_v_u64m1(x, vl);// convert element number -> byte offset (float is 4 bytes)vuint64m1_t vx_bytes = __riscv_vmul_vx_u64m1(vx,sizeof(float), vl);

vy = __riscv_vluxei64_v_f32m1(y, vx_bytes, vl);

The direction is right. Furthermore, since sizeof(float)=4=2^2, multiplying by a constant is (in many cases) cheaper as a left shift than as a multiplication. So the natural form is the following.

vuint64m1_t vx_bytes = __riscv_vsll_vi_u64m1(vx,2, vl);

2.2 Where to do the reduction (sum): the trade-off between correctness and speed

Next is this part. The AI's initial proposal did a vfredusum inside the loop.

  • In each iteration, vector-reduce the gathered vy and fold it into vsum
  • At the end, extract it to a scalar with vfmv_f_s and return it

This is functionally easy to understand, but there are some performance concerns.

  • vfredusum tends to be a fairly high-latency operation (depending on the implementation)
  • Reducing in every iteration makes the dependency chain (vsum depends on the next iteration) stronger, which tends to stall the pipeline

On the other hand, floating-point addition is not associative, so changing how the reduction is done may slightly change the final result.

For this use case, I assume that bit-for-bit agreement with the scalar version is not required and that small differences are acceptable.

Constraint: the order of additions does not need to be exactly the same as in the scalar version (the reduction order may change the numerical result slightly)


2.3 Additional optimization: relaxing the dependency with multiple accumulators

With the above constraint stated explicitly, I asked the AI for further optimization, and it proposed using multiple accumulators (vsum0 to vsum3). The aims are:

  • Shorten the dependency chain to gain ILP
  • Rather than concentrating on a single reduction, distribute the work across different accumulators on each iteration

The reasoning is understandable (of course, whether it helps depends on the reduction implementation and scheduling of the actual hardware).

The code that was finally generated looks like this.

#if __riscv_v_intrinsic >= 1000000
#include <riscv_vector.h>

float ima_rvv(size_t n, const size_t *x, const float *y) {
    size_t vl;
    vfloat32m1_t vsum;
    float sum;

    // initial reduction value (a vector length of 1 is fine)
    size_t vl_init = __riscv_vsetvl_e32m1(1);
    vsum = __riscv_vfmv_v_f_f32m1(0.0f, vl_init);

    for (size_t avl = n; avl > 0; avl -= vl) {
        vl = __riscv_vsetvl_e64m1(avl);

        // load the indices (element numbers)
        vuint64m1_t vidx = __riscv_vle64_v_u64m1(x, vl);

        // element number -> byte offset (float = 4 bytes)
        vuint64m1_t vidx_bytes = __riscv_vsll_vi_u64m1(vidx, 2, vl);

        // gather load: y[ x[i] ]
        vfloat32m1_t vy = __riscv_vluxei64_v_f32m1(y, vidx_bytes, vl);

        // reduce (the order may change)
        vsum = __riscv_vfredusum_vs_f32m1_f32m1(vy, vsum, vl);

        x += vl;
    }

    sum = __riscv_vfmv_f_s_f32m1_f32(vsum);
    return sum;
}
#endif

This is an English translation of the original Japanese post: LLMでアプリケーションベクトル化チャレンジ (2. Indirect Memory Access(ギャザー)を RVV 化してみる)