[llvm-bugs] [Bug 43609] New: Miscompile with -ffast-math and -ftree-vectorize on arithmetic involving doubles and unsigned longs

via llvm-bugs llvm-bugs at lists.llvm.org
Tue Oct 8 10:23:04 PDT 2019


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: unassignedclangbugs at nondot.org
          Reporter: rlcamp.pdx at gmail.com
                CC: blitzrakete at gmail.com, dgregor at apple.com,
                    erik.pilkington at gmail.com, llvm-bugs at lists.llvm.org,
                    richard-llvm at metafoo.co.uk

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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20191008/9b17704c/attachment.html>


More information about the llvm-bugs mailing list