https://gcc.gnu.org/g:a170e2d13746b5c2449408dc7a6a1ede98f5cab5

commit r17-6579-ga170e2d13746b5c2449408dc7a6a1ede98f5cab5
Author: Jerry DeLisle <[email protected]>
Date:   Wed Nov 11 09:07:20 2026 -0800

    libgfortran: [PR128618] Avoid libatomic calls for the unit file id
    
    The PR118935 fix accesses the 64-bit file_dev and file_ino fields of
    gfc_unit with __atomic builtins whenever HAVE_ATOMIC_FETCH_ADD is
    defined, but that configure check tests only an int.  On 32-bit
    targets without inline 64-bit atomics the accesses become calls into
    libatomic, which libgfortran does not link.  r17-6152 made the check
    test a 64-bit type instead, but on those targets that also turns off
    the atomics for the unit waiting count and stream pointer, and the
    waiting count is then incremented under a read lock without them.
    Revert r17-6152 and use atomic accesses for the file id only where
    64-bit atomics are lock-free.
    
    Assisted-by: Claude Opus 5.5
    
            PR libfortran/128618
    
    libgfortran/ChangeLog:
    
            * acinclude.m4 (LIBGFOR_CHECK_ATOMIC_FETCH_ADD): Revert
            r17-6152; test int again.  Note that 64-bit atomics are not
            covered.
            * configure: Regenerate.
            * io/io.h (GFC_ATOMIC_FILE_ID): Define if 64-bit atomics are
            lock-free.
            (set_unit_file_id, unit_file_id_matches): Use it instead of
            HAVE_ATOMIC_FETCH_ADD.

Diff:
---
 libgfortran/acinclude.m4 | 4 +++-
 libgfortran/configure    | 2 +-
 libgfortran/io/io.h      | 9 +++++++--
 3 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/libgfortran/acinclude.m4 b/libgfortran/acinclude.m4
index 044514770b33..919b681ce1af 100644
--- a/libgfortran/acinclude.m4
+++ b/libgfortran/acinclude.m4
@@ -46,10 +46,12 @@ extern void bar(void) __attribute__((alias("foo")));]],
   fi])
 
 dnl Check whether the target supports __atomic_fetch_add.
+dnl Tests int only; 64-bit atomics may need libatomic, which libgfortran
+dnl does not link (see GFC_ATOMIC_FILE_ID in io/io.h).
 AC_DEFUN([LIBGFOR_CHECK_ATOMIC_FETCH_ADD], [
   AC_CACHE_CHECK([whether the target supports __atomic_fetch_add],
                 libgfor_cv_have_atomic_fetch_add, [
-  AC_LINK_IFELSE([AC_LANG_PROGRAM([[__INT64_TYPE__ foovar = 0;]], [[
+  AC_LINK_IFELSE([AC_LANG_PROGRAM([[int foovar = 0;]], [[
 if (foovar <= 0) return __atomic_fetch_add (&foovar, 1, __ATOMIC_ACQ_REL);
 if (foovar > 10) return __atomic_add_fetch (&foovar, -1, 
__ATOMIC_ACQ_REL);]])],
              libgfor_cv_have_atomic_fetch_add=yes, 
libgfor_cv_have_atomic_fetch_add=no)])
diff --git a/libgfortran/configure b/libgfortran/configure
index 3e3e68a46b7d..ed2c497c7298 100755
--- a/libgfortran/configure
+++ b/libgfortran/configure
@@ -32288,7 +32288,7 @@ else
 fi
 cat confdefs.h - <<_ACEOF >conftest.$ac_ext
 /* end confdefs.h.  */
-__INT64_TYPE__ foovar = 0;
+int foovar = 0;
 int
 main ()
 {
diff --git a/libgfortran/io/io.h b/libgfortran/io/io.h
index d80033fd957b..c333a2d12c2f 100644
--- a/libgfortran/io/io.h
+++ b/libgfortran/io/io.h
@@ -1045,13 +1045,18 @@ get_unit_stream (gfc_unit *u)
 #endif
 }
 
+/* 64-bit atomics only where inline; libgfortran does not link libatomic.  */
+#if defined (HAVE_ATOMIC_FETCH_ADD) && __GCC_ATOMIC_LLONG_LOCK_FREE == 2
+# define GFC_ATOMIC_FILE_ID 1
+#endif
+
 /* Record the unit's file identity.  Must be called before the matching
    set_unit_stream.  */
 
 static inline void
 set_unit_file_id (gfc_unit *u, uint64_t dev, uint64_t ino)
 {
-#ifdef HAVE_ATOMIC_FETCH_ADD
+#ifdef GFC_ATOMIC_FILE_ID
   __atomic_store_n (&u->file_dev, dev, __ATOMIC_RELAXED);
   __atomic_store_n (&u->file_ino, ino, __ATOMIC_RELAXED);
 #else
@@ -1063,7 +1068,7 @@ set_unit_file_id (gfc_unit *u, uint64_t dev, uint64_t ino)
 static inline bool
 unit_file_id_matches (gfc_unit *u, uint64_t dev, uint64_t ino)
 {
-#ifdef HAVE_ATOMIC_FETCH_ADD
+#ifdef GFC_ATOMIC_FILE_ID
   return (__atomic_load_n (&u->file_dev, __ATOMIC_RELAXED) == dev
          && __atomic_load_n (&u->file_ino, __ATOMIC_RELAXED) == ino);
 #else

Reply via email to