This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Compiler Optimization problem/bug

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);
  }
}

  • Note this code uses lots of malloc memory.  And it does not check those malloc results for NULL.  To avoid problems, make the malloc memory pool very large by using the linker option --heap_size=num.  That step  may force you to change the allocation of the .sysmem section.

    With that detail covered, I get good results from this code, even when building with optimization.  

    What compiler version do you use?  What build options?

    Thanks and regards,

    -George

  • Thank you very much for the reply.

    Currently we are using make file for build, and to make the problem easier for reproduce, I create a project and I am supperised that I was not able to reproduce the problem. I will let you know if there is any update.

    Thanks!!