<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/54176>54176</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[LoopInterchange] incorrect handling of scalar dependencies and dependence vectors starting with ">"
</td>
</tr>
<tr>
<th>Labels</th>
<td>
loopoptim
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
bmahjour
</td>
</tr>
</table>
<pre>
The following example shows a miscompile:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
__attribute__((noinline))
void foo(int n, int m, float aa[1024][128], float bb[1024][128], float cc[1024][128]) {
#ifndef MYINTERCHANGE
for (int j = 1; j < 128; j++) {
for (int i = 1; i < 1024; i++) {
aa[1][j-1] += bb[i][j];
cc[i][j] = aa[1][j];
}
}
#else
for (int i = 1; i < 1024; i++) {
for (int j = 1; j < 128; j++) {
aa[1][j-1] += bb[i][j];
cc[i][j] = aa[1][j];
}
}
#endif
}
__attribute__((noinline))
void init(int n, int m, float aa[restrict][m]) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
aa[i][j] = i*j;
}
}
}
__attribute__((noinline))
void print(int n, int m, float aa[restrict][m]) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
printf("[%d,%d]=%f\n", i, j, aa[i][j]);
}
}
}
#define N 1024
#define M 128
int main() {
float *f1, *f2, *f3;
f1 = (float *) malloc (N * M * sizeof(float));
f2 = (float *) malloc (N * M * sizeof(float));
f3 = (float *) malloc (N * M * sizeof(float));
memset(f3, 0, N*M*sizeof(float));
init(N,M, (float (*)[])f1);
init(N,M, (float (*)[])f2);
foo(N,M, (float (*)[])f1, (float (*)[])f2, (float (*)[])f3);
print(N,M,(float (*)[])f3);
}
```
```
> clang bad-interchange.c -O3 -o bad -mllvm -enable-loopinterchange
> clang bad-interchange.c -O3 -o good
> good > 1.txt && bad > 2.txt
> diff 1.txt 2.txt | head
258,383c258,383
< [2,1]=1.000000
< [2,2]=2.000000
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzVVs-P2jgU_mvCxQIlNiFwyGGA6W6lHSpVvfQ0cmKHGDk2is1Mt39933MCAXY6w1Q9bJFxnv1--HvfcxwXVvybf6klqazW9lmZLZHfeLPXkrjaPjvCSaNcaZu90jJid1G8juJjP4v71g0pU6bUByFJxFbOC2UndcTuf6LVqnhF3QKSc3XoHx-5B01x8PLxMaJzaMYqo5WREV1gC2ZPVgnIx4JaGU9MRFcEhQaFSlvuCedRukxiOo3SNUoQC4STuiheVZfli-oFibLlkE5lhKzIw9ePmy_3n1d_323-6pMhAK4lPbod5LsmScSWQVwRjIaDiC5DW5AhLCEXvmrwVZ0vYsLRmfOVO-lz76DvxigQtIVIIW3Va_DBLhxD2mfqsPpFtGufKFsfBycRqJHayReouCmdn3FxC4__OypOhBihqn50qXzPlldG-bf2fCvx3Sp9h7K53rcv1CMe6mHeV4x4KEZzWymuGYW17nZv0_jrlO3hnPlzOQvoq5AiBWQRTQVADw_cfQAzraJ0ZVCNuWG3w-6abCTlnSzDroXjDTglm-49vZp9CC_gmUNglisT4F7SF6iGUlcJgkOBHgV2hqtKAkUQ4OSBkRoOX64Spzc4BStj79R3aaujbV_4s1j0N8Zivy9WIxsncUdC5kBBjN0G_B7g_4Zr__qD9eqho--EZ95Bwj0Sio1E_6orvUo-fGdvXfTN0K8bsMu1j2_vcfWbXYe9fHWFeXGS3ZNSc7gZkYKLMSwp27KGsZyUZPyJkbFFBRk3Wj81ZCwNL7Qca2v3Z7a3x9paKwZrHBEUkon_hpnNoIX1cJLi5GAMX5GqN6SdebYiteR9PJoCLSs2Z-VJOvoC7-kS-U-6oyOZxOH3Hz3t9PRSf2RsJHImFmzBR155LXNw-gd4-HjGAxztcNOzbStLT2AKLoJAh62IK7nmLRFyD59DaUol4fZpxDAhyRP42NYR53nr0e1Z-Zrg8QYXRUpHh1bntfd7hzdV-gHaFgwOxQQusDDA-vSP8b61O4gGQ-XcQToQ0mmSzUZ1LqpYymzK0nLBpiLLZrQQfDGfTotZWVEpRpoXcIXJw5FLsc5271UTDuH1SOU0pjRmMUuyZBbPJ4t5JjORVrNplXFeZdE0lnAQ6gnimNh2O2rzAKk4bB0otXLeDUrunNoaGbjE-Pzga9vmRcPrnT20owA_D9h_ALmxDng">