[Bug c/91107] New: __attribute__((pure)) to function with non-const pointers

colomar.6.4.3 at gmail dot com gcc-bugzilla@gcc.gnu.org
Sun Jul 7 16:39:00 GMT 2019


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

            Bug ID: 91107
           Summary: __attribute__((pure)) to function with non-const
                    pointers
           Product: gcc
           Version: 8.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: colomar.6.4.3 at gmail dot com
  Target Milestone: ---

I have a function that takes two arrays and writes into a third array the
result of a pure function (c_i = a_i / b_i):

inline
void    array_division      (ptrdiff_t nmemb,
                             double dest[static nmemb],
                             const double src1[static nmemb],
                             const double src2[static nmemb])
        __attribute__((nonnull, pure));

inline
void    array_division      (ptrdiff_t nmemb,
                             double dest[static nmemb],
                             const double src1[static nmemb],
                             const double src2[static nmemb])
{

        for (ptrdiff_t i = 0; i < nmemb; i++)
                dest[i] = src1[i] / src2[i];
}

Given that it works with full arrays instead of single variables, I can't just
return the output, and therefore need to modify the array through a pointer,
which goes against what a pure function is by the docs.

However, I think it is still as valid as any other pure function to be
optimized as a pure function.

A pure function is one that acts as a function of its parameters, and the
values pointed to by those parameters; and if none of the parameters, nor the
values pointed to by them, change between two calls, they should be redundant
and the call can be omitted, evaluating to the same value returned by the
previous call.

This function follows all those rules: If none of the 3 arrays change between
two function calls, the function call can be completely removed; and they
evaluate to the same value (which is none, because it's void).

Am I right in thinking that GCC should allow this usage of
__attibute__((pure))?

Or is there any optimization achieved by pure that would be defeated by this
function?


Btw, I tricked GCC into accepting to compile this code just by adding a dummy
`return 0;`.  Is that safe, or is the optimization likely to produce broken
code?


If this function is finally accepted as a pure function, pure should be
accepted to be used in a void function, and accept non-const parameters, but if
not, I think there's no reason at all to allow non-const pointers, and should
probably be banned.


More information about the Gcc-bugs mailing list