<HTML>
<HEAD>
<TITLE>Alignment of vectors</TITLE>
</HEAD>
<BODY>
<FONT FACE="Calibri, Verdana, Helvetica, Arial"><SPAN STYLE='font-size:11pt'>Consider the following C code:<BR>
<BR>
typedef __attribute__(( ext_vector_type(2) )) float float2;<BR>
typedef __attribute__(( ext_vector_type(2) )) __attribute__(( aligned(4) )) float float2_align2;<BR>
<BR>
void foo(void)<BR>
{<BR>
const float * p;<BR>
size_t offset;<BR>
float2 tmp = *((float2_align2 *)(p+offset));<BR>
}<BR>
<BR>
When compiled with clang —emit-llvm I get:<BR>
<BR>
define void @foo() {<BR>
entry:<BR>
%p = alloca float*, align 4 ; <float**> [#uses=1]<BR>
%offset = alloca i32, align 4 ; <i32*> [#uses=1]<BR>
%tmp = alloca <2 x float>, align 8 ; <<2 x float>*> [#uses=1]<BR>
%tmp1 = load float** %p ; <float*> [#uses=1]<BR>
%tmp2 = load i32* %offset ; <i32> [#uses=1]<BR>
%add.ptr = getelementptr float* %tmp1, i32 %tmp2 ; <float*> [#uses=1]<BR>
%conv = bitcast float* %add.ptr to <2 x float>* ; <<2 x float>*> [#uses=1]<BR>
%tmp3 = load <2 x float>* %conv ; <<2 x float>> [#uses=1]<BR>
store <2 x float> %tmp3, <2 x float>* %tmp<BR>
ret void<BR>
}<BR>
<BR>
The problem is that the load into tmp3 seems to have lost any information that %conv should not be aligned to 8 bytes but rather 4. Of course, GCC only states that the alignment attribute will try and enforce a minimal alignment and so the above code generated by clang is valid but what about if the following code had been generated:<BR>
<BR>
define void @foo() {<BR>
entry:<BR>
%p = alloca float*, align 4 ; <float**> [#uses=1]<BR>
%offset = alloca i32, align 4 ; <i32*> [#uses=1]<BR>
%tmp = alloca <2 x float>, align 4 ; <<2 x float>*> [#uses=1]<BR>
%tmp1 = load float** %p ; <float*> [#uses=1]<BR>
%tmp2 = load i32* %offset ; <i32> [#uses=1]<BR>
%add.ptr = getelementptr float* %tmp1, i32 %tmp2 ; <float*> [#uses=1]<BR>
%conv = bitcast float* %add.ptr to <2 x float>* ; <<2 x float>*> [#uses=1]<BR>
%tmp3 = load <2 x float>* %conv ; <<2 x float>> [#uses=1]<BR>
store <2 x float> %tmp3, <2 x float>* %tmp<BR>
ret void<BR>
}<BR>
<BR>
I’m assuming that, in general, this is not correct as the code:<BR>
<BR>
%tmp3 = load <2 x float>* %conv ; <<2 x float>> [#uses=1]<BR>
<BR>
lacks the now necessary information that conv is unaligned and instead must be written as:<BR>
<BR>
%tmp3 = load <2 x float>*%conv, align 2 ; <<2 x float>>, align2 [#uses=1]<BR>
<BR>
Of course, it is now up to the backend to assure that an unaligned load is handled correctly in hardware, but assuming this is ok, then is this correct?<BR>
<BR>
Thanks,<BR>
<BR>
Ben</SPAN></FONT>
</BODY>
</HTML>