[PATCH] riscv: Use page-bounded loads in RVV strlen

Peter Bergner bergner@oss.tenstorrent.com
Mon Nov 23 16:20:18 GMT 2026


On 11/23/26 2:41 AM, Chengrong Dai wrote: 
> Replace fault-only-first vector loads with ordinary byte loads bounded

s/ordinary/ordinary vector/.   Your choice if you also want to drop
word "byte", since both the fault-only-first and ordinary loads are
byte loads.



> +    li page_mask, 4095
> +    li page_size, 4096

Andreas showed an improved way to compute page_mask.



> +    /* Linux RISC-V pages are at least 4096 bytes.  Restrict ordinary
> +       vector loads to the current aligned 4096-byte region, so a NUL
> +       before an unreadable page is always found without crossing it.  */
> +L(page):
> +    and remaining, copy_str, page_mask
> +    sub remaining, page_size, remaining
>  L(loop):
> -    vsetvli ivl, zero, e8, ELEM_LMUL_SETTING, ta, ma
> -    vle8ff.v vstr, (copy_str)
> -    csrr cur_vl, vl
> +    /* Use the actual VL returned by vsetvli.  An ordinary load does not
> +       change VL, so no CSR readback is needed.  */
> +    vsetvli ivl, remaining, e8, ELEM_LMUL_SETTING, ta, ma
> +    vle8.v vstr, (copy_str)
>      vmseq.vi vmask_end, vstr, 0
>      vfirst.m end_offset, vmask_end
> -    add copy_str, copy_str, cur_vl
> -    bltz end_offset, L(loop)
> +    bgez end_offset, L(found)
> +
> +    add copy_str, copy_str, ivl
> +    sub remaining, remaining, ivl
> +    bnez remaining, L(loop)
> +    j L(page)
>  
> -    add str, str, cur_vl
> -    add copy_str, copy_str, end_offset
> +L(found):
>      sub result, copy_str, result
> +    add result, result, end_offset

This currently has the main loop embedded inside the page aligning loop,
but we should be able to execute a page aligning loop, followed by the
main loop without having to execute the page aligning code anymore, similar
to how other architectures do it.  That should allow us to hoist the vsetvli
outside the main loop body too, correct?

Peter




More information about the Libc-alpha mailing list