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.

Need volatile keyword for automatic variables?

Hi,

 

I am working with  C28346 DSP and now migrating my code from CCS v3.3 to CCS v5.   

A code which runs well in CCS v3.3 shows strange behaviour in CCS v.5.  After spending one day, I found that problem comes from matrix multiplication function:

void mat_mat_mult(float **m1, float **m2, float **mout, int n, int p, int m)
{
	int i, j, k;

	for(i=0; i < n; i++)
	{
		for(j=0; j < m; j++)
		{
			mout[i][j] = 0.;
			for(k=0; k<m; k++)
			{
				mout[i][j] += m1[i][k]*m2[k][j];
			}
		}
	}
}

 

Obviously, above code runs ok in Debug mode.  But it malfunctions in Release mode with any optimization, -o2,-o3
So I declared index variable 'k' as volatile with some suspicion :
	volatile int k;
 
Then it runs OK.  I don't understand why I should  put volatile keyword in automatic variable.  
As far as I know, valatile is declared for a variable shared by several threads.
 But an automatic variable is used only in fuction.
 
Followings are  code I tested to mulitipliy two matrices A=[1 2; 3 4] and B=[5 6; 7 8].
In order to make code simple, multiplication code is modified to use 2-dim array. 
The answer is C=[19 22; 43 50];  But in Release mode, it gave me C=[21 24; 35 40];
Is there any compiler option to suppress the volatile declaration for automatic variable?
 
 
void main(void)
{
	float A[2][2], B[2][2], C[2][2], C[2][2];

	A[0][0] = 1;	A[0][1] = 2;
	A[1][0] = 3;	A[1][1] = 4;

	B[0][0] = 5;	B[0][1] = 6;
	B[1][0] = 7;	B[1][1] = 8;

	mat_mat_mult(A,B,C,2,2,2);
}

void mat_mat_mult(float m1[][2], float m2[][2], float mout[][2], int n, int p, int m)
{
	int i, j;
	int k;                          // need to declare volatile,	volatile int k;
					
	for(i=0; i < n; i++)
	{
		for(j=0; j < m; j++)
		{
			mout[i][j] = 0.;
			for(k=0; k<m; k++)
			{
				mout[i][j] += m1[i][k]*m2[k][j];
			}
		}
	}
}
 
  • You should not need volatile for that code.  The likely reason volatile is working around the bug is that its presence disables almost all optimization for that variable.

    • Why is the parameter p unused?
    • What version of the compiler are you using? (this is different than the CCS version)
    • Exactly what command-line options are you using?
  • Thanks for reply.

    There is a typo in the last code.  The variable p is used as

                        for(k=0; k<p; k++)

    Even I corrected the typo, still I got same problem.

     

    The compiler verion I used is  C2800 compiler verion 6.0.2 and CCS is v 5.1.0.09000.

     I created a project with default option, so options may be default for release mode.

    I copied flag setting in the properties page:

    -v28 -mt -ml -O2 --define="__IN_DSP__=1" --include_path="C:/ti/ccsv5/tools/compiler/c2000/include" --include_path="../../../include" --diag_warning=225 --display_error_number --float_support=fpu32

     

    Is it the command line option you want to see? 

     

  • I upgraded compiler to v6.2.3.  Then the problem disappeared.

  • I am able to reproduce the problem; thank you for the reproducible test case.  I am not 100% convinced that the problem has been fixed.  I think the fix for another bug (SDSCM00046991) has changed the code enough that the problem isn't exposed.  I have submitted SDSCM00048685 for further analysis of this problem.

  • This issue was fixed with SDSCM00047357.    This is the reason the bug no longer appears after the 6.1.4 release.