[llvm-dev] Aliasing rules difference between GCC and Clang
Friedman, Eli via llvm-dev
llvm-dev at lists.llvm.org
Thu Sep 20 12:25:07 PDT 2018
On 9/20/2018 8:55 AM, Jonas Paulsson via llvm-dev wrote:
>
> Hi,
>
> I found a difference between Clang and GCC in alias handling. This was
> with a benchmark where Clang was considerably slower, and in a hot
> function which does many more loads from the same address due to
> stores between the uses. In other words, a value is loaded and used,
> another value is stored, and then the first value is loaded once again
> before its second use. This happens many times, with three loads
> instead of one for each value. GCC only emits one load.
>
> The values are the arguments to this function:
>
> void su3_projector( su3_vector *a, su3_vector *b, su3_matrix *c ){
> register int i,j;
> register double tmp,tmp2;
> for(i=0;i<3;i++)for(j=0;j<3;j++){
> tmp2 = a->c[i].real * b->c[j].real;
> tmp = a->c[i].imag * b->c[j].imag;
> c->e[i][j].real = tmp + tmp2;
> tmp2 = a->c[i].real * b->c[j].imag;
> tmp = a->c[i].imag * b->c[j].real;
> c->e[i][j].imag = tmp - tmp2;
> }
> }
>
> The types are:
> typedef struct { complex e[3][3]; } su3_matrix;
> typedef struct { complex c[3]; } su3_vector;
>
> So the question here is if the su3_vector and su3_matrix pointers may
> alias? If they may alias, then clang is right in reloading after each
> store. If the standard says they cannot alias, then gcc is right in
> only loading the values once each.
>
> It seems to me that either GCC is too aggressive or LLVM is too
> conservative, but I don't know which one it is... As far as I
> understand, there is the fact of the different struct types of the
> arguments (which means they cannot alias), but also the question if
> su3_vector is included in su3_matrix (which would mean they may alias).
clang currently emits relatively conservative TBAA info... see
CodeGenFunction::EmitLValueForField for the struct handling. It should
be straightforward to add equivalent handling for array indexing.
Not sure about the correctness off the top of my head.
-Eli
--
Employee of Qualcomm Innovation Center, Inc.
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project
More information about the llvm-dev
mailing list