No aliasing optimizations ? -cl-strict-aliasing has no effect

Code like this seems to generate no warnings when -cl-strict-aliasing and -Werror are in effect:

void xx(int * i, float * f)


    i[0] = 0;

    (*f) += 2.2;


__kernel void test(__global float* c)


    float ff = 6.7;

    xx((int *) &ff, &ff);

    c[0] = ff;


It does the right thing though. So it seems that the compiler doesn’t optimize based on aliasing rules, is that right?

Thanks for any input.

-cl-strict-aliasing allows the compiler to apply OpenCL C (in practice C99) aliasing rules to the fullest extent which may allow further optimizations that might break non-compliant (but unfortunately common) code.

However, the compiler is not required to break any code that breaks aliasing rules, and it is further not required to detect and report such errors (that might have been the idea of your -Werror flag), even if some compilers try that (gcc’s -Wstrict-aliasing option, e.g.). The other way around, a compiler is also free to ignore -cl-strict-aliasing (or something similar).

For your simple example, I doubt it is possible to deduce anything. Did you have a look at the PTX assembly? I wouldn’t be surprised if just enough code to write 2.2 to *c is left, as everything can in principle be evaluated at compile time.