riscv: probe for Zbb extension at load time

Due to hysterical raisins, most RISC-V Linux distributions target a
RV64GC baseline excluding the Bit-manipulation ISA extensions, most
notably:
- Zba: address generation extension and
- Zbb: basic bit manipulation extension.
Most CPUs that would make sense to run FFmpeg on support Zba and Zbb
(including the current FATE runner), so it makes sense to optimise for
them. In fact a large chunk of existing assembler optimisations relies
on Zba and/or Zbb.

Since we cannot patch shared library code, the next best thing is to
carry a flag initialised at load-time and check it on need basis.
This results in 3 instructions overhead on isolated use, e.g.:
1:  AUIPC rd, %pcrel_hi(ff_rv_zbb_supported)
    LBU   rd, %pcrel_lo(1b)(rd)
    BEQZ  rd, non_Zbb_fallback_code
    // Zbb code here

The C compiler will typically load the flag ahead of time to reducing
latency, and can also keep it around if Zbb is used multiple times in a
single optimisation scope. For this to work, the flag symbol must be
hidden; otherwise the optimisation degrades with a GOT look-up to
support interposition:
1:  AUIPC rd, GOT_OFFSET_HI
    LD    rd, GOT_OFFSET_LO(rd)
    LBU   rd, (rd)
    BEQZ  rd, non_Zbb_fallback_code
    // Zbb code here

This patch adds code to provision the flag in libraries using bit
manipulation functions from libavutil: byte-swap, bit-weight and
counting leading or trailing zeroes.
This commit is contained in:
Rémi Denis-Courmont 2024-06-08 10:57:15 +03:00
parent 18adaf9fe5
commit 378d1b06c3
14 changed files with 67 additions and 1 deletions

View File

@ -77,3 +77,5 @@ RVV-OBJS-$(CONFIG_VP9_DECODER) += riscv/vp9_intra_rvv.o \
riscv/vp9_mc_rvv.o
OBJS-$(CONFIG_VORBIS_DECODER) += riscv/vorbisdsp_init.o
RVV-OBJS-$(CONFIG_VORBIS_DECODER) += riscv/vorbisdsp_rvv.o
SHLIBOBJS += riscv/cpu_common.o

View File

@ -0,0 +1 @@
#include "libavutil/riscv/cpu_common.c"

View File

@ -0,0 +1 @@
SHLIBOBJS += riscv/cpu_common.o

View File

@ -0,0 +1 @@
#include "libavutil/riscv/cpu_common.c"

View File

@ -1,2 +1,4 @@
OBJS-$(CONFIG_AFIR_FILTER) += riscv/af_afir_init.o
RVV-OBJS-$(CONFIG_AFIR_FILTER) += riscv/af_afir_rvv.o
SHLIBOBJS += riscv/cpu_common.o

View File

@ -0,0 +1 @@
#include "libavutil/riscv/cpu_common.c"

View File

@ -0,0 +1 @@
SHLIBOBJS += riscv/cpu_common.o

View File

@ -0,0 +1 @@
#include "libavutil/riscv/cpu_common.c"

View File

@ -1,7 +1,8 @@
OBJS += riscv/float_dsp_init.o \
riscv/fixed_dsp_init.o \
riscv/lls_init.o \
riscv/cpu.o
riscv/cpu.o \
riscv/cpu_common.o
RVV-OBJS += riscv/float_dsp_rvv.o \
riscv/fixed_dsp_rvv.o \
riscv/lls_rvv.o

View File

@ -24,8 +24,22 @@
#include "config.h"
#include <stdbool.h>
#include <stddef.h>
#include "libavutil/attributes_internal.h"
#include "libavutil/cpu.h"
#ifndef __riscv_zbb
extern attribute_visibility_hidden bool ff_rv_zbb_supported;
#endif
static inline av_const bool ff_rv_zbb_support(void)
{
#ifndef __riscv_zbb
return ff_rv_zbb_supported;
#else
return true;
#endif
}
#if HAVE_RVV
/**
* Returns the vector size in bytes (always a power of two and at least 4).

View File

@ -0,0 +1,33 @@
/*
* Copyright © 2024 Rémi Denis-Courmont.
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "libavutil/cpu.h"
#ifndef __riscv_zbb
unsigned char ff_rv_zbb_supported = 0;
#ifdef __ELF__
__attribute__((constructor))
static void probe_zbb(void)
{
ff_rv_zbb_supported = (av_get_cpu_flags() & AV_CPU_FLAG_RVB_BASIC) != 0;
}
#endif
#endif

View File

@ -4,3 +4,5 @@ RV-OBJS += riscv/rgb2rgb_rvb.o
RVV-OBJS += riscv/input_rvv.o \
riscv/range_rvv.o \
riscv/rgb2rgb_rvv.o
SHLIBOBJS += riscv/cpu_common.o

View File

@ -0,0 +1 @@
#include "libavutil/riscv/cpu_common.c"

View File

@ -3,16 +3,21 @@ libavcodec/file_open.c
libavcodec/interplayacm.c
libavcodec/log2_tab.c
libavcodec/reverse.c
libavcodec/riscv/cpu_common.c
libavdevice/file_open.c
libavdevice/reverse.c
libavdevice/riscv/cpu_common.c
libavfilter/file_open.c
libavfilter/log2_tab.c
libavfilter/riscv/cpu_common.c
libavformat/file_open.c
libavformat/golomb_tab.c
libavformat/log2_tab.c
libavformat/rangecoder_dec.c
libavformat/riscv/cpu_common.c
libswresample/log2_tab.c
libswscale/log2_tab.c
libswscale/riscv/cpu_common.c
tools/uncoded_frame.c
tools/yuvcmp.c
Headers without standard inclusion guards: