[Polly] Probably Miscompile in ISL probably involving UMAX and SCEV
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
-DVALIDATEto changeassumetoassert, 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.
@llvm/issue-subscribers-polly
Author: thapgua
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
-DVALIDATEto changeassumetoassert, 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.
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.
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.