https://gcc.gnu.org/g:25c68e98dbafddb029d9d4e1d68c4f76e85ac2bc
commit r17-6507-g25c68e98dbafddb029d9d4e1d68c4f76e85ac2bc Author: Jin Ma <[email protected]> Date: Fri Nov 20 16:03:42 2026 -0700 [PATCH] RISC-V: Honor the memset threshold in all expansion paths Prevent cache-block clearing and by-pieces expansion from bypassing an explicit memset inline threshold. Assisted-by: qwenwork gcc/ChangeLog: * config/riscv/riscv-string.cc (riscv_expand_block_clear): Check the memset threshold. * config/riscv/riscv.cc (riscv_use_by_pieces_infrastructure_p): Honor the memset threshold before considering by-pieces expansion. gcc/testsuite/ChangeLog: * gcc.target/riscv/setmem-cbo-threshold.c: New test. * gcc.target/riscv/setmem-pieces-threshold.c: New test. Diff: --- gcc/config/riscv/riscv-string.cc | 6 +++++ gcc/config/riscv/riscv.cc | 6 +++++ .../gcc.target/riscv/setmem-cbo-threshold.c | 12 ++++++++++ .../gcc.target/riscv/setmem-pieces-threshold.c | 28 ++++++++++++++++++++++ 4 files changed, 52 insertions(+) diff --git a/gcc/config/riscv/riscv-string.cc b/gcc/config/riscv/riscv-string.cc index 9087ba5fe6f2..afbaf0b95c0e 100644 --- a/gcc/config/riscv/riscv-string.cc +++ b/gcc/config/riscv/riscv-string.cc @@ -1101,6 +1101,12 @@ riscv_expand_block_clear (rtx dest, rtx length, bool testing_p) if (optimize_function_for_size_p (cfun)) return false; + if (CONST_INT_P (length) + && riscv_memset_size_threshold >= 0 + && UINTVAL (length) + > (unsigned HOST_WIDE_INT) riscv_memset_size_threshold) + return false; + return riscv_expand_block_clear_zicboz_zic64b (dest, length, testing_p); } diff --git a/gcc/config/riscv/riscv.cc b/gcc/config/riscv/riscv.cc index 3365a8315699..4fd86d95fe5e 100644 --- a/gcc/config/riscv/riscv.cc +++ b/gcc/config/riscv/riscv.cc @@ -16050,6 +16050,12 @@ riscv_use_by_pieces_infrastructure_p (unsigned HOST_WIDE_INT size, unsigned alignment, enum by_pieces_operation op, bool speed_p) { + /* Do not bypass an explicit memset threshold through by-pieces. */ + if ((op == CLEAR_BY_PIECES || op == SET_BY_PIECES) + && riscv_memset_size_threshold >= 0 + && size > (unsigned HOST_WIDE_INT) riscv_memset_size_threshold) + return false; + /* Query the expanders whether they can handle the given operation. */ if ((op == CLEAR_BY_PIECES || op == SET_BY_PIECES) && speed_p && size > UNITS_PER_WORD) diff --git a/gcc/testsuite/gcc.target/riscv/setmem-cbo-threshold.c b/gcc/testsuite/gcc.target/riscv/setmem-cbo-threshold.c new file mode 100644 index 000000000000..d12a9357877b --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/setmem-cbo-threshold.c @@ -0,0 +1,12 @@ +/* { dg-do compile { target rv64 } } */ +/* { dg-options "-O2 -march=rv64gcv_zvl256b_zic64b_zicboz -mabi=lp64d --param=memset-size-threshold=63" } */ + +void +clear_block (void *dst) +{ + dst = __builtin_assume_aligned (dst, 64); + __builtin_memset (dst, 0, 256); +} + +/* { dg-final { scan-assembler-not "cbo\\.zero" } } */ +/* { dg-final { scan-assembler "\\t(call|tail)\\tmemset" } } */ diff --git a/gcc/testsuite/gcc.target/riscv/setmem-pieces-threshold.c b/gcc/testsuite/gcc.target/riscv/setmem-pieces-threshold.c new file mode 100644 index 000000000000..e2599cd99cdf --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/setmem-pieces-threshold.c @@ -0,0 +1,28 @@ +/* { dg-do compile { target rv64 } } */ +/* { dg-options "-O2 -march=rv64gcv -mabi=lp64d -mtune=generic-ooo --param=memset-size-threshold=3" } */ + +void +clear_short (void *dst) +{ + __builtin_memset (dst, 0, 7); +} + +void +clear_words (void *dst) +{ + __builtin_memset (dst, 0, 32); +} + +void +fill_short (void *dst, int value) +{ + __builtin_memset (dst, value, 7); +} + +void +fill_words (void *dst, int value) +{ + __builtin_memset (dst, value, 32); +} + +/* { dg-final { scan-assembler-times "\\t(call|tail)\\tmemset" 4 } } */
