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

            Bug ID: 125916
           Summary: [17 regression] Mesa maybe miscompiled with
                    __attribute__((const))
           Product: gcc
           Version: 16.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: sjames at gcc dot gnu.org
  Target Milestone: ---

I'm like 99% sure this is bogus but I want to check before I take it to them.

In the last few days, anything using llvmpipe in Mesa started to SIGFPE. This
ended up being because some property was 0.

The relevant function is:
```
static inline const
__attribute__((const))
struct util_cpu_caps_t *
util_get_cpu_caps (void)
{
  extern void _util_cpu_detect_once (void);
  extern struct _util_cpu_caps_state_t _util_cpu_caps_state;

  /* On most CPU architectures, an atomic read is simply a regular memory
   * load instruction with some extra compiler magic to prevent code
   * re-ordering around it.  The perf impact of doing this check should be
   * negligible in most cases.
   *
   * Also, even though it looks like  a bit of a lie, we've declared this
   * function with ATTRIBUTE_CONST.  The GCC docs say:
   *
   *    "Calls to functions whose return value is not affected by changes to
   *    the observable state of the program and that have no observable
   *    effects on such state other than to return a value may lend
   *    themselves to optimizations such as common subexpression elimination.
   *    Declaring such functions with the const attribute allows GCC to avoid
   *    emitting some calls in repeated invocations of the function with the
   *    same argument values."
   *
   * The word "observable" is important here.  With the exception of a
   * llvmpipe debug flag behind an environment variable and a few unit tests,
   * all of which emulate worse CPUs, this function neither affects nor is
   * affected by any "observable" state.  It has its own internal state for
   * sure, but that state is such that it appears to return exactly the same
   * value with the same internal data every time.
   */
  if (unlikely (!p_atomic_read (&_util_cpu_caps_state.detect_done)))
    call_once (&_util_cpu_caps_state.once_flag, _util_cpu_detect_once);

  return &_util_cpu_caps_state.caps;
}
```

```

Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-pc-linux-gnu/17/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-pc-linux-gnu
Configured with:
/var/tmp/portage.notmp/portage/sys-devel/gcc-17.0.9999/work/gcc-17.0.9999/configure
--host=x86_64-pc-linux-gnu --build=x86_64-pc-linux-gnu --prefix=/usr
--bindir=/usr/x86_64-pc-linux-gnu/gcc-bin/17
--includedir=/usr/lib/gcc/x86_64-pc-linux-gnu/17/include
--datadir=/usr/share/gcc-data/x86_64-pc-linux-gnu/17
--mandir=/usr/share/gcc-data/x86_64-pc-linux-gnu/17/man
--infodir=/usr/share/gcc-data/x86_64-pc-linux-gnu/17/info
--with-gxx-include-dir=/usr/lib/gcc/x86_64-pc-linux-gnu/17/include/g++-v17
--disable-silent-rules --disable-dependency-tracking
--with-python-dir=/share/gcc-data/x86_64-pc-linux-gnu/17/python
--enable-libphobos --enable-objc-gc
--enable-languages=c,c++,d,objc,obj-c++,fortran,ada,algol68,m2,jit
--enable-obsolete --enable-secureplt --disable-werror --with-system-zlib
--enable-nls --without-included-gettext --disable-libunwind-exceptions
--enable-checking=yes,extra,rtl --with-bugurl=https://bugs.gentoo.org/
--with-pkgversion='Gentoo Hardened 17.0.9999 p, commit
364a6713b8046ab4d5dcdcffe8501432a4ec0632' --with-gcc-major-version-only
--enable-libstdcxx-time --enable-lto --disable-libstdcxx-pch
--enable-linker-build-id --enable-libada --enable-shared --enable-threads=posix
--enable-__cxa_atexit --enable-clocale=gnu --with-tls=gnu2 --enable-multilib
--with-multilib-list=m32,mx32,m64 --disable-fixed-point --with-abi=m64
--enable-targets=all --enable-offload-defaulted
--enable-offload-targets=nvptx-none --enable-libgomp --disable-libssp
--disable-cet --enable-systemtap --enable-valgrind-annotations
--enable-valgrind-interop --disable-vtable-verify --disable-libvtv --with-zstd
--with-isl --disable-isl-version-check --enable-default-pie --enable-host-pie
--enable-host-bind-now --enable-default-ssp --disable-fixincludes
--with-gxx-libcxx-include-dir=/usr/include/c++/v1 --enable-host-shared
--enable-libgdiagnostics --with-build-config='bootstrap-O3 bootstrap-lto-lean'
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 17.0.0 20260618 (experimental)
7374385c962b867eaf226d0e728aa10352a778bf (Gentoo Hardened 17.0.9999 p, commit
364a6713b8046ab4d5dcdcffe8501432a4ec0632)
```

Reply via email to