https://gcc.gnu.org/bugzilla/show_bug.cgi?id=129060
Bug ID: 129060
Summary: RISC-V can reuse a stale byte value after cbo.zero
clears its cache block
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: target
Assignee: unassigned at gcc dot gnu.org
Reporter: majin at gcc dot gnu.org
Target Milestone: ---
Description:
When code reads a byte before and after a cbo.zero at another address in
the same cache block, GCC folds the difference between the two reads to 0.
Compiler information
GCC 17.0.0 20261125 (experimental) targeting riscv64-unknown-linux-gnu
GCC master revision tested: 1330eed38849ad186518f609c52bd5651c2bfdc1
Reproducer
Save the following as testcase.c.
#include <stdio.h>
static unsigned char data[64] __attribute__ ((aligned (64)));
__attribute__ ((noipa)) static int
clear_block (unsigned char *p)
{
int before = p[31];
__builtin_riscv_zicboz_cbo_zero (p + 7);
return before - p[31];
}
int main (void)
{
data[31] = 17;
int difference = clear_block (data);
printf ("%d\n", difference);
return difference != 17;
}
riscv64-unknown-linux-gnu-gcc -O2 -mabi=lp64d -march=rv64gcv_zicboz_zic64b \
-mrvv-max-lmul=m1 -static testcase.c -o testcase
qemu-riscv64 -cpu max,vlen=256 ./testcase
Actual result
After cbo.zero, clear_block emits li a0,0 followed by ret. The program
prints 0 and exits with status 1.
Expected result
p + 7 and p + 31 lie in the same 64-byte cache block. Clearing the block
should set p[31] to 0, so the difference should be 17. With the proposed
fix, the program prints 17 and exits with status 0.