Hi, we have spot a compiler optimization (software pipeline ) bug.
The bug can be reproduced on c6678 EVM with the following code.
please verify this compiler optimization problem, and fix this problem.
Thanks!
#include "stdlib.h"
#include "stdio.h"
// This is the function that have problem.
// if uncomment line 21, then the result will be correct.
void foo(double **a, int n, int *indx, double b[])
{
int i, ip, j;
int ii = -1;
double sum, tmp;
for (i = 0; i<n; i++)
{
ip = indx[i];
sum = b[ip];
b[ip] = b[i];
if (ii >= 0)
{
for (j = ii; j < i; j++)
{
// asm(" ");
sum -= a[i][j] * b[j];
}
}
else if (sum)
ii = i;
b[i] = sum;
}
}
void allocdim2(double***a, int dim)
{
int i;
*a = (double**)malloc(dim*sizeof(double*));
for (i = 0; i < dim; ++i)
{
(*a)[i] = (double*)malloc(dim*sizeof(double));
}
}
void freedim2(double** a, int dim)
{
int i;
for (i = 0; i < dim; ++i)
{
free(a[i]);
}
free(a);
}
void initlizeMat(double ** a, int dim)
{
int i, j;
for (i = 0; i < dim; ++i)
for (j = 0; j < dim; ++j)
{
a[i][j] = rand() % 32 / (0.41 + rand() % 1234);
}
}
int isEqual(double*a, double *b, int dim)
{
int i;
int count = 0;
for (i = 0; i < dim; ++i)
{
if (a[i] != b[i])
{
printf("diff: %lf\n", a[i]-b[i]);
count++;
}
}
return count==0;
}
void VerifyPipeLineError(int dim)
{
double **a, *res1, *res2;
int i;
int *index;
allocdim2(&a, dim);
initlizeMat(a, dim);
res1 = (double*)malloc(dim*sizeof(double));
res2 = (double*)malloc(dim*sizeof(double));
index = (int*)malloc(dim*sizeof(int));
for (i = 0; i < dim; ++i)
{
res1[i] = rand() % 23 / (0.41 + rand() % 1231);
res2[i] = res1[i];
index[i] = rand() % dim;
}
foo(a, dim, index, res1);
foo(a, dim, index, res2);
if (!isEqual(res1, res2, dim))
printf("BAD!!!!!!!\n");
else
printf("good!\n");
freedim2(a, dim);
free(res1);
free(res2);
free(index);
}
void main()
{
int i;
srand(13);
for (i = 100; i < 200; i+=4)
{
printf("Dim :%d, ", i);
VerifyPipeLineError(i);
}
}