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

[Polly] Probably Miscompile in ISL probably involving UMAX and SCEV

Open thapgua opened this issue 8 months ago • 3 comments

I have probably encountered a piece of code that causes Polly to miscompile the code via [[assume(expr)]].

Reproducer: https://godbolt.org/z/KTz4Pn8fd

compiler version:

clang version 23.0.0git (https://github.com/llvm/llvm-project.git 6f9646a598f25efa3c4db066d2d51fb248b13526)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /data/lenovo/compiler-build/llvm/llvm-project/build/bin
Build config: +assertions

Steps to Reproduce:

  • Save the following code to prep.cpp ;
  • Compile with Polly enabled to trigger the inconsistency;
  • Compile without Polly or -DVALIDATE to change assume to assert, and it returns with correct result.
$ clang prep.cpp && ./a.out
4211739487735230608

$ clang prep.cpp -mllvm -polly -O3 && ./a.out
11791482021929099820

$ clang prep.cpp -DVALIDATE && ./a.out
4211739487735230608

Code:

#include <stdio.h>

#ifndef VALIDATE
#define ASSUME(expr) do { [[assume(expr)]]; } while (0)
#else
#include <cassert>
#define ASSUME(expr) do {assert(expr);} while(0)
#endif

unsigned long long seed = 0;
void hash(unsigned long long *s, unsigned long long v) { *s ^= v + 0x9e3779b9 + ((*s)<<6) + ((*s)>>2); }
short var_2 = -25250, var_14 = 12331;
unsigned int var_6 = 2590940443U;
unsigned char var_8 = 91, var_10 = 201, arr_4[11] __attribute__((aligned(64)));
unsigned short arr_0[11]; signed char arr_1[11];
unsigned int arr_2[11][11][11]; unsigned char arr_3[11][11];
#define CF1 ((((int) ((short) var_10 ))) - (200))
#define CF2 (((int) ( var_8 ))) - (80)
__attribute__((noinline))
void test(short var_2, unsigned int var_6, unsigned char var_8, unsigned char var_10, unsigned short arr_0[11], signed char arr_1[11], unsigned int arr_2[11][11][11], unsigned char arr_3[11][11]) {
    ASSUME((short)((CF2)) == (11));
    ASSUME((short)(CF1) == (1));
    for (short i_0 = (int)((short)(2147483592LL << ((3380610699U > var_6 ? 3380610699U : var_6) - 3380610699U))) + 56; i_0 < (CF2); i_0 += CF1) {
        arr_4 [i_0] = (unsigned char)(((arr_2[i_0][7U][0] ? (unsigned long long int)arr_1[0] : ((unsigned long long int)arr_0[1] - 1ULL)) ? 2ULL : ((arr_3[i_0][0] ? var_6 : 1) ? 3ULL : 4ULL)));
        for (unsigned char i_2 = 1; i_2 < 10; i_2 += 3) {
            var_14 ^= arr_2[i_2][0][i_0];
        } 
    }
}
int main() {
    for (int i = 0; i < 11; ++i) {
        arr_0[i] = 38364; arr_1[i] = -105; arr_4[i] = 252;
        for (int j = 0; j < 11; ++j) {
            arr_3[i][j] = 140;
            for (int k = 0; k < 11; ++k) arr_2[i][j][k] = 191914774U;
        }
    }
    test(var_2, var_6, var_8, var_10, arr_0, arr_1, arr_2, arr_3);
    hash(&seed, var_14);
    for (int i = 0; i < 11; ++i) hash(&seed, arr_4[i]);
    printf("%llu\n", seed);
}

Further attempts to reduce suggests that the ternary operator in the outer loop initialization (3380610699U > var_6 ? 3380610699U : var_6) seems to be critical. Simplifying this to a simple variable or constant hides the bug.

thapgua avatar Apr 02 '26 07:04 thapgua

@llvm/issue-subscribers-polly

Author: thapgua

I have probably encountered a piece of code that causes Polly to miscompile the code via `[[assume(expr)]]`.

Reproducer: https://godbolt.org/z/KTz4Pn8fd

compiler version:

clang version 23.0.0git (https://github.com/llvm/llvm-project.git 6f9646a598f25efa3c4db066d2d51fb248b13526)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /data/lenovo/compiler-build/llvm/llvm-project/build/bin
Build config: +assertions

Steps to Reproduce:

  • Save the following code to prep.cpp ;
  • Compile with Polly enabled to trigger the inconsistency;
  • Compile without Polly or -DVALIDATE to change assume to assert, and it returns with correct result.
$ clang prep.cpp &amp;&amp; ./a.out
4211739487735230608

$ clang prep.cpp -mllvm -polly -O3 &amp;&amp; ./a.out
11791482021929099820

$ clang prep.cpp -DVALIDATE &amp;&amp; ./a.out
4211739487735230608

Code:

#include &lt;stdio.h&gt;

#ifndef VALIDATE
#define ASSUME(expr) do { [[assume(expr)]]; } while (0)
#else
#include &lt;cassert&gt;
#define ASSUME(expr) do {assert(expr);} while(0)
#endif

unsigned long long seed = 0;
void hash(unsigned long long *s, unsigned long long v) { *s ^= v + 0x9e3779b9 + ((*s)&lt;&lt;6) + ((*s)&gt;&gt;2); }
short var_2 = -25250, var_14 = 12331;
unsigned int var_6 = 2590940443U;
unsigned char var_8 = 91, var_10 = 201, arr_4[11] __attribute__((aligned(64)));
unsigned short arr_0[11]; signed char arr_1[11];
unsigned int arr_2[11][11][11]; unsigned char arr_3[11][11];
#define CF1 ((((int) ((short) var_10 ))) - (200))
#define CF2 (((int) ( var_8 ))) - (80)
__attribute__((noinline))
void test(short var_2, unsigned int var_6, unsigned char var_8, unsigned char var_10, unsigned short arr_0[11], signed char arr_1[11], unsigned int arr_2[11][11][11], unsigned char arr_3[11][11]) {
    ASSUME((short)((CF2)) == (11));
    ASSUME((short)(CF1) == (1));
    for (short i_0 = (int)((short)(2147483592LL &lt;&lt; ((3380610699U &gt; var_6 ? 3380610699U : var_6) - 3380610699U))) + 56; i_0 &lt; (CF2); i_0 += CF1) {
        arr_4 [i_0] = (unsigned char)(((arr_2[i_0][7U][0] ? (unsigned long long int)arr_1[0] : ((unsigned long long int)arr_0[1] - 1ULL)) ? 2ULL : ((arr_3[i_0][0] ? var_6 : 1) ? 3ULL : 4ULL)));
        for (unsigned char i_2 = 1; i_2 &lt; 10; i_2 += 3) {
            var_14 ^= arr_2[i_2][0][i_0];
        } 
    }
}
int main() {
    for (int i = 0; i &lt; 11; ++i) {
        arr_0[i] = 38364; arr_1[i] = -105; arr_4[i] = 252;
        for (int j = 0; j &lt; 11; ++j) {
            arr_3[i][j] = 140;
            for (int k = 0; k &lt; 11; ++k) arr_2[i][j][k] = 191914774U;
        }
    }
    test(var_2, var_6, var_8, var_10, arr_0, arr_1, arr_2, arr_3);
    hash(&amp;seed, var_14);
    for (int i = 0; i &lt; 11; ++i) hash(&amp;seed, arr_4[i]);
    printf("%llu\n", seed);
}

Further attempts to reduce suggests that the ternary operator in the outer loop initialization (3380610699U &gt; var_6 ? 3380610699U : var_6) seems to be critical. Simplifying this to a simple variable or constant hides the bug.

llvmbot avatar Apr 02 '26 12:04 llvmbot

With "encountered", do you mean that this has been generated by a fuzzer or reduced from a real program? It would be good to know for prioritizing bugs that are blocking users.

This bug happened to be unrelated to [[assume(...)]]: you could just set the value to 1 respectively 11 with the same result.

Meinersbur avatar Apr 04 '26 12:04 Meinersbur

These test cases are fuzzer-generated and then reduced for simplicity. I'll be sure to indicate that more clearly in future reports to help with prioritization.

thapgua avatar Apr 23 '26 09:04 thapgua