Let's also make the patch available by email. (AKA I forgot to attach it.)

Tobias Burnus wrote:
GCC currently prints the not so helpful:

    24 |   use three
       |      1
Error: Ambiguous !$OMP DECLARE REDUCTION from module three at (1)
    23 |   use two
       |      1
Error: Previous !$OMP DECLARE REDUCTION from module two at (1)


That's okay if there is only a single reduction - but if there
are multiple ...


Solution: Output operator and type in addition. I also switched to
'Note:' to make the relation error + associated clearer compared
to having two 'errors'.

    24 |   use three
       |      1
Error: Ambiguous !$OMP DECLARE REDUCTION ‘operator +’ for type ‘TYPE(t)’ from 
module ‘three’ at (1)
decl-red2.f90:23:6:
    23 |   use two
       |      1
note: Previous !$OMP DECLARE REDUCTION from module ‘two’


Committed as Rev. r17-1443-gfd268b83ffb58a

Are there any (post-commit) comments and remarks?

Tobias

PS: I guess we should eventually add 'gfc_inform' with buffer handling;
additionally 'note:' should be 'Note:' (→ gfc-diagnostic.def).

PPS: The code doesn't handle derived-type renaming. That's the same issue
as the 'declare mapper' patch that I will sent in a moment and will have to
be dealt as followup to both declare mapper/reduction.
commit fd268b83ffb58ae700380e1e52af279f782fdb38
Author: Tobias Burnus <[email protected]>
Date:   Tue Jun 9 13:58:03 2026 +0200

    Fortran/OpenMP: Improve declare-reduction diagnostic
    
    Change the diagnostic for the ambiguity check for 'omp declare reduction'
    to actually output the reduction operator/idenfier and the type to which
    this reduction applies to.
    
    gcc/fortran/ChangeLog:
    
            * module.cc (load_omp_udrs): Improve reduction diagnostic output.
    
    gcc/testsuite/ChangeLog:
    
            * gfortran.dg/gomp/declare-reduction-1.f90: New test.
---
 gcc/fortran/module.cc                              | 13 ++++----
 .../gfortran.dg/gomp/declare-reduction-1.f90       | 39 ++++++++++++++++++++++
 2 files changed, 46 insertions(+), 6 deletions(-)

diff --git a/gcc/fortran/module.cc b/gcc/fortran/module.cc
index 99e162431d4..2bb116c79a4 100644
--- a/gcc/fortran/module.cc
+++ b/gcc/fortran/module.cc
@@ -76,6 +76,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "parse.h" /* FIXME */
 #include "constructor.h"
 #include "cpp.h"
+#include "diagnostic-core.h"
 #include "scanner.h"
 #include <zlib.h>
 
@@ -5443,12 +5444,12 @@ load_omp_udrs (void)
 	  pointer_info *p = get_integer (atom_int);
 	  if (strcmp (p->u.rsym.module, udr->omp_out->module))
 	    {
-	      gfc_error ("Ambiguous !$OMP DECLARE REDUCTION from "
-			 "module %s at %L",
-			 p->u.rsym.module, &gfc_current_locus);
-	      gfc_error ("Previous !$OMP DECLARE REDUCTION from module "
-			 "%s at %L",
-			 udr->omp_out->module, &udr->where);
+	      gfc_error ("Ambiguous !$OMP DECLARE REDUCTION %qs for type %qs "
+			 "from module %qs at %L", udr->name,
+			 gfc_typename (&ts), module_name, &gfc_current_locus);
+	      inform (gfc_get_location (&udr->where),
+		      "Previous !$OMP DECLARE REDUCTION from module %qs",
+		      udr->omp_out->module);
 	    }
 	  skip_list (1);
 	  continue;
diff --git a/gcc/testsuite/gfortran.dg/gomp/declare-reduction-1.f90 b/gcc/testsuite/gfortran.dg/gomp/declare-reduction-1.f90
new file mode 100644
index 00000000000..32f00aaf2b0
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/gomp/declare-reduction-1.f90
@@ -0,0 +1,39 @@
+! { dg-do compile }
+
+module one
+  implicit none
+  type t
+    integer :: x
+  end type t
+end module
+
+module two
+  use one
+  implicit none
+  !$omp declare reduction(+ : t : omp_out%x = omp_out%x + omp_in%x) initializer (omp_priv%x = 0)
+end module
+
+module three
+  use one
+  implicit none
+  !$omp declare reduction(+ : t : omp_out%x = omp_out%x + omp_in%x) initializer (omp_priv%x = 0)
+end module
+
+subroutine sub2
+  use two    ! { dg-note "Previous !.OMP DECLARE REDUCTION from module 'two'" }
+  use three  ! { dg-error "Ambiguous !.OMP DECLARE REDUCTION 'operator \\+' for type 'TYPE\\(t\\)' from module 'three' at .1." }
+  implicit none
+  type(t) :: var(3), sum
+  integer :: i
+
+  var(:)%x = [1,2,3]
+  sum%x = 0
+
+  !$omp parallel do reduction(+: sum)
+  do i = 1, 3
+    sum%x = sum%x + var(i)%x 
+  end do
+end
+
+call sub2
+end

Reply via email to