restrict keyword ignored by the compiler when not passed as function parameters

97 views Asked by At

I have two functions which do the same

#define RES restrict

void fun1(double * tmp1, int n)
{ // In this function, compiler ignores the restrict keyword
    double * RES a = tmp1;
    double * RES b = tmp1+n;
    double * RES c = tmp1+2*n;

    for (int i =0 ; i < n; ++i) 
    {   
        a[i] += c[i]+b[i]; // loads a,b,c, stores a
        a[i] += c[i]*b[i]; // loads b,c ans stores to a
    }
}


void fun2(double * RES a, double * RES b, double * RES c, int n)
{
    // fun2(tmp1, tmp1+n, tmp1+2*n,n)
    // Compiler takes restrict into account.
    for (int i =0 ; i < n; ++i) 
    {   
        a[i] += c[i]+b[i]; // loads a,b, and 2 x c and stores in a
        a[i] += c[i]*b[i];
    }
}

I compile with gcc-13 with -O3 flag. In fun1, the compiler ignores restrict keyword where as in fun2 it reduced one load and one store instruction. From the previous answers, I see restrict is a hint to compiler, but do we have a way to make fun1 generate similar instructions as fun2 (I mean same as with restrict ?). Here is godbolt link

0

There are 0 answers