Adding [Rounding]ShiftRightAndDemoteTo ops
- Dominant language
- C++
- Stars
- 5.8k
- Forks
- 471
- Avg merge
- 1d 6h
- Merged PRs (30d)
- 81
Description
While Clang 20 and later can optimize [Rounding]ShiftRight+DemoteTo down to a single instruction on NEON on AArch64, there are some other targets that can carry out [Rounding]ShiftRight+DemoteTo using fewer instructions than `DemoteTo(dn, [Rounding]ShiftRight(v))` (due to missed optimizations by GCC/Clang as of GCC 15 and Clang 20), including NEON on Armv7, SVE2, RVV, LSX, and LASX.
ShiftRight+DemoteTo is equivalent to NEON `vqshr[u]n_n_*`, SVE2 `svqshr[u]n_n_`+`svuzp1`, RVV `__rvv_vnclip[u]` with the __RISCV_VXRM_RDN rounding mode, LSX `__lsx_vsrani_*`/`__lsx_vsrlni_*`, and LASX `__lasx_xvsrani_*`/`__lasx_xvsrlni_*`+`__lasx_xvpermi_d`.
RoundingShiftRight+DemoteTo is equivalent to NEON `vqrshr[u]n_n_*`, SVE2 `svqrshr[u]n_n_`+`svuzp1`, RVV `__rvv_vnclip[u]` with the __RISCV_VXRM_RNU rounding mode, LSX `__lsx_vsrarni_*`/`__lsx_vsrlrni_*`, and LASX `__lasx_xvsrarni_*`/`__lasx_xvsrlrni_*`+`__lasx_xvpermi_d`.
There are some real-world use cases for RoundingShiftRightAndDemoteTo, including in a portable, cross-platform implementation of the libaom convolve8 ops (whose NEON implementations in aom_dsp/arm/aom_convolve8_neon.c take advantage of vqrshrun_n_s16).
There is also plenty of image processing code that uses `vqrshrun_n_s16` according to a Sourcegraph search.
Should separate [Rounding]ShiftRightAndDemoteTo ops be added due to the better codegen that [Rounding]ShiftRightAndDemoteTo has over `DemoteTo(dn, [Rounding]ShiftRight(v))` on some targets and the use of such ops in image processing code and video codecs?
Contributor guide
Assessment
This issue has not been assessed yet.