llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Silent wrong result at -O2/-O3: SLP vectorization turns a mixed-sign 16-bit comparison into a narrow signed compare

Open ICSE-27 opened this issue 2 months ago • 2 comments

Environment

Clang 19.1.0 (x86-64-unknown-linux-gnu, release build without assertions)

Command

clang++ -std=c++11 -O2 test12.cpp -o a.out && ./a.out

Expected behavior

The program prints "acc=9 (expected 9)" (as the -O0 and -O1 builds do).

Actual behavior

The -O2/-O3 builds print "acc=1 (expected 9)". No crash — the value is silently wrong.

Additional notes

The loop nest accumulates unsigned comparisons that SLP vectorizes with an incorrectly narrowed signed comparison. The test case was produced by an automated compiler-testing tool (GapSmith).

Test case (test12.cpp)

#include <stdio.h>
unsigned int acc = 0;
unsigned short a[6][6];
short b[6];

__attribute__((noinline)) void test() {
    for (long long i = 0; i < 6; i += 2)
        for (long long j = 0; j < 6; j += 2)
            acc += a[j][i] & (a[j][i] >= b[j]);
}

int main() {
    for (int i = 0; i < 6; i++) {
        b[i] = 14288;
        for (int j = 0; j < 6; j++) a[i][j] = 61035;
    }
    test();
    printf("acc=%u (expected 9)\n", acc);
    return acc != 9;
}

ICSE-27 avatar Sep 26 '26 13:09 ICSE-27

Could you please try 23 or main branch? https://godbolt.org should be helpful. Only most recent release is maintained.

EugeneZelenko avatar Sep 26 '26 13:09 EugeneZelenko

https://godbolt.org/z/6GxhMdETj

fixed in trunk but not 23.x

Andarwinux avatar Sep 26 '26 13:09 Andarwinux