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

            Bug ID: 119546
           Summary: Bogus -Wuninitialized warnings with scalar REDUCE
                    intrinsic
           Product: gcc
           Version: 15.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: anlauf at gcc dot gnu.org
  Target Milestone: ---

The following testcase generates bogus warnings with -O -Wall:

program test_reduce
  implicit none
  integer, parameter :: n = 3
  integer, parameter :: vec(n) = [2, 5, 10]
  integer :: res0

  res0 = reduce (vec, add)
  if (res0 /= 17) stop 1
contains
  pure function add(i,j) result(sum_ij)
    integer, intent(in) :: i, j
    integer             :: sum_ij
    sum_ij = i + j
  end function add
end

%  gfc-15 reduce_0.f90 -O -Wall
reduce_0.f90:7:26:

    7 |   res0 = reduce (vec, add)
      |                          ^
Warning: 'sr.1' is used uninitialized [-Wuninitialized]
reduce_0.f90:7:26:

    7 |   res0 = reduce (vec, add)
      |                          ^
note: 'sr.1' declared here


Looking at the dump tree, it appears that the warning is sort of correct,
as the temporary for the result variable is not initialized.

The following chunk from r15-8650 generates this temporary:

@@ -8589,6 +8596,17 @@ gfc_conv_procedure_call (gfc_se * se, gfc_symbol * sym,
       else if (ts.type == BT_CHARACTER)
        vec_safe_push (retargs, len);
     }
+  else if (reduce_scalar)
+    {
+      /* In order that the library function for intrinsic REDUCE be type and
+        kind agnostic, the result is passed by reference.  Allocatable
+        components are handled within the OPERATION wrapper.  */
+      type = gfc_typenode_for_spec (&expr->ts);
+      result = gfc_create_var (type, "sr");
+      tmp =  gfc_build_addr_expr (pvoid_type_node, result);
+      vec_safe_push (retargs, tmp);
+    }
+
   gfc_free_interface_mapping (&mapping);

   /* We need to glom RETARGS + ARGLIST + STRINGARGS + APPEND_ARGS.  */

I tried the following to suppress the warning, but without effect:

(a)

    DECL_INITIAL (result) = build_zero_cst (TREE_TYPE (result));

This zeroes the temporary, but the warning stays.

(b)

    gfc_add_modify (&se->pre, result, build_clobber (TREE_TYPE (result)));

This generates a CLOBBER of sr.1 directly before the call of
_gfortran_reduce_scalar, but again the warning does not go away.

I am confused now.

Reply via email to