CORE-V bitmanip builtin failure with bclr/bset
Created by: jeremybennett
Test program (cv-bitmanip-bug.c
):
#include <stdint.h>
#include <stdlib.h>
volatile uint32_t src;
int main ()
{
src = 0xc64a5933;
if (__builtin_riscv_cv_bitmanip_bclr (src, 0x165) != src & 0xfffe001f)
abort ();
}
Compile with:
riscv32-corev-elf-gcc cv-bitmanip-bug.c -O2 -march=rv32i_xcvbitmanip -mabi=ilp32 -specs=nano.specs -lm -Wl,-Tembecosm-link.ld -save-temps -ggdb3 -o ./cv-bitmanip-bug.exe
(where the linker script is in this case suitable for a FPGA X-HEEP CV32E40Pv2 image).
The code hits abort. Looking at the generated code, we see no use of the cv.bclr
instruction:
Dump of assembler code for function main:
0x00000228 <+0>: lui a4,0xc64a6
0x0000022c <+4>: addi a4,a4,-1741 # 0xc64a5933
0x00000230 <+8>: sw a4,-1936(gp)
0x00000234 <+12>: lw a4,-1936(gp)
0x00000238 <+16>: lw a5,-1936(gp)
0x0000023c <+20>: bnez a5,0x248 <main+32>
0x00000240 <+24>: li a0,0
0x00000244 <+28>: ret
0x00000248 <+32>: addi sp,sp,-16
0x0000024c <+36>: sw ra,12(sp)
0x00000250 <+40>: jal 0x3b0 <abort>
We observe that there is no problem if we do not set src
, so this appears to be a misoptimization based on knowing the constant value in src
and (wrongly) working out what the result of cv.bclr
must be from its machine description.
The analogous problem occurs with cv.bset