https://bugs.llvm.org/show_bug.cgi?id=43609

            Bug ID: 43609
           Summary: Miscompile with -ffast-math and -ftree-vectorize on
                    arithmetic involving doubles and unsigned longs
           Product: clang
           Version: 8.0
          Hardware: PC
                OS: All
            Status: NEW
          Severity: normal
          Priority: P
         Component: C
          Assignee: [email protected]
          Reporter: [email protected]
                CC: [email protected], [email protected],
                    [email protected], [email protected],
                    [email protected]

The following program:

#include <stdlib.h>
#include <stdio.h>

__attribute((noinline)) void fill_vector(double * x, const double dx, const
size_t X) {
    /* fill vector with uniformly spaced values */
    for (size_t ix = 0; ix < X; ix++)
        x[ix] = (ix + 0.5) * dx;
}

int main(void) {
    const size_t X = 19;
    double * x = malloc(sizeof(double) * X);

    fill_vector(x, 1.0, X);

    /* print the values */
    for (size_t ix = 0; ix < X; ix++)
        fprintf(stdout, "x[%zu] = %g\n", ix, x[ix]);
}

produces the following output in Clang 8 and newer with -Ofast (or -O1
-ffast-math -ftree-vectorize):

x[0] = 0
x[1] = 2
x[2] = 2
x[3] = 4
x[4] = 4
x[5] = 6
x[6] = 6
x[7] = 8
x[8] = 8
x[9] = 10
x[10] = 10
x[11] = 12
x[12] = 12
x[13] = 14
x[14] = 14
x[15] = 16
x[16] = 16.5
x[17] = 17.5
x[18] = 18.5

Note the change from incorrect to correct behaviour between the groups-of-4 and
the last three entries. 

Changing the loop variable from "size_t" to "unsigned int" makes the problem go
away. Compiling with "-fno-associative-math" or, strangely,
"-fno-reciprocal-math" also makes it go away.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to