https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108595

            Bug ID: 108595
           Summary: -fcall-saved-a1 with -O2 leads to incorrect RISC-V
                    code-gen around inline assembly
           Product: gcc
           Version: 12.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: rtl-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: idan.horowitz at gmail dot com
  Target Milestone: ---

Created attachment 54370
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=54370&action=edit
preprocessed reproducer

The attached code snippet compiled with `-fcall-saved-a1` (which is usually a
caller-saved register) and `-O2` erroneously saves/restores `a1` around the
inline assembly, as if it's a function call. Since GCC allocates `a1` for the
use of the inline assembly, this means that the result of the inline assembly
is clobbered. Note that this issue does not happen with `-O0` or `-O1`, so it
sounds like an optimizer issue to me.

GCC information:
Using built-in specs.
COLLECT_GCC=/opt/compiler-explorer/riscv64/gcc-12.2.0/riscv64-unknown-linux-gnu/bin/riscv64-unknown-linux-gnu-gcc
Target: riscv64-unknown-linux-gnu
Configured with: /opt/.build/riscv64-unknown-linux-gnu/src/gcc/configure
--build=x86_64-build_pc-linux-gnu --host=x86_64-build_pc-linux-gnu
--target=riscv64-unknown-linux-gnu
--prefix=/opt/compiler-explorer/riscv64/gcc-12.2.0/riscv64-unknown-linux-gnu
--exec_prefix=/opt/compiler-explorer/riscv64/gcc-12.2.0/riscv64-unknown-linux-gnu
--with-sysroot=/opt/compiler-explorer/riscv64/gcc-12.2.0/riscv64-unknown-linux-gnu/riscv64-unknown-linux-gnu/sysroot
--enable-languages=c,c++,fortran,ada,d,objc,obj-c++,go --with-arch=rv64gc
--with-abi=lp64d --with-pkgversion='crosstool-NG UNKNOWN' --enable-__cxa_atexit
--enable-libmudflap --enable-libgomp --enable-libssp --enable-libquadmath
--enable-libquadmath-support --disable-libsanitizer --disable-libmpx
--with-gmp=/opt/.build/riscv64-unknown-linux-gnu/buildtools
--with-mpfr=/opt/.build/riscv64-unknown-linux-gnu/buildtools
--with-mpc=/opt/.build/riscv64-unknown-linux-gnu/buildtools
--with-isl=/opt/.build/riscv64-unknown-linux-gnu/buildtools --enable-lto
--enable-threads=posix --enable-target-optspace --disable-plugin --disable-nls
--disable-multilib
--with-local-prefix=/opt/compiler-explorer/riscv64/gcc-12.2.0/riscv64-unknown-linux-gnu/riscv64-unknown-linux-gnu/sysroot
--enable-long-long
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 12.2.0 (crosstool-NG UNKNOWN) 

The code itself is very short, but the `stdint.h` and `stddef.h` headers make
the preprocessed file pretty large, so it's attached.

Godbolt link which encompasses all of the above:
https://godbolt.org/z/h7rnGcqGh

Reply via email to